Skip to content

Commit

Permalink
Merge branch 'MDL-65922-master' of https://github.com/dmitriim/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jun 24, 2019
2 parents 4a4a3e0 + f82931c commit c5390ec
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 76 deletions.
146 changes: 85 additions & 61 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2325,58 +2325,74 @@ public function test_course_integrity_check() {
* 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());

// 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);

$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);
foreach ($events as $event) {
if ($event instanceof \core\event\course_module_created) {
$eventscount++;

$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());

// 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);
}

/**
Expand Down Expand Up @@ -2455,36 +2471,44 @@ public function test_course_module_updated_event() {
$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());

// 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);
foreach ($events as $event) {
if ($event instanceof \core\event\course_module_updated) {
$eventscount++;

$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());

// 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);
}
}

$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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions lang/en/grades.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
$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';
Expand Down
70 changes: 70 additions & 0 deletions lib/classes/event/grade_item_updated.php
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
* @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 <[email protected]>
* @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 . "'.";
}

}
9 changes: 8 additions & 1 deletion lib/grade/grade_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,14 @@ public function update($source=null) {
$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;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions lib/grade/tests/grade_item_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function test_grade_item() {
$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() {
Expand Down Expand Up @@ -1002,6 +1003,7 @@ protected function sub_test_grade_item_created_event() {

$event = reset($result);
$this->assertEventContextNotUsed($event);
$this->assertInstanceOf('core\event\grade_item_created', $event);

$eventgradeitem = $event->get_grade_item();

Expand All @@ -1011,4 +1013,41 @@ protected function sub_test_grade_item_created_event() {
$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']);
}
}
35 changes: 21 additions & 14 deletions mod/assign/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,27 @@ public function test_identities_revealed() {
$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();
}

Expand Down

0 comments on commit c5390ec

Please sign in to comment.