mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
MDL-40061 mod_data: replaced 'fields delete' add_to_log call with an event
This commit is contained in:
parent
7ddb347cec
commit
e37c413e87
94
mod/data/classes/event/field_deleted.php
Normal file
94
mod/data/classes/event/field_deleted.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?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 field deleted event.
|
||||
*
|
||||
* @property-read array $other {
|
||||
* Extra information about event.
|
||||
*
|
||||
* @type string fieldname the name of the field.
|
||||
* @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 field_deleted extends \core\event\base {
|
||||
|
||||
/**
|
||||
* Init method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data['objecttable'] = 'data_fields';
|
||||
$this->data['crud'] = 'd';
|
||||
$this->data['edulevel'] = self::LEVEL_TEACHING;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_name() {
|
||||
return get_string('eventfielddeleted', 'mod_data');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return 'The field ' . $this->objectid . ' belonging to the data activity ' . $this->other['dataid'] . ' has been deleted.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the legacy event log data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_legacy_logdata() {
|
||||
return array($this->courseid, 'data', 'fields delete', 'field.php?d=' . $this->other['dataid'],
|
||||
$this->other['fieldname'], $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['fieldname'])) {
|
||||
throw new \coding_exception('The fieldname must be set in $other.');
|
||||
}
|
||||
|
||||
if (!isset($this->other['dataid'])) {
|
||||
throw new \coding_exception('The dataid must be set in $other.');
|
||||
}
|
||||
}
|
||||
}
|
@ -194,9 +194,6 @@ switch ($mode) {
|
||||
$DB->update_record('data', $rec);
|
||||
}
|
||||
|
||||
add_to_log($course->id, 'data', 'fields delete',
|
||||
"field.php?d=$data->id", $field->field->name, $cm->id);
|
||||
|
||||
$displaynoticegood = get_string('fielddeleted', 'data');
|
||||
}
|
||||
|
||||
|
@ -119,6 +119,7 @@ $string['editordisable'] = 'Disable editor';
|
||||
$string['editorenable'] = 'Enable editor';
|
||||
$string['emptyadd'] = 'The Add template is empty, generating a default form...';
|
||||
$string['emptyaddform'] = 'You did not fill out any fields!';
|
||||
$string['eventfielddeleted'] = 'Field deleted';
|
||||
$string['fileencoding'] = 'Encoding';
|
||||
$string['entries'] = 'Entries';
|
||||
$string['entrieslefttoadd'] = 'You must add {$a->entriesleft} more entry/entries in order to complete this activity';
|
||||
|
@ -215,9 +215,25 @@ class data_field_base { // Base class for Database Field Types (see field/*/
|
||||
global $DB;
|
||||
|
||||
if (!empty($this->field->id)) {
|
||||
// Get the field before we delete it.
|
||||
$field = $DB->get_record('data_fields', array('id' => $this->field->id));
|
||||
|
||||
$this->delete_content();
|
||||
$DB->delete_records('data_fields', array('id'=>$this->field->id));
|
||||
|
||||
// Trigger an event for deleting this field.
|
||||
$event = \mod_data\event\field_deleted::create(array(
|
||||
'objectid' => $this->field->id,
|
||||
'context' => $this->context,
|
||||
'other' => array(
|
||||
'fieldname' => $this->field->name,
|
||||
'dataid' => $this->data->id
|
||||
)
|
||||
));
|
||||
$event->add_record_snapshot('data_fields', $field);
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
76
mod/data/tests/events_test.php
Normal file
76
mod/data/tests/events_test.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Events tests.
|
||||
*
|
||||
* @package mod_data
|
||||
* @category test
|
||||
* @copyright 2014 Mark Nelson <markn@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
class mod_data_events_testcase extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test set up.
|
||||
*
|
||||
* This is executed before running any test in this file.
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->resetAfterTest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the field deleted event.
|
||||
*/
|
||||
public function test_field_deleted() {
|
||||
$this->setAdminUser();
|
||||
|
||||
// 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));
|
||||
|
||||
// Now we want to create a field.
|
||||
$field = data_get_field_new('text', $data);
|
||||
$fielddata = new stdClass();
|
||||
$fielddata->name = 'Test';
|
||||
$fielddata->description = 'Test description';
|
||||
$field->define_field($fielddata);
|
||||
$field->insert_field();
|
||||
|
||||
// Trigger and capture the event for deleting the field.
|
||||
$sink = $this->redirectEvents();
|
||||
$field->delete_field();
|
||||
$events = $sink->get_events();
|
||||
$event = reset($events);
|
||||
|
||||
// Check that the event data is valid.
|
||||
$this->assertInstanceOf('\mod_data\event\field_deleted', $event);
|
||||
$this->assertEquals(context_module::instance($data->cmid), $event->get_context());
|
||||
$expected = array($course->id, 'data', 'fields delete', 'field.php?d=' . $data->id, $field->field->name, $data->cmid);
|
||||
$this->assertEventLegacyLogData($expected, $event);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user