Skip to content

Commit

Permalink
Merge branch 'MDL-40040-master' of git://github.com/ankitagarwal/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed Dec 2, 2013
2 parents 466aa44 + 6b36411 commit df68e73
Show file tree
Hide file tree
Showing 7 changed files with 387 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
132 changes: 132 additions & 0 deletions blog/tests/bloglib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,137 @@ public function test_blog_entry_deleted_event() {
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventLegacyData($blog, $event);
}


/**
* Tests for event blog_association_created.
*/
public function test_blog_association_created_event() {
global $USER;

$this->setAdminUser();
$this->resetAfterTest();
$sitecontext = context_system::instance();
$coursecontext = context_course::instance($this->courseid);
$contextmodule = context_module::instance($this->cmid);

// Add blog associations with a course.
$blog = new blog_entry($this->postid);
$sink = $this->redirectEvents();
$blog->add_association($coursecontext->id);
$events = $sink->get_events();
$event = reset($events);
$sink->close();

// Validate event data.
$this->assertInstanceOf('\core\event\blog_association_created', $event);
$this->assertEquals($sitecontext->id, $event->contextid);
$this->assertEquals($blog->id, $event->other['blogid']);
$this->assertEquals($this->courseid, $event->other['associateid']);
$this->assertEquals('course', $event->other['associatetype']);
$this->assertEquals($blog->subject, $event->other['subject']);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($this->userid, $event->relateduserid);
$this->assertEquals('blog_association', $event->objecttable);
$arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
$blog->subject, 0, $this->userid);
$this->assertEventLegacyLogData($arr, $event);

// Add blog associations with a module.
$blog = new blog_entry($this->postid);
$sink = $this->redirectEvents();
$blog->add_association($contextmodule->id);
$events = $sink->get_events();
$event = reset($events);
$sink->close();

// Validate event data.
$this->assertEquals($blog->id, $event->other['blogid']);
$this->assertEquals($this->cmid, $event->other['associateid']);
$this->assertEquals('coursemodule', $event->other['associatetype']);
$arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
$blog->subject, $this->cmid, $this->userid);
$this->assertEventLegacyLogData($arr, $event);
}

/**
* Tests for event blog_association_created validations.
*/
public function test_blog_association_created_event_validations() {

$this->resetAfterTest();

// Make sure associatetype validations work.
try {
\core\event\blog_association_created::create(array(
'contextid' => 1,
'objectid' => 3,
'other' => array('associateid' => 2 , 'blogid' => 3, 'subject' => 'blog subject')));
} catch (coding_exception $e) {
$this->assertContains('Invalid associatetype', $e->getMessage());
}
try {
\core\event\blog_association_created::create(array(
'contextid' => 1,
'objectid' => 3,
'other' => array('associateid' => 2 , 'blogid' => 3, 'associatetype' => 'random', 'subject' => 'blog subject')));
} catch (coding_exception $e) {
$this->assertContains('Invalid associatetype', $e->getMessage());
}
// Make sure associateid validations work.
try {
\core\event\blog_association_created::create(array(
'contextid' => 1,
'objectid' => 3,
'other' => array('blogid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
} catch (coding_exception $e) {
$this->assertContains('Associate id must be set', $e->getMessage());
}
// Make sure blogid validations work.
try {
\core\event\blog_association_created::create(array(
'contextid' => 1,
'objectid' => 3,
'other' => array('associateid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
} catch (coding_exception $e) {
$this->assertContains('Blog id must be set', $e->getMessage());
}
// Make sure blogid validations work.
try {
\core\event\blog_association_created::create(array(
'contextid' => 1,
'objectid' => 3,
'other' => array('blogid' => 3, 'associateid' => 3, 'associatetype' => 'course')));
} catch (coding_exception $e) {
$this->assertContains('Subject must be set', $e->getMessage());
}
}

/**
* Tests for event blog_entries_viewed.
*/
public function test_blog_entries_viewed_event() {

$this->setAdminUser();
$this->resetAfterTest();
$other = array('entryid' => $this->postid, 'tagid' => $this->tagid, 'userid' => $this->userid, 'modid' => $this->cmid,
'groupid' => $this->groupid, 'courseid' => $this->courseid, 'search' => 'search', 'fromstart' => 2);

// Trigger event.
$sink = $this->redirectEvents();
$eventparams = array('other' => $other);
$eventinst = \core\event\blog_entries_viewed::create($eventparams);
$eventinst->trigger();
$events = $sink->get_events();
$event = reset($events);
$sink->close();

// Validate event data.
$url = new moodle_url('/blog/index.php', $other);
$url2 = new moodle_url('index.php', $other);
$this->assertEquals($url, $event->get_url());
$arr = array(SITEID, 'blog', 'view', $url2->out(), 'view blog entry');
$this->assertEventLegacyLogData($arr, $event);
}
}

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
108 changes: 108 additions & 0 deletions lib/classes/event/blog_association_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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/>.
/**
* Event for when a new blog entry is associated with a context.
*
* @package core
* @copyright 2013 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;

defined('MOODLE_INTERNAL') || die();

/**
* blog_association_created
*
* Class for event to be triggered when a new blog entry is associated with a context.
*
* @package core
* @copyright 2013 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class blog_association_created extends \core\event\base {

/**
* Set basic properties for the event.
*/
protected function init() {
$this->context = \context_system::instance();
$this->data['objecttable'] = 'blog_association';
$this->data['crud'] = 'c';
$this->data['level'] = self::LEVEL_PARTICIPATING;
}

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventblogassociationadded', 'core_blog');
}

/**
* Returns non-localised event description with id's for admin use only.
*
* @return string
*/
public function get_description() {
return "Blog association added between entry id $this->other['blogid'] and $this->other['associatetype'] with id
$this->other['associateid']";
}

/**
* Returns relevant URL.
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/blog/index.php', array('entryid' => $this->objectid, 'userid' => $this->userid));
}

/**
* replace add_to_log() statement.
*
* @return array of parameters to be passed to legacy add_to_log() function.
*/
protected function get_legacy_logdata() {
if ($this->other['associatetype'] === 'course') {
return array (SITEID, 'blog', 'add association', 'index.php?userid=' . $this->relateduserid. '&entryid=' .
$this->other['blogid'], $this->other['subject'], 0, $this->relateduserid);
} else {
return array (SITEID, 'blog', 'add association', 'index.php?userid=' . $this->relateduserid. '&entryid=' .
$this->other['blogid'], $this->other['subject'], $this->other['associateid'], $this->relateduserid);
}
}

/**
* Custom validations.
*
* @throws \coding_exception when validation fails.
* @return void
*/
protected function validate_data() {
if (empty($this->other['associatetype']) || ($this->other['associatetype'] !== 'course'
&& $this->other['associatetype'] !== 'coursemodule')) {
throw new \coding_exception('Invalid associatetype in event blog_association_created.');
} else if (!isset($this->other['blogid'])) {
throw new \coding_exception('Blog id must be set in event blog_association_created.');
} else if (!isset($this->other['associateid'])) {
throw new \coding_exception('Associate id must be set in event blog_association_created.');
} else if (!isset($this->other['subject'])) {
throw new \coding_exception('Subject must be set in event blog_association_created.');
}
}
}
Loading

0 comments on commit df68e73

Please sign in to comment.