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