forked from lsuits/lsu-block_quickmail
-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
0ca5a0e
commit a12e559
Showing
4 changed files
with
366 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
<?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 block_quickmail. | ||
* | ||
* @package block_quickmail | ||
* @copyright The Regents of the University of California | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
namespace block_quickmail\privacy; | ||
|
||
use context; | ||
use context_course; | ||
use context_user; | ||
use core_privacy\local\metadata\collection; | ||
use core_privacy\local\metadata\provider as metadata_provider; | ||
use core_privacy\local\request\approved_contextlist; | ||
use core_privacy\local\request\approved_userlist; | ||
use core_privacy\local\request\contextlist; | ||
use core_privacy\local\request\core_userlist_provider; | ||
use core_privacy\local\request\plugin\provider as data_provider; | ||
use core_privacy\local\request\userlist; | ||
use core_privacy\local\request\writer; | ||
|
||
/** | ||
* Privacy metadata provider class. | ||
* | ||
* @package block_quickmail | ||
* @copyright The Regents of the University of California | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class provider implements core_userlist_provider, data_provider, metadata_provider { | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function get_metadata(collection $collection): collection { | ||
$collection->add_database_table( | ||
'block_quickmail_log', | ||
[ | ||
'id' => 'privacy:metadata:block_quickmail_log:id', | ||
'courseid' => 'privacy:metadata:block_quickmail_log:courseid', | ||
'userid' => 'privacy:metadata:block_quickmail_log:userid', | ||
'alterateid' => 'privacy:metadata:block_quickmail_log:alterateid', | ||
'mailto' => 'privacy:metadata:block_quickmail_log:mailto', | ||
'subject' => 'privacy:metadata:block_quickmail_log:subject', | ||
'message' => 'privacy:metadata:block_quickmail_log:message', | ||
'attachment' => 'privacy:metadata:block_quickmail_log:attachment', | ||
'format' => 'privacy:metadata:block_quickmail_log:format', | ||
'time' => 'privacy:metadata:block_quickmail_log:time', | ||
'failuserids' => 'privacy:metadata:block_quickmail_log:failuserid', | ||
'additional_emails' => 'privacy:metadata:block_quickmail_log:additional_emails', | ||
], | ||
'privacy:metadata:block_quickmail_log', | ||
); | ||
|
||
$collection->add_database_table( | ||
'block_quickmail_signatures', | ||
[ | ||
'id' => 'privacy:metadata:block_quickmail_signatures:id', | ||
'userid' => 'privacy:metadata:block_quickmail_signatures:userid', | ||
'title' => 'privacy:metadata:block_quickmail_signatures:title', | ||
'signature' => 'privacy:metadata:block_quickmail_signatures:signature', | ||
'default_flag' => 'privacy:metadata:block_quickmail_signatures:default_flag', | ||
], | ||
'privacy:metadata:block_quickmail_signatures', | ||
); | ||
|
||
$collection->add_database_table( | ||
'block_quickmail_drafts', | ||
[ | ||
'id' => 'privacy:metadata:block_quickmail_drafts:id', | ||
'courseid' => 'privacy:metadata:block_quickmail_drafts:courseid', | ||
'userid' => 'privacy:metadata:block_quickmail_drafts:userid', | ||
'alternateid' => 'privacy:metadata:block_quickmail_drafts:alternateid', | ||
'mailto' => 'privacy:metadata:block_quickmail_drafts:mailto', | ||
'subject' => 'privacy:metadata:block_quickmail_drafts:subject', | ||
'message' => 'privacy:metadata:block_quickmail_drafts:message', | ||
'attachment' => 'privacy:metadata:block_quickmail_drafts:attachment', | ||
'format' => 'privacy:metadata:block_quickmail_drafts:format', | ||
'time' => 'privacy:metadata:block_quickmail_drafts:time', | ||
'additional_emails' => 'privacy:metadata:block_quickmail_drafts:additional_email', | ||
], | ||
'privacy:metadata:block_quickmail_drafts' | ||
); | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function get_contexts_for_userid(int $userid): contextlist { | ||
$contextlist = new contextlist(); | ||
|
||
$params = [ | ||
'contextlevel' => CONTEXT_USER, | ||
'userid' => $userid, | ||
]; | ||
|
||
$sql = <<<EOD | ||
SELECT c.id | ||
FROM {block_quickmail_drafts} bqd | ||
JOIN {context} c ON c.instanceid = bqd.userid AND c.contextlevel = :contextlevel | ||
WHERE bqd.userid = :userid | ||
GROUP BY c.id | ||
EOD; | ||
|
||
$contextlist->add_from_sql($sql, $params); | ||
|
||
$sql = <<<EOD | ||
SELECT c.id | ||
FROM {block_quickmail_signatures} bqs | ||
JOIN {context} c ON c.instanceid = bqs.userid AND c.contextlevel = :contextlevel | ||
WHERE bqs.userid = :userid | ||
GROUP BY c.id | ||
EOD; | ||
$contextlist->add_from_sql($sql, $params); | ||
|
||
$sql = <<<EOD | ||
SELECT c.id | ||
FROM {block_quickmail_log} bql | ||
JOIN {context} c ON c.instanceid = bql.userid AND c.contextlevel = :contextlevel | ||
WHERE bql.userid = :userid | ||
GROUP BY c.id | ||
EOD; | ||
$contextlist->add_from_sql($sql, $params); | ||
|
||
return $contextlist; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function export_user_data(approved_contextlist $contextlist): void { | ||
global $DB; | ||
$contexts = $contextlist->get_contexts(); | ||
if (count($contexts) == 0) { | ||
return; | ||
} | ||
|
||
$context = reset($contexts); | ||
|
||
if ($context->contextlevel !== CONTEXT_USER) { | ||
return; | ||
} | ||
|
||
$params = [ | ||
'userid' => $contextlist->get_user()->id, | ||
]; | ||
|
||
$signaturessql = <<<EOD | ||
SELECT bqs.* | ||
FROM {block_quickmail_signatures} bqs | ||
WHERE bqs.userid = :userid | ||
ORDER BY bqs.id | ||
EOD; | ||
|
||
$draftssql = <<<EOD | ||
SELECT bqd.* | ||
FROM {block_quickmail_drafts} bqd | ||
WHERE bqd.userid = :userid | ||
ORDER BY bqd.id | ||
EOD; | ||
|
||
$logsql = <<<EOD | ||
SELECT bql.* | ||
FROM {block_quickmail_log} bql | ||
WHERE bql.userid = :userid | ||
ORDER BY bql.id | ||
EOD; | ||
|
||
$drafts = $DB->get_records_sql($draftssql, $params); | ||
$signatures = $DB->get_records_sql($signaturessql, $params); | ||
$logs = $DB->get_records_sql($logsql, $params); | ||
|
||
$data = (object) [ | ||
'signatures' => $signatures, | ||
'log' => $logs, | ||
'drafts' => $drafts, | ||
]; | ||
|
||
writer::with_context($context)->export_data([ | ||
get_string('pluginname', 'block_quickmail'), | ||
], $data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function delete_data_for_all_users_in_context(context $context): void { | ||
global $DB; | ||
if ($context->contextlevel !== CONTEXT_USER) { | ||
return; | ||
} | ||
|
||
$userid = $context->instanceid; | ||
|
||
$DB->delete_records('block_quickmail_drafts', ['userid' => $userid]); | ||
$DB->delete_records('block_quickmail_signatures', ['userid' => $userid]); | ||
$DB->delete_records('block_quickmail_log', ['userid' => $userid]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function delete_data_for_user(approved_contextlist $contextlist): void { | ||
global $DB; | ||
$contexts = $contextlist->get_contexts(); | ||
if (count($contexts) == 0) { | ||
return; | ||
} | ||
$context = reset($contexts); | ||
if ($context->contextlevel !== CONTEXT_USER) { | ||
return; | ||
} | ||
|
||
$userid = $contextlist->get_user()->id; | ||
|
||
$DB->delete_records('block_quickmail_drafts', ['userid' => $userid]); | ||
$DB->delete_records('block_quickmail_signatures', ['userid' => $userid]); | ||
$DB->delete_records('block_quickmail_log', ['userid' => $userid]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function get_users_in_context(userlist $userlist): void { | ||
$context = $userlist->get_context(); | ||
|
||
if (!$context instanceof context_course) { | ||
return; | ||
} | ||
|
||
$sql = "SELECT * FROM {block_quickmail_drafts}"; | ||
$userlist->add_from_sql('user_id', $sql, ['courseid' => $context->instanceid]); | ||
|
||
$sql = "SELECT * FROM {block_quickmail_signatures}"; | ||
$userlist->add_from_sql('user_id', $sql, ['courseid' => $context->instanceid]); | ||
|
||
$sql = "SELECT * FROM {block_quickmail_log}"; | ||
$userlist->add_from_sql('user_id', $sql, ['courseid' => $context->instanceid]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function delete_data_for_users(approved_userlist $userlist): void { | ||
$users = $userlist->get_users(); | ||
foreach ($users as $user) { | ||
$contextlist = new approved_contextlist( | ||
$user, | ||
'block_quickmail', | ||
[context_user::instance($user->id)->id] | ||
); | ||
self::delete_data_for_user($contextlist); | ||
} | ||
} | ||
} |
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
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