Skip to content

Commit 3766be8

Browse files
committed
Merge branch 'MDL-65803-37' of git://github.com/peterRd/moodle into MOODLE_37_STABLE
2 parents 83273d2 + b798b05 commit 3766be8

File tree

5 files changed

+171
-1
lines changed

5 files changed

+171
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* This file keeps track of upgrades to the recentlyaccesseditems block
19+
*
20+
* Sometimes, changes between versions involve alterations to database structures
21+
* and other major things that may break installations.
22+
*
23+
* The upgrade function in this file will attempt to perform all the necessary
24+
* actions to upgrade your older installation to the current version.
25+
*
26+
* If there's something it cannot do itself, it will tell you what you need to do.
27+
*
28+
* The commands in here will all be database-neutral, using the methods of
29+
* database_manager class
30+
*
31+
* Please do not forget to use upgrade_set_timeout()
32+
* before any action that may take longer time to finish.
33+
*
34+
* @package block_recentlyaccesseditems
35+
* @copyright 2019 Peter Dias
36+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37+
*/
38+
39+
defined('MOODLE_INTERNAL') || die();
40+
41+
/**
42+
* Upgrade the recentlyaccesseditems db table.
43+
*
44+
* @param $oldversion
45+
* @return bool
46+
*/
47+
function xmldb_block_recentlyaccesseditems_upgrade($oldversion, $block) {
48+
global $DB;
49+
50+
// Automatically generated Moodle v3.7.0 release upgrade line.
51+
// Put any upgrade step following this.
52+
if ($oldversion < 2019052001) {
53+
$sql = "courseid NOT IN (SELECT c.id from {course} c) OR cmid NOT IN (SELECT cm.id from {course_modules} cm)";
54+
$DB->delete_records_select("block_recentlyaccesseditems", $sql);
55+
upgrade_block_savepoint(true, 2019052001, 'recentlyaccesseditems', false);
56+
}
57+
58+
return true;
59+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
// This file is part of Moodle - https://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* The interface library between the core and the subsystem.
19+
*
20+
* @package block_recentlyaccesseditems
21+
* @copyright 2019 Peter Dias <[email protected]>
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
defined('MOODLE_INTERNAL') || die();
26+
27+
/**
28+
* Pre-delete course module hook to cleanup any records with references to the deleted module.
29+
*
30+
* @param stdClass $cm The deleted course module
31+
*/
32+
function block_recentlyaccesseditems_pre_course_module_delete($cm) {
33+
global $DB;
34+
35+
$DB->delete_records('block_recentlyaccesseditems', ['cmid' => $cm->id]);
36+
}
37+
38+
/**
39+
* Pre-delete course hook to cleanup any records with references to the deleted course.
40+
*
41+
* @param stdClass $course The deleted course
42+
*/
43+
function block_recentlyaccesseditems_pre_course_delete($course) {
44+
global $DB;
45+
46+
$DB->delete_records('block_recentlyaccesseditems', ['courseid' => $course->id]);
47+
}

blocks/recentlyaccesseditems/tests/externallib_test.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,15 @@ public function test_get_recent_items() {
103103
}
104104
$this->assertTrue($record->timeaccess < $result[$key - 1]->timeaccess);
105105
}
106+
107+
// Delete a course and confirm it's activities don't get returned.
108+
delete_course($courses[0], false);
109+
$result = \block_recentlyaccesseditems\external::get_recent_items();
110+
$this->assertCount((count($forum) + count($chat)) - 2, $result);
111+
112+
// Delete a single course module should still return.
113+
course_delete_module($forum[1]->cmid);
114+
$result = \block_recentlyaccesseditems\external::get_recent_items();
115+
$this->assertCount((count($forum) + count($chat)) - 3, $result);
106116
}
107117
}

blocks/recentlyaccesseditems/tests/privacy_test.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,60 @@ public function test_export_user_data() {
207207
// Confirm student's data is exported.
208208
$writer = \core_privacy\local\request\writer::with_context($studentcontext);
209209
$this->assertTrue($writer->has_any_data());
210+
211+
delete_course($course, false);
212+
$sc = context_user::instance($student->id);
213+
$approvedlist = new approved_contextlist($student, $component, [$sc->id]);
214+
provider::export_user_data($approvedlist);
215+
$writer = \core_privacy\local\request\writer::with_context($sc);
216+
$this->assertTrue($writer->has_any_data());
217+
}
218+
219+
/**
220+
* Test exporting data for an approved contextlist with a deleted course
221+
*/
222+
public function test_export_user_data_with_deleted_course() {
223+
global $DB;
224+
225+
$this->resetAfterTest();
226+
$generator = $this->getDataGenerator();
227+
$component = 'block_recentlyaccesseditems';
228+
229+
$student = $generator->create_user();
230+
$studentcontext = context_user::instance($student->id);
231+
232+
// Enrol user in course and add course items.
233+
$course = $generator->create_course();
234+
$generator->enrol_user($student->id, $course->id, 'student');
235+
$forum = $generator->create_module('forum', ['course' => $course]);
236+
$chat = $generator->create_module('chat', ['course' => $course]);
237+
238+
// Generate some recent activity.
239+
$this->setUser($student);
240+
$event = \mod_forum\event\course_module_viewed::create(['context' => context_module::instance($forum->cmid),
241+
'objectid' => $forum->id]);
242+
$event->trigger();
243+
$event = \mod_chat\event\course_module_viewed::create(['context' => context_module::instance($chat->cmid),
244+
'objectid' => $chat->id]);
245+
$event->trigger();
246+
247+
// Confirm data is present.
248+
$params = [
249+
'courseid' => $course->id,
250+
'userid' => $student->id,
251+
];
252+
253+
$result = $DB->count_records('block_recentlyaccesseditems', $params);
254+
$this->assertEquals(2, $result);
255+
delete_course($course, false);
256+
257+
// Export data for student.
258+
$approvedlist = new approved_contextlist($student, $component, [$studentcontext->id]);
259+
provider::export_user_data($approvedlist);
260+
261+
// Confirm student's data is exported.
262+
$writer = \core_privacy\local\request\writer::with_context($studentcontext);
263+
$this->assertFalse($writer->has_any_data());
210264
}
211265

212266
/**

blocks/recentlyaccesseditems/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
*/
2323
defined('MOODLE_INTERNAL') || die();
2424

25-
$plugin->version = 2019052000; // The current plugin version (Date: YYYYMMDDXX).
25+
$plugin->version = 2019052001; // The current plugin version (Date: YYYYMMDDXX).
2626
$plugin->requires = 2019051100; // Requires this Moodle version.
2727
$plugin->component = 'block_recentlyaccesseditems'; // Full name of the plugin (used for diagnostics).

0 commit comments

Comments
 (0)