Merge branch 'MDL-39954-master' of git://github.com/FMCorz/moodle
authorDan Poltawski <dan@moodle.com>
Tue, 13 Aug 2013 05:23:31 +0000 (13:23 +0800)
committerDan Poltawski <dan@moodle.com>
Tue, 13 Aug 2013 05:23:31 +0000 (13:23 +0800)
28 files changed:
lib/classes/event/assessable_submitted.php [new file with mode: 0644]
lib/classes/event/assessable_uploaded.php [new file with mode: 0644]
mod/assign/classes/event/assessable_submitted.php [new file with mode: 0644]
mod/assign/lang/en/assign.php
mod/assign/locallib.php
mod/assign/submission/file/classes/event/assessable_uploaded.php [new file with mode: 0644]
mod/assign/submission/file/lang/en/assignsubmission_file.php
mod/assign/submission/file/locallib.php
mod/assign/submission/file/tests/events_test.php [new file with mode: 0644]
mod/assign/submission/onlinetext/classes/event/assessable_uploaded.php [new file with mode: 0644]
mod/assign/submission/onlinetext/lang/en/assignsubmission_onlinetext.php
mod/assign/submission/onlinetext/locallib.php
mod/assign/submission/onlinetext/tests/events_test.php [new file with mode: 0644]
mod/assignment/type/online/assignment.class.php
mod/assignment/type/online/classes/event/assessable_uploaded.php [new file with mode: 0644]
mod/assignment/type/online/lang/en/assignment_online.php
mod/assignment/type/upload/assignment.class.php
mod/assignment/type/upload/classes/event/assessable_submitted.php [new file with mode: 0644]
mod/assignment/type/upload/classes/event/assessable_uploaded.php [new file with mode: 0644]
mod/assignment/type/upload/lang/en/assignment_upload.php
mod/forum/classes/event/assessable_uploaded.php [new file with mode: 0644]
mod/forum/lang/en/forum.php
mod/forum/lib.php
mod/forum/tests/lib_test.php [new file with mode: 0644]
mod/workshop/classes/event/assessable_uploaded.php [new file with mode: 0644]
mod/workshop/lang/en/workshop.php
mod/workshop/locallib.php
mod/workshop/submission.php

diff --git a/lib/classes/event/assessable_submitted.php b/lib/classes/event/assessable_submitted.php
new file mode 100644 (file)
index 0000000..dbdc54b
--- /dev/null
@@ -0,0 +1,67 @@
+<?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/>.
+
+/**
+ * Abstract assessable submitted event.
+ *
+ * @package    core
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Abstract assessable submitted event class.
+ *
+ * This class has to be extended by any event which represent that some content,
+ * on which someone will be assessed, has been submitted and so made available
+ * for grading. See {@link \core\event\assessable_uploaded} for when the content
+ * has just been uploaded.
+ *
+ * Both events could be triggered in a row, first the uploaded, then the submitted.
+ *
+ * @package    core
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+abstract class assessable_submitted extends \core\event\base {
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'u';
+        $this->data['level'] = 50;          // TODO MDL-37658.
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws coding_exception on error.
+     * @return void
+     */
+    protected function validate_data() {
+        if (!$this->context->contextlevel === CONTEXT_MODULE) {
+            throw new coding_exception('Content level must be CONTEXT_MODULE.');
+        }
+    }
+
+}
diff --git a/lib/classes/event/assessable_uploaded.php b/lib/classes/event/assessable_uploaded.php
new file mode 100644 (file)
index 0000000..9d7389c
--- /dev/null
@@ -0,0 +1,71 @@
+<?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/>.
+
+/**
+ * Abstract assessable uploaded event.
+ *
+ * @package    core
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Abstract assessable uploaded event class.
+ *
+ * This class has to be extended by any event which represent that some content,
+ * on which someone will be assessed, has been uploaded. This is different
+ * than other events such as assessable_submitted, which means that the content
+ * has been submitted and made available for grading.
+ *
+ * Both events could be triggered in a row, first the uploaded, then the submitted.
+ *
+ * @package    core
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+abstract class assessable_uploaded extends \core\event\base {
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'c';
+        $this->data['level'] = 50;          // TODO MDL-37658.
+    }
+
+    /**
+     * Validation that should be shared among child classes.
+     *
+     * @throws coding_exception when validation fails.
+     * @return void
+     */
+    protected function validate_data() {
+        if (!$this->context->contextlevel === CONTEXT_MODULE) {
+            throw new coding_exception('Content level must be CONTEXT_MODULE.');
+        } else if (!isset($this->other['pathnamehashes']) || !is_array($this->other['pathnamehashes'])) {
+            throw new coding_exception('pathnamehashes must be set in $other and must be an array.');
+        } else if (!isset($this->other['content']) || !is_string($this->other['content'])) {
+            throw new coding_exception('content must be set in $other and must be a string.');
+        }
+    }
+
+}
diff --git a/mod/assign/classes/event/assessable_submitted.php b/mod/assign/classes/event/assessable_submitted.php
new file mode 100644 (file)
index 0000000..ad9d7c8
--- /dev/null
@@ -0,0 +1,138 @@
+<?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/>.
+
+/**
+ * mod_assign assessable submitted event.
+ *
+ * @package    mod_assign
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_assign\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * mod_assign assessable submitted event class.
+ *
+ * @package    mod_assign
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class assessable_submitted extends \core\event\assessable_submitted {
+
+    /**
+     * Legacy log data.
+     *
+     * @var array
+     */
+    protected $legacylogdata;
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User {$this->userid} has submitted the submission {$this->objectid}.";
+    }
+
+    /**
+     * 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->params = array('submission_editable' => $this->other['submission_editable']);
+        return $eventdata;
+    }
+
+    /**
+     * Return the legacy event name.
+     *
+     * @return string
+     */
+    public static function get_legacy_eventname() {
+        return 'assessable_submitted';
+    }
+
+    /**
+     * Return legacy data for add_to_log().
+     *
+     * @return array
+     */
+    public function get_legacy_logdata() {
+        return $this->legacylogdata;
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return \lang_string
+     */
+    public static function get_name() {
+        return new \lang_string('event_assessable_submitted', 'mod_assign');
+    }
+
+    /**
+     * 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 log data.
+     *
+     * @param stdClass $legacylogdata legacy log data.
+     * @return void
+     */
+    public function set_legacy_logdata($legacylogdata) {
+        $this->legacylogdata = $legacylogdata;
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        parent::init();
+        $this->data['objecttable'] = 'assign_submission';
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['submission_editable'])) {
+            throw new coding_exception('Other must contain the key submission_editable.');
+        }
+    }
+}
index 042af51..65d305e 100644 (file)
@@ -140,6 +140,7 @@ $string['editsubmission'] = 'Edit submission';
 $string['editsubmission_help'] = 'Make changes to your submission';
 $string['editingstatus'] = 'Editing status';
 $string['editaction'] = 'Actions...';
+$string['event_assessable_submitted'] = 'A submission has been submitted.';
 $string['extensionduedate'] = 'Extension due date';
 $string['extensionnotafterduedate'] = 'Extension date must be after the due date';
 $string['extensionnotafterfromdate'] = 'Extension date must be after the allow submissions from date';
index e472da5..be7eedb 100644 (file)
@@ -2403,9 +2403,11 @@ class assign {
      * @param string $action The current action
      * @param string $info A detailed description of the change. But no more than 255 characters.
      * @param string $url The url to the assign module instance.
-     * @return void
+     * @param bool $return If true, returns the arguments, else adds to log. The purpose of this is to
+     *                     retrieve the arguments to use them with the new event system (Event 2).
+     * @return void|array
      */
-    public function add_to_log($action = '', $info = '', $url='') {
+    public function add_to_log($action = '', $info = '', $url='', $return = false) {
         global $USER;
 
         $fullurl = 'view.php?id=' . $this->get_course_module()->id;
@@ -2413,13 +2415,20 @@ class assign {
             $fullurl .= '&' . $url;
         }
 
-        add_to_log($this->get_course()->id,
-                   'assign',
-                   $action,
-                   $fullurl,
-                   $info,
-                   $this->get_course_module()->id,
-                   $USER->id);
+        $args = array(
+            $this->get_course()->id,
+            'assign',
+            $action,
+            $fullurl,
+            $info,
+            $this->get_course_module()->id,
+            $USER->id
+        );
+
+        if ($return) {
+            return $args;
+        }
+        call_user_func_array('add_to_log', $args);
     }
 
     /**
@@ -4500,19 +4509,21 @@ class assign {
                                              fullname($USER));
                     $this->add_to_log('submission statement accepted', $logmessage);
                 }
-                $this->add_to_log('submit for grading', $this->format_submission_for_log($submission));
+                $logdata = $this->add_to_log('submit for grading', $this->format_submission_for_log($submission), '', true);
                 $this->notify_graders($submission);
                 $this->notify_student_submission_receipt($submission);
 
                 // Trigger assessable_submitted event on submission.
-                $eventdata = new stdClass();
-                $eventdata->modulename   = 'assign';
-                $eventdata->cmid         = $this->get_course_module()->id;
-                $eventdata->itemid       = $submission->id;
-                $eventdata->courseid     = $this->get_course()->id;
-                $eventdata->userid       = $USER->id;
-                $eventdata->params       = array( 'submission_editable' => false);
-                events_trigger('assessable_submitted', $eventdata);
+                $params = array(
+                    'context' => context_module::instance($this->get_course_module()->id),
+                    'objectid' => $submission->id,
+                    'other' => array(
+                        'submission_editable' => false
+                    )
+                );
+                $event = \mod_assign\event\assessable_submitted::create($params);
+                $event->set_legacy_logdata($logdata);
+                $event->trigger();
             }
         }
         return true;
@@ -5025,16 +5036,15 @@ class assign {
             // The same logic applies here - we could not notify teachers,
             // but then they would wonder why there are submitted assignments
             // and they haven't been notified.
-            $eventdata = new stdClass();
-            $eventdata->modulename   = 'assign';
-            $eventdata->cmid         = $this->get_course_module()->id;
-            $eventdata->itemid       = $submission->id;
-            $eventdata->courseid     = $this->get_course()->id;
-            $eventdata->userid       = $USER->id;
-            $eventdata->params       = array(
-                'submission_editable' => true,
+            $params = array(
+                'context' => context_module::instance($this->get_course_module()->id),
+                'objectid' => $submission->id,
+                'other' => array(
+                    'submission_editable' => true
+                )
             );
-            events_trigger('assessable_submitted', $eventdata);
+            $event = \mod_assign\event\assessable_submitted::create($params);
+            $event->trigger();
         }
         return true;
     }
@@ -5127,16 +5137,15 @@ class assign {
                 $this->notify_student_submission_receipt($submission);
                 $this->notify_graders($submission);
                 // Trigger assessable_submitted event on submission.
-                $eventdata = new stdClass();
-                $eventdata->modulename   = 'assign';
-                $eventdata->cmid         = $this->get_course_module()->id;
-                $eventdata->itemid       = $submission->id;
-                $eventdata->courseid     = $this->get_course()->id;
-                $eventdata->userid       = $USER->id;
-                $eventdata->params       = array(
-                    'submission_editable' => true,
+                $params = array(
+                    'context' => context_module::instance($this->get_course_module()->id),
+                    'objectid' => $submission->id,
+                    'other' => array(
+                        'submission_editable' => true
+                    )
                 );
-                events_trigger('assessable_submitted', $eventdata);
+                $event = \mod_assign\event\assessable_submitted::create($params);
+                $event->trigger();
             }
             return true;
         }
diff --git a/mod/assign/submission/file/classes/event/assessable_uploaded.php b/mod/assign/submission/file/classes/event/assessable_uploaded.php
new file mode 100644 (file)
index 0000000..0f2ccc7
--- /dev/null
@@ -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 description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User {$this->userid} has uploaded a file in submission {$this->objectid}.";
+    }
+
+    /**
+     * 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
+     */
+    public static 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';
+    }
+
+}
index 69124e9..65fe7a0 100644 (file)
@@ -29,6 +29,7 @@ $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['file'] = 'File submissions';
 $string['maxbytes'] = 'Maximum file size';
 $string['maxfilessubmission'] = 'Maximum number of uploaded files';
index 81a04e9..e9d72e4 100644 (file)
@@ -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,
diff --git a/mod/assign/submission/file/tests/events_test.php b/mod/assign/submission/file/tests/events_test.php
new file mode 100644 (file)
index 0000000..1aaa871
--- /dev/null
@@ -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);
+    }
+
+}
diff --git a/mod/assign/submission/onlinetext/classes/event/assessable_uploaded.php b/mod/assign/submission/onlinetext/classes/event/assessable_uploaded.php
new file mode 100644 (file)
index 0000000..179319e
--- /dev/null
@@ -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 description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User {$this->userid} has saved an online text in submission {$this->objectid}.";
+    }
+
+    /**
+     * 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
+     */
+    public static 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';
+    }
+
+}
index 1406b4f..58de072 100644 (file)
@@ -27,6 +27,7 @@ $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['nosubmission'] = 'Nothing has been submitted for this assignment';
 $string['onlinetext'] = 'Online text';
 $string['onlinetextfilename'] = 'onlinetext.html';
index e90c8a8..6f44e4e 100644 (file)
@@ -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) {
 
diff --git a/mod/assign/submission/onlinetext/tests/events_test.php b/mod/assign/submission/onlinetext/tests/events_test.php
new file mode 100644 (file)
index 0000000..8d40bc3
--- /dev/null
@@ -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);
+    }
+
+}
index a917d31..63c4367 100644 (file)
@@ -201,19 +201,20 @@ class assignment_online extends assignment_base {
         $this->update_grade($submission);
         $fs = get_file_storage();
         $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
+
         // Let Moodle know that an assessable content was uploaded (eg for plagiarism detection)
-        $eventdata = new stdClass();
-        $eventdata->modulename   = 'assignment';
-        $eventdata->name         = 'update_submission';
-        $eventdata->cmid         = $this->cm->id;
-        $eventdata->itemid       = $update->id;
-        $eventdata->courseid     = $this->course->id;
-        $eventdata->userid       = $USER->id;
-        $eventdata->content      = trim(format_text($update->data1, $update->data2));
-        if ($files) {
-            $eventdata->pathnamehashes = array_keys($files);
-        }
-        events_trigger('assessable_content_uploaded', $eventdata);
+        $params = array(
+            'context' => $this->context,
+            'objectid' => $submission->id,
+            'other' => array(
+                'content' => trim(format_text($update->data1, $update->data2)),
+                'pathnamehashes' => array_keys($files),
+                'triggeredfrom' => 'update_submission'
+            )
+        );
+        $event = \assignment_online\event\assessable_uploaded::create($params);
+        $event->trigger();
+
         return $submission;
     }
 
diff --git a/mod/assignment/type/online/classes/event/assessable_uploaded.php b/mod/assignment/type/online/classes/event/assessable_uploaded.php
new file mode 100644 (file)
index 0000000..e5116ba
--- /dev/null
@@ -0,0 +1,116 @@
+<?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/>.
+
+/**
+ * assignment_online assessable uploaded event.
+ *
+ * @package    assignment_online
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace assignment_online\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * assignment_online assessable uploaded event class.
+ *
+ * @package    assignment_online
+ * @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 description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User {$this->userid} has saved an online text in submission {$this->objectid}.";
+    }
+
+    /**
+     * Legacy event data if get_legacy_eventname() is not empty.
+     *
+     * @return stdClass
+     */
+    protected function get_legacy_eventdata() {
+        $eventdata = new \stdClass();
+        $eventdata->modulename = 'assignment';
+        $eventdata->name = $this->other['triggeredfrom'];
+        $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
+     */
+    public static function get_legacy_eventname() {
+        return 'assessable_content_uploaded';
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return \lang_string
+     */
+    public static function get_name() {
+        return new \lang_string('event_assessable_uploaded', 'assignment_online');
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/assignment/view.php', array('id' => $this->context->instanceid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        parent::init();
+        $this->data['objecttable'] = 'assignment_submissions';
+    }
+
+    /**
+     * Custom validation
+     *
+     * @throws coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['triggeredfrom'])) {
+            throw new coding_exception('triggeredfrom must be set in $other');
+        }
+    }
+}
index 33cca31..dcf5a27 100644 (file)
@@ -23,4 +23,5 @@
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+$string['event_assessable_uploaded'] = 'An online text has been uploaded.';
 $string['pluginname'] = 'Online';
index 667308a..c7f3f0f 100644 (file)
@@ -512,8 +512,6 @@ class assignment_upload extends assignment_base {
             $updates->numfiles = count($fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, 'sortorder', false));
             $updates->timemodified = time();
             $DB->update_record('assignment_submissions', $updates);
-            add_to_log($this->course->id, 'assignment', 'upload',
-                    'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
             $this->update_grade($submission);
             if (!$this->drafts_tracked()) {
                 $this->email_teachers($submission);
@@ -521,18 +519,21 @@ class assignment_upload extends assignment_base {
 
             // send files to event system
             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
+
             // Let Moodle know that assessable files were  uploaded (eg for plagiarism detection)
-            $eventdata = new stdClass();
-            $eventdata->modulename   = 'assignment';
-            $eventdata->cmid         = $this->cm->id;
-            $eventdata->itemid       = $submission->id;
-            $eventdata->courseid     = $this->course->id;
-            $eventdata->userid       = $USER->id;
-            if ($files) {
-                $eventdata->files        = $files; // This is depreceated - please use pathnamehashes instead!
-            }
-            $eventdata->pathnamehashes = array_keys($files);
-            events_trigger('assessable_file_uploaded', $eventdata);
+            $params = array(
+                'context' => $this->context,
+                'objectid' => $submission->id,
+                'other' => array(
+                    'assignmentid' => $this->assignment->id,
+                    'content' => '',
+                    'pathnamehashes' => array_keys($files)
+                )
+            );
+            $event = \assignment_upload\event\assessable_uploaded::create($params);
+            $event->set_legacy_files($files);
+            $event->trigger();
+
             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
             redirect($returnurl);
         }
@@ -639,14 +640,17 @@ class assignment_upload extends assignment_base {
         $this->update_grade($submission);
         $this->email_teachers($submission);
 
-        // Trigger assessable_files_done event to show files are complete
-        $eventdata = new stdClass();
-        $eventdata->modulename   = 'assignment';
-        $eventdata->cmid         = $this->cm->id;
-        $eventdata->itemid       = $submission->id;
-        $eventdata->courseid     = $this->course->id;
-        $eventdata->userid       = $userid;
-        events_trigger('assessable_files_done', $eventdata);
+        // Trigger assessable_submitted event to show files are complete.
+        $params = array(
+            'context' => $this->context,
+            'objectid' => $submission->id,
+            'other' => array(
+                'assignmentid' => $this->assignment->id,
+                'submission_editable' => false
+            )
+        );
+        $event = \assignment_upload\event\assessable_submitted::create($params);
+        $event->trigger();
 
         if ($forcemode==null) {
             redirect($returnurl->out(false));
diff --git a/mod/assignment/type/upload/classes/event/assessable_submitted.php b/mod/assignment/type/upload/classes/event/assessable_submitted.php
new file mode 100644 (file)
index 0000000..0cd2288
--- /dev/null
@@ -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/>.
+
+/**
+ * assignment_submitted assessable uploaded event.
+ *
+ * @package    assignment_submitted
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace assignment_submitted\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * assignment_submitted assessable uploaded event class.
+ *
+ * @package    assignment_submitted
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class assessable_submitted extends \core\event\assessable_submitted {
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User {$this->userid} has submitted the upload submission {$this->objectid}.";
+    }
+
+    /**
+     * Legacy event data if get_legacy_eventname() is not empty.
+     *
+     * @return stdClass
+     */
+    protected function get_legacy_eventdata() {
+        $eventdata = new \stdClass();
+        $eventdata->modulename   = 'assignment';
+        $eventdata->cmid         = $this->context->instanceid;
+        $eventdata->itemid       = $this->objectid;
+        $eventdata->courseid     = $this->courseid;
+        $eventdata->userid       = $this->userid;
+        return $eventdata;
+    }
+
+    /**
+     * Return the legacy event name.
+     *
+     * @return string
+     */
+    public static function get_legacy_eventname() {
+        return 'assessable_files_done';
+    }
+
+    /**
+     * Get legacy log data.
+     *
+     * @return array
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'assignment', 'upload', 'view.php?a='.$this->other['assignmentid'],
+            $this->other['assignmentid'], $this->context->instanceid);
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return \lang_string
+     */
+    public static function get_name() {
+        return new \lang_string('event_assessable_submitted', 'assignment_submitted');
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/assignment/view.php', array('id' => $this->context->instanceid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        parent::init();
+        $this->data['objecttable'] = 'assignment_submissions';
+    }
+
+    /**
+     * Custom validation
+     *
+     * @throws coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['submission_editable'])) {
+            throw new coding_exception('Other must contain the key submission_editable.');
+        }
+    }
+}
diff --git a/mod/assignment/type/upload/classes/event/assessable_uploaded.php b/mod/assignment/type/upload/classes/event/assessable_uploaded.php
new file mode 100644 (file)
index 0000000..ff7f832
--- /dev/null
@@ -0,0 +1,142 @@
+<?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/>.
+
+/**
+ * assignment_upload assessable uploaded event.
+ *
+ * @package    assignment_upload
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace assignment_upload\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * assignment_upload assessable uploaded event class.
+ *
+ * @package    assignment_upload
+ * @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 files.
+     *
+     * @var array
+     */
+    protected $legacyfiles;
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User {$this->userid} has uploaded a file in submission {$this->objectid}.";
+    }
+
+    /**
+     * Legacy event data if get_legacy_eventname() is not empty.
+     *
+     * @return stdClass
+     */
+    protected function get_legacy_eventdata() {
+        $eventdata = new \stdClass();
+        $eventdata->modulename   = 'assignment';
+        $eventdata->cmid         = $this->context->instanceid;
+        $eventdata->itemid       = $this->objectid;
+        $eventdata->courseid     = $this->courseid;
+        $eventdata->userid       = $this->userid;
+        if ($this->get_legacy_files()) {
+            $eventdata->files    = $this->get_legacy_files(); // This is depreceated - please use pathnamehashes instead!
+        }
+        $eventdata->pathnamehashes = $this->other['pathnamehashes'];
+        return $eventdata;
+    }
+
+    /**
+     * Return the legacy event name.
+     *
+     * @return string
+     */
+    public static function get_legacy_eventname() {
+        return 'assessable_file_uploaded';
+    }
+
+    /**
+     * Get legacy log data.
+     *
+     * @return array
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'assignment', 'upload', 'view.php?a=' . $this->other['assignmentid'],
+            $this->other['assignmentid'], $this->context->id);
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return \lang_string
+     */
+    public static function get_name() {
+        return new \lang_string('event_assessable_uploaded', 'assignment_upload');
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/assignment/view.php', array('id' => $this->context->instanceid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        parent::init();
+        $this->data['objecttable'] = 'assignment_submissions';
+    }
+
+    /**
+     * Set legacy files.
+     *
+     * @param array $files
+     * @return void
+     */
+    public function set_legacy_files($files) {
+        $this->legacyfiles = $files;
+    }
+
+    /**
+     * Custom validation
+     *
+     * @throws coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['triggeredfrom'])) {
+            throw new coding_exception('triggeredfrom must be set in $other');
+        }
+    }
+}
index cd69e73..e767b91 100644 (file)
@@ -23,4 +23,6 @@
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+$string['event_assessable_submitted'] = 'An upload submission has been submitted.';
+$string['event_assessable_uploaded'] = 'A file has been uploaded.';
 $string['pluginname'] = 'Upload';
diff --git a/mod/forum/classes/event/assessable_uploaded.php b/mod/forum/classes/event/assessable_uploaded.php
new file mode 100644 (file)
index 0000000..408547c
--- /dev/null
@@ -0,0 +1,118 @@
+<?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/>.
+
+/**
+ * mod_forum assessable uploaded event.
+ *
+ * @package    mod_forum
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * mod_forum assessable uploaded event class.
+ *
+ * @package    mod_forum
+ * @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 description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User {$this->userid} has posted some content in the forum post {$this->objectid}.";
+    }
+
+    /**
+     * Legacy event data if get_legacy_eventname() is not empty.
+     *
+     * @return stdClass
+     */
+    protected function get_legacy_eventdata() {
+        $eventdata = new \stdClass();
+        $eventdata->modulename   = 'forum';
+        $eventdata->name         = $this->other['triggeredfrom'];
+        $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
+     */
+    public static function get_legacy_eventname() {
+        return 'assessable_content_uploaded';
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return \lang_string
+     */
+    public static function get_name() {
+        return new \lang_string('event_assessable_uploaded', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/forum/discuss.php', array('d' => $this->other['discussionid'], 'parent' => $this->objectid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        parent::init();
+        $this->data['objecttable'] = 'forum_posts';
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['discussionid'])) {
+            throw new coding_exception('discussionid must be set in $other.');
+        } else if (!isset($this->other['triggeredfrom'])) {
+            throw new coding_exception('triggeredfrom must be set in $other');
+        }
+    }
+}
index bbf4ffb..79a7bc0 100644 (file)
@@ -147,6 +147,7 @@ $string['erroremptymessage'] = 'Post message cannot be empty';
 $string['erroremptysubject'] = 'Post subject cannot be empty.';
 $string['errorenrolmentrequired'] = 'You must be enrolled in this course to access this content';
 $string['errorwhiledelete'] = 'An error occurred while deleting record.';
+$string['event_assessable_uploaded'] = 'Some content has been posted.';
 $string['everyonecanchoose'] = 'Everyone can choose to be subscribed';
 $string['everyonecannowchoose'] = 'Everyone can now choose to be subscribed';
 $string['everyoneisnowsubscribed'] = 'Everyone is now subscribed to this forum';
index 245179d..0dac55b 100644 (file)
@@ -4620,19 +4620,18 @@ function forum_trigger_content_uploaded_event($post, $cm, $name) {
     $context = context_module::instance($cm->id);
     $fs = get_file_storage();
     $files = $fs->get_area_files($context->id, 'mod_forum', 'attachment', $post->id, "timemodified", false);
-    $eventdata = new stdClass();
-    $eventdata->modulename   = 'forum';
-    $eventdata->name         = $name;
-    $eventdata->cmid         = $cm->id;
-    $eventdata->itemid       = $post->id;
-    $eventdata->courseid     = $post->course;
-    $eventdata->userid       = $post->userid;
-    $eventdata->content      = $post->message;
-    if ($files) {
-        $eventdata->pathnamehashes = array_keys($files);
-    }
-    events_trigger('assessable_content_uploaded', $eventdata);
-
+    $params = array(
+        'context' => $context,
+        'objectid' => $post->id,
+        'other' => array(
+            'content' => $post->message,
+            'discussionid' => $post->discussion,
+            'pathnamehashes' => array_keys($files),
+            'triggeredfrom' => $name,
+        )
+    );
+    $event = \mod_forum\event\assessable_uploaded::create($params);
+    $event->trigger();
     return true;
 }
 
diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php
new file mode 100644 (file)
index 0000000..7845091
--- /dev/null
@@ -0,0 +1,78 @@
+<?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/>.
+
+/**
+ * The module forums tests
+ *
+ * @package    mod_forum
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+class mod_forum_lib_testcase extends advanced_testcase {
+
+    public function test_forum_trigger_content_uploaded_event() {
+        $this->resetAfterTest();
+
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+        $context = context_module::instance($forum->cmid);
+
+        $this->setUser($user->id);
+        $fakepost = (object) array('id' => 123, 'message' => 'Yay!', 'discussion' => 100);
+        $cm = get_coursemodule_from_instance('forum', $forum->cmid);
+
+        $fs = get_file_storage();
+        $dummy = (object) array(
+            'contextid' => $context->id,
+            'component' => 'mod_forum',
+            'filearea' => 'attachment',
+            'itemid' => $fakepost->id,
+            'filepath' => '/',
+            'filename' => 'myassignmnent.pdf'
+        );
+        $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
+
+        $data = new stdClass();
+        $sink = $this->redirectEvents();
+        forum_trigger_content_uploaded_event($fakepost, $cm, 'some triggered from value');
+        $events = $sink->get_events();
+
+        $this->assertCount(1, $events);
+        $event = reset($events);
+        $this->assertInstanceOf('\mod_forum\event\assessable_uploaded', $event);
+        $this->assertEquals($context->id, $event->contextid);
+        $this->assertEquals($fakepost->id, $event->objectid);
+        $this->assertEquals($fakepost->message, $event->other['content']);
+        $this->assertEquals($fakepost->discussion, $event->other['discussionid']);
+        $this->assertCount(1, $event->other['pathnamehashes']);
+        $this->assertEquals($fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
+        $expected = new stdClass();
+        $expected->modulename = 'forum';
+        $expected->name = 'some triggered from value';
+        $expected->cmid = $forum->cmid;
+        $expected->itemid = $fakepost->id;
+        $expected->courseid = $course->id;
+        $expected->userid = $user->id;
+        $expected->content = $fakepost->message;
+        $expected->pathnamehashes = array($fi->get_pathnamehash());
+        $this->assertEventLegacyData($expected, $event);
+    }
+
+}
diff --git a/mod/workshop/classes/event/assessable_uploaded.php b/mod/workshop/classes/event/assessable_uploaded.php
new file mode 100644 (file)
index 0000000..c7907f9
--- /dev/null
@@ -0,0 +1,130 @@
+<?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/>.
+
+/**
+ * mod_workshop assessable uploaded event.
+ *
+ * @package    mod_workshop
+ * @copyright  2013 Frédéric Massart
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_workshop\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * mod_workshop assessable uploaded event class.
+ *
+ * @package    mod_workshop
+ * @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 log data.
+     *
+     * @var array
+     */
+    protected $legacylogdata = null;
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User {$this->userid} has uploaded the submission {$this->objectid}.";
+    }
+
+    /**
+     * Legacy event data if get_legacy_eventname() is not empty.
+     *
+     * @return stdClass
+     */
+    protected function get_legacy_eventdata() {
+        $eventdata = new \stdClass();
+        $eventdata->modulename   = 'workshop';
+        $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
+     */
+    public static function get_legacy_eventname() {
+        return 'assessable_content_uploaded';
+    }
+
+    /**
+     * Return the legacy log data.
+     *
+     * @return array
+     */
+    protected function get_legacy_logdata() {
+        return $this->legacylogdata;
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return \lang_string
+     */
+    public static function get_name() {
+        return new \lang_string('event_assessable_uploaded', 'mod_workshop');
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/workshop/submission.php',
+            array('cmid' => $this->context->instanceid, 'id' => $this->objectid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        parent::init();
+        $this->data['objecttable'] = 'workshop_submissions';
+    }
+
+    /**
+     * Set the legacy log data.
+     *
+     * @param array $legacylogdata
+     * @return void
+     */
+    public function set_legacy_logdata($legacylogdata) {
+        $this->legacylogdata = $legacylogdata;
+    }
+
+}
index 73fd955..9c99b1e 100644 (file)
@@ -106,6 +106,7 @@ $string['evaluation'] = 'Grading evaluation';
 $string['evaluationmethod'] = 'Grading evaluation method';
 $string['evaluationmethod_help'] = 'The grading evaluation method determines how the grade for assessment is calculated. You can let it re-calculate grades repeatedly with different settings unless you are happy with the result.';
 $string['evaluationsettings'] = 'Grading evaluation settings';
+$string['event_assessable_uploaded'] = 'A submission has been uploaded.';
 $string['example'] = 'Example submission';
 $string['exampleadd'] = 'Add example submission';
 $string['exampleassess'] = 'Assess example submission';
index e13f497..04a389c 100644 (file)
@@ -1510,8 +1510,10 @@ class workshop {
      * @param string $action to be logged
      * @param moodle_url $url absolute url as returned by {@see workshop::submission_url()} and friends
      * @param mixed $info additional info, usually id in a table
+     * @param bool $return true to return the arguments for add_to_log.
+     * @return void|array array of arguments for add_to_log if $return is true
      */
-    public function log($action, moodle_url $url = null, $info = null) {
+    public function log($action, moodle_url $url = null, $info = null, $return = false) {
 
         if (is_null($url)) {
             $url = $this->view_url();
@@ -1522,7 +1524,11 @@ class workshop {
         }
 
         $logurl = $this->log_convert_url($url);
-        add_to_log($this->course->id, 'workshop', $action, $logurl, $info, $this->cm->id);
+        $args = array($this->course->id, 'workshop', $action, $logurl, $info, $this->cm->id);
+        if ($return) {
+            return $args;
+        }
+        call_user_func_array('add_to_log', $args);
     }
 
     /**
index 3d56078..183889c 100644 (file)
@@ -187,11 +187,12 @@ if ($edit) {
         if ($workshop->phase == workshop::PHASE_ASSESSMENT) {
             $formdata->late = $formdata->late | 0x2;
         }
+        $logdata = null;
         if (is_null($submission->id)) {
             $submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
-            $workshop->log('add submission', $workshop->submission_url($submission->id), $submission->id);
+            $logdata = $workshop->log('add submission', $workshop->submission_url($submission->id), $submission->id, true);
         } else {
-            $workshop->log('update submission', $workshop->submission_url($submission->id), $submission->id);
+            $logdata = $workshop->log('update submission', $workshop->submission_url($submission->id), $submission->id, true);
             if (empty($formdata->id) or empty($submission->id) or ($formdata->id != $submission->id)) {
                 throw new moodle_exception('err_submissionid', 'workshop');
             }
@@ -211,17 +212,18 @@ if ($edit) {
         // send submitted content for plagiarism detection
         $fs = get_file_storage();
         $files = $fs->get_area_files($workshop->context->id, 'mod_workshop', 'submission_attachment', $submission->id);
-        $eventdata = new stdClass();
-        $eventdata->modulename   = 'workshop';
-        $eventdata->cmid         = $cm->id;
-        $eventdata->itemid       = $submission->id;
-        $eventdata->courseid     = $course->id;
-        $eventdata->userid       = $USER->id;
-        $eventdata->content      = $formdata->content;
-        if ($files) {
-            $eventdata->pathnamehashes = array_keys($files);
-        }
-        events_trigger('assessable_content_uploaded', $eventdata);
+
+        $params = array(
+            'context' => $workshop->context,
+            'objectid' => $submission->id,
+            'other' => array(
+                'content' => $formdata->content,
+                'files' => array_keys($files)
+            )
+        );
+        $event = \mod_workshop\event\assessable_uploaded::create($params);
+        $event->set_legacy_logdata($logdata);
+        $event->trigger();
 
         redirect($workshop->submission_url($formdata->id));
     }