From abea2c5debd83825dd5fb96f08ebab3bff0ced8d Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Sun, 10 Jan 2016 14:35:54 +0800 Subject: [PATCH] MDL-50851 blogs: use new tag API --- blog/edit.php | 14 ++-- blog/edit_form.php | 5 +- blog/external_blog_edit.php | 19 +++--- blog/external_blog_edit_form.php | 17 +++-- blog/index.php | 1 - blog/lib.php | 107 ++++++++++++++++++++++++++++++- blog/locallib.php | 89 ++++++++++++++----------- blog/renderer.php | 16 +---- blog/rsslib.php | 6 +- blog/tests/lib_test.php | 70 ++++++++++++++++++-- 10 files changed, 247 insertions(+), 97 deletions(-) diff --git a/blog/edit.php b/blog/edit.php index 634c511377c..82480390c3c 100644 --- a/blog/edit.php +++ b/blog/edit.php @@ -24,9 +24,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once(dirname(dirname(__FILE__)).'/config.php'); -require_once('lib.php'); -require_once('locallib.php'); -require_once($CFG->dirroot .'/comment/lib.php'); +require_once($CFG->dirroot . '/blog/lib.php'); +require_once($CFG->dirroot . '/blog/locallib.php'); +require_once($CFG->dirroot . '/comment/lib.php'); +require_once($CFG->dirroot . '/blog/edit_form.php'); $action = required_param('action', PARAM_ALPHA); $id = optional_param('entryid', 0, PARAM_INT); @@ -185,7 +186,6 @@ if (!empty($entry->id)) { } } -require_once('edit_form.php'); $summaryoptions = array('maxfiles' => 99, 'maxbytes' => $CFG->maxbytes, 'trusttext' => true, 'context' => $sitecontext, 'subdirs' => file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id)); $attachmentoptions = array('subdirs' => false, 'maxfiles' => 99, 'maxbytes' => $CFG->maxbytes); @@ -206,9 +206,8 @@ $entry = file_prepare_standard_filemanager($entry, '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); +if (!empty($entry->id)) { + $entry->tags = core_tag_tag::get_item_tags_array('core', 'post', $entry->id); } $entry->action = $action; @@ -271,7 +270,6 @@ switch ($action) { if (empty($entry->id)) { print_error('wrongentryid', 'blog'); } - $entry->tags = tag_get_tags_array('post', $entry->id); $strformheading = get_string('updateentrywithid', 'blog'); break; diff --git a/blog/edit_form.php b/blog/edit_form.php index 773f1533e11..7060c932b5a 100644 --- a/blog/edit_form.php +++ b/blog/edit_form.php @@ -65,10 +65,11 @@ class blog_edit_form extends moodleform { $mform->addHelpButton('publishstate', 'publishto', 'blog'); $mform->setDefault('publishstate', 0); - if (!empty($CFG->usetags)) { + if (core_tag_tag::is_enabled('core', 'post')) { $mform->addElement('header', 'tagshdr', get_string('tags', 'tag')); - $mform->addElement('tags', 'tags', get_string('tags')); } + $mform->addElement('tags', 'tags', get_string('tags'), + array('itemtype' => 'post', 'component' => 'core')); $allmodnames = array(); diff --git a/blog/external_blog_edit.php b/blog/external_blog_edit.php index 069ef9615f5..3f615bdd1b0 100644 --- a/blog/external_blog_edit.php +++ b/blog/external_blog_edit.php @@ -28,7 +28,6 @@ require_once('../config.php'); require_once('lib.php'); require_once('external_blog_edit_form.php'); require_once($CFG->libdir . '/simplepie/moodle_simplepie.php'); -require_once($CFG->dirroot.'/tag/lib.php'); require_login(); $context = context_system::instance(); @@ -58,6 +57,7 @@ if (!empty($id) && !$DB->record_exists('blog_external', array('id' => $id))) { print_error('wrongexternalid', 'blog'); } else if (!empty($id)) { $external = $DB->get_record('blog_external', array('id' => $id)); + $external->autotags = core_tag_tag::get_item_tags_array('core', 'blog_external', $id); } $strformheading = ($action == 'edit') ? get_string('editexternalblog', 'blog') : get_string('addnewexternalblog', 'blog'); @@ -84,12 +84,9 @@ if ($externalblogform->is_cancelled()) { $newexternal->timemodified = time(); $newexternal->id = $DB->insert_record('blog_external', $newexternal); + core_tag_tag::set_item_tags('core', 'blog_external', $newexternal->id, + context_user::instance($newexternal->userid), $data->autotags); blog_sync_external_entries($newexternal); - if ($CFG->usetags) { - $autotags = (!empty($data->autotags)) ? $data->autotags : null; - tag_set('blog_external', $newexternal->id, explode(',', $autotags), 'core', - context_user::instance($newexternal->userid)->id); - } break; @@ -107,11 +104,8 @@ if ($externalblogform->is_cancelled()) { $external->timemodified = time(); $DB->update_record('blog_external', $external); - if ($CFG->usetags) { - $autotags = (!empty($data->autotags)) ? $data->autotags : null; - tag_set('blog_external', $external->id, explode(',', $autotags), 'core', - context_user::instance($external->userid)->id); - } + core_tag_tag::set_item_tags('core', 'blog_external', $external->id, + context_user::instance($external->userid), $data->autotags); } else { print_error('wrongexternalid', 'blog'); } @@ -125,6 +119,9 @@ if ($externalblogform->is_cancelled()) { redirect($returnurl); } +navigation_node::override_active_url(new moodle_url('/blog/external_blogs.php')); +$PAGE->navbar->add(get_string('addnewexternalblog', 'blog')); + $PAGE->set_heading(fullname($USER)); $PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs"); diff --git a/blog/external_blog_edit_form.php b/blog/external_blog_edit_form.php index fc28e9a406d..8de3bdcc58f 100644 --- a/blog/external_blog_edit_form.php +++ b/blog/external_blog_edit_form.php @@ -48,14 +48,14 @@ class blog_edit_external_form extends moodleform { $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7)); $mform->addHelpButton('description', 'description', 'blog'); - if (!empty($CFG->usetags)) { - $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50)); - $mform->setType('filtertags', PARAM_TAGLIST); - $mform->addHelpButton('filtertags', 'filtertags', 'blog'); - $mform->addElement('text', 'autotags', get_string('autotags', 'blog'), array('size' => 50)); - $mform->setType('autotags', PARAM_TAGLIST); - $mform->addHelpButton('autotags', 'autotags', 'blog'); - } + // To filter external blogs by their tags we do not need to check if tags in moodle are enabled. + $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50)); + $mform->setType('filtertags', PARAM_TAGLIST); + $mform->addHelpButton('filtertags', 'filtertags', 'blog'); + + $mform->addElement('tags', 'autotags', get_string('autotags', 'blog'), + array('itemtype' => 'blog_external', 'component' => 'core')); + $mform->addHelpButton('autotags', 'autotags', 'blog'); $this->add_action_buttons(); @@ -115,7 +115,6 @@ class blog_edit_external_form extends moodleform { } if ($id = $mform->getElementValue('id')) { - $mform->setDefault('autotags', implode(',', tag_get_tags_array('blog_external', $id))); $mform->freeze('url'); if ($mform->elementExists('filtertags')) { $mform->freeze('filtertags'); diff --git a/blog/index.php b/blog/index.php index bc3dd1ed06b..1b64b42fe56 100644 --- a/blog/index.php +++ b/blog/index.php @@ -24,7 +24,6 @@ require_once(dirname(dirname(__FILE__)).'/config.php'); require_once($CFG->dirroot .'/blog/lib.php'); require_once($CFG->dirroot .'/blog/locallib.php'); require_once($CFG->dirroot .'/course/lib.php'); -require_once($CFG->dirroot .'/tag/lib.php'); require_once($CFG->dirroot .'/comment/lib.php'); $id = optional_param('id', null, PARAM_INT); diff --git a/blog/lib.php b/blog/lib.php index 00a1ffa6bf5..06b4af808ae 100644 --- a/blog/lib.php +++ b/blog/lib.php @@ -29,7 +29,6 @@ defined('MOODLE_INTERNAL') || die(); * Library of functions and constants for blog */ require_once($CFG->dirroot .'/blog/rsslib.php'); -require_once($CFG->dirroot.'/tag/lib.php'); /** * User can edit a blog entry if this is their own blog entry and they have @@ -254,8 +253,8 @@ function blog_sync_external_entries($externalblog) { $id = $DB->insert_record('post', $newentry); // Set tags. - if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) { - tag_set('post', $id, $tags, 'core', context_user::instance($externalblog->userid)->id); + if ($tags = core_tag_tag::get_item_tags_array('core', 'blog_external', $externalblog->id)) { + core_tag_tag::set_item_tags('core', 'post', $id, context_user::instance($externalblog->userid), $tags); } } else { $newentry->id = $postid; @@ -1046,3 +1045,105 @@ function core_blog_myprofile_navigation(core_user\output\myprofile\tree $tree, $ $tree->add_node($blognode); return true; } + +/** + * Returns posts tagged with a specified tag. + * + * @param core_tag_tag $tag + * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag + * are displayed on the page and the per-page limit may be bigger + * @param int $fromctx context id where the link was displayed, may be used by callbacks + * to display items in the same context first + * @param int $ctx context id where to search for records + * @param bool $rec search in subcontexts as well + * @param int $page 0-based number of page being displayed + * @return \core_tag\output\tagindex + */ +function blog_get_tagged_posts($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = true, $page = 0) { + global $CFG, $OUTPUT; + require_once($CFG->dirroot.'/user/lib.php'); + + $systemcontext = context_system::instance(); + $perpage = $exclusivemode ? 20 : 5; + $context = $ctx ? context::instance_by_id($ctx) : context_system::instance(); + + $content = ''; + if (empty($CFG->enableblogs) || !has_capability('moodle/blog:view', $systemcontext)) { + // Blogs are not enabled or are not visible to the current user. + $totalpages = 0; + } else if ($context->contextlevel != CONTEXT_SYSTEM && empty($CFG->useblogassociations)) { + // No blog entries can be associated to the non-system context. + $totalpages = 0; + } else if (!$rec && $context->contextlevel != CONTEXT_COURSE && $context->contextlevel != CONTEXT_MODULE) { + // No blog entries can be associated with category or block context. + $totalpages = 0; + } else { + require_once($CFG->dirroot.'/blog/locallib.php'); + + $filters = array('tag' => $tag->id); + if ($rec) { + if ($context->contextlevel != CONTEXT_SYSTEM) { + $filters['context'] = $context->id; + } + } else if ($context->contextlevel == CONTEXT_COURSE) { + $filters['course'] = $context->instanceid; + } else if ($context->contextlevel == CONTEXT_MODULE) { + $filters['module'] = $context->instanceid; + } + $bloglisting = new blog_listing($filters); + $blogs = $bloglisting->get_entries($page * $perpage, $perpage); + $totalcount = $bloglisting->count_entries(); + $totalpages = ceil($totalcount / $perpage); + if (!empty($blogs)) { + $tagfeed = new core_tag\output\tagfeed(); + foreach ($blogs as $blog) { + $user = fullclone($blog); + $user->id = $blog->userid; + $user->deleted = 0; + $img = $OUTPUT->user_picture($user, array('size' => 35)); + $subject = format_string($blog->subject); + + if ($blog->publishstate == 'draft') { + $class = 'dimmed'; + } else { + $class = ''; + } + + $url = new moodle_url('/blog/index.php', array('entryid' => $blog->id)); + $subject = html_writer::link($url, $subject, array('class' => $class)); + + $fullname = fullname($user); + if (user_can_view_profile($user)) { + $profilelink = new moodle_url('/user/view.php', array('id' => $blog->userid)); + $fullname = html_writer::link($profilelink, $fullname); + } + $details = $fullname . ', ' . userdate($blog->created); + + $tagfeed->add($img, $subject, $details); + } + + $items = $tagfeed->export_for_template($OUTPUT); + $content = $OUTPUT->render_from_template('core_tag/tagfeed', $items); + + $urlparams = array('tagid' => $tag->id); + if ($context->contextlevel == CONTEXT_COURSE) { + $urlparams['courseid'] = $context->instanceid; + } else if ($context->contextlevel == CONTEXT_MODULE) { + $urlparams['modid'] = $context->instanceid; + } + $allblogsurl = new moodle_url('/blog/index.php', $urlparams); + + $rv = new core_tag\output\tagindex($tag, 'core', 'post', + $content, + $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages); + $rv->exclusiveurl = $allblogsurl; + return $rv; + } + } + + $rv = new core_tag\output\tagindex($tag, 'core', 'post', + $content, + $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages); + $rv->exclusiveurl = null; + return $rv; +} diff --git a/blog/locallib.php b/blog/locallib.php index 7968483fb36..5e31f8e4401 100644 --- a/blog/locallib.php +++ b/blog/locallib.php @@ -256,14 +256,11 @@ class blog_entry implements renderable { // Insert the new blog entry. $this->id = $DB->insert_record('post', $this); - // Update tags. - $this->add_tags_info(); - if (!empty($CFG->useblogassociations)) { $this->add_associations(); } - tag_set('post', $this->id, $this->tags, 'core', context_user::instance($this->userid)->id); + core_tag_tag::set_item_tags('core', 'post', $this->id, context_user::instance($this->userid), $this->tags); // Trigger an event for the new entry. $event = \core\event\blog_entry_created::create(array( @@ -312,7 +309,7 @@ class blog_entry implements renderable { // Update record. $DB->update_record('post', $entry); - tag_set('post', $entry->id, $entry->tags, 'core', context_user::instance($this->userid)->id); + core_tag_tag::set_item_tags('core', 'post', $entry->id, context_user::instance($this->userid), $entry->tags); $event = \core\event\blog_entry_updated::create(array( 'objectid' => $entry->id, @@ -336,7 +333,7 @@ class blog_entry implements renderable { // Get record to pass onto the event. $record = $DB->get_record('post', array('id' => $this->id)); $DB->delete_records('post', array('id' => $this->id)); - tag_set('post', $this->id, array(), 'core', context_user::instance($this->userid)->id); + core_tag_tag::remove_all_item_tags('core', 'post', $this->id); $event = \core\event\blog_entry_deleted::create(array( 'objectid' => $this->id, @@ -424,26 +421,6 @@ class blog_entry implements renderable { $fs->delete_area_files(SYSCONTEXTID, 'blog', 'post', $this->id); } - /** - * function to attach tags into an entry - * @return void - */ - public function add_tags_info() { - - $tags = array(); - - if ($otags = optional_param('otags', '', PARAM_INT)) { - foreach ($otags as $tagid) { - // TODO : make this use the tag name in the form. - if ($tag = tag_get('id', $tagid)) { - $tags[] = $tag->name; - } - } - } - - tag_set('post', $this->id, $tags, 'core', context_user::instance($this->userid)->id); - } - /** * User can edit a blog entry if this is their own blog entry and they have * the capability moodle/blog:create, or if they have the capability @@ -570,7 +547,13 @@ class blog_listing { * Array of blog_entry objects. * @var array $entries */ - public $entries = array(); + public $entries = null; + + /** + * Caches the total number of the entries. + * @var int + */ + public $totalentries = null; /** * An array of blog_filter_* objects @@ -608,9 +591,12 @@ class blog_listing { public function get_entries($start=0, $limit=10) { global $DB; - if (empty($this->entries)) { + if ($this->entries === null) { if ($sqlarray = $this->get_entry_fetch_sql(false, 'created DESC')) { $this->entries = $DB->get_records_sql($sqlarray['sql'], $sqlarray['params'], $start, $limit); + if (!$start && count($this->entries) < $limit) { + $this->totalentries = count($this->entries); + } } else { return false; } @@ -619,6 +605,23 @@ class blog_listing { return $this->entries; } + /** + * Finds total number of blog entries + * + * @return int + */ + public function count_entries() { + global $DB; + if ($this->totalentries === null) { + if ($sqlarray = $this->get_entry_fetch_sql(true)) { + $this->totalentries = $DB->count_records_sql($sqlarray['sql'], $sqlarray['params']); + } else { + $this->totalentries = 0; + } + } + return $this->totalentries; + } + public function get_entry_fetch_sql($count=false, $sort='lastmodified DESC', $userid = false) { global $DB, $USER, $CFG; @@ -626,7 +629,7 @@ class blog_listing { $userid = $USER->id; } - $allnamefields = get_all_user_name_fields(true, 'u'); + $allnamefields = \user_picture::fields('u', null, 'useridalias'); // The query used to locate blog entries is complicated. It will be built from the following components: $requiredfields = "p.*, $allnamefields, u.email"; // The SELECT clause. $tables = array('p' => 'post', 'u' => 'user'); // Components of the FROM clause (table_id => table_name). @@ -707,13 +710,8 @@ class blog_listing { $morelink = '
  '; - 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); + $totalentries = $this->count_entries(); $pagingbar = new paging_bar($totalentries, $page, $limit, $this->get_baseurl()); $pagingbar->pagevar = 'blogpage'; $blogheaders = blog_get_headers(); @@ -909,13 +907,14 @@ class blog_filter_context extends blog_filter { $this->availabletypes = array('site' => get_string('site'), 'course' => get_string('course'), - 'module' => get_string('activity')); + 'module' => get_string('activity'), + 'context' => get_string('coresystem')); switch ($this->type) { case 'course': // Careful of site course! // Ignore course filter if blog associations are not enabled. if ($this->id != $SITE->id && !empty($CFG->useblogassociations)) { - $this->overrides = array('site'); + $this->overrides = array('site', 'context'); $context = context_course::instance($this->id); $this->tables['ba'] = 'blog_association'; $this->conditions[] = 'p.id = ba.blogid'; @@ -930,7 +929,7 @@ class blog_filter_context extends blog_filter { break; case 'module': if (!empty($CFG->useblogassociations)) { - $this->overrides = array('course', 'site'); + $this->overrides = array('course', 'site', 'context'); $context = context_module::instance($this->id); $this->tables['ba'] = 'blog_association'; @@ -939,6 +938,19 @@ class blog_filter_context extends blog_filter { $this->params = array($context->id); } break; + case 'context': + if ($id != context_system::instance()->id && !empty($CFG->useblogassociations)) { + $this->overrides = array('site'); + $context = context::instance_by_id($this->id); + $this->tables['ba'] = 'blog_association'; + $this->tables['ctx'] = 'context'; + $this->conditions[] = 'p.id = ba.blogid'; + $this->conditions[] = 'ctx.id = ba.contextid'; + $this->conditions[] = 'ctx.path LIKE ?'; + $this->params = array($context->path . '%'); + } + break; + } } } @@ -1012,6 +1024,7 @@ class blog_filter_tag extends blog_filter { $this->conditions = array('ti.tagid = t.id', "ti.itemtype = 'post'", + "ti.component = 'core'", 'ti.itemid = p.id', 't.id = ?'); $this->params = array($this->id); diff --git a/blog/renderer.php b/blog/renderer.php index 1045a898685..640686571c2 100644 --- a/blog/renderer.php +++ b/blog/renderer.php @@ -132,21 +132,7 @@ class core_blog_renderer extends plugin_renderer_base { } // Links to tags. - $officialtags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'official'); - $defaulttags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'default'); - - if (!empty($CFG->usetags) && ($officialtags || $defaulttags) ) { - $o .= $this->output->container_start('tags'); - - if ($officialtags) { - $o .= get_string('tags', 'tag') .': '. $this->output->container($officialtags, 'officialblogtags'); - if ($defaulttags) { - $o .= ', '; - } - } - $o .= $defaulttags; - $o .= $this->output->container_end(); - } + $o .= $this->output->tag_list(core_tag_tag::get_item_tags('core', 'post', $entry->id)); // Add associations. if (!empty($CFG->useblogassociations) && !empty($entry->renderable->blogassociations)) { diff --git a/blog/rsslib.php b/blog/rsslib.php index cad38d88d50..366c5cd5654 100644 --- a/blog/rsslib.php +++ b/blog/rsslib.php @@ -217,10 +217,8 @@ function blog_rss_get_feed($context, $args) { $summary = file_rewrite_pluginfile_urls($blogentry->summary, 'pluginfile.php', $sitecontext->id, 'blog', 'post', $blogentry->id); $item->description = format_text($summary, $blogentry->format); - if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blogentry->id)) ) { - if ($blogtags) { - $item->tags = $blogtags; - } + if ($blogtags = core_tag_tag::get_item_tags_array('core', 'post', $blogentry->id)) { + $item->tags = $blogtags; $item->tagscheme = $CFG->wwwroot . '/tag'; } $items[] = $item; diff --git a/blog/tests/lib_test.php b/blog/tests/lib_test.php index 1eb6583868f..81eac309c7e 100644 --- a/blog/tests/lib_test.php +++ b/blog/tests/lib_test.php @@ -65,18 +65,15 @@ class core_blog_lib_testcase extends advanced_testcase { )); // Create default tag. - $tag = new stdClass(); - $tag->userid = $user->id; - $tag->name = 'testtagname'; - $tag->rawname = 'Testtagname'; - $tag->tagtype = 'official'; - $tag->id = $DB->insert_record('tag', $tag); + $tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id, + 'rawname' => 'Testtagname', 'tagtype' => 'official')); // Create default post. $post = new stdClass(); $post->userid = $user->id; $post->groupid = $group->id; $post->content = 'test post content text'; + $post->module = 'blog'; $post->id = $DB->insert_record('post', $post); // Grab important ids. @@ -544,5 +541,66 @@ class core_blog_lib_testcase extends advanced_testcase { $nodes->setAccessible(true); $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree)); } + + public function test_blog_get_listing_course() { + $this->setAdminUser(); + $coursecontext = context_course::instance($this->courseid); + $anothercourse = $this->getDataGenerator()->create_course(); + + // Add blog associations with a course. + $blog = new blog_entry($this->postid); + $blog->add_association($coursecontext->id); + + // There is one entry associated with a course. + $bloglisting = new blog_listing(array('course' => $this->courseid)); + $this->assertCount(1, $bloglisting->get_entries()); + + // There is no entry associated with a wrong course. + $bloglisting = new blog_listing(array('course' => $anothercourse->id)); + $this->assertCount(0, $bloglisting->get_entries()); + + // There is no entry associated with a module. + $bloglisting = new blog_listing(array('module' => $this->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. + $bloglisting = new blog_listing(array('context' => $coursecontext->id)); + $this->assertCount(1, $bloglisting->get_entries()); + } + + 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()); + } }