?>
				<?php 
            if ($completed_courses > 0) {
                ?>
					<?php 
                $count = 0;
                ?>
					<h4>Completed courses</h4>
					<div class="row courses">
						<?php 
                while ($the_query->have_posts()) {
                    if ($count !== 0 && $count % 3 == 0) {
                        echo '</div><div class="row courses">';
                    }
                    $the_query->the_post();
                    $completed = Student_Completion::is_course_complete($user_id, get_the_ID());
                    $progress = do_shortcode('[course_progress course_id="' . get_the_ID() . '"]');
                    if (!$completed && '100' !== $progress) {
                        continue;
                    }
                    $progress = do_shortcode('[course_progress course_id="' . get_the_ID() . '"]');
                    $category_display_label = 'primary';
                    $category_object = wp_get_post_terms(get_the_ID(), 'course_category');
                    $category_object = $category_object[0];
                    if ('design' === $category_object->slug) {
                        $category_display_label = 'danger';
                    }
                    echo '<div class="col-md-4">';
                    echo '<div class="thumbnail">';
                    echo '<a href="' . esc_url(get_permalink()) . '"><img src="' . get_post_meta(get_the_ID(), 'featured_url', true) . '" class="img-responsive"></a>';
                    echo '<span class="course-category label label-' . $category_display_label . '">' . $category_object->name . '</span>';
        function module_status($atts)
        {
            ob_start();
            extract(shortcode_atts(array('course_id' => in_the_loop() ? get_the_ID() : '', 'unit_id' => false, 'message' => __('%d of %d mandatory elements completed.', 'cp'), 'format' => 'true'), $atts, 'module_status'));
            $message = sanitize_text_field($message);
            $format = sanitize_text_field($format);
            $format = 'true' == $format ? true : false;
            if ($course_id) {
                $course_id = (int) $course_id;
            }
            $unit_id = (int) $unit_id;
            if (empty($unit_id)) {
                return '';
            }
            $criteria = Unit::get_module_completion_data($unit_id);
            $unit_status = Unit::get_unit_availability_status($unit_id);
            $unit_available = Unit::is_unit_available($unit_id, $unit_status);
            $input_modules_count = count($criteria['all_input_ids']);
            $assessable_input_modules_count = count($criteria['gradable_modules']);
            $mandatory_input_elements = count($criteria['mandatory_modules']);
            $mandatory_responses = Student_Completion::get_mandatory_steps_completed(get_current_user_id(), $course_id, $unit_id);
            //			$all_responses					 = do_shortcode( '[course_unit_details field="student_module_responses"]' );
            // $is_unit_available				 = do_shortcode( '[course_unit_details field="is_unit_available"]' );
            //			$input_modules_count			 = do_shortcode( '[course_unit_details field="input_modules_count"]' );
            //			$assessable_input_modules_count	 = do_shortcode( '[course_unit_details field="assessable_input_modules_count"]' );
            //			$mandatory_input_elements		 = do_shortcode( '[course_unit_details field="mandatory_input_modules_count"]' );
            //			$mandatory_responses			 = do_shortcode( '[course_unit_details field="student_module_responses" additional="mandatory"]' );
            //			$all_responses					 = do_shortcode( '[course_unit_details field="student_module_responses"]' );
            $unit = new Unit($unit_id);
            $unit->status = $unit_status;
            if ($input_modules_count > 0) {
                ?>
				<span class="unit-archive-single-module-status"><?php 
                if ($unit_available) {
                    if ($mandatory_input_elements > 0) {
                        echo sprintf($message, $mandatory_responses, $mandatory_input_elements);
                    }
                } else {
                    if (isset($unit->status) && $unit->status['mandatory_required']['enabled'] && !$unit->status['mandatory_required']['result'] && !$unit->status['completion_required']['enabled']) {
                        esc_html_e('All mandatory answers are required in previous unit.', 'cp');
                    } elseif (isset($unit->status) && $unit->status['completion_required']['enabled'] && !$unit->status['completion_required']['result']) {
                        esc_html_e('Previous unit must be completed successfully.', 'cp');
                    }
                    if (isset($unit->status) && !$unit->status['date_restriction']['result']) {
                        echo __('Available', 'cp') . ' ' . date_i18n(get_option('date_format'), strtotime(do_shortcode('[course_unit_details field="unit_availability"]')));
                    }
                }
                ?>
</span>
			<?php 
            } else {
                ?>
				<span class="unit-archive-single-module-status"><?php 
                if ($unit_available) {
                    //						 _e('Read-only','cp');
                } else {
                    if (isset($unit->status) && $unit->status['mandatory_required']['enabled'] && !$unit->status['mandatory_required']['result'] && !$unit->status['completion_required']['enabled']) {
                        esc_html_e('All mandatory answers are required in previous unit.', 'cp');
                    } elseif (isset($unit->status) && $unit->status['completion_required']['enabled'] && !$unit->status['completion_required']['result']) {
                        esc_html_e('Previous unit must be completed successfully.', 'cp');
                    }
                    if (isset($unit->status) && !empty($unit->status) && !$unit->status['date_restriction']['result']) {
                        echo __('Available', 'cp') . ' ' . date_i18n(get_option('date_format'), strtotime(do_shortcode('[course_unit_details field="unit_availability"]')));
                    }
                }
                ?>
</span>
			<?php 
            }
            $content = ob_get_clean();
            return $content;
        }
    function save_module_data()
    {
        global $wpdb, $last_inserted_unit_id, $save_elements;
        if (isset($_POST['module_type']) && $save_elements == true) {
            foreach (array_keys($_POST['module_type']) as $module_type => $module_value) {
                if ($module_value == $this->name) {
                    $data = new stdClass();
                    $data->ID = '';
                    $data->unit_id = '';
                    $data->title = '';
                    $data->excerpt = '';
                    $data->content = '';
                    $data->metas = array();
                    $data->metas['module_type'] = $this->name;
                    $data->post_type = 'module';
                    if (isset($_POST[$this->name . '_id'])) {
                        foreach ($_POST[$this->name . '_id'] as $key => $value) {
                            $data->ID = $_POST[$this->name . '_id'][$key];
                            $data->unit_id = (isset($_POST['unit_id']) and isset($_POST['unit']) && $_POST['unit'] != '') ? $_POST['unit_id'] : $last_inserted_unit_id;
                            $data->title = $_POST[$this->name . '_title'][$key];
                            $data->content = $_POST[$this->name . '_content'][$key];
                            $data->metas['module_order'] = $_POST[$this->name . '_module_order'][$key];
                            $data->metas['module_page'] = $_POST[$this->name . '_module_page'][$key];
                            $data->metas['limit_attempts_value'] = $_POST[$this->name . '_limit_attempts_value'][$key];
                            $data->metas['minimum_grade_required'] = $_POST[$this->name . '_minimum_grade_required'][$key];
                            // if ( isset($_POST[$this->name . '_show_title_on_front'][$key]) ) {
                            //     $data->metas['show_title_on_front'] = $_POST[$this->name . '_show_title_on_front'][$key];
                            // } else {
                            //     $data->metas['show_title_on_front'] = 'no';
                            // }
                            $data->metas['show_title_on_front'] = $_POST[$this->name . '_show_title_field'][$key];
                            // if ( isset($_POST[$this->name . '_mandatory_answer'][$key]) ) {
                            //     $data->metas['mandatory_answer'] = $_POST[$this->name . '_mandatory_answer'][$key];
                            // } else {
                            //     $data->metas['mandatory_answer'] = 'no';
                            // }
                            $data->metas['mandatory_answer'] = $_POST[$this->name . '_mandatory_answer_field'][$key];
                            // if ( isset($_POST[$this->name . '_gradable_answer'][$key]) ) {
                            //     $data->metas['gradable_answer'] = $_POST[$this->name . '_gradable_answer'][$key];
                            // } else {
                            //     $data->metas['gradable_answer'] = 'no';
                            // }
                            $data->metas['gradable_answer'] = $_POST[$this->name . '_gradable_answer_field'][$key];
                            // if ( isset($_POST[$this->name . '_limit_attempts'][$key]) ) {
                            //     $data->metas['limit_attempts'] = $_POST[$this->name . '_limit_attempts'][$key];
                            // } else {
                            //     $data->metas['limit_attempts'] = 'no';
                            // }
                            $data->metas['limit_attempts'] = $_POST[$this->name . '_limit_attempts_field'][$key];
                            $data->metas['time_estimation'] = $_POST[$this->name . '_time_estimation'][$key];
                            parent::update_module($data);
                        }
                    }
                }
            }
        }
        if (isset($_POST['submit_modules_data_save']) || isset($_POST['submit_modules_data_done']) || isset($_POST['save_student_progress_indication'])) {
            if ($_FILES) {
                foreach ($_FILES as $file => $array) {
                    $response_id = intval(str_replace($this->name . '_front_', '', $file));
                    if (!function_exists('wp_handle_upload')) {
                        require_once ABSPATH . 'wp-includes/pluggable.php';
                        require_once ABSPATH . 'wp-admin/includes/file.php';
                    }
                    $uploadedfile = $_FILES[$file];
                    $upload_overrides = array('test_form' => false);
                    $movefile = wp_handle_upload($uploadedfile, $upload_overrides);
                    if ($movefile) {
                        //var_dump( $movefile );
                        if (!isset($movefile['error'])) {
                            $filename = $movefile['file'];
                            $wp_upload_dir = wp_upload_dir();
                            $attachment = array('guid' => $movefile['url'], 'post_mime_type' => $movefile['type'], 'post_title' => basename($movefile['url']), 'post_content' => '', 'post_status' => 'inherit');
                            $attach_id = wp_insert_attachment($attachment, $filename, $response_id);
                            $post_ancestors = get_post_ancestors($response_id);
                            $unit_id = $post_ancestors[0];
                            $course_id = get_post_meta($unit_id, 'course_id', true);
                            update_post_meta($attach_id, 'user_ID', get_current_user_ID());
                            update_post_meta($attach_id, 'course_id', $course_id);
                            // Record mandatory question answered
                            $mandatory_answer = get_post_meta($response_id, 'mandatory_answer', true);
                            if (!empty($mandatory_answer) && 'yes' == $mandatory_answer) {
                                Student_Completion::record_mandatory_answer(get_current_user_id(), $course_id, $unit_id, $response_id);
                            }
                        } else {
                            ?>
							<p class="form-info-red"><?php 
                            echo $movefile['error'];
                            ?>
</p>
						<?php 
                        }
                    } else {
                    }
                }
            }
        }
    }
 function save_module_data()
 {
     global $wpdb, $last_inserted_unit_id, $save_elements;
     if (isset($_POST['module_type']) && $save_elements == true) {
         $answers = array();
         if (isset($_POST[$this->name . '_radio_answers'])) {
             foreach ($_POST[$this->name . '_radio_answers'] as $post_answers) {
                 $answers[] = $post_answers;
             }
             foreach (array_keys($_POST['module_type']) as $module_type => $module_value) {
                 if ($module_value == $this->name) {
                     $data = new stdClass();
                     $data->ID = '';
                     $data->unit_id = '';
                     $data->title = '';
                     $data->excerpt = '';
                     $data->content = '';
                     $data->metas = array();
                     $data->metas['module_type'] = $this->name;
                     $data->post_type = 'module';
                     foreach ($_POST[$this->name . '_id'] as $key => $value) {
                         //cp_write_log( $key );
                         $data->ID = $_POST[$this->name . '_id'][$key];
                         $data->unit_id = (isset($_POST['unit_id']) and isset($_POST['unit']) && $_POST['unit'] != '') ? $_POST['unit_id'] : $last_inserted_unit_id;
                         $data->title = $_POST[$this->name . '_title'][$key];
                         $data->content = $_POST[$this->name . '_content'][$key];
                         $data->metas['module_order'] = $_POST[$this->name . '_module_order'][$key];
                         $data->metas['module_page'] = $_POST[$this->name . '_module_page'][$key];
                         $data->metas['checked_answer'] = $_POST[$this->name . '_checked_index'][$key];
                         $data->metas['answers'] = $answers[$key];
                         $data->metas['time_estimation'] = $_POST[$this->name . '_time_estimation'][$key];
                         $data->metas['limit_attempts_value'] = $_POST[$this->name . '_limit_attempts_value'][$key];
                         $data->metas['minimum_grade_required'] = $_POST[$this->name . '_minimum_grade_required'][$key];
                         // if ( isset($_POST[$this->name . '_show_title_on_front'][$key]) ) {
                         //     $data->metas['show_title_on_front'] = $_POST[$this->name . '_show_title_on_front'][$key];
                         // } else {
                         //     $data->metas['show_title_on_front'] = 'no';
                         // }
                         $data->metas['show_title_on_front'] = $_POST[$this->name . '_show_title_field'][$key];
                         // if ( isset($_POST[$this->name . '_mandatory_answer'][$key]) ) {
                         //     $data->metas['mandatory_answer'] = $_POST[$this->name . '_mandatory_answer'][$key];
                         // } else {
                         //     $data->metas['mandatory_answer'] = 'no';
                         // }
                         $data->metas['mandatory_answer'] = $_POST[$this->name . '_mandatory_answer_field'][$key];
                         // if ( isset($_POST[$this->name . '_gradable_answer'][$key]) ) {
                         //     $data->metas['gradable_answer'] = $_POST[$this->name . '_gradable_answer'][$key];
                         // } else {
                         //     $data->metas['gradable_answer'] = 'no';
                         // }
                         $data->metas['gradable_answer'] = $_POST[$this->name . '_gradable_answer_field'][$key];
                         // if ( isset($_POST[$this->name . '_limit_attempts'][$key]) ) {
                         //     $data->metas['limit_attempts'] = $_POST[$this->name . '_limit_attempts'][$key];
                         // } else {
                         //     $data->metas['limit_attempts'] = 'no';
                         // }
                         $data->metas['limit_attempts'] = $_POST[$this->name . '_limit_attempts_field'][$key];
                         parent::update_module($data);
                     }
                 }
             }
         }
     }
     if (isset($_POST['submit_modules_data_save']) || isset($_POST['submit_modules_data_done']) || isset($_POST['save_student_progress_indication'])) {
         foreach ($_POST as $response_name => $response_value) {
             if (preg_match('/' . $this->name . '_front_/', $response_name)) {
                 $response_id = intval(str_replace($this->name . '_front_', '', $response_name));
                 if ($response_value != '') {
                     $data = new stdClass();
                     $data->ID = '';
                     $data->title = '';
                     $data->excerpt = '';
                     $data->content = '';
                     $data->metas = array();
                     $data->metas['user_ID'] = get_current_user_id();
                     $data->post_type = 'module_response';
                     $data->response_id = $response_id;
                     $data->title = '';
                     //__( 'Response to '.$response_id.' module ( Unit '.$_POST['unit_id'].' )' );
                     $response_value = stripslashes($response_value);
                     $response_value = strip_tags($response_value);
                     $response_value = htmlentities($response_value);
                     $data->content = $response_value;
                     /* CHECK AND SET THE GRADE AUTOMATICALLY */
                     $checked_value = get_post_meta($response_id, 'checked_answer', true);
                     $checked_value = stripslashes($checked_value);
                     $checked_value = strip_tags($checked_value);
                     $checked_value = htmlentities($checked_value);
                     if ($data->content == $checked_value) {
                         $response_grade = 100;
                     } else {
                         $response_grade = 0;
                     }
                     $data->auto_grade = $response_grade;
                     // Record mandatory question answered
                     $mandatory_answer = get_post_meta($response_id, 'mandatory_answer', true);
                     $unit_id = (int) $_POST['unit_id'];
                     if (!empty($mandatory_answer) && 'yes' == $mandatory_answer) {
                         $course_id = get_post_meta($unit_id, 'course_id', true);
                         Student_Completion::record_mandatory_answer(get_current_user_id(), $course_id, $unit_id, $response_id);
                     }
                     $data->module_id = $response_id;
                     parent::update_module_response($data);
                 }
             }
         }
     }
 }
 public static function get_unit_availability_status($unit_id)
 {
     $unit_details = false;
     $unit = new Unit((int) $unit_id);
     $unit_details = $unit->get_unit();
     $unit_available_date = get_post_meta($unit_id, 'unit_availability', true);
     /* Not filtering date format as it could cause conflicts.  Only filter date on display. */
     $current_date = date('Y-m-d', current_time('timestamp', 0));
     /* Check if previous has conditions */
     $previous_unit_id = self::get_previous_unit_from_the_same_course($unit->course_id, $unit_id);
     $force_current_unit_completion = !empty($previous_unit_id) ? get_post_meta($previous_unit_id, 'force_current_unit_completion', true) : '';
     $force_current_unit_successful_completion = !empty($previous_unit_id) ? get_post_meta($previous_unit_id, 'force_current_unit_successful_completion', true) : '';
     $available = true;
     $student_id = get_current_user_id();
     $mandatory_done = Student_Completion::is_mandatory_complete($student_id, $unit->course_id, $previous_unit_id);
     $unit_completed = Student_Completion::is_unit_complete($student_id, $unit->course_id, $previous_unit_id);
     $unit->status['mandatory_required']['enabled'] = !empty($force_current_unit_completion) && 'on' == $force_current_unit_completion;
     $unit->status['mandatory_required']['result'] = $mandatory_done;
     $unit->status['completion_required']['enabled'] = !empty($force_current_unit_successful_completion) && 'on' == $force_current_unit_successful_completion;
     $unit->status['completion_required']['result'] = $unit_completed;
     $available = $unit->status['mandatory_required']['enabled'] ? $unit->status['mandatory_required']['result'] : $available;
     $available = $unit->status['completion_required']['enabled'] ? $unit->status['completion_required']['result'] : $available;
     $unit->status['date_restriction']['result'] = $current_date >= $unit_available_date;
     if (!$unit->status['date_restriction']['result'] || !$available) {
         $available = false;
     } else {
         $available = true;
     }
     /**
      * Perform action if unit is available.
      *
      * @since 1.2.2
      * */
     do_action('coursepress_unit_availble', $available, $unit_id);
     /**
      * Return filtered value.
      *
      * Can be used by other plugins to filter unit availability.
      *
      * @since 1.2.2
      * */
     $available = apply_filters('coursepress_filter_unit_availability', $available, $unit_id);
     $status = $unit->status;
     $status['available'] = $available;
     return $status;
 }
 public static function save_response_grade($response_id = '', $response_grade = '', $user_id = false, $course_id = false, $unit_id = false, $module_id = false)
 {
     if ((isset($_POST['response_id']) || $response_id !== '') && (isset($_POST['response_grade']) || $response_grade !== '')) {
         $grade_data = array('grade' => $response_grade !== '' && is_numeric($response_grade) ? $response_grade : $_POST['response_grade'], 'instructor' => get_current_user_ID(), 'time' => current_time('timestamp'));
         update_post_meta($response_id !== '' && is_numeric($response_id) ? $response_id : $_POST['response_id'], 'response_grade', $grade_data);
         if (!$user_id) {
             $user_id = isset($_POST['student_id']) ? (int) $_POST['student_id'] : false;
             $course_id = isset($_POST['course_id']) ? (int) $_POST['course_id'] : false;
             $unit_id = isset($_POST['unit_id']) ? (int) $_POST['unit_id'] : false;
             $module_id = isset($_POST['module_id']) ? (int) $_POST['module_id'] : false;
         }
         if (!empty($user_id) && !empty($course_id) && !empty($unit_id) && !empty($module_id)) {
             $mandatory_answer = get_post_meta($module_id, 'mandatory_answer', true);
             if (!empty($mandatory_answer) && 'yes' == $mandatory_answer) {
                 Student_Completion::record_mandatory_answer($user_id, $course_id, $unit_id, $module_id);
             }
             Student_Completion::record_gradable_result($user_id, $course_id, $unit_id, $module_id, floatval($grade_data['grade']));
         }
         return true;
     } else {
         return false;
     }
 }
 public static function get_certificate_link($student_id, $course_id, $link_title, $pre = '', $post = '', $show_link = false)
 {
     if (!$show_link) {
         $show_link = CP_Basic_Certificate::option('basic_certificate_enabled');
         $show_link = !empty($show_link) ? true : false;
         $show_link = CoursePress_Capabilities::is_pro() ? $show_link : false;
     }
     if ($show_link) {
         if (Student_Completion::is_course_complete($student_id, $course_id)) {
             $certificate_permalink = add_query_arg(array('action' => 'view_certificate', 'course_id' => $course_id), get_permalink($course_id));
             return esc_html($pre) . '<a target="_blank" href="' . esc_url($certificate_permalink) . '">' . esc_html($link_title) . '</a>' . esc_html($post);
         }
     }
     return '';
 }
 /**
  * Check to see if the student has completed a given course.
  *
  * Better to use Course_Completion[] class. See code in this function.
  *
  * @see Course_Completion
  *
  * @param int $course_ID
  * @param string $user_ID
  *
  * @return bool True if course is complete.
  */
 function is_course_complete($course_ID = 0, $user_ID = '')
 {
     if ($user_ID == '') {
         $user_ID = $this->ID;
     }
     //			$completion	= new Course_Completion( $course_ID );
     //			$completion->init_student_status( $user_ID );
     //
     //			return $completion->is_course_complete();
     if (100 == (int) Student_Completion::calculate_course_completion($user_ID, $course_ID)) {
         return true;
     } else {
         return false;
     }
 }
/**
 * LEGACY, still needed for now.
 *
 * @todo: needs to be replaced and removed soon
 */
function cp_set_visited_unit_page($unit_id = false, $page_num = false, $student_id = false, $course_id = false)
{
    if (!$unit_id) {
        return false;
    }
    if (!$student_id) {
        $student_id = get_current_user_ID();
    }
    if (!$course_id) {
        $course_id = do_shortcode('[get_parent_course_id');
    }
    Student_Completion::record_visited_page($student_id, $course_id, $unit_id, $page_num);
    // Legacy, needed still
    $visited_pages = get_user_option('visited_unit_pages_' . $unit_id . '_page', $student_id);
    if ($visited_pages === false) {
        $visited_pages = $page_num;
    } else {
        $visited_pages = explode(',', $visited_pages);
        if (!in_array($page_num, $visited_pages)) {
            $visited_pages[] = $page_num;
        }
        $visited_pages = implode(',', $visited_pages);
    }
    $global_option = !is_multisite();
    update_user_option($student_id, 'visited_unit_pages_' . $unit_id . '_page', $visited_pages, $global_option);
    cp_set_visited_course($unit_id, $student_id);
    cp_set_last_visited_unit_page($unit_id, $page_num, $student_id);
}