* Privacy Subsystem implementation for mod_choice.
*
* @package mod_choice
+ * @category privacy
* @copyright 2018 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_privacy\local\metadata\collection;
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\deletion_criteria;
use core_privacy\local\request\helper;
+use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
defined('MOODLE_INTERNAL') || die();
\core_privacy\local\metadata\provider,
// This plugin is a core_user_data_provider.
- \core_privacy\local\request\plugin\provider {
+ \core_privacy\local\request\plugin\provider,
+
+ // This plugin is capable of determining which users have data within it.
+ \core_privacy\local\request\core_userlist_provider {
/**
* Return the fields which contain personal data.
*
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_module) {
+ return;
+ }
+
+ // Fetch all choice answers.
+ $sql = "SELECT ca.userid
+ FROM {course_modules} cm
+ JOIN {modules} m ON m.id = cm.module AND m.name = :modname
+ JOIN {choice} ch ON ch.id = cm.instance
+ JOIN {choice_options} co ON co.choiceid = ch.id
+ JOIN {choice_answers} ca ON ca.optionid = co.id AND ca.choiceid = ch.id
+ WHERE cm.id = :cmid";
+
+ $params = [
+ 'cmid' => $context->instanceid,
+ 'modname' => 'choice',
+ ];
+
+ $userlist->add_from_sql('userid', $sql, $params);
+ }
+
/**
* Export personal data for the given approved_contextlist. User and context information is contained within the contextlist.
*
if (!$context instanceof \context_module) {
continue;
}
- $instanceid = $DB->get_field('course_modules', 'instance', ['id' => $context->instanceid], MUST_EXIST);
+ $instanceid = $DB->get_field('course_modules', 'instance', ['id' => $context->instanceid]);
+ if (!$instanceid) {
+ continue;
+ }
$DB->delete_records('choice_answers', ['choiceid' => $instanceid, 'userid' => $userid]);
}
}
+
+ /**
+ * 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_module) {
+ return;
+ }
+
+ $cm = get_coursemodule_from_id('choice', $context->instanceid);
+
+ if (!$cm) {
+ // Only choice module will be handled.
+ return;
+ }
+
+ $userids = $userlist->get_userids();
+ list($usersql, $userparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
+
+ $select = "choiceid = :choiceid AND userid $usersql";
+ $params = ['choiceid' => $cm->instance] + $userparams;
+ $DB->delete_records_select('choice_answers', $select, $params);
+ }
}
// And that it's the other student's response.
$this->assertEquals($otherstudent->id, $lastresponse->userid);
}
+
+ /**
+ * Test for provider::get_users_in_context().
+ */
+ public function test_get_users_in_context() {
+ $cm = get_coursemodule_from_instance('choice', $this->choice->id);
+ $cmcontext = context_module::instance($cm->id);
+
+ $userlist = new \core_privacy\local\request\userlist($cmcontext, 'mod_choice');
+ \mod_choice\privacy\provider::get_users_in_context($userlist);
+
+ $this->assertEquals(
+ [$this->student->id],
+ $userlist->get_userids()
+ );
+ }
+
+ /**
+ * Test for provider::get_users_in_context() with invalid context type.
+ */
+ public function test_get_users_in_context_invalid_context_type() {
+ $systemcontext = context_system::instance();
+
+ $userlist = new \core_privacy\local\request\userlist($systemcontext, 'mod_choice');
+ \mod_choice\privacy\provider::get_users_in_context($userlist);
+
+ $this->assertCount(0, $userlist->get_userids());
+ }
+
+ /**
+ * Test for provider::delete_data_for_users().
+ */
+ public function test_delete_data_for_users() {
+ global $DB;
+
+ $choice = $this->choice;
+ $generator = $this->getDataGenerator();
+ $cm1 = get_coursemodule_from_instance('choice', $this->choice->id);
+
+ // Create a second choice activity.
+ $options = ['Boracay', 'Camiguin', 'Bohol', 'Cebu', 'Coron'];
+ $params = [
+ 'course' => $this->course->id,
+ 'option' => $options,
+ 'name' => 'Which do you think is the best island in the Philippines?',
+ 'showpreview' => 0
+ ];
+ $plugingenerator = $generator->get_plugin_generator('mod_choice');
+ $choice2 = $plugingenerator->create_instance($params);
+ $plugingenerator->create_instance($params);
+ $cm2 = get_coursemodule_from_instance('choice', $choice2->id);
+
+ // Make a selection for the first student for the 2nd choice activity.
+ $choicewithoptions = choice_get_choice($choice2->id);
+ $optionids = array_keys($choicewithoptions->option);
+ choice_user_submit_response($optionids[2], $choice2, $this->student->id, $this->course, $cm2);
+
+ // Create 2 other students who will answer the first choice activity.
+ $otherstudent = $generator->create_and_enrol($this->course, 'student');
+ $anotherstudent = $generator->create_and_enrol($this->course, 'student');
+
+ $choicewithoptions = choice_get_choice($choice->id);
+ $optionids = array_keys($choicewithoptions->option);
+
+ choice_user_submit_response($optionids[1], $choice, $otherstudent->id, $this->course, $cm1);
+ choice_user_submit_response($optionids[1], $choice, $anotherstudent->id, $this->course, $cm1);
+
+ // Before deletion, we should have 3 responses in the first choice activity.
+ $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
+ $this->assertEquals(3, $count);
+
+ $context1 = context_module::instance($cm1->id);
+ $approveduserlist = new \core_privacy\local\request\approved_userlist($context1, 'choice',
+ [$this->student->id, $otherstudent->id]);
+ provider::delete_data_for_users($approveduserlist);
+
+ // After deletion, the choice answers of the 2 students provided above should have been deleted
+ // from the first choice activity. So there should only remain 1 answer which is for $anotherstudent.
+ $choiceanswers = $DB->get_records('choice_answers', ['choiceid' => $choice->id]);
+ $this->assertCount(1, $choiceanswers);
+ $lastresponse = reset($choiceanswers);
+ $this->assertEquals($anotherstudent->id, $lastresponse->userid);
+
+ // Confirm that the answer that was submitted in the other choice activity is intact.
+ $choiceanswers = $DB->get_records_select('choice_answers', 'choiceid <> ?', [$choice->id]);
+ $this->assertCount(1, $choiceanswers);
+ $lastresponse = reset($choiceanswers);
+ // And that it's for the choice2 activity.
+ $this->assertEquals($choice2->id, $lastresponse->choiceid);
+ }
}