MDL-39954 assignsubmission_onlinetext: Replacing old-style events_trigger
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
<?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_onlinetext assessable uploaded event.
|
||||
*
|
||||
* @package assignsubmission_onlinetext
|
||||
* @copyright 2013 Frédéric Massart
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace assignsubmission_onlinetext\event;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* assignsubmission_onlinetext assessable uploaded event class.
|
||||
*
|
||||
* @package assignsubmission_onlinetext
|
||||
* @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 {
|
||||
|
||||
/**
|
||||
* Returns localised description of what happened.
|
||||
*
|
||||
* @return \lang_string
|
||||
*/
|
||||
public function get_description() {
|
||||
return new \lang_string('event_assessable_uploaded_desc', 'assignsubmission_onlinetext', $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;
|
||||
$eventdata->content = $this->other['content'];
|
||||
if ($this->other['pathnamehashes']) {
|
||||
$eventdata->pathnamehashes = $this->other['pathnamehashes'];
|
||||
}
|
||||
return $eventdata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the legacy event name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_legacy_eventname() {
|
||||
return 'assessable_content_uploaded';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_name() {
|
||||
return get_string('event_assessable_uploaded', 'assignsubmission_onlinetext');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
|
||||
/**
|
||||
* Init method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function init() {
|
||||
parent::init();
|
||||
$this->data['objecttable'] = 'assign_submission';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,6 +27,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'] = 'Online text';
|
||||
$string['enabled_help'] = 'If enabled, students are able to type rich text directly into an editor field for their submission.';
|
||||
$string['event_assessable_uploaded'] = 'An online text has been uploaded.';
|
||||
$string['event_assessable_uploaded_desc'] = 'User {$a->userid} has saved an online text in submission {$a->objectid}.';
|
||||
$string['nosubmission'] = 'Nothing has been submitted for this assignment';
|
||||
$string['onlinetext'] = 'Online text';
|
||||
$string['onlinetextfilename'] = 'onlinetext.html';
|
||||
|
||||
@@ -153,19 +153,16 @@ class assign_submission_onlinetext extends assign_submission_plugin {
|
||||
'id',
|
||||
false);
|
||||
|
||||
// Let Moodle know that an assessable content 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;
|
||||
$eventdata->content = trim($text);
|
||||
|
||||
if ($files) {
|
||||
$eventdata->pathnamehashes = array_keys($files);
|
||||
}
|
||||
events_trigger('assessable_content_uploaded', $eventdata);
|
||||
$params = array(
|
||||
'context' => context_module::instance($this->assignment->get_course_module()->id),
|
||||
'objectid' => $submission->id,
|
||||
'other' => array(
|
||||
'pathnamehashes' => array_keys($files),
|
||||
'content' => trim($text)
|
||||
)
|
||||
);
|
||||
$event = \assignsubmission_onlinetext\event\assessable_uploaded::create($params);
|
||||
$event->trigger();
|
||||
|
||||
if ($onlinetextsubmission) {
|
||||
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
<?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_onlinetext
|
||||
* @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_onlinetext_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);
|
||||
$data = new stdClass();
|
||||
$data->onlinetext_editor = array(
|
||||
'itemid' => file_get_unused_draft_itemid(),
|
||||
'text' => 'Submission text',
|
||||
'format' => FORMAT_PLAIN
|
||||
);
|
||||
$plugin = $assign->get_submission_plugin_by_type('onlinetext');
|
||||
$sink = $this->redirectEvents();
|
||||
$plugin->save($submission, $data);
|
||||
$events = $sink->get_events();
|
||||
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf('\assignsubmission_onlinetext\event\assessable_uploaded', $event);
|
||||
$this->assertEquals($context->id, $event->contextid);
|
||||
$this->assertEquals($submission->id, $event->objectid);
|
||||
$this->assertEquals(array(), $event->other['pathnamehashes']);
|
||||
$this->assertEquals('Submission text', $event->other['content']);
|
||||
$expected = new stdClass();
|
||||
$expected->modulename = 'assign';
|
||||
$expected->cmid = $cm->id;
|
||||
$expected->itemid = $submission->id;
|
||||
$expected->courseid = $course->id;
|
||||
$expected->userid = $user->id;
|
||||
$expected->content = 'Submission text';
|
||||
$this->assertEventLegacyData($expected, $event);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user