MDL-76897 quiz: get the context more efficiently where possible

This commit is contained in:
Tim Hunt 2023-02-27 17:43:32 +00:00
parent 1ee7f41762
commit c12ff3d5f3
9 changed files with 36 additions and 15 deletions

View File

@ -64,14 +64,14 @@ class submit_question_version extends external_api {
'newversion' => $newversion
];
$params = self::validate_parameters(self::execute_parameters(), $params);
$response = ['result' => false];
$response = [];
// Get the required data.
$referencedata = $DB->get_record('question_references',
['itemid' => $params['slotid'], 'component' => 'mod_quiz', 'questionarea' => 'slot']);
$slotdata = $DB->get_record('quiz_slots', ['id' => $slotid]);
// Capability check.
list($course, $cm) = get_course_and_cm_from_instance($slotdata->quizid, 'quiz');
[, $cm] = get_course_and_cm_from_instance($slotdata->quizid, 'quiz');
$context = \context_module::instance($cm->id);
self::validate_context($context);
require_capability('mod/quiz:manage', $context);

View File

@ -16,6 +16,8 @@
namespace mod_quiz\local\structure;
use context_module;
/**
* Class slot_random, represents a random question slot type.
*
@ -210,7 +212,7 @@ class slot_random {
// Log slot created event.
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
$event = \mod_quiz\event\slot_created::create([
'context' => \context_module::instance($cm->id),
'context' => context_module::instance($cm->id),
'objectid' => $this->record->id,
'other' => [
'quizid' => $quiz->id,

View File

@ -751,7 +751,7 @@ class edit_renderer extends \plugin_renderer_base {
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())));
$slotid, $structure->get_context()));
$questionnumber = $questionnumbercustomised;
} else {
$questionnumber = $structure->get_displayed_number_for_slot($slot);

View File

@ -363,6 +363,15 @@ class quiz_attempt {
return $this->quizobj->get_cmid();
}
/**
* Get the quiz context.
*
* @return context_module the context of the quiz this attempt belongs to.
*/
public function get_context(): context_module {
return $this->quizobj->get_context();
}
/**
* Is the current user is someone who previews the quiz, rather than attempting it?
*
@ -2108,7 +2117,7 @@ class quiz_attempt {
'objectid' => $this->get_attemptid(),
'relateduserid' => $this->get_userid(),
'courseid' => $this->get_courseid(),
'context' => context_module::instance($this->get_cmid()),
'context' => $this->get_context(),
'other' => [
'quizid' => $this->get_quizid(),
'page' => $this->get_currentpage()
@ -2129,7 +2138,7 @@ class quiz_attempt {
'objectid' => $this->get_attemptid(),
'relateduserid' => $this->get_userid(),
'courseid' => $this->get_courseid(),
'context' => context_module::instance($this->get_cmid()),
'context' => $this->get_context(),
'other' => [
'quizid' => $this->get_quizid(),
'page' => $this->get_currentpage()
@ -2150,7 +2159,7 @@ class quiz_attempt {
'objectid' => $this->get_attemptid(),
'relateduserid' => $this->get_userid(),
'courseid' => $this->get_courseid(),
'context' => context_module::instance($this->get_cmid()),
'context' => $this->get_context(),
'other' => [
'quizid' => $this->get_quizid(),
'page' => $this->get_currentpage()
@ -2173,7 +2182,7 @@ class quiz_attempt {
'objectid' => $this->get_attemptid(),
'relateduserid' => $this->get_userid(),
'courseid' => $this->get_courseid(),
'context' => context_module::instance($this->get_cmid()),
'context' => $this->get_context(),
'other' => [
'quizid' => $this->get_quizid(),
'page' => $this->get_currentpage(),
@ -2197,7 +2206,7 @@ class quiz_attempt {
'objectid' => $this->get_attemptid(),
'relateduserid' => $this->get_userid(),
'courseid' => $this->get_courseid(),
'context' => context_module::instance($this->get_cmid()),
'context' => $this->get_context(),
'other' => [
'quizid' => $this->get_quizid()
]
@ -2218,7 +2227,7 @@ class quiz_attempt {
'objectid' => $this->get_attemptid(),
'relateduserid' => $this->get_userid(),
'courseid' => $this->get_courseid(),
'context' => context_module::instance($this->get_cmid()),
'context' => $this->get_context(),
'other' => [
'quizid' => $this->get_quizid()
]
@ -2236,7 +2245,7 @@ class quiz_attempt {
'objectid' => $this->get_attemptid(),
'relateduserid' => $this->get_userid(),
'courseid' => $this->get_courseid(),
'context' => context_module::instance($this->get_cmid()),
'context' => $this->get_context(),
'other' => [
'quizid' => $this->get_quizid()
]

View File

@ -16,6 +16,7 @@
namespace mod_quiz;
use context_module;
use mod_quiz\question\bank\qbank_helper;
use mod_quiz\question\qubaids_for_quiz;
use stdClass;
@ -341,6 +342,15 @@ class structure {
return $this->quizobj->get_cmid();
}
/**
* Get the quiz context.
*
* @return context_module the context of the quiz that this is the structure of.
*/
public function get_context(): context_module {
return $this->quizobj->get_context();
}
/**
* Get id of the quiz.
*

View File

@ -90,7 +90,7 @@ if (data_submitted() && confirm_sesskey()) {
$params = [
'objectid' => $attemptobj->get_question_attempt($slot)->get_question_id(),
'courseid' => $attemptobj->get_courseid(),
'context' => context_module::instance($attemptobj->get_cmid()),
'context' => $attemptobj->get_quizobj()->get_context(),
'other' => [
'quizid' => $attemptobj->get_quizid(),
'attemptid' => $attemptobj->get_attemptid(),

View File

@ -62,7 +62,7 @@ require_login($course, false, $cm);
$structure = $quizobj->get_structure();
$gradecalculator = $quizobj->get_grade_calculator();
$modcontext = context_module::instance($cm->id);
$modcontext = $quizobj->get_context();
echo $OUTPUT->header(); // Send headers.

View File

@ -64,7 +64,7 @@ if (!$category = $DB->get_record('question_categories', ['id' => $filterconditio
$catcontext = context::instance_by_id($category->contextid);
require_capability('moodle/question:useall', $catcontext);
$thiscontext = context_module::instance($cm->id);
$thiscontext = $quizobj->get_context();
$contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
// Create the editing form.

View File

@ -41,7 +41,7 @@ $cm = $quizobj->get_cm();
$course = $quizobj->get_course();
require_login($course, false, $cm);
$reportlist = quiz_report_list(context_module::instance($cm->id));
$reportlist = quiz_report_list($quizobj->get_context());
if (empty($reportlist) || $userid == $USER->id) {
// If the user cannot see reports, or can see reports but is looking
// at their own grades, redirect them to the view.php page.