/** * Outputs all the blog entries aggregated by this blog listing. * * @return void */ public function print_entries() { global $CFG, $USER, $DB, $OUTPUT; $sitecontext = get_context_instance(CONTEXT_SYSTEM); $page = optional_param('blogpage', 0, PARAM_INT); $limit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT); $start = $page * $limit; $morelink = '<br /> '; if ($sqlarray = $this->get_entry_fetch_sql(true)) { $totalentries = $DB->count_records_sql($sqlarray['sql'], $sqlarray['params']); } else { $totalentries = 0; } $entries = $this->get_entries($start, $limit); $pagingbar = new paging_bar($totalentries, $page, $limit, $this->get_baseurl()); $pagingbar->pagevar = 'blogpage'; $blogheaders = blog_get_headers(); echo $OUTPUT->render($pagingbar); if (has_capability('moodle/blog:create', $sitecontext)) { //the user's blog is enabled and they are viewing their own blog $userid = optional_param('userid', null, PARAM_INT); if (empty($userid) || !empty($userid) && $userid == $USER->id) { $addurl = new moodle_url("{$CFG->wwwroot}/blog/edit.php"); $urlparams = array('action' => 'add', 'userid' => $userid, 'courseid' => optional_param('courseid', null, PARAM_INT), 'groupid' => optional_param('groupid', null, PARAM_INT), 'modid' => optional_param('modid', null, PARAM_INT), 'tagid' => optional_param('tagid', null, PARAM_INT), 'tag' => optional_param('tag', null, PARAM_INT), 'search' => optional_param('search', null, PARAM_INT)); foreach ($urlparams as $var => $val) { if (empty($val)) { unset($urlparams[$var]); } } $addurl->params($urlparams); $addlink = '<div class="addbloglink">'; $addlink .= '<a href="' . $addurl->out() . '">' . $blogheaders['stradd'] . '</a>'; $addlink .= '</div>'; echo $addlink; } } if ($entries) { $count = 0; foreach ($entries as $entry) { $blogentry = new blog_entry(null, $entry); $blogentry->print_html(); $count++; } echo $OUTPUT->render($pagingbar); if (!$count) { print '<br /><div style="text-align:center">' . get_string('noentriesyet', 'blog') . '</div><br />'; } print $morelink . '<br />' . "\n"; return; } }
/** * Outputs all the blog entries aggregated by this blog listing. * * @return void */ public function print_entries() { global $CFG, $USER, $DB, $OUTPUT, $PAGE; $sitecontext = context_system::instance(); // Blog renderer. $output = $PAGE->get_renderer('blog'); $page = optional_param('blogpage', 0, PARAM_INT); $limit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT); $start = $page * $limit; $morelink = '<br /> '; $entries = $this->get_entries($start, $limit); $totalentries = $this->count_entries(); $pagingbar = new paging_bar($totalentries, $page, $limit, $this->get_baseurl()); $pagingbar->pagevar = 'blogpage'; $blogheaders = blog_get_headers(); echo $OUTPUT->render($pagingbar); if (has_capability('moodle/blog:create', $sitecontext)) { // The user's blog is enabled and they are viewing their own blog. $userid = optional_param('userid', null, PARAM_INT); if (empty($userid) || !empty($userid) && $userid == $USER->id) { $courseid = optional_param('courseid', null, PARAM_INT); $modid = optional_param('modid', null, PARAM_INT); $addurl = new moodle_url("{$CFG->wwwroot}/blog/edit.php"); $urlparams = array('action' => 'add', 'userid' => $userid, 'courseid' => $courseid, 'groupid' => optional_param('groupid', null, PARAM_INT), 'modid' => $modid, 'tagid' => optional_param('tagid', null, PARAM_INT), 'tag' => optional_param('tag', null, PARAM_INT), 'search' => optional_param('search', null, PARAM_INT)); $urlparams = array_filter($urlparams); $addurl->params($urlparams); $addlink = '<div class="addbloglink">'; $addlink .= '<a href="' . $addurl->out() . '">' . $blogheaders['stradd'] . '</a>'; $addlink .= '</div>'; echo $addlink; } } if ($entries) { $count = 0; foreach ($entries as $entry) { $blogentry = new blog_entry(null, $entry); // Get the required blog entry data to render it. $blogentry->prepare_render(); echo $output->render($blogentry); $count++; } echo $OUTPUT->render($pagingbar); if (!$count) { print '<br /><div style="text-align:center">' . get_string('noentriesyet', 'blog') . '</div><br />'; } print $morelink . '<br />' . "\n"; return; } }
$entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id); $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id); if (!empty($CFG->usetags) && !empty($entry->id)) { include_once $CFG->dirroot . '/tag/lib.php'; $entry->tags = tag_get_tags_array('post', $entry->id); } $entry->action = $action; // set defaults $blogeditform->set_data($entry); if ($blogeditform->is_cancelled()) { redirect($returnurl); } else { if ($data = $blogeditform->get_data()) { switch ($action) { case 'add': $blogentry = new blog_entry(null, $data, $blogeditform); $blogentry->add(); $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions); break; case 'edit': if (empty($entry->id)) { print_error('wrongentryid', 'blog'); } $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions); break; default: print_error('invalidaction'); } redirect($returnurl); } }
function definition() { global $CFG, $DB; $mform =& $this->_form; $entry = $this->_customdata['entry']; $courseid = $this->_customdata['courseid']; $modid = $this->_customdata['modid']; $summaryoptions = $this->_customdata['summaryoptions']; $attachmentoptions = $this->_customdata['attachmentoptions']; $sitecontext = $this->_customdata['sitecontext']; $mform->addElement('header', 'general', get_string('general', 'form')); $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), 'size="60"'); $mform->addElement('editor', 'summary_editor', get_string('entrybody', 'blog'), null, $summaryoptions); $mform->setType('subject', PARAM_TEXT); $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client'); $mform->setType('summary_editor', PARAM_RAW); $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client'); $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions); //disable publishstate options that are not allowed $publishstates = array(); $i = 0; foreach (blog_entry::get_applicable_publish_states() as $state => $desc) { $publishstates[$state] = $desc; //no maximum was set $i++; } $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates); $mform->addHelpButton('publishstate', 'publishto', 'blog'); $mform->setDefault('publishstate', 0); if (!empty($CFG->usetags)) { $mform->addElement('header', 'tagshdr', get_string('tags', 'tag')); $mform->addElement('tags', 'tags', get_string('tags')); } $allmodnames = array(); if (!empty($CFG->useblogassociations)) { if ((!empty($entry->courseassoc) || !empty($courseid) && empty($modid)) && has_capability('moodle/blog:associatecourse', $sitecontext)) { if (!empty($courseid)) { $course = $DB->get_record('course', array('id' => $courseid)); $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); $context = get_context_instance(CONTEXT_COURSE, $courseid); $a = new stdClass(); $a->coursename = format_string($course->fullname, true, array('context' => $context)); $contextid = $context->id; } else { $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?'; $a = new stdClass(); $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc)); $contextid = $entry->courseassoc; } $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid)); $mform->setDefault('courseassoc', $contextid); } else { if ((!empty($entry->modassoc) || !empty($modid)) && has_capability('moodle/blog:associatemodule', $sitecontext)) { if (!empty($modid)) { $mod = get_coursemodule_from_id(false, $modid); $a = new stdClass(); $a->modtype = get_string('modulename', $mod->modname); $a->modname = $mod->name; $context = get_context_instance(CONTEXT_MODULE, $modid); } else { $context = $DB->get_record('context', array('id' => $entry->modassoc)); $cm = $DB->get_record('course_modules', array('id' => $context->instanceid)); $a = new stdClass(); $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module)); $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance)); } $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id)); $mform->setDefault('modassoc', $context->id); } } } $this->add_action_buttons(); $mform->addElement('hidden', 'action'); $mform->setType('action', PARAM_ACTION); $mform->setDefault('action', ''); $mform->addElement('hidden', 'entryid'); $mform->setType('entryid', PARAM_INT); $mform->setDefault('entryid', $entry->id); $mform->addElement('hidden', 'modid'); $mform->setType('modid', PARAM_INT); $mform->setDefault('modid', $modid); $mform->addElement('hidden', 'courseid'); $mform->setType('courseid', PARAM_INT); $mform->setDefault('courseid', $courseid); }
/** * Prepares environment for test_move_tags_* tests */ protected function prepare_move_tags() { global $CFG; require_once $CFG->dirroot . '/blog/locallib.php'; $this->setUser($this->getDataGenerator()->create_user()); $collid1 = core_tag_collection::get_default(); $collid2 = core_tag_collection::create(array('name' => 'newcoll'))->id; $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $blogpost = new blog_entry(null, array('subject' => 'test'), null); $states = blog_entry::get_applicable_publish_states(); $blogpost->publishstate = reset($states); $blogpost->add(); core_tag_tag::set_item_tags('core', 'user', $user1->id, context_user::instance($user1->id), array('Tag1', 'Tag2')); core_tag_tag::set_item_tags('core', 'user', $user2->id, context_user::instance($user2->id), array('Tag2', 'Tag3')); $this->getDataGenerator()->create_tag(array('rawname' => 'Tag4', 'tagcollid' => $collid1, 'tagtype' => 'official')); $this->getDataGenerator()->create_tag(array('rawname' => 'Tag5', 'tagcollid' => $collid2, 'tagtype' => 'official')); return array($collid1, $collid2, $user1, $user2, $blogpost); }
/** * Test the notification sending features. */ public function test_process_event() { global $DB, $USER; $this->resetAfterTest(); $this->setAdminUser(); $msgsink = $this->redirectMessages(); // Generate data. $course = $this->getDataGenerator()->create_course(); $toolgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor'); $rulerecord = new stdClass(); $rulerecord->courseid = $course->id; $rulerecord->eventname = '\\mod_book\\event\\course_module_instance_list_viewed'; $rulerecord->frequency = 1; $rule = $toolgenerator->create_rule($rulerecord); $subrecord = new stdClass(); $subrecord->courseid = $course->id; $subrecord->ruleid = $rule->id; $subrecord->userid = $USER->id; $toolgenerator->create_subscription($subrecord); $recordexists = $DB->record_exists('task_adhoc', array('component' => 'tool_monitor')); $this->assertFalse($recordexists); // Now let us trigger the event. $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course); $event->trigger(); $this->verify_processed_data($msgsink); // Clean up. \tool_monitor\rule_manager::delete_rule($rule->id); $DB->delete_records('tool_monitor_events'); // Let us create a rule with more than 1 frequency. $rulerecord->frequency = 5; $rule = $toolgenerator->create_rule($rulerecord); $subrecord->ruleid = $rule->id; $toolgenerator->create_subscription($subrecord); // Let us trigger events. for ($i = 0; $i < 5; $i++) { $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course); $event->trigger(); if ($i != 4) { $this->verify_message_not_sent_yet($msgsink); } } $this->verify_processed_data($msgsink); // Clean up. \tool_monitor\rule_manager::delete_rule($rule->id); $DB->delete_records('tool_monitor_events'); // Now let us create a rule specific to a module instance. $cm = new stdClass(); $cm->course = $course->id; $book = $this->getDataGenerator()->create_module('book', $cm); $rulerecord->eventname = '\\mod_book\\event\\course_module_viewed'; $rulerecord->cmid = $book->cmid; $rule = $toolgenerator->create_rule($rulerecord); $subrecord->ruleid = $rule->id; $toolgenerator->create_subscription($subrecord); // Let us trigger events. $params = array('context' => context_module::instance($book->cmid), 'objectid' => $book->id); for ($i = 0; $i < 5; $i++) { $event = \mod_book\event\course_module_viewed::create($params); $event->trigger(); if ($i != 4) { $this->verify_message_not_sent_yet($msgsink); } } $this->verify_processed_data($msgsink); // Clean up. \tool_monitor\rule_manager::delete_rule($rule->id); $DB->delete_records('tool_monitor_events'); // Now let us create a rule for event that happens in category context events. $rulerecord->eventname = '\\core\\event\\course_category_created'; $rulerecord->courseid = 0; $rule = $toolgenerator->create_rule($rulerecord); $subrecord->courseid = 0; $subrecord->ruleid = $rule->id; $toolgenerator->create_subscription($subrecord); // Let us trigger events. for ($i = 0; $i < 5; $i++) { $this->getDataGenerator()->create_category(); if ($i != 4) { $this->verify_message_not_sent_yet($msgsink); } } $this->verify_processed_data($msgsink); // Clean up. \tool_monitor\rule_manager::delete_rule($rule->id); $DB->delete_records('tool_monitor_events'); // Now let us create a rule at site level. $rulerecord->eventname = '\\core\\event\\blog_entry_created'; $rulerecord->courseid = 0; $rule = $toolgenerator->create_rule($rulerecord); $subrecord->courseid = 0; $subrecord->ruleid = $rule->id; $toolgenerator->create_subscription($subrecord); // Let us trigger events. $blog = new blog_entry(); $blog->subject = "Subject of blog"; $blog->userid = $USER->id; $states = blog_entry::get_applicable_publish_states(); $blog->publishstate = reset($states); for ($i = 0; $i < 5; $i++) { $newblog = fullclone($blog); $newblog->add(); if ($i != 4) { $this->verify_message_not_sent_yet($msgsink); } } $this->verify_processed_data($msgsink); }
/** * 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); $url = new moodle_url('/blog/index.php', array('entryid' => $event->other['blogid'])); $this->assertEquals($url, $event->get_url()); $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); $this->assertEventContextNotUsed($event); }
public function test_blog_get_listing_module() { $this->setAdminUser(); $coursecontext = context_course::instance($this->courseid); $contextmodule = context_module::instance($this->cmid); $anothermodule = $this->getDataGenerator()->create_module('page', array('course' => $this->courseid)); // Add blog associations with a course. $blog = new blog_entry($this->postid); $blog->add_association($contextmodule->id); // There is no entry associated with a course. $bloglisting = new blog_listing(array('course' => $this->courseid)); $this->assertCount(0, $bloglisting->get_entries()); // There is one entry associated with a module. $bloglisting = new blog_listing(array('module' => $this->cmid)); $this->assertCount(1, $bloglisting->get_entries()); // There is no entry associated with a wrong module. $bloglisting = new blog_listing(array('module' => $anothermodule->cmid)); $this->assertCount(0, $bloglisting->get_entries()); // There is one entry associated with a site (id is ignored). $bloglisting = new blog_listing(array('site' => 12345)); $this->assertCount(1, $bloglisting->get_entries()); // There is one entry associated with course context (module is a subcontext of a course). $bloglisting = new blog_listing(array('context' => $coursecontext->id)); $this->assertCount(1, $bloglisting->get_entries()); }
/** * Tests for event blog_entry_deleted. */ public function test_blog_entry_deleted_event() { global $USER, $DB; $this->setAdminUser(); $this->resetAfterTest(); $sitecontext = context_system::instance(); // Delete a user blog entry as Admin. $blog = new blog_entry($this->postid); $sink = $this->redirectEvents(); $record = $DB->get_record('post', array('id' => $blog->id)); $blog->delete(); $events = $sink->get_events(); $event = array_pop($events); $sink->close(); // Validate event data. $this->assertInstanceOf('\\core\\event\\blog_entry_deleted', $event); $this->assertEquals($sitecontext->id, $event->contextid); $this->assertEquals($blog->id, $event->objectid); $this->assertEquals($USER->id, $event->userid); $this->assertEquals($this->userid, $event->relateduserid); $this->assertEquals("post", $event->objecttable); $this->assertEquals($record, $event->get_record_snapshot("post", $blog->id)); $this->assertSame('blog_entry_deleted', $event->get_legacy_eventname()); $arr = array(SITEID, 'blog', 'delete', 'index.php?userid=' . $blog->userid, 'deleted blog entry with entry id# ' . $blog->id); $this->assertEventLegacyLogData($arr, $event); $this->assertEventLegacyData($blog, $event); }
/** * Test various blog related events. */ public function test_blog_entry_events() { global $USER, $DB; $this->setAdminUser(); $this->resetAfterTest(); // Create a blog entry. $blog = new blog_entry(); $blog->summary = "This is summary of blog"; $blog->subject = "Subject of blog"; $states = blog_entry::get_applicable_publish_states(); $blog->publishstate = reset($states); $sink = $this->redirectEvents(); $blog->add(); $events = $sink->get_events(); $event = reset($events); $sitecontext = context_system::instance(); // Validate event data. $this->assertInstanceOf('\core\event\blog_entry_created', $event); $this->assertEquals($sitecontext->id, $event->contextid); $this->assertEquals($blog->id, $event->objectid); $this->assertEquals($USER->id, $event->userid); $this->assertEquals("post", $event->objecttable); // Delete a blog entry. $record = $DB->get_record('post', array('id' => $blog->id)); $blog->delete(); $events = $sink->get_events(); $event = array_pop($events); // Validate event data. $this->assertInstanceOf('\core\event\blog_entry_deleted', $event); $this->assertEquals(context_system::instance()->id, $event->contextid); $this->assertEquals($blog->id, $event->objectid); $this->assertEquals($USER->id, $event->userid); $this->assertEquals("post", $event->objecttable); $this->assertEquals($record, $event->get_record_snapshot("post", $blog->id)); $this->assertSame('blog_entry_deleted', $event->get_legacy_eventname()); }