Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catalyst 401 stable #75

Open
wants to merge 3 commits into
base: MOODLE_401_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
202 changes: 202 additions & 0 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?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_grade_me.
*
* @package block_grade_me
* @copyright 2019 Nathan Nguyen <nathannguyen@catalyst-net.au>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_grade_me\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\approved_userlist;
use core_privacy\local\request\userlist;

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

/**
* The block_grade_me does not store any data.
*
*/
class provider implements
// This plugin has data.
\core_privacy\local\metadata\provider,

// This plugin is capable of determining which users have data within it.
\core_privacy\local\request\core_userlist_provider,

// This plugin currently implements the original plugin\provider interface.
\core_privacy\local\request\plugin\provider {

/**
* 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 contextlist();

$params = [
'contextuser' => CONTEXT_USER,
'userid' => $userid
];

$sql = "SELECT c.id
FROM {block_grade_me_quiz_ngrade} gme
JOIN {context} c ON c.instanceid = gme.userid AND c.contextlevel = :contextuser
WHERE gme.userid = :userid
GROUP BY c.id";

$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;

$contexts = $contextlist->get_contexts();
if (count($contexts) == 0) {
return;
}
$context = reset($contexts);

if ($context->contextlevel !== CONTEXT_USER) {
return;
}
$userid = $context->instanceid;

$params = [
'userid' => $userid
];

$sql = "SELECT *
FROM {block_grade_me_quiz_ngrade} gme
WHERE gme.userid = :userid";

$grademe = $DB->get_records_sql($sql, $params);

$data = (object) [
'grade_me' => $grademe,
];

$subcontext = [
get_string('pluginname', 'block_grade_me'),
get_string('privacydata', 'block_grade_me')
];

writer::with_context($context)->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) {
global $DB;

if ($context->contextlevel !== CONTEXT_USER) {
return;
}
$userid = $context->instanceid;

$DB->delete_records('block_grade_me_quiz_ngrade', ['userid' => $userid]);
}

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

$contexts = $contextlist->get_contexts();
if (count($contexts) == 0) {
return;
}
$context = reset($contexts);

if ($context->contextlevel !== CONTEXT_USER) {
return;
}
$userid = $context->instanceid;

$DB->delete_records('block_grade_me_quiz_ngrade', ['userid' => $userid]);
}

/**
* 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('block_grade_me_quiz_ngrade', [
'userid' => 'privacy:metadata:block_grade_me_quiz_ngrade:userid',
'quizid' => 'privacy:metadata:block_grade_me_quiz_ngrade:quizid',
'questionattemptstepid' => 'privacy:metadata:block_grade_me_quiz_ngrade:questionattemptstepid',
'courseid' => 'privacy:metadata:block_grade_me_quiz_ngrade:courseid',
'attemptid' => 'privacy:metadata:block_grade_me_quiz_ngrade:attemptid',
], 'privacy:metadata:block_grade_me_quiz_ngrade');

return $collection;
}

/**
* 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_course) {
return;
}

$sql = "SELECT * FROM {block_grade_me_quiz_ngrade}";
$userlist->add_from_sql('userid', $sql, ['courseid' => $context->instanceid]);
}

/**
* Delete multiple users within a single context.
*
* @param \core_privacy\local\request\approved_userlist $userlist
*/
public static function delete_data_for_users(approved_userlist $userlist) {
$users = $userlist->get_users();
foreach ($users as $user) {
// Create a contextlist with only system context.
$contextlist = new approved_contextlist($user, 'block_grade_me', [\context_user::instance($user->id)->id]);
self::delete_data_for_user($contextlist);
}
}

}
8 changes: 8 additions & 0 deletions lang/en/block_grade_me.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@

$string['quiz_update_ngrade_complete'] = 'Update complete';
$string['quiz_update_ngrade_success'] = 'Quiz attempt list successfully updated, currently there is {$a} questions needing grading.';

$string['privacy:metadata:block_grade_me_quiz_ngrade'] = 'Caches information about quizes needing grades.';
$string['privacy:metadata:block_grade_me_quiz_ngrade:userid'] = 'User id';
$string['privacy:metadata:block_grade_me_quiz_ngrade:quizid'] = 'Quiz id';
$string['privacy:metadata:block_grade_me_quiz_ngrade:questionattemptstepid'] = 'Question Attempt';
$string['privacy:metadata:block_grade_me_quiz_ngrade:courseid'] = 'Course id';
$string['privacy:metadata:block_grade_me_quiz_ngrade:attemptid'] = 'Attempt id';
$string['privacydata'] = 'Grade me';
45 changes: 25 additions & 20 deletions tests/grade_me_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class block_grade_me_testcase extends advanced_testcase {
*/
protected function create_grade_me_data($file) {
// Read the datafile and get the table names.
$dataset = $this->createXMLDataSet(__DIR__ . '/fixtures/' . $file);
$names = array_flip($dataset->getTableNames());
$dataset = $this->dataset_from_files([__DIR__ . '/fixtures/' . $file]);
$datasetrows = $dataset->get_rows();
$names = array_keys($datasetrows);

// Generate Data.
$generator = $this->getDataGenerator();
Expand All @@ -64,12 +65,11 @@ protected function create_grade_me_data($file) {

$gradeables = array('assign', 'assignment', 'forum', 'glossary', 'quiz');
foreach ($gradeables as $gradeable) {
if (array_key_exists($gradeable, $names)) {
if (in_array($gradeable, $names)) {
$pgen = $generator->get_plugin_generator("mod_{$gradeable}");
$table = $dataset->getTable($gradeable);
$rows = $table->getRowCount();
for ($row = 0; $row < $rows; $row += 1) {
$fields = $table->getRow($row);
$gradeablerows = $datasetrows[$gradeable];
for ($row = 0; $row < count($gradeablerows); $row += 1) {
$fields = $gradeablerows[$row];
unset($fields['id']);
$fields['course'] = $courses[$fields['course']]->id;
$instance = $pgen->create_instance($fields);
Expand Down Expand Up @@ -151,7 +151,7 @@ protected function create_grade_me_data($file) {
foreach ($overrides as $field => $override) {
foreach ($override['tables'] as $tablename) {
// Skip tables that aren't in the dataset.
if (array_key_exists($tablename, $names)) {
if (in_array($tablename, $names)) {
if (!array_key_exists($tablename, $tables)) {
$tables[$tablename] = array($field => array());
}
Expand All @@ -162,26 +162,31 @@ protected function create_grade_me_data($file) {

// Perform the overrides.
foreach ($tables as $tablename => $translations) {
$table = $dataset->getTable($tablename);
$rows = $table->getRowCount();
foreach ($translations as $column => $values) {
foreach ($values as $value) {
$list = $value['list'];
$field = $value['field'];
for ($row = 0; $row < $rows; $row += 1) {
$index = $table->getValue($row, $column);
$tablerows = $datasetrows[$tablename];
for ($row = 0; $row < count($tablerows); $row += 1) {
$index = $tablerows[$row][$column];
if (isset(${$list}[$index])) {
$table->setValue($row, $column, ${$list}[$index]->$field);
$datasetrows[$tablename][$row][$column] = ${$list}[$index]->$field;
}
}
}
}
}

// Load the data.
$filtered = new \PHPUnit\DbUnit\DataSet\Filter($dataset);
$filtered->addExcludeTables($excludes);
$this->loadDataSet($filtered);
// Remove any empty tables (otherwise dataset_from_array breaks).
foreach (array_keys($datasetrows) as $tablename) {
if (empty($datasetrows[$tablename])) {
unset($datasetrows[$tablename]);
}
}

// Load back in the modified dataset and send to the db.
$finaldataset = $this->dataset_from_array($datasetrows);
$finaldataset->to_database();

// Return the generated users and courses because the tests often need them for result calculations.
return array($users, $courses, $plugins);
Expand Down Expand Up @@ -829,7 +834,7 @@ public function test_get_content_single_user($plugin, $expectedvalues) {

foreach ($expectedvalues as $expected) {
$match = str_replace('[user0]', $users[0]->id, $expected);
$this->assertRegExp($match, $content->text);
$this->assertMatchesRegularExpression($match, $content->text);
}
}

Expand Down Expand Up @@ -957,7 +962,7 @@ public function test_get_content_multiple_user($plugin, $expectedvalues) {
foreach ($expectedvalues as $expected) {
$match = str_replace('[user0]', $users[0]->id, $expected);
$match = str_replace('[user1]', $users[1]->id, $match);
$this->assertRegExp($match, $content->text);
$this->assertMatchesRegularExpression($match, $content->text);
}
}

Expand All @@ -982,7 +987,7 @@ public function test_tree_uses_correct_forum_discussion_id() {

$this->assertFalse(empty($gradeables), 'Expected results not found.');
$actual = block_grade_me_tree($gradeables);
$this->assertRegExp('/mod\/forum\/discuss.php\?d=100\#p1/', $actual);
$this->assertMatchesRegularExpression('/mod\/forum\/discuss.php\?d=100\#p1/', $actual);
}

/**
Expand Down