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
c69a43f2
MG
138/**
139 * Remove module associated blogs and blog tag instances.
140 *
141 * @param int $modcontextid Module context ID.
142 */
143function blog_remove_associations_for_module($modcontextid) {
144 global $DB;
145
146 if (!empty($assocblogids = $DB->get_fieldset_select('blog_association', 'blogid',
147 'contextid = :contextid', ['contextid' => $modcontextid]))) {
148 list($sql, $params) = $DB->get_in_or_equal($assocblogids, SQL_PARAMS_NAMED);
149
150 $DB->delete_records_select('tag_instance', "itemid $sql", $params);
151 $DB->delete_records_select('post', "id $sql AND module = :module",
152 array_merge($params, ['module' => 'blog']));
153 $DB->delete_records('blog_association', ['contextid' => $modcontextid]);
154 }
155}
156
cae83708 157/**
158 * Given a record in the {blog_external} table, checks the blog's URL
b7c1da93 159 * for new entries not yet copied into Moodle.
50ff50da 160 * Also attempts to identify and remove deleted blog entries
cae83708 161 *
1c7b8b93
NC
162 * @param object $externalblog
163 * @return boolean False if the Feed is invalid
cae83708 164 */
1c7b8b93 165function blog_sync_external_entries($externalblog) {
cae83708 166 global $CFG, $DB;
e14de6f9 167 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
4a173181 168
d958e6bd
PS
169 $rss = new moodle_simplepie();
170 $rssfile = $rss->registry->create('File', array($externalblog->url));
171 $filetest = $rss->registry->create('Locator', array($rssfile));
1c7b8b93
NC
172
173 if (!$filetest->is_feed($rssfile)) {
174 $externalblog->failedlastsync = 1;
175 $DB->update_record('blog_external', $externalblog);
176 return false;
8397492b 177 } else if (!empty($externalblog->failedlastsync)) {
1c7b8b93
NC
178 $externalblog->failedlastsync = 0;
179 $DB->update_record('blog_external', $externalblog);
c2ee4e87 180 }
4a173181 181
d958e6bd
PS
182 $rss->set_feed_url($externalblog->url);
183 $rss->init();
c2ee4e87 184
e14de6f9 185 if (empty($rss->data)) {
cae83708 186 return null;
187 }
2b6e53e8 188 // Used to identify blog posts that have been deleted from the source feed.
cc52e53d 189 $oldesttimestamp = null;
50ff50da 190 $uniquehashes = array();
b13ee30e 191
e14de6f9 192 foreach ($rss->get_items() as $entry) {
2b6e53e8 193 // If filtertags are defined, use them to filter the entries by RSS category.
1c7b8b93
NC
194 if (!empty($externalblog->filtertags)) {
195 $containsfiltertag = false;
196 $categories = $entry->get_categories();
197 $filtertags = explode(',', $externalblog->filtertags);
198 $filtertags = array_map('trim', $filtertags);
199 $filtertags = array_map('strtolower', $filtertags);
200
153b3ad0
SL
201 if (!empty($categories)) {
202 foreach ($categories as $category) {
203 if (in_array(trim(strtolower($category->term)), $filtertags)) {
204 $containsfiltertag = true;
205 }
1c7b8b93
NC
206 }
207 }
208
209 if (!$containsfiltertag) {
210 continue;
c2ee4e87 211 }
212 }
c484c852 213
50ff50da 214 $uniquehashes[] = $entry->get_permalink();
1c7b8b93 215
e463f508 216 $newentry = new stdClass();
1c7b8b93
NC
217 $newentry->userid = $externalblog->userid;
218 $newentry->module = 'blog_external';
219 $newentry->content = $externalblog->id;
220 $newentry->uniquehash = $entry->get_permalink();
221 $newentry->publishstate = 'site';
222 $newentry->format = FORMAT_HTML;
2b6e53e8 223 // Clean subject of html, just in case.
a537e507 224 $newentry->subject = clean_param($entry->get_title(), PARAM_TEXT);
2b6e53e8
AD
225 // Observe 128 max chars in DB.
226 // TODO: +1 to raise this to 255.
2f1e464a
PS
227 if (core_text::strlen($newentry->subject) > 128) {
228 $newentry->subject = core_text::substr($newentry->subject, 0, 125) . '...';
a537e507 229 }
1c7b8b93 230 $newentry->summary = $entry->get_description();
c484c852 231
2b6e53e8
AD
232 // Used to decide whether to insert or update.
233 // Uses enty permalink plus creation date if available.
bb8a75e7 234 $existingpostconditions = array('uniquehash' => $entry->get_permalink());
99cd408f 235
2b6e53e8 236 // Our DB doesnt allow null creation or modified timestamps so check the external blog supplied one.
99cd408f 237 $entrydate = $entry->get_date('U');
afce96f0 238 if (!empty($entrydate)) {
239 $existingpostconditions['created'] = $entrydate;
240 }
c484c852 241
2b6e53e8 242 // The post ID or false if post not found in DB.
afce96f0 243 $postid = $DB->get_field('post', 'id', $existingpostconditions);
c484c852 244
afce96f0 245 $timestamp = null;
99cd408f 246 if (empty($entrydate)) {
afce96f0 247 $timestamp = time();
99cd408f 248 } else {
afce96f0 249 $timestamp = $entrydate;
99cd408f 250 }
c484c852 251
2b6e53e8 252 // Only set created if its a new post so we retain the original creation timestamp if the post is edited.
bb8a75e7 253 if ($postid === false) {
afce96f0 254 $newentry->created = $timestamp;
255 }
256 $newentry->lastmodified = $timestamp;
694fb770 257
bb8a75e7 258 if (empty($oldesttimestamp) || $timestamp < $oldesttimestamp) {
2b6e53e8 259 // Found an older post.
cc52e53d 260 $oldesttimestamp = $timestamp;
261 }
1c7b8b93 262
2f1e464a 263 if (core_text::strlen($newentry->uniquehash) > 255) {
3b59524d
SH
264 // The URL for this item is too long for the field. Rather than add
265 // the entry without the link we will skip straight over it.
266 // RSS spec says recommended length 500, we use 255.
267 debugging('External blog entry skipped because of oversized URL', DEBUG_DEVELOPER);
268 continue;
269 }
270
bb8a75e7 271 if ($postid === false) {
9829e3d8 272 $id = $DB->insert_record('post', $newentry);
1c7b8b93 273
2b6e53e8 274 // Set tags.
abea2c5d
MG
275 if ($tags = core_tag_tag::get_item_tags_array('core', 'blog_external', $externalblog->id)) {
276 core_tag_tag::set_item_tags('core', 'post', $id, context_user::instance($externalblog->userid), $tags);
9829e3d8 277 }
278 } else {
279 $newentry->id = $postid;
afce96f0 280 $DB->update_record('post', $newentry);
1c7b8b93 281 }
cae83708 282 }
c484c852 283
ed79b89b
DM
284 // Look at the posts we have in the database to check if any of them have been deleted from the feed.
285 // Only checking posts within the time frame returned by the rss feed. Older items may have been deleted or
286 // may just not be returned anymore. We can't tell the difference so we leave older posts alone.
287 $sql = "SELECT id, uniquehash
288 FROM {post}
289 WHERE module = 'blog_external'
290 AND " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text(':blogid') . "
291 AND created > :ts";
42291c61
AD
292 $dbposts = $DB->get_records_sql($sql, array('blogid' => $externalblog->id, 'ts' => $oldesttimestamp));
293
50ff50da 294 $todelete = array();
2b6e53e8 295 foreach ($dbposts as $dbpost) {
29487578 296 if ( !in_array($dbpost->uniquehash, $uniquehashes) ) {
50ff50da 297 $todelete[] = $dbpost->id;
298 }
299 }
300 $DB->delete_records_list('post', 'id', $todelete);
c2ee4e87 301
e5137f24 302 $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => time()));
1c7b8b93
NC
303}
304
305/**
306 * Given an external blog object, deletes all related blog entries from the post table.
307 * NOTE: The external blog's id is saved as post.content, a field that is not oterhwise used by blog entries.
308 * @param object $externablog
309 */
310function blog_delete_external_entries($externalblog) {
311 global $DB;
41b38360 312 require_capability('moodle/blog:manageexternal', context_system::instance());
a742eb1f
EL
313 $DB->delete_records_select('post',
314 "module='blog_external' AND " . $DB->sql_compare_text('content') . " = ?",
315 array($externalblog->id));
cae83708 316}
d02240f3 317
27bad0a6 318/**
593270c6 319 * This function checks that blogs are enabled, and that the user can see blogs at all
27bad0a6
SH
320 * @return bool
321 */
322function blog_is_enabled_for_user() {
323 global $CFG;
850d2db8 324 return (!empty($CFG->enableblogs) && (isloggedin() || ($CFG->bloglevel == BLOG_GLOBAL_LEVEL)));
27bad0a6
SH
325}
326
327/**
328 * This function gets all of the options available for the current user in respect
329 * to blogs.
897aa80c 330 *
27bad0a6
SH
331 * It loads the following if applicable:
332 * - Module options {@see blog_get_options_for_module}
333 * - Course options {@see blog_get_options_for_course}
334 * - User specific options {@see blog_get_options_for_user}
335 * - General options (BLOG_LEVEL_GLOBAL)
336 *
337 * @param moodle_page $page The page to load for (normally $PAGE)
338 * @param stdClass $userid Load for a specific user
339 * @return array An array of options organised by type.
340 */
341function blog_get_all_options(moodle_page $page, stdClass $userid = null) {
342 global $CFG, $DB, $USER;
343
344 $options = array();
345
2b6e53e8 346 // If blogs are enabled and the user is logged in and not a guest.
27bad0a6 347 if (blog_is_enabled_for_user()) {
2b6e53e8 348 // If the context is the user then assume we want to load for the users context.
27bad0a6
SH
349 if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) {
350 $userid = $page->context->instanceid;
351 }
2b6e53e8 352 // Check the userid var.
0e32a565 353 if (!is_null($userid) && $userid !== $USER->id) {
27bad0a6 354 // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't!
0e32a565 355 $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
27bad0a6
SH
356 } else {
357 $user = null;
358 }
359
360 if ($CFG->useblogassociations && $page->cm !== null) {
2b6e53e8 361 // Load for the module associated with the page.
27bad0a6
SH
362 $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user);
363 } else if ($CFG->useblogassociations && $page->course->id != SITEID) {
2b6e53e8 364 // Load the options for the course associated with the page.
27bad0a6
SH
365 $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user);
366 }
367
2b6e53e8 368 // Get the options for the user.
5b183f51 369 if ($user !== null and !isguestuser($user)) {
2b6e53e8 370 // Load for the requested user.
0e32a565 371 $options[CONTEXT_USER + 1] = blog_get_options_for_user($user);
27bad0a6 372 }
2b6e53e8 373 // Load for the current user.
5b183f51
PS
374 if (isloggedin() and !isguestuser()) {
375 $options[CONTEXT_USER] = blog_get_options_for_user();
376 }
27bad0a6
SH
377 }
378
2b6e53e8
AD
379 // If blog level is global then display a link to view all site entries.
380 if (!empty($CFG->enableblogs)
381 && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL
382 && has_capability('moodle/blog:view', context_system::instance())) {
383
27bad0a6
SH
384 $options[CONTEXT_SYSTEM] = array('viewsite' => array(
385 'string' => get_string('viewsiteentries', 'blog'),
386 'link' => new moodle_url('/blog/index.php')
387 ));
388 }
389
2b6e53e8 390 // Return the options.
27bad0a6
SH
391 return $options;
392}
393
394/**
395 * Get all of the blog options that relate to the passed user.
396 *
397 * If no user is passed the current user is assumed.
398 *
399 * @staticvar array $useroptions Cache so we don't have to regenerate multiple times
400 * @param stdClass $user
401 * @return array The array of options for the requested user
402 */
403function blog_get_options_for_user(stdClass $user=null) {
404 global $CFG, $USER;
2b6e53e8 405 // Cache.
27bad0a6
SH
406 static $useroptions = array();
407
408 $options = array();
2b6e53e8 409 // Blogs must be enabled and the user must be logged in.
27bad0a6
SH
410 if (!blog_is_enabled_for_user()) {
411 return $options;
412 }
413
2b6e53e8 414 // Sort out the user var.
27bad0a6
SH
415 if ($user === null || $user->id == $USER->id) {
416 $user = $USER;
417 $iscurrentuser = true;
418 } else {
419 $iscurrentuser = false;
420 }
421
2b6e53e8 422 // If we've already generated serve from the cache.
27bad0a6
SH
423 if (array_key_exists($user->id, $useroptions)) {
424 return $useroptions[$user->id];
425 }
426
41b38360 427 $sitecontext = context_system::instance();
27bad0a6
SH
428 $canview = has_capability('moodle/blog:view', $sitecontext);
429
430 if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
2b6e53e8 431 // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL.
27bad0a6
SH
432 $options['userentries'] = array(
433 'string' => get_string('viewuserentries', 'blog', fullname($user)),
0e32a565 434 'link' => new moodle_url('/blog/index.php', array('userid' => $user->id))
27bad0a6
SH
435 );
436 } else {
2b6e53e8 437 // It's the current user.
27bad0a6 438 if ($canview) {
2b6e53e8 439 // We can view our own blogs .... BIG surprise.
27bad0a6 440 $options['view'] = array(
4887d152 441 'string' => get_string('blogentries', 'blog'),
0e32a565 442 'link' => new moodle_url('/blog/index.php', array('userid' => $USER->id))
27bad0a6
SH
443 );
444 }
445 if (has_capability('moodle/blog:create', $sitecontext)) {
2b6e53e8 446 // We can add to our own blog.
27bad0a6
SH
447 $options['add'] = array(
448 'string' => get_string('addnewentry', 'blog'),
0e32a565 449 'link' => new moodle_url('/blog/edit.php', array('action' => 'add'))
27bad0a6
SH
450 );
451 }
452 }
0f4c6067 453 if ($canview && $CFG->enablerssfeeds) {
c000545d
JF
454 $options['rss'] = array(
455 'string' => get_string('rssfeed', 'blog'),
456 'link' => new moodle_url(rss_get_url($sitecontext->id, $USER->id, 'blog', 'user/'.$user->id))
2b6e53e8 457 );
c000545d
JF
458 }
459
2b6e53e8 460 // Cache the options.
27bad0a6 461 $useroptions[$user->id] = $options;
2b6e53e8 462 // Return the options.
27bad0a6
SH
463 return $options;
464}
465
466/**
467 * Get the blog options that relate to the given course for the given user.
468 *
469 * @staticvar array $courseoptions A cache so we can save regenerating multiple times
470 * @param stdClass $course The course to load options for
471 * @param stdClass $user The user to load options for null == current user
472 * @return array The array of options
473 */
474function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
475 global $CFG, $USER;
2b6e53e8 476 // Cache.
27bad0a6 477 static $courseoptions = array();
897aa80c 478
27bad0a6
SH
479 $options = array();
480
2b6e53e8 481 // User must be logged in and blogs must be enabled.
27bad0a6
SH
482 if (!blog_is_enabled_for_user()) {
483 return $options;
484 }
485
2b6e53e8 486 // Check that the user can associate with the course.
41b38360 487 $sitecontext = context_system::instance();
2b6e53e8 488 // Generate the cache key.
27bad0a6
SH
489 $key = $course->id.':';
490 if (!empty($user)) {
491 $key .= $user->id;
492 } else {
493 $key .= $USER->id;
494 }
2b6e53e8 495 // Serve from the cache if we've already generated for this course.
27bad0a6 496 if (array_key_exists($key, $courseoptions)) {
af8fe217 497 return $courseoptions[$key];
27bad0a6 498 }
897aa80c 499
4ef08298 500 if (has_capability('moodle/blog:view', $sitecontext)) {
27bad0a6
SH
501 // We can view!
502 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
2b6e53e8 503 // View entries about this course.
27bad0a6
SH
504 $options['courseview'] = array(
505 'string' => get_string('viewcourseblogs', 'blog'),
4ef08298 506 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id))
27bad0a6
SH
507 );
508 }
2b6e53e8 509 // View MY entries about this course.
27bad0a6
SH
510 $options['courseviewmine'] = array(
511 'string' => get_string('viewmyentriesaboutcourse', 'blog'),
4ef08298 512 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $USER->id))
27bad0a6
SH
513 );
514 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
2b6e53e8 515 // View the provided users entries about this course.
27bad0a6
SH
516 $options['courseviewuser'] = array(
517 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)),
4ef08298 518 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $user->id))
27bad0a6
SH
519 );
520 }
521 }
522
4ef08298 523 if (has_capability('moodle/blog:create', $sitecontext)) {
2b6e53e8 524 // We can blog about this course.
27bad0a6 525 $options['courseadd'] = array(
cfa11fd6 526 'string' => get_string('blogaboutthiscourse', 'blog'),
4ef08298 527 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'courseid' => $course->id))
27bad0a6
SH
528 );
529 }
530
2b6e53e8 531 // Cache the options for this course.
27bad0a6 532 $courseoptions[$key] = $options;
2b6e53e8 533 // Return the options.
27bad0a6
SH
534 return $options;
535}
536
537/**
538 * Get the blog options relating to the given module for the given user
539 *
540 * @staticvar array $moduleoptions Cache
78f0f64d 541 * @param stdClass|cm_info $module The module to get options for
27bad0a6
SH
542 * @param stdClass $user The user to get options for null == currentuser
543 * @return array
544 */
78f0f64d 545function blog_get_options_for_module($module, $user=null) {
27bad0a6 546 global $CFG, $USER;
2b6e53e8 547 // Cache.
27bad0a6
SH
548 static $moduleoptions = array();
549
550 $options = array();
2b6e53e8 551 // User must be logged in, blogs must be enabled.
27bad0a6
SH
552 if (!blog_is_enabled_for_user()) {
553 return $options;
554 }
555
41b38360 556 $sitecontext = context_system::instance();
27bad0a6 557
2b6e53e8 558 // Generate the cache key.
27bad0a6
SH
559 $key = $module->id.':';
560 if (!empty($user)) {
561 $key .= $user->id;
562 } else {
563 $key .= $USER->id;
564 }
565 if (array_key_exists($key, $moduleoptions)) {
2b6e53e8 566 // Serve from the cache so we don't have to regenerate.
46a710e9 567 return $moduleoptions[$key];
27bad0a6
SH
568 }
569
4ef08298 570 if (has_capability('moodle/blog:view', $sitecontext)) {
dc9fa9cb
RT
571 // Save correct module name for later usage.
572 $modulename = get_string('modulename', $module->modname);
4eaf120a 573
27bad0a6
SH
574 // We can view!
575 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
2b6e53e8 576 // View all entries about this module.
27bad0a6 577 $a = new stdClass;
dc9fa9cb 578 $a->type = $modulename;
27bad0a6
SH
579 $options['moduleview'] = array(
580 'string' => get_string('viewallmodentries', 'blog', $a),
0e32a565 581 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id))
27bad0a6
SH
582 );
583 }
2b6e53e8 584 // View MY entries about this module.
27bad0a6 585 $options['moduleviewmine'] = array(
dc9fa9cb 586 'string' => get_string('viewmyentriesaboutmodule', 'blog', $modulename),
0e32a565 587 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id, 'userid' => $USER->id))
27bad0a6
SH
588 );
589 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
2b6e53e8 590 // View the given users entries about this module.
27bad0a6 591 $a = new stdClass;
dc9fa9cb 592 $a->mod = $modulename;
27bad0a6
SH
593 $a->user = fullname($user);
594 $options['moduleviewuser'] = array(
595 'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a),
0e32a565 596 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id, 'userid' => $user->id))
27bad0a6
SH
597 );
598 }
599 }
600
4ef08298 601 if (has_capability('moodle/blog:create', $sitecontext)) {
2b6e53e8 602 // The user can blog about this module.
27bad0a6 603 $options['moduleadd'] = array(
dc9fa9cb 604 'string' => get_string('blogaboutthismodule', 'blog', $modulename),
0e32a565 605 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'modid' => $module->id))
27bad0a6
SH
606 );
607 }
2b6e53e8 608 // Cache the options.
27bad0a6 609 $moduleoptions[$key] = $options;
2b6e53e8 610 // Return the options.
27bad0a6
SH
611 return $options;
612}
613
cae83708 614/**
615 * This function encapsulates all the logic behind the complex
616 * navigation, titles and headings of the blog listing page, depending
1c7b8b93
NC
617 * on URL params. It looks at URL params and at the current context level.
618 * It builds and returns an array containing:
619 *
620 * 1. heading: The heading displayed above the blog entries
621 * 2. stradd: The text to be used as the "Add entry" link
622 * 3. strview: The text to be used as the "View entries" link
623 * 4. url: The moodle_url object used as the base for add and view links
624 * 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block
cae83708 625 *
c5dc10ee 626 * All other variables are set directly in $PAGE
cae83708 627 *
628 * It uses the current URL to build these variables.
629 * A number of mutually exclusive use cases are used to structure this function.
630 *
631 * @return array
632 */
451f1e38 633function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=null) {
cae83708 634 global $CFG, $PAGE, $DB, $USER;
635
9366362a 636 $id = optional_param('id', null, PARAM_INT);
cae83708 637 $tag = optional_param('tag', null, PARAM_NOTAGS);
451f1e38
AD
638 $tagid = optional_param('tagid', $tagid, PARAM_INT);
639 $userid = optional_param('userid', $userid, PARAM_INT);
cae83708 640 $modid = optional_param('modid', null, PARAM_INT);
641 $entryid = optional_param('entryid', null, PARAM_INT);
451f1e38
AD
642 $groupid = optional_param('groupid', $groupid, PARAM_INT);
643 $courseid = optional_param('courseid', $courseid, PARAM_INT);
cae83708 644 $search = optional_param('search', null, PARAM_RAW);
645 $action = optional_param('action', null, PARAM_ALPHA);
646 $confirm = optional_param('confirm', false, PARAM_BOOL);
647
2b6e53e8 648 // Ignore userid when action == add.
1c7b8b93
NC
649 if ($action == 'add' && $userid) {
650 unset($userid);
651 $PAGE->url->remove_params(array('userid'));
652 } else if ($action == 'add' && $entryid) {
653 unset($entryid);
654 $PAGE->url->remove_params(array('entryid'));
655 }
656
657 $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
cae83708 658
a6855934 659 $blogurl = new moodle_url('/blog/index.php');
1c7b8b93 660
1c7b8b93
NC
661 $headers['stradd'] = get_string('addnewentry', 'blog');
662 $headers['strview'] = null;
cae83708 663
1c7b8b93 664 $site = $DB->get_record('course', array('id' => SITEID));
41b38360 665 $sitecontext = context_system::instance();
2b6e53e8 666 // Common Lang strings.
cae83708 667 $strparticipants = get_string("participants");
668 $strblogentries = get_string("blogentries", 'blog');
669
2b6e53e8 670 // Prepare record objects as needed.
cae83708 671 if (!empty($courseid)) {
1c7b8b93 672 $headers['filters']['course'] = $courseid;
cae83708 673 $course = $DB->get_record('course', array('id' => $courseid));
674 }
e96f2a77 675
cae83708 676 if (!empty($userid)) {
1c7b8b93 677 $headers['filters']['user'] = $userid;
cae83708 678 $user = $DB->get_record('user', array('id' => $userid));
516194d0 679 }
bbbf2d40 680
2b6e53e8 681 if (!empty($groupid)) { // The groupid always overrides courseid.
1c7b8b93 682 $headers['filters']['group'] = $groupid;
cae83708 683 $group = $DB->get_record('groups', array('id' => $groupid));
684 $course = $DB->get_record('course', array('id' => $group->courseid));
685 }
bbbf2d40 686
897aa80c 687 $PAGE->set_pagelayout('standard');
27bad0a6 688
2b6e53e8 689 // The modid always overrides courseid, so the $course object may be reset here.
1b30a9fa
DM
690 if (!empty($modid) && $CFG->useblogassociations) {
691
1c7b8b93 692 $headers['filters']['module'] = $modid;
2b6e53e8 693 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case.
0e32a565 694 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
1c7b8b93 695 $course = $DB->get_record('course', array('id' => $courseid));
cae83708 696 $cm = $DB->get_record('course_modules', array('id' => $modid));
697 $cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module));
698 $cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance));
e463f508 699 $a = new stdClass();
1c7b8b93 700 $a->type = get_string('modulename', $cm->modname);
c5dc10ee 701 $PAGE->set_cm($cm, $course);
1c7b8b93
NC
702 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
703 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 704 }
b73d1ca4 705
1c7b8b93 706 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
2b6e53e8 707 // Note: if action is set to 'add' or 'edit', we do this at the end.
1c7b8b93
NC
708 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
709 $PAGE->navbar->add($strblogentries, $blogurl);
4e1f6047
DW
710 $PAGE->set_title($site->fullname);
711 $PAGE->set_heading($site->fullname);
712 $headers['heading'] = get_string('siteblogheading', 'blog');
4a8b890a 713 }
c2ee4e87 714
2b6e53e8 715 // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information.
cae83708 716 if (!empty($entryid)) {
1c7b8b93
NC
717 $headers['filters']['entry'] = $entryid;
718 $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id';
cae83708 719 $user = $DB->get_record_sql($sql, array($entryid));
1c7b8b93 720 $entry = $DB->get_record('post', array('id' => $entryid));
c2ee4e87 721
1c7b8b93 722 $blogurl->param('userid', $user->id);
c2ee4e87 723
cae83708 724 if (!empty($course)) {
9366362a 725 $mycourseid = $course->id;
1c7b8b93 726 $blogurl->param('courseid', $mycourseid);
c2ee4e87 727 } else {
9366362a 728 $mycourseid = $site->id;
c2ee4e87 729 }
41b38360 730 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
e14de6f9 731
1c7b8b93
NC
732 $PAGE->navbar->add($strblogentries, $blogurl);
733
734 $blogurl->remove_params('userid');
735 $PAGE->navbar->add($entry->subject, $blogurl);
8ebbb06a
SH
736 $PAGE->set_title("$shortname: " . fullname($user) . ": $entry->subject");
737 $PAGE->set_heading("$shortname: " . fullname($user) . ": $entry->subject");
cae83708 738 $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user));
739
2b6e53e8 740 // We ignore tag and search params.
1c7b8b93
NC
741 if (empty($action) || !$CFG->useblogassociations) {
742 $headers['url'] = $blogurl;
9366362a 743 return $headers;
744 }
c2ee4e87 745 }
240075cd 746
1c7b8b93 747 if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
2b6e53e8
AD
748 // Case 3: A user's blog entries.
749
41b38360 750 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
1c7b8b93 751 $blogurl->param('userid', $userid);
8ebbb06a
SH
752 $PAGE->set_title("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
753 $PAGE->set_heading("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
cae83708 754 $headers['heading'] = get_string('userblog', 'blog', fullname($user));
27bad0a6 755 $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user));
1c7b8b93 756
2b6e53e8
AD
757 } else if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) {
758 // Case 4: No blog associations, no userid.
cae83708 759
4e1f6047
DW
760 $PAGE->set_title($site->fullname);
761 $PAGE->set_heading($site->fullname);
762 $headers['heading'] = get_string('siteblogheading', 'blog');
2b6e53e8
AD
763 } else if (!empty($userid) && !empty($modid) && empty($entryid)) {
764 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored).
cae83708 765
41b38360 766 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
1c7b8b93
NC
767 $blogurl->param('userid', $userid);
768 $blogurl->param('modid', $modid);
cae83708 769
2b6e53e8 770 // Course module navigation is handled by build_navigation as the second param.
cae83708 771 $headers['cm'] = $cm;
c5dc10ee 772 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 773 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 774
8ebbb06a
SH
775 $PAGE->set_title("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
776 $PAGE->set_heading("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
cae83708 777
e463f508 778 $a = new stdClass();
cae83708 779 $a->user = fullname($user);
780 $a->mod = $cm->name;
1c7b8b93 781 $a->type = get_string('modulename', $cm->modname);
cae83708 782 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
1c7b8b93
NC
783 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
784 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
2b6e53e8
AD
785 } else if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
786 // Case 6: Blog entries associated with a course by a specific user.
cae83708 787
1c7b8b93
NC
788 $blogurl->param('userid', $userid);
789 $blogurl->param('courseid', $courseid);
2c27b6ae 790
4e1f6047
DW
791 $PAGE->set_title($course->fullname);
792 $PAGE->set_heading($course->fullname);
cae83708 793
e463f508 794 $a = new stdClass();
cae83708 795 $a->user = fullname($user);
41b38360 796 $a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
1c7b8b93 797 $a->type = get_string('course');
cae83708 798 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
1c7b8b93
NC
799 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
800 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
801
2b6e53e8 802 // Remove the userid from the URL to inform the blog_menu block correctly.
1c7b8b93 803 $blogurl->remove_params(array('userid'));
2b6e53e8
AD
804 } else if (!empty($groupid) && empty($modid) && empty($entryid)) {
805 // Case 7: Blog entries by members of a group, associated with that group's course.
cae83708 806
1c7b8b93 807 $blogurl->param('courseid', $course->id);
e14de6f9 808
1c7b8b93
NC
809 $PAGE->navbar->add($strblogentries, $blogurl);
810 $blogurl->remove_params(array('courseid'));
811 $blogurl->param('groupid', $groupid);
812 $PAGE->navbar->add($group->name, $blogurl);
cae83708 813
4e1f6047
DW
814 $PAGE->set_title($course->fullname);
815 $PAGE->set_heading($course->fullname);
cae83708 816
e463f508 817 $a = new stdClass();
cae83708 818 $a->group = $group->name;
41b38360 819 $a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
1c7b8b93 820 $a->type = get_string('course');
cae83708 821 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
1c7b8b93
NC
822 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
823 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
2b6e53e8
AD
824 } else if (!empty($groupid) && !empty($modid) && empty($entryid)) {
825 // Case 8: Blog entries by members of a group, associated with an activity in that course.
cae83708 826
cae83708 827 $headers['cm'] = $cm;
1c7b8b93
NC
828 $blogurl->param('modid', $modid);
829 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 830
1c7b8b93
NC
831 $blogurl->param('groupid', $groupid);
832 $PAGE->navbar->add($group->name, $blogurl);
cae83708 833
4e1f6047
DW
834 $PAGE->set_title($course->fullname);
835 $PAGE->set_heading($course->fullname);
cae83708 836
e463f508 837 $a = new stdClass();
cae83708 838 $a->group = $group->name;
839 $a->mod = $cm->name;
1c7b8b93 840 $a->type = get_string('modulename', $cm->modname);
cae83708 841 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
1c7b8b93
NC
842 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
843 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 844
2b6e53e8
AD
845 } else if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
846 // Case 9: All blog entries associated with an activity.
cae83708 847
c5dc10ee 848 $PAGE->set_cm($cm, $course);
1c7b8b93
NC
849 $blogurl->param('modid', $modid);
850 $PAGE->navbar->add($strblogentries, $blogurl);
4e1f6047
DW
851 $PAGE->set_title($course->fullname);
852 $PAGE->set_heading($course->fullname);
cae83708 853 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
e463f508 854 $a = new stdClass();
1c7b8b93
NC
855 $a->type = get_string('modulename', $cm->modname);
856 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
857 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
2b6e53e8
AD
858 } else if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
859 // Case 10: All blog entries associated with a course.
cae83708 860
1c7b8b93
NC
861 $blogurl->param('courseid', $courseid);
862 $PAGE->navbar->add($strblogentries, $blogurl);
4e1f6047
DW
863 $PAGE->set_title($course->fullname);
864 $PAGE->set_heading($course->fullname);
e463f508 865 $a = new stdClass();
1c7b8b93 866 $a->type = get_string('course');
2b6e53e8
AD
867 $headers['heading'] = get_string('blogentriesabout',
868 'blog',
869 format_string($course->fullname,
870 true,
871 array('context' => context_course::instance($course->id))));
1c7b8b93
NC
872 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
873 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
874 $blogurl->remove_params(array('userid'));
875 }
876
877 if (!in_array($action, array('edit', 'add'))) {
2b6e53e8 878 // Append Tag info.
1c7b8b93
NC
879 if (!empty($tagid)) {
880 $headers['filters']['tag'] = $tagid;
881 $blogurl->param('tagid', $tagid);
0e32a565 882 $tagrec = $DB->get_record('tag', array('id' => $tagid));
1c7b8b93 883 $PAGE->navbar->add($tagrec->name, $blogurl);
2b6e53e8 884 } else if (!empty($tag)) {
1af9063e
AA
885 if ($tagrec = $DB->get_record('tag', array('name' => $tag))) {
886 $tagid = $tagrec->id;
887 $headers['filters']['tag'] = $tagid;
888 $blogurl->param('tag', $tag);
889 $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl);
890 }
1c7b8b93 891 }
240075cd 892
2b6e53e8 893 // Append Search info.
99fbcc7b 894 if (!empty($search) && has_capability('moodle/blog:search', $sitecontext)) {
1c7b8b93
NC
895 $headers['filters']['search'] = $search;
896 $blogurl->param('search', $search);
897 $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
898 }
ee00eb8c 899 }
900
2b6e53e8 901 // Append edit mode info.
cae83708 902 if (!empty($action) && $action == 'add') {
f36b47ef 903
cae83708 904 } else if (!empty($action) && $action == 'edit') {
c5dc10ee 905 $PAGE->navbar->add(get_string('editentry', 'blog'));
240075cd 906 }
ee00eb8c 907
1c7b8b93
NC
908 if (empty($headers['url'])) {
909 $headers['url'] = $blogurl;
910 }
cae83708 911 return $headers;
912}
23677261 913
1c7b8b93
NC
914/**
915 * Shortcut function for getting a count of blog entries associated with a course or a module
916 * @param int $courseid The ID of the course
917 * @param int $cmid The ID of the course_modules
918 * @return string The number of associated entries
919 */
920function blog_get_associated_count($courseid, $cmid=null) {
921 global $DB;
41b38360 922 $context = context_course::instance($courseid);
1c7b8b93 923 if ($cmid) {
41b38360 924 $context = context_module::instance($cmid);
1c7b8b93
NC
925 }
926 return $DB->count_records('blog_association', array('contextid' => $context->id));
593270c6 927}
c1951ea9
DC
928
929/**
930 * Running addtional permission check on plugin, for example, plugins
931 * may have switch to turn on/off comments option, this callback will
932 * affect UI display, not like pluginname_comment_validate only throw
933 * exceptions.
9a909b1a
RT
934 * blog_comment_validate will be called before viewing/adding/deleting
935 * comment, so don't repeat checks.
c1951ea9
DC
936 * Capability check has been done in comment->check_permissions(), we
937 * don't need to do it again here.
938 *
35453657
DC
939 * @package core_blog
940 * @category comment
941 *
0e32a565 942 * @param stdClass $commentparam {
c1951ea9
DC
943 * context => context the context object
944 * courseid => int course id
945 * cm => stdClass course module object
946 * commentarea => string comment area
947 * itemid => int itemid
948 * }
949 * @return array
950 */
0e32a565 951function blog_comment_permissions($commentparam) {
9a909b1a
RT
952 global $DB;
953
954 // If blog is public and current user is guest, then don't let him post comments.
0e32a565 955 $blogentry = $DB->get_record('post', array('id' => $commentparam->itemid), 'publishstate', MUST_EXIST);
9a909b1a
RT
956
957 if ($blogentry->publishstate != 'public') {
958 if (!isloggedin() || isguestuser()) {
959 return array('post' => false, 'view' => true);
960 }
961 }
962 return array('post' => true, 'view' => true);
c1951ea9
DC
963}
964
965/**
966 * Validate comment parameter before perform other comments actions
967 *
35453657
DC
968 * @package core_blog
969 * @category comment
970 *
c1951ea9
DC
971 * @param stdClass $comment {
972 * context => context the context object
973 * courseid => int course id
974 * cm => stdClass course module object
975 * commentarea => string comment area
976 * itemid => int itemid
977 * }
978 * @return boolean
979 */
0e32a565 980function blog_comment_validate($commentparam) {
9a909b1a
RT
981 global $CFG, $DB, $USER;
982
983 // Check if blogs are enabled user can comment.
984 if (empty($CFG->enableblogs) || empty($CFG->blogusecomments)) {
985 throw new comment_exception('nopermissiontocomment');
c1951ea9 986 }
9a909b1a 987
89f5e430 988 // Validate comment area.
0e32a565 989 if ($commentparam->commentarea != 'format_blog') {
c1951ea9
DC
990 throw new comment_exception('invalidcommentarea');
991 }
9a909b1a 992
0e32a565 993 $blogentry = $DB->get_record('post', array('id' => $commentparam->itemid), '*', MUST_EXIST);
9a909b1a 994
89f5e430 995 // Validation for comment deletion.
0e32a565
AD
996 if (!empty($commentparam->commentid)) {
997 if ($record = $DB->get_record('comments', array('id' => $commentparam->commentid))) {
c1951ea9
DC
998 if ($record->commentarea != 'format_blog') {
999 throw new comment_exception('invalidcommentarea');
1000 }
0e32a565 1001 if ($record->contextid != $commentparam->context->id) {
c1951ea9
DC
1002 throw new comment_exception('invalidcontext');
1003 }
0e32a565 1004 if ($record->itemid != $commentparam->itemid) {
c1951ea9
DC
1005 throw new comment_exception('invalidcommentitemid');
1006 }
1007 } else {
1008 throw new comment_exception('invalidcommentid');
1009 }
1010 }
9a909b1a
RT
1011
1012 // Validate if user has blog view permission.
1013 $sitecontext = context_system::instance();
1014 return has_capability('moodle/blog:view', $sitecontext) &&
1015 blog_user_can_view_user_entry($blogentry->userid, $blogentry);
c1951ea9 1016}
b1627a92
DC
1017
1018/**
1019 * Return a list of page types
1020 * @param string $pagetype current page type
1021 * @param stdClass $parentcontext Block's parent context
1022 * @param stdClass $currentcontext Current context of block
1023 */
b38e2e28 1024function blog_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92 1025 return array(
0e32a565
AD
1026 '*' => get_string('page-x', 'pagetype'),
1027 'blog-*' => get_string('page-blog-x', 'blog'),
1028 'blog-index' => get_string('page-blog-index', 'blog'),
1029 'blog-edit' => get_string('page-blog-edit', 'blog')
b1627a92
DC
1030 );
1031}
b19cc4ef
AA
1032
1033/**
1034 * Add nodes to myprofile page.
1035 *
1036 * @param \core_user\output\myprofile\tree $tree Tree object
1037 * @param stdClass $user user object
1038 * @param bool $iscurrentuser
1039 * @param stdClass $course Course object
1040 *
1041 * @return bool
1042 */
1043function core_blog_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
0c713162 1044 global $CFG;
b19cc4ef
AA
1045 if (!blog_is_enabled_for_user() || isguestuser($user)) {
1046 // The guest user cannot post, so it is not possible to view any posts.
1047 // Also blogs might be disabled.
1048 // May as well just bail aggressively here.
1049 return true;
1050 }
c5954f64 1051 if (!blog_user_can_view_user_entry($user->id)) {
0c713162
AG
1052 return true;
1053 }
b19cc4ef 1054 $url = new moodle_url("/blog/index.php", array('userid' => $user->id));
6e918b64
AG
1055 if (!empty($course)) {
1056 $url->param('courseid', $course->id);
1057 }
b19cc4ef 1058 if ($iscurrentuser) {
4887d152 1059 $title = get_string('blogentries', 'core_blog');
b19cc4ef 1060 } else {
e29866b5 1061 $title = get_string('myprofileuserblogs', 'core_blog');
b19cc4ef
AA
1062 }
1063 $blognode = new core_user\output\myprofile\node('miscellaneous', 'blogs', $title, null, $url);
1064 $tree->add_node($blognode);
1065 return true;
c1f97c77 1066}
abea2c5d
MG
1067
1068/**
1069 * Returns posts tagged with a specified tag.
1070 *
1071 * @param core_tag_tag $tag
1072 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
1073 * are displayed on the page and the per-page limit may be bigger
1074 * @param int $fromctx context id where the link was displayed, may be used by callbacks
1075 * to display items in the same context first
1076 * @param int $ctx context id where to search for records
1077 * @param bool $rec search in subcontexts as well
1078 * @param int $page 0-based number of page being displayed
1079 * @return \core_tag\output\tagindex
1080 */
1081function blog_get_tagged_posts($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = true, $page = 0) {
1082 global $CFG, $OUTPUT;
1083 require_once($CFG->dirroot.'/user/lib.php');
1084
1085 $systemcontext = context_system::instance();
1086 $perpage = $exclusivemode ? 20 : 5;
1087 $context = $ctx ? context::instance_by_id($ctx) : context_system::instance();
1088
1089 $content = '';
1090 if (empty($CFG->enableblogs) || !has_capability('moodle/blog:view', $systemcontext)) {
1091 // Blogs are not enabled or are not visible to the current user.
1092 $totalpages = 0;
1093 } else if ($context->contextlevel != CONTEXT_SYSTEM && empty($CFG->useblogassociations)) {
1094 // No blog entries can be associated to the non-system context.
1095 $totalpages = 0;
1096 } else if (!$rec && $context->contextlevel != CONTEXT_COURSE && $context->contextlevel != CONTEXT_MODULE) {
1097 // No blog entries can be associated with category or block context.
1098 $totalpages = 0;
1099 } else {
1100 require_once($CFG->dirroot.'/blog/locallib.php');
1101
1102 $filters = array('tag' => $tag->id);
1103 if ($rec) {
1104 if ($context->contextlevel != CONTEXT_SYSTEM) {
1105 $filters['context'] = $context->id;
1106 }
1107 } else if ($context->contextlevel == CONTEXT_COURSE) {
1108 $filters['course'] = $context->instanceid;
1109 } else if ($context->contextlevel == CONTEXT_MODULE) {
1110 $filters['module'] = $context->instanceid;
1111 }
1112 $bloglisting = new blog_listing($filters);
1113 $blogs = $bloglisting->get_entries($page * $perpage, $perpage);
1114 $totalcount = $bloglisting->count_entries();
1115 $totalpages = ceil($totalcount / $perpage);
1116 if (!empty($blogs)) {
1117 $tagfeed = new core_tag\output\tagfeed();
1118 foreach ($blogs as $blog) {
1119 $user = fullclone($blog);
1120 $user->id = $blog->userid;
1121 $user->deleted = 0;
1122 $img = $OUTPUT->user_picture($user, array('size' => 35));
1123 $subject = format_string($blog->subject);
1124
1125 if ($blog->publishstate == 'draft') {
1126 $class = 'dimmed';
1127 } else {
1128 $class = '';
1129 }
1130
1131 $url = new moodle_url('/blog/index.php', array('entryid' => $blog->id));
1132 $subject = html_writer::link($url, $subject, array('class' => $class));
1133
1134 $fullname = fullname($user);
1135 if (user_can_view_profile($user)) {
1136 $profilelink = new moodle_url('/user/view.php', array('id' => $blog->userid));
1137 $fullname = html_writer::link($profilelink, $fullname);
1138 }
1139 $details = $fullname . ', ' . userdate($blog->created);
1140
1141 $tagfeed->add($img, $subject, $details);
1142 }
1143
1144 $items = $tagfeed->export_for_template($OUTPUT);
1145 $content = $OUTPUT->render_from_template('core_tag/tagfeed', $items);
1146
1147 $urlparams = array('tagid' => $tag->id);
1148 if ($context->contextlevel == CONTEXT_COURSE) {
1149 $urlparams['courseid'] = $context->instanceid;
1150 } else if ($context->contextlevel == CONTEXT_MODULE) {
1151 $urlparams['modid'] = $context->instanceid;
1152 }
1153 $allblogsurl = new moodle_url('/blog/index.php', $urlparams);
1154
1155 $rv = new core_tag\output\tagindex($tag, 'core', 'post',
1156 $content,
1157 $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
1158 $rv->exclusiveurl = $allblogsurl;
1159 return $rv;
1160 }
1161 }
1162
1163 $rv = new core_tag\output\tagindex($tag, 'core', 'post',
1164 $content,
1165 $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
1166 $rv->exclusiveurl = null;
1167 return $rv;
1168}