/** * Get the entry IDs that have at least one quiz pending. * * @return array */ protected function get_pending_quiz_entries() { if (null === $this->pending_quiz_entries) { if (is_array($this->items)) { $entry_ids = array(); foreach ($this->items as $item) { $entry_ids[] = $item['ID']; } $this->pending_quiz_entries = Edr_Manager::get('edr_quizzes')->check_for_pending_quizzes($entry_ids); } else { $this->pending_quiz_entries = array(); } } return $this->pending_quiz_entries; }
/** * Test the "can_view_certificate" method. */ public function testCanViewCertificate() { global $current_user; // Create student. $student1_id = wp_insert_user(array('user_login' => 'student1', 'user_pass' => '123456', 'role' => 'student')); // Create entry. $entry = Edr_Crt_Test::get_instance()->addEntry(array('user_id' => $student1_id, 'course_id' => 1, 'entry_status' => 'complete')); // Create certificate. $edr_crt = Edr_Manager::get('edr_crt'); $certificate_id = $edr_crt->create_certificate($entry); $certificate = get_post($certificate_id); // Guest user. $this->assertEquals(0, get_current_user_id()); $this->assertFalse($edr_crt->can_view_certificate($certificate)); // Student 1. $current_user = new WP_User($student1_id); $this->assertEquals($student1_id, get_current_user_id()); $this->assertTrue($edr_crt->can_view_certificate($certificate)); // Administrator. $current_user = new WP_User(1); $this->assertEquals(1, get_current_user_id()); $this->assertTrue($edr_crt->can_view_certificate($certificate)); }
public function testAddAnswer() { $quizzes = Edr_Manager::get('edr_quizzes'); $questions = $quizzes->get_questions($this->lessons[0]); $question1 = reset($questions); $choices = $quizzes->get_choices($this->lessons[0], true); $choice1 = reset($choices[$question1->ID]); $grade_id = $quizzes->add_grade(array('lesson_id' => $this->lessons[0], 'entry_id' => $this->entries['inprogress'], 'grade' => 80.29000000000001, 'status' => 'approved')); $answer_data = array('question_id' => $question1->ID, 'grade_id' => $grade_id, 'entry_id' => $this->entries['inprogress'], 'correct' => $choice1->correct, 'choice_id' => $choice1->ID, 'answer_text' => 'abc'); $quizzes->add_answer($answer_data); // Get original answer data as object. $original_answer_object = new stdClass(); foreach ($answer_data as $key => $value) { $original_answer_object->{$key} = $value; } // Get the created answer. $answers = $quizzes->get_answers($grade_id); $answer = reset($answers); unset($answer->ID); $this->assertEquals($original_answer_object, $answer); }
/** * Output the data for the custom columns on the certificates list page. * * @param string $column * @param int $post_id * @return array */ public function output_certificate_column_values($column, $post_id) { switch ($column) { case 'student': $student_id = get_post_meta($post_id, 'student_id', true); if ($student_id && ($student = get_user_by('id', $student_id))) { echo esc_html(Edr_Manager::get('edr_crt')->get_student_name($student)); } break; case 'course': $course_id = get_post_meta($post_id, 'course_id', true); if ($course_id && ($course = get_post($course_id))) { echo esc_html($course->post_title); } break; case 'date_created': the_date(); break; } }
/** * Display the "view certificate" link in the completed courses table. * * @param array $values * @param string $status * @param IB_Educator_Entry $entry * @return array */ public function my_courses_certificate_link($values, $status, $entry) { if ('complete' == $status) { $certificate_url = Edr_Manager::get('edr_crt')->get_certificate_url($entry->ID); if (!empty($certificate_url)) { if (!isset($values['actions'])) { $values['actions'] = '<td><a href="' . esc_url($certificate_url) . '" target="_blank">' . __('View Certificate', 'edr-crt') . '</a></td>'; } } else { if (!isset($values['actions'])) { $values['actions'] = '<td></td>'; } } } return $values; }
<?php $quizzes = Edr_Manager::get('edr_quizzes'); $lesson_id = (int) $post->ID; $attempts_number = get_post_meta($lesson_id, '_edr_attempts', true); if (!$attempts_number) { $attempts_number = 1; } ?> <h2><?php _e('Settings', 'ibeducator'); ?> </h2> <div class="ib-edu-field"> <div class="ib-edu-label"> <label for="edr-attempts-number"><?php _e('Number of attempts', 'ibeducator'); ?> </label> </div> <div class="ib-edu-control"> <input type="number" id="edr-attempts-number" name="_edr_attempts" value="<?php echo intval($attempts_number); ?> "> </div> </div> <h2><?php
/** * 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; }
/** * Get the entries with ungraded quizzes. * * @param array $ids * @return array */ public function check_quiz_pending($ids) { _ib_edu_deprecated_function('IB_Educator::check_quiz_pending', '1.6', 'Edr_Quizzes::check_for_pending_quizzes'); return Edr_Manager::get('edr_quizzes')->check_for_pending_quizzes($ids); }
/** * AJAX: add grade for a quiz. */ public static function quiz_grade() { global $wpdb; $entry_id = isset($_POST['entry_id']) ? absint($_POST['entry_id']) : 0; $lesson_id = isset($_POST['lesson_id']) ? absint($_POST['lesson_id']) : 0; // Verify nonce. if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'edr_edit_progress_' . $entry_id)) { exit; } // Verify capabilities. if (!current_user_can('edit_ib_educator_lesson', $lesson_id)) { exit; } $quizzes = Edr_Manager::get('edr_quizzes'); $quiz_grade = $quizzes->get_grade($lesson_id, $entry_id); if (!$quiz_grade) { exit; } $grade = isset($_POST['grade']) ? floatval($_POST['grade']) : 0; $quizzes->update_grade($quiz_grade->ID, array('grade' => $grade, 'status' => 'approved')); // Send notification email to the student. $entry = edr_get_entry($entry_id); $student = get_user_by('id', $entry->user_id); if ($student) { $lesson_title = get_the_title($lesson_id); ib_edu_send_notification($student->user_email, 'quiz_grade', array('lesson_title' => $lesson_title), array('student_name' => $student->display_name, 'lesson_title' => $lesson_title, 'grade' => ib_edu_format_grade($grade))); } echo json_encode(array('status' => 'success')); exit; }