Skip to content

Commit 7ee266c

Browse files
author
Anisa Kusumadewi
committed
Merge branch 'delete_pictures_in_comment_for_provider_api' into 'testing4'
add sql queries for See merge request moodle_l2p/moodle-mod_pdfannotator!219
2 parents 7be2101 + 1c9732c commit 7ee266c

File tree

2 files changed

+263
-9
lines changed

2 files changed

+263
-9
lines changed

classes/privacy/provider.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828
defined('MOODLE_INTERNAL') || die();
2929

3030
use \core_privacy\local\request\approved_contextlist;
31-
use \core_privacy\local\request\deletion_criteria;
3231
use \core_privacy\local\request\writer;
33-
use \core_privacy\local\request\helper as request_helper;
3432
use \core_privacy\local\metadata\collection;
35-
use \core_privacy\local\request\transform;
33+
use core_privacy\local\request\approved_userlist;
34+
use core_privacy\local\request\userlist;
3635

3736
/**
3837
* Description of provider
@@ -329,27 +328,41 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
329328
$instanceid = $DB->get_field('course_modules', 'instance', ['id' => $context->instanceid], MUST_EXIST);
330329

331330
// 1. Delete all reports this user made in this annotator.
332-
$DB->delete_records('pdfannotator_reports', ['pdfannotatorid' => $instanceid, 'userid' => $userid]);
331+
$DB->delete_records(
332+
'pdfannotator_reports',
333+
['pdfannotatorid' => $instanceid, 'userid' => $userid]
334+
);
333335

334336
// 2. Delete all votes this user made in this annotator.
335-
$sql = "SELECT v.id FROM {pdfannotator_votes} v WHERE v.userid = ? AND v.commentid IN (SELECT c.id FROM {pdfannotator_comments} c WHERE c.pdfannotatorid = ?)";
337+
$sql = "SELECT v.id
338+
FROM {pdfannotator_votes} v
339+
WHERE v.userid = ? AND v.commentid IN
340+
(SELECT c.id
341+
FROM {pdfannotator_comments} c
342+
WHERE c.pdfannotatorid = ?)";
336343
$votes = $DB->get_records_sql($sql , array($userid, $instanceid));
337344
foreach ($votes as $vote) {
338345
$DB->delete_records('pdfannotator_votes', array("id" => $vote->id));
339346
}
340347

341348
// 3. Delete all subscriptions this user made in this annotator.
342-
$sql = "SELECT s.id FROM {pdfannotator_subscriptions} s WHERE s.userid = ? AND s.annotationid IN "
343-
. "(SELECT a.id FROM {pdfannotator_annotations} a WHERE a.pdfannotatorid = ?)";
349+
$sql = "SELECT s.id
350+
FROM {pdfannotator_subscriptions} s
351+
WHERE s.userid = ? AND s.annotationid IN
352+
(SELECT a.id
353+
FROM {pdfannotator_annotations} a
354+
WHERE a.pdfannotatorid = ?)";
344355
$subscriptions = $DB->get_records_sql($sql, array($userid, $instanceid));
345356
foreach ($subscriptions as $subscription) {
346357
$DB->delete_records('pdfannotator_subscriptions', array("id" => $subscription->id));
347358
}
348359

349360
// 4. Select all comments this user made in this annotator.
350-
$comments = $DB->get_records_sql("SELECT c.* FROM {pdfannotator_comments} c WHERE c.pdfannotatorid = ? AND c.userid = ?", array($instanceid, $userid));
361+
$sql = "SELECT c.*
362+
FROM {pdfannotator_comments} c
363+
WHERE c.pdfannotatorid = ? AND c.userid = ?";
364+
$comments = $DB->get_records_sql($sql, array($instanceid, $userid));
351365
foreach ($comments as $comment) {
352-
353366
// Delete question comments, their underlying annotation as well as all answers and subscriptions.
354367
if ($comment->isquestion) {
355368
self::delete_annotation($comment->annotationid);
@@ -389,6 +402,8 @@ public static function delete_annotation($annotationid) {
389402
// 1.2 Delete any votes for these comments.
390403
$DB->delete_records('pdfannotator_votes', array("commentid" => $comment->id));
391404

405+
// Delete any pictures of the comment.
406+
$DB->delete_records('files', array("component" => "mod_pdfannotator", "filearea" => "post", "itemid" => $comment->id));
392407
}
393408

394409
// 1.3 Now delete all comments.
@@ -551,5 +566,13 @@ public static function delete_data_for_users(approved_userlist $userlist) {
551566
$DB->delete_records_select('pdfannotator_annotations', $sql, $params);
552567
$DB->delete_records_select('pdfannotator_reports', $sql, $params);
553568
$DB->delete_records_select('pdfannotator_comments', $sql, $params);
569+
570+
// Delete pictures in comments.
571+
$DB->execute("DELETE FORM {files} imgs
572+
WHERE imgs.component = 'mod_pdfannotator'
573+
AND imgs.filearea = 'post'
574+
AND imgs.userid {$userinsql}
575+
AND imgs.itemid {$commentinsql}",
576+
array_merge($userinparams, $commentinparams));
554577
}
555578
}

tests/privacy/provider_test.php

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace mod_pdfannotator\privacy;
18+
19+
use mod_pdfannotator\privacy\provider;
20+
use core_privacy\local\request\approved_contextlist;
21+
use core_privacy\local\request\approved_userlist;
22+
use core_privacy\tests\provider_testcase;
23+
use stdClass;
24+
25+
defined('MOODLE_INTERNAL') || die();
26+
27+
require_once(__DIR__ . '/../fixtures/test_indicator_max.php');
28+
require_once(__DIR__ . '/../fixtures/test_indicator_min.php');
29+
require_once(__DIR__ . '/../fixtures/test_target_site_users.php');
30+
require_once(__DIR__ . '/../fixtures/test_target_course_users.php');
31+
32+
/**
33+
* Unit tests for privacy.
34+
*
35+
* @package mod_pdfannotator
36+
* @copyright IT Center RWTH Aachen University
37+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38+
*/
39+
class provider_test extends provider_testcase {
40+
41+
protected $course;
42+
protected $cmcontext;
43+
protected $user;
44+
protected $pdffile;
45+
protected $annotations;
46+
protected $questions;
47+
protected $answers;
48+
49+
public function setUp(): void {
50+
global $DB;
51+
52+
$this->resetAfterTest();
53+
54+
// Create a course.
55+
$generator = $this->getDataGenerator();
56+
$this->course = $generator->create_course();
57+
58+
// Create a pdfannotator module.
59+
$cm = $generator->create_module('pdfannotator', ['course' => $this->course->id]);
60+
$this->cmcontext = \context_module::instance($cm->cmid);
61+
62+
// Create a user.
63+
$this->user = $generator->create_user();
64+
65+
// Create a pdf object.
66+
$pdfobject = new stdClass();
67+
$pdfobject->course = $this->course->id;
68+
$pdfobject->name = "PDF_Provider_Test";
69+
$pdfobject->intro = "";
70+
$pdfobject->introformat = 1;
71+
$pdfobject->usevotes = 1;
72+
$pdfobject->useprint = 1;
73+
$pdfobject->useprintcomments = 1;
74+
$pdfobject->use_studenttextbox = 0;
75+
$pdfobject->use_studentdrawing = 0;
76+
$pdfobject->useprivatecomments = 0;
77+
$pdfobject->useprotectedcomments = 0;
78+
$pdfobject->timecreated = time();
79+
$pdfobject->timemodified = time();
80+
$pdfobject->id = $DB->insert_record('pdfannotator', $pdfobject);
81+
$this->pdffile = $pdfobject;
82+
83+
// Create an (pin as a test) annotation to the test pdf.
84+
$annotationobj = new stdClass();
85+
$annotationobj->pdfannotatorid = $this->pdffile->id;
86+
$annotationobj->page = 1;
87+
$annotationobj->userid = $this->user->id;
88+
$pinannotation = $DB->get_record('pdfannotator_annotationtypes', ['name' => 'pin']);
89+
$annotationobj->annotationtypeid = $pinannotation->id;
90+
$annotationobj->data = json_encode(['x' => 365, 'y' => 166]);
91+
$annotationobj->timecreated = time();
92+
$annotationobj->timemodified = time();
93+
$annotationobj->id = $DB->insert_record('pdfannotator_annotations', $annotationobj);
94+
$this->annotations[] = $annotationobj;
95+
96+
// Create a question for the pin annotation.
97+
$question = new stdClass();
98+
$question->pdfannotatorid = $this->pdffile->id;
99+
$question->annotationid = $this->annotations[0]->id;
100+
$question->userid = $this->user->id;
101+
$question->timecreated = time();
102+
$question->timemodified = time();
103+
$question->isquestion = 1;
104+
$question->isdeleted = 0;
105+
$question->ishidden = 0;
106+
$question->solved = 0;
107+
$question->id = $DB->insert_record('pdfannotator_comments', $question);
108+
$this->questions[] = $question;
109+
110+
// Create a comment for the question above.
111+
$answer = new stdClass();
112+
$answer->pdfannotatorid = $this->pdffile->id;
113+
$answer->annotationid = $this->annotations[0]->id;
114+
$answer->userid = $this->user->id;
115+
$answer->timecreated = time();
116+
$answer->timemodified = time();
117+
$answer->isquestion = 0;
118+
$answer->isdeleted = 0;
119+
$answer->ishidden = 0;
120+
$answer->solved = 0;
121+
$answer->id = $DB->insert_record('pdfannotator_comments', $answer);
122+
$this->answers[] = $answer;
123+
}
124+
125+
public function test_delete_data_for_users() {
126+
global $DB;
127+
128+
$this->resetAfterTest();
129+
130+
$component = 'mod_pdfannotator';
131+
132+
$usercontext1 = \context_user::instance($this->user->id);
133+
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
134+
provider::get_users_in_context($userlist1);
135+
$this->assertCount(1, $userlist1);
136+
137+
// Create a comment for the question above.
138+
$answer = new stdClass();
139+
$answer->pdfannotatorid = $this->pdffile->id;
140+
$answer->annotationid = $this->annotations[0]->id;
141+
$answer->userid = $this->user->id;
142+
$answer->timecreated = time();
143+
$answer->timemodified = time();
144+
$answer->isquestion = 0;
145+
$answer->isdeleted = 0;
146+
$answer->ishidden = 0;
147+
$answer->solved = 0;
148+
$answer->id = $DB->insert_record('pdfannotator_comments', $answer);
149+
$this->answers[] = $answer;
150+
151+
// Report the first comment from the setUp().
152+
$reportobj = new stdClass();
153+
$reportobj->commentid = $this->answers[0]->id;
154+
$reportobj->courseid = $this->course->id;
155+
$reportobj->pdfannotatorid = $this->pdffile->id;
156+
$reportobj->userid = $this->user->id;
157+
$reportobj->timecreated = time();
158+
$reportobj->seen = 0;
159+
$reportobj->id = $DB->insert_record('pdfannotator_reports', $reportobj);
160+
161+
// Vote the second comment in the quetions[0].
162+
$voteobj = new stdClass();
163+
$voteobj->commentid = $this->answers[1]->id;
164+
$voteobj->userid = $this->user->id;
165+
$voteobj->vote = 1;
166+
$voteobj->id = $DB->insert_record('pdfannotator_votes', $voteobj);
167+
168+
// Subscribe the questions[0].
169+
$subscriptionsobj = new stdClass();
170+
$subscriptionsobj->annotationid = $this->questions[0]->id;
171+
$subscriptionsobj->userid = $this->user->id;
172+
$subscriptionsobj->id = $DB->insert_record('pdfannotator_subscriptions', $voteobj);
173+
174+
// Perform delete_data_for_users.
175+
$systemcontext = \context_system::instance();
176+
$component = 'mod_pdfannotator';
177+
$userlist = new approved_userlist($systemcontext, $component, $this->user->id);
178+
// Delete using delete_data_for_user.
179+
provider::delete_data_for_users($userlist);
180+
181+
list($userinsql, $userinparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
182+
183+
$annotatorid = $this->pdffile->id;
184+
185+
// Combine instance + user sql.
186+
$params = array_merge(['pdfannotatorid' => $annotatorid], $userinparams);
187+
$sql = "pdfannotatorid = :pdfannotatorid AND userid {$userinsql}";
188+
189+
// Count subscriptions.
190+
$annotations = $DB->get_records('pdfannotator_annotations', ['pdfannotatorid' => $annotatorid]);
191+
$annotationids = array_column($annotations, 'id');
192+
list($subinsql, $subinparams) = $DB->get_in_or_equal($annotationids, SQL_PARAMS_NAMED);
193+
194+
$count_subs = $DB->count_records_sql("SELECT *
195+
FROM {pdfannotator_subscriptions} sub
196+
WHERE sub.userid {$userinsql}
197+
AND sub.annotationid {$subinsql}",
198+
array_merge($userinparams, $subinparams));
199+
$this->assertCount(0, $count_subs);
200+
201+
// Count votes.
202+
$comments = $DB->get_records('pdfannotator_comments', ['pdfannotatorid' => $annotatorid]);
203+
$commentsids = array_column($comments, 'id');
204+
list($commentinsql, $commentinparams) = $DB->get_in_or_equal($commentsids, SQL_PARAMS_NAMED);
205+
206+
$count_votes = $DB->count_records_sql("SELECT *
207+
FORM {pdfannotator_votes} votes
208+
WHERE vote.userid {$userinsql}
209+
AND vote.commentid {$commentinsql}",
210+
array_merge($userinparams, $commentinparams));
211+
$this->assertCount(0, $count_votes);
212+
213+
// Count annotations, reports, and comments.
214+
$count_annotations = count($DB->get_records_select('pdfannotator_annotations', $sql, $params));
215+
$this->assertCount(0, $count_annotations);
216+
$count_reports = count($DB->get_records_select('pdfannotator_reports', $sql, $params));
217+
$this->assertCount(0, $count_reports);
218+
$count_comments = count($DB->get_records_select('pdfannotator_comments', $sql, $params));
219+
$this->assertCount(0, $count_comments);
220+
221+
// Count pictures in comments.
222+
$count_pics = $DB->count_records_sql("SELECT *
223+
FORM {files} imgs
224+
WHERE imgs.component = 'mod_pdfannotator'
225+
AND imgs.filearea = 'post'
226+
AND imgs.userid {$userinsql}
227+
AND imgs.itemid {$commentinsql}",
228+
array_merge($userinparams, $commentinparams));
229+
$this->assertCount(0, $count_pics);
230+
}
231+
}

0 commit comments

Comments
 (0)