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