weekly release 3.6dev
[moodle.git] / blog / lib.php
CommitLineData
cae83708 1<?php
cae83708 2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
cae83708 17/**
18 * Core global functions for Blog.
19 *
20 * @package moodlecore
21 * @subpackage blog
22 * @copyright 2009 Nicolas Connault
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
35716b86
PS
26defined('MOODLE_INTERNAL') || die();
27
2b6e53e8 28/*
cae83708 29 * Library of functions and constants for blog
30 */
31require_once($CFG->dirroot .'/blog/rsslib.php');
cae83708 32
cae83708 33/**
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.
37 *
38 * This also applies to deleting of entries.
39 */
1c7b8b93
NC
40function blog_user_can_edit_entry($blogentry) {
41 global $USER;
6524adcf 42
41b38360 43 $sitecontext = context_system::instance();
4a173181 44
cae83708 45 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
2b6e53e8 46 return true; // Can edit any blog entry.
cae83708 47 }
c2ee4e87 48
1c7b8b93 49 if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) {
2b6e53e8 50 return true; // Can edit own when having blog:create capability.
cae83708 51 }
4a173181 52
cae83708 53 return false;
54}
d02240f3 55
3bfcfdca 56
cae83708 57/**
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
60 * in blog/index.php
61 */
1c7b8b93 62function blog_user_can_view_user_entry($targetuserid, $blogentry=null) {
cae83708 63 global $CFG, $USER, $DB;
c2ee4e87 64
850d2db8 65 if (empty($CFG->enableblogs)) {
89f5e430 66 return false; // Blog system disabled.
cae83708 67 }
68
2c27b6ae 69 if (isloggedin() && $USER->id == $targetuserid) {
89f5e430 70 return true; // Can view own entries in any case.
cae83708 71 }
72
41b38360 73 $sitecontext = context_system::instance();
cae83708 74 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
89f5e430 75 return true; // Can manage all entries.
cae83708 76 }
c2ee4e87 77
89f5e430 78 // If blog is in draft state, then make sure user have proper capability.
1c7b8b93 79 if ($blogentry && $blogentry->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
89f5e430 80 return false; // Can not view draft of others.
cae83708 81 }
82
9a909b1a 83 // If blog entry is not public, make sure user is logged in.
1c7b8b93 84 if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) {
cae83708 85 return false;
86 }
d02240f3 87
89f5e430 88 // If blogentry is not passed or all above checks pass, then check capability based on system config.
cae83708 89 switch ($CFG->bloglevel) {
90 case BLOG_GLOBAL_LEVEL:
91 return true;
92 break;
c2ee4e87 93
cae83708 94 case BLOG_SITE_LEVEL:
89f5e430 95 if (isloggedin()) { // Not logged in viewers forbidden.
cae83708 96 return true;
c2ee4e87 97 }
cae83708 98 return false;
99 break;
d02240f3 100
cae83708 101 case BLOG_USER_LEVEL:
102 default:
89f5e430 103 // If user is viewing other user blog, then user should have user:readuserblogs capability.
41b38360 104 $personalcontext = context_user::instance($targetuserid);
cae83708 105 return has_capability('moodle/user:readuserblogs', $personalcontext);
106 break;
4a173181 107
cae83708 108 }
109}
110
111/**
112 * remove all associations for the blog entries of a particular user
113 * @param int userid - id of user whose blog associations will be deleted
114 */
115function blog_remove_associations_for_user($userid) {
1c7b8b93 116 global $DB;
d91181dc
PS
117 throw new coding_exception('function blog_remove_associations_for_user() is not finished');
118 /*
1c7b8b93
NC
119 $blogentries = blog_fetch_entries(array('user' => $userid), 'lasmodified DESC');
120 foreach ($blogentries as $entry) {
121 if (blog_user_can_edit_entry($entry)) {
122 blog_remove_associations_for_entry($entry->id);
123 }
b0e90a0c 124 }
d91181dc 125 */
cae83708 126}
127
128/**
1c7b8b93
NC
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
cae83708 131 */
1c7b8b93
NC
132function blog_remove_associations_for_course($courseid) {
133 global $DB;
41b38360 134 $context = context_course::instance($courseid);
1c7b8b93 135 $DB->delete_records('blog_association', array('contextid' => $context->id));
cae83708 136}
137
cae83708 138/**
139 * Given a record in the {blog_external} table, checks the blog's URL
b7c1da93 140 * for new entries not yet copied into Moodle.
50ff50da 141 * Also attempts to identify and remove deleted blog entries
cae83708 142 *
1c7b8b93
NC
143 * @param object $externalblog
144 * @return boolean False if the Feed is invalid
cae83708 145 */
1c7b8b93 146function blog_sync_external_entries($externalblog) {
cae83708 147 global $CFG, $DB;
e14de6f9 148 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
4a173181 149
d958e6bd
PS
150 $rss = new moodle_simplepie();
151 $rssfile = $rss->registry->create('File', array($externalblog->url));
152 $filetest = $rss->registry->create('Locator', array($rssfile));
1c7b8b93
NC
153
154 if (!$filetest->is_feed($rssfile)) {
155 $externalblog->failedlastsync = 1;
156 $DB->update_record('blog_external', $externalblog);
157 return false;
8397492b 158 } else if (!empty($externalblog->failedlastsync)) {
1c7b8b93
NC
159 $externalblog->failedlastsync = 0;
160 $DB->update_record('blog_external', $externalblog);
c2ee4e87 161 }
4a173181 162
d958e6bd
PS
163 $rss->set_feed_url($externalblog->url);
164 $rss->init();
c2ee4e87 165
e14de6f9 166 if (empty($rss->data)) {
cae83708 167 return null;
168 }
2b6e53e8 169 // Used to identify blog posts that have been deleted from the source feed.
cc52e53d 170 $oldesttimestamp = null;
50ff50da 171 $uniquehashes = array();
b13ee30e 172
e14de6f9 173 foreach ($rss->get_items() as $entry) {
2b6e53e8 174 // If filtertags are defined, use them to filter the entries by RSS category.
1c7b8b93
NC
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);
181
153b3ad0
SL
182 if (!empty($categories)) {
183 foreach ($categories as $category) {
184 if (in_array(trim(strtolower($category->term)), $filtertags)) {
185 $containsfiltertag = true;
186 }
1c7b8b93
NC
187 }
188 }
189
190 if (!$containsfiltertag) {
191 continue;
c2ee4e87 192 }
193 }
c484c852 194
50ff50da 195 $uniquehashes[] = $entry->get_permalink();
1c7b8b93 196
e463f508 197 $newentry = new stdClass();
1c7b8b93
NC
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;
2b6e53e8 204 // Clean subject of html, just in case.
a537e507 205 $newentry->subject = clean_param($entry->get_title(), PARAM_TEXT);
2b6e53e8
AD
206 // Observe 128 max chars in DB.
207 // TODO: +1 to raise this to 255.
2f1e464a
PS
208 if (core_text::strlen($newentry->subject) > 128) {
209 $newentry->subject = core_text::substr($newentry->subject, 0, 125) . '...';
a537e507 210 }
1c7b8b93 211 $newentry->summary = $entry->get_description();
c484c852 212
2b6e53e8
AD
213 // Used to decide whether to insert or update.
214 // Uses enty permalink plus creation date if available.
bb8a75e7 215 $existingpostconditions = array('uniquehash' => $entry->get_permalink());
99cd408f 216
2b6e53e8 217 // Our DB doesnt allow null creation or modified timestamps so check the external blog supplied one.
99cd408f 218 $entrydate = $entry->get_date('U');
afce96f0 219 if (!empty($entrydate)) {
220 $existingpostconditions['created'] = $entrydate;
221 }
c484c852 222
2b6e53e8 223 // The post ID or false if post not found in DB.
afce96f0 224 $postid = $DB->get_field('post', 'id', $existingpostconditions);
c484c852 225
afce96f0 226 $timestamp = null;
99cd408f 227 if (empty($entrydate)) {
afce96f0 228 $timestamp = time();
99cd408f 229 } else {
afce96f0 230 $timestamp = $entrydate;
99cd408f 231 }
c484c852 232
2b6e53e8 233 // Only set created if its a new post so we retain the original creation timestamp if the post is edited.
bb8a75e7 234 if ($postid === false) {
afce96f0 235 $newentry->created = $timestamp;
236 }
237 $newentry->lastmodified = $timestamp;
694fb770 238
bb8a75e7 239 if (empty($oldesttimestamp) || $timestamp < $oldesttimestamp) {
2b6e53e8 240 // Found an older post.
cc52e53d 241 $oldesttimestamp = $timestamp;
242 }
1c7b8b93 243
2f1e464a 244 if (core_text::strlen($newentry->uniquehash) > 255) {
3b59524d
SH
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);
249 continue;
250 }
251
bb8a75e7 252 if ($postid === false) {
9829e3d8 253 $id = $DB->insert_record('post', $newentry);
1c7b8b93 254
2b6e53e8 255 // Set tags.
abea2c5d
MG
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);
9829e3d8 258 }
259 } else {
260 $newentry->id = $postid;
afce96f0 261 $DB->update_record('post', $newentry);
1c7b8b93 262 }
cae83708 263 }
c484c852 264
ed79b89b
DM
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
269 FROM {post}
270 WHERE module = 'blog_external'
271 AND " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text(':blogid') . "
272 AND created > :ts";
42291c61
AD
273 $dbposts = $DB->get_records_sql($sql, array('blogid' => $externalblog->id, 'ts' => $oldesttimestamp));
274
50ff50da 275 $todelete = array();
2b6e53e8 276 foreach ($dbposts as $dbpost) {
29487578 277 if ( !in_array($dbpost->uniquehash, $uniquehashes) ) {
50ff50da 278 $todelete[] = $dbpost->id;
279 }
280 }
281 $DB->delete_records_list('post', 'id', $todelete);
c2ee4e87 282
e5137f24 283 $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => time()));
1c7b8b93
NC
284}
285
286/**
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
290 */
291function blog_delete_external_entries($externalblog) {
292 global $DB;
41b38360 293 require_capability('moodle/blog:manageexternal', context_system::instance());
a742eb1f
EL
294 $DB->delete_records_select('post',
295 "module='blog_external' AND " . $DB->sql_compare_text('content') . " = ?",
296 array($externalblog->id));
cae83708 297}
d02240f3 298
27bad0a6 299/**
593270c6 300 * This function checks that blogs are enabled, and that the user can see blogs at all
27bad0a6
SH
301 * @return bool
302 */
303function blog_is_enabled_for_user() {
304 global $CFG;
850d2db8 305 return (!empty($CFG->enableblogs) && (isloggedin() || ($CFG->bloglevel == BLOG_GLOBAL_LEVEL)));
27bad0a6
SH
306}
307
308/**
309 * This function gets all of the options available for the current user in respect
310 * to blogs.
897aa80c 311 *
27bad0a6
SH
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)
317 *
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.
321 */
322function blog_get_all_options(moodle_page $page, stdClass $userid = null) {
323 global $CFG, $DB, $USER;
324
325 $options = array();
326
2b6e53e8 327 // If blogs are enabled and the user is logged in and not a guest.
27bad0a6 328 if (blog_is_enabled_for_user()) {
2b6e53e8 329 // If the context is the user then assume we want to load for the users context.
27bad0a6
SH
330 if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) {
331 $userid = $page->context->instanceid;
332 }
2b6e53e8 333 // Check the userid var.
0e32a565 334 if (!is_null($userid) && $userid !== $USER->id) {
27bad0a6 335 // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't!
0e32a565 336 $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
27bad0a6
SH
337 } else {
338 $user = null;
339 }
340
341 if ($CFG->useblogassociations && $page->cm !== null) {
2b6e53e8 342 // Load for the module associated with the page.
27bad0a6
SH
343 $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user);
344 } else if ($CFG->useblogassociations && $page->course->id != SITEID) {
2b6e53e8 345 // Load the options for the course associated with the page.
27bad0a6
SH
346 $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user);
347 }
348
2b6e53e8 349 // Get the options for the user.
5b183f51 350 if ($user !== null and !isguestuser($user)) {
2b6e53e8 351 // Load for the requested user.
0e32a565 352 $options[CONTEXT_USER + 1] = blog_get_options_for_user($user);
27bad0a6 353 }
2b6e53e8 354 // Load for the current user.
5b183f51
PS
355 if (isloggedin() and !isguestuser()) {
356 $options[CONTEXT_USER] = blog_get_options_for_user();
357 }
27bad0a6
SH
358 }
359
2b6e53e8
AD
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())) {
364
27bad0a6
SH
365 $options[CONTEXT_SYSTEM] = array('viewsite' => array(
366 'string' => get_string('viewsiteentries', 'blog'),
367 'link' => new moodle_url('/blog/index.php')
368 ));
369 }
370
2b6e53e8 371 // Return the options.
27bad0a6
SH
372 return $options;
373}
374
375/**
376 * Get all of the blog options that relate to the passed user.
377 *
378 * If no user is passed the current user is assumed.
379 *
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
383 */
384function blog_get_options_for_user(stdClass $user=null) {
385 global $CFG, $USER;
2b6e53e8 386 // Cache.
27bad0a6
SH
387 static $useroptions = array();
388
389 $options = array();
2b6e53e8 390 // Blogs must be enabled and the user must be logged in.
27bad0a6
SH
391 if (!blog_is_enabled_for_user()) {
392 return $options;
393 }
394
2b6e53e8 395 // Sort out the user var.
27bad0a6
SH
396 if ($user === null || $user->id == $USER->id) {
397 $user = $USER;
398 $iscurrentuser = true;
399 } else {
400 $iscurrentuser = false;
401 }
402
2b6e53e8 403 // If we've already generated serve from the cache.
27bad0a6
SH
404 if (array_key_exists($user->id, $useroptions)) {
405 return $useroptions[$user->id];
406 }
407
41b38360 408 $sitecontext = context_system::instance();
27bad0a6
SH
409 $canview = has_capability('moodle/blog:view', $sitecontext);
410
411 if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
2b6e53e8 412 // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL.
27bad0a6
SH
413 $options['userentries'] = array(
414 'string' => get_string('viewuserentries', 'blog', fullname($user)),
0e32a565 415 'link' => new moodle_url('/blog/index.php', array('userid' => $user->id))
27bad0a6
SH
416 );
417 } else {
2b6e53e8 418 // It's the current user.
27bad0a6 419 if ($canview) {
2b6e53e8 420 // We can view our own blogs .... BIG surprise.
27bad0a6 421 $options['view'] = array(
4887d152 422 'string' => get_string('blogentries', 'blog'),
0e32a565 423 'link' => new moodle_url('/blog/index.php', array('userid' => $USER->id))
27bad0a6
SH
424 );
425 }
426 if (has_capability('moodle/blog:create', $sitecontext)) {
2b6e53e8 427 // We can add to our own blog.
27bad0a6
SH
428 $options['add'] = array(
429 'string' => get_string('addnewentry', 'blog'),
0e32a565 430 'link' => new moodle_url('/blog/edit.php', array('action' => 'add'))
27bad0a6
SH
431 );
432 }
433 }
0f4c6067 434 if ($canview && $CFG->enablerssfeeds) {
c000545d
JF
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))
2b6e53e8 438 );
c000545d
JF
439 }
440
2b6e53e8 441 // Cache the options.
27bad0a6 442 $useroptions[$user->id] = $options;
2b6e53e8 443 // Return the options.
27bad0a6
SH
444 return $options;
445}
446
447/**
448 * Get the blog options that relate to the given course for the given user.
449 *
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
454 */
455function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
456 global $CFG, $USER;
2b6e53e8 457 // Cache.
27bad0a6 458 static $courseoptions = array();
897aa80c 459
27bad0a6
SH
460 $options = array();
461
2b6e53e8 462 // User must be logged in and blogs must be enabled.
27bad0a6
SH
463 if (!blog_is_enabled_for_user()) {
464 return $options;
465 }
466
2b6e53e8 467 // Check that the user can associate with the course.
41b38360 468 $sitecontext = context_system::instance();
2b6e53e8 469 // Generate the cache key.
27bad0a6
SH
470 $key = $course->id.':';
471 if (!empty($user)) {
472 $key .= $user->id;
473 } else {
474 $key .= $USER->id;
475 }
2b6e53e8 476 // Serve from the cache if we've already generated for this course.
27bad0a6 477 if (array_key_exists($key, $courseoptions)) {
af8fe217 478 return $courseoptions[$key];
27bad0a6 479 }
897aa80c 480
4ef08298 481 if (has_capability('moodle/blog:view', $sitecontext)) {
27bad0a6
SH
482 // We can view!
483 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
2b6e53e8 484 // View entries about this course.
27bad0a6
SH
485 $options['courseview'] = array(
486 'string' => get_string('viewcourseblogs', 'blog'),
4ef08298 487 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id))
27bad0a6
SH
488 );
489 }
2b6e53e8 490 // View MY entries about this course.
27bad0a6
SH
491 $options['courseviewmine'] = array(
492 'string' => get_string('viewmyentriesaboutcourse', 'blog'),
4ef08298 493 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $USER->id))
27bad0a6
SH
494 );
495 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
2b6e53e8 496 // View the provided users entries about this course.
27bad0a6
SH
497 $options['courseviewuser'] = array(
498 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)),
4ef08298 499 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $user->id))
27bad0a6
SH
500 );
501 }
502 }
503
4ef08298 504 if (has_capability('moodle/blog:create', $sitecontext)) {
2b6e53e8 505 // We can blog about this course.
27bad0a6 506 $options['courseadd'] = array(
cfa11fd6 507 'string' => get_string('blogaboutthiscourse', 'blog'),
4ef08298 508 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'courseid' => $course->id))
27bad0a6
SH
509 );
510 }
511
2b6e53e8 512 // Cache the options for this course.
27bad0a6 513 $courseoptions[$key] = $options;
2b6e53e8 514 // Return the options.
27bad0a6
SH
515 return $options;
516}
517
518/**
519 * Get the blog options relating to the given module for the given user
520 *
521 * @staticvar array $moduleoptions Cache
78f0f64d 522 * @param stdClass|cm_info $module The module to get options for
27bad0a6
SH
523 * @param stdClass $user The user to get options for null == currentuser
524 * @return array
525 */
78f0f64d 526function blog_get_options_for_module($module, $user=null) {
27bad0a6 527 global $CFG, $USER;
2b6e53e8 528 // Cache.
27bad0a6
SH
529 static $moduleoptions = array();
530
531 $options = array();
2b6e53e8 532 // User must be logged in, blogs must be enabled.
27bad0a6
SH
533 if (!blog_is_enabled_for_user()) {
534 return $options;
535 }
536
41b38360 537 $sitecontext = context_system::instance();
27bad0a6 538
2b6e53e8 539 // Generate the cache key.
27bad0a6
SH
540 $key = $module->id.':';
541 if (!empty($user)) {
542 $key .= $user->id;
543 } else {
544 $key .= $USER->id;
545 }
546 if (array_key_exists($key, $moduleoptions)) {
2b6e53e8 547 // Serve from the cache so we don't have to regenerate.
46a710e9 548 return $moduleoptions[$key];
27bad0a6
SH
549 }
550
4ef08298 551 if (has_capability('moodle/blog:view', $sitecontext)) {
dc9fa9cb
RT
552 // Save correct module name for later usage.
553 $modulename = get_string('modulename', $module->modname);
4eaf120a 554
27bad0a6
SH
555 // We can view!
556 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
2b6e53e8 557 // View all entries about this module.
27bad0a6 558 $a = new stdClass;
dc9fa9cb 559 $a->type = $modulename;
27bad0a6
SH
560 $options['moduleview'] = array(
561 'string' => get_string('viewallmodentries', 'blog', $a),
0e32a565 562 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id))
27bad0a6
SH
563 );
564 }
2b6e53e8 565 // View MY entries about this module.
27bad0a6 566 $options['moduleviewmine'] = array(
dc9fa9cb 567 'string' => get_string('viewmyentriesaboutmodule', 'blog', $modulename),
0e32a565 568 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id, 'userid' => $USER->id))
27bad0a6
SH
569 );
570 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
2b6e53e8 571 // View the given users entries about this module.
27bad0a6 572 $a = new stdClass;
dc9fa9cb 573 $a->mod = $modulename;
27bad0a6
SH
574 $a->user = fullname($user);
575 $options['moduleviewuser'] = array(
576 'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a),
0e32a565 577 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id, 'userid' => $user->id))
27bad0a6
SH
578 );
579 }
580 }
581
4ef08298 582 if (has_capability('moodle/blog:create', $sitecontext)) {
2b6e53e8 583 // The user can blog about this module.
27bad0a6 584 $options['moduleadd'] = array(
dc9fa9cb 585 'string' => get_string('blogaboutthismodule', 'blog', $modulename),
0e32a565 586 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'modid' => $module->id))
27bad0a6
SH
587 );
588 }
2b6e53e8 589 // Cache the options.
27bad0a6 590 $moduleoptions[$key] = $options;
2b6e53e8 591 // Return the options.
27bad0a6
SH
592 return $options;
593}
594
cae83708 595/**
596 * This function encapsulates all the logic behind the complex
597 * navigation, titles and headings of the blog listing page, depending
1c7b8b93
NC
598 * on URL params. It looks at URL params and at the current context level.
599 * It builds and returns an array containing:
600 *
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
cae83708 606 *
c5dc10ee 607 * All other variables are set directly in $PAGE
cae83708 608 *
609 * It uses the current URL to build these variables.
610 * A number of mutually exclusive use cases are used to structure this function.
611 *
612 * @return array
613 */
451f1e38 614function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=null) {
cae83708 615 global $CFG, $PAGE, $DB, $USER;
616
9366362a 617 $id = optional_param('id', null, PARAM_INT);
cae83708 618 $tag = optional_param('tag', null, PARAM_NOTAGS);
451f1e38
AD
619 $tagid = optional_param('tagid', $tagid, PARAM_INT);
620 $userid = optional_param('userid', $userid, PARAM_INT);
cae83708 621 $modid = optional_param('modid', null, PARAM_INT);
622 $entryid = optional_param('entryid', null, PARAM_INT);
451f1e38
AD
623 $groupid = optional_param('groupid', $groupid, PARAM_INT);
624 $courseid = optional_param('courseid', $courseid, PARAM_INT);
cae83708 625 $search = optional_param('search', null, PARAM_RAW);
626 $action = optional_param('action', null, PARAM_ALPHA);
627 $confirm = optional_param('confirm', false, PARAM_BOOL);
628
2b6e53e8 629 // Ignore userid when action == add.
1c7b8b93
NC
630 if ($action == 'add' && $userid) {
631 unset($userid);
632 $PAGE->url->remove_params(array('userid'));
633 } else if ($action == 'add' && $entryid) {
634 unset($entryid);
635 $PAGE->url->remove_params(array('entryid'));
636 }
637
638 $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
cae83708 639
a6855934 640 $blogurl = new moodle_url('/blog/index.php');
1c7b8b93 641
1c7b8b93
NC
642 $headers['stradd'] = get_string('addnewentry', 'blog');
643 $headers['strview'] = null;
cae83708 644
1c7b8b93 645 $site = $DB->get_record('course', array('id' => SITEID));
41b38360 646 $sitecontext = context_system::instance();
2b6e53e8 647 // Common Lang strings.
cae83708 648 $strparticipants = get_string("participants");
649 $strblogentries = get_string("blogentries", 'blog');
650
2b6e53e8 651 // Prepare record objects as needed.
cae83708 652 if (!empty($courseid)) {
1c7b8b93 653 $headers['filters']['course'] = $courseid;
cae83708 654 $course = $DB->get_record('course', array('id' => $courseid));
655 }
e96f2a77 656
cae83708 657 if (!empty($userid)) {
1c7b8b93 658 $headers['filters']['user'] = $userid;
cae83708 659 $user = $DB->get_record('user', array('id' => $userid));
516194d0 660 }
bbbf2d40 661
2b6e53e8 662 if (!empty($groupid)) { // The groupid always overrides courseid.
1c7b8b93 663 $headers['filters']['group'] = $groupid;
cae83708 664 $group = $DB->get_record('groups', array('id' => $groupid));
665 $course = $DB->get_record('course', array('id' => $group->courseid));
666 }
bbbf2d40 667
897aa80c 668 $PAGE->set_pagelayout('standard');
27bad0a6 669
2b6e53e8 670 // The modid always overrides courseid, so the $course object may be reset here.
1b30a9fa
DM
671 if (!empty($modid) && $CFG->useblogassociations) {
672
1c7b8b93 673 $headers['filters']['module'] = $modid;
2b6e53e8 674 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case.
0e32a565 675 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
1c7b8b93 676 $course = $DB->get_record('course', array('id' => $courseid));
cae83708 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));
e463f508 680 $a = new stdClass();
1c7b8b93 681 $a->type = get_string('modulename', $cm->modname);
c5dc10ee 682 $PAGE->set_cm($cm, $course);
1c7b8b93
NC
683 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
684 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 685 }
b73d1ca4 686
1c7b8b93 687 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
2b6e53e8 688 // Note: if action is set to 'add' or 'edit', we do this at the end.
1c7b8b93
NC
689 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
690 $PAGE->navbar->add($strblogentries, $blogurl);
4e1f6047
DW
691 $PAGE->set_title($site->fullname);
692 $PAGE->set_heading($site->fullname);
693 $headers['heading'] = get_string('siteblogheading', 'blog');
4a8b890a 694 }
c2ee4e87 695
2b6e53e8 696 // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information.
cae83708 697 if (!empty($entryid)) {
1c7b8b93
NC
698 $headers['filters']['entry'] = $entryid;
699 $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id';
cae83708 700 $user = $DB->get_record_sql($sql, array($entryid));
1c7b8b93 701 $entry = $DB->get_record('post', array('id' => $entryid));
c2ee4e87 702
1c7b8b93 703 $blogurl->param('userid', $user->id);
c2ee4e87 704
cae83708 705 if (!empty($course)) {
9366362a 706 $mycourseid = $course->id;
1c7b8b93 707 $blogurl->param('courseid', $mycourseid);
c2ee4e87 708 } else {
9366362a 709 $mycourseid = $site->id;
c2ee4e87 710 }
41b38360 711 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
e14de6f9 712
1c7b8b93
NC
713 $PAGE->navbar->add($strblogentries, $blogurl);
714
715 $blogurl->remove_params('userid');
716 $PAGE->navbar->add($entry->subject, $blogurl);
8ebbb06a
SH
717 $PAGE->set_title("$shortname: " . fullname($user) . ": $entry->subject");
718 $PAGE->set_heading("$shortname: " . fullname($user) . ": $entry->subject");
cae83708 719 $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user));
720
2b6e53e8 721 // We ignore tag and search params.
1c7b8b93
NC
722 if (empty($action) || !$CFG->useblogassociations) {
723 $headers['url'] = $blogurl;
9366362a 724 return $headers;
725 }
c2ee4e87 726 }
240075cd 727
1c7b8b93 728 if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
2b6e53e8
AD
729 // Case 3: A user's blog entries.
730
41b38360 731 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
1c7b8b93 732 $blogurl->param('userid', $userid);
8ebbb06a
SH
733 $PAGE->set_title("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
734 $PAGE->set_heading("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
cae83708 735 $headers['heading'] = get_string('userblog', 'blog', fullname($user));
27bad0a6 736 $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user));
1c7b8b93 737
2b6e53e8
AD
738 } else if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) {
739 // Case 4: No blog associations, no userid.
cae83708 740
4e1f6047
DW
741 $PAGE->set_title($site->fullname);
742 $PAGE->set_heading($site->fullname);
743 $headers['heading'] = get_string('siteblogheading', 'blog');
2b6e53e8
AD
744 } else if (!empty($userid) && !empty($modid) && empty($entryid)) {
745 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored).
cae83708 746
41b38360 747 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
1c7b8b93
NC
748 $blogurl->param('userid', $userid);
749 $blogurl->param('modid', $modid);
cae83708 750
2b6e53e8 751 // Course module navigation is handled by build_navigation as the second param.
cae83708 752 $headers['cm'] = $cm;
c5dc10ee 753 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 754 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 755
8ebbb06a
SH
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'));
cae83708 758
e463f508 759 $a = new stdClass();
cae83708 760 $a->user = fullname($user);
761 $a->mod = $cm->name;
1c7b8b93 762 $a->type = get_string('modulename', $cm->modname);
cae83708 763 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
1c7b8b93
NC
764 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
765 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
2b6e53e8
AD
766 } else if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
767 // Case 6: Blog entries associated with a course by a specific user.
cae83708 768
1c7b8b93
NC
769 $blogurl->param('userid', $userid);
770 $blogurl->param('courseid', $courseid);
2c27b6ae 771
4e1f6047
DW
772 $PAGE->set_title($course->fullname);
773 $PAGE->set_heading($course->fullname);
cae83708 774
e463f508 775 $a = new stdClass();
cae83708 776 $a->user = fullname($user);
41b38360 777 $a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
1c7b8b93 778 $a->type = get_string('course');
cae83708 779 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
1c7b8b93
NC
780 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
781 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
782
2b6e53e8 783 // Remove the userid from the URL to inform the blog_menu block correctly.
1c7b8b93 784 $blogurl->remove_params(array('userid'));
2b6e53e8
AD
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.
cae83708 787
1c7b8b93 788 $blogurl->param('courseid', $course->id);
e14de6f9 789
1c7b8b93
NC
790 $PAGE->navbar->add($strblogentries, $blogurl);
791 $blogurl->remove_params(array('courseid'));
792 $blogurl->param('groupid', $groupid);
793 $PAGE->navbar->add($group->name, $blogurl);
cae83708 794
4e1f6047
DW
795 $PAGE->set_title($course->fullname);
796 $PAGE->set_heading($course->fullname);
cae83708 797
e463f508 798 $a = new stdClass();
cae83708 799 $a->group = $group->name;
41b38360 800 $a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
1c7b8b93 801 $a->type = get_string('course');
cae83708 802 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
1c7b8b93
NC
803 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
804 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
2b6e53e8
AD
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.
cae83708 807
cae83708 808 $headers['cm'] = $cm;
1c7b8b93
NC
809 $blogurl->param('modid', $modid);
810 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 811
1c7b8b93
NC
812 $blogurl->param('groupid', $groupid);
813 $PAGE->navbar->add($group->name, $blogurl);
cae83708 814
4e1f6047
DW
815 $PAGE->set_title($course->fullname);
816 $PAGE->set_heading($course->fullname);
cae83708 817
e463f508 818 $a = new stdClass();
cae83708 819 $a->group = $group->name;
820 $a->mod = $cm->name;
1c7b8b93 821 $a->type = get_string('modulename', $cm->modname);
cae83708 822 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
1c7b8b93
NC
823 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
824 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 825
2b6e53e8
AD
826 } else if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
827 // Case 9: All blog entries associated with an activity.
cae83708 828
c5dc10ee 829 $PAGE->set_cm($cm, $course);
1c7b8b93
NC
830 $blogurl->param('modid', $modid);
831 $PAGE->navbar->add($strblogentries, $blogurl);
4e1f6047
DW
832 $PAGE->set_title($course->fullname);
833 $PAGE->set_heading($course->fullname);
cae83708 834 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
e463f508 835 $a = new stdClass();
1c7b8b93
NC
836 $a->type = get_string('modulename', $cm->modname);
837 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
838 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
2b6e53e8
AD
839 } else if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
840 // Case 10: All blog entries associated with a course.
cae83708 841
1c7b8b93
NC
842 $blogurl->param('courseid', $courseid);
843 $PAGE->navbar->add($strblogentries, $blogurl);
4e1f6047
DW
844 $PAGE->set_title($course->fullname);
845 $PAGE->set_heading($course->fullname);
e463f508 846 $a = new stdClass();
1c7b8b93 847 $a->type = get_string('course');
2b6e53e8
AD
848 $headers['heading'] = get_string('blogentriesabout',
849 'blog',
850 format_string($course->fullname,
851 true,
852 array('context' => context_course::instance($course->id))));
1c7b8b93
NC
853 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
854 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
855 $blogurl->remove_params(array('userid'));
856 }
857
858 if (!in_array($action, array('edit', 'add'))) {
2b6e53e8 859 // Append Tag info.
1c7b8b93
NC
860 if (!empty($tagid)) {
861 $headers['filters']['tag'] = $tagid;
862 $blogurl->param('tagid', $tagid);
0e32a565 863 $tagrec = $DB->get_record('tag', array('id' => $tagid));
1c7b8b93 864 $PAGE->navbar->add($tagrec->name, $blogurl);
2b6e53e8 865 } else if (!empty($tag)) {
1af9063e
AA
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);
871 }
1c7b8b93 872 }
240075cd 873
2b6e53e8 874 // Append Search info.
99fbcc7b 875 if (!empty($search) && has_capability('moodle/blog:search', $sitecontext)) {
1c7b8b93
NC
876 $headers['filters']['search'] = $search;
877 $blogurl->param('search', $search);
878 $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
879 }
ee00eb8c 880 }
881
2b6e53e8 882 // Append edit mode info.
cae83708 883 if (!empty($action) && $action == 'add') {
f36b47ef 884
cae83708 885 } else if (!empty($action) && $action == 'edit') {
c5dc10ee 886 $PAGE->navbar->add(get_string('editentry', 'blog'));
240075cd 887 }
ee00eb8c 888
1c7b8b93
NC
889 if (empty($headers['url'])) {
890 $headers['url'] = $blogurl;
891 }
cae83708 892 return $headers;
893}
23677261 894
1c7b8b93
NC
895/**
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
900 */
901function blog_get_associated_count($courseid, $cmid=null) {
902 global $DB;
41b38360 903 $context = context_course::instance($courseid);
1c7b8b93 904 if ($cmid) {
41b38360 905 $context = context_module::instance($cmid);
1c7b8b93
NC
906 }
907 return $DB->count_records('blog_association', array('contextid' => $context->id));
593270c6 908}
c1951ea9
DC
909
910/**
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
914 * exceptions.
9a909b1a
RT
915 * blog_comment_validate will be called before viewing/adding/deleting
916 * comment, so don't repeat checks.
c1951ea9
DC
917 * Capability check has been done in comment->check_permissions(), we
918 * don't need to do it again here.
919 *
35453657
DC
920 * @package core_blog
921 * @category comment
922 *
0e32a565 923 * @param stdClass $commentparam {
c1951ea9
DC
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
929 * }
930 * @return array
931 */
0e32a565 932function blog_comment_permissions($commentparam) {
9a909b1a
RT
933 global $DB;
934
935 // If blog is public and current user is guest, then don't let him post comments.
0e32a565 936 $blogentry = $DB->get_record('post', array('id' => $commentparam->itemid), 'publishstate', MUST_EXIST);
9a909b1a
RT
937
938 if ($blogentry->publishstate != 'public') {
939 if (!isloggedin() || isguestuser()) {
940 return array('post' => false, 'view' => true);
941 }
942 }
943 return array('post' => true, 'view' => true);
c1951ea9
DC
944}
945
946/**
947 * Validate comment parameter before perform other comments actions
948 *
35453657
DC
949 * @package core_blog
950 * @category comment
951 *
c1951ea9
DC
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
958 * }
959 * @return boolean
960 */
0e32a565 961function blog_comment_validate($commentparam) {
9a909b1a
RT
962 global $CFG, $DB, $USER;
963
964 // Check if blogs are enabled user can comment.
965 if (empty($CFG->enableblogs) || empty($CFG->blogusecomments)) {
966 throw new comment_exception('nopermissiontocomment');
c1951ea9 967 }
9a909b1a 968
89f5e430 969 // Validate comment area.
0e32a565 970 if ($commentparam->commentarea != 'format_blog') {
c1951ea9
DC
971 throw new comment_exception('invalidcommentarea');
972 }
9a909b1a 973
0e32a565 974 $blogentry = $DB->get_record('post', array('id' => $commentparam->itemid), '*', MUST_EXIST);
9a909b1a 975
89f5e430 976 // Validation for comment deletion.
0e32a565
AD
977 if (!empty($commentparam->commentid)) {
978 if ($record = $DB->get_record('comments', array('id' => $commentparam->commentid))) {
c1951ea9
DC
979 if ($record->commentarea != 'format_blog') {
980 throw new comment_exception('invalidcommentarea');
981 }
0e32a565 982 if ($record->contextid != $commentparam->context->id) {
c1951ea9
DC
983 throw new comment_exception('invalidcontext');
984 }
0e32a565 985 if ($record->itemid != $commentparam->itemid) {
c1951ea9
DC
986 throw new comment_exception('invalidcommentitemid');
987 }
988 } else {
989 throw new comment_exception('invalidcommentid');
990 }
991 }
9a909b1a
RT
992
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);
c1951ea9 997}
b1627a92
DC
998
999/**
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
1004 */
b38e2e28 1005function blog_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92 1006 return array(
0e32a565
AD
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')
b1627a92
DC
1011 );
1012}
b19cc4ef
AA
1013
1014/**
1015 * Add nodes to myprofile page.
1016 *
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
1021 *
1022 * @return bool
1023 */
1024function core_blog_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
0c713162 1025 global $CFG;
b19cc4ef
AA
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.
1030 return true;
1031 }
c5954f64 1032 if (!blog_user_can_view_user_entry($user->id)) {
0c713162
AG
1033 return true;
1034 }
b19cc4ef 1035 $url = new moodle_url("/blog/index.php", array('userid' => $user->id));
6e918b64
AG
1036 if (!empty($course)) {
1037 $url->param('courseid', $course->id);
1038 }
b19cc4ef 1039 if ($iscurrentuser) {
4887d152 1040 $title = get_string('blogentries', 'core_blog');
b19cc4ef 1041 } else {
e29866b5 1042 $title = get_string('myprofileuserblogs', 'core_blog');
b19cc4ef
AA
1043 }
1044 $blognode = new core_user\output\myprofile\node('miscellaneous', 'blogs', $title, null, $url);
1045 $tree->add_node($blognode);
1046 return true;
c1f97c77 1047}
abea2c5d
MG
1048
1049/**
1050 * Returns posts tagged with a specified tag.
1051 *
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
1061 */
1062function 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');
1065
1066 $systemcontext = context_system::instance();
1067 $perpage = $exclusivemode ? 20 : 5;
1068 $context = $ctx ? context::instance_by_id($ctx) : context_system::instance();
1069
1070 $content = '';
1071 if (empty($CFG->enableblogs) || !has_capability('moodle/blog:view', $systemcontext)) {
1072 // Blogs are not enabled or are not visible to the current user.
1073 $totalpages = 0;
1074 } else if ($context->contextlevel != CONTEXT_SYSTEM && empty($CFG->useblogassociations)) {
1075 // No blog entries can be associated to the non-system context.
1076 $totalpages = 0;
1077 } else if (!$rec && $context->contextlevel != CONTEXT_COURSE && $context->contextlevel != CONTEXT_MODULE) {
1078 // No blog entries can be associated with category or block context.
1079 $totalpages = 0;
1080 } else {
1081 require_once($CFG->dirroot.'/blog/locallib.php');
1082
1083 $filters = array('tag' => $tag->id);
1084 if ($rec) {
1085 if ($context->contextlevel != CONTEXT_SYSTEM) {
1086 $filters['context'] = $context->id;
1087 }
1088 } else if ($context->contextlevel == CONTEXT_COURSE) {
1089 $filters['course'] = $context->instanceid;
1090 } else if ($context->contextlevel == CONTEXT_MODULE) {
1091 $filters['module'] = $context->instanceid;
1092 }
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;
1102 $user->deleted = 0;
1103 $img = $OUTPUT->user_picture($user, array('size' => 35));
1104 $subject = format_string($blog->subject);
1105
1106 if ($blog->publishstate == 'draft') {
1107 $class = 'dimmed';
1108 } else {
1109 $class = '';
1110 }
1111
1112 $url = new moodle_url('/blog/index.php', array('entryid' => $blog->id));
1113 $subject = html_writer::link($url, $subject, array('class' => $class));
1114
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);
1119 }
1120 $details = $fullname . ', ' . userdate($blog->created);
1121
1122 $tagfeed->add($img, $subject, $details);
1123 }
1124
1125 $items = $tagfeed->export_for_template($OUTPUT);
1126 $content = $OUTPUT->render_from_template('core_tag/tagfeed', $items);
1127
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;
1133 }
1134 $allblogsurl = new moodle_url('/blog/index.php', $urlparams);
1135
1136 $rv = new core_tag\output\tagindex($tag, 'core', 'post',
1137 $content,
1138 $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
1139 $rv->exclusiveurl = $allblogsurl;
1140 return $rv;
1141 }
1142 }
1143
1144 $rv = new core_tag\output\tagindex($tag, 'core', 'post',
1145 $content,
1146 $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
1147 $rv->exclusiveurl = null;
1148 return $rv;
1149}