3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * Core global functions for Blog.
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 * Library of functions and constants for blog
31 require_once($CFG->dirroot .'/blog/rsslib.php');
32 require_once($CFG->dirroot.'/tag/lib.php');
35 * User can edit a blog entry if this is their own blog entry and they have
36 * the capability moodle/blog:create, or if they have the capability
37 * moodle/blog:manageentries.
39 * This also applies to deleting of entries.
41 function blog_user_can_edit_entry($blogentry) {
44 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
46 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
47 return true; // can edit any blog entry
50 if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) {
51 return true; // can edit own when having blog:create capability
59 * Checks to see if a user can view the blogs of another user.
60 * Only blog level is checked here, the capabilities are enforced
63 function blog_user_can_view_user_entry($targetuserid, $blogentry=null) {
64 global $CFG, $USER, $DB;
66 if (empty($CFG->bloglevel)) {
67 return false; // blog system disabled
70 if (isloggdin() && $USER->id == $targetuserid) {
71 return true; // can view own entries in any case
74 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
75 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
76 return true; // can manage all entries
79 // coming for 1 entry, make sure it's not a draft
80 if ($blogentry && $blogentry->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
81 return false; // can not view draft of others
84 // coming for 1 entry, make sure user is logged in, if not a public blog
85 if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) {
89 switch ($CFG->bloglevel) {
90 case BLOG_GLOBAL_LEVEL:
95 if (isloggedin()) { // not logged in viewers forbidden
101 case BLOG_USER_LEVEL:
103 $personalcontext = get_context_instance(CONTEXT_USER, $targetuserid);
104 return has_capability('moodle/user:readuserblogs', $personalcontext);
111 * remove all associations for the blog entries of a particular user
112 * @param int userid - id of user whose blog associations will be deleted
114 function blog_remove_associations_for_user($userid) {
116 $blogentries = blog_fetch_entries(array('user' => $userid), 'lasmodified DESC');
117 foreach ($blogentries as $entry) {
118 if (blog_user_can_edit_entry($entry)) {
119 blog_remove_associations_for_entry($entry->id);
125 * remove all associations for the blog entries of a particular course
126 * @param int courseid - id of user whose blog associations will be deleted
128 function blog_remove_associations_for_course($courseid) {
130 $context = get_context_instance(CONTEXT_COURSE, $courseid);
131 $DB->delete_records('blog_association', array('contextid' => $context->id));
135 * Given a record in the {blog_external} table, checks the blog's URL
136 * for new entries not yet copied into Moodle.
138 * @param object $externalblog
139 * @return boolean False if the Feed is invalid
141 function blog_sync_external_entries($externalblog) {
143 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
145 $rssfile = new moodle_simplepie_file($externalblog->url);
146 $filetest = new SimplePie_Locator($rssfile);
148 if (!$filetest->is_feed($rssfile)) {
149 $externalblog->failedlastsync = 1;
150 $DB->update_record('blog_external', $externalblog);
152 } else if ($externalblog->failedlastsync) {
153 $externalblog->failedlastsync = 0;
154 $DB->update_record('blog_external', $externalblog);
157 // Delete all blog entries associated with this external blog
158 blog_delete_external_entries($externalblog);
160 $rss = new moodle_simplepie($externalblog->url);
162 if (empty($rss->data)) {
166 foreach ($rss->get_items() as $entry) {
167 // If filtertags are defined, use them to filter the entries by RSS category
168 if (!empty($externalblog->filtertags)) {
169 $containsfiltertag = false;
170 $categories = $entry->get_categories();
171 $filtertags = explode(',', $externalblog->filtertags);
172 $filtertags = array_map('trim', $filtertags);
173 $filtertags = array_map('strtolower', $filtertags);
175 foreach ($categories as $category) {
176 if (in_array(trim(strtolower($category->term)), $filtertags)) {
177 $containsfiltertag = true;
181 if (!$containsfiltertag) {
186 $newentry = new object();
187 $newentry->userid = $externalblog->userid;
188 $newentry->module = 'blog_external';
189 $newentry->content = $externalblog->id;
190 $newentry->uniquehash = $entry->get_permalink();
191 $newentry->publishstate = 'site';
192 $newentry->format = FORMAT_HTML;
193 $newentry->subject = $entry->get_title();
194 $newentry->summary = $entry->get_description();
195 $newentry->created = $entry->get_date('U');
196 $newentry->lastmodified = $entry->get_date('U');
198 $id = $DB->insert_record('post', $newentry);
201 if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) {
202 tag_set('post', $id, $tags);
206 $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => mktime()));
210 * Given an external blog object, deletes all related blog entries from the post table.
211 * NOTE: The external blog's id is saved as post.content, a field that is not oterhwise used by blog entries.
212 * @param object $externablog
214 function blog_delete_external_entries($externalblog) {
216 require_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM));
217 $DB->delete_records('post', array('content' => $externalblog->id, 'module' => 'blog_external'));
221 * Returns a URL based on the context of the current page.
222 * This URL points to blog/index.php and includes filter parameters appropriate for the current page.
224 * @param stdclass $context
227 function blog_get_context_url($context=null) {
230 $viewblogentriesurl = new moodle_url('/blog/index.php');
232 if (empty($context)) {
234 $context = $PAGE->context;
237 // Change contextlevel to SYSTEM if viewing the site course
238 if ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID) {
239 $context->contextlevel = CONTEXT_SYSTEM;
245 switch ($context->contextlevel) {
248 case CONTEXT_COURSECAT:
251 $filterparam = 'courseid';
252 $strlevel = get_string('course');
255 $filterparam = 'modid';
256 $strlevel = print_context_name($context);
259 $filterparam = 'userid';
260 $strlevel = get_string('user');
264 if (!empty($filterparam)) {
265 $viewblogentriesurl->param($filterparam, $context->instanceid);
268 return $viewblogentriesurl;
272 * This function checks that blogs are enabled, and that the user is logged in an
273 * is not the guest user.
276 function blog_is_enabled_for_user() {
278 return (!empty($CFG->bloglevel) && $CFG->bloglevel <= BLOG_GLOBAL_LEVEL && isloggedin() && !isguestuser());
282 * This function gets all of the options available for the current user in respect
285 * It loads the following if applicable:
286 * - Module options {@see blog_get_options_for_module}
287 * - Course options {@see blog_get_options_for_course}
288 * - User specific options {@see blog_get_options_for_user}
289 * - General options (BLOG_LEVEL_GLOBAL)
291 * @param moodle_page $page The page to load for (normally $PAGE)
292 * @param stdClass $userid Load for a specific user
293 * @return array An array of options organised by type.
295 function blog_get_all_options(moodle_page $page, stdClass $userid = null) {
296 global $CFG, $DB, $USER;
300 // If blogs are enabled and the user is logged in and not a guest
301 if (blog_is_enabled_for_user()) {
302 // If the context is the user then assume we want to load for the users context
303 if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) {
304 $userid = $page->context->instanceid;
306 // Check the userid var
307 if (!is_null($userid) && $userid!==$USER->id) {
308 // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't!
309 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
314 if ($CFG->useblogassociations && $page->cm !== null) {
315 // Load for the module associated with the page
316 $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user);
317 } else if ($CFG->useblogassociations && $page->course->id != SITEID) {
318 // Load the options for the course associated with the page
319 $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user);
322 // Get the options for the user
323 if ($user !== null) {
324 // Load for the requested user
325 $options[CONTEXT_USER+1] = blog_get_options_for_user($user);
327 // Load for the current user
328 $options[CONTEXT_USER] = blog_get_options_for_user();
331 // If blog level is global then display a link to view all site entries
332 if (!empty($CFG->bloglevel) && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL && has_capability('moodle/blog:view', get_context_instance(CONTEXT_SYSTEM))) {
333 $options[CONTEXT_SYSTEM] = array('viewsite' => array(
334 'string' => get_string('viewsiteentries', 'blog'),
335 'link' => new moodle_url('/blog/index.php')
339 // Return the options
344 * Get all of the blog options that relate to the passed user.
346 * If no user is passed the current user is assumed.
348 * @staticvar array $useroptions Cache so we don't have to regenerate multiple times
349 * @param stdClass $user
350 * @return array The array of options for the requested user
352 function blog_get_options_for_user(stdClass $user=null) {
355 static $useroptions = array();
358 // Blogs must be enabled and the user must be logged in
359 if (!blog_is_enabled_for_user()) {
363 // Sort out the user var
364 if ($user === null || $user->id == $USER->id) {
366 $iscurrentuser = true;
368 $iscurrentuser = false;
371 // If we've already generated serve from the cache
372 if (array_key_exists($user->id, $useroptions)) {
373 return $useroptions[$user->id];
376 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
377 $canview = has_capability('moodle/blog:view', $sitecontext);
379 if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
380 // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL
381 $options['userentries'] = array(
382 'string' => get_string('viewuserentries', 'blog', fullname($user)),
383 'link' => new moodle_url('/blog/index.php', array('userid'=>$user->id))
386 // It's the current user
388 // We can view our own blogs .... BIG surprise
389 $options['view'] = array(
390 'string' => get_string('viewallmyentries', 'blog'),
391 'link' => new moodle_url('/blog/index.php', array('userid'=>$USER->id))
394 if (has_capability('moodle/blog:create', $sitecontext)) {
395 // We can add to our own blog
396 $options['add'] = array(
397 'string' => get_string('addnewentry', 'blog'),
398 'link' => new moodle_url('/blog/edit.php', array('action'=>'add'))
403 $useroptions[$user->id] = $options;
404 // Return the options
409 * Get the blog options that relate to the given course for the given user.
411 * @staticvar array $courseoptions A cache so we can save regenerating multiple times
412 * @param stdClass $course The course to load options for
413 * @param stdClass $user The user to load options for null == current user
414 * @return array The array of options
416 function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
419 static $courseoptions = array();
423 // User must be logged in and blogs must be enabled
424 if (!blog_is_enabled_for_user()) {
428 // Check that the user can associate with the course
429 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
430 if (!has_capability('moodle/blog:associatecourse', $sitecontext)) {
433 // Generate the cache key
434 $key = $course->id.':';
440 // Serve from the cache if we've already generated for this course
441 if (array_key_exists($key, $courseoptions)) {
442 return $courseoptions[$course->id];
445 if (has_capability('moodle/blog:view', get_context_instance(CONTEXT_COURSE, $course->id))) {
447 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
448 // View entries about this course
449 $options['courseview'] = array(
450 'string' => get_string('viewcourseblogs', 'blog'),
451 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id))
454 // View MY entries about this course
455 $options['courseviewmine'] = array(
456 'string' => get_string('viewmyentriesaboutcourse', 'blog'),
457 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$USER->id))
459 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
460 // View the provided users entries about this course
461 $options['courseviewuser'] = array(
462 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)),
463 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$user->id))
468 if (has_capability('moodle/blog:create', $sitecontext)) {
469 // We can blog about this course
470 $options['courseadd'] = array(
471 'string' => get_string('blogaboutthiscourse', 'blog', get_string('course')),
472 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'courseid'=>$course->id))
477 // Cache the options for this course
478 $courseoptions[$key] = $options;
479 // Return the options
484 * Get the blog options relating to the given module for the given user
486 * @staticvar array $moduleoptions Cache
487 * @param stdClass $module The module to get options for
488 * @param stdClass $user The user to get options for null == currentuser
491 function blog_get_options_for_module(stdClass $module, stdClass $user=null) {
494 static $moduleoptions = array();
497 // User must be logged in, blogs must be enabled
498 if (!blog_is_enabled_for_user()) {
502 // Check the user can associate with the module
503 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
504 if (!has_capability('moodle/blog:associatemodule', $sitecontext)) {
508 // Generate the cache key
509 $key = $module->id.':';
515 if (array_key_exists($key, $moduleoptions)) {
516 // Serve from the cache so we don't have to regenerate
517 return $moduleoptions[$module->id];
520 if (has_capability('moodle/blog:view', get_context_instance(CONTEXT_MODULE, $module->id))) {
522 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
523 // View all entries about this module
525 $a->type = $module->modname;
526 $options['moduleview'] = array(
527 'string' => get_string('viewallmodentries', 'blog', $a),
528 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id))
531 // View MY entries about this module
532 $options['moduleviewmine'] = array(
533 'string' => get_string('viewmyentriesaboutmodule', 'blog', $module->modname),
534 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$USER->id))
536 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
537 // View the given users entries about this module
539 $a->mod = $module->modname;
540 $a->user = fullname($user);
541 $options['moduleviewuser'] = array(
542 'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a),
543 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$user->id))
548 if (has_capability('moodle/blog:create', $sitecontext)) {
549 // The user can blog about this module
550 $options['moduleadd'] = array(
551 'string' => get_string('blogaboutthismodule', 'blog', $module->modname),
552 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'modid'=>$module->id))
556 $moduleoptions[$key] = $options;
557 // Return the options
562 * This function encapsulates all the logic behind the complex
563 * navigation, titles and headings of the blog listing page, depending
564 * on URL params. It looks at URL params and at the current context level.
565 * It builds and returns an array containing:
567 * 1. heading: The heading displayed above the blog entries
568 * 2. stradd: The text to be used as the "Add entry" link
569 * 3. strview: The text to be used as the "View entries" link
570 * 4. url: The moodle_url object used as the base for add and view links
571 * 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block
573 * All other variables are set directly in $PAGE
575 * It uses the current URL to build these variables.
576 * A number of mutually exclusive use cases are used to structure this function.
580 function blog_get_headers() {
581 global $CFG, $PAGE, $DB, $USER;
583 $id = optional_param('id', null, PARAM_INT);
584 $tag = optional_param('tag', null, PARAM_NOTAGS);
585 $tagid = optional_param('tagid', null, PARAM_INT);
586 $userid = optional_param('userid', null, PARAM_INT);
587 $modid = optional_param('modid', null, PARAM_INT);
588 $entryid = optional_param('entryid', null, PARAM_INT);
589 $groupid = optional_param('groupid', null, PARAM_INT);
590 $courseid = optional_param('courseid', null, PARAM_INT);
591 $search = optional_param('search', null, PARAM_RAW);
592 $action = optional_param('action', null, PARAM_ALPHA);
593 $confirm = optional_param('confirm', false, PARAM_BOOL);
595 // Ignore userid when action == add
596 if ($action == 'add' && $userid) {
598 $PAGE->url->remove_params(array('userid'));
599 } else if ($action == 'add' && $entryid) {
601 $PAGE->url->remove_params(array('entryid'));
604 $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
606 $blogurl = new moodle_url('/blog/index.php');
608 // If the title is not yet set, it's likely that the context isn't set either, so skip this part
609 $pagetitle = $PAGE->title;
610 if (!empty($pagetitle)) {
611 $contexturl = blog_get_context_url();
613 // Look at the context URL, it may have additional params that are not in the current URL
614 if (!$blogurl->compare($contexturl)) {
615 $blogurl = $contexturl;
616 if (empty($courseid)) {
617 $courseid = $blogurl->param('courseid');
620 $modid = $blogurl->param('modid');
625 $headers['stradd'] = get_string('addnewentry', 'blog');
626 $headers['strview'] = null;
628 $site = $DB->get_record('course', array('id' => SITEID));
629 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
630 // Common Lang strings
631 $strparticipants = get_string("participants");
632 $strblogentries = get_string("blogentries", 'blog');
634 // Prepare record objects as needed
635 if (!empty($courseid)) {
636 $headers['filters']['course'] = $courseid;
637 $course = $DB->get_record('course', array('id' => $courseid));
640 if (!empty($userid)) {
641 $headers['filters']['user'] = $userid;
642 $user = $DB->get_record('user', array('id' => $userid));
645 if (!empty($groupid)) { // groupid always overrides courseid
646 $headers['filters']['group'] = $groupid;
647 $group = $DB->get_record('groups', array('id' => $groupid));
648 $course = $DB->get_record('course', array('id' => $group->courseid));
651 $PAGE->set_pagelayout('standard');
653 if (!empty($modid) && $CFG->useblogassociations && has_capability('moodle/blog:associatemodule', $sitecontext)) { // modid always overrides courseid, so the $course object may be reset here
654 $headers['filters']['module'] = $modid;
655 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case
656 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
657 $course = $DB->get_record('course', array('id' => $courseid));
658 $cm = $DB->get_record('course_modules', array('id' => $modid));
659 $cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module));
660 $cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance));
661 $a->type = get_string('modulename', $cm->modname);
662 $PAGE->set_cm($cm, $course);
663 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
664 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
667 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
668 // Note: if action is set to 'add' or 'edit', we do this at the end
669 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
670 $PAGE->navbar->add($strblogentries, $blogurl);
671 $PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
672 $PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
673 $headers['heading'] = get_string('siteblog', 'blog');
674 // $headers['strview'] = get_string('viewsiteentries', 'blog');
677 // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information
678 if (!empty($entryid)) {
679 $headers['filters']['entry'] = $entryid;
680 $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id';
681 $user = $DB->get_record_sql($sql, array($entryid));
682 $entry = $DB->get_record('post', array('id' => $entryid));
684 $blogurl->param('userid', $user->id);
686 if (!empty($course)) {
687 $mycourseid = $course->id;
688 $blogurl->param('courseid', $mycourseid);
690 $mycourseid = $site->id;
693 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$mycourseid");
694 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
695 $PAGE->navbar->add($strblogentries, $blogurl);
697 $blogurl->remove_params('userid');
698 $PAGE->navbar->add($entry->subject, $blogurl);
700 $PAGE->set_title("$site->shortname: " . fullname($user) . ": $entry->subject");
701 $PAGE->set_heading("$site->shortname: " . fullname($user) . ": $entry->subject");
702 $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user));
704 // We ignore tag and search params
705 if (empty($action) || !$CFG->useblogassociations) {
706 $headers['url'] = $blogurl;
711 // Case 3: A user's blog entries
712 if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
713 $blogurl->param('userid', $userid);
714 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$site->id");
715 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
716 $PAGE->navbar->add($strblogentries, $blogurl);
717 $PAGE->set_title("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
718 $PAGE->set_heading("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
719 $headers['heading'] = get_string('userblog', 'blog', fullname($user));
720 $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user));
724 // Case 4: No blog associations, no userid
725 if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) {
726 $PAGE->navbar->add($strblogentries, $blogurl);
727 $PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
728 $PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
729 $headers['heading'] = get_string('siteblog', 'blog');
732 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored)
733 if (!empty($userid) && !empty($modid) && empty($entryid)) {
734 $blogurl->param('userid', $userid);
735 $blogurl->param('modid', $modid);
737 // Course module navigation is handled by build_navigation as the second param
738 $headers['cm'] = $cm;
739 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
740 $PAGE->navbar->add($strblogentries, $blogurl);
742 $PAGE->set_title("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
743 $PAGE->set_heading("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
745 $a->user = fullname($user);
747 $a->type = get_string('modulename', $cm->modname);
748 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
749 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
750 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
753 // Case 6: Blog entries associated with a course by a specific user
754 if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
755 $blogurl->param('userid', $userid);
756 $blogurl->param('courseid', $courseid);
758 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$course->id");
759 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
760 $PAGE->navbar->add($strblogentries, $blogurl);
762 $PAGE->set_title("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
763 $PAGE->set_heading("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
765 $a->user = fullname($user);
766 $a->course = $course->fullname;
767 $a->type = get_string('course');
768 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
769 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
770 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
772 // Remove the userid from the URL to inform the blog_menu block correctly
773 $blogurl->remove_params(array('userid'));
776 // Case 7: Blog entries by members of a group, associated with that group's course
777 if (!empty($groupid) && empty($modid) && empty($entryid)) {
778 $blogurl->param('courseid', $course->id);
780 $PAGE->navbar->add($strblogentries, $blogurl);
781 $blogurl->remove_params(array('courseid'));
782 $blogurl->param('groupid', $groupid);
783 $PAGE->navbar->add($group->name, $blogurl);
785 $PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
786 $PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
788 $a->group = $group->name;
789 $a->course = $course->fullname;
790 $a->type = get_string('course');
791 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
792 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
793 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
796 // Case 8: Blog entries by members of a group, associated with an activity in that course
797 if (!empty($groupid) && !empty($modid) && empty($entryid)) {
798 $headers['cm'] = $cm;
799 $blogurl->param('modid', $modid);
800 $PAGE->navbar->add($strblogentries, $blogurl);
802 $blogurl->param('groupid', $groupid);
803 $PAGE->navbar->add($group->name, $blogurl);
805 $PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
806 $PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
808 $a->group = $group->name;
810 $a->type = get_string('modulename', $cm->modname);
811 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
812 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
813 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
817 // Case 9: All blog entries associated with an activity
818 if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
819 $PAGE->set_cm($cm, $course);
820 $blogurl->param('modid', $modid);
821 $PAGE->navbar->add($strblogentries, $blogurl);
822 $PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
823 $PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
824 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
825 $a->type = get_string('modulename', $cm->modname);
826 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
827 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
830 // Case 10: All blog entries associated with a course
831 if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
832 $blogurl->param('courseid', $courseid);
833 $PAGE->navbar->add($strblogentries, $blogurl);
834 $PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
835 $PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
836 $a->type = get_string('course');
837 $headers['heading'] = get_string('blogentriesabout', 'blog', $course->fullname);
838 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
839 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
840 $blogurl->remove_params(array('userid'));
843 if (!in_array($action, array('edit', 'add'))) {
845 if (!empty($tagid)) {
846 $headers['filters']['tag'] = $tagid;
847 $blogurl->param('tagid', $tagid);
848 $tagrec = $DB->get_record('tag', array('id'=>$tagid));
849 $PAGE->navbar->add($tagrec->name, $blogurl);
850 } elseif (!empty($tag)) {
851 $blogurl->param('tag', $tag);
852 $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl);
855 // Append Search info
856 if (!empty($search)) {
857 $headers['filters']['search'] = $search;
858 $blogurl->param('search', $search);
859 $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
863 // Append edit mode info
864 if (!empty($action) && $action == 'add') {
865 if (empty($modid) && empty($courseid)) {
869 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$site->id");
870 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
871 $blogurl->param('userid', $user->id);
872 $PAGE->navbar->add($strblogentries, $blogurl);
874 $PAGE->navbar->add(get_string('addnewentry', 'blog'));
875 } else if (!empty($action) && $action == 'edit') {
876 $PAGE->navbar->add(get_string('editentry', 'blog'));
879 if (empty($headers['url'])) {
880 $headers['url'] = $blogurl;
886 * Function used by the navigation system to provide links to blog preferences and external blogs.
887 * @param object $settingsnav The settings_navigation object
888 * @return navigation key
890 function blog_extend_settings_navigation($settingsnav) {
891 global $USER, $PAGE, $FULLME, $CFG, $DB, $OUTPUT;
892 $blog = $settingsnav->add(get_string('blogadministration', 'blog'));
893 $blog->forceopen = true;
895 $blog->add(get_string('preferences', 'blog'), new moodle_url('preferences.php'), navigation_node::TYPE_SETTING);
897 if ($CFG->useexternalblogs && $CFG->maxexternalblogsperuser > 0 && has_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM))) {
898 $blog->add(get_string('externalblogs', 'blog'), new moodle_url('external_blogs.php'), navigation_node::TYPE_SETTING);
905 * Shortcut function for getting a count of blog entries associated with a course or a module
906 * @param int $courseid The ID of the course
907 * @param int $cmid The ID of the course_modules
908 * @return string The number of associated entries
910 function blog_get_associated_count($courseid, $cmid=null) {
912 $context = get_context_instance(CONTEXT_COURSE, $courseid);
914 $context = get_context_instance(CONTEXT_MODULE, $cmid);
916 return $DB->count_records('blog_association', array('contextid' => $context->id));