diff --git a/mod/data/classes/event/field_deleted.php b/mod/data/classes/event/field_deleted.php new file mode 100644 index 00000000000..800453740e7 --- /dev/null +++ b/mod/data/classes/event/field_deleted.php @@ -0,0 +1,94 @@ +. + +/** + * 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 + * @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.'); + } + } +} diff --git a/mod/data/field.php b/mod/data/field.php index a24d6284139..eb6addde47d 100644 --- a/mod/data/field.php +++ b/mod/data/field.php @@ -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'); } diff --git a/mod/data/lang/en/data.php b/mod/data/lang/en/data.php index d0d911095f0..9680bde8984 100644 --- a/mod/data/lang/en/data.php +++ b/mod/data/lang/en/data.php @@ -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'; diff --git a/mod/data/lib.php b/mod/data/lib.php index 8123cc85c50..24e0ed3e36a 100644 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -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; } diff --git a/mod/data/tests/events_test.php b/mod/data/tests/events_test.php new file mode 100644 index 00000000000..a5ab48f32bf --- /dev/null +++ b/mod/data/tests/events_test.php @@ -0,0 +1,76 @@ +. + +/** + * Events tests. + * + * @package mod_data + * @category test + * @copyright 2014 Mark Nelson + * @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); + } +}