$lastattemptid = false;
    $attemptnumber = 1;
}
/// Check access.
$messages = $accessmanager->prevent_access() + $accessmanager->prevent_new_attempt($attemptnumber - 1, $lastattempt);
if (!$quizobj->is_preview_user() && $messages) {
    print_error('attempterror', 'quiz', $quizobj->view_url(), $accessmanager->print_messages($messages, true));
}
$accessmanager->do_password_check($quizobj->is_preview_user());
/// Delete any previous preview attempts belonging to this user.
quiz_delete_previews($quiz, $USER->id);
/// Create the new attempt and initialize the question sessions
$attempt = quiz_create_attempt($quiz, $attemptnumber, $lastattempt, time(), $quizobj->is_preview_user());
/// Save the attempt in the database.
if (!($attempt->id = $DB->insert_record('quiz_attempts', $attempt))) {
    quiz_error($quiz, 'newattemptfail');
}
/// Log the new attempt.
if ($attempt->preview) {
    add_to_log($course->id, 'quiz', 'preview', 'view.php?id=' . $quizobj->get_cmid(), $quizobj->get_quizid(), $quizobj->get_cmid());
} else {
    add_to_log($course->id, 'quiz', 'attempt', 'review.php?attempt=' . $attempt->id, $quizobj->get_quizid(), $quizobj->get_cmid());
}
/// Fully load all the questions in this quiz.
$quizobj->preload_questions();
$quizobj->load_questions();
/// Create initial states for all questions in this quiz.
if (!($states = get_question_states($quizobj->get_questions(), $quizobj->get_quiz(), $attempt, $lastattemptid))) {
    print_error('cannotrestore', 'quiz');
}
/// Save all the newly created states.
Example #2
0
if ($showall !== 0) {
    $url->param('showall', $showall);
}
$PAGE->set_url($url);
$attemptobj = quiz_attempt::create($attemptid);
/// Check login.
require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
$attemptobj->check_review_capability();
/// Create an object to manage all the other (non-roles) access rules.
$accessmanager = $attemptobj->get_access_manager(time());
$options = $attemptobj->get_review_options();
/// Permissions checks for normal users who do not have quiz:viewreports capability.
if (!$attemptobj->has_capability('mod/quiz:viewreports')) {
    /// Can't review other users' attempts.
    if (!$attemptobj->is_own_attempt()) {
        quiz_error($attemptobj->get_quiz(), 'notyourattempt');
    }
    /// Can't review during the attempt - send them back to the attempt page.
    if (!$attemptobj->is_finished()) {
        redirect($attemptobj->attempt_url(0, $page));
    }
    /// Can't review unless Students may review -> Responses option is turned on.
    if (!$options->responses) {
        $accessmanager->back_to_view_page($attemptobj->is_preview_user(), $accessmanager->cannot_review_message($options));
    }
}
/// Load the questions and states needed by this page.
if ($showall) {
    $questionids = $attemptobj->get_question_ids();
} else {
    $questionids = $attemptobj->get_question_ids($page);
Example #3
0
} else {
    $submittedquestionids = array();
}
if ($finishattempt) {
    $questionids = $attemptobj->get_question_ids();
} else {
    $questionids = $submittedquestionids;
}
/// Load those questions we need, and just the submitted states for now.
$attemptobj->load_questions($questionids);
if (!empty($submittedquestionids)) {
    $attemptobj->load_question_states($submittedquestionids);
}
/// Process the responses /////////////////////////////////////////////////
if (!($responses = data_submitted())) {
    quiz_error($attemptobj->get_quiz(), 'nodatasubmitted');
}
/// Set the default event. This can be overruled by individual buttons.
if ($finishattempt) {
    $event = QUESTION_EVENTCLOSE;
} else {
    $event = QUESTION_EVENTSAVE;
}
/// Unset any variables we know are not responses
unset($responses->id);
unset($responses->q);
unset($responses->oldpage);
unset($responses->newpage);
unset($responses->review);
unset($responses->questionids);
unset($responses->finishattempt);
    $action->responses = $state->responses;
    $action->responses['_flagged'] = $state->flagged;
    $action->timestamp = $state->timestamp;
    if (question_process_responses($attemptobj->get_question($id), $state, $action, $attemptobj->get_quiz(), $attempt)) {
        save_question_session($attemptobj->get_question($id), $state);
    } else {
        $success = false;
    }
}
if (!$success) {
    print_error('errorprocessingresponses', 'question', $attemptobj->attempt_url(0, $page));
}
/// Log the end of this attempt.
add_to_log($attemptobj->get_courseid(), 'quiz', 'close attempt', 'review.php?attempt=' . $attemptobj->get_attemptid(), $attemptobj->get_quizid(), $attemptobj->get_cmid());
/// Update the quiz attempt record.
$attempt->timemodified = $timenow;
$attempt->timefinish = $timenow;
if (!$DB->update_record('quiz_attempts', $attempt)) {
    quiz_error($attemptobj->get_quiz(), 'saveattemptfailed');
}
if (!$attempt->preview) {
    /// Record this user's best grade (if this is not a preview).
    quiz_save_best_grade($attemptobj->get_quiz());
    /// Send any notification emails (if this is not a preview).
    $attemptobj->quiz_send_notification_emails();
}
/// Clear the password check flag in the session.
$accessmanager = $attemptobj->get_access_manager($timenow);
$accessmanager->clear_password_access();
/// Send the user to the review page.
redirect($attemptobj->review_url());
Example #5
0
if ($attemptobj->is_finished()) {
    redirect($attemptobj->review_url(0, $page));
}
/// Check the access rules.
$accessmanager = $attemptobj->get_access_manager(time());
$messages = $accessmanager->prevent_access();
if (!$attemptobj->is_preview_user() && $messages) {
    print_error('attempterror', 'quiz', $quizobj->view_url(), $accessmanager->print_messages($messages, true));
}
$accessmanager->do_password_check($attemptobj->is_preview_user());
add_to_log($attemptobj->get_courseid(), 'quiz', 'continue attempt', 'review.php?attempt=' . $attemptobj->get_attemptid(), $attemptobj->get_quizid(), $attemptobj->get_cmid());
/// Get the list of questions needed by this page.
$questionids = $attemptobj->get_question_ids($page);
/// Check.
if (empty($questionids)) {
    quiz_error($quiz, 'noquestionsfound');
}
/// Load those questions and the associated states.
$attemptobj->load_questions($questionids);
$attemptobj->load_question_states($questionids);
/// Print the quiz page ////////////////////////////////////////////////////////
// Initialise the JavaScript.
$headtags = $attemptobj->get_html_head_contributions($page);
$PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
// Arrange for the navigation to be displayed.
$navbc = $attemptobj->get_navigation_panel('quiz_attempt_nav_panel', $page);
$firstregion = reset($PAGE->blocks->get_regions());
$PAGE->blocks->add_fake_block($navbc, $firstregion);
// Print the page header
$title = get_string('attempt', 'quiz', $attemptobj->get_attempt_number());
$PAGE->set_heading($attemptobj->get_course()->fullname);
Example #6
0
$attemptobj = new quiz_attempt($attemptid);
/// Check login.
require_login($attemptobj->get_courseid(), false, $attemptobj->get_cm());
$attemptobj->check_review_capability();
/// Create an object to manage all the other (non-roles) access rules.
$accessmanager = $attemptobj->get_access_manager(time());
$options = $attemptobj->get_review_options();
/// Permissions checks for normal users who do not have quiz:viewreports capability.
if (!$attemptobj->has_capability('mod/quiz:viewreports')) {
    /// Can't review during the attempt - send them back to the attempt page.
    if (!$attemptobj->is_finished()) {
        redirect($attemptobj->attempt_url(0, $page));
    }
    /// Can't review other users' attempts.
    if (!$attemptobj->is_own_attempt()) {
        quiz_error($quiz, 'notyourattempt');
    }
    /// Can't review unless Students may review -> Responses option is turned on.
    if (!$options->responses) {
        $accessmanager->back_to_view_page($attemptobj->is_preview_user(), $accessmanager->cannot_review_message($options));
    }
}
/// Load the questions and states needed by this page.
if ($showall) {
    $questionids = $attemptobj->get_question_ids();
} else {
    $questionids = $attemptobj->get_question_ids($page);
}
$attemptobj->load_questions($questionids);
$attemptobj->load_question_states($questionids);
/// Save the flag states, if they are being changed.