Skip to content

Commit

Permalink
MDL-69014 tool_usertours: clean up user preferences of deleted tours.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Sep 14, 2020
1 parent b21d86f commit 71ca64f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
19 changes: 14 additions & 5 deletions admin/tool/usertours/classes/tour.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,10 @@ public function remove() {

// Remove the configuration for the tour.
$DB->delete_records('tool_usertours_tours', array('id' => $this->id));

helper::reset_tour_sortorder();

$this->remove_user_preferences();

return null;
}

Expand All @@ -585,6 +586,16 @@ public function reset_step_sortorder() {
return $this;
}

/**
* Remove stored user preferences for the tour
*/
protected function remove_user_preferences(): void {
global $DB;

$DB->delete_records('user_preferences', ['name' => self::TOUR_LAST_COMPLETED_BY_USER . $this->get_id()]);
$DB->delete_records('user_preferences', ['name' => self::TOUR_REQUESTED_BY_USER . $this->get_id()]);
}

/**
* Whether this tour should be displayed to the user.
*
Expand Down Expand Up @@ -665,11 +676,9 @@ public function mark_user_completed() {
* @return $this
*/
public function mark_major_change() {
global $DB;

// Clear old reset and completion notes.
$DB->delete_records('user_preferences', ['name' => self::TOUR_LAST_COMPLETED_BY_USER . $this->get_id()]);
$DB->delete_records('user_preferences', ['name' => self::TOUR_REQUESTED_BY_USER . $this->get_id()]);
$this->remove_user_preferences();

$this->set_config('majorupdatetime', time());
$this->persist();

Expand Down
22 changes: 22 additions & 0 deletions admin/tool/usertours/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
defined('MOODLE_INTERNAL') || die();

use tool_usertours\manager;
use tool_usertours\tour;

/**
* Upgrade the user tours plugin.
Expand Down Expand Up @@ -74,5 +75,26 @@ function xmldb_tool_usertours_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2019111801, 'tool', 'usertours');
}

if ($oldversion < 2019111802) {
// Clean up user preferences of deleted tours.
$select = $DB->sql_like('name', ':lastcompleted') . ' OR ' . $DB->sql_like('name', ':requested');
$params = [
'lastcompleted' => tour::TOUR_LAST_COMPLETED_BY_USER . '%',
'requested' => tour::TOUR_REQUESTED_BY_USER . '%',
];

$preferences = $DB->get_records_select('user_preferences', $select, $params, '', 'DISTINCT name');
foreach ($preferences as $preference) {
// Match tour ID at the end of the preference name, remove all of that preference type if tour ID doesn't exist.
if (preg_match('/(?<tourid>\d+)$/', $preference->name, $matches) &&
!$DB->record_exists('tool_usertours_tours', ['id' => $matches['tourid']])) {

$DB->delete_records('user_preferences', ['name' => $preference->name]);
}
}

upgrade_plugin_savepoint(true, 2019111802, 'tool', 'usertours');
}

return true;
}
11 changes: 8 additions & 3 deletions admin/tool/usertours/tests/tour_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function tearDown() {
/**
* Helper to mock the database.
*
* @return moodle_database
* @return \PHPUnit\Framework\MockObject\MockObject
*/
public function mock_database() {
global $DB;
Expand Down Expand Up @@ -569,9 +569,14 @@ public function test_remove_persisted() {

// Mock the database.
$DB = $this->mock_database();
$DB->expects($this->once())

$DB->expects($this->exactly(3))
->method('delete_records')
->with($this->equalTo('tool_usertours_tours'), $this->equalTo(['id' => $id]))
->withConsecutive(
[$this->equalTo('tool_usertours_tours'), $this->equalTo(['id' => $id])],
[$this->equalTo('user_preferences'), $this->equalTo(['name' => tour::TOUR_LAST_COMPLETED_BY_USER . $id])],
[$this->equalTo('user_preferences'), $this->equalTo(['name' => tour::TOUR_REQUESTED_BY_USER . $id])]
)
->willReturn(null)
;

Expand Down
2 changes: 1 addition & 1 deletion admin/tool/usertours/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

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

$plugin->version = 2019111801; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2019111802; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2019111200; // Requires this Moodle version.
$plugin->component = 'tool_usertours'; // Full name of the plugin (used for diagnostics).

0 comments on commit 71ca64f

Please sign in to comment.