コード例 #1
0
 function course_unit_details($atts)
 {
     global $post_id, $wp, $coursepress;
     extract(shortcode_atts(apply_filters('shortcode_atts_course_unit_details', array('unit_id' => 0, 'field' => 'post_title', 'format' => 'true', 'additional' => '2', 'style' => 'flat', 'class' => 'course-name-content', 'tooltip_alt' => __('Percent of the unit completion', 'cp'), 'knob_fg_color' => '#24bde6', 'knob_bg_color' => '#e0e6eb', 'knob_data_thickness' => '.35', 'knob_data_width' => '70', 'knob_data_height' => '70', 'unit_title' => '', 'unit_page_title_tag' => 'h3', 'unit_page_title_tag_class' => '', 'last_visited' => 'false', 'parent_course_preceding_content' => __('Course: ', 'cp'), 'student_id' => get_current_user_ID(), 'decimal_places' => '0')), $atts));
     $unit_id = (int) $unit_id;
     $field = sanitize_html_class($field);
     $format = sanitize_text_field($format);
     $format = 'true' == $format ? true : false;
     $additional = sanitize_text_field($additional);
     $style = sanitize_html_class($style);
     $tooltip_alt = sanitize_text_field($tooltip_alt);
     $knob_fg_color = sanitize_text_field($knob_fg_color);
     $knob_bg_color = sanitize_text_field($knob_bg_color);
     $knob_data_thickness = sanitize_text_field($knob_data_thickness);
     $knob_data_width = (int) $knob_data_width;
     $knob_data_height = (int) $knob_data_height;
     $unit_title = sanitize_text_field($unit_title);
     $unit_page_title_tag = sanitize_html_class($unit_page_title_tag);
     $unit_page_title_tag_class = sanitize_html_class($unit_page_title_tag_class);
     $parent_course_preceding_content = sanitize_text_field($parent_course_preceding_content);
     $student_id = (int) $student_id;
     $last_visited = sanitize_text_field($last_visited);
     $last_visited = 'true' == $last_visited ? true : false;
     $class = sanitize_html_class($class);
     $decimal_places = sanitize_text_field($decimal_places);
     if ($unit_id == 0) {
         $unit_id = get_the_ID();
     }
     $unit = new Unit($unit_id);
     if (!isset($unit->details)) {
         $unit->details = new stdClass();
     }
     $student = new Student(get_current_user_id());
     $class = sanitize_html_class($class);
     if ($field == 'is_unit_available') {
         $unit->details->{$field} = Unit::is_unit_available($unit_id);
     }
     if ($field == 'unit_page_title') {
         $paged = isset($wp->query_vars['paged']) ? absint($wp->query_vars['paged']) : 1;
         $page_name = $unit->get_unit_page_name($paged);
         if ($unit_title !== '') {
             $page_title_prepend = $unit_title . ': ';
         } else {
             $page_title_prepend = '';
         }
         $show_title_array = get_post_meta($unit_id, 'show_page_title', true);
         $show_title = false;
         if (isset($show_title_array[$paged - 1]) && 'yes' == $show_title_array[$paged - 1]) {
             $show_title = true;
         }
         if (!empty($page_name) && $show_title) {
             $unit->details->{$field} = '<' . $unit_page_title_tag . '' . ($unit_page_title_tag_class !== '' ? ' class="' . $unit_page_title_tag_class . '"' : '') . '>' . $page_title_prepend . $unit->get_unit_page_name($paged) . '</' . $unit_page_title_tag . '>';
         } else {
             $unit->details->{$field} = '';
         }
     }
     if ($field == 'parent_course') {
         $course = new Course($unit->course_id);
         $unit->details->{$field} = $parent_course_preceding_content . '<a href="' . $course->get_permalink() . '" class="' . $class . '">' . $course->details->post_title . '</a>';
     }
     /* ------------ */
     $front_save_count = 0;
     $modules = Unit_Module::get_modules($unit_id);
     $mandatory_answers = 0;
     $mandatory = 'no';
     foreach ($modules as $mod) {
         $mandatory = get_post_meta($mod->ID, 'mandatory_answer', true);
         if ($mandatory == 'yes') {
             $mandatory_answers++;
         }
         $class_name = $mod->module_type;
         if (class_exists($class_name)) {
             if (constant($class_name . '::FRONT_SAVE')) {
                 $front_save_count++;
             }
         }
     }
     $input_modules_count = $front_save_count;
     /* ------------ */
     //$input_modules_count = do_shortcode( '[course_unit_details field="input_modules_count" unit_id="' . $unit_id . '"]' );
     $responses_count = 0;
     $modules = Unit_Module::get_modules($unit_id);
     foreach ($modules as $module) {
         if (Unit_Module::did_student_respond($module->ID, $student_id)) {
             $responses_count++;
         }
     }
     $student_modules_responses_count = $responses_count;
     //$student_modules_responses_count = do_shortcode( '[course_unit_details field="student_module_responses" unit_id="' . $unit_id . '"]' );
     if ($student_modules_responses_count > 0) {
         $percent_value = $mandatory_answers > 0 ? round(100 / $mandatory_answers * $student_modules_responses_count, 0) : 0;
         $percent_value = $percent_value > 100 ? 100 : $percent_value;
         //in case that student gave answers on all mandatory plus optional questions
     } else {
         $percent_value = 0;
     }
     if ($input_modules_count == 0) {
         $grade = 0;
         $front_save_count = 0;
         $assessable_answers = 0;
         $responses = 0;
         $graded = 0;
         //$input_modules_count = do_shortcode( '[course_unit_details field="input_modules_count" unit_id="' . get_the_ID() . '"]' );
         $modules = Unit_Module::get_modules($unit_id);
         if ($input_modules_count > 0) {
             foreach ($modules as $mod) {
                 $class_name = $mod->module_type;
                 $assessable = get_post_meta($mod->ID, 'gradable_answer', true);
                 if (class_exists($class_name)) {
                     if (constant($class_name . '::FRONT_SAVE')) {
                         if ($assessable == 'yes') {
                             $assessable_answers++;
                         }
                         $front_save_count++;
                         $response = call_user_func($class_name . '::get_response', $student_id, $mod->ID);
                         if (isset($response->ID)) {
                             $grade_data = Unit_Module::get_response_grade($response->ID);
                             $grade = $grade + $grade_data['grade'];
                             if (get_post_meta($response->ID, 'response_grade')) {
                                 $graded++;
                             }
                             $responses++;
                         }
                     } else {
                         //read only module
                     }
                 }
             }
             $percent_value = $format == true ? ($responses == $graded && $responses == $front_save_count ? '<span class="grade-active">' : '<span class="grade-inactive">') . ($grade > 0 ? round($grade / $assessable_answers, 0) : 0) . '</span>' : ($grade > 0 ? round($grade / $assessable_answers, 0) : 0);
         } else {
             $student = new Student($student_id);
             if ($student->is_unit_visited($unit_id, $student_id)) {
                 $grade = 100;
                 $percent_value = $format == true ? '<span class="grade-active">' . $grade . '</span>' : $grade;
             } else {
                 $grade = 0;
                 $percent_value = $format == true ? '<span class="grade-inactive">' . $grade . '</span>' : $grade;
             }
         }
         //$percent_value = do_shortcode( '[course_unit_details field="student_unit_grade" unit_id="' . get_the_ID() . '"]' );
     }
     //redirect to the parent course page if not enrolled
     if (!current_user_can('manage_options')) {
         if (!$coursepress->check_access($unit->course_id, $unit_id)) {
             // if( defined('DOING_AJAX') && DOING_AJAX ) { cp_write_log('doing ajax'); }
             //ob_start();
             wp_redirect(get_permalink($unit->course_id));
             exit;
         }
     }
     if ($field == 'percent') {
         //				$completion		 = new Course_Completion( $unit->course_id );
         //				$completion->init_student_status();
         //				$percent_value	 = $completion->unit_progress( $unit_id );
         $percent_value = number_format_i18n(Student_Completion::calculate_unit_completion($student_id, $unit->course_id, $unit_id), $decimal_places);
         $assessable_input_modules_count = do_shortcode('[course_unit_details field="assessable_input_modules_count"]');
         if ($style == 'flat') {
             $unit->details->{$field} = '<span class="percentage">' . ($format == true ? $percent_value . '%' : $percent_value) . '</span>';
         } elseif ($style == 'none') {
             $unit->details->{$field} = $percent_value;
         } else {
             $unit->details->{$field} = '<a class="tooltip" alt="' . $tooltip_alt . '"><input class="knob" data-fgColor="' . $knob_fg_color . '" data-bgColor="' . $knob_bg_color . '" data-thickness="' . $knob_data_thickness . '" data-width="' . $knob_data_width . '" data-height="' . $knob_data_height . '" data-readOnly=true value="' . $percent_value . '"></a>';
         }
     }
     if ($field == 'permalink') {
         if ($last_visited) {
             $last_visited_page = cp_get_last_visited_unit_page($unit_id);
             $unit->details->{$field} = Unit::get_permalink($unit_id, $unit->course_id) . 'page/' . trailingslashit($last_visited_page);
         } else {
             $unit->details->{$field} = Unit::get_permalink($unit_id, $unit->course_id);
         }
     }
     if ($field == 'input_modules_count') {
         $front_save_count = 0;
         $modules = Unit_Module::get_modules($unit_id);
         foreach ($modules as $mod) {
             $class_name = $mod->module_type;
             if (class_exists($class_name)) {
                 if (constant($class_name . '::FRONT_SAVE')) {
                     $front_save_count++;
                 }
             }
         }
         $unit->details->{$field} = $front_save_count;
     }
     if ($field == 'mandatory_input_modules_count') {
         $front_save_count = 0;
         $mandatory_answers = 0;
         $modules = Unit_Module::get_modules($unit_id);
         foreach ($modules as $mod) {
             $mandatory_answer = get_post_meta($mod->ID, 'mandatory_answer', true);
             $class_name = $mod->module_type;
             if (class_exists($class_name)) {
                 if (constant($class_name . '::FRONT_SAVE')) {
                     if ($mandatory_answer == 'yes') {
                         $mandatory_answers++;
                     }
                     //$front_save_count++;
                 }
             }
         }
         $unit->details->{$field} = $mandatory_answers;
     }
     if ($field == 'assessable_input_modules_count') {
         $front_save_count = 0;
         $assessable_answers = 0;
         $modules = Unit_Module::get_modules($unit_id);
         foreach ($modules as $mod) {
             $assessable = get_post_meta($mod->ID, 'gradable_answer', true);
             $class_name = $mod->module_type;
             if (class_exists($class_name)) {
                 if (constant($class_name . '::FRONT_SAVE')) {
                     if ($assessable == 'yes') {
                         $assessable_answers++;
                     }
                     //$front_save_count++;
                 }
             }
         }
         if (isset($unit->details->{$field})) {
             $unit->details->{$field} = $assessable_answers;
         }
     }
     if ($field == 'student_module_responses') {
         $responses_count = 0;
         $mandatory_answers = 0;
         $modules = Unit_Module::get_modules($unit_id);
         foreach ($modules as $module) {
             $mandatory = get_post_meta($module->ID, 'mandatory_answer', true);
             if ($mandatory == 'yes') {
                 $mandatory_answers++;
             }
             if (Unit_Module::did_student_respond($module->ID, $student_id)) {
                 $responses_count++;
             }
         }
         if ($additional == 'mandatory') {
             if ($responses_count > $mandatory_answers) {
                 $unit->details->{$field} = $mandatory_answers;
             } else {
                 $unit->details->{$field} = $responses_count;
             }
             //so we won't have 7 of 6 mandatory answered but mandatory number as a max number
         } else {
             $unit->details->{$field} = $responses_count;
         }
     }
     if ($field == 'student_unit_grade') {
         $grade = 0;
         $front_save_count = 0;
         $responses = 0;
         $graded = 0;
         $input_modules_count = do_shortcode('[course_unit_details field="input_modules_count" unit_id="' . get_the_ID() . '"]');
         $modules = Unit_Module::get_modules($unit_id);
         $mandatory_answers = 0;
         $assessable_answers = 0;
         if ($input_modules_count > 0) {
             foreach ($modules as $mod) {
                 $class_name = $mod->module_type;
                 if (class_exists($class_name)) {
                     if (constant($class_name . '::FRONT_SAVE')) {
                         $front_save_count++;
                         $response = call_user_func($class_name . '::get_response', $student_id, $mod->ID);
                         $assessable = get_post_meta($mod->ID, 'gradable_answer', true);
                         $mandatory = get_post_meta($mod->ID, 'mandatory_answer', true);
                         if ($assessable == 'yes') {
                             $assessable_answers++;
                         }
                         if (isset($response->ID)) {
                             if ($assessable == 'yes') {
                                 $grade_data = Unit_Module::get_response_grade($response->ID);
                                 $grade = $grade + $grade_data['grade'];
                                 if (get_post_meta($response->ID, 'response_grade')) {
                                     $graded++;
                                 }
                                 $responses++;
                             }
                         }
                     } else {
                         //read only module
                     }
                 }
             }
             $unit->details->{$field} = $format == true ? ($responses == $graded && $responses == $front_save_count ? '<span class="grade-active">' : '<span class="grade-inactive">') . ($grade > 0 ? round($grade / $assessable_answers, 0) : 0) . '%</span>' : ($grade > 0 ? round($grade / $assessable_answers, 0) : 0);
         } else {
             $student = new Student($student_id);
             if ($student->is_unit_visited($unit_id, $student_id)) {
                 $grade = 100;
                 $unit->details->{$field} = $format == true ? '<span class="grade-active">' . $grade . '%</span>' : $grade;
             } else {
                 $grade = 0;
                 $unit->details->{$field} = $format == true ? '<span class="grade-inactive">' . $grade . '%</span>' : $grade;
             }
         }
     }
     if ($field == 'student_unit_modules_graded') {
         $grade = 0;
         $front_save_count = 0;
         $responses = 0;
         $graded = 0;
         $modules = Unit_Module::get_modules($unit_id);
         foreach ($modules as $mod) {
             $class_name = $mod->module_type;
             if (class_exists($class_name)) {
                 if (constant($class_name . '::FRONT_SAVE')) {
                     $front_save_count++;
                     $response = call_user_func($class_name . '::get_response', $student_id, $mod->ID);
                     if (isset($response->ID)) {
                         $grade_data = Unit_Module::get_response_grade($response->ID);
                         $grade = $grade + $grade_data['grade'];
                         if (get_post_meta($response->ID, 'response_grade')) {
                             $graded++;
                         }
                         $responses++;
                     }
                 } else {
                     //read only module
                 }
             }
         }
         $unit->details->{$field} = $graded;
     }
     if (isset($unit->details->{$field})) {
         return $unit->details->{$field};
     }
 }