From c4f9401ce4841cf73a1b0b920c2c4fadf3d93b8a Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Wed, 2 Oct 2013 15:45:55 +0800 Subject: [PATCH] MDL-40053 events: Replace add_to_log() with new events in core_notes --- lib/classes/event/note_updated.php | 2 +- notes/index.php | 19 ++++++--- notes/lib.php | 62 ++++++++++++++++++++++-------- 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/lib/classes/event/note_updated.php b/lib/classes/event/note_updated.php index 19d967d6e15..df3ac78c21c 100644 --- a/lib/classes/event/note_updated.php +++ b/lib/classes/event/note_updated.php @@ -42,7 +42,7 @@ class note_updated extends \core\event\base { */ protected function init() { $this->data['objecttable'] = 'post'; - $this->data['crud'] = 'c'; + $this->data['crud'] = 'u'; $this->data['level'] = self::LEVEL_OTHER; } diff --git a/notes/index.php b/notes/index.php index 62c01b9979d..fc94a7882ef 100644 --- a/notes/index.php +++ b/notes/index.php @@ -61,12 +61,6 @@ if ($userid) { /// require login to access notes require_login($course); -add_to_log($courseid, 'notes', 'view', 'index.php?course='.$courseid.'&user='.$userid, 'view notes'); - -if (empty($CFG->enablenotes)) { - print_error('notesdisabled', 'notes'); -} - /// output HTML if ($course->id == SITEID) { $coursecontext = context_system::instance(); // SYSTEM context @@ -75,6 +69,19 @@ if ($course->id == SITEID) { } $systemcontext = context_system::instance(); // SYSTEM context +// Trigger event. +$event = \core\event\notes_viewed::create(array( + 'courseid' => $courseid, + 'relateduserid' => $userid, + 'context' => $coursecontext, + 'other' => array('content' => 'notes') +)); +$event->trigger(); + +if (empty($CFG->enablenotes)) { + print_error('notesdisabled', 'notes'); +} + $strnotes = get_string('notes', 'notes'); if ($userid) { $PAGE->set_context(context_user::instance($user->id)); diff --git a/notes/lib.php b/notes/lib.php index ce720e37c45..1dd5241d320 100644 --- a/notes/lib.php +++ b/notes/lib.php @@ -66,7 +66,7 @@ function note_list($courseid=0, $userid=0, $state = '', $author = 0, $order='las * Retrieves a note object based on its id. * * @param int $note_id id of the note to retrieve - * @return note object + * @return stdClass object */ function note_load($note_id) { global $DB; @@ -79,13 +79,13 @@ function note_load($note_id) { * Saves a note object. The note object is passed by reference and its fields (i.e. id) * might change during the save. * - * @param note $note object to save + * @param stdClass $note object to save * @return boolean true if the object was saved; false otherwise */ function note_save(&$note) { global $USER, $DB; - // setup & clean fields + // Setup & clean fields. $note->module = 'notes'; $note->lastmodified = time(); $note->usermodified = $USER->id; @@ -95,23 +95,40 @@ function note_save(&$note) { if (empty($note->publishstate)) { $note->publishstate = NOTES_STATE_PUBLIC; } - // save data + // Save data. if (empty($note->id)) { - // insert new note + // Insert new note. $note->created = $note->lastmodified; $id = $DB->insert_record('post', $note); $note = note_load($id); - $logurl = new moodle_url('index.php', array('course'=> $note->courseid, 'user'=>$note->userid)); - $logurl->set_anchor('note-' . $id); - add_to_log($note->courseid, 'notes', 'add', $logurl, 'add note'); + // Trigger event. + $event = \core\event\note_created::create(array( + 'objectid' => $note->id, + 'courseid' => $note->courseid, + 'relateduserid' => $note->userid, + 'userid' => $note->usermodified, + 'context' => context_course::instance($note->courseid), + 'other' => array('publishstate' => $note->publishstate) + )); + $event->add_record_snapshot('post', $note); + $event->trigger(); } else { - // update old note + // Update old note. $DB->update_record('post', $note); $note = note_load($note->id); - $logurl = new moodle_url('index.php', array('course'=> $note->courseid, 'user'=>$note->userid)); - $logurl->set_anchor('note-' . $note->id); - add_to_log($note->courseid, 'notes', 'update', $logurl , 'update note'); + + // Trigger event. + $event = \core\event\note_updated::create(array( + 'objectid' => $note->id, + 'courseid' => $note->courseid, + 'relateduserid' => $note->userid, + 'userid' => $note->usermodified, + 'context' => context_course::instance($note->courseid), + 'other' => array('publishstate' => $note->publishstate) + )); + $event->add_record_snapshot('post', $note); + $event->trigger(); } unset($note->module); return true; @@ -127,12 +144,23 @@ function note_delete($note) { global $DB; if (is_int($note)) { $note = note_load($note); - debugging('Warning: providing note_delete with a note object would improve performance.',DEBUG_DEVELOPER); + debugging('Warning: providing note_delete with a note object would improve performance.', DEBUG_DEVELOPER); } - $logurl = new moodle_url('index.php', array('course'=> $note->courseid, 'user'=>$note->userid)); - $logurl->set_anchor('note-' . $note->id); - add_to_log($note->courseid, 'notes', 'delete', $logurl, 'delete note'); - return $DB->delete_records('post', array('id'=>$note->id, 'module'=>'notes')); + $return = $DB->delete_records('post', array('id' => $note->id, 'module' => 'notes')); + + // Trigger event. + $event = \core\event\note_deleted::create(array( + 'objectid' => $note->id, + 'courseid' => $note->courseid, + 'relateduserid' => $note->userid, + 'userid' => $note->usermodified, + 'context' => context_course::instance($note->courseid), + 'other' => array('publishstate' => $note->publishstate) + )); + $event->add_record_snapshot('post', $note); + $event->trigger(); + + return $return; } /**