forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Monllao
committed
May 9, 2018
1 parent
02c7769
commit 74fc7d3
Showing
9 changed files
with
517 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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 Subsystem implementation for core_search. | ||
* | ||
* @package core_search | ||
* @copyright 2018 David Monllao | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace core_search\privacy; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
/** | ||
* Privacy Subsystem for core_search implementing null_provider. | ||
* | ||
* @copyright 2018 David Monllao | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?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. | ||
* | ||
* @package search_simpledb | ||
* @copyright 2018 David Monllao | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
namespace search_simpledb\privacy; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
use core_privacy\local\metadata\collection; | ||
use core_privacy\local\request\writer; | ||
use core_privacy\local\request\transform; | ||
use core_privacy\local\request\contextlist; | ||
use core_privacy\local\request\approved_contextlist; | ||
|
||
/** | ||
* Provider for the search_simpledb plugin. | ||
* | ||
* @copyright 2018 David Monllao | ||
* @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\plugin\provider { | ||
|
||
/** | ||
* Returns meta data 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 { | ||
$collection->add_database_table( | ||
'search_simpledb_index', | ||
[ | ||
'docid' => 'privacy:metadata:index:docid', | ||
'itemid' => 'privacy:metadata:index:itemid', | ||
'title' => 'privacy:metadata:index:title', | ||
'content' => 'privacy:metadata:index:content', | ||
'contextid' => 'privacy:metadata:index:contextid', | ||
'areaid' => 'privacy:metadata:index:areaid', | ||
'type' => 'privacy:metadata:index:type', | ||
'courseid' => 'privacy:metadata:index:courseid', | ||
'owneruserid' => 'privacy:metadata:index:owneruserid', | ||
'modified' => 'privacy:metadata:index:modified', | ||
'userid' => 'privacy:metadata:index:userid', | ||
'description1' => 'privacy:metadata:index:description1', | ||
'description2' => 'privacy:metadata:index:description2', | ||
], | ||
'privacy:metadata:index' | ||
); | ||
return $collection; | ||
} | ||
|
||
/** | ||
* Get the list of contexts that contain user information for the specified user. | ||
* | ||
* @param int $userid The user to search. | ||
* @return contextlist $contextlist The contextlist containing 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(); | ||
|
||
$params = ['userid' => $userid, 'owneruserid' => $userid]; | ||
$sql = "SELECT DISTINCT contextid FROM {search_simpledb_index} WHERE (userid = :userid OR owneruserid = :owneruserid)"; | ||
$contextlist->add_from_sql($sql, $params); | ||
|
||
return $contextlist; | ||
} | ||
|
||
/** | ||
* Export all user data for the specified user, in the specified contexts. | ||
* | ||
* @param approved_contextlist $contextlist The approved contexts to export information for. | ||
*/ | ||
public static function export_user_data(approved_contextlist $contextlist) { | ||
global $DB; | ||
|
||
// Plugin search_simpledb uses the default document object (core_search\document) which uses FORMAT_PLAIN. | ||
$textformat = FORMAT_PLAIN; | ||
|
||
$userid = $contextlist->get_user()->id; | ||
|
||
$ctxfields = \context_helper::get_preload_record_columns_sql('ctx'); | ||
list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED); | ||
$sql = "SELECT ssi.*, $ctxfields FROM {search_simpledb_index} ssi | ||
JOIN {context} ctx ON ctx.id = ssi.contextid | ||
WHERE ssi.contextid $contextsql AND (ssi.userid = :userid OR ssi.owneruserid = :owneruserid)"; | ||
$params = ['userid' => $userid, 'owneruserid' => $userid] + $contextparams; | ||
|
||
$records = $DB->get_recordset_sql($sql, $params); | ||
foreach ($records as $record) { | ||
|
||
\context_helper::preload_from_record($record); | ||
$context = \context::instance_by_id($record->contextid); | ||
$document = (object)[ | ||
'title' => format_string($record->title, true, ['context' => $context]), | ||
'content' => format_text($record->content, $textformat, ['context' => $context]), | ||
'description1' => format_text($record->description1, $textformat, ['context' => $context]), | ||
'description2' => format_text($record->description2, $textformat, ['context' => $context]), | ||
'context' => $context->get_context_name(true, true), | ||
'modified' => transform::datetime($record->modified), | ||
|
||
]; | ||
|
||
$path = [get_string('search', 'search'), $record->docid]; | ||
writer::with_context($context)->export_data($path, $document); | ||
} | ||
$records->close(); | ||
} | ||
|
||
/** | ||
* Delete all data for all users in the specified context. | ||
* | ||
* @param context $context The specific context to delete data for. | ||
*/ | ||
public static function delete_data_for_all_users_in_context(\context $context) { | ||
global $DB; | ||
|
||
$DB->delete_records('search_simpledb_index', ['contextid' => $context->id]); | ||
|
||
if ($context->contextlevel == CONTEXT_USER) { | ||
$select = "userid = :userid OR owneruserid = :owneruserid"; | ||
$params = ['userid' => $context->instanceid, 'owneruserid' => $context->instanceid]; | ||
$DB->delete_records_select('search_simpledb_index', $select, $params); | ||
} | ||
} | ||
|
||
/** | ||
* Delete all user data for the specified user, in the specified contexts. | ||
* | ||
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for. | ||
*/ | ||
public static function delete_data_for_user(approved_contextlist $contextlist) { | ||
global $DB; | ||
|
||
$userid = $contextlist->get_user()->id; | ||
|
||
list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED); | ||
$select = "contextid $contextsql AND (userid = :userid OR owneruserid = :owneruserid)"; | ||
$params = ['userid' => $userid, 'owneruserid' => $userid] + $contextparams; | ||
$DB->delete_records_select('search_simpledb_index', $select, $params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.