-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartattempt.php
More file actions
154 lines (121 loc) · 5.63 KB
/
startattempt.php
File metadata and controls
154 lines (121 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php // $Id:,v 2.0 2012/05/20 16:10:00 Serafim Panov
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot . '/mod/reader/attemptlib.php');
require_once($CFG->dirroot . '/mod/reader/lib.php');
require_once($CFG->dirroot . '/question/engine/lib.php');
// Get submitted parameters.
$id = required_param('id', PARAM_INT); // Course module id
$book = required_param('book', PARAM_INT); // Course module id
$page = optional_param('page', 0, PARAM_INT); // Page to jump to in the attempt.
if (!$cm = get_coursemodule_from_id('reader', $id)) {
print_error('invalidcoursemodule');
}
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
print_error("coursemisconf");
}
if (!$reader = $DB->get_record('reader', array('id' => $cm->instance))) {
print_error('invalidcoursemodule');
}
require_login($course, true, $cm);
$readerobj = reader::create($reader->id, $USER->id, $book);
// This script should only ever be posted to, so set page URL to the view page.
$PAGE->set_url($readerobj->view_url());
// Check login and sesskey.
$PAGE->set_pagelayout('base');
// if no questions have been set up yet redirect to edit.php
if (!$readerobj->has_questions()) {
redirect($readerobj->edit_url());
}
// Look for an existing attempt.
$attempts = reader_get_user_attempts($reader->id, $USER->id, 'all');
$lastattempt = end($attempts);
// If an in-progress attempt exists, check password then redirect to it.
if ($lastattempt && !$lastattempt->timefinish) {
redirect($readerobj->attempt_url($lastattempt->id, $page));
}
// Get number for the next or unfinished attempt ÏÐÎÂÅÐÈÒÜ ÏÎÑËÅÄÍÈÉ ÀÒÒÅÌÏÒ ÇÄÅÑÜ
$lastattempt = false;
$attemptnumber = 1;
$quba = question_engine::make_questions_usage_by_activity('mod_reader', $readerobj->get_context());
$quba->set_preferred_behaviour('deferredfeedback');
// Create the new attempt and initialize the question sessions
$attempt = reader_create_attempt($reader, $attemptnumber, $book);
if (!($reader->attemptonlast && $lastattempt)) {
// Starting a normal, new, reader attempt.
// Fully load all the questions in this reader.
$readerobj->preload_questions();
$readerobj->load_questions();
// Add them all to the $quba.
$idstoslots = array();
$questionsinuse = array_keys($readerobj->get_questions());
foreach ($readerobj->get_questions() as $i => $questiondata) {
if ($questiondata->qtype != 'random') {
if (!$reader->shuffleanswers) {
$questiondata->options->shuffleanswers = false;
}
$question = question_bank::make_question($questiondata);
} else {
$question = question_bank::get_qtype('random')->choose_other_question(
$questiondata, $questionsinuse, $reader->shuffleanswers);
if (is_null($question)) {
throw new moodle_exception('notenoughrandomquestions', 'reader',
$readerobj->view_url(), $questiondata);
}
}
$idstoslots[$i] = $quba->add_question($question, $questiondata->maxmark);
$questionsinuse[] = $question->id;
}
// Start all the questions.
$variantoffset = $attemptnumber;
$quba->start_all_questions(
new question_variant_pseudorandom_no_repeats_strategy($variantoffset),
time());
// Update attempt layout.
$newlayout = array();
foreach (explode(',', $attempt->layout) as $qid) {
if ($qid != 0) {
$newlayout[] = $idstoslots[$qid];
} else {
$newlayout[] = 0;
}
}
$attempt->layout = implode(',', $newlayout);
} else {
// Starting a subsequent attempt in each attempt builds on last mode.
$oldquba = question_engine::load_questions_usage_by_activity($lastattempt->uniqueid);
$oldnumberstonew = array();
foreach ($oldquba->get_attempt_iterator() as $oldslot => $oldqa) {
$newslot = $quba->add_question($oldqa->get_question(), $oldqa->get_max_mark());
$quba->start_question_based_on($newslot, $oldqa);
$oldnumberstonew[$oldslot] = $newslot;
}
// Update attempt layout.
$newlayout = array();
foreach (explode(',', $lastattempt->layout) as $oldslot) {
if ($oldslot != 0) {
$newlayout[] = $oldnumberstonew[$oldslot];
} else {
$newlayout[] = 0;
}
}
$attempt->layout = implode(',', $newlayout);
}
// Save the attempt in the database.
question_engine::save_questions_usage_by_activity($quba);
$attempt->uniqueid = $quba->get_id();
$attempt->id = $DB->insert_record('reader_attempts', $attempt);
// Log the new attempt.
add_to_log($course->id, 'reader', 'attempt', 'review.php?attempt=' . $attempt->id,
$id, $book);
// Trigger event
$eventdata = new stdClass();
$eventdata->component = 'mod_reader';
$eventdata->attemptid = $attempt->id;
$eventdata->timestart = $attempt->timestart;
$eventdata->userid = $attempt->userid;
$eventdata->readerid = $id;
$eventdata->cmid = $book;
$eventdata->courseid = $readerobj->get_courseid();
events_trigger('reader_attempt_started', $eventdata);
// Redirect to the attempt page.
redirect($readerobj->attempt_url($attempt->id, $page));