Пример #1
0
 /**
  * Starts a new attempt at a quiz.
  *
  * @param int $quizid quiz instance id
  * @param array $preflightdata preflight required data (like passwords)
  * @param bool $forcenew Whether to force a new attempt or not.
  * @return array of warnings and the attempt basic data
  * @since Moodle 3.1
  * @throws moodle_quiz_exception
  */
 public static function start_attempt($quizid, $preflightdata = array(), $forcenew = false)
 {
     global $DB, $USER;
     $warnings = array();
     $attempt = array();
     $params = array('quizid' => $quizid, 'preflightdata' => $preflightdata, 'forcenew' => $forcenew);
     $params = self::validate_parameters(self::start_attempt_parameters(), $params);
     $forcenew = $params['forcenew'];
     list($quiz, $course, $cm, $context) = self::validate_quiz($params['quizid']);
     $quizobj = quiz::create($cm->instance, $USER->id);
     // Check questions.
     if (!$quizobj->has_questions()) {
         throw new moodle_quiz_exception($quizobj, 'noquestionsfound');
     }
     // Create an object to manage all the other (non-roles) access rules.
     $timenow = time();
     $accessmanager = $quizobj->get_access_manager($timenow);
     // Validate permissions for creating a new attempt and start a new preview attempt if required.
     list($currentattemptid, $attemptnumber, $lastattempt, $messages, $page) = quiz_validate_new_attempt($quizobj, $accessmanager, $forcenew, -1, false);
     // Check access.
     if (!$quizobj->is_preview_user() && $messages) {
         // Create warnings with the exact messages.
         foreach ($messages as $message) {
             $warnings[] = array('item' => 'quiz', 'itemid' => $quiz->id, 'warningcode' => '1', 'message' => clean_text($message, PARAM_TEXT));
         }
     } else {
         if ($accessmanager->is_preflight_check_required($currentattemptid)) {
             // Need to do some checks before allowing the user to continue.
             $provideddata = array();
             foreach ($params['preflightdata'] as $data) {
                 $provideddata[$data['name']] = $data['value'];
             }
             $errors = $accessmanager->validate_preflight_check($provideddata, [], $currentattemptid);
             if (!empty($errors)) {
                 throw new moodle_quiz_exception($quizobj, array_shift($errors));
             }
             // Pre-flight check passed.
             $accessmanager->notify_preflight_check_passed($currentattemptid);
         }
         if ($currentattemptid) {
             if ($lastattempt->state == quiz_attempt::OVERDUE) {
                 throw new moodle_quiz_exception($quizobj, 'stateoverdue');
             } else {
                 throw new moodle_quiz_exception($quizobj, 'attemptstillinprogress');
             }
         }
         $attempt = quiz_prepare_and_start_new_attempt($quizobj, $attemptnumber, $lastattempt);
     }
     $result = array();
     $result['attempt'] = $attempt;
     $result['warnings'] = $warnings;
     return $result;
 }
Пример #2
0
require_login($quizobj->get_course(), false, $quizobj->get_cm());
require_sesskey();
$PAGE->set_heading($quizobj->get_course()->fullname);
// If no questions have been set up yet redirect to edit.php or display an error.
if (!$quizobj->has_questions()) {
    if ($quizobj->has_capability('mod/quiz:manage')) {
        redirect($quizobj->edit_url());
    } else {
        print_error('cannotstartnoquestions', 'quiz', $quizobj->view_url());
    }
}
// Create an object to manage all the other (non-roles) access rules.
$timenow = time();
$accessmanager = $quizobj->get_access_manager($timenow);
// Validate permissions for creating a new attempt and start a new preview attempt if required.
list($currentattemptid, $attemptnumber, $lastattempt, $messages, $page) = quiz_validate_new_attempt($quizobj, $accessmanager, $forcenew, $page, true);
// Check access.
if (!$quizobj->is_preview_user() && $messages) {
    $output = $PAGE->get_renderer('mod_quiz');
    print_error('attempterror', 'quiz', $quizobj->view_url(), $output->access_messages($messages));
}
if ($accessmanager->is_preflight_check_required($currentattemptid)) {
    // Need to do some checks before allowing the user to continue.
    $mform = $accessmanager->get_preflight_check_form($quizobj->start_attempt_url($page), $currentattemptid);
    if ($mform->is_cancelled()) {
        $accessmanager->back_to_view_page($PAGE->get_renderer('mod_quiz'));
    } else {
        if (!$mform->get_data()) {
            // Form not submitted successfully, re-display it and stop.
            $PAGE->set_url($quizobj->start_attempt_url($page));
            $PAGE->set_title($quizobj->get_quiz_name());