Merge branch 'MDL-65922-master' of https://github.com/dmitriim/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2019-06-25 00:35:28 +02:00
commit c5390ec39c
6 changed files with 221 additions and 73 deletions

View File

@ -2325,58 +2325,74 @@ class core_course_courselib_testcase extends advanced_testcase {
* Tests for event related to course module creation.
*/
public function test_course_module_created_event() {
global $USER, $DB;
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
// Create an assign module.
$sink = $this->redirectEvents();
$modinfo = $this->create_specific_module_test('assign');
$course = $this->getDataGenerator()->create_course();
$module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
$events = $sink->get_events();
$event = array_pop($events);
$cm = get_coursemodule_from_id('assign', $modinfo->coursemodule, 0, false, MUST_EXIST);
$mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
$eventscount = 0;
// Validate event data.
$this->assertInstanceOf('\core\event\course_module_created', $event);
$this->assertEquals($cm->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals('course_modules', $event->objecttable);
$url = new moodle_url('/mod/assign/view.php', array('id' => $cm->id));
$this->assertEquals($url, $event->get_url());
foreach ($events as $event) {
if ($event instanceof \core\event\course_module_created) {
$eventscount++;
// Test legacy data.
$this->assertSame('mod_created', $event->get_legacy_eventname());
$eventdata = new stdClass();
$eventdata->modulename = 'assign';
$eventdata->name = $mod->name;
$eventdata->cmid = $cm->id;
$eventdata->courseid = $cm->course;
$eventdata->userid = $USER->id;
$this->assertEventLegacyData($eventdata, $event);
$this->assertEquals($module->cmid, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals('course_modules', $event->objecttable);
$url = new moodle_url('/mod/assign/view.php', array('id' => $module->cmid));
$this->assertEquals($url, $event->get_url());
$arr = array(
array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
array($cm->course, "assign", "add", "view.php?id=$cm->id", $cm->instance, $cm->id)
);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
// Test legacy data.
$this->assertSame('mod_created', $event->get_legacy_eventname());
$eventdata = new stdClass();
$eventdata->modulename = 'assign';
$eventdata->name = $module->name;
$eventdata->cmid = $module->cmid;
$eventdata->courseid = $module->course;
$eventdata->userid = $USER->id;
$this->assertEventLegacyData($eventdata, $event);
$arr = array(
array($module->course, "course", "add mod", "../mod/assign/view.php?id=$module->cmid", "assign $module->id"),
array($module->course, "assign", "add", "view.php?id=$module->cmid", $module->id, $module->cmid)
);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
}
}
// Only one \core\event\course_module_created event should be triggered.
$this->assertEquals(1, $eventscount);
// Let us see if duplicating an activity results in a nice course module created event.
$sink->clear();
$course = get_course($mod->course);
$course = get_course($module->course);
$cm = get_coursemodule_from_id('assign', $module->cmid, 0, false, MUST_EXIST);
$newcm = duplicate_module($course, $cm);
$events = $sink->get_events();
$event = array_pop($events);
$eventscount = 0;
$sink->close();
// Validate event data.
$this->assertInstanceOf('\core\event\course_module_created', $event);
$this->assertEquals($newcm->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($course->id, $event->courseid);
$url = new moodle_url('/mod/assign/view.php', array('id' => $newcm->id));
$this->assertEquals($url, $event->get_url());
foreach ($events as $event) {
if ($event instanceof \core\event\course_module_created) {
$eventscount++;
// Validate event data.
$this->assertInstanceOf('\core\event\course_module_created', $event);
$this->assertEquals($newcm->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($course->id, $event->courseid);
$url = new moodle_url('/mod/assign/view.php', array('id' => $newcm->id));
$this->assertEquals($url, $event->get_url());
}
}
// Only one \core\event\course_module_created event should be triggered.
$this->assertEquals(1, $eventscount);
}
/**
@ -2455,36 +2471,44 @@ class core_course_courselib_testcase extends advanced_testcase {
$sink = $this->redirectEvents();
$modinfo = $this->update_specific_module_test('forum');
$events = $sink->get_events();
$event = array_pop($events);
$eventscount = 0;
$sink->close();
$cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
$mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
// Validate event data.
$this->assertInstanceOf('\core\event\course_module_updated', $event);
$this->assertEquals($cm->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals('course_modules', $event->objecttable);
$url = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
$this->assertEquals($url, $event->get_url());
foreach ($events as $event) {
if ($event instanceof \core\event\course_module_updated) {
$eventscount++;
// Test legacy data.
$this->assertSame('mod_updated', $event->get_legacy_eventname());
$eventdata = new stdClass();
$eventdata->modulename = 'forum';
$eventdata->name = $mod->name;
$eventdata->cmid = $cm->id;
$eventdata->courseid = $cm->course;
$eventdata->userid = $USER->id;
$this->assertEventLegacyData($eventdata, $event);
$this->assertEquals($cm->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals('course_modules', $event->objecttable);
$url = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
$this->assertEquals($url, $event->get_url());
$arr = array(
array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"),
array($cm->course, "forum", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
// Test legacy data.
$this->assertSame('mod_updated', $event->get_legacy_eventname());
$eventdata = new stdClass();
$eventdata->modulename = 'forum';
$eventdata->name = $mod->name;
$eventdata->cmid = $cm->id;
$eventdata->courseid = $cm->course;
$eventdata->userid = $USER->id;
$this->assertEventLegacyData($eventdata, $event);
$arr = array(
array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"),
array($cm->course, "forum", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
}
}
// Only one \core\event\course_module_updated event should be triggered.
$this->assertEquals(1, $eventscount);
}
/**

View File

@ -195,6 +195,7 @@ $string['errorupdatinggradecategoryaggregation'] = 'Error updating the aggregati
$string['errorupdatinggradeitemaggregationcoef'] = 'Error updating the aggregation coefficient (weight or extra credit) of grade item ID {$a->id}';
$string['eventgradedeleted'] = 'Grade deleted';
$string['eventgradeitemcreated'] = 'Grade item created';
$string['eventgradeitemupdated'] = 'Grade item updated';
$string['eventgradelettercreated'] = 'Grade letter created';
$string['eventgradeletterdeleted'] = 'Grade letter deleted';
$string['eventgradeletterupdated'] = 'Grade letter updated';

View File

@ -0,0 +1,70 @@
<?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/>.
/**
* Grade item updated event.
*
* @package core
* @copyright 2019 Dmitrii Metelkin <dmitriim@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
/**
* Grade item updated event class.
*
* @package core
* @since Moodle 3.8
* @copyright 2019 Dmitrii Metelkin <dmitriim@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_item_updated extends grade_item_created {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'grade_items';
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventgradeitemupdated', 'core_grades');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '" . $this->userid . "' updated a grade item with id '" . $this->objectid . "'" .
" of type '" . $this->other['itemtype'] . "' and name '" . $this->other['itemname'] . "'" .
" in the course with the id '" . $this->courseid . "'.";
}
}

View File

@ -304,7 +304,14 @@ class grade_item extends grade_object {
$this->aggregationcoef = grade_floatval($this->aggregationcoef);
$this->aggregationcoef2 = grade_floatval($this->aggregationcoef2);
return parent::update($source);
$result = parent::update($source);
if ($result) {
$event = \core\event\grade_item_updated::create_from_grade_item($this);
$event->trigger();
}
return $result;
}
/**

View File

@ -68,6 +68,7 @@ class core_grade_item_testcase extends grade_base_testcase {
$this->sub_test_grade_item_can_control_visibility();
$this->sub_test_grade_item_fix_sortorder();
$this->sub_test_grade_item_created_event();
$this->sub_test_grade_item_updated_event();
}
protected function sub_test_grade_item_construct() {
@ -1002,6 +1003,7 @@ class core_grade_item_testcase extends grade_base_testcase {
$event = reset($result);
$this->assertEventContextNotUsed($event);
$this->assertInstanceOf('core\event\grade_item_created', $event);
$eventgradeitem = $event->get_grade_item();
@ -1011,4 +1013,41 @@ class core_grade_item_testcase extends grade_base_testcase {
$this->assertEquals($gradeitem->itemtype, $event->other['itemtype']);
$this->assertEquals($gradeitem->itemmodule, $event->other['itemmodule']);
}
/**
* Test that grade item event triggered when a grade item is updated.
*/
protected function sub_test_grade_item_updated_event() {
$gradeitem = new grade_item();
$gradeitem->courseid = $this->courseid;
$gradeitem->categoryid = $this->grade_categories[1]->id;
$gradeitem->itemname = 'unittestgradeitem4';
$gradeitem->itemtype = 'mod';
$gradeitem->itemmodule = 'quiz';
$gradeitem->iteminfo = 'Grade item used for unit testing';
$gradeitem->insert();
$sink = $this->redirectEvents();
$gradeitem->itemname = 'updatedname';
$gradeitem->update();
$result = $sink->get_events();
$sink->close();
$this->assertCount(1, $result);
$event = reset($result);
$this->assertInstanceOf('core\event\grade_item_updated', $event);
$this->assertEventContextNotUsed($event);
$eventgradeitem = $event->get_grade_item();
$this->assertInstanceOf('grade_item', $eventgradeitem);
$this->assertEquals($gradeitem->id, $eventgradeitem->id);
$this->assertEquals($gradeitem->itemtype, $event->other['itemtype']);
$this->assertEquals($gradeitem->itemmodule, $event->other['itemmodule']);
$this->assertEquals('updatedname', $event->other['itemname']);
}
}

View File

@ -278,20 +278,27 @@ class assign_events_testcase extends advanced_testcase {
$assign->reveal_identities();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\mod_assign\event\identities_revealed', $event);
$this->assertEquals($assign->get_context(), $event->get_context());
$this->assertEquals($assign->get_instance()->id, $event->objectid);
$expected = array(
$assign->get_course()->id,
'assign',
'reveal identities',
'view.php?id=' . $assign->get_course_module()->id,
get_string('revealidentities', 'assign'),
$assign->get_course_module()->id
);
$this->assertEventLegacyLogData($expected, $event);
$eventscount = 0;
foreach ($events as $event) {
if ($event instanceof \mod_assign\event\identities_revealed) {
$eventscount++;
$this->assertInstanceOf('\mod_assign\event\identities_revealed', $event);
$this->assertEquals($assign->get_context(), $event->get_context());
$this->assertEquals($assign->get_instance()->id, $event->objectid);
$expected = array(
$assign->get_course()->id,
'assign',
'reveal identities',
'view.php?id=' . $assign->get_course_module()->id,
get_string('revealidentities', 'assign'),
$assign->get_course_module()->id
);
$this->assertEventLegacyLogData($expected, $event);
}
}
$this->assertEquals(1, $eventscount);
$sink->close();
}