MDL-71378 mod_quiz: Add pill badge to question slot template

This commit is contained in:
Simon Adams 2024-03-22 10:41:29 +00:00
parent 291fdacfd5
commit 5955868d3d
3 changed files with 94 additions and 6 deletions

View File

@ -753,8 +753,23 @@ class edit_renderer extends \plugin_renderer_base {
* @return string HTML to output.
*/
public function question(structure $structure, int $slot, \moodle_url $pageurl) {
global $DB;
// Get the data required by the question_slot template.
$slotid = $structure->get_slot_id_for_slot($slot);
$question = $structure->get_question_in_slot($slot);
$bank = $structure->get_source_bank($slot);
if ($bank?->issharedbank) {
$bankurl = (new \moodle_url('/question/edit.php',
[
'cmid' => $bank->cminfo->id,
'cat' => "{$question->category},{$question->contextid}",
]
))->out(false);
} else {
$bankurl = '';
}
$output = '';
$output .= html_writer::start_tag('div');
@ -780,6 +795,9 @@ class edit_renderer extends \plugin_renderer_base {
'questiondependencyicon' => ($structure->can_be_edited() ? $this->question_dependency_icon($structure, $slot) : ''),
'versionselection' => false,
'draftversion' => $structure->get_question_in_slot($slot)->status == question_version_status::QUESTION_STATUS_DRAFT,
'bankname' => $bank?->cminfo->get_formatted_name(),
'issharedbank' => $bank?->issharedbank,
'bankurl' => $bankurl,
];
$data['versionoptions'] = [];

View File

@ -75,6 +75,9 @@ class structure {
/** @var array the slotids => question tags array for all slots containing a random question. */
protected $randomslottags = null;
/** @var array an array of question banks course_modules records indexed by their associated contextid */
protected array $questionsources = [];
/**
* Create an instance of this class representing an empty quiz.
*
@ -1866,4 +1869,44 @@ class structure {
return $includesubcategories ? get_string('randomcatwithsubcat', 'mod_quiz', $qcategory->name) :
$qcategory->name;
}
/**
* Populate question_sources with cm records for later reference.
*
* @return void
*/
private function populate_question_sources(): void {
global $DB;
$sql = 'SELECT c.id AS contextid, cm.*
FROM {question_categories} qc
JOIN {context} c ON c.id = qc.contextid
JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = ' . CONTEXT_MODULE . '
GROUP BY c.id, cm.id';
$this->questionsources = $DB->get_records_sql($sql);
}
/**
* Get data on the question bank being used by the question in the slot.
*
* @param int $slot slot number
* @return stdClass|null
*/
public function get_source_bank(int $slot): ?stdClass {
$questionid = $this->slotsinorder[$slot]->questionid;
$this->questionsources[$this->questions[$questionid]->contextid] ?? $this->populate_question_sources();
// This shouldn't happen as all categories belong to a module context level but let's account for it.
if (empty($this->questionsources[$this->questions[$questionid]->contextid])) {
return null;
}
$cminfo = \cm_info::create($this->questionsources[$this->questions[$questionid]->contextid]);
return (object) [
'cminfo' => $cminfo,
'issharedbank' => plugin_supports('mod', $cminfo->modname, FEATURE_PUBLISHES_QUESTIONS, false),
];
}
}

View File

@ -18,12 +18,34 @@
Example context (json):
{
"checkbox" : "<input id='selectquestion-1' name='selectquestion[]' type='checkbox' class='select-multiple-checkbox'>",
"questionnumber" : "<span class='slotnumber'><span class='accesshide'>Question</span> 1</span>",
"questionname" : "This is a test question",
"questionicons" : "<i class='icon fa fa-search-plus fa-fw' title='Preview question' aria-label='Preview question'></i>",
"canbeedited" : false,
"questiondependencyicon" : "<span class='question_dependency_wrapper question_dependency_cannot_depend'></span>"
"slotid": "1",
"canbeedited": true,
"checkbox": "<input id='selectquestion-1' name='selectquestion[]' type='checkbox' class='select-multiple-checkbox'>",
"questionnumber": "<span class='slotnumber'><span class='accesshide'>Question</span> 1</span>",
"questionname": "This is a test question",
"questionicons": "<i class='icon fa fa-search-plus fa-fw' title='Preview question'></i>",
"questiondependencyicon": "<span class='question_dependency_wrapper question_dependency_cannot_depend'></span>",
"versionselection": true,
"draftversion": false,
"bankname": "Question bank 1",
"issharedbank": true,
"bankurl": "http://example.com/question/edit.php?cmid=1&cat=1%2C1",
"versionoptions": [],
"versionoption": [
{
"versionid": 0,
"version": 0,
"versionvalue": "Always latest",
"selected": true
},
{
"versionid": "1",
"version": "1",
"questionid": "1",
"selected": false,
"versionvalue": "v1 (latest)"
}
]
}
}}
@ -33,6 +55,11 @@
<div class="mod-indent"></div>
<div class="activityinstance">
{{{questionname}}}
{{#issharedbank}}
<a href="{{bankurl}}">
<span class="badge bg-primary text-light ms-2 mt-1">{{bankname}}</span>
</a>
{{/issharedbank}}
</div>
<span class="actions">
{{#versionselection}}