1: <?php
2: if ( ! defined( 'ABSPATH' ) ) exit;
3:
4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Sensei_Lesson {
15: public $token;
16: public $meta_fields;
17:
18: 19: 20: 21:
22: public function __construct () {
23:
24: $this->token = 'lesson';
25:
26:
27: $this->meta_fields = array( 'lesson_prerequisite', 'lesson_course', 'lesson_preview', 'lesson_length', 'lesson_complexity', 'lesson_video_embed' );
28:
29: $this->question_order = '';
30:
31:
32: if ( is_admin() ) {
33:
34:
35: add_action( 'admin_menu', array( $this, 'meta_box_setup' ), 20 );
36: add_action( 'save_post', array( $this, 'meta_box_save' ) );
37: add_action( 'save_post', array( $this, 'quiz_update' ) );
38:
39:
40: add_filter( 'manage_edit-lesson_columns', array( $this, 'add_column_headings' ), 10, 1 );
41: add_action( 'manage_posts_custom_column', array( $this, 'add_column_data' ), 10, 2 );
42:
43:
44: add_action( 'wp_ajax_lesson_update_question', array( $this, 'lesson_update_question' ) );
45: add_action( 'wp_ajax_nopriv_lesson_update_question', array( $this, 'lesson_update_question' ) );
46:
47:
48: add_action( 'wp_ajax_lesson_add_course', array( $this, 'lesson_add_course' ) );
49: add_action( 'wp_ajax_nopriv_lesson_add_course', array( $this, 'lesson_add_course' ) );
50:
51:
52: add_action( 'wp_ajax_lesson_update_grade_type', array( $this, 'lesson_update_grade_type' ) );
53: add_action( 'wp_ajax_nopriv_lesson_update_grade_type', array( $this, 'lesson_update_grade_type' ) );
54:
55:
56: add_action( 'wp_ajax_lesson_update_question_order', array( $this, 'lesson_update_question_order' ) );
57: add_action( 'wp_ajax_nopriv_lesson_update_question_order', array( $this, 'lesson_update_question_order' ) );
58:
59:
60: add_action( 'wp_ajax_lesson_update_question_order_random', array( $this, 'lesson_update_question_order_random' ) );
61: add_action( 'wp_ajax_nopriv_lesson_update_question_order_random', array( $this, 'lesson_update_question_order_random' ) );
62:
63:
64: add_action( 'wp_ajax_question_get_answer_id', array( $this, 'question_get_answer_id' ) );
65: add_action( 'wp_ajax_nopriv_question_get_answer_id', array( $this, 'question_get_answer_id' ) );
66:
67:
68: add_action( 'wp_ajax_lesson_add_multiple_questions', array( $this, 'lesson_add_multiple_questions' ) );
69: add_action( 'wp_ajax_nopriv_lesson_add_multiple_questions', array( $this, 'lesson_add_multiple_questions' ) );
70:
71:
72: add_action( 'wp_ajax_lesson_remove_multiple_questions', array( $this, 'lesson_remove_multiple_questions' ) );
73: add_action( 'wp_ajax_nopriv_lesson_remove_multiple_questions', array( $this, 'lesson_remove_multiple_questions' ) );
74:
75:
76: add_action( 'wp_ajax_get_question_category_limit', array( $this, 'get_question_category_limit' ) );
77: add_action( 'wp_ajax_nopriv_get_question_category_limit', array( $this, 'get_question_category_limit' ) );
78:
79:
80: add_action( 'wp_ajax_lesson_add_existing_questions', array( $this, 'lesson_add_existing_questions' ) );
81: add_action( 'wp_ajax_nopriv_lesson_add_existing_questions', array( $this, 'lesson_add_existing_questions' ) );
82:
83:
84: add_action( 'wp_ajax_filter_existing_questions', array( $this, 'quiz_panel_filter_existing_questions' ) );
85: add_action( 'wp_ajax_nopriv_filter_existing_questions', array( $this, 'quiz_panel_filter_existing_questions' ) );
86:
87:
88: add_action( 'bulk_edit_custom_box', array( $this, 'all_lessons_edit_fields' ), 10, 2 );
89: add_action( 'quick_edit_custom_box', array( $this, 'all_lessons_edit_fields' ), 10, 2 );
90:
91:
92: add_action('manage_lesson_posts_custom_column', array( $this, 'set_quick_edit_admin_defaults'), 11, 2);
93:
94:
95: add_action( 'wp_ajax_save_bulk_edit_book', array( $this, 'save_all_lessons_edit_fields' ) );
96:
97:
98: add_action('save_post', array( __CLASS__, 'flush_rewrite_rules' ) );
99:
100: } else {
101:
102: }
103: }
104:
105: 106: 107: 108: 109: 110:
111: public function meta_box_setup () {
112:
113:
114: add_meta_box( 'lesson-prerequisite', __( 'Lesson Prerequisite', 'woothemes-sensei' ), array( $this, 'lesson_prerequisite_meta_box_content' ), $this->token, 'side', 'default' );
115:
116:
117: add_meta_box( 'lesson-course', __( 'Lesson Course', 'woothemes-sensei' ), array( $this, 'lesson_course_meta_box_content' ), $this->token, 'side', 'default' );
118:
119:
120: add_meta_box( 'lesson-preview', __( 'Lesson Preview', 'woothemes-sensei' ), array( $this, 'lesson_preview_meta_box_content' ), $this->token, 'side', 'default' );
121:
122:
123: add_meta_box( 'lesson-info', __( 'Lesson Information', 'woothemes-sensei' ), array( $this, 'lesson_info_meta_box_content' ), $this->token, 'normal', 'default' );
124:
125:
126: add_meta_box( 'lesson-quiz-settings', __( 'Quiz Settings', 'woothemes-sensei' ), array( $this, 'lesson_quiz_settings_meta_box_content' ), $this->token, 'normal', 'default' );
127:
128:
129: add_meta_box( 'lesson-quiz', __( 'Quiz Questions', 'woothemes-sensei' ), array( $this, 'lesson_quiz_meta_box_content' ), $this->token, 'normal', 'default' );
130:
131:
132: remove_meta_box( 'woothemes-settings', $this->token, 'normal' );
133:
134:
135: add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
136:
137:
138: add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
139:
140: }
141:
142:
143: 144: 145: 146: 147: 148:
149: public function lesson_info_meta_box_content () {
150: global $post;
151:
152: $lesson_length = get_post_meta( $post->ID, '_lesson_length', true );
153: $lesson_complexity = get_post_meta( $post->ID, '_lesson_complexity', true );
154: $complexity_array = $this->lesson_complexities();
155: $lesson_video_embed = get_post_meta( $post->ID, '_lesson_video_embed', true );
156:
157: $html = '';
158:
159: $html .= '<p><label for="lesson_length">' . __( 'Lesson Length in minutes', 'woothemes-sensei' ) . ': </label>';
160: $html .= '<input type="number" id="lesson-length" name="lesson_length" class="small-text" value="' . esc_attr( $lesson_length ) . '" /></p>' . "\n";
161:
162: $html .= '<p><label for="lesson_complexity">' . __( 'Lesson Complexity', 'woothemes-sensei' ) . ': </label>';
163: $html .= '<select id="lesson-complexity-options" name="lesson_complexity" class="chosen_select lesson-complexity-select">';
164: $html .= '<option value="">' . __( 'None', 'woothemes-sensei' ) . '</option>';
165: foreach ($complexity_array as $key => $value){
166: $html .= '<option value="' . esc_attr( $key ) . '"' . selected( $key, $lesson_complexity, false ) . '>' . esc_html( $value ) . '</option>' . "\n";
167: }
168: $html .= '</select></p>' . "\n";
169:
170: $html .= '<p><label for="lesson_video_embed">' . __( 'Video Embed Code', 'woothemes-sensei' ) . ':</label><br/>' . "\n";
171: $html .= '<textarea rows="5" cols="50" name="lesson_video_embed" tabindex="6" id="course-video-embed">' . $lesson_video_embed . '</textarea></p>' . "\n";
172: $html .= '<p>' . __( 'Paste the embed code for your video (e.g. YouTube, Vimeo etc.) in the box above.', 'woothemes-sensei' ) . '</p>';
173:
174: echo $html;
175:
176: }
177:
178: 179: 180: 181: 182: 183:
184: public function lesson_prerequisite_meta_box_content () {
185: global $post;
186:
187: $select_lesson_prerequisite = get_post_meta( $post->ID, '_lesson_prerequisite', true );
188:
189: $post_args = array( 'post_type' => 'lesson',
190: 'posts_per_page' => -1,
191: 'orderby' => 'title',
192: 'order' => 'ASC',
193: 'exclude' => $post->ID,
194: 'suppress_filters' => 0
195: );
196: $posts_array = get_posts( $post_args );
197:
198: $html = '';
199: $html .= wp_nonce_field( 'sensei-save-post-meta','woo_' . $this->token . '_nonce', true, false );
200: if ( count( $posts_array ) > 0 ) {
201: $html .= '<select id="lesson-prerequisite-options" name="lesson_prerequisite" class="chosen_select widefat">' . "\n";
202: $html .= '<option value="">' . __( 'None', 'woothemes-sensei' ) . '</option>';
203: foreach ($posts_array as $post_item){
204: $html .= '<option value="' . esc_attr( absint( $post_item->ID ) ) . '"' . selected( $post_item->ID, $select_lesson_prerequisite, false ) . '>' . esc_html( $post_item->post_title ) . '</option>' . "\n";
205: }
206: $html .= '</select>' . "\n";
207: } else {
208: $html .= '<p>' . esc_html( __( 'No lessons exist yet. Please add some first.', 'woothemes-sensei' ) ) . '</p>';
209: }
210:
211: echo $html;
212: }
213:
214: 215: 216: 217: 218: 219:
220: public function lesson_preview_meta_box_content () {
221: global $post;
222:
223: $lesson_preview = get_post_meta( $post->ID, '_lesson_preview', true );
224: $html = '';
225: $html .= wp_nonce_field( 'sensei-save-post-meta','woo_' . $this->token . '_nonce', true, false );
226:
227: $checked = '';
228: if ( isset( $lesson_preview ) && ( '' != $lesson_preview ) ) {
229: $checked = checked( 'preview', $lesson_preview, false );
230: }
231:
232: $html .= '<label for="lesson_preview">';
233: $html .= '<input type="checkbox" id="lesson_preview" name="lesson_preview" value="preview" ' . $checked . '> ' . __( 'Allow this lesson to be viewed without purchase/login', 'woothemes-sensei' ) . '<br>';
234:
235:
236: echo $html;
237: }
238:
239: 240: 241: 242: 243: 244: 245:
246: public function meta_box_save ( $post_id ) {
247:
248:
249: if ( ( get_post_type( $post_id ) != $this->token ) || !isset( $_POST[ 'woo_' . $this->token . '_nonce'] ) || ! wp_verify_nonce( $_POST[ 'woo_' . $this->token . '_nonce' ], 'sensei-save-post-meta' ) ) {
250: return $post_id;
251: }
252:
253: $post_type = get_post_type_object( get_post_type( $post_id ) );
254:
255: if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) {
256: return $post_id;
257: }
258:
259:
260: if ( 'page' == $_POST[ 'post_type' ] ) {
261:
262: if ( ! current_user_can( 'edit_page', $post_id ) ) {
263:
264: return $post_id;
265:
266: }
267: } else {
268: if ( ! current_user_can( 'edit_post', $post_id ) ) {
269: return $post_id;
270: }
271: }
272:
273:
274: if ( isset($this->meta_fields) && is_array($this->meta_fields) ) {
275:
276: foreach ( $this->meta_fields as $meta_key ) {
277:
278: remove_action( 'save_post', array( $this, 'meta_box_save') );
279: $this->save_post_meta( $meta_key, $post_id );
280:
281: }
282: }
283: }
284:
285:
286: 287: 288: 289: 290: 291:
292: public function quiz_update( $post_id ) {
293: global $post;
294:
295: if ( ( 'lesson' != get_post_type( $post_id ) )|| !isset( $_POST[ 'woo_' . $this->token . '_nonce'] ) || ! wp_verify_nonce( $_POST[ 'woo_' . $this->token . '_nonce' ], 'sensei-save-post-meta') ) {
296: if ( isset($post->ID) ) {
297: return $post->ID;
298: } else {
299: return false;
300: }
301: }
302:
303: if( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
304: return false;
305: }
306:
307:
308: remove_action( 'save_post', array( $this, 'quiz_update' ) );
309:
310: $quiz_id = $this->lesson_quizzes( $post_id, 'any');
311:
312:
313: $_POST = stripslashes_deep( $_POST );
314: if ( isset( $_POST[ 'quiz_id' ] ) && ( 0 < absint( $_POST[ 'quiz_id' ] ) ) ) {
315: $quiz_id = absint( $_POST[ 'quiz_id' ] );
316: }
317: $post_title = esc_html( $_POST[ 'post_title' ] );
318: $post_status = esc_html( $_POST[ 'post_status' ] );
319: $post_content = '';
320:
321:
322: $post_type_args = array( 'post_content' => $post_content,
323: 'post_status' => $post_status,
324: 'post_title' => $post_title,
325: 'post_type' => 'quiz',
326: 'post_parent' => $post_id,
327: );
328:
329: $settings = $this->get_quiz_settings();
330:
331:
332: if ( 0 < $quiz_id ) {
333:
334: $post_type_args[ 'ID' ] = $quiz_id;
335: wp_update_post($post_type_args);
336:
337:
338: update_post_meta( $quiz_id, '_quiz_lesson', $post_id );
339:
340: foreach( $settings as $field ) {
341: if( 'random_question_order' != $field['id'] ) {
342: $value = $this->get_submitted_setting_value( $field );
343: if( isset( $value ) ) {
344: update_post_meta( $quiz_id, '_' . $field['id'], $value );
345: }
346: }
347: }
348:
349:
350: wp_set_post_terms( $quiz_id, array( 'multiple-choice' ), 'quiz-type' );
351: } else {
352:
353: $quiz_id = wp_insert_post($post_type_args);
354:
355:
356: update_post_meta( $quiz_id, '_quiz_lesson', $post_id );
357:
358: foreach( $settings as $field ) {
359: if( 'random_question_order' != $field['id'] ) {
360:
361:
362:
363:
364: if( !isset( $_POST[ $field['id'] ] ) ){
365: continue;
366: }
367:
368: $value = $this->get_submitted_setting_value( $field );
369: if( isset( $value ) ) {
370: add_post_meta( $quiz_id, '_' . $field['id'], $value );
371: }
372: }
373: }
374:
375:
376: wp_set_post_terms( $quiz_id, array( 'multiple-choice' ), 'quiz-type' );
377: }
378:
379:
380: $course_id = get_post_meta( $post_id, '_lesson_course', true );
381: if( $course_id ) {
382: if( ! get_post_meta( $post_id, '_order_' . $course_id, true ) ) {
383: update_post_meta( $post_id, '_order_' . $course_id, 0 );
384: }
385: }
386:
387: update_post_meta( $post_id, '_lesson_quiz', $quiz_id );
388:
389: $quiz_questions = Sensei()->lesson->lesson_quiz_questions( $quiz_id );
390: if( 0 < count( $quiz_questions ) ) {
391: update_post_meta( $post_id, '_quiz_has_questions', '1' );
392: }
393: else {
394: delete_post_meta( $post_id, '_quiz_has_questions' );
395: }
396:
397:
398: add_action( 'save_post', array( $this, 'quiz_update' ) );
399:
400: }
401:
402: public function get_submitted_setting_value( $field = false ) {
403:
404: if( ! $field ) return;
405:
406: $value = false;
407:
408: if( 'quiz_grade_type' == $field['id'] ) {
409: if( isset( $_POST[ $field['id'] ] ) && 'on' == $_POST[ $field['id'] ] ) {
410: $value = 'auto';
411: } else {
412: $value = 'manual';
413: }
414: return $value;
415: }
416:
417: if ( isset( $_POST[ $field['id'] ] ) ) {
418: $value = $_POST[ $field['id'] ];
419: } else {
420: $value = $field['default'];
421: }
422:
423: return $value;
424: }
425:
426: 427: 428: 429: 430: 431: 432: 433:
434: private function save_post_meta( $post_key = '', $post_id = 0 ) {
435:
436: $meta_key = '_' . $post_key;
437:
438:
439:
440: if( !isset( $_POST[ $post_key ] ) ){
441:
442:
443: if( 'lesson_preview' == $post_key ){
444:
445: $_POST[ $post_key ] = '';
446:
447: } else {
448:
449: return false;
450:
451: }
452:
453: }
454:
455:
456: if ( 'lesson_video_embed' == $post_key) {
457: $new_meta_value = esc_html( $_POST[$post_key] );
458: } else {
459: $new_meta_value = ( isset( $_POST[$post_key] ) ? sanitize_html_class( $_POST[$post_key] ) : '' );
460: }
461:
462:
463: if ( 'lesson_preview' == $post_key && isset( $_POST['action'] ) && $_POST['action'] =='inline-save' ) {
464: $new_meta_value = '-1';
465: }
466:
467: if( -1 != $new_meta_value ){
468: return update_post_meta( $post_id, $meta_key, $new_meta_value );
469: }
470:
471: }
472:
473: 474: 475: 476: 477: 478:
479: public function lesson_course_meta_box_content () {
480: global $post;
481:
482: $selected_lesson_course = 0;
483: if ( 0 < $post->ID ) {
484: $selected_lesson_course = get_post_meta( $post->ID, '_lesson_course', true );
485: }
486:
487: if ( isset( $_GET[ 'course_id' ] ) && ( 0 < absint( $_GET[ 'course_id' ] ) ) ) {
488: $selected_lesson_course = absint( $_GET[ 'course_id' ] );
489: }
490:
491: $post_args = array( 'post_type' => 'course',
492: 'posts_per_page' => -1,
493: 'orderby' => 'title',
494: 'order' => 'ASC',
495: 'post_status' => 'any',
496: 'suppress_filters' => 0,
497: );
498: $posts_array = get_posts( $post_args );
499:
500: $html = '';
501:
502: $html .= wp_nonce_field( 'sensei-save-post-meta','woo_' . $this->token . '_nonce', true, false );
503:
504:
505: $drop_down_args = array(
506: 'name'=>'lesson_course',
507: 'id' => 'lesson-course-options'
508: );
509:
510: $courses = WooThemes_Sensei_Course::get_all_courses();
511: $courses_options = array();
512: foreach( $courses as $course ){
513: $courses_options[ $course->ID ] = get_the_title( $course ) ;
514: }
515: $html .= Sensei_Utils::generate_drop_down( $selected_lesson_course, $courses_options, $drop_down_args );
516:
517:
518: if ( current_user_can( 'publish_courses' )) {
519: $html .= '<div id="lesson-course-actions">';
520: $html .= '<p>';
521:
522: $html .= '<a id="lesson-course-add" href="#course-add" class="lesson-add-course">+ ' . __('Add New Course', 'woothemes-sensei' ) . '</a>';
523: $html .= '</p>';
524: $html .= '</div>';
525:
526: $html .= '<div id="lesson-course-details" class="hidden">';
527: $html .= '<p>';
528:
529: $html .= '<label>' . __( 'Course Title' , 'woothemes-sensei' ) . '</label> ';
530: $html .= '<input type="text" id="course-title" name="course_title" value="" size="25" class="widefat" />';
531:
532: $html .= '<label>' . __( 'Description' , 'woothemes-sensei' ) . '</label> ';
533: $html .= '<textarea rows="10" cols="40" id="course-content" name="course_content" value="" size="300" class="widefat"></textarea>';
534:
535: $html .= '<label>' . __( 'Course Prerequisite' , 'woothemes-sensei' ) . '</label> ';
536: $html .= '<select id="course-prerequisite-options" name="course_prerequisite" class="chosen_select widefat">' . "\n";
537: $html .= '<option value="">' . __( 'None', 'woothemes-sensei' ) . '</option>';
538: foreach ($posts_array as $post_item){
539: $html .= '<option value="' . esc_attr( absint( $post_item->ID ) ) . '">' . esc_html( $post_item->post_title ) . '</option>' . "\n";
540: }
541: $html .= '</select>' . "\n";
542:
543: if ( Sensei_WC::is_woocommerce_active() ) {
544:
545: $select_course_woocommerce_product = get_post_meta( $post_item->ID, '_course_woocommerce_product', true );
546:
547: $product_args = array( 'post_type' => array( 'product', 'product_variation' ),
548: 'posts_per_page' => -1,
549: 'orderby' => 'title',
550: 'order' => 'DESC',
551: 'post_status' => array( 'publish', 'private', 'draft' ),
552: 'tax_query' => array(
553: array(
554: 'taxonomy' => 'product_type',
555: 'field' => 'slug',
556: 'terms' => array( 'variable', 'grouped' ),
557: 'operator' => 'NOT IN'
558: )
559: ),
560: 'suppress_filters' => 0
561: );
562: $products_array = get_posts( $product_args );
563: $html .= '<label>' . __( 'WooCommerce Product' , 'woothemes-sensei' ) . '</label> ';
564: $html .= '<select id="course-woocommerce-product-options" name="course_woocommerce_product" class="chosen_select widefat">' . "\n";
565: $html .= '<option value="-">' . __( 'None', 'woothemes-sensei' ) . '</option>';
566: $prev_parent_id = 0;
567: foreach ($products_array as $products_item){
568:
569: if ( 'product_variation' == $products_item->post_type ) {
570: $product_object = get_product( $products_item->ID );
571: $parent_id = wp_get_post_parent_id( $products_item->ID );
572: $product_name = ucwords( woocommerce_get_formatted_variation( $product_object->variation_data, true ) );
573: } else {
574: $parent_id = false;
575: $prev_parent_id = 0;
576: $product_name = $products_item->post_title;
577: }
578:
579:
580: if( $parent_id && $parent_id != $prev_parent_id ) {
581: if( 0 != $prev_parent_id ) {
582: $html .= '</optgroup>';
583: }
584: $html .= '<optgroup label="' . get_the_title( $parent_id ) . '">';
585: $prev_parent_id = $parent_id;
586: } elseif( ! $parent_id && 0 == $prev_parent_id ) {
587: $html .= '</optgroup>';
588: }
589:
590: $html .= '<option value="' . esc_attr( absint( $products_item->ID ) ) . '">' . esc_html( $products_item->post_title ) . '</option>' . "\n";
591: }
592: $html .= '</select>' . "\n";
593: } else {
594:
595: $html .= '<input type="hidden" name="course_woocommerce_product" id="course-woocommerce-product-options" value="-" />';
596: }
597:
598: $html .= '<label>' . __( 'Course Category' , 'woothemes-sensei' ) . '</label> ';
599: $cat_args = array( 'echo' => false, 'hierarchical' => true, 'show_option_none' => __( 'None', 'woothemes-sensei' ), 'taxonomy' => 'course-category', 'orderby' => 'name', 'id' => 'course-category-options', 'name' => 'course_category', 'class' => 'widefat' );
600: $html .= wp_dropdown_categories(apply_filters('widget_course_categories_dropdown_args', $cat_args)) . "\n";
601:
602: $html .= '<a title="' . esc_attr( __( 'Save Course', 'woothemes-sensei' ) ) . '" href="#add-course-metadata" class="lesson_course_save button button-highlighted">' . esc_html( __( 'Add Course', 'woothemes-sensei' ) ) . '</a>';
603: $html .= ' ';
604:
605: $html .= '<a href="#course-add-cancel" class="lesson_course_cancel">' . __( 'Cancel', 'woothemes-sensei' ) . '</a>';
606: $html .= '</p>';
607: $html .= '</div>';
608: }
609:
610:
611: echo $html;
612: }
613:
614: public function quiz_panel( $quiz_id = 0 ) {
615:
616: $html = wp_nonce_field( 'sensei-save-post-meta','woo_' . $this->token . '_nonce', true, false );
617: $html .= '<div id="add-quiz-main">';
618: if ( 0 == $quiz_id ) {
619: $html .= '<p>';
620:
621: $html .= esc_html( __( 'Once you have saved your lesson you will be able to add questions.', 'woothemes-sensei' ) );
622: $html .= '</p>';
623: }
624:
625:
626: $quiz_class = '';
627: if ( 0 == $quiz_id ) {
628: $quiz_class = ' class="hidden"';
629: }
630:
631: $message_class = '';
632:
633:
634: $questions = array();
635: if ( 0 < $quiz_id ) {
636: $questions = $this->lesson_quiz_questions( $quiz_id );
637: }
638:
639: $question_count = 0;
640: foreach( $questions as $question ) {
641:
642: if( $question->post_type == 'multiple_question' ) {
643: $question_number = get_post_meta( $question->ID, 'number', true );
644: $question_count += $question_number;
645: } else {
646: ++$question_count;
647: }
648:
649: }
650:
651:
652: $html .= '<div id="add-quiz-metadata"' . $quiz_class . '>';
653:
654:
655: $html .= '<input type="hidden" name="quiz_id" id="quiz_id" value="' . esc_attr( $quiz_id ) . '" />';
656:
657:
658: if ( 0 == $quiz_id ) {
659: $html .= '<p class="save-note">';
660: $html .= esc_html( __( 'Please save your lesson in order to add questions to your quiz.', 'woothemes-sensei' ) );
661: $html .= '</p>';
662: }
663:
664: $html .= '</div>';
665:
666:
667: $html .= '<div id="add-question-main"' . $quiz_class . '>';
668:
669: $html .= '<div id="add-question-metadata">';
670:
671:
672: $html .= '<input type="hidden" name="question_counter" id="question_counter" value="' . esc_attr( $question_count ) . '" />';
673:
674: $html .= '<table class="widefat" id="sortable-questions">
675: <thead>
676: <tr>
677: <th class="question-count-column">#</th>
678: <th>' . __( 'Question', 'woothemes-sensei' ) . '</th>
679: <th style="width:45px;">' . __( 'Grade', 'woothemes-sensei' ) . '</th>
680: <th style="width:125px;">' . __( 'Type', 'woothemes-sensei' ) . '</th>
681: <th style="width:125px;">' . __( 'Action', 'woothemes-sensei' ) . '</th>
682: </tr>
683: </thead>
684: <tfoot>
685: <tr>
686: <th class="question-count-column">#</th>
687: <th>' . __( 'Question', 'woothemes-sensei' ) . '</th>
688: <th>' . __( 'Grade', 'woothemes-sensei' ) . '</th>
689: <th>' . __( 'Type', 'woothemes-sensei' ) . '</th>
690: <th>' . __( 'Action', 'woothemes-sensei' ) . '</th>
691: </tr>
692: </tfoot>';
693:
694: $message_class = '';
695: if ( 0 < $question_count ) { $message_class = 'hidden'; }
696:
697: $html .= '<tbody id="no-questions-message" class="' . esc_attr( $message_class ) . '">';
698: $html .= '<tr>';
699: $html .= '<td colspan="5">' . __( 'There are no Questions for this Quiz yet. Please add some below.', 'woothemes-sensei' ) . '</td>';
700: $html .= '</tr>';
701: $html .= '</tbody>';
702:
703: if( 0 < $question_count ) {
704: $html .= $this->quiz_panel_questions( $questions );
705: }
706:
707: $html .= '</table>';
708:
709: if( ! isset( $this->question_order ) ) {
710: $this->question_order = '';
711: }
712:
713: $html .= '<input type="hidden" id="question-order" name="question-order" value="' . $this->question_order . '" />';
714:
715: $html .= '</div>';
716:
717:
718: $html .= '<div id="add-question-actions">';
719:
720: $html .= $this->quiz_panel_add();
721:
722: $html .= '</div>';
723:
724: $html .= '</div>';
725:
726: $html .= '</div>';
727:
728: return $html;
729:
730: }
731:
732: public function quiz_panel_questions( $questions = array() ) {
733: global $quiz_questions;
734:
735: $quiz_questions = $questions;
736:
737: $html = '';
738:
739: if( count( $questions ) > 0 ) {
740:
741: $question_class = '';
742: $question_counter = 1;
743:
744: foreach ( $questions as $question ) {
745:
746: $question_id = $question->ID;
747:
748: $question_type = Sensei()->question->get_question_type( $question_id );
749:
750: $multiple_data = array();
751: $question_increment = 1;
752: if( 'multiple_question' == $question->post_type ) {
753: $question_type = 'category';
754:
755: $question_category = get_post_meta( $question->ID, 'category', true );
756: $question_cat = get_term( $question_category, 'question-category' );
757:
758: $question_number = get_post_meta( $question->ID, 'number', true );
759: $question_increment = $question_number;
760:
761: $multiple_data = array( $question_cat->name, $question_number );
762: }
763:
764: if( ! $question_type ) {
765: $question_type = 'multiple-choice';
766: }
767:
768:
769: $html .= $this->quiz_panel_question( $question_type, $question_counter, $question_id, 'quiz', $multiple_data );
770: $question_counter += $question_increment;
771:
772: if( isset( $this->question_order ) && strlen( $this->question_order ) > 0 ) { $this->question_order .= ','; }
773: $this->question_order .= $question_id;
774: }
775: }
776:
777: return $html;
778:
779: }
780:
781: public function quiz_panel_question( $question_type = '', $question_counter = 0, $question_id = 0, $context = 'quiz', $multiple_data = array() ) {
782: global $row_counter, $quiz_questions;
783:
784: $html = '';
785:
786: $question_class = '';
787: if( 'quiz' == $context ) {
788: if( ! $row_counter || ! isset( $row_counter ) ) {
789: $row_counter = 1;
790: }
791: if( $row_counter % 2 ) { $question_class = 'alternate'; }
792: ++$row_counter;
793: }
794:
795: if( $question_id ) {
796:
797: if( $question_type != 'category' ) {
798:
799: $question_grade = Sensei()->question->get_question_grade( $question_id );
800:
801: $question_media = get_post_meta( $question_id, '_question_media', true );
802: $question_media_type = $question_media_thumb = $question_media_link = $question_media_title = '';
803: $question_media_thumb_class = $question_media_link_class = $question_media_delete_class = 'hidden';
804: $question_media_add_button = __( 'Add file', 'woothemes-sensei' );
805: if( 0 < intval( $question_media ) ) {
806: $mimetype = get_post_mime_type( $question_media );
807: if( $mimetype ) {
808: $mimetype_array = explode( '/', $mimetype);
809: if( isset( $mimetype_array[0] ) && $mimetype_array[0] ) {
810: $question_media_delete_class = '';
811: $question_media_type = $mimetype_array[0];
812: if( 'image' == $question_media_type ) {
813: $question_media_thumb = wp_get_attachment_thumb_url( $question_media );
814: if( $question_media_thumb ) {
815: $question_media_thumb_class = '';
816: }
817: }
818: $question_media_url = wp_get_attachment_url( $question_media );
819: if( $question_media_url ) {
820: $attachment = get_post( $question_media );
821: $question_media_title = $attachment->post_title;
822:
823: if( ! $question_media_title ) {
824: $question_media_filename = basename( $question_media_url );
825: $question_media_title = $question_media_filename;
826: }
827: $question_media_link = '<a class="' . $question_media_type . '" href="' . esc_url( $question_media_url ) . '" target="_blank">' . $question_media_title . '</a>';
828: $question_media_link_class = '';
829: }
830:
831: $question_media_add_button = __( 'Change file', 'woothemes-sensei' );
832: }
833: }
834: }
835:
836: $random_order = get_post_meta( $question_id, '_random_order', true );
837: if( ! $random_order ) {
838: $random_order = 'yes';
839: }
840:
841: if( ! $question_type ) { $question_type = 'multiple-choice'; }
842: }
843:
844: $html .= '<tbody class="' . $question_class . '">';
845:
846: if( 'quiz' == $context ) {
847: $html .= '<tr>';
848: if( $question_type != 'category' ) {
849: $question = get_post( $question_id );
850: $html .= '<td class="table-count question-number question-count-column"><span class="number">' . $question_counter . '</span></td>';
851: $html .= '<td>' . esc_html( $question->post_title ) . '</td>';
852: $html .= '<td class="question-grade-column">' . esc_html( $question_grade ) . '</td>';
853: $question_types_filtered = ucwords( str_replace( array( '-', 'boolean' ), array( ' ', __( 'True/False', 'woothemes-sensei' ) ), $question_type ) );
854: $html .= '<td>' . esc_html( $question_types_filtered ) . '</td>';
855: $html .= '<td><a title="' . esc_attr( __( 'Edit Question', 'woothemes-sensei' ) ) . '" href="#question_' . $question_counter .'" class="question_table_edit">' . esc_html( __( 'Edit', 'woothemes-sensei' ) ) . '</a> <a title="' . esc_attr( __( 'Remove Question', 'woothemes-sensei' ) ) . '" href="#add-question-metadata" class="question_table_delete">' . esc_html( __( 'Remove', 'woothemes-sensei' ) ) . '</a></td>';
856:
857: } else {
858:
859: $end_number = intval( $question_counter ) + intval( $multiple_data[1] ) - 1;
860: if( $question_counter == $end_number ) {
861: $row_numbers = $question_counter;
862: } else {
863: $row_numbers = $question_counter . ' - ' . $end_number;
864: }
865: $row_title = sprintf( __( 'Selected from \'%1$s\' ', 'woothemes-sensei' ), $multiple_data[0] );
866:
867: $html .= '<td class="table-count question-number question-count-column"><span class="number hidden">' . $question_counter . '</span><span class="hidden total-number">' . $multiple_data[1] . '</span><span class="row-numbers">' . esc_html( $row_numbers ) . '</span></td>';
868: $html .= '<td>' . esc_html( $row_title ) . '</td>';
869: $html .= '<td class="question-grade-column"></td>';
870: $html .= '<td><input type="hidden" name="question_id" class="row_question_id" id="question_' . $question_counter . '_id" value="' . $question_id . '" /></td>';
871: $html .= '<td><a title="' . esc_attr( __( 'Edit Question', 'woothemes-sensei' ) ) . '" href="#question_' . $question_counter .'" class="question_table_edit" style="visibility:hidden;">' . esc_html( __( 'Edit', 'woothemes-sensei' ) ) . '</a> <a title="' . esc_attr( __( 'Remove Question(s)', 'woothemes-sensei' ) ) . '" href="#add-question-metadata" class="question_multiple_delete" rel="' . $question_id . '">' . esc_html( __( 'Remove', 'woothemes-sensei' ) ) . '</a></td>';
872:
873: }
874: $html .= '</tr>';
875: }
876:
877: if( $question_type != 'category' ) {
878:
879: $edit_class = '';
880: if( 'quiz' == $context ) {
881: $edit_class = 'hidden';
882: }
883:
884: $question = get_post( $question_id );
885: $html .= '<tr class="question-quick-edit ' . esc_attr( $edit_class ) . '">';
886: $html .= '<td colspan="5">';
887: $html .= '<span class="hidden question_original_counter">' . $question_counter . '</span>';
888: $html .= '<div class="question_required_fields">';
889:
890:
891: $html .= '<div>';
892: $html .= '<label for="question_' . $question_counter . '">' . __( 'Question:', 'woothemes-sensei' ) . '</label> ';
893: $html .= '<input type="text" id="question_' . $question_counter . '" name="question" value="' . esc_attr( htmlspecialchars( $question->post_title ) ) . '" size="25" class="widefat" />';
894: $html .= '</div>';
895:
896:
897: $html .= '<div>';
898: $html .= '<label for="question_' . $question_counter . '_desc">' . __( 'Question Description (optional):', 'woothemes-sensei' ) . '</label> ';
899: $html .= '</div>';
900: $html .= '<textarea id="question_' . $question_counter . '_desc" name="question_description" class="widefat" rows="4">' . esc_textarea( $question->post_content ) . '</textarea>';
901:
902:
903: $html .= '<div>';
904: $html .= '<label for="question_' . $question_counter . '_grade">' . __( 'Question grade:', 'woothemes-sensei' ) . '</label> ';
905: $html .= '<input type="number" id="question_' . $question_counter . '_grade" class="question_grade small-text" name="question_grade" min="0" value="' . $question_grade . '" />';
906: $html .= '</div>';
907:
908:
909: if( $question_type == 'multiple-choice' ) {
910: $html .= '<div>';
911: $html .= '<label for="' . $question_counter . '_random_order"><input type="checkbox" name="random_order" class="random_order" id="' . $question_counter . '_random_order" value="yes" ' . checked( $random_order, 'yes', false ) . ' /> ' . __( 'Randomise answer order', 'woothemes-sensei' ) . '</label>';
912: $html .= '</div>';
913: }
914:
915:
916: $html .= '<div>';
917: $html .= '<label for="question_' . $question_counter . '_media_button">' . __( 'Question media:', 'woothemes-sensei' ) . '</label><br/>';
918: $html .= '<button id="question_' . $question_counter . '_media_button" class="upload_media_file_button button-secondary" data-uploader_title="' . __( 'Add file to question', 'woothemes-sensei' ) . '" data-uploader_button_text="' . __( 'Add to question', 'woothemes-sensei' ) . '">' . $question_media_add_button . '</button>';
919: $html .= '<button id="question_' . $question_counter . '_media_button_delete" class="delete_media_file_button button-secondary ' . $question_media_delete_class . '">' . __( 'Delete file', 'woothemes-sensei' ) . '</button><br/>';
920: $html .= '<span id="question_' . $question_counter . '_media_link" class="question_media_link ' . $question_media_link_class . '">' . $question_media_link . '</span>';
921: $html .= '<br/><img id="question_' . $question_counter . '_media_preview" class="question_media_preview ' . $question_media_thumb_class . '" src="' . $question_media_thumb . '" /><br/>';
922: $html .= '<input type="hidden" id="question_' . $question_counter . '_media" class="question_media" name="question_media" value="' . $question_media . '" />';
923: $html .= '</div>';
924:
925: $html .= '</div>';
926:
927: $html .= $this->quiz_panel_question_field( $question_type, $question_id, $question_counter );
928:
929: $html .= '<input type="hidden" id="question_' . $question_counter . '_question_type" class="question_type" name="question_type" value="' . $question_type . '" />';
930: $html .= '<input type="hidden" name="question_id" class="row_question_id" id="question_' . $question_counter . '_id" value="' . $question_id . '" />';
931:
932: if( 'quiz' == $context ) {
933: $html .= '<div class="update-question">';
934: $html .= '<a href="#question-edit-cancel" class="lesson_question_cancel" title="' . esc_attr( __( 'Cancel', 'woothemes-sensei' ) ) . '">' . __( 'Cancel', 'woothemes-sensei' ) . '</a> ';
935: $html .= '<a title="' . esc_attr( __( 'Update Question', 'woothemes-sensei' ) ) . '" href="#add-question-metadata" class="question_table_save button button-highlighted">' . esc_html( __( 'Update', 'woothemes-sensei' ) ) . '</a>';
936: $html .= '</div>';
937: }
938:
939: $html .= '</td>';
940: $html .= '</tr>';
941: }
942:
943: $html .= '</tbody>';
944:
945: }
946:
947: return $html;
948: }
949:
950: public function quiz_panel_add( $context = 'quiz' ) {
951:
952:
953: $html = '<div id="add-new-question">';
954:
955: $question_types = Sensei()->question->question_types();
956:
957: $question_cats = get_terms( 'question-category', array( 'hide_empty' => false ) );
958:
959: if( 'quiz' == $context ) {
960: $html .= '<h2 class="nav-tab-wrapper add-question-tabs">';
961: $html .= '<a id="tab-new" class="nav-tab nav-tab-active">' . __( 'New Question' , 'woothemes-sensei' ) . '</a>';
962: $html .= '<a id="tab-existing" class="nav-tab">' . __( 'Existing Questions' , 'woothemes-sensei' ) . '</a>';
963: if ( ! empty( $question_cats ) && ! is_wp_error( $question_cats ) && ! Sensei()->teacher->is_admin_teacher() ) {
964: $html .= '<a id="tab-multiple" class="nav-tab">' . __( 'Category Questions' , 'woothemes-sensei' ) . '</a>';
965: }
966: $html .= '</h2>';
967: }
968:
969: $html .= '<div class="tab-content" id="tab-new-content">';
970:
971: if( 'quiz' == $context ) {
972: $html .= '<p><em>' . sprintf( __( 'Add a new question to this quiz - your question will also be added to the %1$squestion bank%2$s.', 'woothemes-sensei' ), '<a href="' . admin_url( 'edit.php?post_type=question' ) . '">', '</a>' ) . '</em></p>';
973: }
974:
975: $html .= '<div class="question">';
976: $html .= '<div class="question_required_fields">';
977:
978:
979: $html .= '<p><label>' . __( 'Question:' , 'woothemes-sensei' ) . '</label> ';
980: $html .= '<input type="text" id="add_question" name="question" value="" size="25" class="widefat" /></p>';
981:
982:
983: $html .= '<p>';
984: $html .= '<label for="question_desc">' . __( 'Question Description (optional):', 'woothemes-sensei' ) . '</label> ';
985: $html .= '</p>';
986: $html .= '<textarea id="question_desc" name="question_description" class="widefat" rows="4"></textarea>';
987:
988:
989: $html .= '<p><label>' . __( 'Question Type:' , 'woothemes-sensei' ) . '</label> ';
990: $html .= '<select id="add-question-type-options" name="question_type" class="chosen_select widefat question-type-select">' . "\n";
991: foreach ( $question_types as $type => $label ) {
992: $html .= '<option value="' . esc_attr( $type ) . '">' . esc_html( $label ) . '</option>' . "\n";
993: }
994: $html .= '</select></p>' . "\n";
995:
996:
997: if( 'quiz' == $context ) {
998: if ( ! empty( $question_cats ) && ! is_wp_error( $question_cats ) ) {
999: $html .= '<p><label>' . __( 'Question Category:' , 'woothemes-sensei' ) . '</label> ';
1000: $html .= '<select id="add-question-category-options" name="question_category" class="chosen_select widefat question-category-select">' . "\n";
1001: $html .= '<option value="">' . __( 'None', 'woothemes-sensei' ) . '</option>' . "\n";
1002: foreach( $question_cats as $cat ) {
1003: $html .= '<option value="' . esc_attr( $cat->term_id ) . '">' . esc_html( $cat->name ) . '</option>';
1004: }
1005: $html .= '</select></p>' . "\n";
1006: }
1007: }
1008:
1009:
1010: $html .= '<p><label>' . __( 'Question Grade:' , 'woothemes-sensei' ) . '</label> ';
1011: $html .= '<input type="number" id="add-question-grade" name="question_grade" class="small-text" min="0" value="1" /></p>' . "\n";
1012:
1013:
1014: $html .= '<p class="add_question_random_order">';
1015: $html .= '<label for="add_random_order"><input type="checkbox" name="random_order" class="random_order" id="add_random_order" value="yes" checked="checked" /> ' . __( 'Randomise answer order', 'woothemes-sensei' ) . '</label>';
1016: $html .= '</p>';
1017:
1018:
1019: $html .= '<p>';
1020: $html .= '<label for="question_add_new_media_button">' . __( 'Question media:', 'woothemes-sensei' ) . '</label><br/>';
1021: $html .= '<button id="question_add_new_media_button" class="upload_media_file_button button-secondary" data-uploader_title="' . __( 'Add file to question', 'woothemes-sensei' ) . '" data-uploader_button_text="' . __( 'Add to question', 'woothemes-sensei' ) . '">' . __( 'Add file', 'woothemes-sensei' ) . '</button>';
1022: $html .= '<button id="question_add_new_media_button_delete" class="delete_media_file_button button-secondary hidden">' . __( 'Delete file', 'woothemes-sensei' ) . '</button><br/>';
1023: $html .= '<span id="question_add_new_media_link" class="question_media_link hidden"></span>';
1024: $html .= '<br/><img id="question_add_new_media_preview" class="question_media_preview hidden" src="" /><br/>';
1025: $html .= '<input type="hidden" id="question_add_new_media" class="question_media" name="question_media" value="" />';
1026: $html .= '</p>';
1027:
1028: $html .= '</div>';
1029: $html .= '</div>';
1030:
1031: foreach ( $question_types as $type => $label ) {
1032: $html .= $this->quiz_panel_question_field( $type );
1033: }
1034:
1035: if( 'quiz' == $context ) {
1036: $html .= '<div class="add-question">';
1037: $html .= '<a title="' . esc_attr( __( 'Add Question', 'woothemes-sensei' ) ) . '" href="#add-question-metadata" class="add_question_save button button-primary button-highlighted">' . esc_html( __( 'Add Question', 'woothemes-sensei' ) ) . '</a>';
1038: $html .= '</div>';
1039: }
1040:
1041: $html .= '</div>';
1042:
1043: if( 'quiz' == $context ) {
1044:
1045: $html .= '<div class="tab-content hidden" id="tab-existing-content">';
1046:
1047: $html .= '<p><em>' . sprintf( __( 'Add an existing question to this quiz from the %1$squestion bank%2$s.', 'woothemes-sensei' ), '<a href="' . admin_url( 'edit.php?post_type=question' ) . '">', '</a>' ) . '</em></p>';
1048:
1049: $html .= '<div id="existing-filters" class="alignleft actions">
1050: <select id="existing-status">
1051: <option value="all">' . __( 'All', 'woothemes-sensei' ) . '</option>
1052: <option value="unused">' . __( 'Unused', 'woothemes-sensei' ) . '</option>
1053: <option value="used">' . __( 'Used', 'woothemes-sensei' ) . '</option>
1054: </select>
1055: <select id="existing-type">
1056: <option value="">' . __( 'All Types', 'woothemes-sensei' ) . '</option>';
1057: foreach ( $question_types as $type => $label ) {
1058: $html .= '<option value="' . esc_attr( $type ) . '">' . esc_html( $label ) . '</option>';
1059: }
1060: $html .= '</select>
1061: <select id="existing-category">
1062: <option value="">' . __( 'All Categories', 'woothemes-sensei' ) . '</option>';
1063: foreach( $question_cats as $cat ) {
1064: $html .= '<option value="' . esc_attr( $cat->slug ) . '">' . esc_html( $cat->name ) . '</option>';
1065: }
1066: $html .= '</select>
1067: <input type="text" id="existing-search" placeholder="' . __( 'Search', 'woothemes-sensei' ) . '" />
1068: <a class="button" id="existing-filter-button">' . __( 'Filter', 'woothemes-sensei' ) . '</a>
1069: </div>';
1070:
1071: $html .= '<table id="existing-table" class="widefat">';
1072:
1073: $html .= '<thead>
1074: <tr>
1075: <th scope="col" class="column-cb check-column"><input type="checkbox" /></th>
1076: <th scope="col">' . __( 'Question', 'woothemes-sensei' ) . '</th>
1077: <th scope="col">' . __( 'Type', 'woothemes-sensei' ) . '</th>
1078: <th scope="col">' . __( 'Category', 'woothemes-sensei' ) . '</th>
1079: </tr>
1080: </thead>
1081: <tfoot>
1082: <tr>
1083: <th scope="col" class="check-column"><input type="checkbox" /></th>
1084: <th scope="col">' . __( 'Question', 'woothemes-sensei' ) . '</th>
1085: <th scope="col">' . __( 'Type', 'woothemes-sensei' ) . '</th>
1086: <th scope="col">' . __( 'Category', 'woothemes-sensei' ) . '</th>
1087: </tr>
1088: </tfoot>';
1089: $html .= '<tbody id="existing-questions">';
1090:
1091: $questions = $this->quiz_panel_get_existing_questions();
1092:
1093: $row = 1;
1094: foreach( $questions['questions'] as $question ) {
1095: $html .= $this->quiz_panel_add_existing_question( $question->ID, $row );
1096: ++$row;
1097: }
1098:
1099: $html .= '</tbody>';
1100:
1101: $html .= '</table>';
1102:
1103: $next_class = '';
1104: if( $questions['count'] <= 10 ) {
1105: $next_class = 'hidden';
1106: }
1107:
1108: $html .= '<div id="existing-pagination">';
1109: $html .= '<input type="hidden" id="existing-page" value="1" />';
1110: $html .= '<a class="prev no-paging">← ' . __( 'Previous', 'woothemes-sensei') . '</a> <a class="next ' . esc_attr( $next_class ) . '">' . __( 'Next', 'woothemes-sensei') . ' →</a>';
1111: $html .= '</div>';
1112:
1113: $html .= '<div class="existing-actions">';
1114: $html .= '<a title="' . esc_attr( __( 'Add Selected Question(s)', 'woothemes-sensei' ) ) . '" class="add_existing_save button button-primary button-highlighted">' . esc_html( __( 'Add Selected Question(s)', 'woothemes-sensei' ) ) . '</a></p>';
1115: $html .= '</div>';
1116:
1117: $html .= '</div>';
1118:
1119: if ( ! empty( $question_cats ) && ! is_wp_error( $question_cats ) ) {
1120: $html .= '<div class="tab-content hidden" id="tab-multiple-content">';
1121:
1122: $html .= '<p><em>' . sprintf( __( 'Add any number of questions from a specified category. Edit your question categories %1$shere%2$s.', 'woothemes-sensei' ), '<a href="' . admin_url( 'edit-tags.php?taxonomy=question-category&post_type=question' ) . '">', '</a>' ) . '</em></p>';
1123:
1124: $html .= '<p><select id="add-multiple-question-category-options" name="multiple_category" class="chosen_select widefat question-category-select">' . "\n";
1125: $html .= '<option value="">' . __( 'Select a Question Category', 'woothemes-sensei' ) . '</option>' . "\n";
1126: foreach( $question_cats as $cat ) {
1127: $html .= '<option value="' . esc_attr( $cat->term_id ) . '">' . esc_html( $cat->name ) . '</option>';
1128: }
1129: $html .= '</select></p>' . "\n";
1130:
1131: $html .= '<p>' . __( 'Number of questions:', 'woothemes-sensei' ) . ' <input type="number" min="1" value="1" max="1" id="add-multiple-question-count" class="small-text"/>';
1132:
1133: $html .= '<a title="' . esc_attr( __( 'Add Question(s)', 'woothemes-sensei' ) ) . '" class="add_multiple_save button button-primary button-highlighted">' . esc_html( __( 'Add Question(s)', 'woothemes-sensei' ) ) . '</a></p>';
1134:
1135: $html .= '</div>';
1136: }
1137: }
1138:
1139: $html .= '</div>';
1140:
1141: return $html;
1142: }
1143:
1144: public function quiz_panel_get_existing_questions( $question_status = 'all', $question_type = '', $question_category = '', $question_search = '', $page = 1 ) {
1145:
1146: $args = array(
1147: 'post_type' => 'question',
1148: 'posts_per_page' => 10,
1149: 'post_status' => 'publish',
1150: 'suppress_filters' => 0,
1151: );
1152:
1153: switch( $question_status ) {
1154: case 'unused': $quiz_status = 'NOT EXISTS'; break;
1155: case 'used': $quiz_status = 'EXISTS'; break;
1156: default: $quiz_status = ''; break;
1157: }
1158:
1159: if( $quiz_status ) {
1160: switch( $quiz_status ) {
1161: case 'EXISTS':
1162: $args['meta_query'][] = array(
1163: 'key' => '_quiz_id',
1164: 'compare' => $quiz_status,
1165: );
1166: break;
1167:
1168: case 'NOT EXISTS':
1169: $args['meta_query'][] = array(
1170: 'key' => '_quiz_id',
1171: 'value' => 'bug #23268',
1172: 'compare' => $quiz_status,
1173: );
1174: break;
1175: }
1176: }
1177:
1178: if( $question_type ) {
1179: $args['tax_query'][] = array(
1180: 'taxonomy' => 'question-type',
1181: 'field' => 'slug',
1182: 'terms' => $question_type,
1183: );
1184: }
1185:
1186: if( $question_category ) {
1187: $args['tax_query'][] = array(
1188: 'taxonomy' => 'question-category',
1189: 'field' => 'slug',
1190: 'terms' => $question_category,
1191: );
1192: }
1193:
1194: if( $question_type && $question_category ) {
1195: $args['tax_query']['relation'] = 'AND';
1196: }
1197:
1198: if( $question_search ) {
1199: $args['s'] = $question_search;
1200: }
1201:
1202: if( $page ) {
1203: $args['paged'] = $page;
1204: }
1205:
1206: $qry = new WP_Query( $args );
1207:
1208: 1209: 1210: 1211: 1212: 1213: 1214:
1215: $qry = apply_filters( 'sensei_existing_questions_query_results', $qry );
1216:
1217: $questions['questions'] = $qry->posts;
1218: $questions['count'] = intval( $qry->found_posts );
1219: $questions['page'] = $page;
1220:
1221: return $questions;
1222: }
1223:
1224: public function quiz_panel_add_existing_question( $question_id = 0, $row = 1 ) {
1225:
1226: $html = '';
1227:
1228: if( ! $question_id ) {
1229:
1230: return;
1231:
1232: }
1233:
1234: $existing_class = '';
1235: if( $row % 2 ) {
1236: $existing_class = 'alternate';
1237: }
1238:
1239: $question_type = Sensei()->question->get_question_type( $question_id );
1240:
1241: $question_cat_list = strip_tags( get_the_term_list( $question_id, 'question-category', '', ', ', '' ) );
1242:
1243: $html .= '<tr class="' . esc_attr( $existing_class ) . '">
1244: <td class="cb"><input type="checkbox" value="' . $question_id . '" class="existing-item" /></td>
1245: <td>' . get_the_title( $question_id ) . '</td>
1246: <td>' . esc_html( $question_type ) . '</td>
1247: <td>' . esc_html( $question_cat_list ) . '</td>
1248: </tr>';
1249:
1250: return $html;
1251:
1252: }
1253:
1254: public function quiz_panel_filter_existing_questions() {
1255:
1256: $return = '';
1257:
1258:
1259: $nonce = '';
1260: if( isset( $_POST['filter_existing_questions_nonce'] ) ) {
1261: $nonce = esc_html( $_POST['filter_existing_questions_nonce'] );
1262: }
1263:
1264: if( ! wp_verify_nonce( $nonce, 'filter_existing_questions_nonce' ) ) {
1265: die( $return );
1266: }
1267:
1268:
1269: $data = $_POST['data'];
1270: $question_data = array();
1271: parse_str( $data, $question_data );
1272:
1273: if( 0 < count( $question_data ) ) {
1274:
1275: $question_status = '';
1276: if( isset( $question_data['question_status'] ) ) {
1277: $question_status = $question_data['question_status'];
1278: }
1279:
1280: $question_type = '';
1281: if( isset( $question_data['question_type'] ) ) {
1282: $question_type = $question_data['question_type'];
1283: }
1284:
1285: $question_category = '';
1286: if( isset( $question_data['question_category'] ) ) {
1287: $question_category = $question_data['question_category'];
1288: }
1289:
1290: $question_search = '';
1291: if( isset( $question_data['question_search'] ) ) {
1292: $question_search = $question_data['question_search'];
1293: }
1294:
1295: $question_page = 1;
1296: if( isset( $question_data['question_page'] ) ) {
1297: $question_page = intval( $question_data['question_page'] );
1298: }
1299:
1300: $questions = $this->quiz_panel_get_existing_questions( $question_status, $question_type, $question_category, $question_search, $question_page );
1301:
1302: $row = 1;
1303: $html = '';
1304: foreach( $questions['questions'] as $question ) {
1305: $html .= $this->quiz_panel_add_existing_question( $question->ID, $row );
1306: ++$row;
1307: }
1308:
1309: if( ! $html ) {
1310: $html = '<tr class="alternate">
1311: <td class="no-results" colspan="4"><em>' . __( 'There are no questions matching your search.', 'woothemes-sensei' ) . '</em></td>
1312: </tr>';
1313: }
1314:
1315: $return['html'] = $html;
1316: $return['count'] = $questions['count'];
1317: $return['page'] = $questions['page'];
1318:
1319: wp_send_json( $return );
1320: }
1321:
1322: die( $return );
1323: }
1324:
1325: public function quiz_panel_question_field( $question_type = '', $question_id = 0, $question_counter = 0 ) {
1326:
1327: $html = '';
1328:
1329: if( $question_type ) {
1330:
1331: $right_answer = '';
1332: $wrong_answers = array();
1333: $answer_order_string = '';
1334: $answer_order = array();
1335: if( $question_id ) {
1336: $right_answer = get_post_meta( $question_id, '_question_right_answer', true);
1337: $wrong_answers = get_post_meta( $question_id, '_question_wrong_answers', true);
1338: $answer_order_string = get_post_meta( $question_id, '_answer_order', true );
1339: $answer_order = array_filter( explode( ',', $answer_order_string ) );
1340: $question_class = '';
1341: } else {
1342: $question_id = '';
1343: $question_class = 'answer-fields question_required_fields hidden';
1344: }
1345:
1346: switch ( $question_type ) {
1347: case 'multiple-choice':
1348: $html .= '<div class="question_default_fields multiple-choice-answers ' . str_replace( ' hidden', '', $question_class ) . '">';
1349:
1350: $right_answers = (array) $right_answer;
1351:
1352: $total_right = 0;
1353: if( $question_id ) {
1354: $total_right = get_post_meta( $question_id, '_right_answer_count', true );
1355: }
1356: if( 0 == intval( $total_right ) ) {
1357: $total_right = 1;
1358: }
1359: for ( $i = 0; $i < $total_right; $i++ ) {
1360: if ( !isset( $right_answers[ $i ] ) ) { $right_answers[ $i ] = ''; }
1361: $right_answer_id = $this->get_answer_id( $right_answers[ $i ] );
1362:
1363: $right_answer = '<label class="answer" for="question_' . $question_counter . '_right_answer_' . $i . '"><span>' . __( 'Right:' , 'woothemes-sensei' ) . '</span> <input rel="' . esc_attr( $right_answer_id ) . '" type="text" id="question_' . $question_counter . '_right_answer_' . $i . '" name="question_right_answers[]" value="' . esc_attr( $right_answers[ $i ] ) . '" size="25" class="question_answer widefat" /> <a class="remove_answer_option"></a></label>';
1364: if( $question_id ) {
1365: $answers[ $right_answer_id ] = $right_answer;
1366: } else {
1367: $answers[] = $right_answer;
1368: }
1369: }
1370:
1371:
1372: $total_wrong = 0;
1373: if( $question_id ) {
1374: $total_wrong = get_post_meta( $question_id, '_wrong_answer_count', true );
1375: }
1376: if( 0 == intval( $total_wrong ) ) {
1377: $total_wrong = 1;
1378: }
1379:
1380:
1381: foreach ( $wrong_answers as $i => $answer ){
1382:
1383: $answer_id = $this->get_answer_id( $answer );
1384: $wrong_answer = '<label class="answer" for="question_' . $question_counter . '_wrong_answer_' . $i . '"><span>' . __( 'Wrong:' , 'woothemes-sensei' ) ;
1385: $wrong_answer .= '</span> <input rel="' . esc_attr( $answer_id ) . '" type="text" id="question_' . $question_counter . '_wrong_answer_' . $i ;
1386: $wrong_answer .= '" name="question_wrong_answers[]" value="' . esc_attr( $answer ) . '" size="25" class="question_answer widefat" /> <a class="remove_answer_option"></a></label>';
1387: if( $question_id ) {
1388:
1389: $answers[ $answer_id ] = $wrong_answer;
1390:
1391: } else {
1392:
1393: $answers[] = $wrong_answer;
1394:
1395: }
1396:
1397: }
1398:
1399: $answers_sorted = $answers;
1400: if( $question_id && count( $answer_order ) > 0 ) {
1401: $answers_sorted = array();
1402: foreach( $answer_order as $answer_id ) {
1403: if( isset( $answers[ $answer_id ] ) ) {
1404: $answers_sorted[ $answer_id ] = $answers[ $answer_id ];
1405: unset( $answers[ $answer_id ] );
1406: }
1407: }
1408:
1409: if( count( $answers ) > 0 ) {
1410: foreach( $answers as $id => $answer ) {
1411: $answers_sorted[ $id ] = $answer;
1412: }
1413: }
1414: }
1415:
1416: foreach( $answers_sorted as $id => $answer ) {
1417: $html .= $answer;
1418: }
1419:
1420: $html .= '<input type="hidden" class="answer_order" name="answer_order" value="' . $answer_order_string . '" />';
1421: $html .= '<span class="hidden right_answer_count">' . $total_right . '</span>';
1422: $html .= '<span class="hidden wrong_answer_count">' . $total_wrong . '</span>';
1423:
1424: $html .= '<div class="add_answer_options">';
1425: $html .= '<a class="add_right_answer_option add_answer_option button" rel="' . $question_counter . '">' . __( 'Add right answer', 'woothemes-sensei' ) . '</a>';
1426: $html .= '<a class="add_wrong_answer_option add_answer_option button" rel="' . $question_counter . '">' . __( 'Add wrong answer', 'woothemes-sensei' ) . '</a>';
1427: $html .= '</div>';
1428:
1429: $html .= $this->quiz_panel_question_feedback( $question_counter, $question_id , 'multiple-choice' );
1430:
1431: $html .= '</div>';
1432: break;
1433: case 'boolean':
1434: $html .= '<div class="question_boolean_fields ' . $question_class . '">';
1435: if( $question_id ) {
1436: $field_name = 'question_' . $question_id . '_right_answer_boolean';
1437: } else {
1438: $field_name = 'question_right_answer_boolean';
1439: $right_answer = 'true';
1440: }
1441: $html .= '<label for="question_' . $question_id . '_boolean_true"><input id="question_' . $question_id . '_boolean_true" type="radio" name="' . $field_name . '" value="true" '. checked( $right_answer, 'true', false ) . ' /> ' . __( 'True', 'woothemes-sensei' ) . '</label>';
1442: $html .= '<label for="question_' . $question_id . '_boolean_false"><input id="question_' . $question_id . '_boolean_false" type="radio" name="' . $field_name . '" value="false" '. checked( $right_answer, 'false', false ) . ' /> ' . __( 'False', 'woothemes-sensei' ) . '</label>';
1443:
1444: $html .= $this->quiz_panel_question_feedback( $question_counter, $question_id, 'boolean' );
1445:
1446: $html .= '</div>';
1447: break;
1448: case 'gap-fill':
1449: $gapfill_array = explode( '||', $right_answer );
1450: if ( isset( $gapfill_array[0] ) ) { $gapfill_pre = $gapfill_array[0]; } else { $gapfill_pre = ''; }
1451: if ( isset( $gapfill_array[1] ) ) { $gapfill_gap = $gapfill_array[1]; } else { $gapfill_gap = ''; }
1452: if ( isset( $gapfill_array[2] ) ) { $gapfill_post = $gapfill_array[2]; } else { $gapfill_post = ''; }
1453: $html .= '<div class="question_gapfill_fields ' . $question_class . '">';
1454:
1455: $html .= '<label>' . __( 'Text before the Gap:' , 'woothemes-sensei' ) . '</label> ';
1456: $html .= '<input type="text" id="question_' . $question_counter . '_add_question_right_answer_gapfill_pre" name="add_question_right_answer_gapfill_pre" value="' . $gapfill_pre . '" size="25" class="widefat gapfill-field" />';
1457: $html .= '<label>' . __( 'The Gap:' , 'woothemes-sensei' ) . '</label> ';
1458: $html .= '<input type="text" id="question_' . $question_counter . '_add_question_right_answer_gapfill_gap" name="add_question_right_answer_gapfill_gap" value="' . $gapfill_gap . '" size="25" class="widefat gapfill-field" />';
1459: $html .= '<label>' . __( 'Text after the Gap:' , 'woothemes-sensei' ) . '</label> ';
1460: $html .= '<input type="text" id="question_' . $question_counter . '_add_question_right_answer_gapfill_post" name="add_question_right_answer_gapfill_post" value="' . $gapfill_post . '" size="25" class="widefat gapfill-field" />';
1461: $html .= '<label>' . __( 'Preview:' , 'woothemes-sensei' ) . '</label> ';
1462: $html .= '<p class="gapfill-preview">' . $gapfill_pre . ' <u>' . $gapfill_gap . '</u> ' . $gapfill_post . '</p>';
1463: $html .= '</div>';
1464: break;
1465: case 'multi-line':
1466: $html .= '<div class="question_multiline_fields ' . $question_class . '">';
1467:
1468: if( $question_counter ) {
1469: $field_id = 'question_' . $question_counter . '_add_question_right_answer_multiline';
1470: } else {
1471: $field_id = 'add_question_right_answer_multiline';
1472: }
1473: $html .= '<label>' . __( 'Guide/Teacher Notes for grading the answer' , 'woothemes-sensei' ) . '</label> ';
1474: $html .= '<textarea id="' . $field_id . '" name="add_question_right_answer_multiline" rows="4" cols="40" class="widefat">' . $right_answer . '</textarea>';
1475: $html .= '</div>';
1476: break;
1477: case 'single-line':
1478: $html .= '<div class="question_singleline_fields ' . $question_class . '">';
1479:
1480: if( $question_counter ) {
1481: $field_id = 'question_' . $question_counter . '_add_question_right_answer_singleline';
1482: } else {
1483: $field_id = 'add_question_right_answer_singleline';
1484: }
1485: $html .= '<label>' . __( 'Recommended Answer' , 'woothemes-sensei' ) . '</label> ';
1486: $html .= '<input type="text" id="' . $field_id . '" name="add_question_right_answer_singleline" value="' . $right_answer . '" size="25" class="widefat" />';
1487: $html .= '</div>';
1488: break;
1489: case 'file-upload':
1490: $html .= '<div class="question_fileupload_fields ' . $question_class . '">';
1491: if( $question_counter ) {
1492: $right_field_id = 'question_' . $question_counter . '_add_question_right_answer_fileupload';
1493: $wrong_field_id = 'question_' . $question_counter . '_add_question_wrong_answer_fileupload';
1494: } else {
1495: $right_field_id = 'add_question_right_answer_fileupload';
1496: $wrong_field_id = 'add_question_wrong_answer_fileupload';
1497: }
1498:
1499: $wrong_answer = '';
1500: if( isset( $wrong_answers[0] ) ) {
1501: $wrong_answer = $wrong_answers[0];
1502: }
1503: $html .= '<label>' . __( 'Description for student explaining what needs to be uploaded' , 'woothemes-sensei' ) . '</label> ';
1504: $html .= '<textarea id="' . $wrong_field_id . '" name="add_question_wrong_answer_fileupload" rows="4" cols="40" class="widefat">' . $wrong_answer . '</textarea>';
1505:
1506:
1507: $html .= '<label>' . __( 'Guide/Teacher Notes for grading the upload' , 'woothemes-sensei' ) . '</label> ';
1508: $html .= '<textarea id="' . $right_field_id . '" name="add_question_right_answer_fileupload" rows="4" cols="40" class="widefat">' . $right_answer . '</textarea>';
1509: $html .= '</div>';
1510: break;
1511: }
1512: }
1513:
1514: return $html;
1515: }
1516:
1517: public function quiz_panel_question_feedback( $question_counter = 0, $question_id = 0, $question_type = '' ) {
1518:
1519:
1520: $field_name = 'answer_feedback';
1521: if( 'boolean' == $question_type ){
1522:
1523: $field_name = 'answer_feedback_boolean';
1524:
1525: }elseif( 'multiple-choice' == $question_type ){
1526:
1527: $field_name = 'answer_feedback_multiple_choice';
1528:
1529: }
1530:
1531: if( $question_counter ) {
1532: $field_name = 'answer_' . $question_counter . '_feedback';
1533: }
1534:
1535: $feedback = '';
1536: if( $question_id ) {
1537: $feedback = get_post_meta( $question_id, '_answer_feedback', true );
1538: }
1539:
1540: $html = '<p title="' . __( 'This feedback will be automatically displayed to the student once they have completed the quiz.', 'woothemes-sensei' ) . '">';
1541: $html .= '<label for="' . $field_name . '">' . __( 'Answer Feedback' , 'woothemes-sensei' ) . ':</label>';
1542: $html .= '<textarea id="' . $field_name . '" name="' . $field_name . '" rows="4" cols="40" class="answer_feedback widefat">' . $feedback . '</textarea>';
1543: $html .= '</p>';
1544:
1545: return $html;
1546: }
1547:
1548: public function question_get_answer_id() {
1549: $data = $_POST['data'];
1550: $answer_data = array();
1551: parse_str( $data, $answer_data );
1552: $answer = $answer_data['answer_value'];
1553: $answer_id = $this->get_answer_id( $answer );
1554: echo $answer_id;
1555: die();
1556: }
1557:
1558: public function get_answer_id( $answer = '' ) {
1559:
1560: $answer_id = '';
1561:
1562: if( $answer ) {
1563: $answer_id = md5( $answer );
1564: }
1565:
1566: return $answer_id;
1567:
1568: }
1569:
1570: 1571: 1572: 1573: 1574: 1575:
1576: public function lesson_quiz_meta_box_content () {
1577: global $post;
1578:
1579:
1580: $quiz_id = 0;
1581: $quizzes = array();
1582: if ( 0 < $post->ID ) {
1583: $quiz_id = $this->lesson_quizzes( $post->ID, 'any' );
1584: }
1585:
1586: echo $this->quiz_panel( $quiz_id );
1587:
1588: }
1589:
1590: 1591: 1592: 1593:
1594: public function lesson_quiz_settings_meta_box_content() {
1595: global $post;
1596:
1597: $html = '';
1598:
1599:
1600: $quiz_id = 0;
1601: $lesson_id = $post->ID;
1602: $quizzes = array();
1603: if ( 0 < $lesson_id ) {
1604: $quiz_id = $this->lesson_quizzes( $lesson_id, 'any' );
1605: }
1606:
1607: if( $quiz_id ) {
1608: $html .= $this->quiz_settings_panel( $lesson_id, $quiz_id );
1609: } else {
1610: $html .= '<p><em>' . __( 'There is no quiz for this lesson yet - please add one in the \'Quiz Questions\' box.', 'woothemes-sensei' ) . '</em></p>';
1611: }
1612:
1613: echo $html;
1614: }
1615:
1616: public function quiz_settings_panel( $lesson_id = 0, $quiz_id = 0 ) {
1617:
1618:
1619: $html = '';
1620:
1621: if( ! $lesson_id && ! $quiz_id ) return $html;
1622:
1623: $settings = $this->get_quiz_settings( $quiz_id );
1624:
1625: $html = Sensei()->admin->render_settings( $settings, $quiz_id, 'quiz-settings' );
1626:
1627: return $html;
1628:
1629: }
1630:
1631: public function get_quiz_settings( $quiz_id = 0 ) {
1632:
1633: $disable_passmark = '';
1634: $pass_required = get_post_meta( $quiz_id, '_pass_required', true );
1635: if( ! $pass_required ) {
1636: $disable_passmark = 'hidden';
1637: }
1638:
1639:
1640: $questions = array();
1641: if ( 0 < $quiz_id ) {
1642: $questions = $this->lesson_quiz_questions( $quiz_id );
1643: }
1644:
1645:
1646: $question_count = 0;
1647: foreach( $questions as $question ) {
1648: if( $question->post_type == 'multiple_question' ) {
1649: $question_number = get_post_meta( $question->ID, 'number', true );
1650: $question_count += $question_number;
1651: } else {
1652: ++$question_count;
1653: }
1654: }
1655:
1656: $settings = array(
1657: array(
1658: 'id' => 'pass_required',
1659: 'label' => __( 'Pass required to complete lesson', 'woothemes-sensei' ),
1660: 'description' => __( 'The passmark must be achieved before the lesson is complete.', 'woothemes-sensei' ),
1661: 'type' => 'checkbox',
1662: 'default' => '',
1663: 'checked' => 'on',
1664: ),
1665: array(
1666: 'id' => 'quiz_passmark',
1667: 'label' => __( 'Quiz passmark percentage', 'woothemes-sensei' ),
1668: 'description' => '',
1669: 'type' => 'number',
1670: 'default' => 0,
1671: 'placeholder' => 0,
1672: 'min' => 0,
1673: 'max' => 100,
1674: 'class' => $disable_passmark,
1675: ),
1676: array(
1677: 'id' => 'show_questions',
1678: 'label' => __( 'Number of questions to show', 'woothemes-sensei' ),
1679: 'description' => __( 'Show a random selection of questions from this quiz each time a student views it.', 'woothemes-sensei' ),
1680: 'type' => 'number',
1681: 'default' => '',
1682: 'placeholder' => __( 'All', 'woothemes-sensei' ),
1683: 'min' => 1,
1684: 'max' => $question_count,
1685: ),
1686: array(
1687: 'id' => 'random_question_order',
1688: 'label' => __( 'Randomise question order', 'woothemes-sensei' ),
1689: 'description' => '',
1690: 'type' => 'checkbox',
1691: 'default' => 'no',
1692: 'checked' => 'yes',
1693: ),
1694: array(
1695: 'id' => 'quiz_grade_type',
1696: 'label' => __( 'Grade quiz automatically', 'woothemes-sensei' ),
1697: 'description' => __( 'Grades quiz and displays answer explanation immediately after completion. Only applicable if quiz is limited to Multiple Choice, True/False and Gap Fill questions. Questions that have a grade of zero are skipped during autograding.', 'woothemes-sensei' ),
1698: 'type' => 'checkbox',
1699: 'default' => 'auto',
1700: 'checked' => 'auto',
1701: ),
1702: array(
1703: 'id' => 'enable_quiz_reset',
1704: 'label' => __( 'Allow user to retake the quiz', 'woothemes-sensei' ),
1705: 'description' => __( 'Enables the quiz reset button.', 'woothemes-sensei' ),
1706: 'type' => 'checkbox',
1707: 'default' => '',
1708: 'checked' => 'on',
1709: ),
1710: );
1711:
1712: return apply_filters( 'sensei_quiz_settings', $settings );
1713: }
1714:
1715: 1716: 1717: 1718: 1719: 1720:
1721: public function enqueue_scripts( $hook ) {
1722: global $post_type;
1723:
1724: $allowed_post_types = apply_filters( 'sensei_scripts_allowed_post_types', array( 'lesson', 'course', 'question' ) );
1725: $allowed_post_type_pages = apply_filters( 'sensei_scripts_allowed_post_type_pages', array( 'edit.php', 'post-new.php', 'post.php', 'edit-tags.php' ) );
1726: $allowed_pages = apply_filters( 'sensei_scripts_allowed_pages', array( 'sensei_grading', 'sensei_analysis', 'sensei_learners', 'sensei_updates', 'woothemes-sensei-settings', 'lesson-order' ) );
1727:
1728:
1729: if ( ( ( isset( $post_type ) && in_array( $post_type, $allowed_post_types ) ) && ( isset( $hook ) && in_array( $hook, $allowed_post_type_pages ) ) ) || ( isset( $_GET['page'] ) && in_array( $_GET['page'], $allowed_pages ) ) ) {
1730:
1731: $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
1732:
1733:
1734: wp_enqueue_media();
1735: wp_enqueue_script( 'sensei-lesson-metadata', Sensei()->plugin_url . 'assets/js/lesson-metadata' . $suffix . '.js', array( 'jquery', 'sensei-core-select2' ,'jquery-ui-sortable' ), Sensei()->version, true );
1736: wp_enqueue_script( 'sensei-lesson-chosen', Sensei()->plugin_url . 'assets/chosen/chosen.jquery' . $suffix . '.js', array( 'jquery' ), Sensei()->version, true );
1737: wp_enqueue_script( 'sensei-chosen-ajax', Sensei()->plugin_url . 'assets/chosen/ajax-chosen.jquery' . $suffix . '.js', array( 'jquery', 'sensei-lesson-chosen' ), Sensei()->version, true );
1738:
1739:
1740: if( 'edit.php' == $hook && 'lesson'==$_GET['post_type'] ) {
1741: wp_enqueue_script( 'sensei-lessons-bulk-edit', Sensei()->plugin_url . 'assets/js/admin/lesson-bulk-edit' . $suffix . '.js', array( 'jquery' ), Sensei()->version , true);
1742: }
1743:
1744:
1745: $translation_strings = array( 'right_colon' => __( 'Right:', 'woothemes-sensei' ), 'wrong_colon' => __( 'Wrong:', 'woothemes-sensei' ), 'add_file' => __( 'Add file', 'woothemes-sensei' ), 'change_file' => __( 'Change file', 'woothemes-sensei' ), 'confirm_remove' => __( 'Are you sure you want to remove this question?', 'woothemes-sensei' ), 'confirm_remove_multiple' => __( 'Are you sure you want to remove these questions?', 'woothemes-sensei' ), 'too_many_for_cat' => __( 'You have selected more questions than this category contains - please reduce the number of questions that you are adding.', 'woothemes-sensei' ) );
1746: $ajax_vars = array( 'lesson_update_question_nonce' => wp_create_nonce( 'lesson_update_question_nonce' ), 'lesson_add_course_nonce' => wp_create_nonce( 'lesson_add_course_nonce' ), 'lesson_update_grade_type_nonce' => wp_create_nonce( 'lesson_update_grade_type_nonce' ), 'lesson_update_question_order_nonce' => wp_create_nonce( 'lesson_update_question_order_nonce' ), 'lesson_update_question_order_random_nonce' => wp_create_nonce( 'lesson_update_question_order_random_nonce' ), 'lesson_add_multiple_questions_nonce' => wp_create_nonce( 'lesson_add_multiple_questions_nonce' ), 'lesson_remove_multiple_questions_nonce' => wp_create_nonce( 'lesson_remove_multiple_questions_nonce' ), 'lesson_add_existing_questions_nonce' => wp_create_nonce( 'lesson_add_existing_questions_nonce' ), 'filter_existing_questions_nonce' => wp_create_nonce( 'filter_existing_questions_nonce' ) );
1747: $data = array_merge( $translation_strings, $ajax_vars );
1748: wp_localize_script( 'sensei-lesson-metadata', 'woo_localized_data', $data );
1749:
1750:
1751: if ( is_rtl() ) {
1752: wp_enqueue_script( 'sensei-chosen-rtl', Sensei()->plugin_url . 'assets/chosen/chosen-rtl' . $suffix . '.js', array( 'jquery' ), Sensei()->version, true );
1753: }
1754:
1755: }
1756:
1757: }
1758:
1759: 1760: 1761: 1762: 1763: 1764: 1765:
1766: public function enqueue_styles ( $hook ) {
1767: global $post_type;
1768:
1769: $allowed_post_types = apply_filters( 'sensei_scripts_allowed_post_types', array( 'lesson', 'course', 'question', 'sensei_message' ) );
1770: $allowed_post_type_pages = apply_filters( 'sensei_scripts_allowed_post_type_pages', array( 'edit.php', 'post-new.php', 'post.php', 'edit-tags.php' ) );
1771: $allowed_pages = apply_filters( 'sensei_scripts_allowed_pages', array( 'sensei_grading', 'sensei_analysis', 'sensei_learners', 'sensei_updates', 'woothemes-sensei-settings' ) );
1772:
1773:
1774: if ( ( ( isset( $post_type ) && in_array( $post_type, $allowed_post_types ) ) && ( isset( $hook ) && in_array( $hook, $allowed_post_type_pages ) ) ) || ( isset( $_GET['page'] ) && in_array( $_GET['page'], $allowed_pages ) ) ) {
1775: wp_enqueue_style( 'woothemes-sensei-settings-api', esc_url( Sensei()->plugin_url . 'assets/css/settings.css' ), '', Sensei()->version );
1776: }
1777:
1778: }
1779:
1780: 1781: 1782: 1783: 1784: 1785: 1786:
1787: public function add_column_headings ( $defaults ) {
1788: $new_columns['cb'] = '<input type="checkbox" />';
1789: $new_columns['title'] = _x( 'Lesson Title', 'column name', 'woothemes-sensei' );
1790: $new_columns['lesson-course'] = _x( 'Course', 'column name', 'woothemes-sensei' );
1791: $new_columns['lesson-prerequisite'] = _x( 'Pre-requisite Lesson', 'column name', 'woothemes-sensei' );
1792: if ( isset( $defaults['date'] ) ) {
1793: $new_columns['date'] = $defaults['date'];
1794: }
1795: return $new_columns;
1796: }
1797:
1798: 1799: 1800: 1801: 1802: 1803: 1804: 1805:
1806: public function add_column_data ( $column_name, $id ) {
1807: global $wpdb, $post;
1808:
1809: switch ( $column_name ) {
1810: case 'id':
1811: echo $id;
1812: break;
1813: case 'lesson-course':
1814: $lesson_course_id = get_post_meta( $id, '_lesson_course', true);
1815: if ( 0 < absint( $lesson_course_id ) ) {
1816: echo '<a href="' . esc_url( get_edit_post_link( absint( $lesson_course_id ) ) ) . '" title="' . esc_attr( sprintf( __( 'Edit %s', 'woothemes-sensei' ), get_the_title( absint( $lesson_course_id ) ) ) ) . '">' . get_the_title( absint( $lesson_course_id ) ) . '</a>';
1817: }
1818: break;
1819: case 'lesson-prerequisite':
1820: $lesson_prerequisite_id = get_post_meta( $id, '_lesson_prerequisite', true);
1821: if ( 0 < absint( $lesson_prerequisite_id ) ) {
1822: echo '<a href="' . esc_url( get_edit_post_link( absint( $lesson_prerequisite_id ) ) ) . '" title="' . esc_attr( sprintf( __( 'Edit %s', 'woothemes-sensei' ), get_the_title( absint( $lesson_prerequisite_id ) ) ) ) . '">' . get_the_title( absint( $lesson_prerequisite_id ) ) . '</a>';
1823: }
1824: break;
1825: default:
1826: break;
1827: }
1828: }
1829:
1830: 1831: 1832: 1833: 1834: 1835:
1836: public function lesson_add_course () {
1837: global $current_user;
1838:
1839: if ( isset($_POST['lesson_add_course_nonce']) ) {
1840: $nonce = esc_html( $_POST['lesson_add_course_nonce'] );
1841: }
1842: if ( ! wp_verify_nonce( $nonce, 'lesson_add_course_nonce' )
1843: || ! current_user_can( 'edit_lessons' ) ) {
1844: die('');
1845: }
1846:
1847: $data = $_POST['data'];
1848: $course_data = array();
1849: parse_str($data, $course_data);
1850:
1851: $updated = false;
1852: $current_user = wp_get_current_user();
1853: $question_data['post_author'] = $current_user->ID;
1854: $updated = $this->lesson_save_course($course_data);
1855: echo $updated;
1856: die();
1857: }
1858:
1859: 1860: 1861: 1862: 1863: 1864:
1865: public function lesson_update_question () {
1866: global $current_user;
1867:
1868: if ( isset($_POST['lesson_update_question_nonce']) ) {
1869: $nonce = esc_html( $_POST['lesson_update_question_nonce'] );
1870: }
1871: if ( ! wp_verify_nonce( $nonce, 'lesson_update_question_nonce' )
1872: || ! current_user_can( 'edit_questions' )) {
1873:
1874: die('');
1875:
1876: }
1877:
1878:
1879:
1880:
1881:
1882: $data = wp_unslash( $_POST['data'] );
1883:
1884: $question_data = array();
1885: parse_str($data, $question_data);
1886:
1887: $question_data = wp_slash( $question_data );
1888:
1889: $return = false;
1890:
1891: if ( isset( $question_data['action'] ) && ( $question_data['action'] == 'delete' ) ) {
1892:
1893: $return = $this->lesson_remove_question($question_data);
1894: } else {
1895:
1896: if ( isset( $question_data['quiz_id'] ) && ( 0 < absint( $question_data['quiz_id'] ) ) ) {
1897: $current_user = wp_get_current_user();
1898: $question_data['post_author'] = $current_user->ID;
1899: $question_id = $this->lesson_save_question( $question_data );
1900: $question_type = Sensei()->question->get_question_type( $question_id );
1901:
1902: $question_count = intval( $question_data['question_count'] );
1903: ++$question_count;
1904:
1905: $return = $this->quiz_panel_question( $question_type, $question_count, $question_id );
1906: }
1907: }
1908:
1909: echo $return;
1910:
1911: die();
1912: }
1913:
1914: public function lesson_add_multiple_questions() {
1915:
1916: $return = '';
1917:
1918:
1919: $nonce = '';
1920: if( isset( $_POST['lesson_add_multiple_questions_nonce'] ) ) {
1921: $nonce = esc_html( $_POST['lesson_add_multiple_questions_nonce'] );
1922: }
1923:
1924: if( ! wp_verify_nonce( $nonce, 'lesson_add_multiple_questions_nonce' )
1925: || ! current_user_can( 'edit_lessons' ) ) {
1926: die( $return );
1927: }
1928:
1929:
1930: $data = $_POST['data'];
1931: $question_data = array();
1932: parse_str( $data, $question_data );
1933:
1934: if( is_array( $question_data ) ) {
1935: if( isset( $question_data['quiz_id'] ) && ( 0 < absint( $question_data['quiz_id'] ) ) ) {
1936:
1937: $quiz_id = intval( $question_data['quiz_id'] );
1938: $question_number = intval( $question_data['question_number'] );
1939: $question_category = intval( $question_data['question_category'] );
1940:
1941: $question_counter = intval( $question_data['question_count'] );
1942: ++$question_counter;
1943:
1944: $cat = get_term( $question_category, 'question-category' );
1945:
1946: $post_data = array(
1947: 'post_content' => '',
1948: 'post_status' => 'publish',
1949: 'post_title' => sprintf( __( '%1$s Question(s) from %2$s', 'woothemes-sensei' ), $question_number, $cat->name ),
1950: 'post_type' => 'multiple_question'
1951: );
1952:
1953: $multiple_id = wp_insert_post( $post_data );
1954:
1955: if( $multiple_id && ! is_wp_error( $multiple_id ) ) {
1956: add_post_meta( $multiple_id, 'category', $question_category );
1957: add_post_meta( $multiple_id, 'number', $question_number );
1958: add_post_meta( $multiple_id, '_quiz_id', $quiz_id, false );
1959: add_post_meta( $multiple_id, '_quiz_question_order' . $quiz_id, $quiz_id . '000' . $question_counter );
1960: $lesson_id = get_post_meta( $quiz_id, '_quiz_lesson', true );
1961: update_post_meta( $lesson_id, '_quiz_has_questions', '1' );
1962: $return = $this->quiz_panel_question( 'category', $question_counter, $multiple_id, 'quiz', array( $cat->name, $question_number ) );
1963: }
1964: }
1965: }
1966:
1967: echo $return;
1968:
1969: die();
1970: }
1971:
1972: public function lesson_remove_multiple_questions() {
1973:
1974:
1975: $nonce = '';
1976: if( isset( $_POST['lesson_remove_multiple_questions_nonce'] ) ) {
1977: $nonce = esc_html( $_POST['lesson_remove_multiple_questions_nonce'] );
1978: }
1979:
1980: if( ! wp_verify_nonce( $nonce, 'lesson_remove_multiple_questions_nonce' )
1981: || ! current_user_can( 'edit_lessons' ) || ! isset( $_POST['data'] ) ) {
1982: die('');
1983: }
1984:
1985:
1986: $question_data = array();
1987: parse_str( $_POST['data'], $question_data );
1988:
1989: $question_id_to_remove = $question_data['question_id'];
1990: $quiz_id_to_be_removed_from = $question_data['quiz_id'];
1991:
1992:
1993: $quizzes = get_post_meta( $question_id_to_remove, '_quiz_id', false );
1994: foreach( $quizzes as $quiz_id ) {
1995: if( $quiz_id == $quiz_id_to_be_removed_from ) {
1996: delete_post_meta( $question_id_to_remove, '_quiz_id', $quiz_id );
1997: die( 'Deleted' );
1998: }
1999: }
2000:
2001: die('');
2002: }
2003:
2004: public function get_question_category_limit() {
2005:
2006:
2007: $return = 1;
2008:
2009:
2010: $data = $_POST['data'];
2011: $cat_data = array();
2012: parse_str( $data, $cat_data );
2013:
2014: if( isset( $cat_data['cat'] ) && '' != $cat_data['cat'] ) {
2015: $cat = get_term( $cat_data['cat'], 'question-category' );
2016: if( isset( $cat->count ) ) {
2017: $return = $cat->count;
2018: }
2019: }
2020:
2021: echo $return;
2022:
2023: die('');
2024: }
2025:
2026: public function lesson_add_existing_questions() {
2027:
2028:
2029: $nonce = '';
2030: if( isset( $_POST['lesson_add_existing_questions_nonce'] ) ) {
2031: $nonce = esc_html( $_POST['lesson_add_existing_questions_nonce'] );
2032: }
2033:
2034: if( ! wp_verify_nonce( $nonce, 'lesson_add_existing_questions_nonce' )
2035: || ! current_user_can( 'edit_lessons' ) ) {
2036: die('');
2037: }
2038:
2039:
2040: $data = $_POST['data'];
2041: $question_data = array();
2042: parse_str( $data, $question_data );
2043:
2044: $return = '';
2045:
2046: if( is_array( $question_data ) ) {
2047:
2048: if( isset( $question_data['questions'] ) && '' != $question_data['questions'] ) {
2049:
2050: $questions = explode( ',', trim( $question_data['questions'], ',' ) );
2051: $quiz_id = $question_data['quiz_id'];
2052: $question_count = intval( $question_data['question_count'] );
2053:
2054: foreach( $questions as $question_id ) {
2055:
2056: ++$question_count;
2057:
2058: $quizzes = get_post_meta( $question_id, '_quiz_id', false );
2059: if( ! in_array( $quiz_id, $quizzes ) ) {
2060: add_post_meta( $question_id, '_quiz_id', $quiz_id, false );
2061: $lesson_id = get_post_meta( $quiz_id, '_quiz_lesson', true );
2062: update_post_meta( $lesson_id, '_quiz_has_questions', '1' );
2063: }
2064:
2065: add_post_meta( $question_id, '_quiz_question_order' . $quiz_id, $quiz_id . '000' . $question_count );
2066: $question_type = Sensei()->question->get_question_type( $question_id );
2067:
2068: $return .= $this->quiz_panel_question( $question_type, $question_count, $question_id );
2069: }
2070: }
2071: }
2072:
2073: echo $return;
2074:
2075: die('');
2076: }
2077:
2078: public function lesson_update_grade_type() {
2079:
2080: if ( isset($_POST['lesson_update_grade_type_nonce']) ) {
2081:
2082: $nonce = esc_html( $_POST['lesson_update_grade_type_nonce'] );
2083:
2084: }
2085:
2086: if ( ! wp_verify_nonce( $nonce, 'lesson_update_grade_type_nonce' )
2087: || ! current_user_can( 'edit_lessons' ) ) {
2088:
2089: die('');
2090:
2091: }
2092:
2093:
2094: $data = $_POST['data'];
2095: $quiz_data = array();
2096: parse_str($data, $quiz_data);
2097: update_post_meta( $quiz_data['quiz_id'], '_quiz_grade_type', $quiz_data['quiz_grade_type'] );
2098: die();
2099: }
2100:
2101: public function lesson_update_question_order() {
2102:
2103: if ( isset($_POST['lesson_update_question_order_nonce']) ) {
2104: $nonce = esc_html( $_POST['lesson_update_question_order_nonce'] );
2105: }
2106:
2107: if ( ! wp_verify_nonce( $nonce, 'lesson_update_question_order_nonce' )
2108: ||! current_user_can( 'edit_lessons' ) ) {
2109: die('');
2110: }
2111:
2112:
2113: $data = $_POST['data'];
2114: $quiz_data = array();
2115: parse_str($data, $quiz_data);
2116: if( strlen( $quiz_data['question_order'] ) > 0 ) {
2117: $questions = explode( ',', $quiz_data['question_order'] );
2118: $o = 1;
2119: foreach( $questions as $question_id ) {
2120: update_post_meta( $question_id, '_quiz_question_order' . $quiz_data['quiz_id'], $quiz_data['quiz_id'] . '000' . $o );
2121: ++$o;
2122: }
2123: update_post_meta( $quiz_data['quiz_id'], '_question_order', $questions );
2124: }
2125: die();
2126: }
2127:
2128: public function lesson_update_question_order_random() {
2129:
2130: if ( isset($_POST['lesson_update_question_order_random_nonce']) ) {
2131: $nonce = esc_html( $_POST['lesson_update_question_order_random_nonce'] );
2132: }
2133: if ( ! wp_verify_nonce( $nonce, 'lesson_update_question_order_random_nonce' )
2134: || ! current_user_can( 'edit_lessons' ) ) {
2135:
2136: die('');
2137:
2138: }
2139:
2140: $data = $_POST['data'];
2141: $quiz_data = array();
2142: parse_str($data, $quiz_data);
2143: update_post_meta( $quiz_data['quiz_id'], '_random_question_order', $quiz_data['random_question_order'] );
2144: die();
2145: }
2146:
2147: 2148: 2149: 2150: 2151: 2152: 2153:
2154: private function lesson_save_course( $data = array() ) {
2155: global $current_user;
2156: $return = false;
2157:
2158: $course_id = 0;
2159: $course_content = '';
2160: $course_title = '';
2161: $course_prerequisite = 0;
2162: $course_category = 0;
2163: if ( isset( $data[ 'course_id' ] ) && ( 0 < absint( $data[ 'course_id' ] ) ) ) {
2164: $course_id = absint( $data[ 'course_id' ] );
2165: }
2166: if ( isset( $data[ 'course_title' ] ) && ( '' != $data[ 'course_title' ] ) ) {
2167: $course_title = $data[ 'course_title' ];
2168: }
2169: $post_title = $course_title;
2170: if ( isset($data[ 'post_author' ]) ) {
2171: $post_author = $data[ 'post_author' ];
2172: } else {
2173: $current_user = wp_get_current_user();
2174: $post_author = $current_user->ID;
2175: }
2176: $post_status = 'publish';
2177: $post_type = 'course';
2178: if ( isset( $data[ 'course_content' ] ) && ( '' != $data[ 'course_content' ] ) ) {
2179: $course_content = $data[ 'course_content' ];
2180: }
2181: $post_content = $course_content;
2182:
2183: $post_type_args = array( 'post_content' => $post_content,
2184: 'post_status' => $post_status,
2185: 'post_title' => $post_title,
2186: 'post_type' => $post_type
2187: );
2188:
2189: if ( $post_title != '' ) {
2190:
2191: $course_prerequisite_id = absint( $data[ 'course_prerequisite' ] );
2192: $course_woocommerce_product_id = absint( $data[ 'course_woocommerce_product' ] );
2193: $course_category_id = absint( $data[ 'course_category' ] );
2194: if ( 0 == $course_woocommerce_product_id ) { $course_woocommerce_product_id = '-'; }
2195:
2196: if ( 0 < $course_id ) {
2197: $post_type_args[ 'ID' ] = $course_id;
2198: $course_id = wp_update_post($post_type_args);
2199: update_post_meta( $course_id, '_course_prerequisite', $course_prerequisite_id );
2200: update_post_meta( $course_id, '_course_woocommerce_product', $course_woocommerce_product_id );
2201: if ( 0 < $course_category_id ) {
2202: wp_set_object_terms( $course_id, $course_category_id, 'course-category' );
2203: }
2204: } else {
2205: $course_id = wp_insert_post($post_type_args);
2206: add_post_meta( $course_id, '_course_prerequisite', $course_prerequisite_id );
2207: add_post_meta( $course_id, '_course_woocommerce_product', $course_woocommerce_product_id );
2208: if ( 0 < $course_category_id ) {
2209: wp_set_object_terms( $course_id, $course_category_id, 'course-category' );
2210: }
2211: }
2212: }
2213:
2214: if ( 0 < $course_id ) {
2215: $return = $course_id;
2216: }
2217: return $return;
2218: }
2219:
2220:
2221: 2222: 2223: 2224: 2225: 2226: 2227:
2228: public function lesson_save_question( $data = array(), $context = 'quiz' ) {
2229: $return = false;
2230:
2231:
2232: $question_id = 0;
2233: $question_text = '';
2234: $question_right_answer = '';
2235: $question_wrong_answers = $question_right_answers = array();
2236: $question_type = 'multiple-choice';
2237: $question_category = '';
2238:
2239:
2240: if ( isset( $data[ 'question_type' ] ) && ( '' != $data[ 'question_type' ] ) ) {
2241: $question_type = $data[ 'question_type' ];
2242: }
2243:
2244: if ( isset( $data[ 'question_category' ] ) && ( '' != $data[ 'question_category' ] ) ) {
2245: $question_category = $data[ 'question_category' ];
2246: }
2247:
2248: if ( isset( $data[ 'question_id' ] ) && ( 0 < absint( $data[ 'question_id' ] ) ) ) {
2249: $question_id = absint( $data[ 'question_id' ] );
2250: }
2251: if ( isset( $data[ 'question' ] ) && ( '' != $data[ 'question' ] ) ) {
2252: $question_text = $data[ 'question' ];
2253: }
2254: $post_title = $question_text;
2255:
2256: if ( 'multiple-choice' == $question_type && isset( $data[ 'question_right_answers' ] ) && ( '' != $data[ 'question_right_answers' ] ) ) {
2257: $question_right_answers = $data[ 'question_right_answers' ];
2258: }
2259: elseif ( 'multiple-choice' == $question_type && isset( $data[ 'question_right_answer' ] ) && ( '' != $data[ 'question_right_answer' ] ) ) {
2260: $question_right_answer = $data[ 'question_right_answer' ];
2261: }
2262: if ( 'multiple-choice' == $question_type && isset( $data[ 'question_wrong_answers' ] ) && ( '' != $data[ 'question_wrong_answers' ] ) ) {
2263: $question_wrong_answers = $data[ 'question_wrong_answers' ];
2264: }
2265:
2266: if ( 'boolean' == $question_type && isset( $data[ 'question_' . $question_id . '_right_answer_boolean' ] ) && ( '' != $data[ 'question_' . $question_id . '_right_answer_boolean' ] ) ) {
2267: $question_right_answer = $data[ 'question_' . $question_id . '_right_answer_boolean' ];
2268: }
2269:
2270: if ( 'boolean' == $question_type && isset( $data[ 'question_right_answer_boolean' ] ) && ( '' != $data[ 'question_right_answer_boolean' ] ) ) {
2271: $question_right_answer = $data[ 'question_right_answer_boolean' ];
2272: }
2273:
2274: if ( 'gap-fill' == $question_type && isset( $data[ 'add_question_right_answer_gapfill_gap' ] ) && '' != $data[ 'add_question_right_answer_gapfill_gap' ] ) {
2275: $question_right_answer = $data[ 'add_question_right_answer_gapfill_pre' ] . '||' . $data[ 'add_question_right_answer_gapfill_gap' ] . '||' . $data[ 'add_question_right_answer_gapfill_post' ];
2276: }
2277:
2278: if ( 'multi-line' == $question_type && isset( $data[ 'add_question_right_answer_multiline' ] ) && ( '' != $data[ 'add_question_right_answer_multiline' ] ) ) {
2279: $question_right_answer = $data[ 'add_question_right_answer_multiline' ];
2280: }
2281:
2282: if ( 'single-line' == $question_type && isset( $data[ 'add_question_right_answer_singleline' ] ) && ( '' != $data[ 'add_question_right_answer_singleline' ] ) ) {
2283: $question_right_answer = $data[ 'add_question_right_answer_singleline' ];
2284: }
2285:
2286: if ( 'file-upload' == $question_type && isset( $data[ 'add_question_right_answer_fileupload' ] ) && ( '' != $data[ 'add_question_right_answer_fileupload' ] ) ) {
2287: $question_right_answer = $data[ 'add_question_right_answer_fileupload' ];
2288: }
2289: if ( 'file-upload' == $question_type && isset( $data[ 'add_question_wrong_answer_fileupload' ] ) && ( '' != $data[ 'add_question_wrong_answer_fileupload' ] ) ) {
2290: $question_wrong_answers = array( $data[ 'add_question_wrong_answer_fileupload' ] );
2291: }
2292:
2293:
2294: if ( isset( $data[ 'question_grade' ] ) && ( '' != $data[ 'question_grade' ] ) ) {
2295: $question_grade = $data[ 'question_grade' ];
2296: }
2297:
2298:
2299: $answer_feedback = '';
2300: if ( isset( $data[ 'answer_feedback_boolean' ] ) && !empty( $data[ 'answer_feedback_boolean' ] ) ) {
2301:
2302: $answer_feedback = $data[ 'answer_feedback_boolean' ];
2303:
2304: }elseif( isset( $data[ 'answer_feedback_multiple_choice' ] ) && !empty( $data[ 'answer_feedback_multiple_choice' ] ) ){
2305:
2306: $answer_feedback = $data[ 'answer_feedback_multiple_choice' ];
2307:
2308: }elseif( isset( $data[ 'answer_feedback' ] ) ){
2309:
2310: $answer_feedback = $data[ 'answer_feedback' ];
2311:
2312: }
2313:
2314: $post_title = $question_text;
2315: $post_author = $data[ 'post_author' ];
2316: $post_status = 'publish';
2317: $post_type = 'question';
2318:
2319: if ( isset( $data[ 'question_description' ] ) && ( '' != $data[ 'question_description' ] ) ) {
2320: $post_content = $data[ 'question_description' ];
2321: }
2322: else {
2323: $post_content = '';
2324: }
2325:
2326: $post_type_args = array( 'post_content' => $post_content,
2327: 'post_status' => $post_status,
2328: 'post_title' => $post_title,
2329: 'post_type' => $post_type
2330: );
2331:
2332:
2333: if ( is_array( $question_right_answers ) && 0 < count($question_right_answers) ) {
2334: $question_right_answers_array = array_values( array_filter( $question_right_answers, 'strlen' ) );
2335: $question_right_answers = array();
2336:
2337: foreach( $question_right_answers_array as $answer ) {
2338: if( ! in_array( $answer, $question_right_answers ) ) {
2339: $question_right_answers[] = $answer;
2340: }
2341: }
2342: if ( 0 < count($question_right_answers) ) {
2343: $question_right_answer = $question_right_answers;
2344: }
2345: }
2346: $right_answer_count = count( $question_right_answer );
2347:
2348:
2349: if ( is_array( $question_wrong_answers ) ) {
2350: $question_wrong_answers_array = array_values( array_filter( $question_wrong_answers, 'strlen' ) );
2351: $question_wrong_answers = array();
2352: }
2353:
2354: foreach( $question_wrong_answers_array as $answer ) {
2355: if( ! in_array( $answer, $question_wrong_answers ) ) {
2356: $question_wrong_answers[] = $answer;
2357: }
2358: }
2359:
2360: $wrong_answer_count = count( $question_wrong_answers );
2361:
2362:
2363: if ( $post_title != '' ) {
2364:
2365:
2366: $quiz_id = $data['quiz_id'];
2367:
2368:
2369: $question_media = $data['question_media'];
2370:
2371:
2372: $answer_order = '';
2373: if( isset( $data['answer_order'] ) ) {
2374: $answer_order = $data['answer_order'];
2375: }
2376:
2377:
2378: $random_order = 'no';
2379: if( isset( $data['random_order'] ) ) {
2380: $random_order = $data['random_order'];
2381: }
2382:
2383:
2384: if ( 0 < $question_id ) {
2385:
2386: $post_type_args[ 'ID' ] = $question_id;
2387: $question_id = wp_update_post( $post_type_args );
2388:
2389:
2390: if( 'quiz' == $context ) {
2391: $quizzes = get_post_meta( $question_id, '_quiz_id', false );
2392: if( ! in_array( $quiz_id, $quizzes ) ) {
2393: add_post_meta( $question_id, '_quiz_id', $quiz_id, false );
2394: }
2395: }
2396:
2397: update_post_meta( $question_id, '_question_grade', $question_grade );
2398: update_post_meta( $question_id, '_question_right_answer', $question_right_answer );
2399: update_post_meta( $question_id, '_right_answer_count', $right_answer_count );
2400: update_post_meta( $question_id, '_question_wrong_answers', $question_wrong_answers );
2401: update_post_meta( $question_id, '_wrong_answer_count', $wrong_answer_count );
2402: update_post_meta( $question_id, '_question_media', $question_media );
2403: update_post_meta( $question_id, '_answer_order', $answer_order );
2404: update_post_meta( $question_id, '_random_order', $random_order );
2405:
2406: if( 'quiz' != $context ) {
2407: wp_set_post_terms( $question_id, array( $question_type ), 'question-type', false );
2408: }
2409:
2410: if ( !empty($answer_feedback) ) {
2411: update_post_meta( $question_id, '_answer_feedback', $answer_feedback );
2412: }
2413:
2414: } else {
2415: $question_id = wp_insert_post( $post_type_args );
2416: $question_count = intval( $data['question_count'] );
2417: ++$question_count;
2418:
2419:
2420: if( 'quiz' == $context ) {
2421: add_post_meta( $question_id, '_quiz_id', $quiz_id, false );
2422: $lesson_id = get_post_meta( $quiz_id, '_quiz_lesson', true );
2423: update_post_meta( $lesson_id, '_quiz_has_questions', '1' );
2424: }
2425:
2426: if( isset( $question_grade ) ) {
2427: add_post_meta( $question_id, '_question_grade', $question_grade );
2428: }
2429: add_post_meta( $question_id, '_question_right_answer', $question_right_answer );
2430: add_post_meta( $question_id, '_right_answer_count', $right_answer_count );
2431: add_post_meta( $question_id, '_question_wrong_answers', $question_wrong_answers );
2432: add_post_meta( $question_id, '_wrong_answer_count', $wrong_answer_count );
2433: add_post_meta( $question_id, '_quiz_question_order' . $quiz_id, $quiz_id . '000' . $question_count );
2434: add_post_meta( $question_id, '_question_media', $question_media );
2435: add_post_meta( $question_id, '_answer_order', $answer_order );
2436: add_post_meta( $question_id, '_random_order', $random_order );
2437:
2438: if ( !empty($answer_feedback) ) {
2439: add_post_meta( $question_id, '_answer_feedback', $answer_feedback );
2440: }
2441:
2442:
2443: wp_set_post_terms( $question_id, array( $question_type ), 'question-type' );
2444:
2445: if( $question_category ) {
2446: wp_set_post_terms( $question_id, array( $question_category ), 'question-category' );
2447: }
2448:
2449: }
2450: }
2451:
2452: if ( 0 < $question_id ) {
2453: $return = $question_id;
2454: }
2455: return $return;
2456: }
2457:
2458:
2459: 2460: 2461: 2462: 2463: 2464: 2465:
2466: private function lesson_remove_question( $data = array() ) {
2467:
2468:
2469: $question_id = 0;
2470: if ( isset( $data[ 'question_id' ] ) && ( 0 < absint( $data[ 'question_id' ] ) ) ) {
2471: $question_id = absint( $data[ 'question_id' ] );
2472: }
2473:
2474: if ( empty( $question_id ) ) {
2475: return false;
2476: }
2477:
2478:
2479: $quizzes = get_post_meta( $question_id, '_quiz_id', false );
2480:
2481: foreach( $quizzes as $quiz_id ) {
2482: if( $quiz_id == $data['quiz_id'] ) {
2483: delete_post_meta( $question_id, '_quiz_id', $quiz_id );
2484: }
2485: }
2486:
2487: return true;
2488:
2489: }
2490:
2491:
2492: 2493: 2494: 2495: 2496: 2497:
2498: public function lesson_complexities() {
2499:
2500:
2501: $lesson_complexities = array( 'easy' => __( 'Easy', 'woothemes-sensei' ),
2502: 'std' => __( 'Standard', 'woothemes-sensei' ),
2503: 'hard' => __( 'Hard', 'woothemes-sensei' )
2504: );
2505:
2506: return $lesson_complexities;
2507:
2508: }
2509:
2510:
2511: 2512: 2513: 2514: 2515: 2516: 2517:
2518: public function lesson_count( $post_status = 'publish', $course_id = false ) {
2519:
2520: $post_args = array( 'post_type' => 'lesson',
2521: 'posts_per_page' => -1,
2522:
2523:
2524: 'post_status' => $post_status,
2525: 'suppress_filters' => 0,
2526: 'fields' => 'ids',
2527: );
2528: if( $course_id ) {
2529: $post_args['meta_query'][] = array(
2530: 'key' => '_lesson_course',
2531: 'value' => $course_id,
2532: );
2533: }
2534: else {
2535:
2536: $post_args['meta_query'][] = array(
2537: 'key' => '_lesson_course',
2538: 'value' => 0,
2539: 'compare' => '>=',
2540: );
2541: }
2542:
2543:
2544:
2545: $lessons_query = new WP_Query( apply_filters( 'sensei_lesson_count', $post_args ) );
2546:
2547:
2548: return count( $lessons_query->posts );
2549: }
2550:
2551:
2552: 2553: 2554: 2555: 2556: 2557: 2558: 2559: 2560:
2561: public function lesson_quizzes( $lesson_id = 0, $post_status = 'any', $fields = 'ids' ) {
2562:
2563: $posts_array = array();
2564:
2565: $post_args = array( 'post_type' => 'quiz',
2566: 'posts_per_page' => 1,
2567: 'orderby' => 'title',
2568: 'order' => 'DESC',
2569: 'post_parent' => $lesson_id,
2570: 'post_status' => $post_status,
2571: 'suppress_filters' => 0,
2572: 'fields' => $fields
2573: );
2574: $posts_array = get_posts( $post_args );
2575: $quiz_id = array_shift($posts_array);
2576:
2577: return $quiz_id;
2578: }
2579:
2580:
2581: 2582: 2583: 2584: 2585: 2586: 2587: 2588: 2589: 2590: 2591: 2592: 2593: 2594: 2595: 2596:
2597: public function lesson_quiz_questions( $quiz_id = 0, $post_status = 'any', $orderby = 'meta_value_num title', $order = 'ASC' ) {
2598:
2599: $quiz_id = (string) $quiz_id;
2600: $quiz_lesson_id = Sensei()->quiz->get_lesson_id( $quiz_id );
2601:
2602:
2603: if( is_admin() ) {
2604: $user_id = isset( $_GET['user'] ) ? $_GET['user'] : '' ;
2605: } else {
2606: $user_id = get_current_user_id();
2607: }
2608:
2609:
2610: $user_lesson_status = Sensei_Utils::user_lesson_status( $quiz_lesson_id, $user_id );
2611:
2612:
2613: $this->set_default_question_order( $quiz_id );
2614:
2615:
2616: if ( ! is_admin() ) {
2617: $random_order = get_post_meta( $quiz_id, '_random_question_order', true );
2618: if( $random_order && $random_order == 'yes' ) {
2619: $orderby = 'rand';
2620: }
2621: }
2622:
2623:
2624: $question_query_args = array(
2625: 'post_type' => array( 'question', 'multiple_question' ),
2626: 'posts_per_page' => -1,
2627: 'meta_key' => '_quiz_question_order' . $quiz_id,
2628: 'orderby' => $orderby,
2629: 'order' => $order,
2630: 'meta_query' => array(
2631: array(
2632: 'key' => '_quiz_id',
2633: 'value' => $quiz_id,
2634: )
2635: ),
2636: 'post_status' => $post_status,
2637: 'suppress_filters' => 0
2638: );
2639:
2640:
2641: $questions_query = new WP_Query( $question_query_args );
2642:
2643:
2644: $questions = $questions_query->posts;
2645:
2646:
2647: $questions_array = $questions_query->posts;
2648:
2649:
2650: $selected_questions = false;
2651: if( ! is_admin() || ( is_admin() && isset( $_GET['page'] ) && 'sensei_grading' == $_GET['page'] && isset( $_GET['user'] ) && isset( $_GET['quiz_id'] ) ) ) {
2652:
2653:
2654: $questions_asked_string = !empty( $user_lesson_status->comment_ID) ? get_comment_meta( $user_lesson_status->comment_ID, 'questions_asked', true ) : false;
2655: if( !empty($questions_asked_string) ) {
2656:
2657: $selected_questions = explode( ',', $questions_asked_string );
2658:
2659:
2660: $questions = array();
2661: foreach( $selected_questions as $question_id ) {
2662: if( ! $question_id ) continue;
2663: $question = get_post( $question_id );
2664: if( ! isset( $question ) || ! isset( $question->ID ) ) continue;
2665: $questions[] = $question;
2666: }
2667:
2668: } else {
2669:
2670:
2671:
2672: $multiple_array = array();
2673: $existing_questions = array();
2674:
2675:
2676: foreach( $questions_array as $question ) {
2677: if( 'question' != $question->post_type ) continue;
2678: $existing_questions[] = $question->ID;
2679: }
2680:
2681:
2682: $questions_loop = $questions_array;
2683: $questions_array = array();
2684: foreach( $questions_loop as $k => $question ) {
2685:
2686:
2687: if( 'question' == $question->post_type ) {
2688: $questions_array[] = $question;
2689: } else {
2690:
2691:
2692: $question_cat = intval( get_post_meta( $question->ID, 'category', true ) );
2693: $question_number = intval( get_post_meta( $question->ID, 'number', true ) );
2694:
2695: $qargs = array(
2696: 'post_type' => 'question',
2697: 'posts_per_page' => $question_number,
2698: 'orderby' => $orderby,
2699: 'tax_query' => array(
2700: array(
2701: 'taxonomy' => 'question-category',
2702: 'field' => 'term_id',
2703: 'terms' => $question_cat
2704: )
2705: ),
2706: 'post_status' => $post_status,
2707: 'suppress_filters' => 0,
2708: 'post__not_in' => $existing_questions,
2709: );
2710: $cat_questions = get_posts( $qargs );
2711:
2712:
2713: $questions_array = array_merge( $questions_array, $cat_questions );
2714:
2715:
2716: foreach( $questions_array as $cat_question ) {
2717: if( in_array( $cat_question->ID, $existing_questions ) ) continue;
2718: $existing_questions[] = $cat_question->ID;
2719: }
2720: }
2721: }
2722:
2723:
2724: $questions = $questions_array;
2725: }
2726: }
2727:
2728:
2729: if( ! $selected_questions ) {
2730:
2731:
2732: if( ! is_admin() ) {
2733:
2734:
2735: $show_questions = intval( get_post_meta( $quiz_id, '_show_questions', true ) );
2736: if( $show_questions ) {
2737:
2738:
2739: $selected_questions = array_rand( $questions_array, $show_questions );
2740:
2741:
2742: $questions = array();
2743: foreach( $questions_array as $k => $question ) {
2744:
2745:
2746: if( is_array( $selected_questions ) ) {
2747: if( in_array( $k, $selected_questions ) ) {
2748: $questions[] = $question;
2749: }
2750: } elseif( 1 == $show_questions ) {
2751: if ( $selected_questions == $k ) {
2752: $questions[] = $question;
2753: }
2754: }
2755: }
2756: }
2757: }
2758: }
2759:
2760:
2761:
2762: if( ! is_admin() && $user_lesson_status ){
2763:
2764: $questions_asked = get_comment_meta($user_lesson_status->comment_ID, 'questions_asked', true);
2765: if ( empty( $questions_asked ) && $user_lesson_status) {
2766:
2767: $questions_asked = array();
2768:
2769: foreach ($questions as $question) {
2770: $questions_asked[] = $question->ID;
2771: }
2772:
2773:
2774: $questions_asked_csv = implode(',', $questions_asked);
2775: update_comment_meta($user_lesson_status->comment_ID, 'questions_asked', $questions_asked_csv);
2776: }
2777: }
2778:
2779: 2780: 2781: 2782: 2783: 2784:
2785: return apply_filters( 'sensei_lesson_quiz_questions', $questions, $quiz_id );
2786:
2787: }
2788:
2789: 2790: 2791: 2792:
2793: public function set_default_question_order( $quiz_id = 0 ) {
2794:
2795: if( $quiz_id ) {
2796:
2797: $question_order = get_post_meta( $quiz_id, '_question_order', true );
2798:
2799: if( ! $question_order ) {
2800:
2801: $args = array(
2802: 'post_type' => 'question',
2803: 'posts_per_page' => -1,
2804: 'orderby' => 'ID',
2805: 'order' => 'ASC',
2806: 'meta_query' => array(
2807: array(
2808: 'key' => '_quiz_id',
2809: 'value' => $quiz_id
2810: )
2811: ),
2812: 'post_status' => 'any',
2813: 'suppress_filters' => 0
2814: );
2815: $questions = get_posts( $args );
2816:
2817: $o = 1;
2818: foreach( $questions as $question ) {
2819: add_post_meta( $question->ID, '_quiz_question_order' . $quiz_id, $quiz_id . '000' . $o, true );
2820: $o++;
2821: }
2822: }
2823: }
2824:
2825: }
2826:
2827: 2828: 2829: 2830: 2831: 2832: 2833: 2834: 2835: 2836: 2837:
2838: public function lesson_image( $lesson_id = 0, $width = '100', $height = '100', $widget = false ) {
2839:
2840: $html = '';
2841:
2842:
2843: if ( ( $width == '100' ) && ( $height == '100' ) ) {
2844:
2845: if ( is_singular( 'lesson' ) || !empty( $lesson_id ) ) {
2846:
2847: if ( ! $widget && ! Sensei()->settings->settings[ 'lesson_single_image_enable' ] ) {
2848:
2849: return '';
2850:
2851: }
2852:
2853: $image_thumb_size = 'lesson_single_image';
2854: $dimensions = Sensei()->get_image_size( $image_thumb_size );
2855: $width = $dimensions['width'];
2856: $height = $dimensions['height'];
2857: $crop = $dimensions['crop'];
2858:
2859: } else {
2860:
2861: if ( ! $widget && ! Sensei()->settings->settings[ 'course_lesson_image_enable' ] ) {
2862:
2863: return '';
2864: }
2865:
2866: $image_thumb_size = 'lesson_archive_image';
2867: $dimensions = Sensei()->get_image_size( $image_thumb_size );
2868: $width = $dimensions['width'];
2869: $height = $dimensions['height'];
2870: $crop = $dimensions['crop'];
2871:
2872: }
2873:
2874: }
2875:
2876: $img_element = '';
2877:
2878: if ( has_post_thumbnail( $lesson_id ) ) {
2879:
2880:
2881: $img_element = get_the_post_thumbnail( $lesson_id, array( $width, $height ), array( 'class' => 'woo-image thumbnail alignleft') );
2882:
2883: } else {
2884:
2885:
2886: if ( Sensei()->settings->settings[ 'placeholder_images_enable' ] ) {
2887:
2888: $img_element = apply_filters( 'sensei_lesson_placeholder_image_url', '<img src="http://placehold.it/' . $width . 'x' . $height . '" class="woo-image thumbnail alignleft" />' );
2889:
2890: }
2891:
2892: }
2893:
2894: if ( is_singular( 'lesson' ) ) {
2895:
2896: $html .= $img_element;
2897:
2898: } else {
2899:
2900: $html .= '<a href="' . get_permalink( $lesson_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $lesson_id ) ) . '">' . $img_element . '</a>';
2901:
2902: }
2903:
2904:
2905: return $html;
2906:
2907: }
2908:
2909: 2910: 2911: 2912: 2913: 2914:
2915: public static function the_lesson_image( $lesson_id = 0 ){
2916:
2917: echo Sensei()->lesson->lesson_image( $lesson_id );
2918:
2919: }
2920:
2921: 2922: 2923: 2924: 2925: 2926: 2927:
2928: public static function lesson_excerpt( $lesson = null, $add_p_tags = true ) {
2929: $html = '';
2930: if ( is_a( $lesson, 'WP_Post' ) && 'lesson' == $lesson->post_type ) {
2931:
2932: $excerpt = $lesson->post_excerpt;
2933:
2934:
2935: $html = $add_p_tags ? wpautop( $excerpt ) : $excerpt;
2936:
2937: }
2938: return apply_filters( 'sensei_lesson_excerpt', $html );
2939:
2940: }
2941:
2942: 2943: 2944: 2945: 2946: 2947: 2948: 2949: 2950:
2951: public function get_course_id( $lesson_id ){
2952:
2953: if( ! isset( $lesson_id ) || empty( $lesson_id )
2954: || 'lesson' != get_post_type( $lesson_id ) ){
2955: return false;
2956: }
2957:
2958: $lesson_course_id = get_post_meta( $lesson_id, '_lesson_course', true);
2959:
2960:
2961: if( empty( $lesson_course_id )
2962: || is_array( $lesson_course_id )
2963: || intval( $lesson_course_id ) < 1
2964: || 'course' != get_post_type( $lesson_course_id ) ){
2965:
2966: return false;
2967:
2968: }
2969:
2970: return $lesson_course_id;
2971:
2972: }
2973:
2974: 2975: 2976: 2977: 2978: 2979: 2980: 2981: 2982: 2983: 2984: 2985: 2986: 2987: 2988: 2989: 2990:
2991: public function all_lessons_edit_fields( $column_name, $post_type ) {
2992:
2993:
2994: if( 'lesson' != $post_type || 'lesson-course' != $column_name
2995: || ! current_user_can( 'edit_lessons' ) ) {
2996: return;
2997: }
2998:
2999: ?>
3000: <fieldset class="sensei-edit-field-set inline-edit-lesson">
3001: <div class="sensei-inline-edit-col column-<?php echo $column_name ?>">
3002: <?php
3003: echo '<h4>' . __('Lesson Information', 'woothemes-sensei') . '</h4>';
3004:
3005: wp_nonce_field( 'bulk-edit-lessons', '_edit_lessons_nonce' );
3006: wp_nonce_field( 'sensei-save-post-meta','woo_' . $this->token . '_nonce' );
3007:
3008:
3009:
3010:
3011: $no_change_text = '-- ' . __('No Change', 'woothemes-sensei') . ' --';
3012:
3013:
3014:
3015:
3016: $courses = WooThemes_Sensei_Course::get_all_courses();
3017: $course_options = array();
3018: if ( count( $courses ) > 0 ) {
3019: foreach ($courses as $course ){
3020: $course_options[ $course->ID ] = get_the_title( $course->ID );
3021: }
3022: }
3023:
3024: $course_options['-1']= $no_change_text;
3025: $course_attributes = array( 'name'=> 'lesson_course', 'id'=>'sensei-edit-lesson-course' , 'class'=>' ' );
3026: $course_field = Sensei_Utils::generate_drop_down( '-1', $course_options, $course_attributes );
3027: echo $this->generate_all_lessons_edit_field( __('Lesson Course', 'woothemes-sensei'), $course_field );
3028:
3029:
3030:
3031:
3032: $lesson_complexities = $this->lesson_complexities();
3033:
3034: $lesson_complexities['-1']= $no_change_text;
3035: $complexity_dropdown_attributes = array( 'name'=> 'lesson_complexity', 'id'=>'sensei-edit-lesson-complexity' , 'class'=>' ');
3036: $complexity_filed = Sensei_Utils::generate_drop_down( '-1', $lesson_complexities, $complexity_dropdown_attributes );
3037: echo $this->generate_all_lessons_edit_field( __('Lesson Complexity', 'woothemes-sensei'), $complexity_filed );
3038:
3039: ?>
3040:
3041: <h4><?php _e('Quiz Settings', 'woothemes-sensei'); ?> </h4>
3042:
3043: <?php
3044:
3045:
3046:
3047:
3048: $pass_required_options = array(
3049: '-1' => $no_change_text,
3050: '0' => __('No','woothemes'),
3051: '1' => __('Yes','woothemes'),
3052: );
3053:
3054: $pass_required_select_attributes = array( 'name'=> 'pass_required',
3055: 'id'=> 'sensei-edit-lesson-pass-required',
3056: 'class'=>' ' );
3057: $require_pass_field = Sensei_Utils::generate_drop_down( '-1', $pass_required_options, $pass_required_select_attributes, false );
3058: echo $this->generate_all_lessons_edit_field( __('Pass required', 'woothemes-sensei'), $require_pass_field );
3059:
3060:
3061:
3062:
3063: $quiz_pass_percentage_field = '<input name="quiz_passmark" id="sensei-edit-quiz-pass-percentage" type="number" />';
3064: echo $this->generate_all_lessons_edit_field( __('Pass Percentage', 'woothemes-sensei'), $quiz_pass_percentage_field );
3065:
3066:
3067:
3068:
3069: $quiz_reset_select__options = array(
3070: '-1' => $no_change_text,
3071: '0' => __('No','woothemes'),
3072: '1' => __('Yes','woothemes'),
3073: );
3074: $quiz_reset_name_id = 'sensei-edit-enable-quiz-reset';
3075: $quiz_reset_select_attributes = array( 'name'=> 'enable_quiz_reset', 'id'=>$quiz_reset_name_id, 'class'=>' ' );
3076: $quiz_reset_field = Sensei_Utils::generate_drop_down( '-1', $quiz_reset_select__options, $quiz_reset_select_attributes, false );
3077: echo $this->generate_all_lessons_edit_field( __('Enable quiz reset button', 'woothemes-sensei'), $quiz_reset_field );
3078:
3079: ?>
3080: </div>
3081: </fieldset>
3082: <?php
3083: }
3084:
3085: 3086: 3087: 3088: 3089: 3090: 3091: 3092: 3093: 3094: 3095:
3096: public function generate_all_lessons_edit_field( $title ,$field ){
3097:
3098: $html = '';
3099: $html = '<div class="inline-edit-group" >';
3100: $html .= '<span class="title">'. $title .'</span> ';
3101: $html .= '<span class="input-text-wrap">';
3102: $html .= $field;
3103: $html .= '</span>';
3104: $html .= '</label></div>';
3105:
3106: return $html ;
3107:
3108: }
3109:
3110: 3111: 3112: 3113: 3114: 3115: 3116:
3117: function save_all_lessons_edit_fields() {
3118:
3119:
3120: if( ! isset( $_POST['security'] ) || ! check_ajax_referer( 'bulk-edit-lessons', 'security' )
3121: || empty( $_POST[ 'post_ids' ] ) || ! is_array( $_POST[ 'post_ids' ] ) ) {
3122: die();
3123: }
3124:
3125:
3126: $new_course = sanitize_text_field( $_POST['sensei_edit_lesson_course'] );
3127: $new_complexity = sanitize_text_field( $_POST['sensei_edit_complexity'] );
3128: $new_pass_required = sanitize_text_field( $_POST['sensei_edit_pass_required'] );
3129: $new_pass_percentage = sanitize_text_field( $_POST['sensei_edit_pass_percentage'] );
3130: $new_enable_quiz_reset = sanitize_text_field( $_POST['sensei_edit_enable_quiz_reset'] );
3131:
3132: foreach( $_POST[ 'post_ids' ] as $lesson_id ) {
3133:
3134:
3135: $quiz_id = Sensei()->lesson->lesson_quizzes( $lesson_id );
3136:
3137:
3138:
3139:
3140:
3141: if( -1 != $new_course ){
3142: update_post_meta( $lesson_id, '_lesson_course', $new_course );
3143: }
3144:
3145: if( -1 != $new_complexity ){
3146: update_post_meta( $lesson_id, '_lesson_complexity', $new_complexity );
3147: }
3148:
3149:
3150: if( isset( $quiz_id) && 0 < intval( $quiz_id ) ) {
3151:
3152:
3153: if (-1 != $new_pass_required) {
3154:
3155: $checked = $new_pass_required ? 'on' : '';
3156: update_post_meta($quiz_id, '_pass_required', $checked);
3157: unset( $checked );
3158: }
3159:
3160:
3161: if( !empty( $new_pass_percentage) && is_numeric( $new_pass_percentage ) ){
3162:
3163: update_post_meta($quiz_id, '_quiz_passmark', $new_pass_percentage);
3164:
3165: }
3166:
3167:
3168:
3169:
3170: if (-1 != $new_enable_quiz_reset ) {
3171:
3172: $checked = $new_enable_quiz_reset ? 'on' : '' ;
3173: update_post_meta($quiz_id, '_enable_quiz_reset', $checked);
3174: unset( $checked );
3175:
3176: }
3177:
3178:
3179: }
3180:
3181: }
3182:
3183: die();
3184:
3185: }
3186:
3187: 3188: 3189: 3190: 3191: 3192: 3193: 3194: 3195: 3196: 3197:
3198: public function set_quick_edit_admin_defaults( $column_name, $post_id ){
3199:
3200: if( 'lesson-course' != $column_name ){
3201: return;
3202: }
3203:
3204: $suffix = defined( 'SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
3205: wp_enqueue_script( 'sensei-lesson-quick-edit', Sensei()->plugin_url . 'assets/js/admin/lesson-quick-edit' . $suffix . '.js', array( 'jquery' ), Sensei()->version, true );
3206:
3207:
3208: $data = array();
3209: foreach( $this->meta_fields as $field ){
3210:
3211: $data[$field] = get_post_meta( $post_id, '_'.$field, true );
3212:
3213: }
3214:
3215: $quiz_id = Sensei()->lesson->lesson_quizzes( $post_id );
3216: foreach( Sensei()->quiz->meta_fields as $field ){
3217:
3218: $data[$field] = get_post_meta( $quiz_id, '_'.$field, true );
3219:
3220: }
3221:
3222: wp_localize_script( 'sensei-lesson-quick-edit', 'sensei_quick_edit_'.$post_id, $data );
3223:
3224: }
3225:
3226: 3227: 3228: 3229: 3230: 3231: 3232: 3233: 3234:
3235: public static function single_course_lessons_classes( $classes ){
3236:
3237: if( is_singular('course') ){
3238:
3239: global $post;
3240: $course_id = $post->ID;
3241:
3242: $lesson_classes = array( 'course', 'post' );
3243: if ( is_user_logged_in() ) {
3244:
3245:
3246: $single_lesson_complete = Sensei_Utils::user_completed_lesson( get_the_ID(), get_current_user_id() );
3247: if ( $single_lesson_complete ) {
3248:
3249: $lesson_classes[] = 'lesson-completed';
3250:
3251: }
3252:
3253: }
3254:
3255: $is_user_taking_course = Sensei_Utils::user_started_course( $course_id, get_current_user_id() );
3256: if ( Sensei_Utils::is_preview_lesson( get_the_ID() ) && !$is_user_taking_course ) {
3257:
3258: $lesson_classes[] = 'lesson-preview';
3259:
3260: }
3261:
3262: $classes = array_merge( $classes, $lesson_classes );
3263:
3264: }
3265:
3266: return $classes;
3267:
3268: }
3269:
3270: 3271: 3272: 3273: 3274: 3275:
3276: public static function the_lesson_meta( $lesson_id ){
3277:
3278: global $wp_query;
3279: $loop_lesson_number = $wp_query->current_post + 1;
3280:
3281: $course_id = Sensei()->lesson->get_course_id( $lesson_id );
3282: $single_lesson_complete = false;
3283: $is_user_taking_course = Sensei_Utils::user_started_course( $course_id, get_current_user_id() );
3284:
3285:
3286: $complexity_array = Sensei()->lesson->lesson_complexities();
3287:
3288: $lesson_complexity = get_post_meta( $lesson_id, '_lesson_complexity', true );
3289: if ( '' != $lesson_complexity ) {
3290:
3291: $lesson_complexity = $complexity_array[$lesson_complexity];
3292:
3293: }
3294: $user_info = get_userdata( absint( get_post()->post_author ) );
3295: $is_preview = Sensei_Utils::is_preview_lesson( $lesson_id);
3296: $preview_label = '';
3297: if ( $is_preview && !$is_user_taking_course ) {
3298:
3299: $preview_label = Sensei()->frontend->sensei_lesson_preview_title_text( $lesson_id);
3300: $preview_label = '<span class="preview-heading">' . $preview_label . '</span>';
3301:
3302: }
3303:
3304:
3305: $count_markup= '';
3306: 3307: 3308: 3309: 3310: 3311:
3312: if( apply_filters( 'sensei_show_lesson_numbers', false ) ) {
3313:
3314: $count_markup = '<span class="lesson-number">' . $loop_lesson_number. '</span>';
3315:
3316: }
3317:
3318: $heading_link_title = sprintf( __( 'Start %s', 'woothemes-sensei' ), get_the_title( $lesson_id ) );
3319:
3320: ?>
3321: <header>
3322: <h2>
3323: <a href="<?php echo esc_url_raw( get_permalink( $lesson_id ) ) ?>"
3324: title="<?php esc_attr_e( $heading_link_title ) ?>" >
3325: <?php echo $count_markup. get_the_title( $lesson_id ) . $preview_label; ?>
3326: </a>
3327: </h2>
3328:
3329: <p class="lesson-meta">
3330:
3331: <?php
3332:
3333: $meta_html = '';
3334: $user_lesson_status = Sensei_Utils::user_lesson_status( get_the_ID(), get_current_user_id() );
3335:
3336: $lesson_length = get_post_meta( $lesson_id, '_lesson_length', true );
3337: if ( '' != $lesson_length ) {
3338:
3339: $meta_html .= '<span class="lesson-length">' . __( 'Length: ', 'woothemes-sensei' ) . $lesson_length . __( ' minutes', 'woothemes-sensei' ) . '</span>';
3340:
3341: }
3342:
3343: if ( Sensei()->settings->get( 'lesson_author' ) ) {
3344:
3345: $meta_html .= '<span class="lesson-author">' . __( 'Author: ', 'woothemes-sensei' ) . '<a href="' . get_author_posts_url( absint( get_post()->post_author ) ) . '" title="' . esc_attr( $user_info->display_name ) . '">' . esc_html( $user_info->display_name ) . '</a></span>';
3346:
3347: }
3348: if ( '' != $lesson_complexity ) {
3349:
3350: $meta_html .= '<span class="lesson-complexity">' . __( 'Complexity: ', 'woothemes-sensei' ) . $lesson_complexity .'</span>';
3351:
3352: }
3353:
3354: if ( Sensei_Utils::user_completed_lesson( $lesson_id, get_current_user_id() ) ) {
3355:
3356: $meta_html .= '<span class="lesson-status complete">' .__( 'Complete', 'woothemes-sensei' ) .'</span>';
3357:
3358: } elseif ( $user_lesson_status ) {
3359:
3360: $meta_html .= '<span class="lesson-status in-progress">' . __( 'In Progress', 'woothemes-sensei' ) .'</span>';
3361:
3362: }
3363:
3364: echo $meta_html;
3365:
3366: ?>
3367:
3368: </p> <!-- lesson meta -->
3369:
3370: </header>
3371:
3372: <?php
3373:
3374: }
3375:
3376: 3377: 3378: 3379: 3380: 3381: 3382:
3383: public static function the_lesson_thumbnail( $lesson_id ){
3384:
3385: if( empty( $lesson_id ) ){
3386:
3387: $lesson_id = get_the_ID();
3388:
3389: }
3390:
3391: if( 'lesson' != get_post_type( $lesson_id ) ){
3392: return;
3393: }
3394:
3395: echo Sensei()->lesson->lesson_image( $lesson_id );
3396: }
3397:
3398:
3399: 3400: 3401: 3402: 3403: 3404: 3405:
3406: public static function alter_the_lesson_excerpt( $excerpt ) {
3407:
3408: if ('lesson' == get_post_type(get_the_ID())){
3409:
3410:
3411: remove_filter( 'get_the_excerpt', array( 'WooThemes_Sensei_Lesson','alter_the_lesson_excerpt') );
3412:
3413: return WooThemes_Sensei_Lesson::lesson_excerpt( get_post( get_the_ID() ) );
3414: }
3415:
3416: return $excerpt;
3417:
3418: }
3419:
3420: 3421: 3422: 3423: 3424: 3425: 3426: 3427:
3428: public static function get_lesson_prerequisite_id( $current_lesson_id ){
3429:
3430: $prerequisite_lesson_id = get_post_meta( $current_lesson_id , '_lesson_prerequisite', true );
3431:
3432:
3433: if( empty( $prerequisite_lesson_id )
3434: || 'lesson' != get_post_type( $prerequisite_lesson_id )
3435: || $prerequisite_lesson_id == $current_lesson_id ) {
3436:
3437: $prerequisite_lesson_id = false;
3438:
3439: }
3440:
3441: return apply_filters( 'sensei_lesson_prerequisite', $prerequisite_lesson_id, $current_lesson_id );
3442:
3443: }
3444:
3445: 3446: 3447: 3448: 3449: 3450: 3451: 3452: 3453: 3454: 3455:
3456: public static function is_prerequisite_complete( $lesson_id, $user_id ){
3457:
3458: if( empty( $lesson_id ) || empty( $user_id )
3459: || 'lesson' != get_post_type( $lesson_id )
3460: || ! is_a( get_user_by( 'id', $user_id ), 'WP_User' )){
3461:
3462: return false;
3463:
3464: }
3465:
3466: $pre_requisite_id = (string) self::get_lesson_prerequisite_id( $lesson_id );
3467:
3468:
3469: if( 'lesson' != get_post_type( $pre_requisite_id )
3470: || ! is_numeric( $pre_requisite_id ) ){
3471:
3472: return true;
3473:
3474: }
3475:
3476: return Sensei_Utils::user_completed_lesson( $pre_requisite_id, $user_id );
3477:
3478: }
3479:
3480: 3481: 3482: 3483: 3484:
3485: public static function user_not_taking_course_message(){
3486:
3487: $lesson_id = get_the_ID();
3488:
3489: if( 'lesson' != get_post_type( $lesson_id ) ){
3490: return;
3491: }
3492:
3493: $is_preview = Sensei_Utils::is_preview_lesson( $lesson_id );
3494: $pre_requisite_complete = self::is_prerequisite_complete( $lesson_id , get_current_user_id() );
3495: $lesson_course_id = get_post_meta( $lesson_id, '_lesson_course', true );
3496: $user_taking_course = Sensei_Utils::user_started_course( $lesson_course_id, get_current_user_id() );
3497:
3498: if ( $pre_requisite_complete && $is_preview && !$user_taking_course ) {
3499:
3500:
3501: }
3502:
3503: }
3504:
3505: 3506: 3507: 3508: 3509: 3510: 3511:
3512: public static function course_signup_link( ){
3513:
3514: $course_id = Sensei()->lesson->get_course_id( get_the_ID() );
3515:
3516: if ( empty( $course_id ) || 'course' != get_post_type( $course_id ) || sensei_all_access() ) {
3517:
3518: return;
3519:
3520: }
3521:
3522: ?>
3523:
3524: <section class="course-signup lesson-meta">
3525:
3526: <?php
3527:
3528: global $current_user;
3529: $wc_post_id = (int) get_post_meta( $course_id, '_course_woocommerce_product', true );
3530:
3531: if ( Sensei_WC::is_woocommerce_active() && Sensei_WC::is_course_purchasable( $course_id ) ) {
3532:
3533: if( is_user_logged_in() && ! Sensei_Utils::user_started_course( $course_id, $current_user->ID ) ) {
3534:
3535: $a_element = '<a href="' . esc_url( get_permalink( $course_id ) ) . '" title="' . __( 'Sign Up', 'woothemes-sensei' ) . '">';
3536: $a_element .= __( 'course', 'woothemes-sensei' );
3537: $a_element .= '</a>';
3538:
3539: if( Sensei_Utils::is_preview_lesson( get_the_ID() ) ){
3540:
3541: $message = sprintf( __( 'This is a preview lesson. Please purchase the %1$s before starting the lesson.', 'woothemes-sensei' ), $a_element );
3542:
3543: }else{
3544:
3545: $message = sprintf( __( 'Please purchase the %1$s before starting the lesson.', 'woothemes-sensei' ), $a_element );
3546:
3547: }
3548:
3549: Sensei()->notices->add_notice( $message, 'info' );
3550:
3551: }
3552:
3553: if( ! is_user_logged_in() ) {
3554:
3555: $a_element = '<a href="' . esc_url( get_permalink( $course_id ) ) . '" title="' . __( 'Sign Up', 'woothemes-sensei' ) . '">';
3556: $a_element .= __( 'course', 'woothemes-sensei' );
3557: $a_element .= '</a>';
3558:
3559: if( Sensei_Utils::is_preview_lesson( get_the_ID() ) ){
3560:
3561: $message = sprintf( __( 'This is a preview lesson. Please purchase the %1$s before starting the lesson.', 'woothemes-sensei' ), $a_element );
3562:
3563: }else{
3564:
3565: $message = sprintf( __( 'Please purchase the %1$s before starting the lesson.', 'woothemes-sensei' ), $a_element );
3566:
3567: }
3568:
3569: Sensei()->notices->add_notice( $message, 'alert' );
3570:
3571: }
3572:
3573: } else { ?>
3574:
3575: <?php if( ! Sensei_Utils::user_started_course( $course_id, get_current_user_id() ) && sensei_is_login_required() ) : ?>
3576:
3577: <div class="sensei-message alert">
3578: <?php
3579: $course_link = '<a href="'
3580: . esc_url( get_permalink( $course_id ) )
3581: . '" title="' . __( 'Sign Up', 'woothemes-sensei' )
3582: . '">' . __( 'course', 'woothemes-sensei' )
3583: . '</a>';
3584:
3585: if ( Sensei_Utils::is_preview_lesson( get_the_ID( ) ) ) {
3586:
3587: echo sprintf( __( 'This is a preview lesson. Please sign up for the %1$s to access all lessons.', 'woothemes-sensei' ), $course_link );
3588:
3589: } else {
3590:
3591: echo sprintf( __( 'Please sign up for the %1$s before starting the lesson.', 'woothemes-sensei' ), $course_link );
3592:
3593: }
3594:
3595: ?>
3596: </div>
3597:
3598: <?php endif; ?>
3599:
3600: <?php }
3601:
3602: </section>
3603:
3604: <?php
3605: }
3606:
3607: 3608: 3609: 3610: 3611:
3612: public static function prerequisite_complete_message(){
3613:
3614: $lesson_prerequisite = WooThemes_Sensei_Lesson::get_lesson_prerequisite_id( get_the_ID() );
3615: $lesson_has_pre_requisite = $lesson_prerequisite > 0;
3616: if ( ! WooThemes_Sensei_Lesson::is_prerequisite_complete( get_the_ID(), get_current_user_id() ) && $lesson_has_pre_requisite ) {
3617:
3618: $prerequisite_lesson_link = '<a href="' . esc_url( get_permalink( $lesson_prerequisite ) ) . '" title="' . esc_attr( sprintf( __( 'You must first complete: %1$s', 'woothemes-sensei' ), get_the_title( $lesson_prerequisite ) ) ) . '">' . get_the_title( $lesson_prerequisite ). '</a>';
3619: Sensei()->notices->add_notice( sprintf( __( 'You must first complete %1$s before viewing this Lesson', 'woothemes-sensei' ), $prerequisite_lesson_link ), 'info');
3620:
3621: }
3622:
3623: }
3624:
3625: 3626: 3627: 3628: 3629: 3630:
3631: public static function deprecate_sensei_lesson_archive_header_hook(){
3632:
3633: sensei_do_deprecated_action('sensei_lesson_archive_header', '1.9.0', 'sensei_loop_lesson_inside_before');
3634:
3635: }
3636:
3637: 3638: 3639: 3640: 3641: 3642:
3643: public function the_archive_header( ) {
3644:
3645: $before_html = '<header class="archive-header"><h1>';
3646: $after_html = '</h1></header>';
3647:
3648: $title= '';
3649: if ( is_post_type_archive( 'lesson' ) ){
3650:
3651: $title = __( 'Lessons Archive', 'woothemes-sensei' );
3652:
3653: } elseif ( is_tax( 'module' ) ) {
3654:
3655: global $wp_query;
3656: $term = $wp_query->get_queried_object();
3657: $title = $term->name;
3658:
3659: }
3660:
3661: $html = $before_html . $title . $after_html;
3662:
3663: echo apply_filters( 'sensei_lesson_archive_title', $html );
3664:
3665: }
3666:
3667: 3668: 3669: 3670: 3671: 3672:
3673: public static function the_title(){
3674:
3675: global $post;
3676:
3677: ?>
3678: <header>
3679:
3680: <h1>
3681:
3682: <?php
3683: 3684: 3685:
3686: echo apply_filters( 'sensei_single_title', get_the_title( $post ), $post->post_type );
3687: ?>
3688:
3689: </h1>
3690:
3691: </header>
3692:
3693: <?php
3694:
3695: }
3696:
3697: 3698: 3699: 3700: 3701: 3702: 3703:
3704: public static function flush_rewrite_rules( $post_id ){
3705:
3706: if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
3707:
3708: return;
3709:
3710: }
3711:
3712:
3713: if( 'lesson' == get_post_type( $post_id ) ){
3714:
3715: Sensei()->initiate_rewrite_rules_flush();
3716:
3717: }
3718:
3719: }
3720:
3721: 3722: 3723: 3724: 3725: 3726: 3727: 3728: 3729:
3730: public static function footer_quiz_call_to_action( $lesson_id = 0, $user_id = 0 ) {
3731:
3732:
3733: $lesson_id = empty( $lesson_id ) ? get_the_ID() : $lesson_id;
3734: $user_id = empty( $lesson_id ) ? get_current_user_id() : $user_id;
3735:
3736:
3737: if ( ! sensei_can_user_view_lesson( $lesson_id, $user_id ) ) {
3738: return;
3739: }
3740:
3741: $lesson_prerequisite = (int) get_post_meta( $lesson_id, '_lesson_prerequisite', true );
3742: $lesson_course_id = (int) get_post_meta( $lesson_id, '_lesson_course', true );
3743: $quiz_id = Sensei()->lesson->lesson_quizzes( $lesson_id );
3744: $has_user_completed_lesson = Sensei_Utils::user_completed_lesson( intval( $lesson_id ), $user_id );
3745: $show_actions = is_user_logged_in() ? true : false;
3746:
3747: if( intval( $lesson_prerequisite ) > 0 ) {
3748:
3749:
3750: $show_actions = Sensei_Utils::user_completed_lesson( $lesson_prerequisite, $user_id );
3751:
3752: }
3753:
3754: ?>
3755:
3756: <footer>
3757:
3758: <?php
3759: if( $show_actions && $quiz_id && Sensei()->access_settings() ) {
3760:
3761: $has_quiz_questions = get_post_meta( $lesson_id, '_quiz_has_questions', true );
3762: if( $has_quiz_questions ) {
3763: ?>
3764:
3765: <p>
3766:
3767: <a class="button"
3768: href="<?php echo esc_url_raw( get_permalink( $quiz_id ) ); ?>"
3769: title="<?php _e( 'View the Lesson Quiz', 'woothemes-sensei' ); ?>">
3770:
3771: <?php _e( 'View the Lesson Quiz', 'woothemes-sensei' ); ?>
3772:
3773: </a>
3774:
3775: </p>
3776:
3777: <?php
3778: }
3779:
3780: }
3781:
3782: if ( $show_actions && ! $has_user_completed_lesson ) {
3783:
3784: sensei_complete_lesson_button();
3785:
3786: } elseif( $show_actions ) {
3787:
3788: sensei_reset_lesson_button();
3789:
3790: }
3791: ?>
3792:
3793: </footer>
3794:
3795: <?php
3796: }
3797:
3798: 3799: 3800: 3801: 3802:
3803: public static function output_comments(){
3804:
3805: $course_id = Sensei()->lesson->get_course_id( get_the_ID() );
3806: $allow_comments = Sensei()->settings->settings[ 'lesson_comments' ];
3807: $user_taking_course = Sensei_Utils::user_started_course($course_id );
3808:
3809: $lesson_allow_comments = $allow_comments && $user_taking_course;
3810:
3811: if ( $lesson_allow_comments || is_singular( 'sensei_message' ) ) {
3812:
3813: comments_template();
3814:
3815: }
3816:
3817: }
3818:
3819: 3820: 3821: 3822: 3823: 3824: 3825: 3826:
3827: public static function user_lesson_quiz_status_message( $lesson_id = 0, $user_id = 0){
3828:
3829: $lesson_id = empty( $lesson_id ) ? get_the_ID() : $lesson_id;
3830: $user_id = empty( $lesson_id ) ? get_current_user_id() : $user_id;
3831: $lesson_course_id = (int) get_post_meta( $lesson_id, '_lesson_course', true );
3832: $quiz_id = Sensei()->lesson->lesson_quizzes( $lesson_id );
3833: $has_user_completed_lesson = Sensei_Utils::user_completed_lesson( intval( $lesson_id ), $user_id );
3834:
3835:
3836: if ( $quiz_id && is_user_logged_in()
3837: && Sensei_Utils::user_started_course( $lesson_course_id, $user_id ) ) {
3838:
3839: $no_quiz_count = 0;
3840: $has_quiz_questions = get_post_meta( $lesson_id, '_quiz_has_questions', true );
3841:
3842:
3843: if ( $has_user_completed_lesson || $has_quiz_questions ) {
3844: $status = Sensei_Utils::sensei_user_quiz_status_message( $lesson_id, $user_id, true );
3845:
3846: if( ! empty( $status['message'] ) ){
3847: echo '<div class="sensei-message ' . $status['box_class'] . '">' . $status['message'] . '</div>';
3848: }
3849:
3850: if( $has_quiz_questions ) {
3851:
3852: }
3853: }
3854:
3855: }
3856:
3857: }
3858:
3859: 3860: 3861: 3862: 3863: 3864: 3865:
3866: public static function limit_archive_content ( $content ){
3867:
3868: if( is_post_type_archive( 'lesson' ) && Sensei()->settings->get('access_permission') ){
3869:
3870: return wp_trim_words( $content, $num_words = 30, $more = '…' );
3871: }
3872:
3873: return $content;
3874:
3875: }
3876:
3877: 3878: 3879: 3880: 3881: 3882:
3883: public static function get_all_lesson_ids(){
3884:
3885: return get_posts( array(
3886: 'post_type'=>'lesson',
3887: 'fields'=>'ids',
3888: 'post_status' => 'publish',
3889: 'numberposts' => 4000,
3890: 'post_per_page' => 4000
3891: ));
3892:
3893: }
3894:
3895: }
3896:
3897: 3898: 3899: 3900: 3901:
3902: class WooThemes_Sensei_Lesson extends Sensei_Lesson{}
3903: