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