Skip to content

Commit

Permalink
fixup! MDL-82587 mod_quiz: Add slot version updated event
Browse files Browse the repository at this point in the history
  • Loading branch information
cameron1729 committed Aug 1, 2024
1 parent fe8cf17 commit cf6a1bf
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 29 deletions.
2 changes: 1 addition & 1 deletion mod/quiz/amd/build/question_slot.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mod/quiz/amd/build/question_slot.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion mod/quiz/amd/src/question_slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const setQuestionVersion = (slotId, newVersion) => fetchMany([{
*/
const registerEventListeners = () => {
document.addEventListener('change', e => {
window.console.log('changed');
if (!e.target.matches('[data-action="mod_quiz-select_slot"][data-slot-id]')) {
return;
}
Expand Down
34 changes: 9 additions & 25 deletions mod/quiz/classes/event/slot_version_updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ public static function get_name(): string {
return get_string('eventslotversionupdated', 'mod_quiz');
}

/**
* Get event description.
*
* @return string
*/
#[\Override]
public function get_description(): string {
$previousversion = $this->other['previousversion'] ?? 'Always latest';
$newversion = $this->other['newversion'] ?? 'Always latest';
Expand All @@ -64,18 +60,12 @@ public function get_description(): string {
"Its question version was changed from '$previousversion' to '$newversion'.";
}

/**
* Get associated URL.
*
* @return url
*/
#[\Override]
public function get_url(): url {
return new url('/mod/quiz/edit.php', ['cmid' => $this->contextinstanceid]);
}

/**
* Validate event data.
*/
#[\Override]
protected function validate_data(): void {
parent::validate_data();

Expand All @@ -91,29 +81,23 @@ protected function validate_data(): void {
throw new coding_exception('The \'quizid\' value must be set in other.');
}

if (!isset($this->other['previousversion'])) {
// The value of previousversion and newversion can be null, so we check if
// the array key exists.
if (!array_key_exists('previousversion', $this->other)) {
throw new coding_exception('The \'previousversion\' value must be set in other.');
}

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

/**
* Get object ID mapping.
*
* @return array
*/
#[\Override]
public static function get_objectid_mapping(): array {
return ['db' => 'quiz_slots', 'restore' => 'quiz_question_instance'];
}

/**
* Get other mapping.
*
* @return array
*/
#[\Override]
public static function get_other_mapping(): array {
$othermapped = [];
$othermapped['quizid'] = ['db' => 'quiz', 'restore' => 'quiz'];
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/classes/structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ public function update_slot_version(int $id, ?int $newversion): bool {
$context = $this->quizobj->get_context();
$refparams = ['usingcontextid' => $context->id, 'component' => 'mod_quiz', 'questionarea' => 'slot', 'itemid' => $slot->id];
$reference = $DB->get_record('question_references', $refparams, '*', MUST_EXIST);
$oldversion = (int)$reference->version;
$oldversion = is_null($reference->version) ? null : (int) $reference->version;
$reference->version = $newversion === 0 ? null : $newversion;
$existsparams = ['questionbankentryid' => $reference->questionbankentryid, 'version' => $newversion];
$versionexists = $DB->record_exists('question_versions', $existsparams);
Expand Down
4 changes: 4 additions & 0 deletions mod/quiz/tests/event/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1221,8 +1221,12 @@ public function test_slot_version_updated(): void {
$event = reset($events);

// Check that the event data is valid.
$expecteddesc = "The user with id '2' updated the slot with id '$numqslotid' " .
"belonging to the quiz with course module id '{$quizobj->get_cmid()}'. " .
"Its question version was changed from 'Always latest' to '2'.";
$this->assertInstanceOf('\mod_quiz\event\slot_version_updated', $event);
$this->assertEquals($quizobj->get_context(), $event->get_context());
$this->assertEquals($expecteddesc, $event->get_description());
$this->assertEventContextNotUsed($event);
}

Expand Down

0 comments on commit cf6a1bf

Please sign in to comment.