2009-11-04 11:58:30 +00:00
|
|
|
<?php
|
2011-02-04 16:40:00 +00:00
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This page is the entry page into the quiz UI. Displays information about the
|
|
|
|
* quiz to students and teachers, and lets students see their previous attempts.
|
|
|
|
*
|
2014-02-16 11:59:15 +13:00
|
|
|
* @package mod_quiz
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2011-02-04 16:40:00 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-14 18:07:22 +00:00
|
|
|
use mod_quiz\access_manager;
|
2022-12-19 16:01:51 +00:00
|
|
|
use mod_quiz\output\renderer;
|
2022-12-19 12:54:32 +00:00
|
|
|
use mod_quiz\output\view_page;
|
2022-12-19 21:14:51 +00:00
|
|
|
use mod_quiz\quiz_attempt;
|
2022-12-20 10:50:35 +00:00
|
|
|
use mod_quiz\quiz_settings;
|
2011-02-04 16:40:00 +00:00
|
|
|
|
2016-02-26 17:47:58 +11:00
|
|
|
require_once(__DIR__ . '/../../config.php');
|
2011-02-04 16:40:00 +00:00
|
|
|
require_once($CFG->libdir.'/gradelib.php');
|
|
|
|
require_once($CFG->dirroot.'/mod/quiz/locallib.php');
|
|
|
|
require_once($CFG->libdir . '/completionlib.php');
|
2013-10-04 11:35:47 +08:00
|
|
|
require_once($CFG->dirroot . '/course/format/lib.php');
|
2011-02-04 16:40:00 +00:00
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
$id = optional_param('id', 0, PARAM_INT); // Course Module ID, or ...
|
|
|
|
$q = optional_param('q', 0, PARAM_INT); // Quiz ID.
|
2011-02-04 16:40:00 +00:00
|
|
|
|
|
|
|
if ($id) {
|
|
|
|
if (!$cm = get_coursemodule_from_id('quiz', $id)) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcoursemodule');
|
2005-01-02 07:15:19 +00:00
|
|
|
}
|
2011-02-04 16:40:00 +00:00
|
|
|
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('coursemisconf');
|
2005-02-01 07:16:19 +00:00
|
|
|
}
|
2011-02-04 16:40:00 +00:00
|
|
|
} else {
|
|
|
|
if (!$quiz = $DB->get_record('quiz', array('id' => $q))) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidquizid', 'quiz');
|
2004-08-23 12:35:13 +00:00
|
|
|
}
|
2011-02-04 16:40:00 +00:00
|
|
|
if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcourseid');
|
2005-05-06 06:24:04 +00:00
|
|
|
}
|
2011-02-04 16:40:00 +00:00
|
|
|
if (!$cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcoursemodule');
|
2006-08-22 13:53:39 +00:00
|
|
|
}
|
2011-02-04 16:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check login and get context.
|
2012-04-22 17:41:47 +02:00
|
|
|
require_login($course, false, $cm);
|
2012-07-24 16:55:49 +08:00
|
|
|
$context = context_module::instance($cm->id);
|
2011-02-04 16:40:00 +00:00
|
|
|
require_capability('mod/quiz:view', $context);
|
|
|
|
|
|
|
|
// Cache some other capabilities we use several times.
|
|
|
|
$canattempt = has_capability('mod/quiz:attempt', $context);
|
|
|
|
$canreviewmine = has_capability('mod/quiz:reviewmyattempts', $context);
|
|
|
|
$canpreview = has_capability('mod/quiz:preview', $context);
|
|
|
|
|
|
|
|
// Create an object to manage all the other (non-roles) access rules.
|
|
|
|
$timenow = time();
|
2022-12-20 10:50:35 +00:00
|
|
|
$quizobj = quiz_settings::create($cm->instance, $USER->id);
|
2022-12-14 18:07:22 +00:00
|
|
|
$accessmanager = new access_manager($quizobj, $timenow,
|
2011-04-04 20:11:29 +01:00
|
|
|
has_capability('mod/quiz:ignoretimelimits', $context, null, false));
|
2011-10-05 15:54:57 +01:00
|
|
|
$quiz = $quizobj->get_quiz();
|
2011-02-04 16:40:00 +00:00
|
|
|
|
2016-01-14 10:52:14 +01:00
|
|
|
// Trigger course_module_viewed event and completion.
|
|
|
|
quiz_view($quiz, $course, $cm, $context);
|
2011-05-11 20:29:49 +01:00
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
// Initialize $PAGE, compute blocks.
|
2011-02-04 16:40:00 +00:00
|
|
|
$PAGE->set_url('/mod/quiz/view.php', array('id' => $cm->id));
|
|
|
|
|
2011-11-14 15:35:46 +00:00
|
|
|
// Create view object which collects all the information the renderer will need.
|
2022-12-19 12:54:32 +00:00
|
|
|
$viewobj = new view_page();
|
2011-11-14 15:35:46 +00:00
|
|
|
$viewobj->accessmanager = $accessmanager;
|
2016-11-11 16:26:56 +00:00
|
|
|
$viewobj->canreviewmine = $canreviewmine || $canpreview;
|
2011-11-14 15:35:46 +00:00
|
|
|
|
2011-02-04 16:40:00 +00:00
|
|
|
// Get this user's attempts.
|
|
|
|
$attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'finished', true);
|
|
|
|
$lastfinishedattempt = end($attempts);
|
|
|
|
$unfinished = false;
|
2014-06-22 21:30:04 +01:00
|
|
|
$unfinishedattemptid = null;
|
2011-02-04 16:40:00 +00:00
|
|
|
if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
|
|
|
|
$attempts[] = $unfinishedattempt;
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
|
2012-04-20 14:51:15 +01:00
|
|
|
// If the attempt is now overdue, deal with that - and pass isonline = false.
|
|
|
|
// We want the student notified in this case.
|
|
|
|
$quizobj->create_attempt_object($unfinishedattempt)->handle_if_time_expired(time(), false);
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
|
|
|
|
$unfinished = $unfinishedattempt->state == quiz_attempt::IN_PROGRESS ||
|
|
|
|
$unfinishedattempt->state == quiz_attempt::OVERDUE;
|
|
|
|
if (!$unfinished) {
|
|
|
|
$lastfinishedattempt = $unfinishedattempt;
|
|
|
|
}
|
2014-06-22 21:30:04 +01:00
|
|
|
$unfinishedattemptid = $unfinishedattempt->id;
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
$unfinishedattempt = null; // To make it clear we do not use this again.
|
2011-02-04 16:40:00 +00:00
|
|
|
}
|
|
|
|
$numattempts = count($attempts);
|
|
|
|
|
2011-11-14 15:35:46 +00:00
|
|
|
$viewobj->attempts = $attempts;
|
|
|
|
$viewobj->attemptobjs = array();
|
|
|
|
foreach ($attempts as $attempt) {
|
|
|
|
$viewobj->attemptobjs[] = new quiz_attempt($attempt, $quiz, $cm, $course, false);
|
|
|
|
}
|
|
|
|
|
2011-02-04 16:40:00 +00:00
|
|
|
// Work out the final grade, checking whether it was overridden in the gradebook.
|
2011-12-22 13:51:52 +00:00
|
|
|
if (!$canpreview) {
|
|
|
|
$mygrade = quiz_get_best_grade($quiz, $USER->id);
|
|
|
|
} else if ($lastfinishedattempt) {
|
|
|
|
// Users who can preview the quiz don't get a proper grade, so work out a
|
|
|
|
// plausible value to display instead, so the page looks right.
|
|
|
|
$mygrade = quiz_rescale_grade($lastfinishedattempt->sumgrades, $quiz, false);
|
|
|
|
} else {
|
|
|
|
$mygrade = null;
|
|
|
|
}
|
|
|
|
|
2011-02-04 16:40:00 +00:00
|
|
|
$mygradeoverridden = false;
|
|
|
|
$gradebookfeedback = '';
|
|
|
|
|
2021-03-23 11:55:07 +00:00
|
|
|
$item = null;
|
|
|
|
|
2021-10-06 14:59:06 +11:00
|
|
|
$gradinginfo = grade_get_grades($course->id, 'mod', 'quiz', $quiz->id, $USER->id);
|
|
|
|
if (!empty($gradinginfo->items)) {
|
|
|
|
$item = $gradinginfo->items[0];
|
2011-02-04 16:40:00 +00:00
|
|
|
if (isset($item->grades[$USER->id])) {
|
|
|
|
$grade = $item->grades[$USER->id];
|
|
|
|
|
|
|
|
if ($grade->overridden) {
|
|
|
|
$mygrade = $grade->grade + 0; // Convert to number.
|
|
|
|
$mygradeoverridden = true;
|
|
|
|
}
|
|
|
|
if (!empty($grade->str_feedback)) {
|
|
|
|
$gradebookfeedback = $grade->str_feedback;
|
2007-10-23 07:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-04 16:40:00 +00:00
|
|
|
}
|
2007-10-23 07:48:03 +00:00
|
|
|
|
2011-05-09 14:22:20 +01:00
|
|
|
$title = $course->shortname . ': ' . format_string($quiz->name);
|
|
|
|
$PAGE->set_title($title);
|
|
|
|
$PAGE->set_heading($course->fullname);
|
2021-09-22 10:56:46 +08:00
|
|
|
if (html_is_blank($quiz->intro)) {
|
|
|
|
$PAGE->activityheader->set_description('');
|
|
|
|
}
|
2021-10-04 16:57:04 +02:00
|
|
|
$PAGE->add_body_class('limitedwidth');
|
2022-12-19 16:01:51 +00:00
|
|
|
/** @var renderer $output */
|
2011-05-09 14:22:20 +01:00
|
|
|
$output = $PAGE->get_renderer('mod_quiz');
|
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
// Print table with existing attempts.
|
2011-05-04 09:21:38 +01:00
|
|
|
if ($attempts) {
|
2011-05-06 14:51:05 +01:00
|
|
|
// Work out which columns we need, taking account what data is available in each attempt.
|
2016-01-15 10:58:19 +00:00
|
|
|
list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts);
|
2011-05-06 14:51:05 +01:00
|
|
|
|
2011-11-14 15:35:46 +00:00
|
|
|
$viewobj->attemptcolumn = $quiz->attempts != 1;
|
2011-05-06 14:51:05 +01:00
|
|
|
|
2011-11-14 15:35:46 +00:00
|
|
|
$viewobj->gradecolumn = $someoptions->marks >= question_display_options::MARK_AND_MAX &&
|
2011-05-06 14:51:05 +01:00
|
|
|
quiz_has_grades($quiz);
|
2011-11-14 15:35:46 +00:00
|
|
|
$viewobj->markcolumn = $viewobj->gradecolumn && ($quiz->grade != $quiz->sumgrades);
|
|
|
|
$viewobj->overallstats = $lastfinishedattempt && $alloptions->marks >= question_display_options::MARK_AND_MAX;
|
2011-05-06 14:51:05 +01:00
|
|
|
|
|
|
|
$viewobj->feedbackcolumn = quiz_has_feedback($quiz) && $alloptions->overallfeedback;
|
2011-05-04 09:21:38 +01:00
|
|
|
}
|
2011-05-06 14:51:05 +01:00
|
|
|
|
2011-05-03 13:33:42 +01:00
|
|
|
$viewobj->timenow = $timenow;
|
|
|
|
$viewobj->numattempts = $numattempts;
|
|
|
|
$viewobj->mygrade = $mygrade;
|
2011-10-05 20:58:38 +01:00
|
|
|
$viewobj->moreattempts = $unfinished ||
|
|
|
|
!$accessmanager->is_finished($numattempts, $lastfinishedattempt);
|
2011-05-03 13:33:42 +01:00
|
|
|
$viewobj->mygradeoverridden = $mygradeoverridden;
|
|
|
|
$viewobj->gradebookfeedback = $gradebookfeedback;
|
|
|
|
$viewobj->lastfinishedattempt = $lastfinishedattempt;
|
2011-10-05 20:58:38 +01:00
|
|
|
$viewobj->canedit = has_capability('mod/quiz:manage', $context);
|
|
|
|
$viewobj->editurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cm->id));
|
|
|
|
$viewobj->backtocourseurl = new moodle_url('/course/view.php', array('id' => $course->id));
|
|
|
|
$viewobj->startattempturl = $quizobj->start_attempt_url();
|
2014-06-22 21:30:04 +01:00
|
|
|
|
|
|
|
if ($accessmanager->is_preflight_check_required($unfinishedattemptid)) {
|
|
|
|
$viewobj->preflightcheckform = $accessmanager->get_preflight_check_form(
|
|
|
|
$viewobj->startattempturl, $unfinishedattemptid);
|
|
|
|
}
|
2011-10-05 20:58:38 +01:00
|
|
|
$viewobj->popuprequired = $accessmanager->attempt_must_be_in_popup();
|
|
|
|
$viewobj->popupoptions = $accessmanager->get_popup_options();
|
2011-05-03 13:33:42 +01:00
|
|
|
|
2011-05-06 14:51:05 +01:00
|
|
|
// Display information about this quiz.
|
2011-10-05 20:58:38 +01:00
|
|
|
$viewobj->infomessages = $viewobj->accessmanager->describe_rules();
|
2011-05-06 14:51:05 +01:00
|
|
|
if ($quiz->attempts != 1) {
|
2011-10-05 20:58:38 +01:00
|
|
|
$viewobj->infomessages[] = get_string('gradingmethod', 'quiz',
|
2011-05-06 14:51:05 +01:00
|
|
|
quiz_get_grading_option_name($quiz->grademethod));
|
|
|
|
}
|
|
|
|
|
2021-04-21 22:17:48 +01:00
|
|
|
// Inform user of the grade to pass if non-zero.
|
|
|
|
if ($item && grade_floats_different($item->gradepass, 0)) {
|
2021-03-23 11:55:07 +00:00
|
|
|
$a = new stdClass();
|
|
|
|
$a->grade = quiz_format_grade($quiz, $item->gradepass);
|
|
|
|
$a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
|
|
|
|
$viewobj->infomessages[] = get_string('gradetopassoutof', 'quiz', $a);
|
|
|
|
}
|
|
|
|
|
2011-10-05 20:58:38 +01:00
|
|
|
// Determine wheter a start attempt button should be displayed.
|
2014-01-22 18:19:31 +00:00
|
|
|
$viewobj->quizhasquestions = $quizobj->has_questions();
|
2011-10-05 20:58:38 +01:00
|
|
|
$viewobj->preventmessages = array();
|
|
|
|
if (!$viewobj->quizhasquestions) {
|
|
|
|
$viewobj->buttontext = '';
|
2011-05-06 14:51:05 +01:00
|
|
|
|
|
|
|
} else {
|
2011-10-05 20:58:38 +01:00
|
|
|
if ($unfinished) {
|
2022-03-31 14:33:31 +11:00
|
|
|
if ($canpreview) {
|
2011-10-05 20:58:38 +01:00
|
|
|
$viewobj->buttontext = get_string('continuepreview', 'quiz');
|
2022-03-31 14:33:31 +11:00
|
|
|
} else if ($canattempt) {
|
|
|
|
$viewobj->buttontext = get_string('continueattemptquiz', 'quiz');
|
2011-05-06 14:51:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-03-31 14:33:31 +11:00
|
|
|
if ($canpreview) {
|
|
|
|
$viewobj->buttontext = get_string('previewquizstart', 'quiz');
|
|
|
|
} else if ($canattempt) {
|
2011-10-05 20:58:38 +01:00
|
|
|
$viewobj->preventmessages = $viewobj->accessmanager->prevent_new_attempt(
|
|
|
|
$viewobj->numattempts, $viewobj->lastfinishedattempt);
|
|
|
|
if ($viewobj->preventmessages) {
|
|
|
|
$viewobj->buttontext = '';
|
2011-05-06 14:51:05 +01:00
|
|
|
} else if ($viewobj->numattempts == 0) {
|
2022-02-05 19:20:15 +05:30
|
|
|
$viewobj->buttontext = get_string('attemptquiz', 'quiz');
|
2011-05-06 14:51:05 +01:00
|
|
|
} else {
|
2011-10-05 20:58:38 +01:00
|
|
|
$viewobj->buttontext = get_string('reattemptquiz', 'quiz');
|
2011-05-06 14:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 15:24:16 +11:00
|
|
|
// Users who can preview the quiz should be able to see all messages for not being able to access the quiz.
|
|
|
|
if ($canpreview) {
|
|
|
|
$viewobj->preventmessages = $viewobj->accessmanager->prevent_access();
|
|
|
|
} else if ($viewobj->buttontext) {
|
|
|
|
// If, so far, we think a button should be printed, so check if they will be allowed to access it.
|
2011-05-06 14:51:05 +01:00
|
|
|
if (!$viewobj->moreattempts) {
|
2011-10-05 20:58:38 +01:00
|
|
|
$viewobj->buttontext = '';
|
2022-04-01 15:24:16 +11:00
|
|
|
} else if ($canattempt) {
|
|
|
|
$viewobj->preventmessages = $viewobj->accessmanager->prevent_access();
|
|
|
|
if ($viewobj->preventmessages) {
|
|
|
|
$viewobj->buttontext = '';
|
|
|
|
}
|
2011-05-06 14:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 11:35:47 +08:00
|
|
|
$viewobj->showbacktocourse = ($viewobj->buttontext === '' &&
|
|
|
|
course_get_format($course)->has_view_page());
|
|
|
|
|
2011-05-03 13:45:37 +01:00
|
|
|
echo $OUTPUT->header();
|
|
|
|
|
2021-10-06 14:59:06 +11:00
|
|
|
if (!empty($gradinginfo->errors)) {
|
|
|
|
foreach ($gradinginfo->errors as $error) {
|
|
|
|
$errortext = new \core\output\notification($error, \core\output\notification::NOTIFY_ERROR);
|
|
|
|
echo $OUTPUT->render($errortext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-09 10:15:50 +01:00
|
|
|
if (isguestuser()) {
|
2012-02-08 15:39:26 +00:00
|
|
|
// Guests can't do a quiz, so offer them a choice of logging in or going back.
|
2022-02-05 19:20:15 +05:30
|
|
|
echo $output->view_page_guest($course, $quiz, $cm, $context, $viewobj->infomessages, $viewobj);
|
2011-10-05 20:58:38 +01:00
|
|
|
} else if (!isguestuser() && !($canattempt || $canpreview
|
2011-05-09 10:15:50 +01:00
|
|
|
|| $viewobj->canreviewmine)) {
|
|
|
|
// If they are not enrolled in this course in a good enough role, tell them to enrol.
|
2022-02-05 19:20:15 +05:30
|
|
|
echo $output->view_page_notenrolled($course, $quiz, $cm, $context, $viewobj->infomessages, $viewobj);
|
2011-05-09 10:15:50 +01:00
|
|
|
} else {
|
2011-10-05 20:58:38 +01:00
|
|
|
echo $output->view_page($course, $quiz, $cm, $context, $viewobj);
|
2011-05-09 10:15:50 +01:00
|
|
|
}
|
2011-02-04 16:40:00 +00:00
|
|
|
|
|
|
|
echo $OUTPUT->footer();
|