MDL-39434 Bump main version to observe all deps
[moodle.git] / blog / lib.php
blob799e0d2fc3db0b53205a520ad1fcc60ce75a89b1
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.
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.
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
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Library of functions and constants for blog
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.
40 * This also applies to deleting of entries.
42 function blog_user_can_edit_entry($blogentry) {
43 global $USER;
45 $sitecontext = context_system::instance();
47 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
48 return true; // can edit any blog entry
51 if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) {
52 return true; // can edit own when having blog:create capability
55 return false;
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
64 function blog_user_can_view_user_entry($targetuserid, $blogentry=null) {
65 global $CFG, $USER, $DB;
67 if (empty($CFG->enableblogs)) {
68 return false; // blog system disabled
71 if (isloggedin() && $USER->id == $targetuserid) {
72 return true; // can view own entries in any case
75 $sitecontext = context_system::instance();
76 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
77 return true; // can manage all entries
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
85 // coming for 0 entry, make sure user is logged in, if not a public blog
86 if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) {
87 return false;
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;
99 return false;
100 break;
102 case BLOG_USER_LEVEL:
103 default:
104 $personalcontext = context_user::instance($targetuserid);
105 return has_capability('moodle/user:readuserblogs', $personalcontext);
106 break;
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
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');
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);
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
132 function blog_remove_associations_for_course($courseid) {
133 global $DB;
134 $context = context_course::instance($courseid);
135 $DB->delete_records('blog_association', array('contextid' => $context->id));
139 * Given a record in the {blog_external} table, checks the blog's URL
140 * for new entries not yet copied into Moodle.
141 * Also attempts to identify and remove deleted blog entries
143 * @param object $externalblog
144 * @return boolean False if the Feed is invalid
146 function blog_sync_external_entries($externalblog) {
147 global $CFG, $DB;
148 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
150 $rss = new moodle_simplepie();
151 $rssfile = $rss->registry->create('File', array($externalblog->url));
152 $filetest = $rss->registry->create('Locator', array($rssfile));
154 if (!$filetest->is_feed($rssfile)) {
155 $externalblog->failedlastsync = 1;
156 $DB->update_record('blog_external', $externalblog);
157 return false;
158 } else if (!empty($externalblog->failedlastsync)) {
159 $externalblog->failedlastsync = 0;
160 $DB->update_record('blog_external', $externalblog);
163 $rss->set_feed_url($externalblog->url);
164 $rss->init();
166 if (empty($rss->data)) {
167 return null;
169 //used to identify blog posts that have been deleted from the source feed
170 $oldesttimestamp = null;
171 $uniquehashes = array();
173 foreach ($rss->get_items() as $entry) {
174 // If filtertags are defined, use them to filter the entries by RSS category
175 if (!empty($externalblog->filtertags)) {
176 $containsfiltertag = false;
177 $categories = $entry->get_categories();
178 $filtertags = explode(',', $externalblog->filtertags);
179 $filtertags = array_map('trim', $filtertags);
180 $filtertags = array_map('strtolower', $filtertags);
182 foreach ($categories as $category) {
183 if (in_array(trim(strtolower($category->term)), $filtertags)) {
184 $containsfiltertag = true;
188 if (!$containsfiltertag) {
189 continue;
193 $uniquehashes[] = $entry->get_permalink();
195 $newentry = new stdClass();
196 $newentry->userid = $externalblog->userid;
197 $newentry->module = 'blog_external';
198 $newentry->content = $externalblog->id;
199 $newentry->uniquehash = $entry->get_permalink();
200 $newentry->publishstate = 'site';
201 $newentry->format = FORMAT_HTML;
202 // Clean subject of html, just in case
203 $newentry->subject = clean_param($entry->get_title(), PARAM_TEXT);
204 // Observe 128 max chars in DB
205 // TODO: +1 to raise this to 255
206 if (textlib::strlen($newentry->subject) > 128) {
207 $newentry->subject = textlib::substr($newentry->subject, 0, 125) . '...';
209 $newentry->summary = $entry->get_description();
211 //used to decide whether to insert or update
212 //uses enty permalink plus creation date if available
213 $existingpostconditions = array('uniquehash' => $entry->get_permalink());
215 //our DB doesnt allow null creation or modified timestamps so check the external blog supplied one
216 $entrydate = $entry->get_date('U');
217 if (!empty($entrydate)) {
218 $existingpostconditions['created'] = $entrydate;
221 //the post ID or false if post not found in DB
222 $postid = $DB->get_field('post', 'id', $existingpostconditions);
224 $timestamp = null;
225 if (empty($entrydate)) {
226 $timestamp = time();
227 } else {
228 $timestamp = $entrydate;
231 //only set created if its a new post so we retain the original creation timestamp if the post is edited
232 if ($postid === false) {
233 $newentry->created = $timestamp;
235 $newentry->lastmodified = $timestamp;
237 if (empty($oldesttimestamp) || $timestamp < $oldesttimestamp) {
238 //found an older post
239 $oldesttimestamp = $timestamp;
242 if (textlib::strlen($newentry->uniquehash) > 255) {
243 // The URL for this item is too long for the field. Rather than add
244 // the entry without the link we will skip straight over it.
245 // RSS spec says recommended length 500, we use 255.
246 debugging('External blog entry skipped because of oversized URL', DEBUG_DEVELOPER);
247 continue;
250 if ($postid === false) {
251 $id = $DB->insert_record('post', $newentry);
253 // Set tags
254 if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) {
255 tag_set('post', $id, $tags);
257 } else {
258 $newentry->id = $postid;
259 $DB->update_record('post', $newentry);
263 // Look at the posts we have in the database to check if any of them have been deleted from the feed.
264 // Only checking posts within the time frame returned by the rss feed. Older items may have been deleted or
265 // may just not be returned anymore. We can't tell the difference so we leave older posts alone.
266 $sql = "SELECT id, uniquehash
267 FROM {post}
268 WHERE module = 'blog_external'
269 AND " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text(':blogid') . "
270 AND created > :ts";
271 $dbposts = $DB->get_records_sql($sql, array('blogid' => $externalblog->id, 'ts' => $oldesttimestamp));
273 $todelete = array();
274 foreach($dbposts as $dbpost) {
275 if ( !in_array($dbpost->uniquehash, $uniquehashes) ) {
276 $todelete[] = $dbpost->id;
279 $DB->delete_records_list('post', 'id', $todelete);
281 $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => time()));
285 * Given an external blog object, deletes all related blog entries from the post table.
286 * NOTE: The external blog's id is saved as post.content, a field that is not oterhwise used by blog entries.
287 * @param object $externablog
289 function blog_delete_external_entries($externalblog) {
290 global $DB;
291 require_capability('moodle/blog:manageexternal', context_system::instance());
292 $DB->delete_records_select('post',
293 "module='blog_external' AND " . $DB->sql_compare_text('content') . " = ?",
294 array($externalblog->id));
298 * This function checks that blogs are enabled, and that the user can see blogs at all
299 * @return bool
301 function blog_is_enabled_for_user() {
302 global $CFG;
303 return (!empty($CFG->enableblogs) && (isloggedin() || ($CFG->bloglevel == BLOG_GLOBAL_LEVEL)));
307 * This function gets all of the options available for the current user in respect
308 * to blogs.
310 * It loads the following if applicable:
311 * - Module options {@see blog_get_options_for_module}
312 * - Course options {@see blog_get_options_for_course}
313 * - User specific options {@see blog_get_options_for_user}
314 * - General options (BLOG_LEVEL_GLOBAL)
316 * @param moodle_page $page The page to load for (normally $PAGE)
317 * @param stdClass $userid Load for a specific user
318 * @return array An array of options organised by type.
320 function blog_get_all_options(moodle_page $page, stdClass $userid = null) {
321 global $CFG, $DB, $USER;
323 $options = array();
325 // If blogs are enabled and the user is logged in and not a guest
326 if (blog_is_enabled_for_user()) {
327 // If the context is the user then assume we want to load for the users context
328 if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) {
329 $userid = $page->context->instanceid;
331 // Check the userid var
332 if (!is_null($userid) && $userid!==$USER->id) {
333 // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't!
334 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
335 } else {
336 $user = null;
339 if ($CFG->useblogassociations && $page->cm !== null) {
340 // Load for the module associated with the page
341 $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user);
342 } else if ($CFG->useblogassociations && $page->course->id != SITEID) {
343 // Load the options for the course associated with the page
344 $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user);
347 // Get the options for the user
348 if ($user !== null and !isguestuser($user)) {
349 // Load for the requested user
350 $options[CONTEXT_USER+1] = blog_get_options_for_user($user);
352 // Load for the current user
353 if (isloggedin() and !isguestuser()) {
354 $options[CONTEXT_USER] = blog_get_options_for_user();
358 // If blog level is global then display a link to view all site entries
359 if (!empty($CFG->enableblogs) && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL && has_capability('moodle/blog:view', context_system::instance())) {
360 $options[CONTEXT_SYSTEM] = array('viewsite' => array(
361 'string' => get_string('viewsiteentries', 'blog'),
362 'link' => new moodle_url('/blog/index.php')
366 // Return the options
367 return $options;
371 * Get all of the blog options that relate to the passed user.
373 * If no user is passed the current user is assumed.
375 * @staticvar array $useroptions Cache so we don't have to regenerate multiple times
376 * @param stdClass $user
377 * @return array The array of options for the requested user
379 function blog_get_options_for_user(stdClass $user=null) {
380 global $CFG, $USER;
381 // Cache
382 static $useroptions = array();
384 $options = array();
385 // Blogs must be enabled and the user must be logged in
386 if (!blog_is_enabled_for_user()) {
387 return $options;
390 // Sort out the user var
391 if ($user === null || $user->id == $USER->id) {
392 $user = $USER;
393 $iscurrentuser = true;
394 } else {
395 $iscurrentuser = false;
398 // If we've already generated serve from the cache
399 if (array_key_exists($user->id, $useroptions)) {
400 return $useroptions[$user->id];
403 $sitecontext = context_system::instance();
404 $canview = has_capability('moodle/blog:view', $sitecontext);
406 if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
407 // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL
408 $options['userentries'] = array(
409 'string' => get_string('viewuserentries', 'blog', fullname($user)),
410 'link' => new moodle_url('/blog/index.php', array('userid'=>$user->id))
412 } else {
413 // It's the current user
414 if ($canview) {
415 // We can view our own blogs .... BIG surprise
416 $options['view'] = array(
417 'string' => get_string('viewallmyentries', 'blog'),
418 'link' => new moodle_url('/blog/index.php', array('userid'=>$USER->id))
421 if (has_capability('moodle/blog:create', $sitecontext)) {
422 // We can add to our own blog
423 $options['add'] = array(
424 'string' => get_string('addnewentry', 'blog'),
425 'link' => new moodle_url('/blog/edit.php', array('action'=>'add'))
429 if ($canview && $CFG->enablerssfeeds) {
430 $options['rss'] = array(
431 'string' => get_string('rssfeed', 'blog'),
432 'link' => new moodle_url(rss_get_url($sitecontext->id, $USER->id, 'blog', 'user/'.$user->id))
436 // Cache the options
437 $useroptions[$user->id] = $options;
438 // Return the options
439 return $options;
443 * Get the blog options that relate to the given course for the given user.
445 * @staticvar array $courseoptions A cache so we can save regenerating multiple times
446 * @param stdClass $course The course to load options for
447 * @param stdClass $user The user to load options for null == current user
448 * @return array The array of options
450 function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
451 global $CFG, $USER;
452 // Cache
453 static $courseoptions = array();
455 $options = array();
457 // User must be logged in and blogs must be enabled
458 if (!blog_is_enabled_for_user()) {
459 return $options;
462 // Check that the user can associate with the course
463 $sitecontext = context_system::instance();
464 // Generate the cache key
465 $key = $course->id.':';
466 if (!empty($user)) {
467 $key .= $user->id;
468 } else {
469 $key .= $USER->id;
471 // Serve from the cache if we've already generated for this course
472 if (array_key_exists($key, $courseoptions)) {
473 return $courseoptions[$key];
477 if (has_capability('moodle/blog:view', $sitecontext)) {
478 // We can view!
479 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
480 // View entries about this course
481 $options['courseview'] = array(
482 'string' => get_string('viewcourseblogs', 'blog'),
483 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id))
486 // View MY entries about this course
487 $options['courseviewmine'] = array(
488 'string' => get_string('viewmyentriesaboutcourse', 'blog'),
489 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $USER->id))
491 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
492 // View the provided users entries about this course
493 $options['courseviewuser'] = array(
494 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)),
495 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $user->id))
500 if (has_capability('moodle/blog:create', $sitecontext)) {
501 // We can blog about this course
502 $options['courseadd'] = array(
503 'string' => get_string('blogaboutthiscourse', 'blog'),
504 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'courseid' => $course->id))
509 // Cache the options for this course
510 $courseoptions[$key] = $options;
511 // Return the options
512 return $options;
516 * Get the blog options relating to the given module for the given user
518 * @staticvar array $moduleoptions Cache
519 * @param stdClass|cm_info $module The module to get options for
520 * @param stdClass $user The user to get options for null == currentuser
521 * @return array
523 function blog_get_options_for_module($module, $user=null) {
524 global $CFG, $USER;
525 // Cache
526 static $moduleoptions = array();
528 $options = array();
529 // User must be logged in, blogs must be enabled
530 if (!blog_is_enabled_for_user()) {
531 return $options;
534 $sitecontext = context_system::instance();
536 // Generate the cache key
537 $key = $module->id.':';
538 if (!empty($user)) {
539 $key .= $user->id;
540 } else {
541 $key .= $USER->id;
543 if (array_key_exists($key, $moduleoptions)) {
544 // Serve from the cache so we don't have to regenerate
545 return $moduleoptions[$key];
549 if (has_capability('moodle/blog:view', $sitecontext)) {
550 // Save correct module name for later usage.
551 $modulename = get_string('modulename', $module->modname);
553 // We can view!
554 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
555 // View all entries about this module
556 $a = new stdClass;
557 $a->type = $modulename;
558 $options['moduleview'] = array(
559 'string' => get_string('viewallmodentries', 'blog', $a),
560 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id))
563 // View MY entries about this module
564 $options['moduleviewmine'] = array(
565 'string' => get_string('viewmyentriesaboutmodule', 'blog', $modulename),
566 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$USER->id))
568 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
569 // View the given users entries about this module
570 $a = new stdClass;
571 $a->mod = $modulename;
572 $a->user = fullname($user);
573 $options['moduleviewuser'] = array(
574 'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a),
575 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$user->id))
580 if (has_capability('moodle/blog:create', $sitecontext)) {
581 // The user can blog about this module
582 $options['moduleadd'] = array(
583 'string' => get_string('blogaboutthismodule', 'blog', $modulename),
584 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'modid'=>$module->id))
587 // Cache the options
588 $moduleoptions[$key] = $options;
589 // Return the options
590 return $options;
594 * This function encapsulates all the logic behind the complex
595 * navigation, titles and headings of the blog listing page, depending
596 * on URL params. It looks at URL params and at the current context level.
597 * It builds and returns an array containing:
599 * 1. heading: The heading displayed above the blog entries
600 * 2. stradd: The text to be used as the "Add entry" link
601 * 3. strview: The text to be used as the "View entries" link
602 * 4. url: The moodle_url object used as the base for add and view links
603 * 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block
605 * All other variables are set directly in $PAGE
607 * It uses the current URL to build these variables.
608 * A number of mutually exclusive use cases are used to structure this function.
610 * @return array
612 function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=null) {
613 global $CFG, $PAGE, $DB, $USER;
615 $id = optional_param('id', null, PARAM_INT);
616 $tag = optional_param('tag', null, PARAM_NOTAGS);
617 $tagid = optional_param('tagid', $tagid, PARAM_INT);
618 $userid = optional_param('userid', $userid, PARAM_INT);
619 $modid = optional_param('modid', null, PARAM_INT);
620 $entryid = optional_param('entryid', null, PARAM_INT);
621 $groupid = optional_param('groupid', $groupid, PARAM_INT);
622 $courseid = optional_param('courseid', $courseid, PARAM_INT);
623 $search = optional_param('search', null, PARAM_RAW);
624 $action = optional_param('action', null, PARAM_ALPHA);
625 $confirm = optional_param('confirm', false, PARAM_BOOL);
627 // Ignore userid when action == add
628 if ($action == 'add' && $userid) {
629 unset($userid);
630 $PAGE->url->remove_params(array('userid'));
631 } else if ($action == 'add' && $entryid) {
632 unset($entryid);
633 $PAGE->url->remove_params(array('entryid'));
636 $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
638 $blogurl = new moodle_url('/blog/index.php');
640 $headers['stradd'] = get_string('addnewentry', 'blog');
641 $headers['strview'] = null;
643 $site = $DB->get_record('course', array('id' => SITEID));
644 $sitecontext = context_system::instance();
645 // Common Lang strings
646 $strparticipants = get_string("participants");
647 $strblogentries = get_string("blogentries", 'blog');
649 // Prepare record objects as needed
650 if (!empty($courseid)) {
651 $headers['filters']['course'] = $courseid;
652 $course = $DB->get_record('course', array('id' => $courseid));
655 if (!empty($userid)) {
656 $headers['filters']['user'] = $userid;
657 $user = $DB->get_record('user', array('id' => $userid));
660 if (!empty($groupid)) { // groupid always overrides courseid
661 $headers['filters']['group'] = $groupid;
662 $group = $DB->get_record('groups', array('id' => $groupid));
663 $course = $DB->get_record('course', array('id' => $group->courseid));
666 $PAGE->set_pagelayout('standard');
668 // modid always overrides courseid, so the $course object may be reset here
669 if (!empty($modid) && $CFG->useblogassociations) {
671 $headers['filters']['module'] = $modid;
672 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case
673 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
674 $course = $DB->get_record('course', array('id' => $courseid));
675 $cm = $DB->get_record('course_modules', array('id' => $modid));
676 $cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module));
677 $cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance));
678 $a = new stdClass();
679 $a->type = get_string('modulename', $cm->modname);
680 $PAGE->set_cm($cm, $course);
681 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
682 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
685 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
686 // Note: if action is set to 'add' or 'edit', we do this at the end
687 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
688 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
689 $PAGE->navbar->add($strblogentries, $blogurl);
690 $PAGE->set_title("$shortname: " . get_string('blog', 'blog'));
691 $PAGE->set_heading("$shortname: " . get_string('blog', 'blog'));
692 $headers['heading'] = get_string('siteblog', 'blog', $shortname);
693 // $headers['strview'] = get_string('viewsiteentries', 'blog');
696 // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information
697 if (!empty($entryid)) {
698 $headers['filters']['entry'] = $entryid;
699 $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id';
700 $user = $DB->get_record_sql($sql, array($entryid));
701 $entry = $DB->get_record('post', array('id' => $entryid));
703 $blogurl->param('userid', $user->id);
705 if (!empty($course)) {
706 $mycourseid = $course->id;
707 $blogurl->param('courseid', $mycourseid);
708 } else {
709 $mycourseid = $site->id;
711 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
713 $PAGE->navbar->add($strblogentries, $blogurl);
715 $blogurl->remove_params('userid');
716 $PAGE->navbar->add($entry->subject, $blogurl);
717 $PAGE->set_title("$shortname: " . fullname($user) . ": $entry->subject");
718 $PAGE->set_heading("$shortname: " . fullname($user) . ": $entry->subject");
719 $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user));
721 // We ignore tag and search params
722 if (empty($action) || !$CFG->useblogassociations) {
723 $headers['url'] = $blogurl;
724 return $headers;
728 // Case 3: A user's blog entries
729 if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
730 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
731 $blogurl->param('userid', $userid);
732 $PAGE->set_title("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
733 $PAGE->set_heading("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
734 $headers['heading'] = get_string('userblog', 'blog', fullname($user));
735 $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user));
737 } else
739 // Case 4: No blog associations, no userid
740 if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) {
741 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
742 $PAGE->set_title("$shortname: " . get_string('blog', 'blog'));
743 $PAGE->set_heading("$shortname: " . get_string('blog', 'blog'));
744 $headers['heading'] = get_string('siteblog', 'blog', $shortname);
745 } else
747 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored)
748 if (!empty($userid) && !empty($modid) && empty($entryid)) {
749 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
750 $blogurl->param('userid', $userid);
751 $blogurl->param('modid', $modid);
753 // Course module navigation is handled by build_navigation as the second param
754 $headers['cm'] = $cm;
755 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
756 $PAGE->navbar->add($strblogentries, $blogurl);
758 $PAGE->set_title("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
759 $PAGE->set_heading("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
761 $a = new stdClass();
762 $a->user = fullname($user);
763 $a->mod = $cm->name;
764 $a->type = get_string('modulename', $cm->modname);
765 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
766 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
767 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
768 } else
770 // Case 6: Blog entries associated with a course by a specific user
771 if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
772 $siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
773 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
774 $blogurl->param('userid', $userid);
775 $blogurl->param('courseid', $courseid);
777 $PAGE->navbar->add($strblogentries, $blogurl);
779 $PAGE->set_title("$siteshortname: $courseshortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
780 $PAGE->set_heading("$siteshortname: $courseshortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
782 $a = new stdClass();
783 $a->user = fullname($user);
784 $a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
785 $a->type = get_string('course');
786 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
787 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
788 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
790 // Remove the userid from the URL to inform the blog_menu block correctly
791 $blogurl->remove_params(array('userid'));
792 } else
794 // Case 7: Blog entries by members of a group, associated with that group's course
795 if (!empty($groupid) && empty($modid) && empty($entryid)) {
796 $siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
797 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
798 $blogurl->param('courseid', $course->id);
800 $PAGE->navbar->add($strblogentries, $blogurl);
801 $blogurl->remove_params(array('courseid'));
802 $blogurl->param('groupid', $groupid);
803 $PAGE->navbar->add($group->name, $blogurl);
805 $PAGE->set_title("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog') . ": $group->name");
806 $PAGE->set_heading("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog') . ": $group->name");
808 $a = new stdClass();
809 $a->group = $group->name;
810 $a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
811 $a->type = get_string('course');
812 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
813 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
814 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
815 } else
817 // Case 8: Blog entries by members of a group, associated with an activity in that course
818 if (!empty($groupid) && !empty($modid) && empty($entryid)) {
819 $siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
820 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
821 $headers['cm'] = $cm;
822 $blogurl->param('modid', $modid);
823 $PAGE->navbar->add($strblogentries, $blogurl);
825 $blogurl->param('groupid', $groupid);
826 $PAGE->navbar->add($group->name, $blogurl);
828 $PAGE->set_title("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
829 $PAGE->set_heading("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
831 $a = new stdClass();
832 $a->group = $group->name;
833 $a->mod = $cm->name;
834 $a->type = get_string('modulename', $cm->modname);
835 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
836 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
837 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
839 } else
841 // Case 9: All blog entries associated with an activity
842 if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
843 $siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
844 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
845 $PAGE->set_cm($cm, $course);
846 $blogurl->param('modid', $modid);
847 $PAGE->navbar->add($strblogentries, $blogurl);
848 $PAGE->set_title("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog'));
849 $PAGE->set_heading("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog'));
850 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
851 $a = new stdClass();
852 $a->type = get_string('modulename', $cm->modname);
853 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
854 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
855 } else
857 // Case 10: All blog entries associated with a course
858 if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
859 $siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
860 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
861 $blogurl->param('courseid', $courseid);
862 $PAGE->navbar->add($strblogentries, $blogurl);
863 $PAGE->set_title("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog'));
864 $PAGE->set_heading("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog'));
865 $a = new stdClass();
866 $a->type = get_string('course');
867 $headers['heading'] = get_string('blogentriesabout', 'blog', format_string($course->fullname, true, array('context' => context_course::instance($course->id))));
868 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
869 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
870 $blogurl->remove_params(array('userid'));
873 if (!in_array($action, array('edit', 'add'))) {
874 // Append Tag info
875 if (!empty($tagid)) {
876 $headers['filters']['tag'] = $tagid;
877 $blogurl->param('tagid', $tagid);
878 $tagrec = $DB->get_record('tag', array('id'=>$tagid));
879 $PAGE->navbar->add($tagrec->name, $blogurl);
880 } elseif (!empty($tag)) {
881 $blogurl->param('tag', $tag);
882 $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl);
885 // Append Search info
886 if (!empty($search)) {
887 $headers['filters']['search'] = $search;
888 $blogurl->param('search', $search);
889 $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
893 // Append edit mode info
894 if (!empty($action) && $action == 'add') {
896 } else if (!empty($action) && $action == 'edit') {
897 $PAGE->navbar->add(get_string('editentry', 'blog'));
900 if (empty($headers['url'])) {
901 $headers['url'] = $blogurl;
903 return $headers;
907 * Shortcut function for getting a count of blog entries associated with a course or a module
908 * @param int $courseid The ID of the course
909 * @param int $cmid The ID of the course_modules
910 * @return string The number of associated entries
912 function blog_get_associated_count($courseid, $cmid=null) {
913 global $DB;
914 $context = context_course::instance($courseid);
915 if ($cmid) {
916 $context = context_module::instance($cmid);
918 return $DB->count_records('blog_association', array('contextid' => $context->id));
922 * Running addtional permission check on plugin, for example, plugins
923 * may have switch to turn on/off comments option, this callback will
924 * affect UI display, not like pluginname_comment_validate only throw
925 * exceptions.
926 * Capability check has been done in comment->check_permissions(), we
927 * don't need to do it again here.
929 * @package core_blog
930 * @category comment
932 * @param stdClass $comment_param {
933 * context => context the context object
934 * courseid => int course id
935 * cm => stdClass course module object
936 * commentarea => string comment area
937 * itemid => int itemid
939 * @return array
941 function blog_comment_permissions($comment_param) {
942 return array('post'=>true, 'view'=>true);
946 * Validate comment parameter before perform other comments actions
948 * @package core_blog
949 * @category comment
951 * @param stdClass $comment {
952 * context => context the context object
953 * courseid => int course id
954 * cm => stdClass course module object
955 * commentarea => string comment area
956 * itemid => int itemid
958 * @return boolean
960 function blog_comment_validate($comment_param) {
961 global $DB;
962 // validate comment itemid
963 if (!$entry = $DB->get_record('post', array('id'=>$comment_param->itemid))) {
964 throw new comment_exception('invalidcommentitemid');
966 // validate comment area
967 if ($comment_param->commentarea != 'format_blog') {
968 throw new comment_exception('invalidcommentarea');
970 // validation for comment deletion
971 if (!empty($comment_param->commentid)) {
972 if ($record = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
973 if ($record->commentarea != 'format_blog') {
974 throw new comment_exception('invalidcommentarea');
976 if ($record->contextid != $comment_param->context->id) {
977 throw new comment_exception('invalidcontext');
979 if ($record->itemid != $comment_param->itemid) {
980 throw new comment_exception('invalidcommentitemid');
982 } else {
983 throw new comment_exception('invalidcommentid');
986 return true;
990 * Return a list of page types
991 * @param string $pagetype current page type
992 * @param stdClass $parentcontext Block's parent context
993 * @param stdClass $currentcontext Current context of block
995 function blog_page_type_list($pagetype, $parentcontext, $currentcontext) {
996 return array(
997 '*'=>get_string('page-x', 'pagetype'),
998 'blog-*'=>get_string('page-blog-x', 'blog'),
999 'blog-index'=>get_string('page-blog-index', 'blog'),
1000 'blog-edit'=>get_string('page-blog-edit', 'blog')