/** * 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; }
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(); }
$buttontext = ''; // This will be set something if as start/continue attempt button should appear. if (!$quiz->questions) { echo $OUTPUT->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'); } } }
/** * Validate permissions for creating a new attempt and start a new preview attempt if required. * * @param quiz $quizobj quiz object * @param quiz_access_manager $accessmanager quiz access manager * @param bool $forcenew whether was required to start a new preview attempt * @param int $page page to jump to in the attempt * @param bool $redirect whether to redirect or throw exceptions (for web or ws usage) * @return array an array containing the attempt information, access error messages and the page to jump to in the attempt * @throws moodle_quiz_exception * @since Moodle 3.1 */ function quiz_validate_new_attempt(quiz $quizobj, quiz_access_manager $accessmanager, $forcenew, $page, $redirect) { global $DB, $USER; $timenow = time(); if ($quizobj->is_preview_user() && $forcenew) { $accessmanager->current_attempt_finished(); } // Check capabilities. if (!$quizobj->is_preview_user()) { $quizobj->require_capability('mod/quiz:attempt'); } // Check to see if a new preview was requested. if ($quizobj->is_preview_user() && $forcenew) { // To force the creation of a new preview, we mark the current attempt (if any) // as finished. It will then automatically be deleted below. $DB->set_field('quiz_attempts', 'state', quiz_attempt::FINISHED, array('quiz' => $quizobj->get_quizid(), 'userid' => $USER->id)); } // Look for an existing attempt. $attempts = quiz_get_user_attempts($quizobj->get_quizid(), $USER->id, 'all', true); $lastattempt = end($attempts); $attemptnumber = null; // If an in-progress attempt exists, check password then redirect to it. if ($lastattempt && ($lastattempt->state == quiz_attempt::IN_PROGRESS || $lastattempt->state == quiz_attempt::OVERDUE)) { $currentattemptid = $lastattempt->id; $messages = $accessmanager->prevent_access(); // If the attempt is now overdue, deal with that. $quizobj->create_attempt_object($lastattempt)->handle_if_time_expired($timenow, true); // And, if the attempt is now no longer in progress, redirect to the appropriate place. if ($lastattempt->state == quiz_attempt::ABANDONED || $lastattempt->state == quiz_attempt::FINISHED) { if ($redirect) { redirect($quizobj->review_url($lastattempt->id)); } else { throw new moodle_quiz_exception($quizobj, 'attemptalreadyclosed'); } } // If the page number was not explicitly in the URL, go to the current page. if ($page == -1) { $page = $lastattempt->currentpage; } } else { while ($lastattempt && $lastattempt->preview) { $lastattempt = array_pop($attempts); } // Get number for the next or unfinished attempt. if ($lastattempt) { $attemptnumber = $lastattempt->attempt + 1; } else { $lastattempt = false; $attemptnumber = 1; } $currentattemptid = null; $messages = $accessmanager->prevent_access() + $accessmanager->prevent_new_attempt(count($attempts), $lastattempt); if ($page == -1) { $page = 0; } } return array($currentattemptid, $attemptnumber, $lastattempt, $messages, $page); }