Skip to content

Commit

Permalink
MDL-40040 Cleanup some blog methods and replace add_to_log
Browse files Browse the repository at this point in the history
Cleaned up add_association()
cleaned up add_associations()
Replaced remaining add_to_log() in blogs with events
  • Loading branch information
ankitagarwal committed Dec 2, 2013
1 parent cf0116d commit 6b36411
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
12 changes: 10 additions & 2 deletions blog/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,13 @@
$bloglisting->print_entries();

echo $OUTPUT->footer();

add_to_log($courseid, 'blog', 'view', 'index.php?entryid='.$entryid.'&tagid='.@$tagid.'&tag='.$tag, 'view blog entry');
$eventparams = array(
'other' => array('entryid' => $entryid, 'tagid' => $tagid, 'userid' => $userid, 'modid' => $modid, 'groupid' => $groupid,
'search' => $search, 'fromstart' => $start)
);
if (!empty($userid)) {
$eventparams['relateduserid'] = $userid;
}
$eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid;
$event = \core\event\blog_entries_viewed::create($eventparams);
$event->trigger();
52 changes: 34 additions & 18 deletions blog/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,46 +339,62 @@ public function delete() {
}

/**
* function to add all context associations to an entry
* @param int entry - data object processed to include all 'entry' fields and extra data from the edit_form object
* Function to add all context associations to an entry.
* TODO : Remove $action in 2.9 (MDL-41330)
*
* @param string $action - This does nothing, do not use it. This is present only for Backward compatibility.
*/
public function add_associations($action='add') {
global $DB, $USER;
public function add_associations($action = null) {

if (!empty($action)) {
debugging('blog_entry->add_associations() does not accept any argument', DEBUG_DEVELOPER);
}

$this->remove_associations();

if (!empty($this->courseassoc)) {
$this->add_association($this->courseassoc, $action);
$this->add_association($this->courseassoc);
}

if (!empty($this->modassoc)) {
$this->add_association($this->modassoc, $action);
$this->add_association($this->modassoc);
}
}

/**
* add a single association for a blog entry
* @param int contextid - id of context to associate with the blog entry
* Add a single association for a blog entry
* TODO : Remove $action in 2.9 (MDL-41330)
*
* @param int $contextid - id of context to associate with the blog entry.
* @param string $action - This does nothing, do not use it. This is present only for Backward compatibility.
*/
public function add_association($contextid, $action='add') {
global $DB, $USER;
public function add_association($contextid, $action = null) {
global $DB;

if (!empty($action)) {
debugging('blog_entry->add_association() accepts only one argument', DEBUG_DEVELOPER);
}

$assocobject = new StdClass;
$assocobject->contextid = $contextid;
$assocobject->blogid = $this->id;
$DB->insert_record('blog_association', $assocobject);
$id = $DB->insert_record('blog_association', $assocobject);

// Trigger an association created event.
$context = context::instance_by_id($contextid);
$courseid = null;

$eventparam = array(
'objectid' => $id,
'other' => array('associateid' => $context->instanceid, 'subject' => $this->subject, 'blogid' => $this->id),
'relateduserid' => $this->userid
);
if ($context->contextlevel == CONTEXT_COURSE) {
$courseid = $context->instanceid;
add_to_log($courseid, 'blog', $action, 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject);
$eventparam['other']['associatetype'] = 'course';

} else if ($context->contextlevel == CONTEXT_MODULE) {
$cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
$modulename = $DB->get_field('modules', 'name', array('id' => $cm->module));
add_to_log($cm->course, 'blog', $action, 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject, $cm->id, $this->userid);
$eventparam['other']['associatetype'] = 'coursemodule';
}
$event = \core\event\blog_association_created::create($eventparam);
$event->trigger();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions blog/upgrade.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== 2.7 ===

* blog_entry->add_association() does not accept any params.
* blog_entry->add_associations() accepts only one param.
2 changes: 2 additions & 0 deletions lang/en/blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
$string['entryerrornotyours'] = 'This entry is not yours';
$string['entrysaved'] = 'Your entry has been saved';
$string['entrytitle'] = 'Entry title';
$string['eventblogentriesviewed'] = 'Blog entries viewed';
$string['eventblogassociationcreated'] = 'Blog association created';
$string['evententryadded'] = 'Blog entry added';
$string['evententrydeleted'] = 'Blog entry deleted';
$string['evententryupdated'] = 'Blog entry updated';
Expand Down

0 comments on commit 6b36411

Please sign in to comment.