Merge branch 'wip-MDL-43294-master' of git://github.com/abgreeve/moodle

This commit is contained in:
Damyon Wiese
2014-01-21 13:17:05 +08:00
4 changed files with 164 additions and 3 deletions
+112
View File
@@ -0,0 +1,112 @@
<?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_lesson essay assessed event.
*
* @package mod_lesson
* @copyright 2014 Adrian Greeve <[email protected]>
* @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 <[email protected]>
* @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.');
}
}
}
+16 -3
View File
@@ -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');
+1
View File
@@ -171,6 +171,7 @@ $string['essayemailmessage2'] = '<p>Essay prompt:<blockquote>{$a->question}</blo
$string['essayemailsubject'] = 'Your grade for {$a} question';
$string['essays'] = 'Essays';
$string['essayscore'] = 'Essay score';
$string['eventessayassessed'] = 'Essay assessed';
$string['eventessayattemptviewed'] = 'Essay attempt viewed';
$string['eventhighscoreadded'] = 'Highscore added';
$string['eventhighscoresviewed'] = 'Highscores viewed';
+35
View File
@@ -194,4 +194,39 @@ class mod_lesson_events_testcase extends advanced_testcase {
$this->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);
}
}