2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This page is the entry page into the quiz UI. Displays information about the
19 * quiz to students and teachers, and lets students see their previous attempts.
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once(dirname(__FILE__
) . '/../../config.php');
28 require_once($CFG->libdir
.'/gradelib.php');
29 require_once($CFG->dirroot
.'/mod/quiz/locallib.php');
30 require_once($CFG->libdir
. '/completionlib.php');
31 require_once($CFG->dirroot
. '/course/format/lib.php');
33 $id = optional_param('id', 0, PARAM_INT
); // Course Module ID, or ...
34 $q = optional_param('q', 0, PARAM_INT
); // Quiz ID.
37 if (!$cm = get_coursemodule_from_id('quiz', $id)) {
38 print_error('invalidcoursemodule');
40 if (!$course = $DB->get_record('course', array('id' => $cm->course
))) {
41 print_error('coursemisconf');
44 if (!$quiz = $DB->get_record('quiz', array('id' => $q))) {
45 print_error('invalidquizid', 'quiz');
47 if (!$course = $DB->get_record('course', array('id' => $quiz->course
))) {
48 print_error('invalidcourseid');
50 if (!$cm = get_coursemodule_from_instance("quiz", $quiz->id
, $course->id
)) {
51 print_error('invalidcoursemodule');
55 // Check login and get context.
56 require_login($course, false, $cm);
57 $context = context_module
::instance($cm->id
);
58 require_capability('mod/quiz:view', $context);
60 // Cache some other capabilities we use several times.
61 $canattempt = has_capability('mod/quiz:attempt', $context);
62 $canreviewmine = has_capability('mod/quiz:reviewmyattempts', $context);
63 $canpreview = has_capability('mod/quiz:preview', $context);
65 // Create an object to manage all the other (non-roles) access rules.
67 $quizobj = quiz
::create($cm->instance
, $USER->id
);
68 $accessmanager = new quiz_access_manager($quizobj, $timenow,
69 has_capability('mod/quiz:ignoretimelimits', $context, null, false));
70 $quiz = $quizobj->get_quiz();
74 'objectid' => $quiz->id
,
77 $event = \mod_quiz\event\course_module_viewed
::create($params);
78 $event->add_record_snapshot('quiz', $quiz);
81 $completion = new completion_info($course);
82 $completion->set_module_viewed($cm);
84 // Initialize $PAGE, compute blocks.
85 $PAGE->set_url('/mod/quiz/view.php', array('id' => $cm->id
));
87 // Create view object which collects all the information the renderer will need.
88 $viewobj = new mod_quiz_view_object();
89 $viewobj->accessmanager
= $accessmanager;
90 $viewobj->canreviewmine
= $canreviewmine;
92 // Get this user's attempts.
93 $attempts = quiz_get_user_attempts($quiz->id
, $USER->id
, 'finished', true);
94 $lastfinishedattempt = end($attempts);
96 if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id
, $USER->id
)) {
97 $attempts[] = $unfinishedattempt;
99 // If the attempt is now overdue, deal with that - and pass isonline = false.
100 // We want the student notified in this case.
101 $quizobj->create_attempt_object($unfinishedattempt)->handle_if_time_expired(time(), false);
103 $unfinished = $unfinishedattempt->state
== quiz_attempt
::IN_PROGRESS ||
104 $unfinishedattempt->state
== quiz_attempt
::OVERDUE
;
106 $lastfinishedattempt = $unfinishedattempt;
108 $unfinishedattempt = null; // To make it clear we do not use this again.
110 $numattempts = count($attempts);
112 $viewobj->attempts
= $attempts;
113 $viewobj->attemptobjs
= array();
114 foreach ($attempts as $attempt) {
115 $viewobj->attemptobjs
[] = new quiz_attempt($attempt, $quiz, $cm, $course, false);
118 // Work out the final grade, checking whether it was overridden in the gradebook.
120 $mygrade = quiz_get_best_grade($quiz, $USER->id
);
121 } else if ($lastfinishedattempt) {
122 // Users who can preview the quiz don't get a proper grade, so work out a
123 // plausible value to display instead, so the page looks right.
124 $mygrade = quiz_rescale_grade($lastfinishedattempt->sumgrades
, $quiz, false);
129 $mygradeoverridden = false;
130 $gradebookfeedback = '';
132 $grading_info = grade_get_grades($course->id
, 'mod', 'quiz', $quiz->id
, $USER->id
);
133 if (!empty($grading_info->items
)) {
134 $item = $grading_info->items
[0];
135 if (isset($item->grades
[$USER->id
])) {
136 $grade = $item->grades
[$USER->id
];
138 if ($grade->overridden
) {
139 $mygrade = $grade->grade +
0; // Convert to number.
140 $mygradeoverridden = true;
142 if (!empty($grade->str_feedback
)) {
143 $gradebookfeedback = $grade->str_feedback
;
148 $title = $course->shortname
. ': ' . format_string($quiz->name
);
149 $PAGE->set_title($title);
150 $PAGE->set_heading($course->fullname
);
151 $output = $PAGE->get_renderer('mod_quiz');
153 // Print table with existing attempts.
155 // Work out which columns we need, taking account what data is available in each attempt.
156 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts, $context);
158 $viewobj->attemptcolumn
= $quiz->attempts
!= 1;
160 $viewobj->gradecolumn
= $someoptions->marks
>= question_display_options
::MARK_AND_MAX
&&
161 quiz_has_grades($quiz);
162 $viewobj->markcolumn
= $viewobj->gradecolumn
&& ($quiz->grade
!= $quiz->sumgrades
);
163 $viewobj->overallstats
= $lastfinishedattempt && $alloptions->marks
>= question_display_options
::MARK_AND_MAX
;
165 $viewobj->feedbackcolumn
= quiz_has_feedback($quiz) && $alloptions->overallfeedback
;
168 $viewobj->timenow
= $timenow;
169 $viewobj->numattempts
= $numattempts;
170 $viewobj->mygrade
= $mygrade;
171 $viewobj->moreattempts
= $unfinished ||
172 !$accessmanager->is_finished($numattempts, $lastfinishedattempt);
173 $viewobj->mygradeoverridden
= $mygradeoverridden;
174 $viewobj->gradebookfeedback
= $gradebookfeedback;
175 $viewobj->lastfinishedattempt
= $lastfinishedattempt;
176 $viewobj->canedit
= has_capability('mod/quiz:manage', $context);
177 $viewobj->editurl
= new moodle_url('/mod/quiz/edit.php', array('cmid' => $cm->id
));
178 $viewobj->backtocourseurl
= new moodle_url('/course/view.php', array('id' => $course->id
));
179 $viewobj->startattempturl
= $quizobj->start_attempt_url();
180 $viewobj->startattemptwarning
= $quizobj->confirm_start_attempt_message($unfinished);
181 $viewobj->popuprequired
= $accessmanager->attempt_must_be_in_popup();
182 $viewobj->popupoptions
= $accessmanager->get_popup_options();
184 // Display information about this quiz.
185 $viewobj->infomessages
= $viewobj->accessmanager
->describe_rules();
186 if ($quiz->attempts
!= 1) {
187 $viewobj->infomessages
[] = get_string('gradingmethod', 'quiz',
188 quiz_get_grading_option_name($quiz->grademethod
));
191 // Determine wheter a start attempt button should be displayed.
192 $viewobj->quizhasquestions
= $quizobj->has_questions();
193 $viewobj->preventmessages
= array();
194 if (!$viewobj->quizhasquestions
) {
195 $viewobj->buttontext
= '';
200 $viewobj->buttontext
= get_string('continueattemptquiz', 'quiz');
201 } else if ($canpreview) {
202 $viewobj->buttontext
= get_string('continuepreview', 'quiz');
207 $viewobj->preventmessages
= $viewobj->accessmanager
->prevent_new_attempt(
208 $viewobj->numattempts
, $viewobj->lastfinishedattempt
);
209 if ($viewobj->preventmessages
) {
210 $viewobj->buttontext
= '';
211 } else if ($viewobj->numattempts
== 0) {
212 $viewobj->buttontext
= get_string('attemptquiznow', 'quiz');
214 $viewobj->buttontext
= get_string('reattemptquiz', 'quiz');
217 } else if ($canpreview) {
218 $viewobj->buttontext
= get_string('previewquiznow', 'quiz');
222 // If, so far, we think a button should be printed, so check if they will be
223 // allowed to access it.
224 if ($viewobj->buttontext
) {
225 if (!$viewobj->moreattempts
) {
226 $viewobj->buttontext
= '';
227 } else if ($canattempt
228 && $viewobj->preventmessages
= $viewobj->accessmanager
->prevent_access()) {
229 $viewobj->buttontext
= '';
234 $viewobj->showbacktocourse
= ($viewobj->buttontext
=== '' &&
235 course_get_format($course)->has_view_page());
237 echo $OUTPUT->header();
240 // Guests can't do a quiz, so offer them a choice of logging in or going back.
241 echo $output->view_page_guest($course, $quiz, $cm, $context, $viewobj->infomessages
);
242 } else if (!isguestuser() && !($canattempt ||
$canpreview
243 ||
$viewobj->canreviewmine
)) {
244 // If they are not enrolled in this course in a good enough role, tell them to enrol.
245 echo $output->view_page_notenrolled($course, $quiz, $cm, $context, $viewobj->infomessages
);
247 echo $output->view_page($course, $quiz, $cm, $context, $viewobj);
250 echo $OUTPUT->footer();