/** * Add a single association for a blog entry * * @param int $contextid - id of context to associate with the blog entry. * @param string $unused This does nothing, do not use it. */ public function add_association($contextid, $unused = null) { global $DB; if ($unused !== null) { debugging('Illegal argument used in blog_entry->add_association()', DEBUG_DEVELOPER); } $assocobject = new StdClass(); $assocobject->contextid = $contextid; $assocobject->blogid = $this->id; $id = $DB->insert_record('blog_association', $assocobject); // Trigger an association created event. $context = context::instance_by_id($contextid); $eventparam = array('objectid' => $id, 'other' => array('associateid' => $context->instanceid, 'subject' => $this->subject, 'blogid' => $this->id), 'relateduserid' => $this->userid); if ($context->contextlevel == CONTEXT_COURSE) { $eventparam['other']['associatetype'] = 'course'; } else { if ($context->contextlevel == CONTEXT_MODULE) { $eventparam['other']['associatetype'] = 'coursemodule'; } } $event = \core\event\blog_association_created::create($eventparam); $event->trigger(); }
/** * 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, 'relateduserid' => 2, 'other' => array('associateid' => 2, 'blogid' => 3, 'subject' => 'blog subject'))); } catch (coding_exception $e) { $this->assertContains('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage()); } try { \core\event\blog_association_created::create(array('contextid' => 1, 'objectid' => 3, 'relateduserid' => 2, 'other' => array('associateid' => 2, 'blogid' => 3, 'associatetype' => 'random', 'subject' => 'blog subject'))); } catch (coding_exception $e) { $this->assertContains('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage()); } // Make sure associateid validations work. try { \core\event\blog_association_created::create(array('contextid' => 1, 'objectid' => 3, 'relateduserid' => 2, 'other' => array('blogid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject'))); } catch (coding_exception $e) { $this->assertContains('The \'associateid\' value must be set in other.', $e->getMessage()); } // Make sure blogid validations work. try { \core\event\blog_association_created::create(array('contextid' => 1, 'objectid' => 3, 'relateduserid' => 2, 'other' => array('associateid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject'))); } catch (coding_exception $e) { $this->assertContains('The \'blogid\' value must be set in other.', $e->getMessage()); } // Make sure blogid validations work. try { \core\event\blog_association_created::create(array('contextid' => 1, 'objectid' => 3, 'relateduserid' => 2, 'other' => array('blogid' => 3, 'associateid' => 3, 'associatetype' => 'course'))); } catch (coding_exception $e) { $this->assertContains('The \'subject\' value must be set in other.', $e->getMessage()); } }