Skip to content

Commit

Permalink
MDL-40053 events: Replace add_to_log() with new events in core_notes
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitagarwal committed Oct 8, 2013
1 parent 1b486fc commit c4f9401
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/classes/event/note_updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
19 changes: 13 additions & 6 deletions notes/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@

/// 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
Expand All @@ -75,6 +69,19 @@
}
$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));
Expand Down
62 changes: 45 additions & 17 deletions notes/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit c4f9401

Please sign in to comment.