From b7a9b6f8720c1298575dc33e6e766666ed0a4268 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 27 Sep 2018 11:25:38 +1000 Subject: [PATCH] MDL-27520 assignfeedback_comments: updated privacy provider --- .../comments/classes/privacy/provider.php | 39 ++++++++-- .../lang/en/assignfeedback_comments.php | 1 + .../feedback/comments/tests/privacy_test.php | 73 ++++++++++++++++++- 3 files changed, 104 insertions(+), 9 deletions(-) diff --git a/mod/assign/feedback/comments/classes/privacy/provider.php b/mod/assign/feedback/comments/classes/privacy/provider.php index ac9aa7fa58d..9302526749a 100644 --- a/mod/assign/feedback/comments/classes/privacy/provider.php +++ b/mod/assign/feedback/comments/classes/privacy/provider.php @@ -58,6 +58,8 @@ class provider implements metadataprovider, assignfeedback_provider { 'commenttext' => 'privacy:metadata:commentpurpose' ]; $collection->add_database_table('assignfeedback_comments', $data, 'privacy:metadata:tablesummary'); + $collection->link_subsystem('core_files', 'privacy:metadata:filepurpose'); + return $collection; } @@ -91,13 +93,29 @@ class provider implements metadataprovider, assignfeedback_provider { // Get that comment information and jam it into that exporter. $assign = $exportdata->get_assign(); $plugin = $assign->get_plugin_by_type('assignfeedback', 'comments'); - $comments = $plugin->get_feedback_comments($exportdata->get_pluginobject()->id); + $gradeid = $exportdata->get_pluginobject()->id; + $comments = $plugin->get_feedback_comments($gradeid); if ($comments && !empty($comments->commenttext)) { - $data = (object)['commenttext' => format_text($comments->commenttext, $comments->commentformat, - ['context' => $exportdata->get_context()])]; - writer::with_context($exportdata->get_context()) - ->export_data(array_merge($exportdata->get_subcontext(), - [get_string('privacy:commentpath', 'assignfeedback_comments')]), $data); + $comments->commenttext = writer::with_context($assign->get_context())->rewrite_pluginfile_urls( + [], + ASSIGNFEEDBACK_COMMENTS_COMPONENT, + ASSIGNFEEDBACK_COMMENTS_FILEAREA, + $gradeid, + $comments->commenttext + ); + + $currentpath = array_merge( + $exportdata->get_subcontext(), + [get_string('privacy:commentpath', 'assignfeedback_comments')] + ); + $data = (object) + [ + 'commenttext' => format_text($comments->commenttext, $comments->commentformat, + ['context' => $exportdata->get_context()]) + ]; + writer::with_context($exportdata->get_context())->export_data($currentpath, $data); + writer::with_context($exportdata->get_context())->export_area_files($currentpath, + ASSIGNFEEDBACK_COMMENTS_COMPONENT, ASSIGNFEEDBACK_COMMENTS_FILEAREA, $gradeid); } } @@ -108,6 +126,10 @@ class provider implements metadataprovider, assignfeedback_provider { */ public static function delete_feedback_for_context(assign_plugin_request_data $requestdata) { $assign = $requestdata->get_assign(); + $fs = get_file_storage(); + $fs->delete_area_files($requestdata->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT, + ASSIGNFEEDBACK_COMMENTS_FILEAREA); + $plugin = $assign->get_plugin_by_type('assignfeedback', 'comments'); $plugin->delete_instance(); } @@ -119,6 +141,11 @@ class provider implements metadataprovider, assignfeedback_provider { */ public static function delete_feedback_for_grade(assign_plugin_request_data $requestdata) { global $DB; + + $fs = new \file_storage(); + $fs->delete_area_files($requestdata->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT, + ASSIGNFEEDBACK_COMMENTS_FILEAREA, $requestdata->get_pluginobject()->id); + $DB->delete_records('assignfeedback_comments', ['assignment' => $requestdata->get_assign()->get_instance()->id, 'grade' => $requestdata->get_pluginobject()->id]); } diff --git a/mod/assign/feedback/comments/lang/en/assignfeedback_comments.php b/mod/assign/feedback/comments/lang/en/assignfeedback_comments.php index b8c7551c7ee..ee4c540bdb6 100644 --- a/mod/assign/feedback/comments/lang/en/assignfeedback_comments.php +++ b/mod/assign/feedback/comments/lang/en/assignfeedback_comments.php @@ -30,6 +30,7 @@ $string['pluginname'] = 'Feedback comments'; $string['privacy:commentpath'] = 'Feedback comments'; $string['privacy:metadata:assignmentid'] = 'Assignment ID'; $string['privacy:metadata:commentpurpose'] = 'The comment text.'; +$string['privacy:metadata:filepurpose'] = 'Feedback files from the teacher for the student.'; $string['privacy:metadata:gradepurpose'] = 'The grade ID associated with the comment.'; $string['privacy:metadata:tablesummary'] = 'This stores comments made by the graders as feedback for the student on their submission.'; $string['commentinline'] = 'Comment inline'; diff --git a/mod/assign/feedback/comments/tests/privacy_test.php b/mod/assign/feedback/comments/tests/privacy_test.php index 36494523ac4..276b9cf6423 100644 --- a/mod/assign/feedback/comments/tests/privacy_test.php +++ b/mod/assign/feedback/comments/tests/privacy_test.php @@ -47,6 +47,8 @@ class assignfeedback_comments_privacy_testcase extends \mod_assign\tests\mod_ass * @return array Feedback plugin object and the grade object. */ protected function create_feedback($assign, $student, $teacher, $submissiontext, $feedbacktext) { + global $CFG; + $submission = new \stdClass(); $submission->assignment = $assign->get_instance()->id; $submission->userid = $student->id; @@ -62,11 +64,33 @@ class assignfeedback_comments_privacy_testcase extends \mod_assign\tests\mod_ass $this->setUser($teacher); + $context = context_user::instance($teacher->id); + + $draftitemid = file_get_unused_draft_itemid(); + file_prepare_draft_area($draftitemid, $context->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT, + ASSIGNFEEDBACK_COMMENTS_FILEAREA, $grade->id); + + $dummy = array( + 'contextid' => $context->id, + 'component' => 'user', + 'filearea' => 'draft', + 'itemid' => $draftitemid, + 'filepath' => '/', + 'filename' => 'feedback1.txt' + ); + + $fs = get_file_storage(); + $fs->create_file_from_string($dummy, $feedbacktext); + + $feedbacktext = $feedbacktext . + " assignfeedbackcomments_editor = [ 'text' => $feedbacktext, - 'format' => 1 + 'format' => FORMAT_HTML, + 'itemid' => $draftitemid ]; $plugin->save($grade, $feedbackdata); @@ -109,12 +133,24 @@ class assignfeedback_comments_privacy_testcase extends \mod_assign\tests\mod_ass // The student should be able to see the teachers feedback. $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user1); \assignfeedback_comments\privacy\provider::export_feedback_user_data($exportdata); - $this->assertEquals($feedbacktext, $writer->get_data(['Feedback comments'])->commenttext); + $this->assertContains($feedbacktext, $writer->get_data(['Feedback comments'])->commenttext); + + $filespath = []; + $filespath[] = 'Feedback comments'; + $feedbackfile = $writer->get_files($filespath)['feedback1.txt']; + + $this->assertInstanceOf('stored_file', $feedbackfile); + $this->assertEquals('feedback1.txt', $feedbackfile->get_filename()); // The teacher should also be able to see the feedback that they provided. $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user2); \assignfeedback_comments\privacy\provider::export_feedback_user_data($exportdata); - $this->assertEquals($feedbacktext, $writer->get_data(['Feedback comments'])->commenttext); + $this->assertContains($feedbacktext, $writer->get_data(['Feedback comments'])->commenttext); + + $feedbackfile = $writer->get_files($filespath)['feedback1.txt']; + + $this->assertInstanceOf('stored_file', $feedbackfile); + $this->assertEquals('feedback1.txt', $feedbackfile->get_filename()); } /** @@ -147,6 +183,12 @@ class assignfeedback_comments_privacy_testcase extends \mod_assign\tests\mod_ass $feedbackcomments = $plugin1->get_feedback_comments($grade2->id); $this->assertNotEmpty($feedbackcomments); + $fs = new file_storage(); + $files = $fs->get_area_files($assign->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT, + ASSIGNFEEDBACK_COMMENTS_FILEAREA); + // 4 including directories. + $this->assertEquals(4, count($files)); + // Delete all comments for this context. $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign); assignfeedback_comments\privacy\provider::delete_feedback_for_context($requestdata); @@ -156,6 +198,12 @@ class assignfeedback_comments_privacy_testcase extends \mod_assign\tests\mod_ass $this->assertEmpty($feedbackcomments); $feedbackcomments = $plugin1->get_feedback_comments($grade2->id); $this->assertEmpty($feedbackcomments); + + $fs = new file_storage(); + $files = $fs->get_area_files($assign->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT, + ASSIGNFEEDBACK_COMMENTS_FILEAREA); + $this->assertEquals(0, count($files)); + } /** @@ -188,6 +236,12 @@ class assignfeedback_comments_privacy_testcase extends \mod_assign\tests\mod_ass $feedbackcomments = $plugin1->get_feedback_comments($grade2->id); $this->assertNotEmpty($feedbackcomments); + $fs = new file_storage(); + $files = $fs->get_area_files($assign->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT, + ASSIGNFEEDBACK_COMMENTS_FILEAREA); + // 4 including directories. + $this->assertEquals(4, count($files)); + // Delete all comments for this grade object. $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade1, [], $user1); assignfeedback_comments\privacy\provider::delete_feedback_for_grade($requestdata); @@ -199,5 +253,18 @@ class assignfeedback_comments_privacy_testcase extends \mod_assign\tests\mod_ass // These comments should not. $feedbackcomments = $plugin1->get_feedback_comments($grade2->id); $this->assertNotEmpty($feedbackcomments); + + $fs = new file_storage(); + $files = $fs->get_area_files($assign->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT, + ASSIGNFEEDBACK_COMMENTS_FILEAREA); + // 2 files that were not deleted. + $this->assertEquals(2, count($files)); + + array_shift($files); + $file = array_shift($files); + + $this->assertInstanceOf('stored_file', $file); + $this->assertEquals('feedback1.txt', $file->get_filename()); + $this->assertEquals($grade2->id, $file->get_itemid()); } }