2008-06-27 18:04:48 +00:00
|
|
|
<?php
|
2009-07-23 09:20:33 +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/>.
|
|
|
|
|
2008-06-27 18:04:48 +00:00
|
|
|
/**
|
2009-07-23 09:20:33 +00:00
|
|
|
* Back-end code for handling data about quizzes and the current user's attempt.
|
|
|
|
*
|
|
|
|
* There are classes for loading all the information about a quiz and attempts,
|
|
|
|
* and for displaying the navigation panel.
|
|
|
|
*
|
|
|
|
* @package quiz
|
|
|
|
* @copyright 2008 onwards Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-30 16:56:49 +00:00
|
|
|
* Class for quiz exceptions. Just saves a couple of arguments on the
|
|
|
|
* constructor for a moodle_exception.
|
2009-07-23 09:20:33 +00:00
|
|
|
*
|
|
|
|
* @copyright 2008 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2008-06-27 18:04:48 +00:00
|
|
|
*/
|
|
|
|
class moodle_quiz_exception extends moodle_exception {
|
|
|
|
function __construct($quizobj, $errorcode, $a = NULL, $link = '', $debuginfo = null) {
|
|
|
|
if (!$link) {
|
|
|
|
$link = $quizobj->view_url();
|
|
|
|
}
|
|
|
|
parent::__construct($errorcode, 'quiz', $link, $a, $debuginfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
2009-07-23 09:20:33 +00:00
|
|
|
* A class encapsulating a quiz and the questions it contains, and making the
|
|
|
|
* information available to scripts like view.php.
|
|
|
|
*
|
|
|
|
* Initially, it only loads a minimal amout of information about each question - loading
|
|
|
|
* extra information only when necessary or when asked. The class tracks which questions
|
|
|
|
* are loaded.
|
|
|
|
*
|
|
|
|
* @copyright 2008 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2008-06-30 16:56:49 +00:00
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
class quiz {
|
|
|
|
// Fields initialised in the constructor.
|
|
|
|
protected $course;
|
|
|
|
protected $cm;
|
|
|
|
protected $quiz;
|
|
|
|
protected $context;
|
2008-07-08 16:33:47 +00:00
|
|
|
protected $questionids; // All question ids in order that they appear in the quiz.
|
|
|
|
protected $pagequestionids; // array page no => array of questionids on the page in order.
|
2008-08-29 07:29:35 +00:00
|
|
|
|
2008-06-27 18:04:48 +00:00
|
|
|
// Fields set later if that data is needed.
|
2008-07-08 16:33:47 +00:00
|
|
|
protected $questions = null;
|
2008-06-27 18:04:48 +00:00
|
|
|
protected $accessmanager = null;
|
2008-06-30 16:56:49 +00:00
|
|
|
protected $ispreviewuser = null;
|
2008-06-27 18:04:48 +00:00
|
|
|
|
|
|
|
// Constructor =========================================================================
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* Constructor, assuming we already have the necessary data loaded.
|
|
|
|
*
|
|
|
|
* @param object $quiz the row from the quiz table.
|
|
|
|
* @param object $cm the course_module object for this quiz.
|
|
|
|
* @param object $course the row from the course table for the course we belong to.
|
2008-09-23 07:18:15 +00:00
|
|
|
* @param boolean $getcontext intended for testing - stops the constructor getting the context.
|
2008-06-30 16:56:49 +00:00
|
|
|
*/
|
2008-09-23 07:18:15 +00:00
|
|
|
function __construct($quiz, $cm, $course, $getcontext = true) {
|
2008-06-27 18:04:48 +00:00
|
|
|
$this->quiz = $quiz;
|
|
|
|
$this->cm = $cm;
|
2009-01-16 04:47:23 +00:00
|
|
|
$this->quiz->cmid = $this->cm->id;
|
2008-06-27 18:04:48 +00:00
|
|
|
$this->course = $course;
|
2008-09-23 07:18:15 +00:00
|
|
|
if ($getcontext && !empty($cm->id)) {
|
|
|
|
$this->context = get_context_instance(CONTEXT_MODULE, $cm->id);
|
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
$this->determine_layout();
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 16:01:38 +00:00
|
|
|
/**
|
|
|
|
* Static function to create a new quiz object for a specific user.
|
|
|
|
*
|
|
|
|
* @param integer $quizid the the quiz id.
|
|
|
|
* @param integer $userid the the userid.
|
2010-07-28 15:06:28 +00:00
|
|
|
* @return quiz the new quiz object
|
2010-03-08 16:01:38 +00:00
|
|
|
*/
|
|
|
|
static public function create($quizid, $userid) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if (!$quiz = $DB->get_record('quiz', array('id' => $quizid))) {
|
|
|
|
throw new moodle_exception('invalidquizid', 'quiz');
|
|
|
|
}
|
|
|
|
if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
|
|
|
|
throw new moodle_exception('invalidcoursemodule');
|
|
|
|
}
|
|
|
|
if (!$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id)) {
|
|
|
|
throw new moodle_exception('invalidcoursemodule');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update quiz with override information
|
|
|
|
$quiz = quiz_update_effective_access($quiz, $userid);
|
|
|
|
|
|
|
|
return new quiz($quiz, $cm, $course);
|
|
|
|
}
|
|
|
|
|
2008-06-27 18:04:48 +00:00
|
|
|
// Functions for loading more data =====================================================
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Convenience method. Calls {@link load_questions()} with the list of
|
|
|
|
* question ids for a given page.
|
|
|
|
*
|
|
|
|
* @param integer $page a page number.
|
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
public function load_questions_on_page($page) {
|
2008-07-08 16:33:47 +00:00
|
|
|
$this->load_questions($this->pagequestionids[$page]);
|
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Load just basic information about all the questions in this quiz.
|
|
|
|
*/
|
2008-07-08 16:33:47 +00:00
|
|
|
public function preload_questions() {
|
|
|
|
if (empty($this->questionids)) {
|
|
|
|
throw new moodle_quiz_exception($this, 'noquestions', $this->edit_url());
|
|
|
|
}
|
|
|
|
$this->questions = question_preload_questions($this->questionids,
|
|
|
|
'qqi.grade AS maxgrade, qqi.id AS instance',
|
|
|
|
'{quiz_question_instances} qqi ON qqi.quiz = :quizid AND q.id = qqi.question',
|
|
|
|
array('quizid' => $this->quiz->id));
|
|
|
|
$this->number_questions();
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
|
2008-09-04 06:37:43 +00:00
|
|
|
/**
|
2009-07-23 09:20:33 +00:00
|
|
|
* Fully load some or all of the questions for this quiz. You must call {@link preload_questions()} first.
|
2008-06-27 18:04:48 +00:00
|
|
|
*
|
2008-07-08 16:33:47 +00:00
|
|
|
* @param array $questionids question ids of the questions to load. null for all.
|
2008-06-27 18:04:48 +00:00
|
|
|
*/
|
2008-07-08 16:33:47 +00:00
|
|
|
public function load_questions($questionids = null) {
|
|
|
|
if (is_null($questionids)) {
|
|
|
|
$questionids = $this->questionids;
|
|
|
|
}
|
|
|
|
$questionstoprocess = array();
|
|
|
|
foreach ($questionids as $id) {
|
|
|
|
$questionstoprocess[$id] = $this->questions[$id];
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
if (!get_question_options($questionstoprocess)) {
|
|
|
|
throw new moodle_quiz_exception($this, 'loadingquestionsfailed', implode(', ', $questionids));
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple getters ======================================================================
|
2008-06-30 16:56:49 +00:00
|
|
|
/** @return integer the course id. */
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_courseid() {
|
|
|
|
return $this->course->id;
|
|
|
|
}
|
|
|
|
|
2008-07-08 16:33:47 +00:00
|
|
|
/** @return object the row of the course table. */
|
|
|
|
public function get_course() {
|
|
|
|
return $this->course;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/** @return integer the quiz id. */
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_quizid() {
|
|
|
|
return $this->quiz->id;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/** @return object the row of the quiz table. */
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_quiz() {
|
|
|
|
return $this->quiz;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/** @return string the name of this quiz. */
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_quiz_name() {
|
|
|
|
return $this->quiz->name;
|
|
|
|
}
|
|
|
|
|
2008-07-10 17:23:56 +00:00
|
|
|
/** @return integer the number of attempts allowed at this quiz (0 = infinite). */
|
|
|
|
public function get_num_attempts_allowed() {
|
|
|
|
return $this->quiz->attempts;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/** @return integer the course_module id. */
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_cmid() {
|
|
|
|
return $this->cm->id;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/** @return object the course_module object. */
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_cm() {
|
|
|
|
return $this->cm;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
2008-08-29 07:29:35 +00:00
|
|
|
* @return boolean wether the current user is someone who previews the quiz,
|
2008-06-30 16:56:49 +00:00
|
|
|
* rather than attempting it.
|
|
|
|
*/
|
|
|
|
public function is_preview_user() {
|
|
|
|
if (is_null($this->ispreviewuser)) {
|
|
|
|
$this->ispreviewuser = has_capability('mod/quiz:preview', $this->context);
|
|
|
|
}
|
|
|
|
return $this->ispreviewuser;
|
|
|
|
}
|
|
|
|
|
2008-07-08 16:33:47 +00:00
|
|
|
/**
|
|
|
|
* @return integer number fo pages in this quiz.
|
|
|
|
*/
|
|
|
|
public function get_num_pages() {
|
|
|
|
return count($this->pagequestionids);
|
|
|
|
}
|
2008-08-29 07:29:35 +00:00
|
|
|
|
2008-07-08 16:33:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $page page number
|
|
|
|
* @return boolean true if this is the last page of the quiz.
|
|
|
|
*/
|
|
|
|
public function is_last_page($page) {
|
|
|
|
return $page == count($this->pagequestionids) - 1;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* @param integer $id the question id.
|
|
|
|
* @return object the question object with that id.
|
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_question($id) {
|
|
|
|
return $this->questions[$id];
|
|
|
|
}
|
|
|
|
|
2008-07-08 16:33:47 +00:00
|
|
|
/**
|
|
|
|
* @param array $questionids question ids of the questions to load. null for all.
|
|
|
|
*/
|
|
|
|
public function get_questions($questionids = null) {
|
|
|
|
if (is_null($questionids)) {
|
|
|
|
$questionids = $this->questionids;
|
|
|
|
}
|
|
|
|
$questions = array();
|
|
|
|
foreach ($questionids as $id) {
|
|
|
|
$questions[$id] = $this->questions[$id];
|
|
|
|
$this->ensure_question_loaded($id);
|
|
|
|
}
|
|
|
|
return $questions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-08-29 07:29:35 +00:00
|
|
|
* Return the list of question ids for either a given page of the quiz, or for the
|
2008-07-08 16:33:47 +00:00
|
|
|
* whole quiz.
|
|
|
|
*
|
|
|
|
* @param mixed $page string 'all' or integer page number.
|
|
|
|
* @return array the reqested list of question ids.
|
|
|
|
*/
|
|
|
|
public function get_question_ids($page = 'all') {
|
2008-07-10 17:31:12 +00:00
|
|
|
if ($page === 'all') {
|
2008-07-08 16:33:47 +00:00
|
|
|
$list = $this->questionids;
|
|
|
|
} else {
|
2008-07-10 17:34:18 +00:00
|
|
|
$list = $this->pagequestionids[$page];
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
// Clone the array, so our private arrays cannot be modified.
|
|
|
|
$result = array();
|
|
|
|
foreach ($list as $id) {
|
|
|
|
$result[] = $id;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* @param integer $timenow the current time as a unix timestamp.
|
2010-08-04 15:22:04 +00:00
|
|
|
* @return quiz_access_manager and instance of the quiz_access_manager class for this quiz at this time.
|
2008-06-30 16:56:49 +00:00
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_access_manager($timenow) {
|
|
|
|
if (is_null($this->accessmanager)) {
|
2008-07-08 16:33:47 +00:00
|
|
|
$this->accessmanager = new quiz_access_manager($this, $timenow,
|
2008-06-27 18:04:48 +00:00
|
|
|
has_capability('mod/quiz:ignoretimelimits', $this->context, NULL, false));
|
|
|
|
}
|
|
|
|
return $this->accessmanager;
|
|
|
|
}
|
|
|
|
|
2010-08-10 09:56:48 +00:00
|
|
|
public function get_overall_feedback($grade) {
|
|
|
|
return quiz_feedback_for_grade($grade, $this->quiz, $this->context, $this->cm);
|
|
|
|
}
|
|
|
|
|
2008-07-08 16:33:47 +00:00
|
|
|
/**
|
|
|
|
* Wrapper round the has_capability funciton that automatically passes in the quiz context.
|
|
|
|
*/
|
|
|
|
public function has_capability($capability, $userid = NULL, $doanything = true) {
|
|
|
|
return has_capability($capability, $this->context, $userid, $doanything);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper round the require_capability funciton that automatically passes in the quiz context.
|
|
|
|
*/
|
|
|
|
public function require_capability($capability, $userid = NULL, $doanything = true) {
|
|
|
|
return require_capability($capability, $this->context, $userid, $doanything);
|
|
|
|
}
|
|
|
|
|
2008-06-27 18:04:48 +00:00
|
|
|
// URLs related to this attempt ========================================================
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* @return string the URL of this quiz's view page.
|
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
public function view_url() {
|
|
|
|
global $CFG;
|
|
|
|
return $CFG->wwwroot . '/mod/quiz/view.php?id=' . $this->cm->id;
|
|
|
|
}
|
|
|
|
|
2008-07-08 16:33:47 +00:00
|
|
|
/**
|
|
|
|
* @return string the URL of this quiz's edit page.
|
|
|
|
*/
|
|
|
|
public function edit_url() {
|
|
|
|
global $CFG;
|
|
|
|
return $CFG->wwwroot . '/mod/quiz/edit.php?cmid=' . $this->cm->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param integer $attemptid the id of an attempt.
|
|
|
|
* @return string the URL of that attempt.
|
|
|
|
*/
|
|
|
|
public function attempt_url($attemptid) {
|
|
|
|
global $CFG;
|
2008-07-11 17:03:43 +00:00
|
|
|
return $CFG->wwwroot . '/mod/quiz/attempt.php?attempt=' . $attemptid;
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string the URL of this quiz's edit page. Needs to be POSTed to with a cmid parameter.
|
|
|
|
*/
|
|
|
|
public function start_attempt_url() {
|
2010-08-05 18:15:17 +00:00
|
|
|
return new moodle_url('/mod/quiz/startattempt.php',
|
|
|
|
array('cmid' => $this->cm->id, 'sesskey' => sesskey()));
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param integer $attemptid the id of an attempt.
|
|
|
|
* @return string the URL of the review of that attempt.
|
|
|
|
*/
|
|
|
|
public function review_url($attemptid) {
|
2010-08-25 15:58:26 +00:00
|
|
|
return new moodle_url('/mod/quiz/review.php', array('attempt' => $attemptid));
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
2008-06-27 18:04:48 +00:00
|
|
|
// Bits of content =====================================================================
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* @param string $title the name of this particular quiz page.
|
|
|
|
* @return array the data that needs to be sent to print_header_simple as the $navigation
|
|
|
|
* parameter.
|
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
public function navigation($title) {
|
2009-09-07 02:11:54 +00:00
|
|
|
global $PAGE;
|
|
|
|
$PAGE->navbar->add($title);
|
|
|
|
return '';
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private methods =====================================================================
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Check that the definition of a particular question is loaded, and if not throw an exception.
|
|
|
|
* @param $id a questionid.
|
|
|
|
*/
|
2008-07-08 16:33:47 +00:00
|
|
|
protected function ensure_question_loaded($id) {
|
|
|
|
if (isset($this->questions[$id]->_partiallyloaded)) {
|
2008-06-27 18:04:48 +00:00
|
|
|
throw new moodle_quiz_exception($this, 'questionnotloaded', $id);
|
|
|
|
}
|
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Populate {@link $questionids} and {@link $pagequestionids} from the layout.
|
|
|
|
*/
|
|
|
|
protected function determine_layout() {
|
2008-07-08 16:33:47 +00:00
|
|
|
$this->questionids = array();
|
|
|
|
$this->pagequestionids = array();
|
|
|
|
|
|
|
|
// Get the appropriate layout string (from quiz or attempt).
|
2011-01-29 12:11:10 +00:00
|
|
|
$layout = quiz_clean_layout($this->get_layout_string(), true);
|
2008-07-08 16:33:47 +00:00
|
|
|
if (empty($layout)) {
|
|
|
|
// Nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Break up the layout string into pages.
|
2011-01-29 12:11:10 +00:00
|
|
|
$pagelayouts = explode(',0', $layout);
|
2008-07-08 16:33:47 +00:00
|
|
|
|
|
|
|
// Strip off any empty last page (normally there is one).
|
|
|
|
if (end($pagelayouts) == '') {
|
|
|
|
array_pop($pagelayouts);
|
|
|
|
}
|
|
|
|
|
|
|
|
// File the ids into the arrays.
|
|
|
|
$this->questionids = array();
|
|
|
|
$this->pagequestionids = array();
|
|
|
|
foreach ($pagelayouts as $page => $pagelayout) {
|
|
|
|
$pagelayout = trim($pagelayout, ',');
|
|
|
|
if ($pagelayout == '') continue;
|
|
|
|
$this->pagequestionids[$page] = explode(',', $pagelayout);
|
|
|
|
foreach ($this->pagequestionids[$page] as $id) {
|
|
|
|
$this->questionids[] = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Number the questions, adding a _number field to each one.
|
|
|
|
*/
|
2008-07-08 16:33:47 +00:00
|
|
|
private function number_questions() {
|
|
|
|
$number = 1;
|
|
|
|
foreach ($this->pagequestionids as $page => $questionids) {
|
|
|
|
foreach ($questionids as $id) {
|
|
|
|
if ($this->questions[$id]->length > 0) {
|
|
|
|
$this->questions[$id]->_number = $number;
|
|
|
|
$number += $this->questions[$id]->length;
|
|
|
|
} else {
|
|
|
|
$this->questions[$id]->_number = get_string('infoshort', 'quiz');
|
|
|
|
}
|
|
|
|
$this->questions[$id]->_page = $page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string the layout of this quiz. Used by number_questions to
|
2008-08-29 07:29:35 +00:00
|
|
|
* work out which questions are on which pages.
|
2008-07-08 16:33:47 +00:00
|
|
|
*/
|
|
|
|
protected function get_layout_string() {
|
|
|
|
return $this->quiz->questions;
|
|
|
|
}
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* This class extends the quiz class to hold data about the state of a particular attempt,
|
|
|
|
* in addition to the data about the quiz.
|
2009-07-23 09:20:33 +00:00
|
|
|
*
|
|
|
|
* @copyright 2008 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2008-06-30 16:56:49 +00:00
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
class quiz_attempt extends quiz {
|
|
|
|
// Fields initialised in the constructor.
|
|
|
|
protected $attempt;
|
|
|
|
|
|
|
|
// Fields set later if that data is needed.
|
|
|
|
protected $states = array();
|
2008-07-08 17:47:57 +00:00
|
|
|
protected $reviewoptions = null;
|
2008-06-27 18:04:48 +00:00
|
|
|
|
|
|
|
// Constructor =========================================================================
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
2010-03-08 16:01:38 +00:00
|
|
|
* Constructor assuming we already have the necessary data loaded.
|
|
|
|
*
|
|
|
|
* @param object $attempt the row of the quiz_attempts table.
|
|
|
|
* @param object $quiz the quiz object for this attempt and user.
|
|
|
|
* @param object $cm the course_module object for this quiz.
|
|
|
|
* @param object $course the row from the course table for the course we belong to.
|
|
|
|
*/
|
|
|
|
function __construct($attempt, $quiz, $cm, $course) {
|
|
|
|
$this->attempt = $attempt;
|
|
|
|
parent::__construct($quiz, $cm, $course);
|
|
|
|
$this->preload_questions();
|
|
|
|
$this->preload_question_states();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static function to create a new quiz_attempt object given an attemptid.
|
2008-06-30 16:56:49 +00:00
|
|
|
*
|
2010-03-08 16:01:38 +00:00
|
|
|
* @param integer $attemptid the attempt id.
|
2010-08-04 18:56:51 +00:00
|
|
|
* @return quiz_attempt the new quiz_attempt object
|
2008-06-30 16:56:49 +00:00
|
|
|
*/
|
2010-03-08 16:01:38 +00:00
|
|
|
static public function create($attemptid) {
|
2008-06-27 18:04:48 +00:00
|
|
|
global $DB;
|
2010-03-08 16:01:38 +00:00
|
|
|
|
|
|
|
if (!$attempt = quiz_load_attempt($attemptid)) {
|
2008-06-27 18:04:48 +00:00
|
|
|
throw new moodle_exception('invalidattemptid', 'quiz');
|
|
|
|
}
|
2010-03-08 16:01:38 +00:00
|
|
|
if (!$quiz = $DB->get_record('quiz', array('id' => $attempt->quiz))) {
|
2008-06-27 18:04:48 +00:00
|
|
|
throw new moodle_exception('invalidquizid', 'quiz');
|
|
|
|
}
|
|
|
|
if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
|
|
|
|
throw new moodle_exception('invalidcoursemodule');
|
|
|
|
}
|
|
|
|
if (!$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id)) {
|
|
|
|
throw new moodle_exception('invalidcoursemodule');
|
|
|
|
}
|
2010-03-08 16:01:38 +00:00
|
|
|
// Update quiz with override information
|
|
|
|
$quiz = quiz_update_effective_access($quiz, $attempt->userid);
|
|
|
|
|
|
|
|
return new quiz_attempt($attempt, $quiz, $cm, $course);
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Functions for loading more data =====================================================
|
|
|
|
/**
|
2008-07-08 16:33:47 +00:00
|
|
|
* Load the state of a number of questions that have already been loaded.
|
2008-06-27 18:04:48 +00:00
|
|
|
*
|
2008-07-08 16:33:47 +00:00
|
|
|
* @param array $questionids question ids to process. Blank = all.
|
2008-06-27 18:04:48 +00:00
|
|
|
*/
|
2008-07-08 16:33:47 +00:00
|
|
|
public function load_question_states($questionids = null) {
|
|
|
|
if (is_null($questionids)) {
|
|
|
|
$questionids = $this->questionids;
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
$questionstoprocess = array();
|
|
|
|
foreach ($questionids as $id) {
|
|
|
|
$this->ensure_question_loaded($id);
|
|
|
|
$questionstoprocess[$id] = $this->questions[$id];
|
|
|
|
}
|
2008-09-04 07:05:14 +00:00
|
|
|
if (!question_load_states($questionstoprocess, $this->states,
|
2008-09-04 06:37:43 +00:00
|
|
|
$this->quiz, $this->attempt)) {
|
2008-06-27 18:04:48 +00:00
|
|
|
throw new moodle_quiz_exception($this, 'cannotrestore');
|
|
|
|
}
|
2008-09-04 06:37:43 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Load basic information about the state of each question.
|
|
|
|
*
|
|
|
|
* This is enough to, for example, show the state of each question in the
|
|
|
|
* navigation panel, but only takes one DB query.
|
|
|
|
*/
|
2008-09-04 06:37:43 +00:00
|
|
|
public function preload_question_states() {
|
|
|
|
if (empty($this->questionids)) {
|
|
|
|
throw new moodle_quiz_exception($this, 'noquestions', $this->edit_url());
|
|
|
|
}
|
|
|
|
$this->states = question_preload_states($this->attempt->uniqueid);
|
|
|
|
if (!$this->states) {
|
|
|
|
$this->states = array();
|
|
|
|
}
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Load a particular state of a particular question. Used by the reviewquestion.php
|
|
|
|
* script to let the teacher walk through the entire sequence of a student's
|
|
|
|
* interaction with a question.
|
|
|
|
*
|
|
|
|
* @param $questionid the question id
|
|
|
|
* @param $stateid the id of the particular state to load.
|
|
|
|
*/
|
2008-07-15 15:30:39 +00:00
|
|
|
public function load_specific_question_state($questionid, $stateid) {
|
|
|
|
global $DB;
|
|
|
|
$state = question_load_specific_state($this->questions[$questionid],
|
2011-01-29 13:59:51 +00:00
|
|
|
$this->quiz, $this->attempt->uniqueid, $stateid);
|
2008-07-15 15:30:39 +00:00
|
|
|
if ($state === false) {
|
|
|
|
throw new moodle_quiz_exception($this, 'invalidstateid');
|
|
|
|
}
|
|
|
|
$this->states[$questionid] = $state;
|
|
|
|
}
|
|
|
|
|
2008-06-27 18:04:48 +00:00
|
|
|
// Simple getters ======================================================================
|
2008-06-30 16:56:49 +00:00
|
|
|
/** @return integer the attempt id. */
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_attemptid() {
|
|
|
|
return $this->attempt->id;
|
|
|
|
}
|
|
|
|
|
2008-09-18 07:39:10 +00:00
|
|
|
/** @return integer the attempt unique id. */
|
|
|
|
public function get_uniqueid() {
|
|
|
|
return $this->attempt->uniqueid;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/** @return object the row from the quiz_attempts table. */
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_attempt() {
|
|
|
|
return $this->attempt;
|
|
|
|
}
|
|
|
|
|
2008-07-08 16:33:47 +00:00
|
|
|
/** @return integer the number of this attemp (is it this user's first, second, ... attempt). */
|
|
|
|
public function get_attempt_number() {
|
|
|
|
return $this->attempt->attempt;
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/** @return integer the id of the user this attempt belongs to. */
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_userid() {
|
|
|
|
return $this->attempt->userid;
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:26:47 +00:00
|
|
|
/** @return boolean whether this attempt has been finished (true) or is still in progress (false). */
|
2008-06-27 18:04:48 +00:00
|
|
|
public function is_finished() {
|
|
|
|
return $this->attempt->timefinish != 0;
|
|
|
|
}
|
|
|
|
|
2010-08-10 09:56:48 +00:00
|
|
|
/** @return boolean whether this attempt is a preview attempt. */
|
2008-07-08 17:47:57 +00:00
|
|
|
public function is_preview() {
|
|
|
|
return $this->attempt->preview;
|
|
|
|
}
|
|
|
|
|
2008-07-15 15:30:39 +00:00
|
|
|
/**
|
|
|
|
* Is this a student dealing with their own attempt/teacher previewing,
|
|
|
|
* or someone with 'mod/quiz:viewreports' reviewing someone elses attempt.
|
2008-08-29 07:29:35 +00:00
|
|
|
*
|
2008-07-15 15:30:39 +00:00
|
|
|
* @return boolean whether this situation should be treated as someone looking at their own
|
|
|
|
* attempt. The distinction normally only matters when an attempt is being reviewed.
|
|
|
|
*/
|
|
|
|
public function is_own_attempt() {
|
|
|
|
global $USER;
|
|
|
|
return $this->attempt->userid == $USER->id &&
|
|
|
|
(!$this->is_preview_user() || $this->attempt->preview);
|
|
|
|
}
|
|
|
|
|
2009-01-14 07:08:02 +00:00
|
|
|
/**
|
|
|
|
* Check the appropriate capability to see whether this user may review their own attempt.
|
|
|
|
* If not, prints an error.
|
|
|
|
*/
|
|
|
|
public function check_review_capability() {
|
|
|
|
if (!$this->has_capability('mod/quiz:viewreports')) {
|
|
|
|
if ($this->get_review_options()->quizstate == QUIZ_STATE_IMMEDIATELY) {
|
|
|
|
$this->require_capability('mod/quiz:attempt');
|
|
|
|
} else {
|
|
|
|
$this->require_capability('mod/quiz:reviewmyattempts');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Get the current state of a question in the attempt.
|
|
|
|
*
|
|
|
|
* @param $questionid a questionid.
|
|
|
|
* @return object the state.
|
|
|
|
*/
|
2008-07-08 16:33:47 +00:00
|
|
|
public function get_question_state($questionid) {
|
|
|
|
return $this->states[$questionid];
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* Wrapper that calls quiz_get_reviewoptions with the appropriate arguments.
|
|
|
|
*
|
2008-07-08 17:47:57 +00:00
|
|
|
* @return object the review options for this user on this attempt.
|
2008-06-30 16:56:49 +00:00
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_review_options() {
|
|
|
|
if (is_null($this->reviewoptions)) {
|
|
|
|
$this->reviewoptions = quiz_get_reviewoptions($this->quiz, $this->attempt, $this->context);
|
|
|
|
}
|
|
|
|
return $this->reviewoptions;
|
|
|
|
}
|
|
|
|
|
2008-07-08 17:47:57 +00:00
|
|
|
/**
|
|
|
|
* Wrapper that calls get_render_options with the appropriate arguments.
|
|
|
|
*
|
2010-08-10 09:56:48 +00:00
|
|
|
* @param integer questionid the quetsion to get the render options for.
|
2008-07-08 17:47:57 +00:00
|
|
|
* @return object the render options for this user on this attempt.
|
|
|
|
*/
|
2010-08-10 09:56:48 +00:00
|
|
|
public function get_render_options($questionid) {
|
|
|
|
return quiz_get_renderoptions($this->quiz, $this->attempt, $this->context,
|
|
|
|
$this->get_question_state($questionid));
|
2008-07-08 17:47:57 +00:00
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* Get a quiz_attempt_question_iterator for either a page of the quiz, or a whole quiz.
|
|
|
|
* You must have called load_questions with an appropriate argument first.
|
|
|
|
*
|
|
|
|
* @param mixed $page as for the @see{get_question_ids} method.
|
|
|
|
* @return quiz_attempt_question_iterator the requested iterator.
|
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_question_iterator($page = 'all') {
|
|
|
|
return new quiz_attempt_question_iterator($this, $page);
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* Return a summary of the current state of a question in this attempt. You must previously
|
|
|
|
* have called load_question_states to load the state data about this question.
|
|
|
|
*
|
|
|
|
* @param integer $questionid question id of a question that belongs to this quiz.
|
|
|
|
* @return string a brief string (that could be used as a CSS class name, for example)
|
|
|
|
* that describes the current state of a question in this attempt. Possible results are:
|
|
|
|
* open|saved|closed|correct|partiallycorrect|incorrect.
|
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
public function get_question_status($questionid) {
|
2008-06-30 16:56:49 +00:00
|
|
|
$state = $this->states[$questionid];
|
|
|
|
switch ($state->event) {
|
|
|
|
case QUESTION_EVENTOPEN:
|
|
|
|
return 'open';
|
|
|
|
|
|
|
|
case QUESTION_EVENTSAVE:
|
|
|
|
case QUESTION_EVENTGRADE:
|
2008-09-11 03:57:28 +00:00
|
|
|
case QUESTION_EVENTSUBMIT:
|
2008-07-08 18:22:18 +00:00
|
|
|
return 'answered';
|
2008-06-30 16:56:49 +00:00
|
|
|
|
|
|
|
case QUESTION_EVENTCLOSEANDGRADE:
|
|
|
|
case QUESTION_EVENTCLOSE:
|
|
|
|
case QUESTION_EVENTMANUALGRADE:
|
2010-08-10 09:56:48 +00:00
|
|
|
$options = $this->get_render_options($questionid);
|
2008-09-18 04:31:11 +00:00
|
|
|
if ($options->scores && $this->questions[$questionid]->maxgrade > 0) {
|
2008-06-30 16:56:49 +00:00
|
|
|
return question_get_feedback_class($state->last_graded->raw_grade /
|
|
|
|
$this->questions[$questionid]->maxgrade);
|
|
|
|
} else {
|
|
|
|
return 'closed';
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
$a = new stdClass;
|
|
|
|
$a->event = $state->event;
|
|
|
|
$a->questionid = $questionid;
|
|
|
|
$a->attemptid = $this->attempt->id;
|
|
|
|
throw new moodle_quiz_exception($this, 'errorunexpectedevent', $a);
|
|
|
|
}
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
|
2008-08-29 10:08:27 +00:00
|
|
|
/**
|
|
|
|
* @param integer $questionid question id of a question that belongs to this quiz.
|
|
|
|
* @return boolean whether this question hss been flagged by the attempter.
|
|
|
|
*/
|
|
|
|
public function is_question_flagged($questionid) {
|
|
|
|
$state = $this->states[$questionid];
|
|
|
|
return $state->flagged;
|
|
|
|
}
|
|
|
|
|
2008-06-27 18:04:48 +00:00
|
|
|
/**
|
2008-06-30 16:56:49 +00:00
|
|
|
* Return the grade obtained on a particular question, if the user is permitted to see it.
|
|
|
|
* You must previously have called load_question_states to load the state data about this question.
|
2008-06-27 18:04:48 +00:00
|
|
|
*
|
2008-06-30 16:56:49 +00:00
|
|
|
* @param integer $questionid question id of a question that belongs to this quiz.
|
2008-06-27 18:04:48 +00:00
|
|
|
* @return string the formatted grade, to the number of decimal places specified by the quiz.
|
|
|
|
*/
|
|
|
|
public function get_question_score($questionid) {
|
2010-08-10 09:56:48 +00:00
|
|
|
$options = $this->get_render_options($questionid);
|
2008-06-27 18:04:48 +00:00
|
|
|
if ($options->scores) {
|
quiz settings: MDL-18485 Improve quiz settings form
* Reorder form fields to group things more logically.
** and on the corresponding admin page too.
* Set some options to be 'Advanced' by default:
** Apply penalties.
** Each attempt builds on the last.
** Decimal places for question grades.
** The five 'Extra restrictions on attempts' settings. (password, etc.)
* Admins can still change this to suit their institiution at Administration > Plugins > Activity modules > Quiz.
* These new defaults are applied if the admin had not previously set any fields to be advanced.
* Disable some filds when they are not applicable:
** Grading method, if num attempts = 1
** Penaly scheme, if adaptive mode = no
** Each attempt builds of last, if num attempts = 1
** Review after quiz closed options, if no close date.
** Delay between 1st and 2nd attempts, if num attempts = 1
** Delay between later attempts, if num attempts < 3
* Convert quiz.timelimit to be in seconds, for consistency, and ready for the new duration field type (MDL 18500).
** Including ensuring that backup and restore is backwards compatible.
* MDL-5537 New setting, questiondecimalpoints, so, for example, you can show the quiz grade as an integer, but have fractional question grades.
** There is a 'Same as overall decimal points' option, which is the default.
* Improve some field labels.
* Make corresponding changes in the help files.
2009-03-10 08:39:51 +00:00
|
|
|
return quiz_format_question_grade($this->quiz, $this->states[$questionid]->last_graded->grade);
|
2008-06-27 18:04:48 +00:00
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// URLs related to this attempt ========================================================
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
2008-07-08 16:33:47 +00:00
|
|
|
* @param integer $questionid a question id. If set, will add a fragment to the URL
|
2008-06-30 16:56:49 +00:00
|
|
|
* to jump to a particuar question on the page.
|
2009-07-23 09:20:33 +00:00
|
|
|
* @param integer $page if specified, the URL of this particular page of the attempt, otherwise
|
|
|
|
* the URL will go to the first page. If -1, deduce $page from $questionid.
|
|
|
|
* @param integer $thispage if not -1, the current page. Will cause links to other things on
|
|
|
|
* this page to be output as only a fragment.
|
2008-06-30 16:56:49 +00:00
|
|
|
* @return string the URL to continue this attempt.
|
|
|
|
*/
|
2009-07-23 09:20:33 +00:00
|
|
|
public function attempt_url($questionid = 0, $page = -1, $thispage = -1) {
|
|
|
|
return $this->page_and_question_url('attempt', $questionid, $page, false, $thispage);
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* @return string the URL of this quiz's summary page.
|
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
public function summary_url() {
|
|
|
|
global $CFG;
|
|
|
|
return $CFG->wwwroot . '/mod/quiz/summary.php?attempt=' . $this->attempt->id;
|
|
|
|
}
|
|
|
|
|
2008-07-15 16:46:24 +00:00
|
|
|
/**
|
|
|
|
* @return string the URL of this quiz's summary page.
|
|
|
|
*/
|
|
|
|
public function processattempt_url() {
|
|
|
|
global $CFG;
|
|
|
|
return $CFG->wwwroot . '/mod/quiz/processattempt.php';
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
2009-07-23 09:20:33 +00:00
|
|
|
* @param integer $questionid a question id. If set, will add a fragment to the URL
|
|
|
|
* to jump to a particuar question on the page. If -1, deduce $page from $questionid.
|
2008-06-30 16:56:49 +00:00
|
|
|
* @param integer $page if specified, the URL of this particular page of the attempt, otherwise
|
|
|
|
* the URL will go to the first page.
|
|
|
|
* @param boolean $showall if true, the URL will be to review the entire attempt on one page,
|
|
|
|
* and $page will be ignored.
|
2009-07-23 09:20:33 +00:00
|
|
|
* @param integer $thispage if not -1, the current page. Will cause links to other things on
|
|
|
|
* this page to be output as only a fragment.
|
2008-06-30 16:56:49 +00:00
|
|
|
* @return string the URL to review this attempt.
|
|
|
|
*/
|
2009-07-23 09:20:33 +00:00
|
|
|
public function review_url($questionid = 0, $page = -1, $showall = false, $thispage = -1) {
|
|
|
|
return $this->page_and_question_url('review', $questionid, $page, $showall, $thispage);
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bits of content =====================================================================
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Initialise the JS etc. required all the questions on a page..
|
|
|
|
* @param mixed $page a page number, or 'all'.
|
|
|
|
*/
|
2008-07-08 16:33:47 +00:00
|
|
|
public function get_html_head_contributions($page = 'all') {
|
2009-07-23 09:20:33 +00:00
|
|
|
global $PAGE;
|
2010-08-04 15:22:04 +00:00
|
|
|
question_get_html_head_contributions($this->get_question_ids($page), $this->questions, $this->states);
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Initialise the JS etc. required by one question.
|
|
|
|
* @param integer $questionid the question id.
|
|
|
|
*/
|
2008-08-28 02:15:19 +00:00
|
|
|
public function get_question_html_head_contributions($questionid) {
|
2010-08-04 15:22:04 +00:00
|
|
|
question_get_html_head_contributions(array($questionid), $this->questions, $this->states);
|
2008-08-28 02:15:19 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Print the HTML for the start new preview button.
|
|
|
|
*/
|
2008-07-08 16:33:47 +00:00
|
|
|
public function print_restart_preview_button() {
|
2009-08-20 08:45:47 +00:00
|
|
|
global $CFG, $OUTPUT;
|
|
|
|
echo $OUTPUT->container_start('controls');
|
2010-08-05 18:15:17 +00:00
|
|
|
$url = new moodle_url($this->start_attempt_url(), array('forcenew' => true));
|
2010-01-03 15:46:14 +00:00
|
|
|
echo $OUTPUT->single_button($url, get_string('startagain', 'quiz'));
|
2009-08-20 08:45:47 +00:00
|
|
|
echo $OUTPUT->container_end();
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Return the HTML of the quiz timer.
|
|
|
|
* @return string HTML content.
|
|
|
|
*/
|
2009-03-11 07:10:57 +00:00
|
|
|
public function get_timer_html() {
|
|
|
|
return '<div id="quiz-timer">' . get_string('timeleft', 'quiz') .
|
|
|
|
' <span id="quiz-time-left"></span></div>';
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:26:47 +00:00
|
|
|
/**
|
|
|
|
* Wrapper round print_question from lib/questionlib.php.
|
|
|
|
*
|
|
|
|
* @param integer $id the id of a question in this quiz attempt.
|
|
|
|
* @param boolean $reviewing is the being printed on an attempt or a review page.
|
|
|
|
* @param string $thispageurl the URL of the page this question is being printed on.
|
|
|
|
*/
|
|
|
|
public function print_question($id, $reviewing, $thispageurl = '') {
|
2010-10-18 15:28:59 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2008-12-10 06:26:47 +00:00
|
|
|
if ($reviewing) {
|
2008-07-08 17:47:57 +00:00
|
|
|
$options = $this->get_review_options();
|
|
|
|
} else {
|
2010-08-10 09:56:48 +00:00
|
|
|
$options = $this->get_render_options($id);
|
2008-07-08 17:47:57 +00:00
|
|
|
}
|
2010-10-22 17:24:09 +00:00
|
|
|
if ($thispageurl instanceof moodle_url) {
|
|
|
|
$thispageurl = $thispageurl->out(false);
|
|
|
|
}
|
2008-12-10 06:26:47 +00:00
|
|
|
if ($thispageurl) {
|
2010-10-22 17:24:09 +00:00
|
|
|
$this->quiz->thispageurl = str_replace($CFG->wwwroot, '', $thispageurl);
|
2008-12-10 06:26:47 +00:00
|
|
|
} else {
|
|
|
|
unset($thispageurl);
|
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
print_question($this->questions[$id], $this->states[$id], $this->questions[$id]->_number,
|
|
|
|
$this->quiz, $options);
|
|
|
|
}
|
|
|
|
|
2010-08-10 09:56:48 +00:00
|
|
|
public function check_file_access($questionid, $isreviewing, $contextid, $component,
|
|
|
|
$filearea, $args, $forcedownload) {
|
|
|
|
if ($isreviewing) {
|
|
|
|
$options = $this->get_review_options();
|
|
|
|
} else {
|
|
|
|
$options = $this->get_render_options($questionid);
|
|
|
|
}
|
|
|
|
// XXX: mulitichoice type needs quiz id to get maxgrade
|
|
|
|
$options->quizid = $this->attempt->quiz;
|
|
|
|
return question_check_file_access($this->questions[$questionid],
|
|
|
|
$this->get_question_state($questionid), $options, $contextid,
|
|
|
|
$component, $filearea, $args, $forcedownload);
|
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Triggers the sending of the notification emails at the end of this attempt.
|
|
|
|
*/
|
2008-07-08 16:33:47 +00:00
|
|
|
public function quiz_send_notification_emails() {
|
|
|
|
quiz_send_notification_emails($this->course, $this->quiz, $this->attempt,
|
|
|
|
$this->context, $this->cm);
|
|
|
|
}
|
2008-07-11 17:03:43 +00:00
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Get the navigation panel object for this attempt.
|
|
|
|
*
|
|
|
|
* @param $panelclass The type of panel, quiz_attempt_nav_panel or quiz_review_nav_panel
|
|
|
|
* @param $page the current page number.
|
|
|
|
* @param $showall whether we are showing the whole quiz on one page. (Used by review.php)
|
|
|
|
* @return quiz_nav_panel_base the requested object.
|
|
|
|
*/
|
|
|
|
public function get_navigation_panel($panelclass, $page, $showall = false) {
|
|
|
|
$panel = new $panelclass($this, $this->get_review_options(), $page, $showall);
|
2009-07-09 07:35:03 +00:00
|
|
|
return $panel->get_contents();
|
2008-07-11 17:03:43 +00:00
|
|
|
}
|
2008-06-27 18:04:48 +00:00
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
2009-11-04 11:58:30 +00:00
|
|
|
* Given a URL containing attempt={this attempt id}, return an array of variant URLs
|
2009-07-23 09:20:33 +00:00
|
|
|
* @param $url a URL.
|
|
|
|
* @return string HTML fragment. Comma-separated list of links to the other
|
|
|
|
* attempts with the attempt number as the link text. The curent attempt is
|
|
|
|
* included but is not a link.
|
|
|
|
*/
|
2008-07-15 15:30:39 +00:00
|
|
|
public function links_to_other_attempts($url) {
|
|
|
|
$search = '/\battempt=' . $this->attempt->id . '\b/';
|
|
|
|
$attempts = quiz_get_user_attempts($this->quiz->id, $this->attempt->userid, 'all');
|
2008-08-15 06:42:38 +00:00
|
|
|
if (count($attempts) <= 1) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-15 15:30:39 +00:00
|
|
|
$attemptlist = array();
|
|
|
|
foreach ($attempts as $at) {
|
|
|
|
if ($at->id == $this->attempt->id) {
|
|
|
|
$attemptlist[] = '<strong>' . $at->attempt . '</strong>';
|
|
|
|
} else {
|
|
|
|
$changedurl = preg_replace($search, 'attempt=' . $at->id, $url);
|
2009-07-22 09:49:48 +00:00
|
|
|
$attemptlist[] = '<a href="' . s($changedurl) . '">' . $at->attempt . '</a>';
|
2008-07-15 15:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return implode(', ', $attemptlist);
|
|
|
|
}
|
|
|
|
|
2008-09-18 07:39:10 +00:00
|
|
|
// Methods for processing manual comments ==============================================
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Process a manual comment for a question in this attempt.
|
|
|
|
* @param $questionid
|
|
|
|
* @param integer $questionid the question id
|
|
|
|
* @param string $comment the new comment from the teacher.
|
|
|
|
* @param mixed $grade the grade the teacher assigned, or '' to not change the grade.
|
|
|
|
* @return mixed true on success, a string error message if a problem is detected
|
|
|
|
* (for example score out of range).
|
|
|
|
*/
|
2010-11-11 17:32:25 +00:00
|
|
|
public function process_comment($questionid, $comment, $commentformat, $grade) {
|
2009-07-23 09:20:33 +00:00
|
|
|
// I am not sure it is a good idea to have update methods here - this
|
|
|
|
// class is only about getting data out of the question engine, and
|
|
|
|
// helping to display it, apart from this.
|
2008-09-18 07:39:10 +00:00
|
|
|
$this->ensure_question_loaded($questionid);
|
|
|
|
$this->ensure_state_loaded($questionid);
|
|
|
|
$state = $this->states[$questionid];
|
|
|
|
|
|
|
|
$error = question_process_comment($this->questions[$questionid],
|
2010-11-11 17:32:25 +00:00
|
|
|
$state, $this->attempt, $comment, $commentformat, $grade);
|
2008-09-18 07:39:10 +00:00
|
|
|
|
|
|
|
// If the state was update (successfully), save the changes.
|
|
|
|
if (!is_string($error) && $state->changed) {
|
|
|
|
if (!save_question_session($this->questions[$questionid], $state)) {
|
|
|
|
$error = get_string('errorudpatingquestionsession', 'quiz');
|
|
|
|
}
|
|
|
|
if (!quiz_save_best_grade($this->quiz, $this->attempt->userid)) {
|
|
|
|
$error = get_string('errorudpatingbestgrade', 'quiz');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Print the fields of the comment form for questions in this attempt.
|
|
|
|
* @param $questionid a question id.
|
|
|
|
* @param $prefix Prefix to add to all field names.
|
|
|
|
*/
|
2008-09-18 07:39:10 +00:00
|
|
|
public function question_print_comment_fields($questionid, $prefix) {
|
2008-12-10 09:11:30 +00:00
|
|
|
global $DB;
|
|
|
|
|
2008-09-18 07:39:10 +00:00
|
|
|
$this->ensure_question_loaded($questionid);
|
|
|
|
$this->ensure_state_loaded($questionid);
|
2008-12-10 09:11:30 +00:00
|
|
|
|
|
|
|
/// Work out a nice title.
|
|
|
|
$student = $DB->get_record('user', array('id' => $this->get_userid()));
|
2010-09-21 08:37:36 +00:00
|
|
|
$a = new stdClass();
|
2008-12-10 09:11:30 +00:00
|
|
|
$a->fullname = fullname($student, true);
|
|
|
|
$a->attempt = $this->get_attempt_number();
|
|
|
|
|
2008-09-18 07:39:10 +00:00
|
|
|
question_print_comment_fields($this->questions[$questionid],
|
2008-12-10 09:11:30 +00:00
|
|
|
$this->states[$questionid], $prefix, $this->quiz, get_string('gradingattempt', 'quiz_grading', $a));
|
2008-09-18 07:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-06-27 18:04:48 +00:00
|
|
|
// Private methods =====================================================================
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Check that the state of a particular question is loaded, and if not throw an exception.
|
|
|
|
* @param integer $id a question id.
|
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
private function ensure_state_loaded($id) {
|
2008-09-04 06:37:43 +00:00
|
|
|
if (!array_key_exists($id, $this->states) || isset($this->states[$id]->_partiallyloaded)) {
|
2008-06-27 18:04:48 +00:00
|
|
|
throw new moodle_quiz_exception($this, 'statenotloaded', $id);
|
|
|
|
}
|
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string the layout of this quiz. Used by number_questions to
|
2008-08-29 07:29:35 +00:00
|
|
|
* work out which questions are on which pages.
|
2008-07-08 16:33:47 +00:00
|
|
|
*/
|
|
|
|
protected function get_layout_string() {
|
|
|
|
return $this->attempt->layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-23 09:20:33 +00:00
|
|
|
* Get a URL for a particular question on a particular page of the quiz.
|
|
|
|
* Used by {@link attempt_url()} and {@link review_url()}.
|
2008-07-08 16:33:47 +00:00
|
|
|
*
|
2009-07-23 09:20:33 +00:00
|
|
|
* @param string $script. Used in the URL like /mod/quiz/$script.php
|
|
|
|
* @param integer $questionid the id of a particular question on the page to jump to. 0 to just use the $page parameter.
|
|
|
|
* @param integer $page -1 to look up the page number from the questionid, otherwise the page number to go to.
|
|
|
|
* @param boolean $showall if true, return a URL with showall=1, and not page number
|
|
|
|
* @param integer $thispage the page we are currently on. Links to questoins on this
|
|
|
|
* page will just be a fragment #q123. -1 to disable this.
|
|
|
|
* @return The requested URL.
|
2008-07-08 16:33:47 +00:00
|
|
|
*/
|
2009-07-23 09:20:33 +00:00
|
|
|
protected function page_and_question_url($script, $questionid, $page, $showall, $thispage) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
// Fix up $page
|
2008-07-11 17:03:43 +00:00
|
|
|
if ($page == -1) {
|
2009-07-23 09:20:33 +00:00
|
|
|
if ($questionid && !$showall) {
|
2008-07-08 16:33:47 +00:00
|
|
|
$page = $this->questions[$questionid]->_page;
|
|
|
|
} else {
|
|
|
|
$page = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($showall) {
|
|
|
|
$page = 0;
|
|
|
|
}
|
2009-07-23 09:20:33 +00:00
|
|
|
|
|
|
|
// Work out the correct start to the URL.
|
|
|
|
if ($thispage == $page) {
|
|
|
|
$url = '';
|
|
|
|
} else {
|
|
|
|
$url = $CFG->wwwroot . '/mod/quiz/' . $script . '.php?attempt=' . $this->attempt->id;
|
|
|
|
if ($showall) {
|
|
|
|
$url .= '&showall=1';
|
|
|
|
} else if ($page > 0) {
|
|
|
|
$url .= '&page=' . $page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-18 15:28:59 +00:00
|
|
|
// Add a fragment to scroll down to the question.
|
2009-07-09 07:35:03 +00:00
|
|
|
if ($questionid) {
|
|
|
|
if ($questionid == reset($this->pagequestionids[$page])) {
|
|
|
|
// First question on page, go to top.
|
2009-07-23 09:20:33 +00:00
|
|
|
$url .= '#';
|
2009-07-09 07:35:03 +00:00
|
|
|
} else {
|
2009-07-23 09:20:33 +00:00
|
|
|
$url .= '#q' . $questionid;
|
2009-07-09 07:35:03 +00:00
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
2009-07-23 09:20:33 +00:00
|
|
|
|
|
|
|
return $url;
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
2008-06-27 18:04:48 +00:00
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
/**
|
|
|
|
* A PHP Iterator for conviniently looping over the questions in a quiz. The keys are the question
|
|
|
|
* numbers (with 'i' for descriptions) and the values are the question objects.
|
2009-07-23 09:20:33 +00:00
|
|
|
*
|
|
|
|
* @copyright 2008 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2008-06-30 16:56:49 +00:00
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
class quiz_attempt_question_iterator implements Iterator {
|
2008-06-30 16:56:49 +00:00
|
|
|
private $attemptobj; // Reference to the quiz_attempt object we provide access to.
|
|
|
|
private $questionids; // Array of the question ids within that attempt we are iterating over.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor. Normally, you don't want to call this directly. Instead call
|
|
|
|
* quiz_attempt::get_question_iterator
|
|
|
|
*
|
|
|
|
* @param quiz_attempt $attemptobj the quiz_attempt object we will be providing access to.
|
|
|
|
* @param mixed $page as for @see{quiz_attempt::get_question_iterator}.
|
|
|
|
*/
|
2008-06-27 18:04:48 +00:00
|
|
|
public function __construct(quiz_attempt $attemptobj, $page = 'all') {
|
|
|
|
$this->attemptobj = $attemptobj;
|
|
|
|
$this->questionids = $attemptobj->get_question_ids($page);
|
|
|
|
}
|
|
|
|
|
2008-06-30 16:56:49 +00:00
|
|
|
// Implementation of the Iterator interface ============================================
|
2008-06-27 18:04:48 +00:00
|
|
|
public function rewind() {
|
|
|
|
reset($this->questionids);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function current() {
|
|
|
|
$id = current($this->questionids);
|
|
|
|
if ($id) {
|
|
|
|
return $this->attemptobj->get_question($id);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function key() {
|
|
|
|
$id = current($this->questionids);
|
|
|
|
if ($id) {
|
2008-07-08 16:33:47 +00:00
|
|
|
return $this->attemptobj->get_question($id)->_number;
|
2008-06-27 18:04:48 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function next() {
|
|
|
|
$id = next($this->questionids);
|
|
|
|
if ($id) {
|
|
|
|
return $this->attemptobj->get_question($id);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function valid() {
|
|
|
|
return $this->current() !== false;
|
|
|
|
}
|
|
|
|
}
|
2008-07-11 17:03:43 +00:00
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Represents the navigation panel, and builds a {@link block_contents} to allow
|
|
|
|
* it to be output.
|
|
|
|
*
|
|
|
|
* @copyright 2008 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2008-07-11 17:03:43 +00:00
|
|
|
abstract class quiz_nav_panel_base {
|
2010-08-10 13:30:32 +00:00
|
|
|
/** @var quiz_attempt */
|
2008-07-11 17:03:43 +00:00
|
|
|
protected $attemptobj;
|
2010-08-10 13:30:32 +00:00
|
|
|
/** @var question_display_options */
|
2008-07-11 17:03:43 +00:00
|
|
|
protected $options;
|
2010-08-10 13:30:32 +00:00
|
|
|
/** @var integer */
|
2008-07-11 17:03:43 +00:00
|
|
|
protected $page;
|
2010-08-10 13:30:32 +00:00
|
|
|
/** @var boolean */
|
2009-07-23 09:20:33 +00:00
|
|
|
protected $showall;
|
2008-07-11 17:03:43 +00:00
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
public function __construct(quiz_attempt $attemptobj, $options, $page, $showall) {
|
2010-08-10 13:30:32 +00:00
|
|
|
$this->attemptobj = $attemptobj;
|
|
|
|
$this->options = $options;
|
|
|
|
$this->page = $page;
|
|
|
|
$this->showall = $showall;
|
2008-07-11 17:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function get_question_buttons() {
|
2008-09-03 02:35:56 +00:00
|
|
|
$html = '<div class="qn_buttons">' . "\n";
|
2008-07-11 17:03:43 +00:00
|
|
|
foreach ($this->attemptobj->get_question_iterator() as $number => $question) {
|
ajaxlib/require_js: MDL-16693 $PAGE->requires->... deprecates require_js etc.
There is a new implementation of require_js in lib/deprecatedlib.php,
based on $PAGE->requires.
There were a few other recently introduced functions in lib/weblib.php,
namely print_js_call, print_delayed_js_call, print_js_config and
standard_js_config. These have been removed, since they were never in
a stable branch, and all the places that used them have been changed
to use the newer $PAGE->requires->... methods.
get_require_js_code is also gone, and the evil places that were calling
it, even though it is an internal function, have been fixed.
Also, I made some minor improvements to the code I committed yesterday
for MDL-16695.
All that remains is to update all the places in core code that are
still using require_js.
(This commit also fixes the problem where the admin tree would not
start with the right categories expanded.)
2009-06-12 12:13:07 +00:00
|
|
|
$html .= $this->get_question_button($number, $question) . "\n";
|
2008-07-11 17:03:43 +00:00
|
|
|
}
|
2008-09-03 02:35:56 +00:00
|
|
|
$html .= "</div>\n";
|
2008-07-11 17:03:43 +00:00
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
protected function get_question_button($number, $question) {
|
|
|
|
$strstate = get_string($this->attemptobj->get_question_status($question->id), 'quiz');
|
2010-08-09 17:10:25 +00:00
|
|
|
$flagstate = '';
|
|
|
|
if ($this->attemptobj->is_question_flagged($question->id)) {
|
|
|
|
$flagstate = get_string('flagged', 'question');
|
|
|
|
}
|
2009-07-29 03:51:34 +00:00
|
|
|
return '<a href="' . s($this->get_question_url($question)) .
|
2009-07-23 09:20:33 +00:00
|
|
|
'" class="qnbutton ' . $this->get_question_state_classes($question) .
|
|
|
|
'" id="quiznavbutton' . $question->id . '" title="' . $strstate . '">' .
|
2010-08-09 17:10:25 +00:00
|
|
|
$number . ' <span class="accesshide"> (' . $strstate . '
|
|
|
|
<span class="flagstate">' . $flagstate . '</span>)</span></a>';
|
2008-09-03 02:35:56 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
protected function get_before_button_bits() {
|
|
|
|
return '';
|
|
|
|
}
|
2008-07-11 17:03:43 +00:00
|
|
|
|
|
|
|
abstract protected function get_end_bits();
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
abstract protected function get_question_url($question);
|
|
|
|
|
2009-01-07 06:32:13 +00:00
|
|
|
protected function get_user_picture() {
|
2009-08-20 08:45:47 +00:00
|
|
|
global $DB, $OUTPUT;
|
2009-01-07 06:32:13 +00:00
|
|
|
$user = $DB->get_record('user', array('id' => $this->attemptobj->get_userid()));
|
|
|
|
$output = '';
|
|
|
|
$output .= '<div id="user-picture" class="clearfix">';
|
2009-12-27 19:47:21 +00:00
|
|
|
$output .= $OUTPUT->user_picture($user, array('courseid'=>$this->attemptobj->get_courseid()));
|
2009-01-07 06:32:13 +00:00
|
|
|
$output .= ' ' . fullname($user);
|
|
|
|
$output .= '</div>';
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2008-08-29 10:08:27 +00:00
|
|
|
protected function get_question_state_classes($question) {
|
|
|
|
// The current status of the question.
|
|
|
|
$classes = $this->attemptobj->get_question_status($question->id);
|
|
|
|
|
|
|
|
// Plus a marker for the current page.
|
2009-07-23 09:20:33 +00:00
|
|
|
if ($this->showall || $question->_page == $this->page) {
|
2008-08-29 10:08:27 +00:00
|
|
|
$classes .= ' thispage';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Plus a marker for flagged questions.
|
|
|
|
if ($this->attemptobj->is_question_flagged($question->id)) {
|
|
|
|
$classes .= ' flagged';
|
2008-07-11 17:03:43 +00:00
|
|
|
}
|
2008-08-29 10:08:27 +00:00
|
|
|
return $classes;
|
2008-07-11 17:03:43 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 07:35:03 +00:00
|
|
|
public function get_contents() {
|
2009-07-23 09:20:33 +00:00
|
|
|
global $PAGE;
|
2010-08-04 15:22:04 +00:00
|
|
|
$PAGE->requires->js_init_call('M.mod_quiz.nav.init', null, false, quiz_get_js_module());
|
2009-07-23 09:20:33 +00:00
|
|
|
|
2009-01-07 06:32:13 +00:00
|
|
|
$content = '';
|
|
|
|
if ($this->attemptobj->get_quiz()->showuserpicture) {
|
|
|
|
$content .= $this->get_user_picture() . "\n";
|
|
|
|
}
|
2009-07-23 09:20:33 +00:00
|
|
|
$content .= $this->get_before_button_bits();
|
2009-01-07 06:32:13 +00:00
|
|
|
$content .= $this->get_question_buttons() . "\n";
|
|
|
|
$content .= '<div class="othernav">' . "\n" . $this->get_end_bits() . "\n</div>\n";
|
2009-07-09 07:35:03 +00:00
|
|
|
|
|
|
|
$bc = new block_contents();
|
|
|
|
$bc->id = 'quiznavigation';
|
|
|
|
$bc->title = get_string('quiznavigation', 'quiz');
|
|
|
|
$bc->content = $content;
|
|
|
|
return $bc;
|
2008-07-11 17:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Specialisation of {@link quiz_nav_panel_base} for the attempt quiz page.
|
|
|
|
*
|
|
|
|
* @copyright 2008 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2008-07-11 17:03:43 +00:00
|
|
|
class quiz_attempt_nav_panel extends quiz_nav_panel_base {
|
2009-07-23 09:20:33 +00:00
|
|
|
protected function get_question_url($question) {
|
|
|
|
return $this->attemptobj->attempt_url($question->id, -1, $this->page);
|
2008-07-11 17:03:43 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
protected function get_before_button_bits() {
|
|
|
|
return '<div id="quiznojswarning">' . get_string('navnojswarning', 'quiz') . "</div>\n";
|
2008-07-11 17:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function get_end_bits() {
|
2009-07-23 09:20:33 +00:00
|
|
|
global $PAGE;
|
2008-07-22 11:37:01 +00:00
|
|
|
$output = '';
|
2010-11-01 17:44:59 +00:00
|
|
|
$output .= '<a href="' . s($this->attemptobj->summary_url()) . '" class="endtestlink">' . get_string('finishattemptdots', 'quiz') . '</a>';
|
2009-03-11 07:10:57 +00:00
|
|
|
$output .= $this->attemptobj->get_timer_html();
|
2008-07-22 11:37:01 +00:00
|
|
|
return $output;
|
2008-07-11 17:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-23 09:20:33 +00:00
|
|
|
/**
|
|
|
|
* Specialisation of {@link quiz_nav_panel_base} for the review quiz page.
|
|
|
|
*
|
|
|
|
* @copyright 2008 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2008-07-11 17:03:43 +00:00
|
|
|
class quiz_review_nav_panel extends quiz_nav_panel_base {
|
2009-07-23 09:20:33 +00:00
|
|
|
protected function get_question_url($question) {
|
|
|
|
return $this->attemptobj->review_url($question->id, -1, $this->showall, $this->page);
|
2008-07-11 17:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function get_end_bits() {
|
2009-07-23 09:20:33 +00:00
|
|
|
$html = '';
|
|
|
|
if ($this->attemptobj->get_num_pages() > 1) {
|
|
|
|
if ($this->showall) {
|
2010-08-10 13:30:32 +00:00
|
|
|
$html .= '<a href="' . s($this->attemptobj->review_url(0, 0, false)) . '">' . get_string('showeachpage', 'quiz') . '</a>';
|
2009-07-23 09:20:33 +00:00
|
|
|
} else {
|
2010-08-10 13:30:32 +00:00
|
|
|
$html .= '<a href="' . s($this->attemptobj->review_url(0, 0, true)) . '">' . get_string('showall', 'quiz') . '</a>';
|
2009-07-23 09:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-14 15:40:24 +00:00
|
|
|
$accessmanager = $this->attemptobj->get_access_manager(time());
|
|
|
|
$html .= $accessmanager->print_finish_review_link($this->attemptobj->is_preview_user(), true);
|
2008-07-11 17:03:43 +00:00
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
}
|