Skip to content

Commit

Permalink
MDL-63658 core_favourites: implement privacy API for favourites
Browse files Browse the repository at this point in the history
  • Loading branch information
snake committed Oct 18, 2018
1 parent 4a02aae commit ac9138d
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 0 deletions.
143 changes: 143 additions & 0 deletions favourites/classes/privacy/provider.php
@@ -0,0 +1,143 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Privacy class for requesting user data for the favourites subsystem.
*
* @package core_favourites
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_favourites\privacy;

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

use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\context;
use \core_privacy\local\request\approved_contextlist;

/**
* Privacy class for requesting user data.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\provider, \core_privacy\local\request\subsystem\plugin_provider {

/**
* Returns metadata about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection) : collection {
return $collection->add_database_table('favourite', [
'userid' => 'privacy:metadata:favourite:userid',
'component' => 'privacy:metadata:favourite:component',
'itemtype' => 'privacy:metadata:favourite:itemtype',
'itemid' => 'privacy:metadata:favourite:itemid',
'ordering' => 'privacy:metadata:favourite:ordering',
'timecreated' => 'privacy:metadata:favourite:timecreated',
'timemodified' => 'privacy:metadata:favourite:timemodified',
], 'privacy:metadata:favourite');
}

/**
* Provide a list of contexts which have favourites for the user, in the respective area (component/itemtype combination).
*
* This method is to be called by consumers of the favourites subsystem (plugins), in their get_contexts_for_userid() method,
* to add the contexts for items which may have been favourited, but would normally not be reported as having user data by the
* plugin responsible for them.
*
* Consider an example: Favourite courses.
* Favourite courses will be handled by the core_course subsystem and courses can be favourited at site context.
*
* Now normally, the course provider method get_contexts_for_userid() would report the context of any courses the user is in.
* Then, we'd export data for those contexts. This won't include courses the user has favourited, but is not a member of.
*
* To report the full list, the course provider needs to be made aware of the contexts of any courses the user may have marked
* as favourites. Course will need to ask th favourites subsystem for this - a call to add_contexts_for_userid($userid).
*
* Once called, if a course has been marked as a favourite, at site context, then we'd return the site context. During export,
* the consumer (course), just looks at all contexts and decides whether to export favourite courses for each one.
*
* @param \core_privacy\local\request\contextlist $contextlist
* @param int $userid The id of the user in scope.
* @param string $component the frankenstyle component name.
* @param string $itemtype the type of the favourited items.
*/
public static function add_contexts_for_userid(\core_privacy\local\request\contextlist $contextlist, int $userid,
string $component, string $itemtype = null) {
$sql = "SELECT contextid
FROM {favourite} f
WHERE userid = :userid
AND component = :component";
if (!is_null($itemtype)) {
$sql .= "AND itemtype = :itemtype";
}
$params = ['userid' => $userid, 'component' => $component, 'itemtype' => $itemtype];
$contextlist->add_from_sql($sql, $params);
}

/**
* Delete all favourites for all users in the specified contexts, and component area.
*
* @param \context $context The context to which deletion is scoped.
* @param string $component The favourite's component name.
* @param string $itemtype The favourite's itemtype.
* @throws \dml_exception if any errors are encountered during deletion.
*/
public static function delete_favourites_for_all_users(\context $context, string $component, string $itemtype) {
global $DB;

$params = [
'component' => $component,
'itemtype' => $itemtype,
'contextid' => $context->id
];

$select = "component = :component AND itemtype =:itemtype AND contextid = :contextid";
$DB->delete_records_select('favourite', $select, $params);
}

/**
* Delete all favourites for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
* @param string $component
* @param string $itemtype
* @throws \coding_exception
* @throws \dml_exception
*/
public static function delete_favourites_for_user(approved_contextlist $contextlist, string $component, string $itemtype) {
global $DB;

$userid = $contextlist->get_user()->id;

list($insql, $inparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);

$params = [
'userid' => $userid,
'component' => $component,
'itemtype' => $itemtype,
];
$params += $inparams;

$select = "userid = :userid AND component = :component AND itemtype =:itemtype AND contextid $insql";
$DB->delete_records_select('favourite', $select, $params);
}
}
137 changes: 137 additions & 0 deletions favourites/tests/privacy_test.php
@@ -0,0 +1,137 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Privacy tests for core_favourites.
*
* @package core_favourites
* @category test
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

use \core_privacy\tests\provider_testcase;
use \core_favourites\privacy\provider;

/**
* Unit tests for favourites/classes/privacy/provider
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class privacy_test extends provider_testcase {

public function setUp() {
$this->resetAfterTest(true);
}

/**
* Helper to set up some sample users and courses.
*/
protected function set_up_courses_and_users() {
$user1 = self::getDataGenerator()->create_user();
$user1context = \context_user::instance($user1->id);
$user2 = self::getDataGenerator()->create_user();
$user2context = \context_user::instance($user2->id);
$course1 = self::getDataGenerator()->create_course();
$course2 = self::getDataGenerator()->create_course();
$course1context = context_course::instance($course1->id);
$course2context = context_course::instance($course2->id);
return [$user1, $user2, $user1context, $user2context, $course1context, $course2context];
}

/**
* Test confirming that contexts of favourited items can be added to the contextlist.
*/
public function test_add_contexts_for_userid() {
list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();

// Favourite 2 courses for user1 and 1 course for user2, all at the site context.
$ufservice1 = \core_favourites\services::get_service_for_user_context($user1context);
$ufservice2 = \core_favourites\services::get_service_for_user_context($user2context);
$systemcontext = context_system::instance();
$ufservice1->create_favourite('core_course', 'course', $course1context->instanceid, $systemcontext);
$ufservice1->create_favourite('core_course', 'course', $course2context->instanceid, $systemcontext);
$ufservice2->create_favourite('core_course', 'course', $course2context->instanceid, $systemcontext);
$this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'course'));
$this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'course'));

// Now, just for variety, let's assume you can favourite a course at user context, and do so for user1.
$ufservice1->create_favourite('core_course', 'course', $course1context->instanceid, $user1context);

// Now, ask the favourites privacy api to export contexts for favourites of the type we just created, for user1.
$contextlist = new \core_privacy\local\request\contextlist();
\core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $user1->id, 'core_course', 'course');

// Verify we have two contexts in the list for user1.
$this->assertCount(2, $contextlist->get_contextids());

// And verify we only have the system context returned for user2.
$contextlist = new \core_privacy\local\request\contextlist();
\core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $user2->id, 'core_course', 'course');
$this->assertCount(1, $contextlist->get_contextids());
}

/**
* Test deletion of user favourites based on an approved_contextlist and component area.
*/
public function test_delete_favourites_for_user() {
list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();

// Favourite 2 courses for user1 and 1 course for user2, all at the user context.
$ufservice1 = \core_favourites\services::get_service_for_user_context($user1context);
$ufservice2 = \core_favourites\services::get_service_for_user_context($user2context);
$ufservice1->create_favourite('core_course', 'course', $course1context->instanceid, $user1context);
$ufservice1->create_favourite('core_course', 'course', $course2context->instanceid, $user1context);
$ufservice2->create_favourite('core_course', 'course', $course2context->instanceid, $user2context);
$this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'course'));
$this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'course'));

// Now, delete the favourites for user1 only.
$approvedcontextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_course', [$user1context->id]);
provider::delete_favourites_for_user($approvedcontextlist, 'core_course', 'course');

// Verify that we have no favourite courses for user1 but that the records are in tact for user2.
$this->assertCount(0, $ufservice1->find_favourites_by_type('core_course', 'course'));
$this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'course'));
}

public function test_delete_favourites_for_all_users() {
list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();

// Favourite 2 course modules for user1 and 1 course module for user2 all in course 1 context.
$ufservice1 = \core_favourites\services::get_service_for_user_context($user1context);
$ufservice2 = \core_favourites\services::get_service_for_user_context($user2context);
$ufservice1->create_favourite('core_course', 'modules', 1, $course1context);
$ufservice1->create_favourite('core_course', 'modules', 2, $course1context);
$ufservice2->create_favourite('core_course', 'modules', 3, $course1context);

// Now, favourite a different course module for user2 in course 2.
$ufservice2->create_favourite('core_course', 'modules', 5, $course2context);

$this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'modules'));
$this->assertCount(2, $ufservice2->find_favourites_by_type('core_course', 'modules'));

// Now, delete all course module favourites in the 'course1' context only.
provider::delete_favourites_for_all_users($course1context, 'core_course', 'modules');

// Verify that only a single favourite for user1 in course 1 remains.
$this->assertCount(0, $ufservice1->find_favourites_by_type('core_course', 'modules'));
$this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'modules'));
}
}
8 changes: 8 additions & 0 deletions lang/en/favourites.php
Expand Up @@ -20,3 +20,11 @@
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['privacy:metadata:favourite'] = 'Stores references to items which have been marked as favourites.';
$string['privacy:metadata:favourite:component'] = 'The component to which the favourite belongs to. E.g. core_user.';
$string['privacy:metadata:favourite:itemid'] = 'The identifier for the item being marked as a favourite.';
$string['privacy:metadata:favourite:itemtype'] = 'The type of the favourite item. E.g. course.';
$string['privacy:metadata:favourite:ordering'] = 'A number used to order the favourites of the same type.';
$string['privacy:metadata:favourite:timecreated'] = 'The time at which the item was marked as a favourite.';
$string['privacy:metadata:favourite:timemodified'] = 'The time at which favourite was last modified.';
$string['privacy:metadata:favourite:userid'] = 'The user who created the favourite.';

0 comments on commit ac9138d

Please sign in to comment.