diff --git a/admin/tool/usertours/classes/tour.php b/admin/tool/usertours/classes/tour.php index 4c4a2014790..0765ee35155 100644 --- a/admin/tool/usertours/classes/tour.php +++ b/admin/tool/usertours/classes/tour.php @@ -557,9 +557,10 @@ class tour { // 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; } @@ -585,6 +586,16 @@ class tour { 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. * @@ -665,11 +676,9 @@ class tour { * @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(); diff --git a/admin/tool/usertours/db/upgrade.php b/admin/tool/usertours/db/upgrade.php index 6769e01ca47..e3c0fc92441 100644 --- a/admin/tool/usertours/db/upgrade.php +++ b/admin/tool/usertours/db/upgrade.php @@ -25,6 +25,7 @@ defined('MOODLE_INTERNAL') || die(); use tool_usertours\manager; +use tool_usertours\tour; /** * Upgrade the user tours plugin. @@ -57,5 +58,26 @@ function xmldb_tool_usertours_upgrade($oldversion) { // Automatically generated Moodle v3.9.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2020082700) { + // 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('/(?\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, 2020082700, 'tool', 'usertours'); + } + return true; } diff --git a/admin/tool/usertours/tests/tour_test.php b/admin/tool/usertours/tests/tour_test.php index d08a9df79b9..c5bf23f012a 100644 --- a/admin/tool/usertours/tests/tour_test.php +++ b/admin/tool/usertours/tests/tour_test.php @@ -64,7 +64,7 @@ class tour_testcase extends advanced_testcase { /** * Helper to mock the database. * - * @return moodle_database + * @return \PHPUnit\Framework\MockObject\MockObject */ public function mock_database() { global $DB; @@ -569,9 +569,14 @@ class tour_testcase extends advanced_testcase { // 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) ; diff --git a/admin/tool/usertours/version.php b/admin/tool/usertours/version.php index c477167923a..e34cc473e8c 100644 --- a/admin/tool/usertours/version.php +++ b/admin/tool/usertours/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2020061501; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2020082700; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2020060900; // Requires this Moodle version. $plugin->component = 'tool_usertours'; // Full name of the plugin (used for diagnostics).