Skip to content

Commit

Permalink
MDL-74609 Quiz: allow the displayed question numbers to be customised
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud Kassaei committed Dec 21, 2022
1 parent 5019abf commit 2074448
Show file tree
Hide file tree
Showing 17 changed files with 591 additions and 24 deletions.
11 changes: 8 additions & 3 deletions mod/quiz/attemptlib.php
Expand Up @@ -715,9 +715,9 @@ public function load_questions() {

$this->quba = question_engine::load_questions_usage_by_activity($this->attempt->uniqueid);
$this->slots = $DB->get_records('quiz_slots',
array('quizid' => $this->get_quizid()), 'slot', 'slot, id, requireprevious');
['quizid' => $this->get_quizid()], 'slot', 'slot, id, requireprevious, displaynumber');
$this->sections = array_values($DB->get_records('quiz_sections',
array('quizid' => $this->get_quizid()), 'firstslot'));
['quizid' => $this->get_quizid()], 'firstslot'));

$this->link_sections_and_slots();
$this->determine_layout();
Expand Down Expand Up @@ -796,7 +796,12 @@ protected function number_questions() {
foreach ($this->pagelayout as $page => $slots) {
foreach ($slots as $slot) {
if ($length = $this->is_real_question($slot)) {
$this->questionnumbers[$slot] = $number;
// Whether question numbering is customised or is numeric and automatically incremented.
if (!empty($this->slots[$slot]->displaynumber) && !is_null($this->slots[$slot]->displaynumber)) {
$this->questionnumbers[$slot] = $this->slots[$slot]->displaynumber;
} else {
$this->questionnumbers[$slot] = $number;
}
$number += $length;
} else {
$this->questionnumbers[$slot] = get_string('infoshort', 'quiz');
Expand Down
3 changes: 2 additions & 1 deletion mod/quiz/backup/moodle2/backup_quiz_stepslib.php
Expand Up @@ -49,7 +49,8 @@ protected function define_structure() {
$qinstances = new backup_nested_element('question_instances');

$qinstance = new backup_nested_element('question_instance', ['id'],
['slot', 'page', 'requireprevious', 'questionid', 'questioncategoryid', 'includingsubcategories', 'maxmark']);
['slot', 'page', 'displaynumber', 'requireprevious', 'questionid',
'questioncategoryid', 'includingsubcategories', 'maxmark']);

$this->add_question_references($qinstance, 'mod_quiz', 'slot');

Expand Down
115 changes: 115 additions & 0 deletions mod/quiz/classes/event/slot_displaynumber_updated.php
@@ -0,0 +1,115 @@
<?php
// 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/>.

namespace mod_quiz\event;

/**
* The mod_quiz slot display updated event class.
*
* @property-read array $other {
* Extra information about event.
*
* - int quizid: the id of the quiz.
* - string displaynumber: the slot's customised question number value.
* }
*
* @package mod_quiz
* @copyright 2022 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class slot_displaynumber_updated extends \core\event\base {
/**
* Initialise the quiz_slots table.
*/
protected function init(): void {
$this->data['objecttable'] = 'quiz_slots';
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}

/**
* Return the name of the event.
*
* @return string
*/
public static function get_name(): string {
return get_string('eventslotdisplayedquestionnumberupdated', 'mod_quiz');
}

/**
* Log describes which user customised the question number in a given slot and in which quiz.
*
* @return string
*/
public function get_description(): string {
return "The user with id '$this->userid' updated the slot with id '{$this->objectid}' " .
"belonging to the quiz with course module id '$this->contextinstanceid'. " .
"Its customised question number value was set to '{$this->other['displaynumber']}'.";
}

/**
* Return the url object of the quiz editing page.
*
* @return \moodle_url
*/
public function get_url(): \moodle_url {
return new \moodle_url('/mod/quiz/edit.php', ['cmid' => $this->contextinstanceid]);
}

/**
* validate the data being logged.
*/
protected function validate_data(): void {
parent::validate_data();

if (!isset($this->objectid)) {
throw new \coding_exception('The \'objectid\' value must be set.');
}

if (!isset($this->contextinstanceid)) {
throw new \coding_exception('The \'contextinstanceid\' value must be set.');
}

if (!isset($this->other['quizid'])) {
throw new \coding_exception('The \'quizid\' value must be set in other.');
}

if (!isset($this->other['displaynumber'])) {
throw new \coding_exception('The \'displaynumber\' value must be set in other.');
}
}

/**
* Return the mapped array.
*
* @return string[]
*/
public static function get_objectid_mapping(): array {
return ['db' => 'quiz_slots', 'restore' => 'quiz_question_instance'];
}

/**
* Return the mapped array.
*
* @return array
*/
public static function get_other_mapping(): array {
$othermapped = [];
$othermapped['quizid'] = ['db' => 'quiz', 'restore' => 'quiz'];

return $othermapped;
}
}
15 changes: 11 additions & 4 deletions mod/quiz/classes/output/edit_renderer.php
Expand Up @@ -23,7 +23,6 @@
*/

namespace mod_quiz\output;
defined('MOODLE_INTERNAL') || die();

use mod_quiz\question\bank\qbank_helper;
use \mod_quiz\structure;
Expand Down Expand Up @@ -742,11 +741,19 @@ public function question(structure $structure, int $slot, \moodle_url $pageurl)
$output .= $this->question_move_icon($structure, $slot);
}

if ($structure->can_display_number_be_customised($slot)) {
$questionnumbercustomised = $this->output->render($structure->make_slot_display_number_in_place_editable(
$slotid, \context_module::instance($structure->get_cmid())));
$questionnumber = $questionnumbercustomised;
} else {
$questionnumber = $structure->get_displayed_number_for_slot($slot);
}

$data = [
'slotid' => $slotid,
'canbeedited' => $structure->can_be_edited(),
'checkbox' => $this->get_checkbox_render($structure, $slot),
'questionnumber' => $this->question_number($structure->get_displayed_number_for_slot($slot)),
'questionnumber' => $this->question_number($questionnumber),
'questionname' => $this->get_question_name_for_slot($structure, $slot, $pageurl),
'questionicons' => $this->get_action_icon($structure, $slot, $pageurl),
'questiondependencyicon' => ($structure->can_be_edited() ? $this->question_dependency_icon($structure, $slot) : ''),
Expand Down Expand Up @@ -851,10 +858,10 @@ public function question_move_icon(structure $structure, $slot) {
* @return string HTML to output.
*/
public function question_number($number) {
if (is_numeric($number)) {
if ($number !== get_string('infoshort', 'quiz')) {
$number = html_writer::span(get_string('question'), 'accesshide') . ' ' . $number;
}
return html_writer::tag('span', $number, array('class' => 'slotnumber'));
return html_writer::tag('span', $number, ['class' => 'slotnumber']);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions mod/quiz/classes/question/bank/qbank_helper.php
Expand Up @@ -22,6 +22,7 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/question/engine/bank.php');
require_once($CFG->dirroot . '/mod/quiz/accessmanager.php');
require_once($CFG->dirroot . '/mod/quiz/attemptlib.php');

Expand Down Expand Up @@ -108,6 +109,7 @@ public static function get_question_structure(int $quizid, \context_module $quiz
slot.id AS slotid,
slot.page,
slot.maxmark,
slot.displaynumber,
slot.requireprevious,
qsr.filtercondition,
qv.status,
Expand Down

0 comments on commit 2074448

Please sign in to comment.