MDL-40055 mod_assign: Replace add_to_log 'grade submission'
authorFrederic Massart <fred@moodle.com>
Fri, 9 Aug 2013 07:53:55 +0000 (15:53 +0800)
committerFrederic Massart <fred@moodle.com>
Mon, 26 Aug 2013 06:17:18 +0000 (14:17 +0800)
mod/assign/classes/event/submission_graded.php [new file with mode: 0644]
mod/assign/lang/en/assign.php
mod/assign/locallib.php
mod/assign/tests/base_test.php
mod/assign/tests/locallib_test.php

diff --git a/mod/assign/classes/event/submission_graded.php b/mod/assign/classes/event/submission_graded.php
new file mode 100644 (file)
index 0000000..1f4e5f7
--- /dev/null
@@ -0,0 +1,113 @@
+<?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 submission graded 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 submission graded 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 submission_graded extends \core\event\base {
+
+    /**
+     * Legacy log data.
+     *
+     * @var array
+     */
+    protected $legacylogdata;
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User {$this->userid} has graded the submission {$this->objectid}.";
+    }
+
+    /**
+     * 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_submission_graded', '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));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'u';
+        $this->data['level'] = 50;          // TODO MDL-41040.
+        $this->data['objecttable'] = 'assign_grades';
+    }
+
+    /**
+     * Sets the legacy event log data.
+     *
+     * @param stdClass $legacylogdata legacy log data.
+     * @return void
+     */
+    public function set_legacy_logdata($legacylogdata) {
+        $this->legacylogdata = $legacylogdata;
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        if (!isset($this->relateduserid)) {
+            throw new coding_exception('relateduserid is a mandatory property.');
+        }
+    }
+}
index f590ab9..02b4866 100644 (file)
@@ -147,6 +147,7 @@ $string['event_identities_revealed'] = 'The identities have been revealed.';
 $string['event_marker_updated'] = 'The allocated marker has been updated.';
 $string['event_statement_accepted'] = 'The user has accepted the statement of the submission.';
 $string['event_submission_duplicated'] = 'The user duplicated his submission.';
+$string['event_submission_graded'] = 'The submission has been graded.';
 $string['event_submission_locked'] = 'The submissions have been locked for a user.';
 $string['event_submission_status_updated'] = 'The status of the submission has been updated.';
 $string['event_submission_unlocked'] = 'The submissions have been unlocked for a user.';
index 4355b0f..51274f4 100644 (file)
@@ -4800,7 +4800,15 @@ class assign {
                 }
             }
 
-            $this->add_to_log('grade submission', $this->format_grade_for_log($grade));
+            $addtolog = $this->add_to_log('grade submission', $this->format_grade_for_log($grade), '', true);
+            $params = array(
+                'context' => $this->context,
+                'objectid' => $grade->id,
+                'relateduserid' => $userid
+            );
+            $event = \mod_assign\event\submission_graded::create($params);
+            $event->set_legacy_logdata($addtolog);
+            $event->trigger();
         }
 
         return get_string('quickgradingchangessaved', 'assign');
@@ -5949,9 +5957,16 @@ class assign {
         }
         $this->update_grade($grade);
         $this->notify_grade_modified($grade);
-        $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
 
-        $this->add_to_log('grade submission', $this->format_grade_for_log($grade));
+        $addtolog = $this->add_to_log('grade submission', $this->format_grade_for_log($grade), '', true);
+        $params = array(
+            'context' => $this->context,
+            'objectid' => $grade->id,
+            'relateduserid' => $userid
+        );
+        $event = \mod_assign\event\submission_graded::create($params);
+        $event->set_legacy_logdata($addtolog);
+        $event->trigger();
     }
 
 
index 17db4f2..cae9553 100644 (file)
@@ -263,6 +263,13 @@ class testable_assign extends assign {
         return parent::process_lock($userid);
     }
 
+    public function testable_process_save_quick_grades($postdata) {
+        // Ugly hack to get something into the method.
+        global $_POST;
+        $_POST = $postdata;
+        return parent::process_save_quick_grades();
+    }
+
     public function testable_process_unlock($userid = 0) {
         return parent::process_unlock($userid);
     }
index 494a271..774ca1c 100644 (file)
@@ -1210,5 +1210,67 @@ class mod_assign_locallib_testcase extends mod_assign_base_testcase {
         $this->editingteachers[0]->ignoresesskey = false;
     }
 
+    public function test_submission_graded_event() {
+        $this->setUser($this->editingteachers[0]);
+        $assign = $this->create_instance();
+
+        // Test apply_grade_to_user.
+        $sink = $this->redirectEvents();
+
+        $data = new stdClass();
+        $data->grade = '50.0';
+        $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
+        $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
+
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+        $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
+        $this->assertEquals($assign->get_context(), $event->get_context());
+        $this->assertEquals($grade->id, $event->objectid);
+        $this->assertEquals($this->students[0]->id, $event->relateduserid);
+        $expected = array(
+            $assign->get_course()->id,
+            'assign',
+            'grade submission',
+            'view.php?id=' . $assign->get_course_module()->id,
+            $assign->format_grade_for_log($grade),
+            $assign->get_course_module()->id,
+            $this->editingteachers[0]->id
+        );
+        $this->assertEventLegacyLogData($expected, $event);
+        $sink->close();
+
+        // Test process_save_quick_grades.
+        $sink = $this->redirectEvents();
+
+        $data = array(
+            'grademodified_' . $this->students[0]->id => time(),
+            'quickgrade_' . $this->students[0]->id => '60.0'
+        );
+        $assign->testable_process_save_quick_grades($data);
+        $grade = $assign->get_user_grade($this->students[0]->id, false);
+        $this->assertEquals('60.0', $grade->grade);
+
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+        $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
+        $this->assertEquals($assign->get_context(), $event->get_context());
+        $this->assertEquals($grade->id, $event->objectid);
+        $this->assertEquals($this->students[0]->id, $event->relateduserid);
+        $expected = array(
+            $assign->get_course()->id,
+            'assign',
+            'grade submission',
+            'view.php?id=' . $assign->get_course_module()->id,
+            $assign->format_grade_for_log($grade),
+            $assign->get_course_module()->id,
+            $this->editingteachers[0]->id
+        );
+        $this->assertEventLegacyLogData($expected, $event);
+        $sink->close();
+    }
+
 }