$viewobj->gradecolumn = $someoptions->marks >= question_display_options::MARK_AND_MAX && quiz_has_grades($quiz); $viewobj->markcolumn = $viewobj->gradecolumn && ($quiz->grade != $quiz->sumgrades); $viewobj->overallstats = $lastfinishedattempt && $alloptions->marks >= question_display_options::MARK_AND_MAX; $viewobj->feedbackcolumn = quiz_has_feedback($quiz) && $alloptions->overallfeedback; } else { $viewobj->attemptcolumn = 1; } $viewobj->timenow = $timenow; $viewobj->numattempts = $numattempts; $viewobj->mygrade = $mygrade; $viewobj->moreattempts = $unfinished || !$accessmanager->is_finished($numattempts, $lastfinishedattempt); $viewobj->mygradeoverridden = $mygradeoverridden; $viewobj->gradebookfeedback = $gradebookfeedback; $viewobj->lastfinishedattempt = $lastfinishedattempt; $viewobj->canedit = has_capability('mod/quiz:manage', $context); $viewobj->editurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cm->id)); $viewobj->backtocourseurl = new moodle_url('/course/view.php', array('id' => $course->id)); $viewobj->startattempturl = $quizobj->start_attempt_url(); $viewobj->startattemptwarning = $quizobj->confirm_start_attempt_message($unfinished); $viewobj->popuprequired = $accessmanager->attempt_must_be_in_popup(); $viewobj->popupoptions = $accessmanager->get_popup_options(); // Display information about this quiz. $viewobj->infomessages = $viewobj->accessmanager->describe_rules(); if ($quiz->attempts != 1) { $viewobj->infomessages[] = get_string('gradingmethod', 'quiz',
/** * Return access information for a given attempt in a quiz. * * @param int $quizid quiz instance id * @param int $attemptid attempt id, 0 for the user last attempt if exists * @return array of warnings and the access information * @since Moodle 3.1 * @throws moodle_quiz_exception */ public static function get_attempt_access_information($quizid, $attemptid = 0) { global $DB, $USER; $warnings = array(); $params = array('quizid' => $quizid, 'attemptid' => $attemptid); $params = self::validate_parameters(self::get_attempt_access_information_parameters(), $params); list($quiz, $course, $cm, $context) = self::validate_quiz($params['quizid']); $attempttocheck = 0; if (!empty($params['attemptid'])) { $attemptobj = quiz_attempt::create($params['attemptid']); if ($attemptobj->get_userid() != $USER->id) { throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt'); } $attempttocheck = $attemptobj->get_attempt(); } // Access manager now. $quizobj = quiz::create($cm->instance, $USER->id); $ignoretimelimits = has_capability('mod/quiz:ignoretimelimits', $context, null, false); $timenow = time(); $accessmanager = new quiz_access_manager($quizobj, $timenow, $ignoretimelimits); $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'finished', true); $lastfinishedattempt = end($attempts); if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) { $attempts[] = $unfinishedattempt; // Check if the attempt is now overdue. In that case the state will change. $quizobj->create_attempt_object($unfinishedattempt)->handle_if_time_expired(time(), false); if ($unfinishedattempt->state != quiz_attempt::IN_PROGRESS and $unfinishedattempt->state != quiz_attempt::OVERDUE) { $lastfinishedattempt = $unfinishedattempt; } } $numattempts = count($attempts); if (!$attempttocheck) { $attempttocheck = $unfinishedattempt ? $unfinishedattempt : $lastfinishedattempt; } $result = array(); $result['isfinished'] = $accessmanager->is_finished($numattempts, $lastfinishedattempt); $result['preventnewattemptreasons'] = $accessmanager->prevent_new_attempt($numattempts, $lastfinishedattempt); if ($attempttocheck) { $endtime = $accessmanager->get_end_time($attempttocheck); $result['endtime'] = $endtime === false ? 0 : $endtime; $attemptid = $unfinishedattempt ? $unfinishedattempt->id : null; $result['ispreflightcheckrequired'] = $accessmanager->is_preflight_check_required($attemptid); } $result['warnings'] = $warnings; return $result; }
/** * Obtains the automatic completion state for this quiz on any conditions * in quiz settings, such as if all attempts are used or a certain grade is achieved. * * @param object $course Course * @param object $cm Course-module * @param int $userid User ID * @param bool $type Type of comparison (or/and; can be used as return value if no conditions) * @return bool True if completed, false if not. (If no conditions, then return * value depends on comparison type) */ function quiz_get_completion_state($course, $cm, $userid, $type) { global $DB; global $CFG; $quiz = $DB->get_record('quiz', array('id' => $cm->instance), '*', MUST_EXIST); if (!$quiz->completionattemptsexhausted && !$quiz->completionpass) { return $type; } // Check if the user has used up all attempts. if ($quiz->completionattemptsexhausted) { $attempts = quiz_get_user_attempts($quiz->id, $userid, 'finished', true); if ($attempts) { $lastfinishedattempt = end($attempts); $context = context_module::instance($cm->id); $quizobj = quiz::create($quiz->id, $userid); $accessmanager = new quiz_access_manager($quizobj, time(), has_capability('mod/quiz:ignoretimelimits', $context, $userid, false)); if ($accessmanager->is_finished(count($attempts), $lastfinishedattempt)) { return true; } } } // Check for passing grade. if ($quiz->completionpass) { require_once $CFG->libdir . '/gradelib.php'; $item = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $cm->instance, 'outcomeid' => null)); if ($item) { $grades = grade_grade::fetch_users_grades($item, array($userid), false); if (!empty($grades[$userid])) { return $grades[$userid]->is_passed($item); } } } return false; }
$PAGE->set_heading($course->fullname); $output = $PAGE->get_renderer('mod_quiz'); // Print table with existing attempts. if ($attempts) { // Work out which columns we need, taking account what data is available in each attempt. list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts, $context); $viewobj->attemptcolumn = $quiz->attempts != 1; $viewobj->gradecolumn = $someoptions->marks >= question_display_options::MARK_AND_MAX && quiz_has_grades($quiz); $viewobj->markcolumn = $viewobj->gradecolumn && $quiz->grade != $quiz->sumgrades; $viewobj->overallstats = $lastfinishedattempt && $alloptions->marks >= question_display_options::MARK_AND_MAX; $viewobj->feedbackcolumn = quiz_has_feedback($quiz) && $alloptions->overallfeedback; } $viewobj->timenow = $timenow; $viewobj->numattempts = $numattempts; $viewobj->mygrade = $mygrade; $viewobj->moreattempts = $unfinished || !$accessmanager->is_finished($numattempts, $lastfinishedattempt); $viewobj->mygradeoverridden = $mygradeoverridden; $viewobj->gradebookfeedback = $gradebookfeedback; $viewobj->lastfinishedattempt = $lastfinishedattempt; $viewobj->canedit = has_capability('mod/quiz:manage', $context); $viewobj->editurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cm->id)); $viewobj->backtocourseurl = new moodle_url('/course/view.php', array('id' => $course->id)); $viewobj->startattempturl = $quizobj->start_attempt_url(); $viewobj->startattemptwarning = $quizobj->confirm_start_attempt_message($unfinished); $viewobj->popuprequired = $accessmanager->attempt_must_be_in_popup(); $viewobj->popupoptions = $accessmanager->get_popup_options(); // Display information about this quiz. $viewobj->infomessages = $viewobj->accessmanager->describe_rules(); if ($quiz->attempts != 1) { $viewobj->infomessages[] = get_string('gradingmethod', 'quiz', quiz_get_grading_option_name($quiz->grademethod)); }
function prepareQuizAttempt($q) { global $DB; if ($id) { if (!($cm = get_coursemodule_from_id('quiz', $id))) { print_error('invalidcoursemodule'); } if (!($course = $DB->get_record('course', array('id' => $cm->course)))) { print_error('coursemisconf'); } if (!($quiz = $DB->get_record('quiz', array('id' => $cm->instance)))) { print_error('invalidcoursemodule'); } } else { if (!($quiz = $DB->get_record('quiz', array('id' => $q)))) { print_error('invalidquizid', 'quiz'); } if (!($course = $DB->get_record('course', array('id' => $quiz->course)))) { print_error('invalidcourseid'); } if (!($cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id))) { print_error('invalidcoursemodule'); } } /// Check login and get context. require_login($course->id, false, $cm); $context = get_context_instance(CONTEXT_MODULE, $cm->id); require_capability('mod/quiz:view', $context); /// Cache some other capabilites we use several times. $canattempt = has_capability('mod/quiz:attempt', $context); $canreviewmine = has_capability('mod/quiz:reviewmyattempts', $context); $canpreview = has_capability('mod/quiz:preview', $context); /// Create an object to manage all the other (non-roles) access rules. $timenow = time(); $accessmanager = new quiz_access_manager(new quiz($quiz, $cm, $course), $timenow, has_capability('mod/quiz:ignoretimelimits', $context, NULL, false)); /// Print information about the student's best score for this quiz if possible. $moreattempts = $unfinished || !$accessmanager->is_finished($numattempts, $lastfinishedattempt); /// Determine if we should be showing a start/continue attempt button, /// or a button to go back to the course page. print_box_start('quizattempt'); $buttontext = ''; // This will be set something if as start/continue attempt button should appear. if (!$quiz->questions) { print_heading(get_string("noquestions", "quiz")); } else { if ($unfinished) { if ($canattempt) { $buttontext = get_string('continueattemptquiz', 'quiz'); } else { if ($canpreview) { $buttontext = get_string('continuepreview', 'quiz'); } } } else { if ($canattempt) { $messages = $accessmanager->prevent_new_attempt($numattempts, $lastfinishedattempt); if ($messages) { $accessmanager->print_messages($messages); } else { if ($numattempts == 0) { $buttontext = get_string('attemptquiznow', 'quiz'); } else { $buttontext = get_string('reattemptquiz', 'quiz'); } } } else { if ($canpreview) { $buttontext = get_string('previewquiznow', 'quiz'); } } } // If, so far, we think a button should be printed, so check if they will be allowed to access it. if ($buttontext) { if (!$moreattempts) { $buttontext = ''; } else { if ($canattempt && ($messages = $accessmanager->prevent_access())) { $accessmanager->print_messages($messages); $buttontext = ''; } } } } /// Now actually print the appropriate button. if ($buttontext) { $accessmanager->print_start_attempt_button($canpreview, $buttontext, $unfinished); } else { print_continue($CFG->wwwroot . '/course/view.php?id=' . $course->id); } print_box_end(); }