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