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');
34 * User can edit a blog entry if this is their own blog entry and they have
35 * the capability moodle/blog:create, or if they have the capability
36 * moodle/blog:manageentries.
38 * This also applies to deleting of entries.
40 function blog_user_can_edit_entry($blogentry) {
43 $sitecontext = context_system
::instance();
45 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
46 return true; // Can edit any blog entry.
49 if ($blogentry->userid
== $USER->id
&& has_capability('moodle/blog:create', $sitecontext)) {
50 return true; // Can edit own when having blog:create capability.
58 * Checks to see if a user can view the blogs of another user.
59 * Only blog level is checked here, the capabilities are enforced
62 function blog_user_can_view_user_entry($targetuserid, $blogentry=null) {
63 global $CFG, $USER, $DB;
65 if (empty($CFG->enableblogs
)) {
66 return false; // Blog system disabled.
69 if (isloggedin() && $USER->id
== $targetuserid) {
70 return true; // Can view own entries in any case.
73 $sitecontext = context_system
::instance();
74 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
75 return true; // Can manage all entries.
78 // If blog is in draft state, then make sure user have proper capability.
79 if ($blogentry && $blogentry->publishstate
== 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
80 return false; // Can not view draft of others.
83 // If blog entry is not public, make sure user is logged in.
84 if ($blogentry && $blogentry->publishstate
!= 'public' && !isloggedin()) {
88 // If blogentry is not passed or all above checks pass, then check capability based on system config.
89 switch ($CFG->bloglevel
) {
90 case BLOG_GLOBAL_LEVEL
:
95 if (isloggedin()) { // Not logged in viewers forbidden.
101 case BLOG_USER_LEVEL
:
103 // If user is viewing other user blog, then user should have user:readuserblogs capability.
104 $personalcontext = context_user
::instance($targetuserid);
105 return has_capability('moodle/user:readuserblogs', $personalcontext);
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) {
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) {
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) {
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);
158 } else if (!empty($externalblog->failedlastsync
)) {
159 $externalblog->failedlastsync
= 0;
160 $DB->update_record('blog_external', $externalblog);
163 $rss->set_feed_url($externalblog->url
);
166 if (empty($rss->data
)) {
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 if (!empty($categories)) {
183 foreach ($categories as $category) {
184 if (in_array(trim(strtolower($category->term
)), $filtertags)) {
185 $containsfiltertag = true;
190 if (!$containsfiltertag) {
195 $uniquehashes[] = $entry->get_permalink();
197 $newentry = new stdClass();
198 $newentry->userid
= $externalblog->userid
;
199 $newentry->module
= 'blog_external';
200 $newentry->content
= $externalblog->id
;
201 $newentry->uniquehash
= $entry->get_permalink();
202 $newentry->publishstate
= 'site';
203 $newentry->format
= FORMAT_HTML
;
204 // Clean subject of html, just in case.
205 $newentry->subject
= clean_param($entry->get_title(), PARAM_TEXT
);
206 // Observe 128 max chars in DB.
207 // TODO: +1 to raise this to 255.
208 if (core_text
::strlen($newentry->subject
) > 128) {
209 $newentry->subject
= core_text
::substr($newentry->subject
, 0, 125) . '...';
211 $newentry->summary
= $entry->get_description();
213 // Used to decide whether to insert or update.
214 // Uses enty permalink plus creation date if available.
215 $existingpostconditions = array('uniquehash' => $entry->get_permalink());
217 // Our DB doesnt allow null creation or modified timestamps so check the external blog supplied one.
218 $entrydate = $entry->get_date('U');
219 if (!empty($entrydate)) {
220 $existingpostconditions['created'] = $entrydate;
223 // The post ID or false if post not found in DB.
224 $postid = $DB->get_field('post', 'id', $existingpostconditions);
227 if (empty($entrydate)) {
230 $timestamp = $entrydate;
233 // Only set created if its a new post so we retain the original creation timestamp if the post is edited.
234 if ($postid === false) {
235 $newentry->created
= $timestamp;
237 $newentry->lastmodified
= $timestamp;
239 if (empty($oldesttimestamp) ||
$timestamp < $oldesttimestamp) {
240 // Found an older post.
241 $oldesttimestamp = $timestamp;
244 if (core_text
::strlen($newentry->uniquehash
) > 255) {
245 // The URL for this item is too long for the field. Rather than add
246 // the entry without the link we will skip straight over it.
247 // RSS spec says recommended length 500, we use 255.
248 debugging('External blog entry skipped because of oversized URL', DEBUG_DEVELOPER
);
252 if ($postid === false) {
253 $id = $DB->insert_record('post', $newentry);
256 if ($tags = core_tag_tag
::get_item_tags_array('core', 'blog_external', $externalblog->id
)) {
257 core_tag_tag
::set_item_tags('core', 'post', $id, context_user
::instance($externalblog->userid
), $tags);
260 $newentry->id
= $postid;
261 $DB->update_record('post', $newentry);
265 // Look at the posts we have in the database to check if any of them have been deleted from the feed.
266 // Only checking posts within the time frame returned by the rss feed. Older items may have been deleted or
267 // may just not be returned anymore. We can't tell the difference so we leave older posts alone.
268 $sql = "SELECT id, uniquehash
270 WHERE module = 'blog_external'
271 AND " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text(':blogid') . "
273 $dbposts = $DB->get_records_sql($sql, array('blogid' => $externalblog->id
, 'ts' => $oldesttimestamp));
276 foreach ($dbposts as $dbpost) {
277 if ( !in_array($dbpost->uniquehash
, $uniquehashes) ) {
278 $todelete[] = $dbpost->id
;
281 $DB->delete_records_list('post', 'id', $todelete);
283 $DB->update_record('blog_external', array('id' => $externalblog->id
, 'timefetched' => time()));
287 * Given an external blog object, deletes all related blog entries from the post table.
288 * NOTE: The external blog's id is saved as post.content, a field that is not oterhwise used by blog entries.
289 * @param object $externablog
291 function blog_delete_external_entries($externalblog) {
293 require_capability('moodle/blog:manageexternal', context_system
::instance());
294 $DB->delete_records_select('post',
295 "module='blog_external' AND " . $DB->sql_compare_text('content') . " = ?",
296 array($externalblog->id
));
300 * This function checks that blogs are enabled, and that the user can see blogs at all
303 function blog_is_enabled_for_user() {
305 return (!empty($CFG->enableblogs
) && (isloggedin() ||
($CFG->bloglevel
== BLOG_GLOBAL_LEVEL
)));
309 * This function gets all of the options available for the current user in respect
312 * It loads the following if applicable:
313 * - Module options {@see blog_get_options_for_module}
314 * - Course options {@see blog_get_options_for_course}
315 * - User specific options {@see blog_get_options_for_user}
316 * - General options (BLOG_LEVEL_GLOBAL)
318 * @param moodle_page $page The page to load for (normally $PAGE)
319 * @param stdClass $userid Load for a specific user
320 * @return array An array of options organised by type.
322 function blog_get_all_options(moodle_page
$page, stdClass
$userid = null) {
323 global $CFG, $DB, $USER;
327 // If blogs are enabled and the user is logged in and not a guest.
328 if (blog_is_enabled_for_user()) {
329 // If the context is the user then assume we want to load for the users context.
330 if (is_null($userid) && $page->context
->contextlevel
== CONTEXT_USER
) {
331 $userid = $page->context
->instanceid
;
333 // Check the userid var.
334 if (!is_null($userid) && $userid !== $USER->id
) {
335 // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't!
336 $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST
);
341 if ($CFG->useblogassociations
&& $page->cm
!== null) {
342 // Load for the module associated with the page.
343 $options[CONTEXT_MODULE
] = blog_get_options_for_module($page->cm
, $user);
344 } else if ($CFG->useblogassociations
&& $page->course
->id
!= SITEID
) {
345 // Load the options for the course associated with the page.
346 $options[CONTEXT_COURSE
] = blog_get_options_for_course($page->course
, $user);
349 // Get the options for the user.
350 if ($user !== null and !isguestuser($user)) {
351 // Load for the requested user.
352 $options[CONTEXT_USER +
1] = blog_get_options_for_user($user);
354 // Load for the current user.
355 if (isloggedin() and !isguestuser()) {
356 $options[CONTEXT_USER
] = blog_get_options_for_user();
360 // If blog level is global then display a link to view all site entries.
361 if (!empty($CFG->enableblogs
)
362 && $CFG->bloglevel
>= BLOG_GLOBAL_LEVEL
363 && has_capability('moodle/blog:view', context_system
::instance())) {
365 $options[CONTEXT_SYSTEM
] = array('viewsite' => array(
366 'string' => get_string('viewsiteentries', 'blog'),
367 'link' => new moodle_url('/blog/index.php')
371 // Return the options.
376 * Get all of the blog options that relate to the passed user.
378 * If no user is passed the current user is assumed.
380 * @staticvar array $useroptions Cache so we don't have to regenerate multiple times
381 * @param stdClass $user
382 * @return array The array of options for the requested user
384 function blog_get_options_for_user(stdClass
$user=null) {
387 static $useroptions = array();
390 // Blogs must be enabled and the user must be logged in.
391 if (!blog_is_enabled_for_user()) {
395 // Sort out the user var.
396 if ($user === null ||
$user->id
== $USER->id
) {
398 $iscurrentuser = true;
400 $iscurrentuser = false;
403 // If we've already generated serve from the cache.
404 if (array_key_exists($user->id
, $useroptions)) {
405 return $useroptions[$user->id
];
408 $sitecontext = context_system
::instance();
409 $canview = has_capability('moodle/blog:view', $sitecontext);
411 if (!$iscurrentuser && $canview && ($CFG->bloglevel
>= BLOG_SITE_LEVEL
)) {
412 // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL.
413 $options['userentries'] = array(
414 'string' => get_string('viewuserentries', 'blog', fullname($user)),
415 'link' => new moodle_url('/blog/index.php', array('userid' => $user->id
))
418 // It's the current user.
420 // We can view our own blogs .... BIG surprise.
421 $options['view'] = array(
422 'string' => get_string('blogentries', 'blog'),
423 'link' => new moodle_url('/blog/index.php', array('userid' => $USER->id
))
426 if (has_capability('moodle/blog:create', $sitecontext)) {
427 // We can add to our own blog.
428 $options['add'] = array(
429 'string' => get_string('addnewentry', 'blog'),
430 'link' => new moodle_url('/blog/edit.php', array('action' => 'add'))
434 if ($canview && $CFG->enablerssfeeds
) {
435 $options['rss'] = array(
436 'string' => get_string('rssfeed', 'blog'),
437 'link' => new moodle_url(rss_get_url($sitecontext->id
, $USER->id
, 'blog', 'user/'.$user->id
))
441 // Cache the options.
442 $useroptions[$user->id
] = $options;
443 // Return the options.
448 * Get the blog options that relate to the given course for the given user.
450 * @staticvar array $courseoptions A cache so we can save regenerating multiple times
451 * @param stdClass $course The course to load options for
452 * @param stdClass $user The user to load options for null == current user
453 * @return array The array of options
455 function blog_get_options_for_course(stdClass
$course, stdClass
$user=null) {
458 static $courseoptions = array();
462 // User must be logged in and blogs must be enabled.
463 if (!blog_is_enabled_for_user()) {
467 // Check that the user can associate with the course.
468 $sitecontext = context_system
::instance();
469 // Generate the cache key.
470 $key = $course->id
.':';
476 // Serve from the cache if we've already generated for this course.
477 if (array_key_exists($key, $courseoptions)) {
478 return $courseoptions[$key];
481 if (has_capability('moodle/blog:view', $sitecontext)) {
483 if ($CFG->bloglevel
>= BLOG_SITE_LEVEL
) {
484 // View entries about this course.
485 $options['courseview'] = array(
486 'string' => get_string('viewcourseblogs', 'blog'),
487 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id
))
490 // View MY entries about this course.
491 $options['courseviewmine'] = array(
492 'string' => get_string('viewmyentriesaboutcourse', 'blog'),
493 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id
, 'userid' => $USER->id
))
495 if (!empty($user) && ($CFG->bloglevel
>= BLOG_SITE_LEVEL
)) {
496 // View the provided users entries about this course.
497 $options['courseviewuser'] = array(
498 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)),
499 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id
, 'userid' => $user->id
))
504 if (has_capability('moodle/blog:create', $sitecontext)) {
505 // We can blog about this course.
506 $options['courseadd'] = array(
507 'string' => get_string('blogaboutthiscourse', 'blog'),
508 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'courseid' => $course->id
))
512 // Cache the options for this course.
513 $courseoptions[$key] = $options;
514 // Return the options.
519 * Get the blog options relating to the given module for the given user
521 * @staticvar array $moduleoptions Cache
522 * @param stdClass|cm_info $module The module to get options for
523 * @param stdClass $user The user to get options for null == currentuser
526 function blog_get_options_for_module($module, $user=null) {
529 static $moduleoptions = array();
532 // User must be logged in, blogs must be enabled.
533 if (!blog_is_enabled_for_user()) {
537 $sitecontext = context_system
::instance();
539 // Generate the cache key.
540 $key = $module->id
.':';
546 if (array_key_exists($key, $moduleoptions)) {
547 // Serve from the cache so we don't have to regenerate.
548 return $moduleoptions[$key];
551 if (has_capability('moodle/blog:view', $sitecontext)) {
552 // Save correct module name for later usage.
553 $modulename = get_string('modulename', $module->modname
);
556 if ($CFG->bloglevel
>= BLOG_SITE_LEVEL
) {
557 // View all entries about this module.
559 $a->type
= $modulename;
560 $options['moduleview'] = array(
561 'string' => get_string('viewallmodentries', 'blog', $a),
562 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id
))
565 // View MY entries about this module.
566 $options['moduleviewmine'] = array(
567 'string' => get_string('viewmyentriesaboutmodule', 'blog', $modulename),
568 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id
, 'userid' => $USER->id
))
570 if (!empty($user) && ($CFG->bloglevel
>= BLOG_SITE_LEVEL
)) {
571 // View the given users entries about this module.
573 $a->mod
= $modulename;
574 $a->user
= fullname($user);
575 $options['moduleviewuser'] = array(
576 'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a),
577 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id
, 'userid' => $user->id
))
582 if (has_capability('moodle/blog:create', $sitecontext)) {
583 // The user can blog about this module.
584 $options['moduleadd'] = array(
585 'string' => get_string('blogaboutthismodule', 'blog', $modulename),
586 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'modid' => $module->id
))
589 // Cache the options.
590 $moduleoptions[$key] = $options;
591 // Return the options.
596 * This function encapsulates all the logic behind the complex
597 * navigation, titles and headings of the blog listing page, depending
598 * on URL params. It looks at URL params and at the current context level.
599 * It builds and returns an array containing:
601 * 1. heading: The heading displayed above the blog entries
602 * 2. stradd: The text to be used as the "Add entry" link
603 * 3. strview: The text to be used as the "View entries" link
604 * 4. url: The moodle_url object used as the base for add and view links
605 * 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block
607 * All other variables are set directly in $PAGE
609 * It uses the current URL to build these variables.
610 * A number of mutually exclusive use cases are used to structure this function.
614 function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=null) {
615 global $CFG, $PAGE, $DB, $USER;
617 $id = optional_param('id', null, PARAM_INT
);
618 $tag = optional_param('tag', null, PARAM_NOTAGS
);
619 $tagid = optional_param('tagid', $tagid, PARAM_INT
);
620 $userid = optional_param('userid', $userid, PARAM_INT
);
621 $modid = optional_param('modid', null, PARAM_INT
);
622 $entryid = optional_param('entryid', null, PARAM_INT
);
623 $groupid = optional_param('groupid', $groupid, PARAM_INT
);
624 $courseid = optional_param('courseid', $courseid, PARAM_INT
);
625 $search = optional_param('search', null, PARAM_RAW
);
626 $action = optional_param('action', null, PARAM_ALPHA
);
627 $confirm = optional_param('confirm', false, PARAM_BOOL
);
629 // Ignore userid when action == add.
630 if ($action == 'add' && $userid) {
632 $PAGE->url
->remove_params(array('userid'));
633 } else if ($action == 'add' && $entryid) {
635 $PAGE->url
->remove_params(array('entryid'));
638 $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
640 $blogurl = new moodle_url('/blog/index.php');
642 $headers['stradd'] = get_string('addnewentry', 'blog');
643 $headers['strview'] = null;
645 $site = $DB->get_record('course', array('id' => SITEID
));
646 $sitecontext = context_system
::instance();
647 // Common Lang strings.
648 $strparticipants = get_string("participants");
649 $strblogentries = get_string("blogentries", 'blog');
651 // Prepare record objects as needed.
652 if (!empty($courseid)) {
653 $headers['filters']['course'] = $courseid;
654 $course = $DB->get_record('course', array('id' => $courseid));
657 if (!empty($userid)) {
658 $headers['filters']['user'] = $userid;
659 $user = $DB->get_record('user', array('id' => $userid));
662 if (!empty($groupid)) { // The groupid always overrides courseid.
663 $headers['filters']['group'] = $groupid;
664 $group = $DB->get_record('groups', array('id' => $groupid));
665 $course = $DB->get_record('course', array('id' => $group->courseid
));
668 $PAGE->set_pagelayout('standard');
670 // The modid always overrides courseid, so the $course object may be reset here.
671 if (!empty($modid) && $CFG->useblogassociations
) {
673 $headers['filters']['module'] = $modid;
674 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case.
675 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
676 $course = $DB->get_record('course', array('id' => $courseid));
677 $cm = $DB->get_record('course_modules', array('id' => $modid));
678 $cm->modname
= $DB->get_field('modules', 'name', array('id' => $cm->module
));
679 $cm->name
= $DB->get_field($cm->modname
, 'name', array('id' => $cm->instance
));
681 $a->type
= get_string('modulename', $cm->modname
);
682 $PAGE->set_cm($cm, $course);
683 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
684 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
687 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
688 // Note: if action is set to 'add' or 'edit', we do this at the end.
689 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
690 $PAGE->navbar
->add($strblogentries, $blogurl);
691 $PAGE->set_title($site->fullname
);
692 $PAGE->set_heading($site->fullname
);
693 $headers['heading'] = get_string('siteblogheading', '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);
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 $PAGE->set_title($site->fullname
);
742 $PAGE->set_heading($site->fullname
);
743 $headers['heading'] = get_string('siteblogheading', 'blog');
744 } else if (!empty($userid) && !empty($modid) && empty($entryid)) {
745 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored).
747 $shortname = format_string($site->shortname
, true, array('context' => context_course
::instance(SITEID
)));
748 $blogurl->param('userid', $userid);
749 $blogurl->param('modid', $modid);
751 // Course module navigation is handled by build_navigation as the second param.
752 $headers['cm'] = $cm;
753 $PAGE->navbar
->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
754 $PAGE->navbar
->add($strblogentries, $blogurl);
756 $PAGE->set_title("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
757 $PAGE->set_heading("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
760 $a->user
= fullname($user);
762 $a->type
= get_string('modulename', $cm->modname
);
763 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
764 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
765 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
766 } else if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
767 // Case 6: Blog entries associated with a course by a specific user.
769 $blogurl->param('userid', $userid);
770 $blogurl->param('courseid', $courseid);
772 $PAGE->set_title($course->fullname
);
773 $PAGE->set_heading($course->fullname
);
776 $a->user
= fullname($user);
777 $a->course
= format_string($course->fullname
, true, array('context' => context_course
::instance($course->id
)));
778 $a->type
= get_string('course');
779 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
780 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
781 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
783 // Remove the userid from the URL to inform the blog_menu block correctly.
784 $blogurl->remove_params(array('userid'));
785 } else if (!empty($groupid) && empty($modid) && empty($entryid)) {
786 // Case 7: Blog entries by members of a group, associated with that group's course.
788 $blogurl->param('courseid', $course->id
);
790 $PAGE->navbar
->add($strblogentries, $blogurl);
791 $blogurl->remove_params(array('courseid'));
792 $blogurl->param('groupid', $groupid);
793 $PAGE->navbar
->add($group->name
, $blogurl);
795 $PAGE->set_title($course->fullname
);
796 $PAGE->set_heading($course->fullname
);
799 $a->group
= $group->name
;
800 $a->course
= format_string($course->fullname
, true, array('context' => context_course
::instance($course->id
)));
801 $a->type
= get_string('course');
802 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
803 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
804 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
805 } else if (!empty($groupid) && !empty($modid) && empty($entryid)) {
806 // Case 8: Blog entries by members of a group, associated with an activity in that course.
808 $headers['cm'] = $cm;
809 $blogurl->param('modid', $modid);
810 $PAGE->navbar
->add($strblogentries, $blogurl);
812 $blogurl->param('groupid', $groupid);
813 $PAGE->navbar
->add($group->name
, $blogurl);
815 $PAGE->set_title($course->fullname
);
816 $PAGE->set_heading($course->fullname
);
819 $a->group
= $group->name
;
821 $a->type
= get_string('modulename', $cm->modname
);
822 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
823 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
824 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
826 } else if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
827 // Case 9: All blog entries associated with an activity.
829 $PAGE->set_cm($cm, $course);
830 $blogurl->param('modid', $modid);
831 $PAGE->navbar
->add($strblogentries, $blogurl);
832 $PAGE->set_title($course->fullname
);
833 $PAGE->set_heading($course->fullname
);
834 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name
);
836 $a->type
= get_string('modulename', $cm->modname
);
837 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
838 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
839 } else if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
840 // Case 10: All blog entries associated with a course.
842 $blogurl->param('courseid', $courseid);
843 $PAGE->navbar
->add($strblogentries, $blogurl);
844 $PAGE->set_title($course->fullname
);
845 $PAGE->set_heading($course->fullname
);
847 $a->type
= get_string('course');
848 $headers['heading'] = get_string('blogentriesabout',
850 format_string($course->fullname
,
852 array('context' => context_course
::instance($course->id
))));
853 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
854 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
855 $blogurl->remove_params(array('userid'));
858 if (!in_array($action, array('edit', 'add'))) {
860 if (!empty($tagid)) {
861 $headers['filters']['tag'] = $tagid;
862 $blogurl->param('tagid', $tagid);
863 $tagrec = $DB->get_record('tag', array('id' => $tagid));
864 $PAGE->navbar
->add($tagrec->name
, $blogurl);
865 } else if (!empty($tag)) {
866 if ($tagrec = $DB->get_record('tag', array('name' => $tag))) {
867 $tagid = $tagrec->id
;
868 $headers['filters']['tag'] = $tagid;
869 $blogurl->param('tag', $tag);
870 $PAGE->navbar
->add(get_string('tagparam', 'blog', $tag), $blogurl);
874 // Append Search info.
875 if (!empty($search) && has_capability('moodle/blog:search', $sitecontext)) {
876 $headers['filters']['search'] = $search;
877 $blogurl->param('search', $search);
878 $PAGE->navbar
->add(get_string('searchterm', 'blog', $search), $blogurl->out());
882 // Append edit mode info.
883 if (!empty($action) && $action == 'add') {
885 } else if (!empty($action) && $action == 'edit') {
886 $PAGE->navbar
->add(get_string('editentry', 'blog'));
889 if (empty($headers['url'])) {
890 $headers['url'] = $blogurl;
896 * Shortcut function for getting a count of blog entries associated with a course or a module
897 * @param int $courseid The ID of the course
898 * @param int $cmid The ID of the course_modules
899 * @return string The number of associated entries
901 function blog_get_associated_count($courseid, $cmid=null) {
903 $context = context_course
::instance($courseid);
905 $context = context_module
::instance($cmid);
907 return $DB->count_records('blog_association', array('contextid' => $context->id
));
911 * Running addtional permission check on plugin, for example, plugins
912 * may have switch to turn on/off comments option, this callback will
913 * affect UI display, not like pluginname_comment_validate only throw
915 * blog_comment_validate will be called before viewing/adding/deleting
916 * comment, so don't repeat checks.
917 * Capability check has been done in comment->check_permissions(), we
918 * don't need to do it again here.
923 * @param stdClass $commentparam {
924 * context => context the context object
925 * courseid => int course id
926 * cm => stdClass course module object
927 * commentarea => string comment area
928 * itemid => int itemid
932 function blog_comment_permissions($commentparam) {
935 // If blog is public and current user is guest, then don't let him post comments.
936 $blogentry = $DB->get_record('post', array('id' => $commentparam->itemid
), 'publishstate', MUST_EXIST
);
938 if ($blogentry->publishstate
!= 'public') {
939 if (!isloggedin() ||
isguestuser()) {
940 return array('post' => false, 'view' => true);
943 return array('post' => true, 'view' => true);
947 * Validate comment parameter before perform other comments actions
952 * @param stdClass $comment {
953 * context => context the context object
954 * courseid => int course id
955 * cm => stdClass course module object
956 * commentarea => string comment area
957 * itemid => int itemid
961 function blog_comment_validate($commentparam) {
962 global $CFG, $DB, $USER;
964 // Check if blogs are enabled user can comment.
965 if (empty($CFG->enableblogs
) ||
empty($CFG->blogusecomments
)) {
966 throw new comment_exception('nopermissiontocomment');
969 // Validate comment area.
970 if ($commentparam->commentarea
!= 'format_blog') {
971 throw new comment_exception('invalidcommentarea');
974 $blogentry = $DB->get_record('post', array('id' => $commentparam->itemid
), '*', MUST_EXIST
);
976 // Validation for comment deletion.
977 if (!empty($commentparam->commentid
)) {
978 if ($record = $DB->get_record('comments', array('id' => $commentparam->commentid
))) {
979 if ($record->commentarea
!= 'format_blog') {
980 throw new comment_exception('invalidcommentarea');
982 if ($record->contextid
!= $commentparam->context
->id
) {
983 throw new comment_exception('invalidcontext');
985 if ($record->itemid
!= $commentparam->itemid
) {
986 throw new comment_exception('invalidcommentitemid');
989 throw new comment_exception('invalidcommentid');
993 // Validate if user has blog view permission.
994 $sitecontext = context_system
::instance();
995 return has_capability('moodle/blog:view', $sitecontext) &&
996 blog_user_can_view_user_entry($blogentry->userid
, $blogentry);
1000 * Return a list of page types
1001 * @param string $pagetype current page type
1002 * @param stdClass $parentcontext Block's parent context
1003 * @param stdClass $currentcontext Current context of block
1005 function blog_page_type_list($pagetype, $parentcontext, $currentcontext) {
1007 '*' => get_string('page-x', 'pagetype'),
1008 'blog-*' => get_string('page-blog-x', 'blog'),
1009 'blog-index' => get_string('page-blog-index', 'blog'),
1010 'blog-edit' => get_string('page-blog-edit', 'blog')
1015 * Add nodes to myprofile page.
1017 * @param \core_user\output\myprofile\tree $tree Tree object
1018 * @param stdClass $user user object
1019 * @param bool $iscurrentuser
1020 * @param stdClass $course Course object
1024 function core_blog_myprofile_navigation(core_user\output\myprofile\tree
$tree, $user, $iscurrentuser, $course) {
1026 if (!blog_is_enabled_for_user() ||
isguestuser($user)) {
1027 // The guest user cannot post, so it is not possible to view any posts.
1028 // Also blogs might be disabled.
1029 // May as well just bail aggressively here.
1032 if (!blog_user_can_view_user_entry($user->id
)) {
1035 $url = new moodle_url("/blog/index.php", array('userid' => $user->id
));
1036 if (!empty($course)) {
1037 $url->param('courseid', $course->id
);
1039 if ($iscurrentuser) {
1040 $title = get_string('blogentries', 'core_blog');
1042 $title = get_string('myprofileuserblogs', 'core_blog');
1044 $blognode = new core_user\output\myprofile\node
('miscellaneous', 'blogs', $title, null, $url);
1045 $tree->add_node($blognode);
1050 * Returns posts tagged with a specified tag.
1052 * @param core_tag_tag $tag
1053 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
1054 * are displayed on the page and the per-page limit may be bigger
1055 * @param int $fromctx context id where the link was displayed, may be used by callbacks
1056 * to display items in the same context first
1057 * @param int $ctx context id where to search for records
1058 * @param bool $rec search in subcontexts as well
1059 * @param int $page 0-based number of page being displayed
1060 * @return \core_tag\output\tagindex
1062 function blog_get_tagged_posts($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = true, $page = 0) {
1063 global $CFG, $OUTPUT;
1064 require_once($CFG->dirroot
.'/user/lib.php');
1066 $systemcontext = context_system
::instance();
1067 $perpage = $exclusivemode ?
20 : 5;
1068 $context = $ctx ? context
::instance_by_id($ctx) : context_system
::instance();
1071 if (empty($CFG->enableblogs
) ||
!has_capability('moodle/blog:view', $systemcontext)) {
1072 // Blogs are not enabled or are not visible to the current user.
1074 } else if ($context->contextlevel
!= CONTEXT_SYSTEM
&& empty($CFG->useblogassociations
)) {
1075 // No blog entries can be associated to the non-system context.
1077 } else if (!$rec && $context->contextlevel
!= CONTEXT_COURSE
&& $context->contextlevel
!= CONTEXT_MODULE
) {
1078 // No blog entries can be associated with category or block context.
1081 require_once($CFG->dirroot
.'/blog/locallib.php');
1083 $filters = array('tag' => $tag->id
);
1085 if ($context->contextlevel
!= CONTEXT_SYSTEM
) {
1086 $filters['context'] = $context->id
;
1088 } else if ($context->contextlevel
== CONTEXT_COURSE
) {
1089 $filters['course'] = $context->instanceid
;
1090 } else if ($context->contextlevel
== CONTEXT_MODULE
) {
1091 $filters['module'] = $context->instanceid
;
1093 $bloglisting = new blog_listing($filters);
1094 $blogs = $bloglisting->get_entries($page * $perpage, $perpage);
1095 $totalcount = $bloglisting->count_entries();
1096 $totalpages = ceil($totalcount / $perpage);
1097 if (!empty($blogs)) {
1098 $tagfeed = new core_tag\output\tagfeed
();
1099 foreach ($blogs as $blog) {
1100 $user = fullclone($blog);
1101 $user->id
= $blog->userid
;
1103 $img = $OUTPUT->user_picture($user, array('size' => 35));
1104 $subject = format_string($blog->subject
);
1106 if ($blog->publishstate
== 'draft') {
1112 $url = new moodle_url('/blog/index.php', array('entryid' => $blog->id
));
1113 $subject = html_writer
::link($url, $subject, array('class' => $class));
1115 $fullname = fullname($user);
1116 if (user_can_view_profile($user)) {
1117 $profilelink = new moodle_url('/user/view.php', array('id' => $blog->userid
));
1118 $fullname = html_writer
::link($profilelink, $fullname);
1120 $details = $fullname . ', ' . userdate($blog->created
);
1122 $tagfeed->add($img, $subject, $details);
1125 $items = $tagfeed->export_for_template($OUTPUT);
1126 $content = $OUTPUT->render_from_template('core_tag/tagfeed', $items);
1128 $urlparams = array('tagid' => $tag->id
);
1129 if ($context->contextlevel
== CONTEXT_COURSE
) {
1130 $urlparams['courseid'] = $context->instanceid
;
1131 } else if ($context->contextlevel
== CONTEXT_MODULE
) {
1132 $urlparams['modid'] = $context->instanceid
;
1134 $allblogsurl = new moodle_url('/blog/index.php', $urlparams);
1136 $rv = new core_tag\output\tagindex
($tag, 'core', 'post',
1138 $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
1139 $rv->exclusiveurl
= $allblogsurl;
1144 $rv = new core_tag\output\tagindex
($tag, 'core', 'post',
1146 $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
1147 $rv->exclusiveurl
= null;