Skip to content

Commit

Permalink
Privacy provider support implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
vasanthlmsace committed Sep 6, 2021
1 parent d487235 commit b1d47c1
Show file tree
Hide file tree
Showing 9 changed files with 530 additions and 4 deletions.
43 changes: 43 additions & 0 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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 implementation for learning tools parent plugin
*
* @package local_learningtools
* @copyright 2021, bdecent gmbh bdecent.de
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_learningtools\privacy;

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

/**
* The local_learningtools parent plugin does not store any data. Subplugin stores user data.
*/
class provider implements \core_privacy\local\metadata\null_provider {

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason() : string {

return 'privacy:metadata';
}
}
2 changes: 1 addition & 1 deletion lang/en/local_learningtools.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
$string['learningtoolssettings'] = "Learning Tools Settings";
$string['ltoolsusermenu'] = "Display Learning Tools in usermenu bar";
$string['ltoolusermenu_help'] = "List of menus available to display in the user menu. copy and paste the below given menu tools path in user menu items. Go to the Appearence->Themes->Theme settings in user menu items. ";

$string['privacy:metadata'] = 'Learning tools parent plugin don\'t store any user-related data, The learning tool sub plugins are stores the users data individually.';
$string['coursenotes'] = "Course Notes";
$string['addbookmark'] = "Add bookmark";
$string['createnote'] = "Create note";
Expand Down
227 changes: 227 additions & 0 deletions ltool/bookmarks/classes/privacy/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
<?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 implementation for bookmarks learning tools subplugin.
*
* @package ltool_bookmarks
* @copyright bdecent GmbH 2021
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace ltool_bookmarks\privacy;

use stdClass;
use context;

use core_privacy\local\metadata\collection;
use \core_privacy\local\request\contextlist;
use \core_privacy\local\request\userlist;
use \core_privacy\local\request\approved_userlist;
use \core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\helper;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;

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

/**
* The ltool_note modules data export and deletion options.
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\plugin\provider {

/**
* List of summary for the stored data.
*
* @param collection $collection
* @return collection
*/
public static function get_metadata(collection $collection): collection {
$bookmarksmetadata = [
'userid' => 'privacy:metadata:bookmarks:userid',
'course' => 'privacy:metadata:bookmarks:course',
'coursemodule' => 'privacy:metadata:bookmarks:coursemodule',
'contextlevel' => 'privacy:metadata:bookmarks:contextlevel',
'contextid' => 'privacy:metadata:bookmarks:contextid',
'pagetype' => 'privacy:metadata:bookmarks:pagetype',
'pagetitle' => 'privacy:metadata:bookmarks:pagetitle',
'pageurl' => 'privacy:metadata:bookmarks:pageurl',
'timemodified' => 'privacy:metadata:bookmarks:timemodified'
];
$collection->add_database_table('learningtools_bookmarks', $bookmarksmetadata, 'privacy:metadata:bookmarksmetadata');

return $collection;
}

/**
* Check the context user has any bookmarks.
*
* @param int $userid
* @return bool
*/
public static function user_has_bookmark_data($userid) {
global $DB;

if ($DB->count_records('learningtools_bookmarks', ['userid' => $userid])) {
return true;
}
return false;
}

/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid) : contextlist {
$contextlist = new \core_privacy\local\request\contextlist();

if (self::user_has_bookmark_data($userid)) {
$contextlist->add_user_context($userid);
}

return $contextlist;
}

/**
* Get the list of users who have data within a context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();

if (!$context instanceof \context_user) {
return;
}

if (self::user_has_bookmark_data($context->instanceid)) {
$userlist->add_user($context->instanceid);
}
}

/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
global $DB;
$context = $userlist->get_context();
if ($context instanceof \context_user) {
list($userinsql, $userinparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
if (!empty($userinparams)) {
$sql = "userid {$userinsql}";
$DB->delete_records_select('learningtools_bookmarks', $sql, $userinparams);
}
}
}

/**
* Delete user completion data for multiple context.
*
* @param approved_contextlist $contextlist The approved context and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;

if (empty($contextlist->count())) {
return;
}

foreach ($contextlist->get_contexts() as $context) {
if ($context->contextlevel == CONTEXT_USER) {
self::delete_user_data($context->instanceid);
}
}
}

/**
* Delete all completion data for all users in the specified context.
*
* @param context $context Context to delete data from.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
global $DB;
if ($context->contextlevel == CONTEXT_USER) {
self::delete_user_data($context->instanceid);
}
}

/**
* This does the deletion of user data given a userid.
*
* @param int $userid The user ID
*/
private static function delete_user_data(int $userid) {
global $DB;
if ($DB->delete_records('learningtools_bookmarks', ['userid' => $userid])) {
return true;
}
return false;
}

/**
* Export all user data for the specified user, in the specified contexts, using the supplied exporter instance.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;

if (empty($contextlist->count())) {
return;
}
// Context user.
$user = $contextlist->get_user();

$records = $DB->get_records('learningtools_bookmarks', ['userid' => $user->id]);

if (empty($records)) {
return;
}

$exportdata = array_map(function($record) {

$modulename = ($record->coursemodule) ? get_coursemodule_from_id('', $record->coursemodule)->name : '-';

return [
'contextlevel' => $record->contextlevel,
'contextid' => $record->contextid,
'course' => ($record->course == 1) ? 'system' : format_string(get_course($record->course)->fullname),
'coursemodule' => format_string($modulename),
'pagetitle' => $record->pagetitle,
'pagetype' => $record->pagetype,
'pageurl' => $record->pageurl,
'timecreated' => ($record->timecreated) ? transform::datetime($record->timecreated) : '-',
];
}, $records);

if (!empty($exportdata)) {
$context = \context_user::instance($user->id);
// Fetch the generic module data for the bookmarks.
$contextdata = helper::get_context_data($context, $user);
$contextdata = (object)array_merge((array)$contextdata, $exportdata);
writer::with_context($context)->export_data(
[get_string('privacybookmarks', 'ltool_bookmarks').' '.$user->id],
$contextdata
);
}
}
}
14 changes: 14 additions & 0 deletions ltool/bookmarks/lang/en/ltool_bookmarks.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@
$string['bookmarks:viewbookmarks'] = "View the others bookmarks.";
$string['bookmarks:managebookmarks'] = "Manage the others boomarks.";
$string['bookmarks'] = "Bookmarks";

// Privacy API Metadata.
$string['privacy:metadata:bookmarks:userid'] = 'The ID of the user';
$string['privacy:metadata:bookmarks:course'] = 'The ID of the course';
$string['privacy:metadata:bookmarks:coursemodule'] = 'The ID of the course module if user bookmarked the course modules page';
$string['privacy:metadata:bookmarks:contextlevel'] = 'The Context level of the bookmark page';
$string['privacy:metadata:bookmarks:contextid'] = 'The Context ID of the bookmark page';
$string['privacy:metadata:bookmarks:pagetype'] = 'Bookmarked pagetype';
$string['privacy:metadata:bookmarks:pagetitle'] = 'Page title of Bookmarked page';
$string['privacy:metadata:bookmarks:pageurl'] = 'Bookmarked page url';
$string['privacy:metadata:bookmarks:timecreated'] = 'Time of the bookmarks created';
$string['privacy:metadata:bookmarks:timemodified'] = 'Time of the bookmarks modified';
$string['privacy:metadata:bookmarksmetadata'] = 'List of users data stored in bookmarks subplugin';
$string['privacybookmarks'] = 'Learning tools - Bookmarks';
2 changes: 1 addition & 1 deletion ltool/bookmarks/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'ltool_bookmarks';
$plugin->version = 2021082900;
$plugin->version = 2021090600;
$plugin->requires = 2020061501;


Loading

0 comments on commit b1d47c1

Please sign in to comment.