MDL-39954 assignsubmission_file: Replacing old-style events_trigger
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* assignsubmission_file assessable uploaded event.
|
||||
*
|
||||
* @package assignsubmission_file
|
||||
* @copyright 2013 Frédéric Massart
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace assignsubmission_file\event;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* assignsubmission_file assessable uploaded event class.
|
||||
*
|
||||
* @package assignsubmission_file
|
||||
* @copyright 2013 Frédéric Massart
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class assessable_uploaded extends \core\event\assessable_uploaded {
|
||||
|
||||
/**
|
||||
* Legacy event files.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $legacyfiles = array();
|
||||
|
||||
/**
|
||||
* Returns localised description of what happened.
|
||||
*
|
||||
* @return \lang_string
|
||||
*/
|
||||
public function get_description() {
|
||||
return new \lang_string('event_assessable_uploaded_desc', 'assignsubmission_file', $this->get_data());
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy event data if get_legacy_eventname() is not empty.
|
||||
*
|
||||
* @return stdClass
|
||||
*/
|
||||
protected function get_legacy_eventdata() {
|
||||
$eventdata = new \stdClass();
|
||||
$eventdata->modulename = 'assign';
|
||||
$eventdata->cmid = $this->context->instanceid;
|
||||
$eventdata->itemid = $this->objectid;
|
||||
$eventdata->courseid = $this->courseid;
|
||||
$eventdata->userid = $this->userid;
|
||||
if (count($this->legacyfiles) > 1) {
|
||||
$eventdata->files = $this->legacyfiles;
|
||||
}
|
||||
$eventdata->file = $this->legacyfiles;
|
||||
$eventdata->pathnamehashes = array_keys($this->legacyfiles);
|
||||
return $eventdata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the legacy event name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_legacy_eventname() {
|
||||
return 'assessable_file_uploaded';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_name() {
|
||||
return get_string('event_assessable_uploaded', 'assignsubmission_file');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*
|
||||
* @return \moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url('/mod/assign/view.php', array('id' => $this->context->instanceid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the legacy event data.
|
||||
*
|
||||
* @param stdClass $legacyfiles legacy event data.
|
||||
* @return void
|
||||
*/
|
||||
public function set_legacy_files($legacyfiles) {
|
||||
$this->legacyfiles = $legacyfiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function init() {
|
||||
parent::init();
|
||||
$this->data['objecttable'] = 'assign_submission';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,6 +29,8 @@ $string['default'] = 'Enabled by default';
|
||||
$string['default_help'] = 'If set, this submission method will be enabled by default for all new assignments.';
|
||||
$string['enabled'] = 'File submissions';
|
||||
$string['enabled_help'] = 'If enabled, students are able to upload one or more files as their submission.';
|
||||
$string['event_assessable_uploaded'] = 'A file has been uploaded.';
|
||||
$string['event_assessable_uploaded_desc'] = 'User {$a->userid} has uploaded a file in submission {$a->objectid}.';
|
||||
$string['file'] = 'File submissions';
|
||||
$string['maxbytes'] = 'Maximum file size';
|
||||
$string['maxfilessubmission'] = 'Maximum number of uploaded files';
|
||||
|
||||
@@ -219,20 +219,17 @@ class assign_submission_file extends assign_submission_plugin {
|
||||
|
||||
$count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
|
||||
|
||||
// Send files to event system.
|
||||
// This lets Moodle know that an assessable file was uploaded (eg for plagiarism detection).
|
||||
$eventdata = new stdClass();
|
||||
$eventdata->modulename = 'assign';
|
||||
$eventdata->cmid = $this->assignment->get_course_module()->id;
|
||||
$eventdata->itemid = $submission->id;
|
||||
$eventdata->courseid = $this->assignment->get_course()->id;
|
||||
$eventdata->userid = $USER->id;
|
||||
if ($count > 1) {
|
||||
$eventdata->files = $files;
|
||||
}
|
||||
$eventdata->file = $files;
|
||||
$eventdata->pathnamehashes = array_keys($files);
|
||||
events_trigger('assessable_file_uploaded', $eventdata);
|
||||
$params = array(
|
||||
'context' => context_module::instance($this->assignment->get_course_module()->id),
|
||||
'objectid' => $submission->id,
|
||||
'other' => array(
|
||||
'content' => '',
|
||||
'pathnamehashes' => array_keys($files)
|
||||
)
|
||||
);
|
||||
$event = \assignsubmission_file\event\assessable_uploaded::create($params);
|
||||
$event->set_legacy_files($files);
|
||||
$event->trigger();
|
||||
|
||||
if ($filesubmission) {
|
||||
$filesubmission->numfiles = $this->count_files($submission->id,
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Contains the event tests for the plugin.
|
||||
*
|
||||
* @package assignsubmission_file
|
||||
* @copyright 2013 Frédéric Massart
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
|
||||
|
||||
class assignsubmission_file_events_testcase extends advanced_testcase {
|
||||
|
||||
public function test_assessable_uploaded() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
||||
$params['course'] = $course->id;
|
||||
$instance = $generator->create_instance($params);
|
||||
$cm = get_coursemodule_from_instance('assign', $instance->id);
|
||||
$context = context_module::instance($cm->id);
|
||||
$assign = new testable_assign($context, $cm, $course);
|
||||
|
||||
$this->setUser($user->id);
|
||||
$submission = $assign->get_user_submission($user->id, true);
|
||||
|
||||
$fs = get_file_storage();
|
||||
$dummy = (object) array(
|
||||
'contextid' => $context->id,
|
||||
'component' => 'assignsubmission_file',
|
||||
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
||||
'itemid' => $submission->id,
|
||||
'filepath' => '/',
|
||||
'filename' => 'myassignmnent.pdf'
|
||||
);
|
||||
$fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
||||
$dummy = (object) array(
|
||||
'contextid' => $context->id,
|
||||
'component' => 'assignsubmission_file',
|
||||
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
||||
'itemid' => $submission->id,
|
||||
'filepath' => '/',
|
||||
'filename' => 'myassignmnent.png'
|
||||
);
|
||||
$fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
||||
$files = $fs->get_area_files($context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
|
||||
$submission->id, 'id', false);
|
||||
|
||||
$data = new stdClass();
|
||||
$plugin = $assign->get_submission_plugin_by_type('file');
|
||||
$sink = $this->redirectEvents();
|
||||
$plugin->save($submission, $data);
|
||||
$events = $sink->get_events();
|
||||
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf('\assignsubmission_file\event\assessable_uploaded', $event);
|
||||
$this->assertEquals($context->id, $event->contextid);
|
||||
$this->assertEquals($submission->id, $event->objectid);
|
||||
$this->assertCount(2, $event->other['pathnamehashes']);
|
||||
$this->assertEquals($fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
|
||||
$this->assertEquals($fi2->get_pathnamehash(), $event->other['pathnamehashes'][1]);
|
||||
$expected = new stdClass();
|
||||
$expected->modulename = 'assign';
|
||||
$expected->cmid = $cm->id;
|
||||
$expected->itemid = $submission->id;
|
||||
$expected->courseid = $course->id;
|
||||
$expected->userid = $user->id;
|
||||
$expected->file = $files;
|
||||
$expected->files = $files;
|
||||
$expected->pathnamehashes = array($fi->get_pathnamehash(), $fi2->get_pathnamehash());
|
||||
$this->assertEventLegacyData($expected, $event);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user