Skip to content

Commit

Permalink
MDL-40061 mod_data: replaced 'add' add_to_log call with an event
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Feb 15, 2014
1 parent 45eae37 commit 76ca452
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 3 deletions.
90 changes: 90 additions & 0 deletions mod/data/classes/event/record_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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/>.

/**
* The mod_data data record created event.
*
* @property-read array $other {
* Extra information about event.
*
* @type int dataid the id of the data activity.
* }
*
* @package mod_data
* @copyright 2014 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_data\event;

defined('MOODLE_INTERNAL') || die();

class record_created extends \core\event\base {

/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'data_records';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_TEACHING;
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventrecordcreated', 'mod_data');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return 'The data record ' . $this->objectid . ' belonging to the database activity ' . $this->other['dataid'] .
' was created by the user ' . $this->userid;
}

/**
* Get the legacy event log data.
*
* @return array
*/
public function get_legacy_logdata() {
return array($this->courseid, 'data', 'add', 'view.php?d=' . $this->other['dataid'] . '&amp;rid=' . $this->objectid,
$this->other['dataid'], $this->contextinstanceid);
}

/**
* Custom validation.
*
* @throws \coding_exception when validation does not pass.
* @return void
*/
protected function validate_data() {
parent::validate_data();

if (!isset($this->other['dataid'])) {
throw new \coding_exception('The dataid must be set in $other.');
}
}
}
2 changes: 0 additions & 2 deletions mod/data/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,6 @@
}
}

add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&amp;rid=$recordid", $data->id, $cm->id);

if (!empty($datarecord->saveandview)) {
redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$recordid);
}
Expand Down
1 change: 1 addition & 0 deletions mod/data/lang/en/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
$string['eventfieldcreated'] = 'Field created';
$string['eventfielddeleted'] = 'Field deleted';
$string['eventfieldupdated'] = 'Field updated';
$string['eventrecordcreated'] = 'Record created';
$string['eventrecordupdated'] = 'Record updated';
$string['fileencoding'] = 'Encoding';
$string['entries'] = 'Entries';
Expand Down
14 changes: 13 additions & 1 deletion mod/data/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,19 @@ function data_add_record($data, $groupid=0){
} else {
$record->approved = 0;
}
return $DB->insert_record('data_records', $record);
$record->id = $DB->insert_record('data_records', $record);

// Trigger an event for creating this record.
$event = \mod_data\event\record_created::create(array(
'objectid' => $record->id,
'context' => $context,
'other' => array(
'dataid' => $data->id
)
));
$event->trigger();

return $record->id;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions mod/data/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,33 @@ public function test_field_deleted() {
$this->assertEventLegacyLogData($expected, $event);
}

/**
* Test the record created event.
*/
public function test_record_created() {
// Create a course we are going to add a data module to.
$course = $this->getDataGenerator()->create_course();

// The generator used to create a data module.
$generator = $this->getDataGenerator()->get_plugin_generator('mod_data');

// Create a data module.
$data = $generator->create_instance(array('course' => $course->id));

// Trigger and capture the event for creating the record.
$sink = $this->redirectEvents();
$recordid = data_add_record($data);
$events = $sink->get_events();
$event = reset($events);

// Check that the event data is valid.
$this->assertInstanceOf('\mod_data\event\record_created', $event);
$this->assertEquals(context_module::instance($data->cmid), $event->get_context());
$expected = array($course->id, 'data', 'add', 'view.php?d=' . $data->id . '&amp;rid=' . $recordid,
$data->id, $data->cmid);
$this->assertEventLegacyLogData($expected, $event);
}

/**
* Test the record updated event.
*
Expand Down

0 comments on commit 76ca452

Please sign in to comment.