/** * Output column content to the lessons list in admin. * * @param string $column_name * @param int $post_id */ public static function lessons_column_output($column_name, $post_id) { if ('course' == $column_name) { $course_id = ib_edu_get_course_id($post_id); if ($course_id && ($course = get_post($course_id))) { echo '<a href="' . esc_url(get_permalink($course->ID)) . '" target="_blank">' . esc_html($course->post_title) . '</a>'; } } }
public static function can_user_access($object, $object_id) { $access = false; $user_id = get_current_user_id(); if ('lesson' == $object) { $lesson_access = ib_edu_lesson_access($object_id); if ('public' == $lesson_access) { $access = true; } elseif ($user_id) { if ('logged_in' == $lesson_access) { $access = true; } else { self::set_current_user_courses($user_id); if (in_array(ib_edu_get_course_id($object_id), self::$current_user_courses)) { $access = true; } } } } return $access; }
<?php $api = IB_Educator::get_instance(); // Get entry data for the current student. Entry status must be "inprogress". $entry = $api->get_entry(array('user_id' => get_current_user_id(), 'course_id' => ib_edu_get_course_id(get_the_ID()), 'entry_status' => 'inprogress')); if (!$entry) { return; } $lesson_id = get_the_ID(); $questions = $api->get_questions(array('lesson_id' => $lesson_id)); ?> <?php if ($questions) { ?> <?php $message = get_query_var('edu-message'); if (!$message) { $message = ib_edu_message('quiz'); } if ($message) { switch ($message) { case 'empty-answers': echo '<div class="ib-edu-message error">' . __('Please answer all questions before submitting the quiz.', 'ibeducator') . '</div>'; break; case 'quiz-submitted': echo '<div class="ib-edu-message success">' . __('Thank you. The quiz has been accepted.', 'ibeducator') . '</div>'; break; } } $quiz_submitted = $api->is_quiz_submitted($lesson_id, $entry->ID);
the_post(); ?> <section class="section-content"> <div class="container clearfix"> <div class="main-content"> <?php IB_Educator_View::template_part('content', 'single-lesson'); ?> </div> <div class="page-sidebar"> <?php $lesson_id = get_the_ID(); $api = IB_Educator::get_instance(); $lessons = $api->get_lessons(ib_edu_get_course_id($lesson_id)); if ($lessons->have_posts()) { $cl = null; if (class_exists('IB_Educator_Completed_Lessons')) { $cl = IB_Educator_Completed_Lessons::get_instance(); } echo '<aside class="widget"><h1 class="widget-title">' . __('Lessons', 'ib-educator') . '</h1>'; echo '<ul class="lessons-nav">'; while ($lessons->have_posts()) { $lessons->the_post(); $classes = null; if ($cl) { $classes = $cl->add_lesson_class(array(), get_the_ID()); } if ($lesson_id == get_the_ID()) { ?>
/** * Get the adjacent lesson. * * @param bool $previous * @return mixed If global post object is not set returns null, if post is not found, returns empty string, else returns WP_Post. */ function ib_edu_get_adjacent_lesson($previous = true) { global $wpdb; if (!($lesson = get_post())) { return null; } $course_id = ib_edu_get_course_id($lesson->ID); $cmp = $previous ? '<' : '>'; $order = $previous ? 'DESC' : 'ASC'; $join = "INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID"; $where = $wpdb->prepare("WHERE p.post_type = 'ib_educator_lesson' AND p.post_status = 'publish' AND p.menu_order {$cmp} %d AND pm.meta_key = '_ibedu_course' AND pm.meta_value = %d", $lesson->menu_order, $course_id); $sort = "ORDER BY p.menu_order {$order}"; $query = "SELECT p.ID FROM {$wpdb->posts} as p {$join} {$where} {$sort} LIMIT 1"; $result = $wpdb->get_var($query); if (null === $result) { return ''; } return get_post($result); }
/** * This template renders a quiz. * * @version 1.1.0 */ $user_id = get_current_user_id(); if ($user_id == 0) { echo '<p>'; printf(__('You must be <a href="%s">logged in</a> to take the quiz.', 'ibeducator'), esc_url(wp_login_url(get_permalink()))); echo '</p>'; return; } $lesson_id = get_the_ID(); // Get entry data for the current student. Entry status must be "inprogress". $entry = IB_Educator::get_instance()->get_entry(array('user_id' => $user_id, 'course_id' => ib_edu_get_course_id($lesson_id), 'entry_status' => 'inprogress')); $entry_id = $entry ? $entry->ID : 0; if (!$entry_id && 'ib_educator_lesson' == get_post_type()) { return; } $quizzes = Edr_Manager::get('edr_quizzes'); $questions = $quizzes->get_questions($lesson_id); ?> <?php if (!empty($questions)) { ?> <?php $message = get_query_var('edu-message'); if (!$message) { $message = ib_edu_message('quiz');
/** * Submit quiz. */ public static function submit_quiz() { if (empty($_POST)) { return; } $lesson_id = get_the_ID(); $user_id = get_current_user_id(); $api = IB_Educator::get_instance(); // Verify nonce. check_admin_referer('ibedu_submit_quiz_' . $lesson_id); // Get questions. $questions = $api->get_questions(array('lesson_id' => $lesson_id)); if (!$questions) { return; } $num_questions = count($questions); $num_answers = 0; if (!isset($_POST['answers'])) { // The student has to submit the answers to all questions. ib_edu_message('quiz', 'empty-answers'); return; } elseif (is_array($_POST['answers'])) { // Count answers. foreach ($_POST['answers'] as $answer) { if (!empty($answer)) { ++$num_answers; } } if ($num_answers != $num_questions) { // Not all questions were answered. ib_edu_message('quiz', 'empty-answers'); return; } } // Get entry. $entry = $api->get_entry(array('user_id' => $user_id, 'course_id' => ib_edu_get_course_id($lesson_id), 'entry_status' => 'inprogress')); if (!$entry) { return; } if ($api->is_quiz_submitted($lesson_id, $entry->ID)) { // Quiz has been submitted already. return; } $user_answer = ''; $answered = 0; $correct = 0; $automatic_grade = true; $choices = $api->get_choices($lesson_id, true); // Check answers to the quiz questions. foreach ($questions as $question) { if (!isset($_POST['answers'][$question->ID])) { // Student has to submit an answer. continue; } // Every question type needs a specific way to check for the valid answer. switch ($question->question_type) { // Multiple Choice Question. case 'multiplechoice': $user_answer = absint($_POST['answers'][$question->ID]); if (isset($choices[$question->ID]) && isset($choices[$question->ID][$user_answer])) { $choice = $choices[$question->ID][$user_answer]; // Add answer to database. $added = $api->add_student_answer(array('question_id' => $question->ID, 'entry_id' => $entry->ID, 'correct' => $choice->correct, 'choice_id' => $choice->ID)); if (1 == $added) { ++$answered; } if (1 == $choice->correct) { ++$correct; } } break; // Written Answer Question. // Written Answer Question. case 'writtenanswer': // We cannot check written answers automatically. if ($automatic_grade) { $automatic_grade = false; } $user_answer = stripslashes($_POST['answers'][$question->ID]); if (empty($user_answer)) { continue; } // Add answer to database. $added = $api->add_student_answer(array('question_id' => $question->ID, 'entry_id' => $entry->ID, 'correct' => -1, 'answer_text' => $user_answer)); if (1 == $added) { ++$answered; } break; } } if ($answered == $num_questions) { $grade_data = array('lesson_id' => $lesson_id, 'entry_id' => $entry->ID); if ($automatic_grade) { $grade_data['grade'] = round($correct / $answered * 100); $grade_data['status'] = 'approved'; } else { $grade_data['grade'] = 0; $grade_data['status'] = 'pending'; } $api->add_quiz_grade($grade_data); wp_redirect(ib_edu_get_endpoint_url('edu-message', 'quiz-submitted', get_permalink())); exit; } }
/** * Submit quiz. */ public static function submit_quiz() { if (empty($_POST)) { return; } $lesson_id = get_the_ID(); check_admin_referer('edr_submit_quiz_' . $lesson_id); $user_id = get_current_user_id(); if (!$user_id) { return; } $quizzes = Edr_Manager::get('edr_quizzes'); $questions = $quizzes->get_questions($lesson_id); if (empty($questions)) { return; } if (!isset($_POST['answers']) || !is_array($_POST['answers'])) { // No answers. ib_edu_message('quiz', 'empty-answers'); return; } else { foreach ($questions as $question) { if (empty($_POST['answers'][$question->ID])) { // Not all questions were answered. ib_edu_message('quiz', 'empty-answers'); return; } } } $entry = IB_Educator::get_instance()->get_entry(array('user_id' => $user_id, 'course_id' => ib_edu_get_course_id($lesson_id), 'entry_status' => 'inprogress')); $entry_id = $entry ? $entry->ID : 0; if (!$entry_id && 'ib_educator_lesson' == get_post_type()) { return; } $max_attempts_number = $quizzes->get_max_attempts_number($lesson_id); if (!is_numeric($max_attempts_number)) { $max_attempts_number = 1; } $attempts_number = $quizzes->get_attempts_number($lesson_id, $entry_id); // Check if the student exceeded the number of allowed attempts. if ($attempts_number >= $max_attempts_number) { return; } // Add initial grade data to the database. $grade_id = $quizzes->add_grade(array('lesson_id' => $lesson_id, 'entry_id' => $entry_id, 'user_id' => $user_id, 'grade' => 0, 'status' => 'pending')); if (!$grade_id) { return; } $user_answer = ''; $correct = 0; $automatic_grade = true; $choices = $quizzes->get_choices($lesson_id, true); // Check answers to the quiz questions. foreach ($questions as $question) { // Every question type needs a specific way to check for the valid answer. switch ($question->question_type) { // Multiple Choice Question. case 'multiplechoice': $user_answer = absint($_POST['answers'][$question->ID]); if (isset($choices[$question->ID]) && isset($choices[$question->ID][$user_answer])) { $choice = $choices[$question->ID][$user_answer]; $answer_data = apply_filters('edr_submit_answer_pre', array('question_id' => $question->ID, 'grade_id' => $grade_id, 'entry_id' => $entry_id, 'correct' => $choice->correct, 'choice_id' => $choice->ID), $question); $added = $quizzes->add_answer($answer_data); if (1 == $choice->correct) { ++$correct; } } break; // Written Answer Question. // Written Answer Question. case 'writtenanswer': // We cannot check written answers automatically. if ($automatic_grade) { $automatic_grade = false; } $user_answer = stripslashes($_POST['answers'][$question->ID]); if (empty($user_answer)) { continue; } $answer_data = apply_filters('edr_submit_answer_pre', array('question_id' => $question->ID, 'grade_id' => $grade_id, 'entry_id' => $entry_id, 'correct' => -1, 'answer_text' => $user_answer), $question); $added = $quizzes->add_answer($answer_data); break; } } if ($automatic_grade) { $quizzes->update_grade($grade_id, array('grade' => round($correct / count($questions) * 100), 'status' => 'approved')); } wp_redirect(ib_edu_get_endpoint_url('edu-message', 'quiz-submitted', get_permalink())); exit; }
?> </h1> <div id="ib-edu-breadcrumbs"><?php ib_edu_breadcrumbs(); ?> </div> <div class="lesson-content entry-content"> <?php if ($student_can_study) { the_content(); Edr_View::template_part('quiz'); } else { echo '<p>'; printf(__('Please register for the %s to view this lesson.', 'ibeducator'), '<a href="' . esc_url(get_permalink(ib_edu_get_course_id())) . '">' . __('course', 'ibeducator') . '</a>'); echo '</p>'; } ?> </div> <nav class="ib-edu-lesson-nav"> <?php echo ib_edu_get_adjacent_lesson_link('previous', '<div class="nav-previous">« %link</div>', __('Previous Lesson', 'ibeducator')); echo ib_edu_get_adjacent_lesson_link('next', '<div class="nav-next">%link »</div>', __('Next Lesson', 'ibeducator')); ?> </nav> </article> <?php // Comments.
<?php if ($student_can_study) { ?> <div class="preview space-30"> <?php get_template_part('templates/content/content', get_post_format()); ?> </div> <?php the_content(); Edr_View::template_part('quiz'); } else { echo '<p>'; printf(esc_html__('Please register for the %s to view this lesson.', 'training'), '<a href="' . esc_url(get_permalink(ib_edu_get_course_id())) . '">' . esc_html__('course', 'training') . '</a>'); echo '</p>'; } ?> </div> <nav class="ib-edu-lesson-nav"> <?php echo ib_edu_get_adjacent_lesson_link('previous', '<div class="nav-previous">« %link</div>', esc_html__('Previous Lesson', 'training')); echo ib_edu_get_adjacent_lesson_link('next', '<div class="nav-next">%link »</div>', esc_html__('Next Lesson', 'training')); ?> </nav> </article> </div> </div>