Skip to content

Commit

Permalink
MDL-62426 enrol_flatfile: make provider a plugin provider
Browse files Browse the repository at this point in the history
The enrol_flatfile table can contain userdata relating to pending
enrolments, so this should be exported and deleted as necessary.
  • Loading branch information
snake committed May 16, 2018
1 parent dc71a8b commit 37e1069
Show file tree
Hide file tree
Showing 3 changed files with 396 additions and 8 deletions.
155 changes: 148 additions & 7 deletions enrol/flatfile/classes/privacy/provider.php
Expand Up @@ -21,21 +21,162 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_flatfile\privacy;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\context;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\writer;
use core_privacy\local\request\transform;

defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for enrol_flatfile implementing null_provider.
*
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\plugin\provider {

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
* Returns meta data about this system.
*
* @return string
* @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_reason() : string {
return 'privacy:metadata';
public static function get_metadata(collection $collection) : collection {
return $collection->add_database_table('enrol_flatfile', [
'action' => 'privacy:metadata:enrol_flatfile:action',
'roleid' => 'privacy:metadata:enrol_flatfile:roleid',
'userid' => 'privacy:metadata:enrol_flatfile:userid',
'courseid' => 'privacy:metadata:enrol_flatfile:courseid',
'timestart' => 'privacy:metadata:enrol_flatfile:timestart',
'timeend' => 'privacy:metadata:enrol_flatfile:timeend',
'timemodified' => 'privacy:metadata:enrol_flatfile:timemodified'
], 'privacy:metadata:enrol_flatfile');
}
}

/**
* 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 {
$sql = "SELECT c.id
FROM {enrol_flatfile} ef
JOIN {context} c ON c.contextlevel = ? AND c.instanceid = ef.courseid
WHERE ef.userid = ?";
$params = [CONTEXT_COURSE, $userid];

$contextlist = new contextlist();
$contextlist->set_component('enrol_flatfile');
return $contextlist->add_from_sql($sql, $params);
}

/**
* 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;

// Ensure all contexts are CONTEXT_COURSE.
$contexts = static::validate_contextlist_contexts($contextlist);
if (empty($contexts)) {
return;
}

// Get the context instance ids from the contexts. These are the course ids..
$contextinstanceids = array_map(function($context) {
return $context->instanceid;
}, $contexts);
$userid = $contextlist->get_user()->id;

// Now, we just need to fetch and format all entries corresponding to the contextids provided.
$sql = "SELECT ef.action, r.shortname, ef.courseid, ef.timestart, ef.timeend, ef.timemodified
FROM {enrol_flatfile} ef
JOIN {context} c ON c.contextlevel = :contextlevel AND c.instanceid = ef.courseid
JOIN {role} r ON r.id = ef.roleid
WHERE ef.userid = :userid";
$params = ['contextlevel' => CONTEXT_COURSE, 'userid' => $userid];
list($insql, $inparams) = $DB->get_in_or_equal($contextinstanceids, SQL_PARAMS_NAMED);
$sql .= " AND ef.courseid $insql";
$params = array_merge($params, $inparams);

$futureenrolments = $DB->get_recordset_sql($sql, $params);
$enrolmentdata = [];
foreach ($futureenrolments as $futureenrolment) {
// It's possible to have more than one future enrolment per course.
$futureenrolment->timestart = transform::datetime($futureenrolment->timestart);
$futureenrolment->timeend = transform::datetime($futureenrolment->timeend);
$futureenrolment->timemodified = transform::datetime($futureenrolment->timemodified);
$enrolmentdata[$futureenrolment->courseid][] = $futureenrolment;
}
$futureenrolments->close();

// And finally, write out the data to the relevant course contexts.
$subcontext = [get_string('flatfileenrolments', 'enrol_flatfile')];
foreach ($enrolmentdata as $courseid => $enrolments) {
$data = (object) [
'pendingenrolments' => $enrolments,
];
writer::with_context(\context_course::instance($courseid))->export_data($subcontext, $data);
}
}

/**
* 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) {
if ($context->contextlevel != CONTEXT_COURSE) {
return;
}
global $DB;
$DB->delete_records('enrol_flatfile', ['courseid' => $context->instanceid]);
}

/**
* 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) {
// Only delete data from contexts which are at the COURSE_MODULE contextlevel.
$contexts = self::validate_contextlist_contexts($contextlist);
if (empty($contexts)) {
return;
}

// Get the course ids based on the provided contexts.
$contextinstanceids = array_map(function($context) {
return $context->instanceid;
}, $contextlist->get_contexts());

global $DB;
$user = $contextlist->get_user();
list($insql, $inparams) = $DB->get_in_or_equal($contextinstanceids, SQL_PARAMS_NAMED);
$params = array_merge(['userid' => $user->id], $inparams);
$sql = "userid = :userid AND courseid $insql";
$DB->delete_records_select('enrol_flatfile', $sql, $params);
}

/**
* Simple sanity check on the contextlist contexts, making sure they're of CONTEXT_COURSE contextlevel.
*
* @param approved_contextlist $contextlist
* @return array the array of contexts filtered to only include those of CONTEXT_COURSE contextlevel.
*/
protected static function validate_contextlist_contexts(approved_contextlist $contextlist) {
return array_reduce($contextlist->get_contexts(), function($carry, $context) {
if ($context->contextlevel == CONTEXT_COURSE) {
$carry[] = $context;
}
return $carry;
}, []);
}
}
10 changes: 9 additions & 1 deletion enrol/flatfile/lang/en/enrol_flatfile.php
Expand Up @@ -29,6 +29,7 @@
$string['filelockedmailsubject'] = 'Important error: Enrolment file';
$string['flatfile:manage'] = 'Manage user enrolments manually';
$string['flatfile:unenrol'] = 'Unenrol users from the course manually';
$string['flatfileenrolments'] = 'Flat file (CSV) enrolments';
$string['flatfilesync'] = 'Flat file enrolment sync';
$string['location'] = 'File location';
$string['location_desc'] = 'Specify full path to the enrolment file. The file is automatically deleted after processing.';
Expand Down Expand Up @@ -61,4 +62,11 @@
del, student, 17, CF101
add, student, 21, CF101, 1091115000, 1091215000
</pre>';
$string['privacy:metadata'] = 'The Flat file (CSV) enrolment plugin does not store any personal data.';
$string['privacy:metadata:enrol_flatfile'] = 'The Flat file (CSV) enrolment plugin may store personal data relating to future enrolments in the enrol_flatfile table.';
$string['privacy:metadata:enrol_flatfile:action'] = 'The enrolment action expected at the given date.';
$string['privacy:metadata:enrol_flatfile:courseid'] = 'The courseid to which the enrolment relates.';
$string['privacy:metadata:enrol_flatfile:roleid'] = 'The id of the role to be assigned or revoked.';
$string['privacy:metadata:enrol_flatfile:timestart'] = 'The time at which the enrolment change starts.';
$string['privacy:metadata:enrol_flatfile:timeend'] = 'The time at which the enrolment change ends.';
$string['privacy:metadata:enrol_flatfile:timemodified'] = 'The modification time of this enrolment change.';
$string['privacy:metadata:enrol_flatfile:userid'] = 'The id of the user to which the role assignment relates.';

0 comments on commit 37e1069

Please sign in to comment.