MDL-24321 switching to stdClass in /blog/
[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/>.
18 /**
19  * Core global functions for Blog.
20  *
21  * @package    moodlecore
22  * @subpackage blog
23  * @copyright  2009 Nicolas Connault
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Library of functions and constants for blog
31  */
32 require_once($CFG->dirroot .'/blog/rsslib.php');
33 require_once($CFG->dirroot.'/tag/lib.php');
35 /**
36  * User can edit a blog entry if this is their own blog entry and they have
37  * the capability moodle/blog:create, or if they have the capability
38  * moodle/blog:manageentries.
39  *
40  * This also applies to deleting of entries.
41  */
42 function blog_user_can_edit_entry($blogentry) {
43     global $USER;
45     $sitecontext = get_context_instance(CONTEXT_SYSTEM);
47     if (has_capability('moodle/blog:manageentries', $sitecontext)) {
48         return true; // can edit any blog entry
49     }
51     if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) {
52         return true; // can edit own when having blog:create capability
53     }
55     return false;
56 }
59 /**
60  * Checks to see if a user can view the blogs of another user.
61  * Only blog level is checked here, the capabilities are enforced
62  * in blog/index.php
63  */
64 function blog_user_can_view_user_entry($targetuserid, $blogentry=null) {
65     global $CFG, $USER, $DB;
67     if (empty($CFG->bloglevel)) {
68         return false; // blog system disabled
69     }
71     if (isloggedin() && $USER->id == $targetuserid) {
72         return true; // can view own entries in any case
73     }
75     $sitecontext = get_context_instance(CONTEXT_SYSTEM);
76     if (has_capability('moodle/blog:manageentries', $sitecontext)) {
77         return true; // can manage all entries
78     }
80     // coming for 1 entry, make sure it's not a draft
81     if ($blogentry && $blogentry->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
82         return false;  // can not view draft of others
83     }
85     // coming for 1 entry, make sure user is logged in, if not a public blog
86     if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) {
87         return false;
88     }
90     switch ($CFG->bloglevel) {
91         case BLOG_GLOBAL_LEVEL:
92             return true;
93         break;
95         case BLOG_SITE_LEVEL:
96             if (isloggedin()) { // not logged in viewers forbidden
97                 return true;
98             }
99             return false;
100         break;
102         case BLOG_USER_LEVEL:
103         default:
104             $personalcontext = get_context_instance(CONTEXT_USER, $targetuserid);
105             return has_capability('moodle/user:readuserblogs', $personalcontext);
106         break;
108     }
111 /**
112  * remove all associations for the blog entries of a particular user
113  * @param int userid - id of user whose blog associations will be deleted
114  */
115 function blog_remove_associations_for_user($userid) {
116     global $DB;
117     throw new coding_exception('function blog_remove_associations_for_user() is not finished');
118     /*
119     $blogentries = blog_fetch_entries(array('user' => $userid), 'lasmodified DESC');
120     foreach ($blogentries as $entry) {
121         if (blog_user_can_edit_entry($entry)) {
122             blog_remove_associations_for_entry($entry->id);
123         }
124     }
125      */
128 /**
129  * remove all associations for the blog entries of a particular course
130  * @param int courseid - id of user whose blog associations will be deleted
131  */
132 function blog_remove_associations_for_course($courseid) {
133     global $DB;
134     $context = get_context_instance(CONTEXT_COURSE, $courseid);
135     $DB->delete_records('blog_association', array('contextid' => $context->id));
138 /**
139  * Given a record in the {blog_external} table, checks the blog's URL
140  * for new entries not yet copied into Moodle.
141  *
142  * @param object $externalblog
143  * @return boolean False if the Feed is invalid
144  */
145 function blog_sync_external_entries($externalblog) {
146     global $CFG, $DB;
147     require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
149     $rssfile = new moodle_simplepie_file($externalblog->url);
150     $filetest = new SimplePie_Locator($rssfile);
152     if (!$filetest->is_feed($rssfile)) {
153         $externalblog->failedlastsync = 1;
154         $DB->update_record('blog_external', $externalblog);
155         return false;
156     } else if (!empty($externalblog->failedlastsync)) {
157         $externalblog->failedlastsync = 0;
158         $DB->update_record('blog_external', $externalblog);
159     }
161     // Delete all blog entries associated with this external blog
162     blog_delete_external_entries($externalblog);
164     $rss = new moodle_simplepie($externalblog->url);
166     if (empty($rss->data)) {
167         return null;
168     }
170     foreach ($rss->get_items() as $entry) {
171         // If filtertags are defined, use them to filter the entries by RSS category
172         if (!empty($externalblog->filtertags)) {
173             $containsfiltertag = false;
174             $categories = $entry->get_categories();
175             $filtertags = explode(',', $externalblog->filtertags);
176             $filtertags = array_map('trim', $filtertags);
177             $filtertags = array_map('strtolower', $filtertags);
179             foreach ($categories as $category) {
180                 if (in_array(trim(strtolower($category->term)), $filtertags)) {
181                     $containsfiltertag = true;
182                 }
183             }
185             if (!$containsfiltertag) {
186                 continue;
187             }
188         }
190         $newentry = new stdClass();
191         $newentry->userid = $externalblog->userid;
192         $newentry->module = 'blog_external';
193         $newentry->content = $externalblog->id;
194         $newentry->uniquehash = $entry->get_permalink();
195         $newentry->publishstate = 'site';
196         $newentry->format = FORMAT_HTML;
197         $newentry->subject = $entry->get_title();
198         $newentry->summary = $entry->get_description();
199         $newentry->created = $entry->get_date('U');
200         $newentry->lastmodified = $entry->get_date('U');
202         $id = $DB->insert_record('post', $newentry);
204         // Set tags
205         if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) {
206             tag_set('post', $id, $tags);
207         }
208     }
210     $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => mktime()));
213 /**
214  * Given an external blog object, deletes all related blog entries from the post table.
215  * NOTE: The external blog's id is saved as post.content, a field that is not oterhwise used by blog entries.
216  * @param object $externablog
217  */
218 function blog_delete_external_entries($externalblog) {
219     global $DB;
220     require_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM));
221     $DB->delete_records('post', array('content' => $externalblog->id, 'module' => 'blog_external'));
224 /**
225  * Returns a URL based on the context of the current page.
226  * This URL points to blog/index.php and includes filter parameters appropriate for the current page.
227  *
228  * @param stdclass $context
229  * @return string
230  */
231 function blog_get_context_url($context=null) {
232     global $CFG;
234     $viewblogentriesurl = new moodle_url('/blog/index.php');
236     if (empty($context)) {
237         global $PAGE;
238         $context = $PAGE->context;
239     }
241     // Change contextlevel to SYSTEM if viewing the site course
242     if ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID) {
243         $context->contextlevel = CONTEXT_SYSTEM;
244     }
246     $filterparam = '';
247     $strlevel = '';
249     switch ($context->contextlevel) {
250         case CONTEXT_SYSTEM:
251         case CONTEXT_BLOCK:
252         case CONTEXT_COURSECAT:
253             break;
254         case CONTEXT_COURSE:
255             $filterparam = 'courseid';
256             $strlevel = get_string('course');
257             break;
258         case CONTEXT_MODULE:
259             $filterparam = 'modid';
260             $strlevel = print_context_name($context);
261             break;
262         case CONTEXT_USER:
263             $filterparam = 'userid';
264             $strlevel = get_string('user');
265             break;
266     }
268     if (!empty($filterparam)) {
269         $viewblogentriesurl->param($filterparam, $context->instanceid);
270     }
272     return $viewblogentriesurl;
275 /**
276  * This function checks that blogs are enabled, and that the user can see blogs at all
277  * @return bool
278  */
279 function blog_is_enabled_for_user() {
280     global $CFG;
281     //return (!empty($CFG->bloglevel) && $CFG->bloglevel <= BLOG_GLOBAL_LEVEL && isloggedin() && !isguestuser());
282     return (!empty($CFG->bloglevel) && (isloggedin() || ($CFG->bloglevel == BLOG_GLOBAL_LEVEL)));
285 /**
286  * This function gets all of the options available for the current user in respect
287  * to blogs.
288  *
289  * It loads the following if applicable:
290  * -  Module options {@see blog_get_options_for_module}
291  * -  Course options {@see blog_get_options_for_course}
292  * -  User specific options {@see blog_get_options_for_user}
293  * -  General options (BLOG_LEVEL_GLOBAL)
294  *
295  * @param moodle_page $page The page to load for (normally $PAGE)
296  * @param stdClass $userid Load for a specific user
297  * @return array An array of options organised by type.
298  */
299 function blog_get_all_options(moodle_page $page, stdClass $userid = null) {
300     global $CFG, $DB, $USER;
302     $options = array();
304     // If blogs are enabled and the user is logged in and not a guest
305     if (blog_is_enabled_for_user()) {
306         // If the context is the user then assume we want to load for the users context
307         if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) {
308             $userid = $page->context->instanceid;
309         }
310         // Check the userid var
311         if (!is_null($userid) && $userid!==$USER->id) {
312             // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't!
313             $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
314         } else {
315             $user = null;
316         }
318         if ($CFG->useblogassociations && $page->cm !== null) {
319             // Load for the module associated with the page
320             $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user);
321         } else if ($CFG->useblogassociations && $page->course->id != SITEID) {
322             // Load the options for the course associated with the page
323             $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user);
324         }
326         // Get the options for the user
327         if ($user !== null) {
328             // Load for the requested user
329             $options[CONTEXT_USER+1] = blog_get_options_for_user($user);
330         }
331         // Load for the current user
332         $options[CONTEXT_USER] = blog_get_options_for_user();
333     }
335     // If blog level is global then display a link to view all site entries
336     if (!empty($CFG->bloglevel) && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL && has_capability('moodle/blog:view', get_context_instance(CONTEXT_SYSTEM))) {
337         $options[CONTEXT_SYSTEM] = array('viewsite' => array(
338             'string' => get_string('viewsiteentries', 'blog'),
339             'link' => new moodle_url('/blog/index.php')
340         ));
341     }
343     // Return the options
344     return $options;
347 /**
348  * Get all of the blog options that relate to the passed user.
349  *
350  * If no user is passed the current user is assumed.
351  *
352  * @staticvar array $useroptions Cache so we don't have to regenerate multiple times
353  * @param stdClass $user
354  * @return array The array of options for the requested user
355  */
356 function blog_get_options_for_user(stdClass $user=null) {
357     global $CFG, $USER;
358     // Cache
359     static $useroptions = array();
361     $options = array();
362     // Blogs must be enabled and the user must be logged in
363     if (!blog_is_enabled_for_user()) {
364         return $options;
365     }
367     // Sort out the user var
368     if ($user === null || $user->id == $USER->id) {
369         $user = $USER;
370         $iscurrentuser = true;
371     } else {
372         $iscurrentuser = false;
373     }
375     // If we've already generated serve from the cache
376     if (array_key_exists($user->id, $useroptions)) {
377         return $useroptions[$user->id];
378     }
380     $sitecontext = get_context_instance(CONTEXT_SYSTEM);
381     $canview = has_capability('moodle/blog:view', $sitecontext);
383     if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
384         // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL
385         $options['userentries'] = array(
386             'string' => get_string('viewuserentries', 'blog', fullname($user)),
387             'link' => new moodle_url('/blog/index.php', array('userid'=>$user->id))
388         );
389     } else {
390         // It's the current user
391         if ($canview) {
392             // We can view our own blogs .... BIG surprise
393             $options['view'] = array(
394                 'string' => get_string('viewallmyentries', 'blog'),
395                 'link' => new moodle_url('/blog/index.php', array('userid'=>$USER->id))
396             );
397         }
398         if (has_capability('moodle/blog:create', $sitecontext)) {
399             // We can add to our own blog
400             $options['add'] = array(
401                 'string' => get_string('addnewentry', 'blog'),
402                 'link' => new moodle_url('/blog/edit.php', array('action'=>'add'))
403             );
404         }
405     }
406     // Cache the options
407     $useroptions[$user->id] = $options;
408     // Return the options
409     return $options;
412 /**
413  * Get the blog options that relate to the given course for the given user.
414  *
415  * @staticvar array $courseoptions A cache so we can save regenerating multiple times
416  * @param stdClass $course The course to load options for
417  * @param stdClass $user The user to load options for null == current user
418  * @return array The array of options
419  */
420 function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
421     global $CFG, $USER;
422     // Cache
423     static $courseoptions = array();
425     $options = array();
427     // User must be logged in and blogs must be enabled
428     if (!blog_is_enabled_for_user()) {
429         return $options;
430     }
432     // Check that the user can associate with the course
433     $sitecontext =      get_context_instance(CONTEXT_SYSTEM);
434     if (!has_capability('moodle/blog:associatecourse', $sitecontext)) {
435         return $options;
436     }
437     // Generate the cache key
438     $key = $course->id.':';
439     if (!empty($user)) {
440         $key .= $user->id;
441     } else {
442         $key .= $USER->id;
443     }
444     // Serve from the cache if we've already generated for this course
445     if (array_key_exists($key, $courseoptions)) {
446         return $courseoptions[$key];
447     }
449     if (has_capability('moodle/blog:view', get_context_instance(CONTEXT_COURSE, $course->id))) {
450         // We can view!
451         if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
452             // View entries about this course
453             $options['courseview'] = array(
454                 'string' => get_string('viewcourseblogs', 'blog'),
455                 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id))
456             );
457         }
458         // View MY entries about this course
459         $options['courseviewmine'] = array(
460             'string' => get_string('viewmyentriesaboutcourse', 'blog'),
461             'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$USER->id))
462         );
463         if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
464             // View the provided users entries about this course
465             $options['courseviewuser'] = array(
466                 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)),
467                 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$user->id))
468             );
469         }
470     }
472     if (has_capability('moodle/blog:create', $sitecontext)) {
473         // We can blog about this course
474         $options['courseadd'] = array(
475             'string' => get_string('blogaboutthiscourse', 'blog', get_string('course')),
476             'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'courseid'=>$course->id))
477         );
478     }
481     // Cache the options for this course
482     $courseoptions[$key] = $options;
483     // Return the options
484     return $options;
487 /**
488  * Get the blog options relating to the given module for the given user
489  *
490  * @staticvar array $moduleoptions Cache
491  * @param stdClass $module The module to get options for
492  * @param stdClass $user The user to get options for null == currentuser
493  * @return array
494  */
495 function blog_get_options_for_module(stdClass $module, stdClass $user=null) {
496     global $CFG, $USER;
497     // Cache
498     static $moduleoptions = array();
500     $options = array();
501     // User must be logged in, blogs must be enabled
502     if (!blog_is_enabled_for_user()) {
503         return $options;
504     }
506     // Check the user can associate with the module
507     $sitecontext =      get_context_instance(CONTEXT_SYSTEM);
508     if (!has_capability('moodle/blog:associatemodule', $sitecontext)) {
509         return $options;
510     }
512     // Generate the cache key
513     $key = $module->id.':';
514     if (!empty($user)) {
515         $key .= $user->id;
516     } else {
517         $key .= $USER->id;
518     }
519     if (array_key_exists($key, $moduleoptions)) {
520         // Serve from the cache so we don't have to regenerate
521         return $moduleoptions[$module->id];
522     }
524     if (has_capability('moodle/blog:view', get_context_instance(CONTEXT_MODULE, $module->id))) {
525         // We can view!
526         if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
527             // View all entries about this module
528             $a = new stdClass;
529             $a->type = $module->modname;
530             $options['moduleview'] = array(
531                 'string' => get_string('viewallmodentries', 'blog', $a),
532                 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id))
533             );
534         }
535         // View MY entries about this module
536         $options['moduleviewmine'] = array(
537             'string' => get_string('viewmyentriesaboutmodule', 'blog', $module->modname),
538             'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$USER->id))
539         );
540         if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
541             // View the given users entries about this module
542             $a = new stdClass;
543             $a->mod = $module->modname;
544             $a->user = fullname($user);
545             $options['moduleviewuser'] = array(
546                 'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a),
547                 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$user->id))
548             );
549         }
550     }
552     if (has_capability('moodle/blog:create', $sitecontext)) {
553         // The user can blog about this module
554         $options['moduleadd'] = array(
555             'string' => get_string('blogaboutthismodule', 'blog', $module->modname),
556             'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'modid'=>$module->id))
557         );
558     }
559     // Cache the options
560     $moduleoptions[$key] = $options;
561     // Return the options
562     return $options;
565 /**
566  * This function encapsulates all the logic behind the complex
567  * navigation, titles and headings of the blog listing page, depending
568  * on URL params. It looks at URL params and at the current context level.
569  * It builds and returns an array containing:
570  *
571  * 1. heading: The heading displayed above the blog entries
572  * 2. stradd:  The text to be used as the "Add entry" link
573  * 3. strview: The text to be used as the "View entries" link
574  * 4. url:     The moodle_url object used as the base for add and view links
575  * 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block
576  *
577  * All other variables are set directly in $PAGE
578  *
579  * It uses the current URL to build these variables.
580  * A number of mutually exclusive use cases are used to structure this function.
581  *
582  * @return array
583  */
584 function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=null) {
585     global $CFG, $PAGE, $DB, $USER;
587     $id       = optional_param('id', null, PARAM_INT);
588     $tag      = optional_param('tag', null, PARAM_NOTAGS);
589     $tagid    = optional_param('tagid', $tagid, PARAM_INT);
590     $userid   = optional_param('userid', $userid, PARAM_INT);
591     $modid    = optional_param('modid', null, PARAM_INT);
592     $entryid  = optional_param('entryid', null, PARAM_INT);
593     $groupid  = optional_param('groupid', $groupid, PARAM_INT);
594     $courseid = optional_param('courseid', $courseid, PARAM_INT);
595     $search   = optional_param('search', null, PARAM_RAW);
596     $action   = optional_param('action', null, PARAM_ALPHA);
597     $confirm  = optional_param('confirm', false, PARAM_BOOL);
599     // Ignore userid when action == add
600     if ($action == 'add' && $userid) {
601         unset($userid);
602         $PAGE->url->remove_params(array('userid'));
603     } else if ($action == 'add' && $entryid) {
604         unset($entryid);
605         $PAGE->url->remove_params(array('entryid'));
606     }
608     $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
610     $blogurl = new moodle_url('/blog/index.php');
612     // If the title is not yet set, it's likely that the context isn't set either, so skip this part
613     $pagetitle = $PAGE->title;
614     if (!empty($pagetitle)) {
615         $contexturl = blog_get_context_url();
617         // Look at the context URL, it may have additional params that are not in the current URL
618         if (!$blogurl->compare($contexturl)) {
619             $blogurl = $contexturl;
620             if (empty($courseid)) {
621                 $courseid = $blogurl->param('courseid');
622             }
623             if (empty($modid)) {
624                 $modid = $blogurl->param('modid');
625             }
626         }
627     }
629     $headers['stradd'] = get_string('addnewentry', 'blog');
630     $headers['strview'] = null;
632     $site = $DB->get_record('course', array('id' => SITEID));
633     $sitecontext = get_context_instance(CONTEXT_SYSTEM);
634     // Common Lang strings
635     $strparticipants = get_string("participants");
636     $strblogentries  = get_string("blogentries", 'blog');
638     // Prepare record objects as needed
639     if (!empty($courseid)) {
640         $headers['filters']['course'] = $courseid;
641         $course = $DB->get_record('course', array('id' => $courseid));
642     }
644     if (!empty($userid)) {
645         $headers['filters']['user'] = $userid;
646         $user = $DB->get_record('user', array('id' => $userid));
647     }
649     if (!empty($groupid)) { // groupid always overrides courseid
650         $headers['filters']['group'] = $groupid;
651         $group = $DB->get_record('groups', array('id' => $groupid));
652         $course = $DB->get_record('course', array('id' => $group->courseid));
653     }
655     $PAGE->set_pagelayout('standard');
657     if (!empty($modid) && $CFG->useblogassociations && has_capability('moodle/blog:associatemodule', $sitecontext)) { // modid always overrides courseid, so the $course object may be reset here
658         $headers['filters']['module'] = $modid;
659         // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case
660         $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
661         $course = $DB->get_record('course', array('id' => $courseid));
662         $cm = $DB->get_record('course_modules', array('id' => $modid));
663         $cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module));
664         $cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance));
665         $a = new stdClass();
666         $a->type = get_string('modulename', $cm->modname);
667         $PAGE->set_cm($cm, $course);
668         $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
669         $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
670     }
672     // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
673     // Note: if action is set to 'add' or 'edit', we do this at the end
674     if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
675         $PAGE->navbar->add($strblogentries, $blogurl);
676         $PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
677         $PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
678         $headers['heading'] = get_string('siteblog', 'blog', $site->shortname);
679         // $headers['strview'] = get_string('viewsiteentries', 'blog');
680     }
682     // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information
683     if (!empty($entryid)) {
684         $headers['filters']['entry'] = $entryid;
685         $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id';
686         $user = $DB->get_record_sql($sql, array($entryid));
687         $entry = $DB->get_record('post', array('id' => $entryid));
689         $blogurl->param('userid', $user->id);
691         if (!empty($course)) {
692             $mycourseid = $course->id;
693             $blogurl->param('courseid', $mycourseid);
694         } else {
695             $mycourseid = $site->id;
696         }
698         $PAGE->navbar->add($strblogentries, $blogurl);
700         $blogurl->remove_params('userid');
701         $PAGE->navbar->add($entry->subject, $blogurl);
703         $PAGE->set_title("$site->shortname: " . fullname($user) . ": $entry->subject");
704         $PAGE->set_heading("$site->shortname: " . fullname($user) . ": $entry->subject");
705         $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user));
707         // We ignore tag and search params
708         if (empty($action) || !$CFG->useblogassociations) {
709             $headers['url'] = $blogurl;
710             return $headers;
711         }
712     }
714     // Case 3: A user's blog entries
715     if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
716         $blogurl->param('userid', $userid);
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->set_title("$site->shortname: " . get_string('blog', 'blog'));
727         $PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
728         $headers['heading'] = get_string('siteblog', 'blog', $site->shortname);
729     } else
731     // Case 5: Blog entries associated with an activity by a specific user (courseid ignored)
732     if (!empty($userid) && !empty($modid) && empty($entryid)) {
733         $blogurl->param('userid', $userid);
734         $blogurl->param('modid', $modid);
736         // Course module navigation is handled by build_navigation as the second param
737         $headers['cm'] = $cm;
738         $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
739         $PAGE->navbar->add($strblogentries, $blogurl);
741         $PAGE->set_title("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
742         $PAGE->set_heading("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
744         $a = new stdClass();
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($strblogentries, $blogurl);
760         $PAGE->set_title("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
761         $PAGE->set_heading("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
763         $a = new stdClass();
764         $a->user = fullname($user);
765         $a->course = $course->fullname;
766         $a->type = get_string('course');
767         $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
768         $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
769         $headers['strview'] = get_string('viewblogentries', 'blog', $a);
771         // Remove the userid from the URL to inform the blog_menu block correctly
772         $blogurl->remove_params(array('userid'));
773     } else
775     // Case 7: Blog entries by members of a group, associated with that group's course
776     if (!empty($groupid) && empty($modid) && empty($entryid)) {
777         $blogurl->param('courseid', $course->id);
779         $PAGE->navbar->add($strblogentries, $blogurl);
780         $blogurl->remove_params(array('courseid'));
781         $blogurl->param('groupid', $groupid);
782         $PAGE->navbar->add($group->name, $blogurl);
784         $PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
785         $PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
787         $a = new stdClass();
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 = new stdClass();
809         $a->group = $group->name;
810         $a->mod = $cm->name;
811         $a->type = get_string('modulename', $cm->modname);
812         $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
813         $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
814         $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
816     } else
818     // Case 9: All blog entries associated with an activity
819     if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
820         $PAGE->set_cm($cm, $course);
821         $blogurl->param('modid', $modid);
822         $PAGE->navbar->add($strblogentries, $blogurl);
823         $PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
824         $PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
825         $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
826         $a = new stdClass();
827         $a->type = get_string('modulename', $cm->modname);
828         $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
829         $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
830     } else
832     // Case 10: All blog entries associated with a course
833     if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
834         $blogurl->param('courseid', $courseid);
835         $PAGE->navbar->add($strblogentries, $blogurl);
836         $PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
837         $PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
838         $a = new stdClass();
839         $a->type = get_string('course');
840         $headers['heading'] = get_string('blogentriesabout', 'blog', $course->fullname);
841         $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
842         $headers['strview'] = get_string('viewblogentries', 'blog', $a);
843         $blogurl->remove_params(array('userid'));
844     }
846     if (!in_array($action, array('edit', 'add'))) {
847         // Append Tag info
848         if (!empty($tagid)) {
849             $headers['filters']['tag'] = $tagid;
850             $blogurl->param('tagid', $tagid);
851             $tagrec = $DB->get_record('tag', array('id'=>$tagid));
852             $PAGE->navbar->add($tagrec->name, $blogurl);
853         } elseif (!empty($tag)) {
854             $blogurl->param('tag', $tag);
855             $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl);
856         }
858         // Append Search info
859         if (!empty($search)) {
860             $headers['filters']['search'] = $search;
861             $blogurl->param('search', $search);
862             $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
863         }
864     }
866     // Append edit mode info
867     if (!empty($action) && $action == 'add') {
869     } else if (!empty($action) && $action == 'edit') {
870         $PAGE->navbar->add(get_string('editentry', 'blog'));
871     }
873     if (empty($headers['url'])) {
874         $headers['url'] = $blogurl;
875     }
876     return $headers;
879 /**
880  * Shortcut function for getting a count of blog entries associated with a course or a module
881  * @param int $courseid The ID of the course
882  * @param int $cmid The ID of the course_modules
883  * @return string The number of associated entries
884  */
885 function blog_get_associated_count($courseid, $cmid=null) {
886     global $DB;
887     $context = get_context_instance(CONTEXT_COURSE, $courseid);
888     if ($cmid) {
889         $context = get_context_instance(CONTEXT_MODULE, $cmid);
890     }
891     return $DB->count_records('blog_association', array('contextid' => $context->id));