diff --git a/mod/lesson/classes/event/essay_assessed.php b/mod/lesson/classes/event/essay_assessed.php new file mode 100644 index 00000000000..19d56dbc83a --- /dev/null +++ b/mod/lesson/classes/event/essay_assessed.php @@ -0,0 +1,112 @@ +. + +/** + * mod_lesson essay assessed event. + * + * @package mod_lesson + * @copyright 2014 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_lesson\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * mod_lesson essay assessed event class. + * + * @property-read array $other { + * Extra information about the event. + * + * @type int lessonid The ID of the lesson. + * @type int attemptid The ID for the attempt. + * } + * + * @package mod_lesson + * @copyright 2014 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class essay_assessed extends \core\event\base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'u'; + $this->data['level'] = self::LEVEL_TEACHING; + $this->data['objecttable'] = 'lesson_grades'; + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return 'User ' . $this->userid . ' has marked the essay ' . $this->other['attemptid'] + . ' and recorded a mark ' . $this->objectid . ' in the lesson ' . $this->other['lessonid'] . '.'; + } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $lesson = $this->get_record_snapshot('lesson', $this->other['lessonid']); + return array($this->courseid, 'lesson', 'update grade', 'essay.php?id=' . + $this->context->instanceid, $lesson->name, $this->context->instanceid); + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventessayassessed', 'mod_lesson'); + } + + /** + * Get URL related to the action + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/lesson/essay.php', array('id' => $this->context->instanceid)); + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + if (!isset($this->relateduserid)) { + throw new \coding_exception('relateduserid is a mandatory property.'); + } + if (!isset($this->other['lessonid'])) { + throw new \coding_exception('lessonid is a mandatory property.'); + } + if (!isset($this->other['attemptid'])) { + throw new \coding_exception('attemptid is a mandatory property.'); + } + } +} diff --git a/mod/lesson/essay.php b/mod/lesson/essay.php index ac8816fc53a..44e972bf199 100644 --- a/mod/lesson/essay.php +++ b/mod/lesson/essay.php @@ -34,7 +34,8 @@ $mode = optional_param('mode', 'display', PARAM_ALPHA); $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST); $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); -$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST)); +$dblesson = $DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST); +$lesson = new lesson($dblesson); require_login($course, false, $cm); $context = context_module::instance($cm->id); @@ -129,8 +130,20 @@ switch ($mode) { $updategrade->id = $grade->id; $updategrade->grade = $gradeinfo->grade; $DB->update_record('lesson_grades', $updategrade); - // Log it - add_to_log($course->id, 'lesson', 'update grade', "essay.php?id=$cm->id", $lesson->name, $cm->id); + + $params = array( + 'context' => $context, + 'objectid' => $grade->id, + 'courseid' => $course->id, + 'relateduserid' => $attempt->userid, + 'other' => array( + 'lessonid' => $lesson->id, + 'attemptid' => $attemptid + ) + ); + $event = \mod_lesson\event\essay_assessed::create($params); + $event->add_record_snapshot('lesson', $dblesson); + $event->trigger(); $lesson->add_message(get_string('changessaved'), 'notifysuccess'); diff --git a/mod/lesson/lang/en/lesson.php b/mod/lesson/lang/en/lesson.php index 98e6f0c4759..971381d192c 100644 --- a/mod/lesson/lang/en/lesson.php +++ b/mod/lesson/lang/en/lesson.php @@ -171,6 +171,7 @@ $string['essayemailmessage2'] = '

Essay prompt:

{$a->question}lesson->properties()->id, $this->lesson->properties()->cmid); $this->assertEventLegacyLogData($expected, $event); } + + /** + * Test the essay assessed event. + * + * There is no external API for assessing an essay, so the unit test will simply + * create and trigger the event and ensure the legacy log data is returned as expected. + */ + public function test_essay_assessed() { + // Create an essay assessed event + $gradeid = 5; + $attemptid = 7; + $event = \mod_lesson\event\essay_assessed::create(array( + 'objectid' => $gradeid, + 'relateduserid' => 3, + 'context' => context_module::instance($this->lesson->properties()->cmid), + 'courseid' => $this->course->id, + 'other' => array( + 'lessonid' => $this->lesson->id, + 'attemptid' => $attemptid + ) + )); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\mod_lesson\event\essay_assessed', $event); + $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context()); + $expected = array($this->course->id, 'lesson', 'update grade', 'essay.php?id=' . $this->lesson->properties()->cmid, + $this->lesson->name, $this->lesson->properties()->cmid); + $this->assertEventLegacyLogData($expected, $event); + } }