2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Core global functions for Blog.
22 * @copyright 2009 Nicolas Connault
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 * Library of functions and constants for blog
31 require_once($CFG->dirroot .'/blog/rsslib.php');
32 require_once($CFG->dirroot.'/tag/lib.php');
35 * User can edit a blog entry if this is their own blog entry and they have
36 * the capability moodle/blog:create, or if they have the capability
37 * moodle/blog:manageentries.
39 * This also applies to deleting of entries.
41 function blog_user_can_edit_entry($blogentry) {
44 $sitecontext = context_system::instance();
46 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
47 return true; // Can edit any blog entry.
50 if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) {
51 return true; // Can edit own when having blog:create capability.
59 * Checks to see if a user can view the blogs of another user.
60 * Only blog level is checked here, the capabilities are enforced
63 function blog_user_can_view_user_entry($targetuserid, $blogentry=null) {
64 global $CFG, $USER, $DB;
66 if (empty($CFG->enableblogs)) {
67 return false; // Blog system disabled.
70 if (isloggedin() && $USER->id == $targetuserid) {
71 return true; // Can view own entries in any case.
74 $sitecontext = context_system::instance();
75 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
76 return true; // Can manage all entries.
79 // If blog is in draft state, then make sure user have proper capability.
80 if ($blogentry && $blogentry->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
81 return false; // Can not view draft of others.
84 // If blog entry is not public, make sure user is logged in.
85 if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) {
89 // If blogentry is not passed or all above checks pass, then check capability based on system config.
90 switch ($CFG->bloglevel) {
91 case BLOG_GLOBAL_LEVEL:
96 if (isloggedin()) { // Not logged in viewers forbidden.
102 case BLOG_USER_LEVEL:
104 // If user is viewing other user blog, then user should have user:readuserblogs capability.
105 $personalcontext = context_user::instance($targetuserid);
106 return has_capability('moodle/user:readuserblogs', $personalcontext);
113 * remove all associations for the blog entries of a particular user
114 * @param int userid - id of user whose blog associations will be deleted
116 function blog_remove_associations_for_user($userid) {
118 throw new coding_exception('function blog_remove_associations_for_user() is not finished');
120 $blogentries = blog_fetch_entries(array('user' => $userid), 'lasmodified DESC');
121 foreach ($blogentries as $entry) {
122 if (blog_user_can_edit_entry($entry)) {
123 blog_remove_associations_for_entry($entry->id);
130 * remove all associations for the blog entries of a particular course
131 * @param int courseid - id of user whose blog associations will be deleted
133 function blog_remove_associations_for_course($courseid) {
135 $context = context_course::instance($courseid);
136 $DB->delete_records('blog_association', array('contextid' => $context->id));
140 * Given a record in the {blog_external} table, checks the blog's URL
141 * for new entries not yet copied into Moodle.
142 * Also attempts to identify and remove deleted blog entries
144 * @param object $externalblog
145 * @return boolean False if the Feed is invalid
147 function blog_sync_external_entries($externalblog) {
149 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
151 $rss = new moodle_simplepie();
152 $rssfile = $rss->registry->create('File', array($externalblog->url));
153 $filetest = $rss->registry->create('Locator', array($rssfile));
155 if (!$filetest->is_feed($rssfile)) {
156 $externalblog->failedlastsync = 1;
157 $DB->update_record('blog_external', $externalblog);
159 } else if (!empty($externalblog->failedlastsync)) {
160 $externalblog->failedlastsync = 0;
161 $DB->update_record('blog_external', $externalblog);
164 $rss->set_feed_url($externalblog->url);
167 if (empty($rss->data)) {
170 // Used to identify blog posts that have been deleted from the source feed.
171 $oldesttimestamp = null;
172 $uniquehashes = array();
174 foreach ($rss->get_items() as $entry) {
175 // If filtertags are defined, use them to filter the entries by RSS category.
176 if (!empty($externalblog->filtertags)) {
177 $containsfiltertag = false;
178 $categories = $entry->get_categories();
179 $filtertags = explode(',', $externalblog->filtertags);
180 $filtertags = array_map('trim', $filtertags);
181 $filtertags = array_map('strtolower', $filtertags);
183 foreach ($categories as $category) {
184 if (in_array(trim(strtolower($category->term)), $filtertags)) {
185 $containsfiltertag = true;
189 if (!$containsfiltertag) {
194 $uniquehashes[] = $entry->get_permalink();
196 $newentry = new stdClass();
197 $newentry->userid = $externalblog->userid;
198 $newentry->module = 'blog_external';
199 $newentry->content = $externalblog->id;
200 $newentry->uniquehash = $entry->get_permalink();
201 $newentry->publishstate = 'site';
202 $newentry->format = FORMAT_HTML;
203 // Clean subject of html, just in case.
204 $newentry->subject = clean_param($entry->get_title(), PARAM_TEXT);
205 // Observe 128 max chars in DB.
206 // TODO: +1 to raise this to 255.
207 if (core_text::strlen($newentry->subject) > 128) {
208 $newentry->subject = core_text::substr($newentry->subject, 0, 125) . '...';
210 $newentry->summary = $entry->get_description();
212 // Used to decide whether to insert or update.
213 // Uses enty permalink plus creation date if available.
214 $existingpostconditions = array('uniquehash' => $entry->get_permalink());
216 // Our DB doesnt allow null creation or modified timestamps so check the external blog supplied one.
217 $entrydate = $entry->get_date('U');
218 if (!empty($entrydate)) {
219 $existingpostconditions['created'] = $entrydate;
222 // The post ID or false if post not found in DB.
223 $postid = $DB->get_field('post', 'id', $existingpostconditions);
226 if (empty($entrydate)) {
229 $timestamp = $entrydate;
232 // Only set created if its a new post so we retain the original creation timestamp if the post is edited.
233 if ($postid === false) {
234 $newentry->created = $timestamp;
236 $newentry->lastmodified = $timestamp;
238 if (empty($oldesttimestamp) || $timestamp < $oldesttimestamp) {
239 // Found an older post.
240 $oldesttimestamp = $timestamp;
243 if (core_text::strlen($newentry->uniquehash) > 255) {
244 // The URL for this item is too long for the field. Rather than add
245 // the entry without the link we will skip straight over it.
246 // RSS spec says recommended length 500, we use 255.
247 debugging('External blog entry skipped because of oversized URL', DEBUG_DEVELOPER);
251 if ($postid === false) {
252 $id = $DB->insert_record('post', $newentry);
255 if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) {
256 tag_set('post', $id, $tags, 'core', context_user::instance($externalblog->userid)->id);
259 $newentry->id = $postid;
260 $DB->update_record('post', $newentry);
264 // Look at the posts we have in the database to check if any of them have been deleted from the feed.
265 // Only checking posts within the time frame returned by the rss feed. Older items may have been deleted or
266 // may just not be returned anymore. We can't tell the difference so we leave older posts alone.
267 $sql = "SELECT id, uniquehash
269 WHERE module = 'blog_external'
270 AND " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text(':blogid') . "
272 $dbposts = $DB->get_records_sql($sql, array('blogid' => $externalblog->id, 'ts' => $oldesttimestamp));
275 foreach ($dbposts as $dbpost) {
276 if ( !in_array($dbpost->uniquehash, $uniquehashes) ) {
277 $todelete[] = $dbpost->id;
280 $DB->delete_records_list('post', 'id', $todelete);
282 $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => time()));
286 * Given an external blog object, deletes all related blog entries from the post table.
287 * NOTE: The external blog's id is saved as post.content, a field that is not oterhwise used by blog entries.
288 * @param object $externablog
290 function blog_delete_external_entries($externalblog) {
292 require_capability('moodle/blog:manageexternal', context_system::instance());
293 $DB->delete_records_select('post',
294 "module='blog_external' AND " . $DB->sql_compare_text('content') . " = ?",
295 array($externalblog->id));
299 * This function checks that blogs are enabled, and that the user can see blogs at all
302 function blog_is_enabled_for_user() {
304 return (!empty($CFG->enableblogs) && (isloggedin() || ($CFG->bloglevel == BLOG_GLOBAL_LEVEL)));
308 * This function gets all of the options available for the current user in respect
311 * It loads the following if applicable:
312 * - Module options {@see blog_get_options_for_module}
313 * - Course options {@see blog_get_options_for_course}
314 * - User specific options {@see blog_get_options_for_user}
315 * - General options (BLOG_LEVEL_GLOBAL)
317 * @param moodle_page $page The page to load for (normally $PAGE)
318 * @param stdClass $userid Load for a specific user
319 * @return array An array of options organised by type.
321 function blog_get_all_options(moodle_page $page, stdClass $userid = null) {
322 global $CFG, $DB, $USER;
326 // If blogs are enabled and the user is logged in and not a guest.
327 if (blog_is_enabled_for_user()) {
328 // If the context is the user then assume we want to load for the users context.
329 if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) {
330 $userid = $page->context->instanceid;
332 // Check the userid var.
333 if (!is_null($userid) && $userid!==$USER->id) {
334 // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't!
335 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
340 if ($CFG->useblogassociations && $page->cm !== null) {
341 // Load for the module associated with the page.
342 $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user);
343 } else if ($CFG->useblogassociations && $page->course->id != SITEID) {
344 // Load the options for the course associated with the page.
345 $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user);
348 // Get the options for the user.
349 if ($user !== null and !isguestuser($user)) {
350 // Load for the requested user.
351 $options[CONTEXT_USER+1] = blog_get_options_for_user($user);
353 // Load for the current user.
354 if (isloggedin() and !isguestuser()) {
355 $options[CONTEXT_USER] = blog_get_options_for_user();
359 // If blog level is global then display a link to view all site entries.
360 if (!empty($CFG->enableblogs)
361 && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL
362 && has_capability('moodle/blog:view', context_system::instance())) {
364 $options[CONTEXT_SYSTEM] = array('viewsite' => array(
365 'string' => get_string('viewsiteentries', 'blog'),
366 'link' => new moodle_url('/blog/index.php')
370 // Return the options.
375 * Get all of the blog options that relate to the passed user.
377 * If no user is passed the current user is assumed.
379 * @staticvar array $useroptions Cache so we don't have to regenerate multiple times
380 * @param stdClass $user
381 * @return array The array of options for the requested user
383 function blog_get_options_for_user(stdClass $user=null) {
386 static $useroptions = array();
389 // Blogs must be enabled and the user must be logged in.
390 if (!blog_is_enabled_for_user()) {
394 // Sort out the user var.
395 if ($user === null || $user->id == $USER->id) {
397 $iscurrentuser = true;
399 $iscurrentuser = false;
402 // If we've already generated serve from the cache.
403 if (array_key_exists($user->id, $useroptions)) {
404 return $useroptions[$user->id];
407 $sitecontext = context_system::instance();
408 $canview = has_capability('moodle/blog:view', $sitecontext);
410 if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
411 // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL.
412 $options['userentries'] = array(
413 'string' => get_string('viewuserentries', 'blog', fullname($user)),
414 'link' => new moodle_url('/blog/index.php', array('userid'=>$user->id))
417 // It's the current user.
419 // We can view our own blogs .... BIG surprise.
420 $options['view'] = array(
421 'string' => get_string('viewallmyentries', 'blog'),
422 'link' => new moodle_url('/blog/index.php', array('userid'=>$USER->id))
425 if (has_capability('moodle/blog:create', $sitecontext)) {
426 // We can add to our own blog.
427 $options['add'] = array(
428 'string' => get_string('addnewentry', 'blog'),
429 'link' => new moodle_url('/blog/edit.php', array('action'=>'add'))
433 if ($canview && $CFG->enablerssfeeds) {
434 $options['rss'] = array(
435 'string' => get_string('rssfeed', 'blog'),
436 'link' => new moodle_url(rss_get_url($sitecontext->id, $USER->id, 'blog', 'user/'.$user->id))
440 // Cache the options.
441 $useroptions[$user->id] = $options;
442 // Return the options.
447 * Get the blog options that relate to the given course for the given user.
449 * @staticvar array $courseoptions A cache so we can save regenerating multiple times
450 * @param stdClass $course The course to load options for
451 * @param stdClass $user The user to load options for null == current user
452 * @return array The array of options
454 function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
457 static $courseoptions = array();
461 // User must be logged in and blogs must be enabled.
462 if (!blog_is_enabled_for_user()) {
466 // Check that the user can associate with the course.
467 $sitecontext = context_system::instance();
468 // Generate the cache key.
469 $key = $course->id.':';
475 // Serve from the cache if we've already generated for this course.
476 if (array_key_exists($key, $courseoptions)) {
477 return $courseoptions[$key];
480 if (has_capability('moodle/blog:view', $sitecontext)) {
482 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
483 // View entries about this course.
484 $options['courseview'] = array(
485 'string' => get_string('viewcourseblogs', 'blog'),
486 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id))
489 // View MY entries about this course.
490 $options['courseviewmine'] = array(
491 'string' => get_string('viewmyentriesaboutcourse', 'blog'),
492 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $USER->id))
494 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
495 // View the provided users entries about this course.
496 $options['courseviewuser'] = array(
497 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)),
498 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $user->id))
503 if (has_capability('moodle/blog:create', $sitecontext)) {
504 // We can blog about this course.
505 $options['courseadd'] = array(
506 'string' => get_string('blogaboutthiscourse', 'blog'),
507 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'courseid' => $course->id))
511 // Cache the options for this course.
512 $courseoptions[$key] = $options;
513 // Return the options.
518 * Get the blog options relating to the given module for the given user
520 * @staticvar array $moduleoptions Cache
521 * @param stdClass|cm_info $module The module to get options for
522 * @param stdClass $user The user to get options for null == currentuser
525 function blog_get_options_for_module($module, $user=null) {
528 static $moduleoptions = array();
531 // User must be logged in, blogs must be enabled.
532 if (!blog_is_enabled_for_user()) {
536 $sitecontext = context_system::instance();
538 // Generate the cache key.
539 $key = $module->id.':';
545 if (array_key_exists($key, $moduleoptions)) {
546 // Serve from the cache so we don't have to regenerate.
547 return $moduleoptions[$key];
550 if (has_capability('moodle/blog:view', $sitecontext)) {
551 // Save correct module name for later usage.
552 $modulename = get_string('modulename', $module->modname);
555 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
556 // View all entries about this module.
558 $a->type = $modulename;
559 $options['moduleview'] = array(
560 'string' => get_string('viewallmodentries', 'blog', $a),
561 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id))
564 // View MY entries about this module.
565 $options['moduleviewmine'] = array(
566 'string' => get_string('viewmyentriesaboutmodule', 'blog', $modulename),
567 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$USER->id))
569 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
570 // View the given users entries about this module.
572 $a->mod = $modulename;
573 $a->user = fullname($user);
574 $options['moduleviewuser'] = array(
575 'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a),
576 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$user->id))
581 if (has_capability('moodle/blog:create', $sitecontext)) {
582 // The user can blog about this module.
583 $options['moduleadd'] = array(
584 'string' => get_string('blogaboutthismodule', 'blog', $modulename),
585 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'modid'=>$module->id))
588 // Cache the options.
589 $moduleoptions[$key] = $options;
590 // Return the options.
595 * This function encapsulates all the logic behind the complex
596 * navigation, titles and headings of the blog listing page, depending
597 * on URL params. It looks at URL params and at the current context level.
598 * It builds and returns an array containing:
600 * 1. heading: The heading displayed above the blog entries
601 * 2. stradd: The text to be used as the "Add entry" link
602 * 3. strview: The text to be used as the "View entries" link
603 * 4. url: The moodle_url object used as the base for add and view links
604 * 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block
606 * All other variables are set directly in $PAGE
608 * It uses the current URL to build these variables.
609 * A number of mutually exclusive use cases are used to structure this function.
613 function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=null) {
614 global $CFG, $PAGE, $DB, $USER;
616 $id = optional_param('id', null, PARAM_INT);
617 $tag = optional_param('tag', null, PARAM_NOTAGS);
618 $tagid = optional_param('tagid', $tagid, PARAM_INT);
619 $userid = optional_param('userid', $userid, PARAM_INT);
620 $modid = optional_param('modid', null, PARAM_INT);
621 $entryid = optional_param('entryid', null, PARAM_INT);
622 $groupid = optional_param('groupid', $groupid, PARAM_INT);
623 $courseid = optional_param('courseid', $courseid, PARAM_INT);
624 $search = optional_param('search', null, PARAM_RAW);
625 $action = optional_param('action', null, PARAM_ALPHA);
626 $confirm = optional_param('confirm', false, PARAM_BOOL);
628 // Ignore userid when action == add.
629 if ($action == 'add' && $userid) {
631 $PAGE->url->remove_params(array('userid'));
632 } else if ($action == 'add' && $entryid) {
634 $PAGE->url->remove_params(array('entryid'));
637 $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
639 $blogurl = new moodle_url('/blog/index.php');
641 $headers['stradd'] = get_string('addnewentry', 'blog');
642 $headers['strview'] = null;
644 $site = $DB->get_record('course', array('id' => SITEID));
645 $sitecontext = context_system::instance();
646 // Common Lang strings.
647 $strparticipants = get_string("participants");
648 $strblogentries = get_string("blogentries", 'blog');
650 // Prepare record objects as needed.
651 if (!empty($courseid)) {
652 $headers['filters']['course'] = $courseid;
653 $course = $DB->get_record('course', array('id' => $courseid));
656 if (!empty($userid)) {
657 $headers['filters']['user'] = $userid;
658 $user = $DB->get_record('user', array('id' => $userid));
661 if (!empty($groupid)) { // The groupid always overrides courseid.
662 $headers['filters']['group'] = $groupid;
663 $group = $DB->get_record('groups', array('id' => $groupid));
664 $course = $DB->get_record('course', array('id' => $group->courseid));
667 $PAGE->set_pagelayout('standard');
669 // The modid always overrides courseid, so the $course object may be reset here.
670 if (!empty($modid) && $CFG->useblogassociations) {
672 $headers['filters']['module'] = $modid;
673 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case.
674 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
675 $course = $DB->get_record('course', array('id' => $courseid));
676 $cm = $DB->get_record('course_modules', array('id' => $modid));
677 $cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module));
678 $cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance));
680 $a->type = get_string('modulename', $cm->modname);
681 $PAGE->set_cm($cm, $course);
682 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
683 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
686 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
687 // Note: if action is set to 'add' or 'edit', we do this at the end.
688 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
689 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
690 $PAGE->navbar->add($strblogentries, $blogurl);
691 $PAGE->set_title("$shortname: " . get_string('blog', 'blog'));
692 $PAGE->set_heading("$shortname: " . get_string('blog', 'blog'));
693 $headers['heading'] = get_string('siteblog', 'blog', $shortname);
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);
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;
728 if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
729 // Case 3: A user's blog entries.
731 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
732 $blogurl->param('userid', $userid);
733 $PAGE->set_title("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
734 $PAGE->set_heading("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
735 $headers['heading'] = get_string('userblog', 'blog', fullname($user));
736 $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user));
738 } else if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) {
739 // Case 4: No blog associations, no userid.
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 if (!empty($userid) && !empty($modid) && empty($entryid)) {
746 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored).
748 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
749 $blogurl->param('userid', $userid);
750 $blogurl->param('modid', $modid);
752 // Course module navigation is handled by build_navigation as the second param.
753 $headers['cm'] = $cm;
754 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
755 $PAGE->navbar->add($strblogentries, $blogurl);
757 $PAGE->set_title("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
758 $PAGE->set_heading("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
761 $a->user = fullname($user);
763 $a->type = get_string('modulename', $cm->modname);
764 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
765 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
766 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
767 } else if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
768 // Case 6: Blog entries associated with a course by a specific user.
770 $siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
771 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
772 $blogurl->param('userid', $userid);
773 $blogurl->param('courseid', $courseid);
775 $PAGE->navbar->add($strblogentries, $blogurl);
777 $PAGE->set_title("$siteshortname: $courseshortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
778 $PAGE->set_heading("$siteshortname: $courseshortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
781 $a->user = fullname($user);
782 $a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
783 $a->type = get_string('course');
784 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
785 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
786 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
788 // Remove the userid from the URL to inform the blog_menu block correctly.
789 $blogurl->remove_params(array('userid'));
790 } else if (!empty($groupid) && empty($modid) && empty($entryid)) {
791 // Case 7: Blog entries by members of a group, associated with that group's course.
793 $siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
794 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
795 $blogurl->param('courseid', $course->id);
797 $PAGE->navbar->add($strblogentries, $blogurl);
798 $blogurl->remove_params(array('courseid'));
799 $blogurl->param('groupid', $groupid);
800 $PAGE->navbar->add($group->name, $blogurl);
802 $PAGE->set_title("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog') . ": $group->name");
803 $PAGE->set_heading("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog') . ": $group->name");
806 $a->group = $group->name;
807 $a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
808 $a->type = get_string('course');
809 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
810 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
811 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
812 } else if (!empty($groupid) && !empty($modid) && empty($entryid)) {
813 // Case 8: Blog entries by members of a group, associated with an activity in that course.
815 $siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
816 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
817 $headers['cm'] = $cm;
818 $blogurl->param('modid', $modid);
819 $PAGE->navbar->add($strblogentries, $blogurl);
821 $blogurl->param('groupid', $groupid);
822 $PAGE->navbar->add($group->name, $blogurl);
824 $PAGE->set_title("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
825 $PAGE->set_heading("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
828 $a->group = $group->name;
830 $a->type = get_string('modulename', $cm->modname);
831 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
832 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
833 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
835 } else if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
836 // Case 9: All blog entries associated with an activity.
838 $siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
839 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
840 $PAGE->set_cm($cm, $course);
841 $blogurl->param('modid', $modid);
842 $PAGE->navbar->add($strblogentries, $blogurl);
843 $PAGE->set_title("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog'));
844 $PAGE->set_heading("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog'));
845 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
847 $a->type = get_string('modulename', $cm->modname);
848 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
849 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
850 } else if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
851 // Case 10: All blog entries associated with a course.
853 $siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
854 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
855 $blogurl->param('courseid', $courseid);
856 $PAGE->navbar->add($strblogentries, $blogurl);
857 $PAGE->set_title("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog'));
858 $PAGE->set_heading("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog'));
860 $a->type = get_string('course');
861 $headers['heading'] = get_string('blogentriesabout',
863 format_string($course->fullname,
865 array('context' => context_course::instance($course->id))));
866 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
867 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
868 $blogurl->remove_params(array('userid'));
871 if (!in_array($action, array('edit', 'add'))) {
873 if (!empty($tagid)) {
874 $headers['filters']['tag'] = $tagid;
875 $blogurl->param('tagid', $tagid);
876 $tagrec = $DB->get_record('tag', array('id'=>$tagid));
877 $PAGE->navbar->add($tagrec->name, $blogurl);
878 } else if (!empty($tag)) {
879 if ($tagrec = $DB->get_record('tag', array('name' => $tag))) {
880 $tagid = $tagrec->id;
881 $headers['filters']['tag'] = $tagid;
882 $blogurl->param('tag', $tag);
883 $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl);
887 // Append Search info.
888 if (!empty($search)) {
889 $headers['filters']['search'] = $search;
890 $blogurl->param('search', $search);
891 $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
895 // Append edit mode info.
896 if (!empty($action) && $action == 'add') {
898 } else if (!empty($action) && $action == 'edit') {
899 $PAGE->navbar->add(get_string('editentry', 'blog'));
902 if (empty($headers['url'])) {
903 $headers['url'] = $blogurl;
909 * Shortcut function for getting a count of blog entries associated with a course or a module
910 * @param int $courseid The ID of the course
911 * @param int $cmid The ID of the course_modules
912 * @return string The number of associated entries
914 function blog_get_associated_count($courseid, $cmid=null) {
916 $context = context_course::instance($courseid);
918 $context = context_module::instance($cmid);
920 return $DB->count_records('blog_association', array('contextid' => $context->id));
924 * Running addtional permission check on plugin, for example, plugins
925 * may have switch to turn on/off comments option, this callback will
926 * affect UI display, not like pluginname_comment_validate only throw
928 * blog_comment_validate will be called before viewing/adding/deleting
929 * comment, so don't repeat checks.
930 * Capability check has been done in comment->check_permissions(), we
931 * don't need to do it again here.
936 * @param stdClass $comment_param {
937 * context => context the context object
938 * courseid => int course id
939 * cm => stdClass course module object
940 * commentarea => string comment area
941 * itemid => int itemid
945 function blog_comment_permissions($comment_param) {
948 // If blog is public and current user is guest, then don't let him post comments.
949 $blogentry = $DB->get_record('post', array('id' => $comment_param->itemid), 'publishstate', MUST_EXIST);
951 if ($blogentry->publishstate != 'public') {
952 if (!isloggedin() || isguestuser()) {
953 return array('post' => false, 'view' => true);
956 return array('post' => true, 'view' => true);
960 * Validate comment parameter before perform other comments actions
965 * @param stdClass $comment {
966 * context => context the context object
967 * courseid => int course id
968 * cm => stdClass course module object
969 * commentarea => string comment area
970 * itemid => int itemid
974 function blog_comment_validate($comment_param) {
975 global $CFG, $DB, $USER;
977 // Check if blogs are enabled user can comment.
978 if (empty($CFG->enableblogs) || empty($CFG->blogusecomments)) {
979 throw new comment_exception('nopermissiontocomment');
982 // Validate comment area.
983 if ($comment_param->commentarea != 'format_blog') {
984 throw new comment_exception('invalidcommentarea');
987 $blogentry = $DB->get_record('post', array('id' => $comment_param->itemid), '*', MUST_EXIST);
989 // Validation for comment deletion.
990 if (!empty($comment_param->commentid)) {
991 if ($record = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
992 if ($record->commentarea != 'format_blog') {
993 throw new comment_exception('invalidcommentarea');
995 if ($record->contextid != $comment_param->context->id) {
996 throw new comment_exception('invalidcontext');
998 if ($record->itemid != $comment_param->itemid) {
999 throw new comment_exception('invalidcommentitemid');
1002 throw new comment_exception('invalidcommentid');
1006 // Validate if user has blog view permission.
1007 $sitecontext = context_system::instance();
1008 return has_capability('moodle/blog:view', $sitecontext) &&
1009 blog_user_can_view_user_entry($blogentry->userid, $blogentry);
1013 * Return a list of page types
1014 * @param string $pagetype current page type
1015 * @param stdClass $parentcontext Block's parent context
1016 * @param stdClass $currentcontext Current context of block
1018 function blog_page_type_list($pagetype, $parentcontext, $currentcontext) {
1020 '*'=>get_string('page-x', 'pagetype'),
1021 'blog-*'=>get_string('page-blog-x', 'blog'),
1022 'blog-index'=>get_string('page-blog-index', 'blog'),
1023 'blog-edit'=>get_string('page-blog-edit', 'blog')