Merge branch 'MDL-69014-39' of https://github.com/paulholden/moodle into MOODLE_39_STABLE

This commit is contained in:
Eloy Lafuente (stronk7) 2020-09-22 23:10:53 +02:00
commit 8193ba9da9
4 changed files with 45 additions and 9 deletions

View file

@ -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();

View file

@ -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 < 2020061502) {
// 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, 2020061502, 'tool', 'usertours');
}
return true;
}

View file

@ -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)
;

View file

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2020061501; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2020061502; // 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).