class-learndash-admin-assignment-edit.php
15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/**
* LearnDash Admin Assignment Edit.
*
* @since 3.2.3
* @package LearnDash\Assignment\Edit
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'Learndash_Admin_Post_Edit' ) ) && ( ! class_exists( 'Learndash_Admin_Assignment_Edit' ) ) ) {
/**
* Class LearnDash Admin Assignment Edit.
*
* @since 3.2.3
* @uses Learndash_Admin_Post_Edit
*/
class Learndash_Admin_Assignment_Edit extends Learndash_Admin_Post_Edit {
/**
* Public constructor for class.
*
* @since 3.2.3
*/
public function __construct() {
$this->post_type = learndash_get_post_type_slug( 'assignment' );
parent::__construct();
}
/**
* On Load handler function for this post type edit.
* This function is called by a WP action when the admin
* page 'post.php' or 'post-new.php' are loaded.
*
* @since 3.2.3
*/
public function on_load() {
if ( $this->post_type_check() ) {
parent::on_load();
add_meta_box(
'learndash_assignment_metabox',
esc_html__( 'Assignment', 'learndash' ),
array( $this, 'assignment_metabox_content' ),
learndash_get_post_type_slug( 'assignment' ),
'advanced',
'high'
);
}
}
/**
* Adds approval Link to assignment metabox.
*
* @global WP_Post $post Global post object.
* @global SFWD_LMS $sfwd_lms Global SFWD_LMS object.
*
* @since 3.2.3
*/
public function assignment_metabox_content() {
global $post, $sfwd_lms;
$assignment_course_id = intval( get_post_meta( $post->ID, 'course_id', true ) );
$assignment_lesson_id = intval( get_post_meta( $post->ID, 'lesson_id', true ) );
wp_nonce_field( 'ld-assignment-nonce-' . $post->ID, 'ld-assignment-nonce' );
?>
<div class="sfwd sfwd_options sfwd-assignment_settings">
<div class="sfwd_input " id="sfwd-assignment_course">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;"><a class="sfwd_help_text_link" style="cursor:pointer;" title="<?php esc_html_e( 'Click for Help!', 'learndash' ); ?>" onclick="toggleVisibility('sfwd-assignment_course_tip');"><img src="<?php echo esc_url( LEARNDASH_LMS_PLUGIN_URL ); ?>/assets/images/question.png" /><label class="sfwd_label textinput">
<?php
// translators: placeholder: Course.
echo sprintf( esc_html_x( 'Associated %s', 'placeholder: Course', 'learndash' ), esc_attr( LearnDash_Custom_Label::get_label( 'course' ) ) );
?>
</label></a></span>
<span class="sfwd_option_input"><div class="sfwd_option_div">
<?php
if ( empty( $assignment_course_id ) ) {
?>
<select name="sfwd-assignment_course">
<option value="">
<?php
// translators: placeholder: Course.
echo sprintf( esc_html_x( '-- Select a %s --', 'placeholder: Course', 'learndash' ), esc_attr( LearnDash_Custom_Label::get_label( 'course' ) ) );
?>
</option>
<?php
$cb_courses = array();
if ( ! empty( $assignment_lesson_id ) ) {
$cb_courses = learndash_get_courses_for_step( $assignment_lesson_id, true );
if ( ! empty( $cb_courses ) ) {
$cb_courses = array_keys( $cb_courses );
}
}
$query_courses_args = array(
'post_type' => 'sfwd-courses',
'post_status' => 'any',
'posts_per_page' => -1,
'post__in' => $cb_courses,
'orderby' => 'title',
'order' => 'ASC',
);
$query_courses = new WP_Query( $query_courses_args );
if ( ! empty( $query_courses->posts ) ) {
foreach ( $query_courses->posts as $p ) {
?>
<option value="<?php echo absint( $p->ID ); ?>"><?php echo wp_kses_post( $p->post_title ); ?></option>
<?php
}
}
?>
</select>
<?php
} else {
echo '<p>' . wp_kses_post( get_the_title( $assignment_course_id ) ) . ' (<a href="' . esc_url( get_permalink( $assignment_course_id ) ) . '">' . esc_html__( 'edit', 'learndash' ) . '</a>)</p>';
}
?>
</div><div class="sfwd_help_text_div" style="display:none" id="sfwd-assignment_course_tip"><label class="sfwd_help_text">
<?php
// translators: placeholder: Course.
echo sprintf( esc_html_x( 'Associate with a %s.', 'placeholder: Course', 'learndash' ), esc_attr( LearnDash_Custom_Label::get_label( 'course' ) ) );
?>
</label></div></span><p style="clear:left"></p></div>
</div>
<div class="sfwd sfwd_options sfwd-assignment_settings">
<div class="sfwd_input " id="sfwd-assignment_lesson">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;"><a class="sfwd_help_text_link" style="cursor:pointer;" title="<?php esc_html_e( 'Click for Help!', 'learndash' ); ?>" onclick="toggleVisibility('sfwd-assignment_lesson_tip');"><img src="<?php echo esc_url( LEARNDASH_LMS_PLUGIN_URL . '/assets/images/question.png' ); ?>" /><label class="sfwd_label textinput">
<?php
// translators: placeholder: Lesson.
echo sprintf( esc_html_x( 'Associated %s', 'placeholder: Lesson', 'learndash' ), esc_attr( LearnDash_Custom_Label::get_label( 'lesson' ) ) );
?>
</label></a></span>
<span class="sfwd_option_input"><div class="sfwd_option_div">
<?php
if ( empty( $assignment_lesson_id ) ) {
?>
<select name="sfwd-assignment_lesson">
<option value="">
<?php
// translators: placeholder: Lesson.
echo sprintf( esc_html_x( '-- Select a %s --', 'placeholder: Lesson', 'learndash' ), esc_attr( LearnDash_Custom_Label::get_label( 'lesson' ) ) );
?>
</option>
<?php
if ( ! empty( $assignment_course_id ) ) {
$course_lessons = $sfwd_lms->select_a_lesson_or_topic( $assignment_course_id, true );
if ( ! empty( $course_lessons ) ) {
foreach ( $course_lessons as $l_id => $l_label ) {
?>
<option value="<?php echo esc_attr( $l_id ); ?>"><?php echo esc_html( $l_label ); ?></option>
<?php
}
}
}
?>
</select>
<?php
} else {
echo '<p>' . wp_kses_post( get_the_title( $assignment_lesson_id ) ) . ' (<a href="' . esc_url( get_permalink( $assignment_lesson_id ) ) . '">' . esc_html__( 'edit', 'learndash' ) . '</a>)</p>';
}
?>
</div><div class="sfwd_help_text_div" style="display:none" id="sfwd-assignment_lesson_tip"><label class="sfwd_help_text">
<?php
// translators: placeholder: Lesson.
echo sprintf( esc_html_x( 'Associate with a %s.', 'placeholder: Lesson', 'learndash' ), esc_attr( LearnDash_Custom_Label::get_label( 'lesson' ) ) );
?>
</label></div></span><p style="clear:left"></p></div>
</div>
<div class="sfwd sfwd_options sfwd-assignment_settings">
<div class="sfwd_input " id="sfwd-assignment_status">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;"><a class="sfwd_help_text_link" style="cursor:pointer;" title="<?php esc_html_e( 'Click for Help!', 'learndash' ); ?>" onclick="toggleVisibility('sfwd-assignment_status_tip');"><img src="<?php echo esc_url( LEARNDASH_LMS_PLUGIN_URL . '/assets/images/question.png' ); ?>" /><label class="sfwd_label textinput"><?php esc_html_e( 'Status', 'learndash' ); ?></label></a></span>
<span class="sfwd_option_input"><div class="sfwd_option_div">
<?php
$approval_status_flag = learndash_is_assignment_approved_by_meta( $post->ID );
if ( 1 == $approval_status_flag ) {
$approval_status_label = esc_html__( 'Approved', 'learndash' );
echo '<p>' . esc_html( $approval_status_label ) . '</p>';
} else {
if ( ( learndash_get_setting( $assignment_lesson_id, 'lesson_assignment_points_enabled' ) === 'on' ) && ( intval( learndash_get_setting( $assignment_lesson_id, 'lesson_assignment_points_amount' ) ) > 0 ) ) {
$approval_status_label = esc_html__( 'Not Approved', 'learndash' );
echo '<p>' . esc_html( $approval_status_label ) . '</p>';
} else {
$approve_text = esc_html__( 'Approve', 'learndash' );
echo '<p><input name="assignment-status" type="submit" class="button button-primary button-large" id="publish" value="' . esc_attr( $approve_text ) . '"></p>';
}
}
?>
</div><div class="sfwd_help_text_div" style="display:none" id="sfwd-assignment_status_tip"><label class="sfwd_help_text">
<?php
esc_html_e( 'Assignment status.', 'learndash' );
?>
</label></div></span><p style="clear:left"></p></div>
</div>
<div class="sfwd sfwd_options sfwd-assignment_settings">
<div class="sfwd_input " id="sfwd-assignment_points">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;"><a class="sfwd_help_text_link" style="cursor:pointer;" title="<?php esc_html_e( 'Click for Help!', 'learndash' ); ?>" onclick="toggleVisibility('sfwd-assignment_points_tip');"><img src="<?php echo esc_url( LEARNDASH_LMS_PLUGIN_URL . '/assets/images/question.png' ); ?>" /><label class="sfwd_label textinput"><?php esc_html_e( 'Points', 'learndash' ); ?></label></a></span>
<span class="sfwd_option_input"><div class="sfwd_option_div">
<?php
if ( ( ! empty( $assignment_course_id ) ) && ( ! empty( $assignment_lesson_id ) ) ) {
if ( ( learndash_get_setting( $assignment_lesson_id, 'lesson_assignment_points_enabled' ) === 'on' ) && ( intval( learndash_get_setting( $assignment_lesson_id, 'lesson_assignment_points_amount' ) ) > 0 ) ) {
$max_points = intval( learndash_get_setting( $assignment_lesson_id, 'lesson_assignment_points_amount' ) );
$current_points = intval( get_post_meta( $post->ID, 'points', true ) );
$update_text = learndash_is_assignment_approved_by_meta( $post->ID ) ? esc_html__( 'Update', 'learndash' ) : esc_html__( 'Update & Approve', 'learndash' );
echo '<p>';
echo "<label for='assignment-points'>" .
// translators: placeholder: max points.
sprintf( esc_html_x( 'Awarded Points (Out of %d):', 'placeholder: max points', 'learndash' ), esc_attr( $max_points ) ) . '</label><br />';
echo '<input name="assignment-points" type="number" min="0" max="' . absint( $max_points ) . '" value="' . absint( $current_points ) . '">';
echo '<p><input name="save" type="submit" class="button button-primary button-large" id="publish" value="' . esc_attr( $update_text ) . '"></p>';
echo '</p>';
} else {
echo '<p>' . esc_html__( 'Points not enabled', 'learndash' ) . '</p>';
}
}
?>
</div><div class="sfwd_help_text_div" style="display:none" id="sfwd-assignment_points_tip"><label class="sfwd_help_text"><?php esc_html_e( 'Assignment Points.', 'learndash' ); ?></label></div></span><p style="clear:left"></p></div>
</div>
<?php
$file_link = get_post_meta( $post->ID, 'file_link', true );
if ( ! empty( $file_link ) ) {
?>
<div class="sfwd sfwd_options sfwd-assignment_settings">
<div class="sfwd_input " id="sfwd-assignment_download">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;"><a class="sfwd_help_text_link" style="cursor:pointer;" title="<?php esc_html_e( 'Click for Help!', 'learndash' ); ?>" onclick="toggleVisibility('sfwd-assignment_download_tip');"><img src="<?php echo esc_url( LEARNDASH_LMS_PLUGIN_URL . '/assets/images/question.png' ); ?>" /><label class="sfwd_label textinput"><?php esc_html_e( 'Actions', 'learndash' ); ?></label></a></span>
<span class="sfwd_option_input"><div class="sfwd_option_div">
<?php
// Link handling.
$file_link = get_post_meta( $post->ID, 'file_link', true );
echo "<a href='" . esc_url( $file_link ) . "' target='_blank' class='button'>" . esc_html__( 'Download', 'learndash' ) . '</a>';
?>
</div><div class="sfwd_help_text_div" style="display:none" id="sfwd-assignment_download_tip"><label class="sfwd_help_text"><?php esc_html_e( 'Assignment download.', 'learndash' ); ?></label></div></span><p style="clear:left"></p></div>
</div>
<?php
}
}
/**
* Save metabox handler function.
*
* @since 3.2.3
*
* @param integer $post_id Post ID Question being edited.
* @param object $post WP_Post Question being edited.
* @param boolean $update If update true, else false.
*/
public function save_post( $post_id = 0, $post = null, $update = false ) {
if ( ! $this->post_type_check( $post ) ) {
return false;
}
if ( ! parent::save_post( $post_id, $post, $update ) ) {
return false;
}
$this->assignment_save_metabox_content( $post_id );
}
/**
* Updates assignment points and approval status.
*
* Fires on `save_post` hook.
*
* @since 3.2.3
*
* @param int $assignment_id Assignment ID.
*/
protected function assignment_save_metabox_content( $assignment_id ) {
if ( ! isset( $_POST['ld-assignment-nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ld-assignment-nonce'] ) ), 'ld-assignment-nonce-' . $assignment_id ) ) {
return;
}
$assignment_course_id = intval( get_post_meta( $assignment_id, 'course_id', true ) );
if ( ( empty( $assignment_course_id ) ) && ( isset( $_POST['sfwd-assignment_course'] ) ) && ( ! empty( $_POST['sfwd-assignment_course'] ) ) ) {
update_post_meta( $assignment_id, 'course_id', intval( $_POST['sfwd-assignment_course'] ) );
}
$assignment_lesson_id = intval( get_post_meta( $assignment_id, 'lesson_id', true ) );
if ( ( empty( $assignment_lesson_id ) ) && ( isset( $_POST['sfwd-assignment_lesson'] ) ) && ( ! empty( $_POST['sfwd-assignment_lesson'] ) ) ) {
update_post_meta( $assignment_id, 'lesson_id', intval( $_POST['sfwd-assignment_lesson'] ) );
}
if ( isset( $_POST['assignment-points'] ) ) {
// update points.
$points = intval( $_POST['assignment-points'] );
update_post_meta( $assignment_id, 'points', $points );
// approve assignment.
$assignment_post = get_post( $assignment_id );
$lesson_id = get_post_meta( $assignment_id, 'lesson_id', true );
learndash_approve_assignment( (int) $assignment_post->post_author, $lesson_id, $assignment_post->ID );
} elseif ( ( isset( $_POST['assignment-status'] ) ) && ( esc_html__( 'Approve', 'learndash' ) == $_POST['assignment-status'] ) ) {
// approve assignment.
$assignment_post = get_post( $assignment_id );
$lesson_id = get_post_meta( $assignment_id, 'lesson_id', true );
learndash_approve_assignment( (int) $assignment_post->post_author, $lesson_id, $assignment_post->ID );
}
}
// End of functions.
}
}
new Learndash_Admin_Assignment_Edit();
/**
* Check if Group Leader can edit Essay.
*
* @since 3.4.0
*
* parameters documented in /wp-includes/class-wp-user.php
*/
// phpcs:ignore Squiz.Commenting.FunctionComment
function learndash_group_leader_can_edit_assignment_filter( $allcaps, $cap, $args, $user ) {
global $pagenow, $typenow;
if ( ( 'post.php' !== $pagenow ) && ( 'post-new.php' !== $pagenow ) ) {
return $allcaps;
}
if ( learndash_get_post_type_slug( 'assignment' ) !== $typenow ) {
return $allcaps;
}
if ( ! in_array( 'edit_others_assignments', $cap, true ) ) {
return $allcaps;
}
if ( ( ! isset( $args[2] ) ) || ( empty( $args[2] ) ) || ( get_post_type( $args[2] ) !== learndash_get_post_type_slug( 'assignment' ) ) ) {
return $allcaps;
}
$post_id = absint( $args[2] );
$post = get_post( $post_id );
if ( ( ! isset( $args[1] ) ) || ( empty( $args[1] ) ) || ( ! learndash_is_group_leader_user( $args[1] ) ) ) {
return $allcaps;
}
$gl_user_id = absint( $args[1] );
$course_id = get_post_meta( $post_id, 'course_id', true );
$course_id = absint( $course_id );
if ( ! learndash_check_group_leader_course_user_intersect( $gl_user_id, (int) $post->post_author, $course_id ) ) {
foreach ( $cap as $cap_slug ) {
$allcaps[ $cap_slug ] = false;
}
}
return $allcaps;
}
add_action(
'init',
function () {
if ( learndash_is_group_leader_user() ) {
add_filter( 'user_has_cap', 'learndash_group_leader_can_edit_assignment_filter', 10, 4 );
}
},
10
);