Beispiel #1
0
/**
 * Fire an event to tell the rest of Moodle a quiz attempt has started.
 *
 * @param object $attempt
 * @param quiz   $quizobj
 */
function quiz_fire_attempt_started_event($attempt, $quizobj) {
    // Trigger event.
    $eventdata = array();
    $eventdata['context'] = $quizobj->get_context();
    $eventdata['courseid'] = $quizobj->get_courseid();
    $eventdata['relateduserid'] = $attempt->userid;
    $eventdata['objectid'] = $attempt->id;
    $event = \mod_quiz\event\attempt_started::create($eventdata);
    $event->add_record_snapshot('quiz', $quizobj->get_quiz());
    $event->add_record_snapshot('quiz_attempts', $attempt);
    $event->trigger();
}
$id = required_param('cmid', PARAM_INT);
// Course Module ID
$forcenew = optional_param('forcenew', false, PARAM_BOOL);
// Used to force a new preview
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');
}
$quizobj = new quiz($quiz, $cm, $course);
/// Check login and sesskey.
require_login($quizobj->get_courseid(), false, $quizobj->get_cm());
if (!confirm_sesskey()) {
    throw new moodle_exception('confirmsesskeybad', 'error', $quizobj->view_url());
}
/// if no questions have been set up yet redirect to edit.php
if (!$quizobj->get_question_ids() && $quizobj->has_capability('mod/quiz:manage')) {
    redirect($quizobj->edit_url());
}
/// Create an object to manage all the other (non-roles) access rules.
$accessmanager = $quizobj->get_access_manager(time());
if ($quizobj->is_preview_user() && $forcenew) {
    $accessmanager->clear_password_access();
}
/// Check capabilites.
if (!$quizobj->is_preview_user()) {
    $quizobj->require_capability('mod/quiz:attempt');
/**
 * The save started question usage and quiz attempt in db and log the started attempt.
 *
 * @param quiz                       $quizobj
 * @param question_usage_by_activity $quba
 * @param object                     $attempt
 * @return object                    attempt object with uniqueid and id set.
 */
function quiz_attempt_save_started($quizobj, $quba, $attempt)
{
    global $DB;
    // Save the attempt in the database.
    question_engine::save_questions_usage_by_activity($quba);
    $attempt->uniqueid = $quba->get_id();
    $attempt->id = $DB->insert_record('quiz_attempts', $attempt);
    // Params used by the events below.
    $params = array('objectid' => $attempt->id, 'relateduserid' => $attempt->userid, 'courseid' => $quizobj->get_courseid(), 'context' => $quizobj->get_context());
    // Decide which event we are using.
    if ($attempt->preview) {
        $params['other'] = array('quizid' => $quizobj->get_quizid());
        $event = \mod_quiz\event\attempt_preview_started::create($params);
    } else {
        $event = \mod_quiz\event\attempt_started::create($params);
    }
    // Trigger the event.
    $event->add_record_snapshot('quiz', $quizobj->get_quiz());
    $event->add_record_snapshot('quiz_attempts', $attempt);
    $event->trigger();
    return $attempt;
}