This commit is contained in:
Jun Pataleta
2025-09-12 09:45:46 +08:00
4 changed files with 178 additions and 0 deletions
@@ -0,0 +1,79 @@
<?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_file\event;
/**
* One or all of the feedback files have been downloaded.
*
* @package assignfeedback_file
* @since Moodle 5.1
* @copyright 2025 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class feedback_downloaded extends \core\event\base {
#[\Override]
public function get_description() {
return "The user with id '$this->userid' downloaded feeedback file '{$this->other['filename']}' (" .
"'{$this->other['fileid']}') for the assignment with course module id '$this->contextinstanceid'.";
}
#[\Override]
public static function get_name() {
return get_string('eventfeedback_downloaded', 'assignfeedback_file');
}
#[\Override]
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'assign_grades';
}
/**
* Creates the event.
*
* @param \stored_file $file File that was downloaded
* @return feedback_downloaded Event instance
*/
public static function create_for_file(\stored_file $file): feedback_downloaded {
return self::create([
'contextid' => $file->get_contextid(),
'objectid' => $file->get_itemid(), // The file item id is the assign_grades id.
'other' => [
'fileid' => $file->get_id(),
'filename' => $file->get_filename(),
],
]);
}
#[\Override]
protected function validate_data() {
if (empty($this->other['fileid'])) {
throw new \coding_exception('other[\'fileid\'] must be set');
}
if (empty($this->other['filename'])) {
throw new \coding_exception('other[\'filename\'] must be set');
}
parent::validate_data();
}
#[\Override]
public static function get_objectid_mapping() {
return ['db' => 'assign_grades', 'restore' => 'grade'];
}
}
@@ -33,6 +33,7 @@ $string['default'] = 'Enabled by default';
$string['default_help'] = 'If set, this feedback method will be enabled by default for all new assignments.';
$string['enabled'] = 'File feedback';
$string['enabled_help'] = 'If enabled, the teacher will be able to upload files with feedback when marking assignment submissions. These files may be, but are not limited to, marked-up student submissions, documents with comments or spoken audio feedback.';
$string['eventfeedback_downloaded'] = 'Feedback file downloaded';
$string['feedbackzip'] = 'Zip file with feedback files';
$string['feedbackfileadded'] = 'New feedback file "{$a->filename}" for student "{$a->student}"';
$string['feedbackfileupdated'] = 'Modified feedback file "{$a->filename}" for student "{$a->student}"';
+4
View File
@@ -74,6 +74,10 @@ function assignfeedback_file_pluginfile($course,
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// Log event so that we can later track if students downloaded their feedback files.
\assignfeedback_file\event\feedback_downloaded::create_for_file($file)->trigger();
// Download MUST be forced - security!
send_stored_file($file, 0, 0, true, $options);
}
@@ -0,0 +1,94 @@
<?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_file\event;
/**
* Tests {@see feedback_downloaded} event.
*
* @package assignfeedback_file
* @category test
* @copyright 2025 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \assignfeedback_file\event\feedback_downloaded
*/
final class feedback_downloaded_test extends \advanced_testcase {
/**
* Tests basic usage of the event, including creation, name, and description.
*/
public function test_feedback_downloaded_ok(): void {
$this->resetAfterTest();
$generator = self::getDataGenerator();
$course = $generator->create_course();
$assign = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$this->setUser($user);
$fs = get_file_storage();
// We are not creating a real grade item, so I'm using 123 for its id.
$file = $fs->create_file_from_string([
'contextid' => \context_module::instance($assign->cmid)->id,
'component' => 'assignfeedback_file',
'filearea' => 'feedback_files',
'itemid' => 123,
'filepath' => '/',
'filename' => 'testfile.txt',
], 'hello world');
$event = feedback_downloaded::create_for_file($file);
$this->assertInstanceOf(feedback_downloaded::class, $event);
$this->assertEquals(CONTEXT_MODULE, $event->get_context()->contextlevel);
$this->assertEquals($assign->cmid, $event->get_context()->instanceid);
$this->assertEquals(123, $event->objectid);
$this->assertEquals($file->get_id(), $event->other['fileid']);
$this->assertEquals('testfile.txt', $event->other['filename']);
$this->assertEquals('Feedback file downloaded', $event->get_name());
$this->assertEquals(
"The user with id '{$user->id}' downloaded feeedback file 'testfile.txt'" .
" ('{$file->get_id()}') for the assignment with course module id '{$assign->cmid}'.",
$event->get_description(),
);
}
/**
* Tests invalid missing fileid if you create manually (which you shouldn't).
*/
public function test_no_fileid(): void {
$this->expectExceptionMessageMatches('~other\[\'fileid\'\] must be set~');
feedback_downloaded::create([
'context' => \context_system::instance(),
'objectid' => 123,
'other' => [
'filename' => 'testfile.txt',
],
]);
}
/**
* Tests invalid missing filename if you create manually (which you shouldn't).
*/
public function test_no_filename(): void {
$this->expectExceptionMessageMatches('~other\[\'filename\'\] must be set~');
feedback_downloaded::create([
'context' => \context_system::instance(),
'objectid' => 123,
'other' => [
'fileid' => 456,
],
]);
}
}