first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,189 @@
@mod @mod_assign @assignfeedback @assignfeedback_editpdf @_file_upload
Feature: In an assignment, teacher can annotate PDF files during grading
In order to provide visual report on a graded PDF
As a teacher
I need to use the PDF editor
@javascript
Scenario: Submit a PDF file as a student and annotate the PDF as a teacher then overwrite the submission as a student
Given ghostscript is installed
And the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activity" exists:
| activity | assign |
| course | C1 |
| name | Test assignment name |
| assignfeedback_editpdf_enabled | 1 |
| assignfeedback_comments_enabled | 1 |
| assignsubmission_file_enabled | 1 |
| assignsubmission_file_maxfiles | 2 |
| assignsubmission_file_maxsizebytes | 102400 |
| maxfilessubmission | 2 |
| submissiondrafts | 0 |
And the following "mod_assign > submission" exists:
| assign | Test assignment name |
| user | student1 |
| file | mod/assign/feedback/editpdf/tests/fixtures/testgs.pdf |
When I am on the "Test assignment name" Activity page logged in as teacher1
And I follow "View all submissions"
And I click on "Grade" "link" in the "Submitted for grading" "table_row"
Then I should see "Page 1 of 1"
And I wait for the complete PDF to load
And I click on ".linebutton" "css_element"
And I draw on the pdf
And I press "Save changes"
And I should see "The changes to the grade and feedback were saved"
And I am on the "Test assignment name" Activity page logged in as student1
And I follow "View annotated PDF..."
Then I should see "Page 1 of 1"
And I click on ".closebutton" "css_element"
And I press "Edit submission"
And I upload "mod/assign/feedback/editpdf/tests/fixtures/submission.pdf" file to "File submissions" filemanager
And I press "Save changes"
And I follow "View annotated PDF..."
Then I should see "Page 1 of 1"
And I am on the "Test assignment name" Activity page logged in as teacher1
And I follow "View all submissions"
And I click on "Grade" "link" in the "Submitted for grading" "table_row"
Then I should see "Page 1 of 3"
And I wait for the complete PDF to load
And I click on ".linebutton" "css_element"
And I draw on the pdf
And I press "Save changes"
And I should see "The changes to the grade and feedback were saved"
And I am on the "Test assignment name" Activity page logged in as student1
And I follow "View annotated PDF..."
Then I should see "Page 1 of 3"
@javascript
Scenario: Submit a PDF file as a student and annotate the PDF as a teacher
Given ghostscript is installed
And the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activity" exists:
| activity | assign |
| course | C1 |
| name | Test assignment name |
| assignfeedback_editpdf_enabled | 1 |
| assignfeedback_comments_enabled | 1 |
| assignsubmission_file_enabled | 1 |
| assignsubmission_file_maxfiles | 2 |
| assignsubmission_file_maxsizebytes | 102400 |
| maxfilessubmission | 2 |
| submissiondrafts | 0 |
And the following "mod_assign > submission" exists:
| assign | Test assignment name |
| user | student1 |
| file | mod/assign/feedback/editpdf/tests/fixtures/submission.pdf, mod/assign/feedback/editpdf/tests/fixtures/testgs.pdf |
And I log in as "admin"
And I am on site homepage
And I navigate to "Plugins > Activity modules > Assignment > Feedback plugins > Annotate PDF" in site administration
And I upload "pix/help.png" file to "" filemanager
And I upload "pix/docs.png" file to "" filemanager
And I press "Save changes"
And I should see "Changes saved"
And I follow "Test ghostscript path"
And I should see "The ghostscript path appears to be OK"
And I am on site homepage
And I log out
When I am on the "Test assignment name" Activity page logged in as teacher1
And I follow "View all submissions"
And I click on "Grade" "link" in the "Submitted for grading" "table_row"
Then I should see "Page 1 of 3"
And I click on ".navigate-next-button" "css_element"
And I should see "Page 2 of 3"
And I click on ".stampbutton" "css_element"
And I click on ".linebutton" "css_element"
And I click on ".commentcolourbutton" "css_element"
And I click on "//img[@alt=\"Blue\"]/parent::button" "xpath_element"
And I wait until the page is ready
And I press "Save changes"
And I wait until the page is ready
And I should see "The changes to the grade and feedback were saved"
@javascript
Scenario: Submit a PDF file as a student in a team and annotate the PDF as a teacher
Given ghostscript is installed
And the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
| student3 | Student | 3 | student3@example.com |
| student4 | Student | 4 | student4@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
| student2 | C1 | student |
| student3 | C1 | student |
| student4 | C1 | student |
And the following "groups" exist:
| name | course | idnumber |
| G1 | C1 | G1 |
| G2 | C1 | G2 |
And the following "groupings" exist:
| name | course | idnumber |
| G1 | C1 | G1 |
And the following "group members" exist:
| user | group |
| student1 | G1 |
| student2 | G1 |
| student3 | G2 |
| student4 | G2 |
And the following "grouping groups" exist:
| grouping | group |
| G1 | G1 |
| G1 | G2 |
And the following "activity" exists:
| activity | assign |
| course | C1 |
| name | Test assignment name |
| assignfeedback_comments_enabled | 1 |
| assignfeedback_editpdf_enabled | 1 |
| assignsubmission_file_enabled | 1 |
| assignsubmission_file_maxfiles | 2 |
| assignsubmission_file_maxsizebytes | 102400 |
| maxfilessubmission | 2 |
| teamsubmission | 1 |
| grouping | G1 |
| submissiondrafts | 0 |
And the following "mod_assign > submission" exists:
| assign | Test assignment name |
| user | student1 |
| file | mod/assign/feedback/editpdf/tests/fixtures/submission.pdf |
And I am on the "Test assignment name" Activity page logged in as teacher1
And I follow "View all submissions"
And I open the action menu in "Student 2" "table_row"
And I click on "Grade" "link" in the "Student 2" "table_row"
And I wait for the complete PDF to load
And I click on ".linebutton" "css_element"
And I draw on the pdf
And I press "Save changes"
And I should see "The changes to the grade and feedback were saved"
And I follow "View all submissions"
And I should see "View annotated PDF..." in the "student2@example.com" "table_row"
@@ -0,0 +1,129 @@
<?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/>.
/**
* Behat editpdf-related steps definitions.
*
* @package assignfeedback_editpdf
* @category test
* @copyright 2013 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../../../../lib/behat/behat_base.php');
/**
* Steps definitions related with the editpdf.
*
* @package assignfeedback_editpdf
* @category test
* @copyright 2013 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_assignfeedback_editpdf extends behat_base {
/**
* Checks that Ghostscript is installed.
*
* @Given /^ghostscript is installed$/
*/
public function ghostscript_is_installed() {
$testpath = assignfeedback_editpdf\pdf::test_gs_path();
if (!extension_loaded('zlib') or
$testpath->status !== assignfeedback_editpdf\pdf::GSPATH_OK) {
throw new \Moodle\BehatExtension\Exception\SkippedException;
}
}
/**
* Draw on the pdf.
*
* @When /^I draw on the pdf$/
*/
public function i_draw_on_the_pdf() {
// There appears to be a bug with detecting changes to
// annotations. If a PDF is annotated, then the student
// updates the submission, if the teacher then draws the
// exact same annotations on the new PDF, the readonly
// pages are not updated and the student's view of the
// annotated PDF still shows the old PDF. So we add some
// randomness in this step to ensure the annotations are
// different every time.
//
// This was added to test MDL-75898. See MDL-76659 for
// more details about the bug.
//
// Note - the start, move and end locations must all be different.
// If they are the same, it's possible the PDF tool selected does not activate.
$startx = 100 + rand(0, 50);
$starty = 250 + rand(0, 50);
$js = ' (function() {
var instance = M.assignfeedback_editpdf.instance;
var event = { clientX: ' . $startx . ', clientY: ' . $starty . ', preventDefault: function() {} };
instance.edit_start(event);
}()); ';
$this->execute_script($js);
sleep(1);
// Move slightly in one direction.
$movex = $startx + 50;
$movey = $starty + 30;
$js = ' (function() {
var instance = M.assignfeedback_editpdf.instance;
var event = { clientX: ' . $movex . ', clientY: ' . $movey . ', preventDefault: function() {} };
instance.edit_move(event);
}()); ';
$this->execute_script($js);
sleep(1);
// Move a little further to stop.
$endx = $movex + 15;
$endy = $movey + 15;
$js = ' (function() {
var instance = M.assignfeedback_editpdf.instance;
var event = { clientX: ' . $endx . ', clientY: ' . $endy . ', preventDefault: function() {} };
instance.edit_end(event);
}()); ';
$this->execute_script($js);
sleep(1);
}
/**
* I wait for all pages in the PDF document to be converted to images and loaded.
*
* @Given /^I wait for the complete PDF to load$/
*/
public function i_wait_for_all_editpdf_pages_to_load() {
// No need to wait if not running JS.
if (!$this->running_javascript()) {
return;
}
// Ensure that the document is ready, and all pages are loaded.
$conditions = [
'typeof M !== "undefined"',
'typeof M.assignfeedback_editpdf !== "undefined"',
'typeof M.assignfeedback_editpdf.instance !== "undefined"',
'M.assignfeedback_editpdf.instance.documentstatus === 2',
'M.assignfeedback_editpdf.instance.pagecount === M.assignfeedback_editpdf.instance.pages.length',
];
$js = implode(' && ', $conditions);
$this->getSession()->wait(self::get_timeout() * 1000, "({$js})");
}
}
@@ -0,0 +1,51 @@
@mod @mod_assign @assignfeedback @assignfeedback_editpdf @javascript @_file_upload
Feature: Ensure that a comment remains visible if its popup menu is open
In order to insert quick list comments in the PDF editor
As a teacher
I need the comment to stay visible when its popup menu is open
Background:
Given ghostscript is installed
And the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activity" exists:
| activity | assign |
| course | C1 |
| name | Test assignment name |
| assignsubmission_file_enabled | 1 |
| assignsubmission_file_maxfiles | 1 |
| assignsubmission_file_maxsizebytes | 102400 |
| assignfeedback_editpdf_enabled | 1 |
| submissiondrafts | 0 |
And the following "mod_assign > submission" exists:
| assign | Test assignment name |
| user | student1 |
| file | mod/assign/feedback/editpdf/tests/fixtures/submission.pdf |
And I am on the "Test assignment name" Activity page logged in as teacher1
And I follow "View all submissions"
And I click on "Grade" "link" in the "Submitted for grading" "table_row"
And I wait for the complete PDF to load
Scenario: Add an empty comment and open its menu
When I click on ".commentbutton" "css_element"
And I draw on the pdf
And I click on ".commentdrawable a" "css_element"
Then ".drawingcanvas .commentdrawable" "css_element" should exist
Scenario: Add text to a comment and open its menu
When I click on ".commentbutton" "css_element"
And I draw on the pdf
And I set the field with xpath "//div[@class='commentdrawable']//textarea" to "Comment"
And I click on ".commentdrawable a" "css_element"
Then ".drawingcanvas .commentdrawable" "css_element" should exist
And the "class" attribute of ".drawingcanvas .commentdrawable" "css_element" should not contain "commentcollapsed"
@@ -0,0 +1,86 @@
@mod @mod_assign @assignfeedback @assignfeedback_editpdf @_file_upload
Feature: In a group assignment, teacher can annotate PDF files for all users
In order to provide visual report on a graded PDF for all users
As a teacher
I need to use the PDF editor for a group assignment
Background:
Given ghostscript is installed
And the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
| student2 | C1 | student |
And the following "groups" exist:
| name | course | idnumber |
| G1 | C1 | G1 |
And the following "group members" exist:
| user | group |
| student1 | G1 |
| student2 | G1 |
And the following "activity" exists:
| activity | assign |
| course | C1 |
| name | Test assignment name |
| assignsubmission_file_enabled | 1 |
| assignsubmission_file_maxfiles | 1 |
| assignsubmission_file_maxsizebytes | 102400 |
| assignfeedback_editpdf_enabled | 1 |
| submissiondrafts | 0 |
| teamsubmission | 1 |
And the following "mod_assign > submission" exists:
| assign | Test assignment name |
| user | student1 |
| file | mod/assign/feedback/editpdf/tests/fixtures/submission.pdf |
And I am on the "Test assignment name" Activity page logged in as teacher1
And I follow "View all submissions"
And I click on "Grade" "link" in the "Submitted for grading" "table_row"
And I wait for the complete PDF to load
And I click on ".navigate-next-button" "css_element"
And I wait until the page is ready
And I click on ".stampbutton" "css_element"
And I draw on the pdf
And I wait until the page is ready
@javascript
Scenario: Submit a PDF file as a student and annotate the PDF as a teacher
Given I set the field "applytoall" to "0"
And I press "Save changes"
And I should see "The changes to the grade and feedback were saved"
And I click on "Edit settings" "link"
And I log out
And I am on the "Test assignment name" Activity page logged in as student1
When I follow "View annotated PDF..."
Then I should see "Annotate PDF"
And I wait until the page is ready
And I click on "Close" "button" in the "Annotate PDF" "dialogue"
And I log out
And I am on the "Test assignment name" Activity page logged in as student2
And I should not see "View annotated PDF..."
@javascript
Scenario: Submit a PDF file as a student and annotate the PDF as a teacher and all students in the group get a copy of the annotated PDF.
Given I press "Save changes"
And I should see "The changes to the grade and feedback were saved"
And I am on the "Test assignment name" Activity page
And I log out
And I am on the "Test assignment name" Activity page logged in as student1
When I follow "View annotated PDF..."
And I change window size to "large"
Then I should see "Annotate PDF"
And I change window size to "medium"
And I wait until the page is ready
And I click on "Close" "button" in the "Annotate PDF" "dialogue"
And I log out
And I am on the "Test assignment name" Activity page logged in as student2
And I should see "View annotated PDF..."
@@ -0,0 +1,55 @@
@mod @mod_assign @assignfeedback @assignfeedback_editpdf @_file_upload
Feature: In an assignment, teacher can view the feedback for a previous attempt.
In order to see the history of attempts
As a teacher
I need to see the previous annotations.
@javascript
Scenario: Submit a PDF file as a student and annotate the PDF as a teacher, allowing another attempt
Given ghostscript is installed
And the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activity" exists:
| activity | assign |
| course | C1 |
| name | Test assignment name |
| maxattempts | 0 |
| assignsubmission_file_enabled | 1 |
| assignsubmission_file_maxfiles | 2 |
| assignsubmission_file_maxsizebytes | 102400 |
| assignfeedback_editpdf_enabled | 1 |
| submissiondrafts | 0 |
| attemptreopenmethod | manual |
And the following "mod_assign > submission" exists:
| assign | Test assignment name |
| user | student1 |
| file | mod/assign/feedback/editpdf/tests/fixtures/submission.pdf, mod/assign/feedback/editpdf/tests/fixtures/testgs.pdf |
When I am on the "Test assignment name" Activity page logged in as teacher1
And I follow "View all submissions"
And I click on "Grade" "link" in the "Submitted for grading" "table_row"
Then I should see "Page 1 of 3"
And I click on ".navigate-next-button" "css_element"
And I should see "Page 2 of 3"
And I click on ".stampbutton" "css_element"
And I draw on the pdf
And I wait until the page is ready
And I set the field "Allow another attempt" to "Yes"
And I press "Save changes"
And I wait until the page is ready
And I should see "The changes to the grade and feedback were saved"
And I follow "View a different attempt"
And I click on "Attempt 1" "radio" in the "View a different attempt" "dialogue"
And I press "View"
And I wait until the page is ready
And I should see "You are editing the feedback for a previous attempt. This is attempt 1 out of 2."
And I should see "Page 1 of 3"
@@ -0,0 +1,156 @@
<?php
// This file is part of Moodle - https://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/>.
namespace assignfeedback_editpdf;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
/**
* Unit tests for document services.
*
* @package assignfeedback_editpdf
* @category test
* @copyright 2022 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \assignfeedback_editpdf\document_services
*/
final class document_services_test extends \advanced_testcase {
use \mod_assign_test_generator;
/**
* Test that the save file method saves the file.
*/
public function test_save_file_saves_the_file(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$assign = $this->create_instance($course);
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
$method = new \ReflectionMethod('\assignfeedback_editpdf\document_services', 'save_file');
$filearea = document_services::TMP_ROTATED_JPG_FILEAREA;
$content = 'some random content';
$tempfile = make_request_directory() . DIRECTORY_SEPARATOR . 'mock.file';
file_put_contents($tempfile, $content);
// Invoke the method and confirm, that the file is saved.
$file1 = $method->invoke(null, $assign, $user->id, 1, $filearea, $tempfile);
$this->assertInstanceOf('stored_file', $file1);
$this->assertEquals(1, $DB->count_records('files', ['id' => $file1->get_id()]));
// Invoke the method again and confirm, that exising file is returned.
$file2 = $method->invoke(null, $assign, $user->id, 1, $filearea, $tempfile);
$this->assertEquals($file1->get_id(), $file2->get_id());
}
/**
* Test that save_rotated_image_file() method saves the file.
*/
public function test_save_rotated_image_file_saves_the_file(): void {
global $CFG, $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$assign = $this->create_instance($course);
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
$method = new \ReflectionMethod('\assignfeedback_editpdf\document_services', 'save_rotated_image_file');
$imagecontent = file_get_contents($CFG->dirroot . '/lib/filestorage/tests/fixtures/testimage.png');
$imageresource = imagecreatefromstring($imagecontent);
// Invoke the method and confirm, that the file is saved.
$file1 = $method->invoke(null, $assign, $user->id, 1, $imageresource, 'testimage.png');
$this->assertInstanceOf('stored_file', $file1);
$this->assertEquals(1, $DB->count_records('files', ['id' => $file1->get_id()]));
// Invoke the method again and confirm, that exising file is returned.
$file2 = $method->invoke(null, $assign, $user->id, 1, $imageresource, 'testimage.png');
$this->assertEquals($file1->get_id(), $file2->get_id());
}
/**
* Test that get_combined_document_for_attempt() method rotates the image only once.
*/
public function test_get_combined_document_for_attempt_rotates_image(): void {
global $CFG, $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$assignparams = [
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignsubmission_file_maxsizebytes' => 1024 * 1024,
];
$assign = $this->create_instance($course, $assignparams);
$student = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($student->id, $course->id, 'student');
$this->setUser($student);
$notices = [];
$submission = $assign->get_user_submission($student->id, true, 1);
$data = (object) ['files_filemanager' => $submission->id];
$assign->save_submission($data, $notices);
// This image was manually rotated to be upside down. Also, Orientation, ExifImageWidth
// and ExifImageLength EXIF tags were written into its metadata.
// This is needed to make sure that this image will be rotated by stored_file::rotate_image()
// and stored as a new rotated file.
$filename = 'testimage_rotated.jpg';
$filepath = $CFG->dirroot . '/lib/filestorage/tests/fixtures/' . $filename;
$filerecord = [
'contextid' => $assign->get_context()->id,
'component' => 'assignsubmission_file',
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
'itemid' => $submission->id,
'filepath' => '/',
'filename' => $filename,
];
$fs = get_file_storage();
$fs->create_file_from_pathname($filerecord, $filepath);
$params = [
'filearea' => document_services::TMP_ROTATED_JPG_FILEAREA,
'component' => document_services::COMPONENT,
'filename' => $filename,
];
// Combine the document and get the rotated file.
document_services::get_combined_document_for_attempt($assign, $student->id, 1);
$records = $DB->get_records('files', $params);
$this->assertCount(1, $records);
$record1 = reset($records);
// Polling file converters do this twice: one call to start a conversion and another one
// to poll the converted file. So we combine the document again here.
document_services::get_combined_document_for_attempt($assign, $student->id, 1);
$records = $DB->get_records('files', $params);
$this->assertCount(1, $records);
$record2 = reset($records);
// Confirm, that the second get_combined_document_for_attempt() call doesn't create new
// rotated file and re-uses the one that was created as part of the first
// get_combined_document_for_attempt() call.
$this->assertEquals($record1->id, $record2->id);
}
}
@@ -0,0 +1,611 @@
<?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/>.
namespace assignfeedback_editpdf;
use mod_assign_test_generator;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
/**
* Unit tests for assignfeedback_editpdf\comments_quick_list
*
* @package assignfeedback_editpdf
* @category test
* @copyright 2013 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class feedback_test extends \advanced_testcase {
// Use the generator helper.
use mod_assign_test_generator;
/**
* Ensure that GS is available.
*/
protected function require_ghostscript() {
// Skip this test if ghostscript is not supported.
$result = pdf::test_gs_path(false);
if ($result->status !== pdf::GSPATH_OK) {
$this->markTestSkipped('Ghostscript not setup');
}
}
/**
* Helper method to add a file to a submission.
*
* @param \stdClass $student Student submitting.
* @param \assign $assign Assignment being submitted.
* @param bool $textfile Use textfile fixture instead of pdf.
*/
protected function add_file_submission($student, $assign, $textfile = false) {
global $CFG;
$this->setUser($student);
// Create a file submission with the test pdf.
$submission = $assign->get_user_submission($student->id, true);
$fs = get_file_storage();
$filerecord = (object) array(
'contextid' => $assign->get_context()->id,
'component' => 'assignsubmission_file',
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
'itemid' => $submission->id,
'filepath' => '/',
'filename' => $textfile ? 'submission.txt' : 'submission.pdf'
);
$sourcefile = $CFG->dirroot . '/mod/assign/feedback/editpdf/tests/fixtures/submission.' . ($textfile ? 'txt' : 'pdf');
$fs->create_file_from_pathname($filerecord, $sourcefile);
$data = new \stdClass();
$plugin = $assign->get_submission_plugin_by_type('file');
$plugin->save($submission, $data);
}
public function test_comments_quick_list(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
$this->setUser($teacher);
$this->assertEmpty(comments_quick_list::get_comments());
$comment = comments_quick_list::add_comment('test', 45, 'red');
$comments = comments_quick_list::get_comments();
$this->assertEquals(count($comments), 1);
$first = reset($comments);
$this->assertEquals($comment, $first);
$commentbyid = comments_quick_list::get_comment($comment->id);
$this->assertEquals($comment, $commentbyid);
$this->assertTrue(comments_quick_list::remove_comment($comment->id));
$comments = comments_quick_list::get_comments();
$this->assertEmpty($comments);
}
public function test_page_editor(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
$assign = $this->create_instance($course, [
'assignsubmission_onlinetext_enabled' => 1,
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 1,
'assignsubmission_file_maxsizebytes' => 1000000,
]);
// Add the standard submission.
$this->add_file_submission($student, $assign);
$this->setUser($teacher);
$grade = $assign->get_user_grade($student->id, true);
$notempty = page_editor::has_annotations_or_comments($grade->id, false);
$this->assertFalse($notempty);
$comment = new comment();
$comment->rawtext = 'Comment text';
$comment->width = 100;
$comment->x = 100;
$comment->y = 100;
$comment->colour = 'red';
$comment2 = new comment();
$comment2->rawtext = 'Comment text 2';
$comment2->width = 100;
$comment2->x = 200;
$comment2->y = 100;
$comment2->colour = 'clear';
page_editor::set_comments($grade->id, 0, array($comment, $comment2));
$annotation = new annotation();
$annotation->path = '';
$annotation->x = 100;
$annotation->y = 100;
$annotation->endx = 200;
$annotation->endy = 200;
$annotation->type = 'line';
$annotation->colour = 'red';
$annotation2 = new annotation();
$annotation2->path = '';
$annotation2->x = 100;
$annotation2->y = 100;
$annotation2->endx = 200;
$annotation2->endy = 200;
$annotation2->type = 'rectangle';
$annotation2->colour = 'yellow';
page_editor::set_annotations($grade->id, 0, array($annotation, $annotation2));
// Still empty because all edits are still drafts.
$this->assertFalse(page_editor::has_annotations_or_comments($grade->id, false));
$comments = page_editor::get_comments($grade->id, 0, false);
$this->assertEmpty($comments);
$comments = page_editor::get_comments($grade->id, 0, true);
$this->assertEquals(count($comments), 2);
$annotations = page_editor::get_annotations($grade->id, 0, false);
$this->assertEmpty($annotations);
$annotations = page_editor::get_annotations($grade->id, 0, true);
$this->assertEquals(count($annotations), 2);
$comment = reset($comments);
$annotation = reset($annotations);
page_editor::remove_comment($comment->id);
page_editor::remove_annotation($annotation->id);
$comments = page_editor::get_comments($grade->id, 0, true);
$this->assertEquals(count($comments), 1);
$annotations = page_editor::get_annotations($grade->id, 0, true);
$this->assertEquals(count($annotations), 1);
// Release the drafts.
page_editor::release_drafts($grade->id);
$notempty = page_editor::has_annotations_or_comments($grade->id, false);
$this->assertTrue($notempty);
// Unrelease the drafts.
page_editor::unrelease_drafts($grade->id);
$notempty = page_editor::has_annotations_or_comments($grade->id, false);
$this->assertFalse($notempty);
}
public function test_document_services(): void {
$this->require_ghostscript();
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
$assign = $this->create_instance($course, [
'assignsubmission_onlinetext_enabled' => 1,
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 1,
'assignsubmission_file_maxsizebytes' => 1000000,
]);
// Add the standard submission.
$this->add_file_submission($student, $assign);
$this->setUser($teacher);
$grade = $assign->get_user_grade($student->id, true);
$contextid = $assign->get_context()->id;
$component = 'assignfeedback_editpdf';
$filearea = document_services::COMBINED_PDF_FILEAREA;
$itemid = $grade->id;
$filepath = '/';
$filename = document_services::COMBINED_PDF_FILENAME;
$fs = \get_file_storage();
// Generate a blank combined pdf.
$record = new \stdClass();
$record->contextid = $contextid;
$record->component = $component;
$record->filearea = $filearea;
$record->itemid = $itemid;
$record->filepath = $filepath;
$record->filename = $filename;
$fs->create_file_from_string($record, base64_decode(document_services::BLANK_PDF_BASE64));
// Verify that the blank combined pdf has the expected hash.
$combinedpdf = $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename);
$this->assertEquals($combinedpdf->get_contenthash(), document_services::BLANK_PDF_HASH);
// Generate page images and verify that the combined pdf has been replaced.
document_services::get_page_images_for_attempt($assign, $student->id, -1);
$combinedpdf = $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename);
$this->assertNotEquals($combinedpdf->get_contenthash(), document_services::BLANK_PDF_HASH);
$notempty = page_editor::has_annotations_or_comments($grade->id, false);
$this->assertFalse($notempty);
$comment = new comment();
// Use some different charset in the comment text.
$comment->rawtext = 'Testing example: בקלות ואמנות';
$comment->width = 100;
$comment->x = 100;
$comment->y = 100;
$comment->colour = 'red';
page_editor::set_comments($grade->id, 0, array($comment));
$annotations = array();
$annotation = new annotation();
$annotation->path = '';
$annotation->x = 100;
$annotation->y = 100;
$annotation->endx = 200;
$annotation->endy = 200;
$annotation->type = 'line';
$annotation->colour = 'red';
array_push($annotations, $annotation);
$annotation = new annotation();
$annotation->path = '';
$annotation->x = 100;
$annotation->y = 100;
$annotation->endx = 200;
$annotation->endy = 200;
$annotation->type = 'rectangle';
$annotation->colour = 'yellow';
array_push($annotations, $annotation);
$annotation = new annotation();
$annotation->path = '';
$annotation->x = 100;
$annotation->y = 100;
$annotation->endx = 200;
$annotation->endy = 200;
$annotation->type = 'oval';
$annotation->colour = 'green';
array_push($annotations, $annotation);
$annotation = new annotation();
$annotation->path = '';
$annotation->x = 100;
$annotation->y = 100;
$annotation->endx = 200;
$annotation->endy = 116;
$annotation->type = 'highlight';
$annotation->colour = 'blue';
array_push($annotations, $annotation);
$annotation = new annotation();
$annotation->path = '100,100:105,105:110,100';
$annotation->x = 100;
$annotation->y = 100;
$annotation->endx = 110;
$annotation->endy = 105;
$annotation->type = 'pen';
$annotation->colour = 'black';
array_push($annotations, $annotation);
page_editor::set_annotations($grade->id, 0, $annotations);
page_editor::release_drafts($grade->id);
$notempty = page_editor::has_annotations_or_comments($grade->id, false);
$this->assertTrue($notempty);
$file = document_services::generate_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
$this->assertNotEmpty($file);
$file2 = document_services::get_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
$this->assertEquals($file, $file2);
document_services::delete_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
$file3 = document_services::get_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
$this->assertEmpty($file3);
}
/**
* Test Convert submission ad-hoc task.
*
* @covers \assignfeedback_editpdf\task\convert_submission
*/
public function test_conversion_task(): void {
$this->require_ghostscript();
$this->resetAfterTest();
\core\cron::setup_user();
$course = $this->getDataGenerator()->create_course();
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
$assignopts = [
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 1,
'assignsubmission_file_maxsizebytes' => 1000000,
];
$assign = $this->create_instance($course, $assignopts);
// Add the standard submission.
$this->add_file_submission($student, $assign);
// Run the conversion task.
$task = \core\task\manager::get_next_adhoc_task(time());
ob_start();
$task->execute();
\core\task\manager::adhoc_task_complete($task);
$output = ob_get_clean();
// Confirm, that submission has been converted and the task queue is now empty.
$this->assertStringContainsString('Converting submission for user id ' . $student->id, $output);
$this->assertStringContainsString('The document has been successfully converted', $output);
$this->assertNull(\core\task\manager::get_next_adhoc_task(time()));
// Trigger a re-queue by 'updating' a submission.
$submission = $assign->get_user_submission($student->id, true);
$plugin = $assign->get_submission_plugin_by_type('file');
$plugin->save($submission, (new \stdClass));
$task = \core\task\manager::get_next_adhoc_task(time());
// Verify that queued a conversion task.
$this->assertNotNull($task);
ob_start();
$task->execute();
\core\task\manager::adhoc_task_complete($task);
$output = ob_get_clean();
// Confirm, that submission has been converted and the task queue is now empty.
$this->assertStringContainsString('Converting submission for user id ' . $student->id, $output);
$this->assertStringContainsString('The document has been successfully converted', $output);
$this->assertNull(\core\task\manager::get_next_adhoc_task(time()));
}
/**
* Test that modifying the annotated pdf form return true when modified
* and false when not modified.
*/
public function test_is_feedback_modified(): void {
$this->require_ghostscript();
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
$assign = $this->create_instance($course, [
'assignsubmission_onlinetext_enabled' => 1,
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 1,
'assignsubmission_file_maxsizebytes' => 1000000,
]);
// Add the standard submission.
$this->add_file_submission($student, $assign);
$this->setUser($teacher);
$grade = $assign->get_user_grade($student->id, true);
$notempty = page_editor::has_annotations_or_comments($grade->id, false);
$this->assertFalse($notempty);
$comment = new comment();
$comment->rawtext = 'Comment text';
$comment->width = 100;
$comment->x = 100;
$comment->y = 100;
$comment->colour = 'red';
page_editor::set_comments($grade->id, 0, array($comment));
$annotations = array();
$annotation = new annotation();
$annotation->path = '';
$annotation->x = 100;
$annotation->y = 100;
$annotation->endx = 200;
$annotation->endy = 200;
$annotation->type = 'line';
$annotation->colour = 'red';
array_push($annotations, $annotation);
page_editor::set_annotations($grade->id, 0, $annotations);
$plugin = $assign->get_feedback_plugin_by_type('editpdf');
$data = new \stdClass();
$data->editpdf_source_userid = $student->id;
$this->assertTrue($plugin->is_feedback_modified($grade, $data));
$plugin->save($grade, $data);
$annotation = new annotation();
$annotation->gradeid = $grade->id;
$annotation->pageno = 0;
$annotation->path = '';
$annotation->x = 100;
$annotation->y = 100;
$annotation->endx = 200;
$annotation->endy = 200;
$annotation->type = 'rectangle';
$annotation->colour = 'yellow';
$yellowannotationid = page_editor::add_annotation($annotation);
// Add a comment as well.
$comment = new comment();
$comment->gradeid = $grade->id;
$comment->pageno = 0;
$comment->rawtext = 'Second Comment text';
$comment->width = 100;
$comment->x = 100;
$comment->y = 100;
$comment->colour = 'red';
page_editor::add_comment($comment);
$this->assertTrue($plugin->is_feedback_modified($grade, $data));
$plugin->save($grade, $data);
// We should have two annotations.
$this->assertCount(2, page_editor::get_annotations($grade->id, 0, false));
// And two comments.
$this->assertCount(2, page_editor::get_comments($grade->id, 0, false));
// Add one annotation and delete another.
$annotation = new annotation();
$annotation->gradeid = $grade->id;
$annotation->pageno = 0;
$annotation->path = '100,100:105,105:110,100';
$annotation->x = 100;
$annotation->y = 100;
$annotation->endx = 110;
$annotation->endy = 105;
$annotation->type = 'pen';
$annotation->colour = 'black';
page_editor::add_annotation($annotation);
$annotations = page_editor::get_annotations($grade->id, 0, true);
page_editor::remove_annotation($yellowannotationid);
$this->assertTrue($plugin->is_feedback_modified($grade, $data));
$plugin->save($grade, $data);
// We should have two annotations.
$this->assertCount(2, page_editor::get_annotations($grade->id, 0, false));
// And two comments.
$this->assertCount(2, page_editor::get_comments($grade->id, 0, false));
// Add a comment and then remove it. Should not be considered as modified.
$comment = new comment();
$comment->gradeid = $grade->id;
$comment->pageno = 0;
$comment->rawtext = 'Third Comment text';
$comment->width = 400;
$comment->x = 57;
$comment->y = 205;
$comment->colour = 'black';
$comment->id = page_editor::add_comment($comment);
// We should now have three comments.
$this->assertCount(3, page_editor::get_comments($grade->id, 0, true));
// Now delete the newest record.
page_editor::remove_comment($comment->id);
// Back to two comments.
$this->assertCount(2, page_editor::get_comments($grade->id, 0, true));
// No modification.
$this->assertFalse($plugin->is_feedback_modified($grade, $data));
}
/**
* Test that overwriting a submission file deletes any associated conversions.
*
* @covers \core_files\conversion::get_conversions_for_file
*/
public function test_submission_file_overridden(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
$assign = $this->create_instance($course, [
'assignsubmission_onlinetext_enabled' => 1,
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 1,
'assignsubmission_file_maxsizebytes' => 1000000,
]);
$this->add_file_submission($student, $assign, true);
$submission = $assign->get_user_submission($student->id, true);
$fs = get_file_storage();
$sourcefile = $fs->get_file(
$assign->get_context()->id,
'assignsubmission_file',
ASSIGNSUBMISSION_FILE_FILEAREA,
$submission->id,
'/',
'submission.txt'
);
$conversion = new \core_files\conversion(0, (object)[
'sourcefileid' => $sourcefile->get_id(),
'targetformat' => 'pdf'
]);
$conversion->create();
$conversions = \core_files\conversion::get_conversions_for_file($sourcefile, 'pdf');
$this->assertCount(1, $conversions);
$filerecord = (object)[
'contextid' => $assign->get_context()->id,
'component' => 'core',
'filearea' => 'unittest',
'itemid' => $submission->id,
'filepath' => '/',
'filename' => 'submission.txt'
];
$fs = get_file_storage();
$newfile = $fs->create_file_from_string($filerecord, 'something totally different');
$sourcefile->replace_file_with($newfile);
$conversions = \core_files\conversion::get_conversions_for_file($sourcefile, 'pdf');
$this->assertCount(0, $conversions);
}
/**
* Tests that when the plugin is not enabled for an assignment it does not create conversion tasks.
*
* @covers \assignfeedback_editpdf\event\observer
*/
public function test_submission_not_enabled(): void {
$this->require_ghostscript();
$this->resetAfterTest();
\core\cron::setup_user();
$course = $this->getDataGenerator()->create_course();
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
$assignopts = [
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 0,
'assignsubmission_file_maxsizebytes' => 1000000,
];
$assign = $this->create_instance($course, $assignopts);
// Add the standard submission.
$this->add_file_submission($student, $assign);
$task = \core\task\manager::get_next_adhoc_task(time());
// No task was created.
$this->assertNull($task);
}
}
Binary file not shown.
@@ -0,0 +1,3 @@
你行你上啊!
不行别BB
Binary file not shown.
@@ -0,0 +1,319 @@
<?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/>.
/**
* Unit tests for assignfeedback_editpdf.
*
* @package assignfeedback_editpdf
* @copyright 2018 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace assignfeedback_editpdf\privacy;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/assign/locallib.php');
require_once($CFG->dirroot . '/mod/assign/tests/privacy/provider_test.php');
use assignfeedback_editpdf\page_editor;
use mod_assign\privacy\assign_plugin_request_data;
/**
* Unit tests for mod/assign/feedback/editpdf/classes/privacy/
*
* @copyright 2018 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends \mod_assign\privacy\provider_test {
public function setUp(): void {
// Skip this test if ghostscript is not supported.
$result = \assignfeedback_editpdf\pdf::test_gs_path(false);
if ($result->status !== \assignfeedback_editpdf\pdf::GSPATH_OK) {
$this->markTestSkipped('Ghostscript not setup');
return;
}
parent::setUp();
}
/**
* Convenience function for creating feedback data.
*
* @param object $assign assign object
* @param \stdClass $student user object
* @param \stdClass $teacher user object
* @return array Feedback plugin object and the grade object.
*/
protected function create_feedback($assign, $student, $teacher) {
global $CFG;
// Create a file submission with the test pdf.
$submission = $assign->get_user_submission($student->id, true);
$this->setUser($student->id);
$fs = get_file_storage();
$pdfsubmission = (object) array(
'contextid' => $assign->get_context()->id,
'component' => 'assignsubmission_file',
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
'itemid' => $submission->id,
'filepath' => '/',
'filename' => 'submission.pdf'
);
$sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
$fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
$data = new \stdClass();
$plugin = $assign->get_submission_plugin_by_type('file');
$plugin->save($submission, $data);
$this->setUser($teacher->id);
$plugin = $assign->get_feedback_plugin_by_type('editpdf');
$grade = $assign->get_user_grade($student->id, true);
$comment = new \assignfeedback_editpdf\comment();
$comment->rawtext = 'Comment text';
$comment->width = 100;
$comment->x = 100;
$comment->y = 100;
$comment->colour = 'red';
page_editor::set_comments($grade->id, 0, [$comment]);
$annotation = new \assignfeedback_editpdf\annotation();
$annotation->path = '';
$annotation->x = 100;
$annotation->y = 100;
$annotation->endx = 200;
$annotation->endy = 200;
$annotation->type = 'line';
$annotation->colour = 'red';
page_editor::set_annotations($grade->id, 0, [$annotation]);
$comments = page_editor::get_comments($grade->id, 0, true);
$annotations = page_editor::get_annotations($grade->id, 0, false);
page_editor::release_drafts($grade->id);
$storedfile = \assignfeedback_editpdf\document_services::generate_feedback_document($assign->get_instance()->id, $student->id,
$grade->attemptnumber);
return [$plugin, $grade, $storedfile];
}
/**
* Quick test to make sure that get_metadata returns something.
*/
public function test_get_metadata(): void {
$collection = new \core_privacy\local\metadata\collection('assignfeedback_editpdf');
$collection = \assignfeedback_editpdf\privacy\provider::get_metadata($collection);
$this->assertNotEmpty($collection);
}
/**
* Test that feedback comments are exported for a user.
*/
public function test_export_feedback_user_data(): void {
$this->resetAfterTest();
// Create course, assignment, submission, and then a feedback comment.
$course = $this->getDataGenerator()->create_course();
// Student.
$user1 = $this->getDataGenerator()->create_user();
// Teacher.
$user2 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
$assign = $this->create_instance(['course' => $course,
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 1,
'assignsubmission_file_maxsizebytes' => 1000000]);
$context = $assign->get_context();
list($plugin, $grade, $storedfile) = $this->create_feedback($assign, $user1, $user2);
// Check that we have data.
$this->assertFalse($plugin->is_empty($grade));
$writer = \core_privacy\local\request\writer::with_context($context);
$this->assertFalse($writer->has_any_data());
// The student should be able to see the teachers feedback.
$exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user1);
\assignfeedback_editpdf\privacy\provider::export_feedback_user_data($exportdata);
// print_object($writer->get_files([get_string('privacy:path', 'assignfeedback_editpdf')]));
// print_object($writer->get_files(['PDF feedback', $storedfile->get_filename()]));
$pdffile = $writer->get_files([get_string('privacy:path', 'assignfeedback_editpdf')])[$storedfile->get_filename()];
// The writer should have returned a stored file.
$this->assertInstanceOf('stored_file', $pdffile);
}
/**
* Test that all feedback is deleted for a context.
*/
public function test_delete_feedback_for_context(): void {
$this->resetAfterTest();
// Create course, assignment, submission, and then a feedback comment.
$course = $this->getDataGenerator()->create_course();
// Students.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
// Teacher.
$user3 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
$this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
$assign = $this->create_instance(['course' => $course,
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 1,
'assignsubmission_file_maxsizebytes' => 1000000]);
$context = $assign->get_context();
list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign, $user1, $user3);
list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign, $user2, $user3);
// Check that we have data.
$this->assertFalse($plugin1->is_empty($grade1));
$this->assertFalse($plugin2->is_empty($grade2));
$requestdata = new assign_plugin_request_data($context, $assign);
\assignfeedback_editpdf\privacy\provider::delete_feedback_for_context($requestdata);
// Check that we now have no data.
$this->assertTrue($plugin1->is_empty($grade1));
$this->assertTrue($plugin2->is_empty($grade2));
}
/**
* Test that a grade item is deleted for a user.
*/
public function test_delete_feedback_for_grade(): void {
$this->resetAfterTest();
// Create course, assignment, submission, and then a feedback comment.
$course = $this->getDataGenerator()->create_course();
// Students.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
// Teacher.
$user3 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
$this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
$assign = $this->create_instance(['course' => $course,
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 1,
'assignsubmission_file_maxsizebytes' => 1000000]);
$context = $assign->get_context();
list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign, $user1, $user3);
list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign, $user2, $user3);
// Check that we have data.
$this->assertFalse($plugin1->is_empty($grade1));
$this->assertFalse($plugin2->is_empty($grade2));
$requestdata = new assign_plugin_request_data($context, $assign, $grade1, [], $user1);
\assignfeedback_editpdf\privacy\provider::delete_feedback_for_grade($requestdata);
// Check that we now have no data for user 1.
$this->assertTrue($plugin1->is_empty($grade1));
// Check that user 2 data is still there.
$this->assertFalse($plugin2->is_empty($grade2));
}
/**
* Test that a grade item is deleted for a user.
*/
public function test_delete_feedback_for_grades(): void {
global $DB;
$this->resetAfterTest();
// Create course, assignment, submission, and then a feedback comment.
$course = $this->getDataGenerator()->create_course();
// Students.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$user4 = $this->getDataGenerator()->create_user();
// Teacher.
$user5 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
$this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
$this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
$this->getDataGenerator()->enrol_user($user5->id, $course->id, 'editingteacher');
$assign1 = $this->create_instance(['course' => $course,
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 1,
'assignsubmission_file_maxsizebytes' => 1000000]);
$assign2 = $this->create_instance(['course' => $course,
'assignsubmission_file_enabled' => 1,
'assignsubmission_file_maxfiles' => 1,
'assignfeedback_editpdf_enabled' => 1,
'assignsubmission_file_maxsizebytes' => 1000000]);
$context = $assign1->get_context();
list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign1, $user1, $user5);
list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign1, $user2, $user5);
list($plugin3, $grade3, $storedfile3) = $this->create_feedback($assign1, $user3, $user5);
list($plugin4, $grade4, $storedfile4) = $this->create_feedback($assign2, $user3, $user5);
list($plugin5, $grade5, $storedfile5) = $this->create_feedback($assign2, $user4, $user5);
// Check that we have data.
$this->assertFalse($plugin1->is_empty($grade1));
$this->assertFalse($plugin2->is_empty($grade2));
$this->assertFalse($plugin3->is_empty($grade3));
$this->assertFalse($plugin4->is_empty($grade4));
$this->assertFalse($plugin5->is_empty($grade5));
// Check that there are also files generated.
$files = $DB->get_records('files', ['component' => 'assignfeedback_editpdf', 'filearea' => 'download']);
$this->assertCount(10, $files);
$deletedata = new assign_plugin_request_data($context, $assign1);
$deletedata->set_userids([$user1->id, $user3->id]);
$deletedata->populate_submissions_and_grades();
\assignfeedback_editpdf\privacy\provider::delete_feedback_for_grades($deletedata);
// Check that we now have no data for user 1.
$this->assertTrue($plugin1->is_empty($grade1));
// Check that user 2 data is still there.
$this->assertFalse($plugin2->is_empty($grade2));
// User 3 in assignment 1 should be gone.
$this->assertTrue($plugin3->is_empty($grade3));
// User 3 in assignment 2 should still be here.
$this->assertFalse($plugin4->is_empty($grade4));
// User 4 in assignment 2 should also still be here.
$this->assertFalse($plugin5->is_empty($grade5));
// Check the files as well.
$files = $DB->get_records('files', ['component' => 'assignfeedback_editpdf', 'filearea' => 'download']);
// We should now only have six records here.
$this->assertCount(6, $files);
}
}