e7203ec930b97b3f445c07bb62cd1604029ba137
[moodle.git] / blog / lib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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.
14 //
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/>.
19 /**
20  * Core global functions for Blog.
21  *
22  * @package    moodlecore
23  * @subpackage blog
24  * @copyright  2009 Nicolas Connault
25  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26  */
28 /**
29  * Library of functions and constants for blog
30  */
31 require_once($CFG->dirroot .'/blog/rsslib.php');
32 require_once($CFG->dirroot.'/tag/lib.php');
34 /**
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.
38  *
39  * This also applies to deleting of entries.
40  */
41 function blog_user_can_edit_entry($blogentry) {
42     global $USER;
44     $sitecontext = get_context_instance(CONTEXT_SYSTEM);
46     if (has_capability('moodle/blog:manageentries', $sitecontext)) {
47         return true; // can edit any blog entry
48     }
50     if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) {
51         return true; // can edit own when having blog:create capability
52     }
54     return false;
55 }
58 /**
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
61  * in blog/index.php
62  */
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
68     }
70     if (isloggdin() && $USER->id == $targetuserid) {
71         return true; // can view own entries in any case
72     }
74     $sitecontext = get_context_instance(CONTEXT_SYSTEM);
75     if (has_capability('moodle/blog:manageentries', $sitecontext)) {
76         return true; // can manage all entries
77     }
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
82     }
84     // coming for 1 entry, make sure user is logged in, if not a public blog
85     if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) {
86         return false;
87     }
89     switch ($CFG->bloglevel) {
90         case BLOG_GLOBAL_LEVEL:
91             return true;
92         break;
94         case BLOG_SITE_LEVEL:
95             if (isloggedin()) { // not logged in viewers forbidden
96                 return true;
97             }
98             return false;
99         break;
101         case BLOG_USER_LEVEL:
102         default:
103             $personalcontext = get_context_instance(CONTEXT_USER, $targetuserid);
104             return has_capability('moodle/user:readuserblogs', $personalcontext);
105         break;
107     }
110 /**
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
113  */
114 function blog_remove_associations_for_user($userid) {
115     global $DB;
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);
120         }
121     }
124 /**
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
127  */
128 function blog_remove_associations_for_course($courseid) {
129     global $DB;
130     $context = get_context_instance(CONTEXT_COURSE, $courseid);
131     $DB->delete_records('blog_association', array('contextid' => $context->id));
134 /**
135  * Given a record in the {blog_external} table, checks the blog's URL
136  * for new entries not yet copied into Moodle.
137  *
138  * @param object $externalblog
139  * @return boolean False if the Feed is invalid
140  */
141 function blog_sync_external_entries($externalblog) {
142     global $CFG, $DB;
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);
151         return false;
152     } else if ($externalblog->failedlastsync) {
153         $externalblog->failedlastsync = 0;
154         $DB->update_record('blog_external', $externalblog);
155     }
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)) {
163         return null;
164     }
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;
178                 }
179             }
181             if (!$containsfiltertag) {
182                 continue;
183             }
184         }
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);
200         // Set tags
201         if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) {
202             tag_set('post', $id, $tags);
203         }
204     }
206     $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => mktime()));
209 /**
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
213  */
214 function blog_delete_external_entries($externalblog) {
215     global $DB;
216     require_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM));
217     $DB->delete_records('post', array('content' => $externalblog->id, 'module' => 'blog_external'));
220 /**
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.
223  *
224  * @param stdclass $context
225  * @return string
226  */
227 function blog_get_context_url($context=null) {
228     global $CFG;
230     $viewblogentriesurl = new moodle_url('/blog/index.php');
232     if (empty($context)) {
233         global $PAGE;
234         $context = $PAGE->context;
235     }
237     // Change contextlevel to SYSTEM if viewing the site course
238     if ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID) {
239         $context->contextlevel = CONTEXT_SYSTEM;
240     }
242     $filterparam = '';
243     $strlevel = '';
245     switch ($context->contextlevel) {
246         case CONTEXT_SYSTEM:
247         case CONTEXT_BLOCK:
248         case CONTEXT_COURSECAT:
249             break;
250         case CONTEXT_COURSE:
251             $filterparam = 'courseid';
252             $strlevel = get_string('course');
253             break;
254         case CONTEXT_MODULE:
255             $filterparam = 'modid';
256             $strlevel = print_context_name($context);
257             break;
258         case CONTEXT_USER:
259             $filterparam = 'userid';
260             $strlevel = get_string('user');
261             break;
262     }
264     if (!empty($filterparam)) {
265         $viewblogentriesurl->param($filterparam, $context->instanceid);
266     }
268     return $viewblogentriesurl;
271 /**
272  * This function checks that blogs are enabled, and that the user is logged in an
273  * is not the guest user.
274  * @return bool
275  */
276 function blog_is_enabled_for_user() {
277     global $CFG;
278     return (!empty($CFG->bloglevel) && $CFG->bloglevel <= BLOG_GLOBAL_LEVEL && isloggedin() && !isguestuser());
281 /**
282  * This function gets all of the options available for the current user in respect
283  * to blogs.
284  * 
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)
290  *
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.
294  */
295 function blog_get_all_options(moodle_page $page, stdClass $userid = null) {
296     global $CFG, $DB, $USER;
298     $options = array();
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;
305         }
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);
310         } else {
311             $user = null;
312         }
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);
320         }
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);
326         }
327         // Load for the current user
328         $options[CONTEXT_USER] = blog_get_options_for_user();
329     }
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')
336         ));
337     }
339     // Return the options
340     return $options;
343 /**
344  * Get all of the blog options that relate to the passed user.
345  *
346  * If no user is passed the current user is assumed.
347  *
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
351  */
352 function blog_get_options_for_user(stdClass $user=null) {
353     global $CFG, $USER;
354     // Cache
355     static $useroptions = array();
357     $options = array();
358     // Blogs must be enabled and the user must be logged in
359     if (!blog_is_enabled_for_user()) {
360         return $options;
361     }
363     // Sort out the user var
364     if ($user === null || $user->id == $USER->id) {
365         $user = $USER;
366         $iscurrentuser = true;
367     } else {
368         $iscurrentuser = false;
369     }
371     // If we've already generated serve from the cache
372     if (array_key_exists($user->id, $useroptions)) {
373         return $useroptions[$user->id];
374     }
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))
384         );
385     } else {
386         // It's the current user
387         if ($canview) {
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))
392             );
393         }
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'))
399             );
400         }
401     }
402     // Cache the options
403     $useroptions[$user->id] = $options;
404     // Return the options
405     return $options;
408 /**
409  * Get the blog options that relate to the given course for the given user.
410  *
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
415  */
416 function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
417     global $CFG, $USER;
418     // Cache
419     static $courseoptions = array();
420     
421     $options = array();
423     // User must be logged in and blogs must be enabled
424     if (!blog_is_enabled_for_user()) {
425         return $options;
426     }
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)) {
431         return $options;
432     }
433     // Generate the cache key
434     $key = $course->id.':';
435     if (!empty($user)) {
436         $key .= $user->id;
437     } else {
438         $key .= $USER->id;
439     }
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];
443     }
444     
445     if (has_capability('moodle/blog:view', get_context_instance(CONTEXT_COURSE, $course->id))) {
446         // We can view!
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))
452             );
453         }
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))
458         );
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))
464             );
465         }
466     }
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))
473         );
474     }
477     // Cache the options for this course
478     $courseoptions[$key] = $options;
479     // Return the options
480     return $options;
483 /**
484  * Get the blog options relating to the given module for the given user
485  *
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
489  * @return array
490  */
491 function blog_get_options_for_module(stdClass $module, stdClass $user=null) {
492     global $CFG, $USER;
493     // Cache
494     static $moduleoptions = array();
496     $options = array();
497     // User must be logged in, blogs must be enabled
498     if (!blog_is_enabled_for_user()) {
499         return $options;
500     }
502     // Check the user can associate with the module
503     $sitecontext =      get_context_instance(CONTEXT_SYSTEM);
504     if (!has_capability('moodle/blog:associatemodule', $sitecontext)) {
505         return $options;
506     }
508     // Generate the cache key
509     $key = $module->id.':';
510     if (!empty($user)) {
511         $key .= $user->id;
512     } else {
513         $key .= $USER->id;
514     }
515     if (array_key_exists($key, $moduleoptions)) {
516         // Serve from the cache so we don't have to regenerate
517         return $moduleoptions[$module->id];
518     }
520     if (has_capability('moodle/blog:view', get_context_instance(CONTEXT_MODULE, $module->id))) {
521         // We can view!
522         if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
523             // View all entries about this module
524             $a = new stdClass;
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))
529             );
530         }
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))
535         );
536         if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
537             // View the given users entries about this module
538             $a = new stdClass;
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))
544             );
545         }
546     }
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))
553         );
554     }
555     // Cache the options
556     $moduleoptions[$key] = $options;
557     // Return the options
558     return $options;
561 /**
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:
566  *
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
572  *
573  * All other variables are set directly in $PAGE
574  *
575  * It uses the current URL to build these variables.
576  * A number of mutually exclusive use cases are used to structure this function.
577  *
578  * @return array
579  */
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) {
597         unset($userid);
598         $PAGE->url->remove_params(array('userid'));
599     } else if ($action == 'add' && $entryid) {
600         unset($entryid);
601         $PAGE->url->remove_params(array('entryid'));
602     }
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');
618             }
619             if (empty($modid)) {
620                 $modid = $blogurl->param('modid');
621             }
622         }
623     }
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));
638     }
640     if (!empty($userid)) {
641         $headers['filters']['user'] = $userid;
642         $user = $DB->get_record('user', array('id' => $userid));
643     }
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));
649     }
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);
665     }
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');
675     }
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);
689         } else {
690             $mycourseid = $site->id;
691         }
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;
707             return $headers;
708         }
709     }
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));
722     } else
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');
730     } else
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);
746         $a->mod = $cm->name;
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);
751     } else
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'));
774     } else
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);
794     } else
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;
809         $a->mod = $cm->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);
815     } else
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);
828     } else
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'));
841     }
843     if (!in_array($action, array('edit', 'add'))) {
844         // Append Tag info
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);
853         }
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());
860         }
861     }
863     // Append edit mode info
864     if (!empty($action) && $action == 'add') {
865         if (empty($modid) && empty($courseid)) {
866             if (empty($user)) {
867                 $user = $USER;
868             }
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);
873         }
874         $PAGE->navbar->add(get_string('addnewentry', 'blog'));
875     } else if (!empty($action) && $action == 'edit') {
876         $PAGE->navbar->add(get_string('editentry', 'blog'));
877     }
879     if (empty($headers['url'])) {
880         $headers['url'] = $blogurl;
881     }
882     return $headers;
885 /**
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
889  */
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);
899     }
901     return $blog;
904 /**
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
909  */
910 function blog_get_associated_count($courseid, $cmid=null) {
911     global $DB;
912     $context = get_context_instance(CONTEXT_COURSE, $courseid);
913     if ($cmid) {
914         $context = get_context_instance(CONTEXT_MODULE, $cmid);
915     }
916     return $DB->count_records('blog_association', array('contextid' => $context->id));