blog MDL-22161 Fixed minor regression
[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
18
19/**
20 * Core global functions for Blog.
21 *
22 * @package moodlecore
23 * @subpackage blog
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28/**
29 * Library of functions and constants for blog
30 */
31require_once($CFG->dirroot .'/blog/rsslib.php');
32require_once($CFG->dirroot.'/tag/lib.php');
33
cae83708 34/**
35 * User can edit a blog entry if this is their own blog entry and they have
36 * the capability moodle/blog:create, or if they have the capability
37 * moodle/blog:manageentries.
38 *
39 * This also applies to deleting of entries.
40 */
1c7b8b93
NC
41function blog_user_can_edit_entry($blogentry) {
42 global $USER;
6524adcf 43
cae83708 44 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
4a173181 45
cae83708 46 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
47 return true; // can edit any blog entry
48 }
c2ee4e87 49
1c7b8b93 50 if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) {
cae83708 51 return true; // can edit own when having blog:create capability
52 }
4a173181 53
cae83708 54 return false;
55}
d02240f3 56
3bfcfdca 57
cae83708 58/**
59 * Checks to see if a user can view the blogs of another user.
60 * Only blog level is checked here, the capabilities are enforced
61 * in blog/index.php
62 */
1c7b8b93 63function blog_user_can_view_user_entry($targetuserid, $blogentry=null) {
cae83708 64 global $CFG, $USER, $DB;
c2ee4e87 65
cae83708 66 if (empty($CFG->bloglevel)) {
67 return false; // blog system disabled
68 }
69
4f0c2d00 70 if (isloggdin() && $USER->id == $targetuserid) {
cae83708 71 return true; // can view own entries in any case
72 }
73
74 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
75 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
76 return true; // can manage all entries
77 }
c2ee4e87 78
cae83708 79 // coming for 1 entry, make sure it's not a draft
1c7b8b93 80 if ($blogentry && $blogentry->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
cae83708 81 return false; // can not view draft of others
82 }
83
84 // coming for 1 entry, make sure user is logged in, if not a public blog
1c7b8b93 85 if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) {
cae83708 86 return false;
87 }
d02240f3 88
cae83708 89 switch ($CFG->bloglevel) {
90 case BLOG_GLOBAL_LEVEL:
91 return true;
92 break;
c2ee4e87 93
cae83708 94 case BLOG_SITE_LEVEL:
4f0c2d00 95 if (isloggedin()) { // not logged in viewers forbidden
cae83708 96 return true;
c2ee4e87 97 }
cae83708 98 return false;
99 break;
d02240f3 100
cae83708 101 case BLOG_USER_LEVEL:
102 default:
103 $personalcontext = get_context_instance(CONTEXT_USER, $targetuserid);
104 return has_capability('moodle/user:readuserblogs', $personalcontext);
105 break;
4a173181 106
cae83708 107 }
108}
109
110/**
111 * remove all associations for the blog entries of a particular user
112 * @param int userid - id of user whose blog associations will be deleted
113 */
114function blog_remove_associations_for_user($userid) {
1c7b8b93
NC
115 global $DB;
116 $blogentries = blog_fetch_entries(array('user' => $userid), 'lasmodified DESC');
117 foreach ($blogentries as $entry) {
118 if (blog_user_can_edit_entry($entry)) {
119 blog_remove_associations_for_entry($entry->id);
120 }
b0e90a0c 121 }
cae83708 122}
123
124/**
1c7b8b93
NC
125 * remove all associations for the blog entries of a particular course
126 * @param int courseid - id of user whose blog associations will be deleted
cae83708 127 */
1c7b8b93
NC
128function blog_remove_associations_for_course($courseid) {
129 global $DB;
130 $context = get_context_instance(CONTEXT_COURSE, $courseid);
131 $DB->delete_records('blog_association', array('contextid' => $context->id));
cae83708 132}
133
cae83708 134/**
135 * Given a record in the {blog_external} table, checks the blog's URL
136 * for new entries not yet copied into Moodle.
137 *
1c7b8b93
NC
138 * @param object $externalblog
139 * @return boolean False if the Feed is invalid
cae83708 140 */
1c7b8b93 141function blog_sync_external_entries($externalblog) {
cae83708 142 global $CFG, $DB;
e14de6f9 143 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
4a173181 144
1c7b8b93
NC
145 $rssfile = new moodle_simplepie_file($externalblog->url);
146 $filetest = new SimplePie_Locator($rssfile);
147
148 if (!$filetest->is_feed($rssfile)) {
149 $externalblog->failedlastsync = 1;
150 $DB->update_record('blog_external', $externalblog);
151 return false;
152 } else if ($externalblog->failedlastsync) {
153 $externalblog->failedlastsync = 0;
154 $DB->update_record('blog_external', $externalblog);
c2ee4e87 155 }
4a173181 156
1c7b8b93
NC
157 // Delete all blog entries associated with this external blog
158 blog_delete_external_entries($externalblog);
159
160 $rss = new moodle_simplepie($externalblog->url);
c2ee4e87 161
e14de6f9 162 if (empty($rss->data)) {
cae83708 163 return null;
164 }
b13ee30e 165
e14de6f9 166 foreach ($rss->get_items() as $entry) {
1c7b8b93
NC
167 // If filtertags are defined, use them to filter the entries by RSS category
168 if (!empty($externalblog->filtertags)) {
169 $containsfiltertag = false;
170 $categories = $entry->get_categories();
171 $filtertags = explode(',', $externalblog->filtertags);
172 $filtertags = array_map('trim', $filtertags);
173 $filtertags = array_map('strtolower', $filtertags);
174
175 foreach ($categories as $category) {
176 if (in_array(trim(strtolower($category->term)), $filtertags)) {
177 $containsfiltertag = true;
178 }
179 }
180
181 if (!$containsfiltertag) {
182 continue;
c2ee4e87 183 }
184 }
1c7b8b93
NC
185
186 $newentry = new object();
187 $newentry->userid = $externalblog->userid;
188 $newentry->module = 'blog_external';
189 $newentry->content = $externalblog->id;
190 $newentry->uniquehash = $entry->get_permalink();
191 $newentry->publishstate = 'site';
192 $newentry->format = FORMAT_HTML;
193 $newentry->subject = $entry->get_title();
194 $newentry->summary = $entry->get_description();
195 $newentry->created = $entry->get_date('U');
196 $newentry->lastmodified = $entry->get_date('U');
197
198 $id = $DB->insert_record('post', $newentry);
199
200 // Set tags
201 if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) {
202 tag_set('post', $id, $tags);
203 }
cae83708 204 }
c2ee4e87 205
1c7b8b93
NC
206 $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => mktime()));
207}
208
209/**
210 * Given an external blog object, deletes all related blog entries from the post table.
211 * NOTE: The external blog's id is saved as post.content, a field that is not oterhwise used by blog entries.
212 * @param object $externablog
213 */
214function blog_delete_external_entries($externalblog) {
215 global $DB;
216 require_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM));
217 $DB->delete_records('post', array('content' => $externalblog->id, 'module' => 'blog_external'));
cae83708 218}
d02240f3 219
cae83708 220/**
221 * Returns a URL based on the context of the current page.
222 * This URL points to blog/index.php and includes filter parameters appropriate for the current page.
223 *
224 * @param stdclass $context
225 * @return string
226 */
227function blog_get_context_url($context=null) {
228 global $CFG;
c2ee4e87 229
a6855934 230 $viewblogentriesurl = new moodle_url('/blog/index.php');
c2ee4e87 231
cae83708 232 if (empty($context)) {
233 global $PAGE;
5515b536 234 $context = $PAGE->context;
cae83708 235 }
c2ee4e87 236
cae83708 237 // Change contextlevel to SYSTEM if viewing the site course
238 if ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID) {
239 $context->contextlevel = CONTEXT_SYSTEM;
240 }
c2ee4e87 241
cae83708 242 $filterparam = '';
243 $strlevel = '';
c2ee4e87 244
cae83708 245 switch ($context->contextlevel) {
246 case CONTEXT_SYSTEM:
247 case CONTEXT_BLOCK:
248 case CONTEXT_COURSECAT:
c2ee4e87 249 break;
cae83708 250 case CONTEXT_COURSE:
251 $filterparam = 'courseid';
252 $strlevel = get_string('course');
c2ee4e87 253 break;
cae83708 254 case CONTEXT_MODULE:
255 $filterparam = 'modid';
256 $strlevel = print_context_name($context);
c2ee4e87 257 break;
cae83708 258 case CONTEXT_USER:
259 $filterparam = 'userid';
260 $strlevel = get_string('user');
c2ee4e87 261 break;
516194d0 262 }
263
cae83708 264 if (!empty($filterparam)) {
b0e90a0c 265 $viewblogentriesurl->param($filterparam, $context->instanceid);
cae83708 266 }
bbbf2d40 267
b0e90a0c 268 return $viewblogentriesurl;
cae83708 269}
270
27bad0a6
SH
271/**
272 * This function checks that blogs are enabled, and that the user is logged in an
273 * is not the guest user.
274 * @return bool
275 */
276function blog_is_enabled_for_user() {
277 global $CFG;
278 return (!empty($CFG->bloglevel) && $CFG->bloglevel <= BLOG_GLOBAL_LEVEL && isloggedin() && !isguestuser());
279}
280
281/**
282 * This function gets all of the options available for the current user in respect
283 * to blogs.
284 *
285 * It loads the following if applicable:
286 * - Module options {@see blog_get_options_for_module}
287 * - Course options {@see blog_get_options_for_course}
288 * - User specific options {@see blog_get_options_for_user}
289 * - General options (BLOG_LEVEL_GLOBAL)
290 *
291 * @param moodle_page $page The page to load for (normally $PAGE)
292 * @param stdClass $userid Load for a specific user
293 * @return array An array of options organised by type.
294 */
295function blog_get_all_options(moodle_page $page, stdClass $userid = null) {
296 global $CFG, $DB, $USER;
297
298 $options = array();
299
300 // If blogs are enabled and the user is logged in and not a guest
301 if (blog_is_enabled_for_user()) {
302 // If the context is the user then assume we want to load for the users context
303 if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) {
304 $userid = $page->context->instanceid;
305 }
306 // Check the userid var
307 if (!is_null($userid) && $userid!==$USER->id) {
308 // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't!
309 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
310 } else {
311 $user = null;
312 }
313
314 if ($CFG->useblogassociations && $page->cm !== null) {
315 // Load for the module associated with the page
316 $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user);
317 } else if ($CFG->useblogassociations && $page->course->id != SITEID) {
318 // Load the options for the course associated with the page
319 $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user);
320 }
321
322 // Get the options for the user
323 if ($user !== null) {
324 // Load for the requested user
325 $options[CONTEXT_USER+1] = blog_get_options_for_user($user);
326 }
327 // Load for the current user
328 $options[CONTEXT_USER] = blog_get_options_for_user();
329 }
330
331 // If blog level is global then display a link to view all site entries
332 if (!empty($CFG->bloglevel) && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL && has_capability('moodle/blog:view', get_context_instance(CONTEXT_SYSTEM))) {
333 $options[CONTEXT_SYSTEM] = array('viewsite' => array(
334 'string' => get_string('viewsiteentries', 'blog'),
335 'link' => new moodle_url('/blog/index.php')
336 ));
337 }
338
339 // Return the options
340 return $options;
341}
342
343/**
344 * Get all of the blog options that relate to the passed user.
345 *
346 * If no user is passed the current user is assumed.
347 *
348 * @staticvar array $useroptions Cache so we don't have to regenerate multiple times
349 * @param stdClass $user
350 * @return array The array of options for the requested user
351 */
352function blog_get_options_for_user(stdClass $user=null) {
353 global $CFG, $USER;
354 // Cache
355 static $useroptions = array();
356
357 $options = array();
358 // Blogs must be enabled and the user must be logged in
359 if (!blog_is_enabled_for_user()) {
360 return $options;
361 }
362
363 // Sort out the user var
364 if ($user === null || $user->id == $USER->id) {
365 $user = $USER;
366 $iscurrentuser = true;
367 } else {
368 $iscurrentuser = false;
369 }
370
371 // If we've already generated serve from the cache
372 if (array_key_exists($user->id, $useroptions)) {
373 return $useroptions[$user->id];
374 }
375
376 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
377 $canview = has_capability('moodle/blog:view', $sitecontext);
378
379 if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
380 // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL
381 $options['userentries'] = array(
382 'string' => get_string('viewuserentries', 'blog', fullname($user)),
383 'link' => new moodle_url('/blog/index.php', array('userid'=>$user->id))
384 );
385 } else {
386 // It's the current user
387 if ($canview) {
388 // We can view our own blogs .... BIG surprise
389 $options['view'] = array(
390 'string' => get_string('viewallmyentries', 'blog'),
391 'link' => new moodle_url('/blog/index.php', array('userid'=>$USER->id))
392 );
393 }
394 if (has_capability('moodle/blog:create', $sitecontext)) {
395 // We can add to our own blog
396 $options['add'] = array(
397 'string' => get_string('addnewentry', 'blog'),
398 'link' => new moodle_url('/blog/edit.php', array('action'=>'add'))
399 );
400 }
401 }
402 // Cache the options
403 $useroptions[$user->id] = $options;
404 // Return the options
405 return $options;
406}
407
408/**
409 * Get the blog options that relate to the given course for the given user.
410 *
411 * @staticvar array $courseoptions A cache so we can save regenerating multiple times
412 * @param stdClass $course The course to load options for
413 * @param stdClass $user The user to load options for null == current user
414 * @return array The array of options
415 */
416function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
417 global $CFG, $USER;
418 // Cache
419 static $courseoptions = array();
420
421 $options = array();
422
423 // User must be logged in and blogs must be enabled
424 if (!blog_is_enabled_for_user()) {
425 return $options;
426 }
427
428 // Check that the user can associate with the course
429 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
430 if (!has_capability('moodle/blog:associatecourse', $sitecontext)) {
431 return $options;
432 }
433 // Generate the cache key
434 $key = $course->id.':';
435 if (!empty($user)) {
436 $key .= $user->id;
437 } else {
438 $key .= $USER->id;
439 }
440 // Serve from the cache if we've already generated for this course
441 if (array_key_exists($key, $courseoptions)) {
af8fe217 442 return $courseoptions[$key];
27bad0a6
SH
443 }
444
445 if (has_capability('moodle/blog:view', get_context_instance(CONTEXT_COURSE, $course->id))) {
446 // We can view!
447 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
448 // View entries about this course
449 $options['courseview'] = array(
450 'string' => get_string('viewcourseblogs', 'blog'),
451 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id))
452 );
453 }
454 // View MY entries about this course
455 $options['courseviewmine'] = array(
456 'string' => get_string('viewmyentriesaboutcourse', 'blog'),
457 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$USER->id))
458 );
459 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
460 // View the provided users entries about this course
461 $options['courseviewuser'] = array(
462 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)),
463 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$user->id))
464 );
465 }
466 }
467
468 if (has_capability('moodle/blog:create', $sitecontext)) {
469 // We can blog about this course
470 $options['courseadd'] = array(
471 'string' => get_string('blogaboutthiscourse', 'blog', get_string('course')),
472 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'courseid'=>$course->id))
473 );
474 }
475
476
477 // Cache the options for this course
478 $courseoptions[$key] = $options;
479 // Return the options
480 return $options;
481}
482
483/**
484 * Get the blog options relating to the given module for the given user
485 *
486 * @staticvar array $moduleoptions Cache
487 * @param stdClass $module The module to get options for
488 * @param stdClass $user The user to get options for null == currentuser
489 * @return array
490 */
491function blog_get_options_for_module(stdClass $module, stdClass $user=null) {
492 global $CFG, $USER;
493 // Cache
494 static $moduleoptions = array();
495
496 $options = array();
497 // User must be logged in, blogs must be enabled
498 if (!blog_is_enabled_for_user()) {
499 return $options;
500 }
501
502 // Check the user can associate with the module
503 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
504 if (!has_capability('moodle/blog:associatemodule', $sitecontext)) {
505 return $options;
506 }
507
508 // Generate the cache key
509 $key = $module->id.':';
510 if (!empty($user)) {
511 $key .= $user->id;
512 } else {
513 $key .= $USER->id;
514 }
515 if (array_key_exists($key, $moduleoptions)) {
516 // Serve from the cache so we don't have to regenerate
517 return $moduleoptions[$module->id];
518 }
519
520 if (has_capability('moodle/blog:view', get_context_instance(CONTEXT_MODULE, $module->id))) {
521 // We can view!
522 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
523 // View all entries about this module
524 $a = new stdClass;
525 $a->type = $module->modname;
526 $options['moduleview'] = array(
527 'string' => get_string('viewallmodentries', 'blog', $a),
528 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id))
529 );
530 }
531 // View MY entries about this module
532 $options['moduleviewmine'] = array(
533 'string' => get_string('viewmyentriesaboutmodule', 'blog', $module->modname),
534 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$USER->id))
535 );
536 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
537 // View the given users entries about this module
538 $a = new stdClass;
539 $a->mod = $module->modname;
540 $a->user = fullname($user);
541 $options['moduleviewuser'] = array(
542 'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a),
543 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$user->id))
544 );
545 }
546 }
547
548 if (has_capability('moodle/blog:create', $sitecontext)) {
549 // The user can blog about this module
550 $options['moduleadd'] = array(
551 'string' => get_string('blogaboutthismodule', 'blog', $module->modname),
552 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'modid'=>$module->id))
553 );
554 }
555 // Cache the options
556 $moduleoptions[$key] = $options;
557 // Return the options
558 return $options;
559}
560
cae83708 561/**
562 * This function encapsulates all the logic behind the complex
563 * navigation, titles and headings of the blog listing page, depending
1c7b8b93
NC
564 * on URL params. It looks at URL params and at the current context level.
565 * It builds and returns an array containing:
566 *
567 * 1. heading: The heading displayed above the blog entries
568 * 2. stradd: The text to be used as the "Add entry" link
569 * 3. strview: The text to be used as the "View entries" link
570 * 4. url: The moodle_url object used as the base for add and view links
571 * 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block
cae83708 572 *
c5dc10ee 573 * All other variables are set directly in $PAGE
cae83708 574 *
575 * It uses the current URL to build these variables.
576 * A number of mutually exclusive use cases are used to structure this function.
577 *
578 * @return array
579 */
580function blog_get_headers() {
581 global $CFG, $PAGE, $DB, $USER;
582
9366362a 583 $id = optional_param('id', null, PARAM_INT);
cae83708 584 $tag = optional_param('tag', null, PARAM_NOTAGS);
585 $tagid = optional_param('tagid', null, PARAM_INT);
586 $userid = optional_param('userid', null, PARAM_INT);
587 $modid = optional_param('modid', null, PARAM_INT);
588 $entryid = optional_param('entryid', null, PARAM_INT);
589 $groupid = optional_param('groupid', null, PARAM_INT);
590 $courseid = optional_param('courseid', null, PARAM_INT);
591 $search = optional_param('search', null, PARAM_RAW);
592 $action = optional_param('action', null, PARAM_ALPHA);
593 $confirm = optional_param('confirm', false, PARAM_BOOL);
594
1c7b8b93
NC
595 // Ignore userid when action == add
596 if ($action == 'add' && $userid) {
597 unset($userid);
598 $PAGE->url->remove_params(array('userid'));
599 } else if ($action == 'add' && $entryid) {
600 unset($entryid);
601 $PAGE->url->remove_params(array('entryid'));
602 }
603
604 $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
cae83708 605
a6855934 606 $blogurl = new moodle_url('/blog/index.php');
1c7b8b93
NC
607
608 // If the title is not yet set, it's likely that the context isn't set either, so skip this part
6f5e0852 609 $pagetitle = $PAGE->title;
1c7b8b93
NC
610 if (!empty($pagetitle)) {
611 $contexturl = blog_get_context_url();
612
613 // Look at the context URL, it may have additional params that are not in the current URL
614 if (!$blogurl->compare($contexturl)) {
615 $blogurl = $contexturl;
616 if (empty($courseid)) {
617 $courseid = $blogurl->param('courseid');
618 }
619 if (empty($modid)) {
620 $modid = $blogurl->param('modid');
621 }
622 }
623 }
624
625 $headers['stradd'] = get_string('addnewentry', 'blog');
626 $headers['strview'] = null;
cae83708 627
1c7b8b93
NC
628 $site = $DB->get_record('course', array('id' => SITEID));
629 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
cae83708 630 // Common Lang strings
631 $strparticipants = get_string("participants");
632 $strblogentries = get_string("blogentries", 'blog');
633
634 // Prepare record objects as needed
635 if (!empty($courseid)) {
1c7b8b93 636 $headers['filters']['course'] = $courseid;
cae83708 637 $course = $DB->get_record('course', array('id' => $courseid));
638 }
e96f2a77 639
cae83708 640 if (!empty($userid)) {
1c7b8b93 641 $headers['filters']['user'] = $userid;
cae83708 642 $user = $DB->get_record('user', array('id' => $userid));
516194d0 643 }
bbbf2d40 644
cae83708 645 if (!empty($groupid)) { // groupid always overrides courseid
1c7b8b93 646 $headers['filters']['group'] = $groupid;
cae83708 647 $group = $DB->get_record('groups', array('id' => $groupid));
648 $course = $DB->get_record('course', array('id' => $group->courseid));
649 }
bbbf2d40 650
27bad0a6
SH
651 $PAGE->set_pagelayout('standard');
652
1c7b8b93
NC
653 if (!empty($modid) && $CFG->useblogassociations && has_capability('moodle/blog:associatemodule', $sitecontext)) { // modid always overrides courseid, so the $course object may be reset here
654 $headers['filters']['module'] = $modid;
cae83708 655 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case
1c7b8b93
NC
656 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
657 $course = $DB->get_record('course', array('id' => $courseid));
cae83708 658 $cm = $DB->get_record('course_modules', array('id' => $modid));
659 $cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module));
660 $cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance));
1c7b8b93 661 $a->type = get_string('modulename', $cm->modname);
c5dc10ee 662 $PAGE->set_cm($cm, $course);
1c7b8b93
NC
663 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
664 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 665 }
b73d1ca4 666
1c7b8b93
NC
667 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
668 // Note: if action is set to 'add' or 'edit', we do this at the end
669 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
670 $PAGE->navbar->add($strblogentries, $blogurl);
4a8b890a 671 $PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
672 $PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
673 $headers['heading'] = get_string('siteblog', 'blog');
1c7b8b93 674 // $headers['strview'] = get_string('viewsiteentries', 'blog');
4a8b890a 675 }
c2ee4e87 676
1c7b8b93 677 // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information
cae83708 678 if (!empty($entryid)) {
1c7b8b93
NC
679 $headers['filters']['entry'] = $entryid;
680 $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id';
cae83708 681 $user = $DB->get_record_sql($sql, array($entryid));
1c7b8b93 682 $entry = $DB->get_record('post', array('id' => $entryid));
c2ee4e87 683
1c7b8b93 684 $blogurl->param('userid', $user->id);
c2ee4e87 685
cae83708 686 if (!empty($course)) {
9366362a 687 $mycourseid = $course->id;
1c7b8b93 688 $blogurl->param('courseid', $mycourseid);
c2ee4e87 689 } else {
9366362a 690 $mycourseid = $site->id;
c2ee4e87 691 }
e14de6f9 692
9366362a 693 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$mycourseid");
c5dc10ee 694 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93
NC
695 $PAGE->navbar->add($strblogentries, $blogurl);
696
697 $blogurl->remove_params('userid');
698 $PAGE->navbar->add($entry->subject, $blogurl);
c5dc10ee 699
700 $PAGE->set_title("$site->shortname: " . fullname($user) . ": $entry->subject");
701 $PAGE->set_heading("$site->shortname: " . fullname($user) . ": $entry->subject");
cae83708 702 $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user));
703
704 // We ignore tag and search params
1c7b8b93
NC
705 if (empty($action) || !$CFG->useblogassociations) {
706 $headers['url'] = $blogurl;
9366362a 707 return $headers;
708 }
c2ee4e87 709 }
240075cd 710
1c7b8b93
NC
711 // Case 3: A user's blog entries
712 if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
713 $blogurl->param('userid', $userid);
c5dc10ee 714 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$site->id");
715 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 716 $PAGE->navbar->add($strblogentries, $blogurl);
c5dc10ee 717 $PAGE->set_title("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
718 $PAGE->set_heading("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
cae83708 719 $headers['heading'] = get_string('userblog', 'blog', fullname($user));
27bad0a6 720 $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user));
1c7b8b93
NC
721
722 } else
cae83708 723
1c7b8b93
NC
724 // Case 4: No blog associations, no userid
725 if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) {
726 $PAGE->navbar->add($strblogentries, $blogurl);
727 $PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
728 $PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
729 $headers['heading'] = get_string('siteblog', 'blog');
cae83708 730 } else
731
1c7b8b93 732 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored)
9366362a 733 if (!empty($userid) && !empty($modid) && empty($entryid)) {
1c7b8b93
NC
734 $blogurl->param('userid', $userid);
735 $blogurl->param('modid', $modid);
cae83708 736
737 // Course module navigation is handled by build_navigation as the second param
738 $headers['cm'] = $cm;
c5dc10ee 739 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 740 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 741
c5dc10ee 742 $PAGE->set_title("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
743 $PAGE->set_heading("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
cae83708 744
745 $a->user = fullname($user);
746 $a->mod = $cm->name;
1c7b8b93 747 $a->type = get_string('modulename', $cm->modname);
cae83708 748 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
1c7b8b93
NC
749 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
750 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 751 } else
752
1c7b8b93 753 // Case 6: Blog entries associated with a course by a specific user
9366362a 754 if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
1c7b8b93
NC
755 $blogurl->param('userid', $userid);
756 $blogurl->param('courseid', $courseid);
cae83708 757
c5dc10ee 758 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$course->id");
759 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 760 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 761
c5dc10ee 762 $PAGE->set_title("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
763 $PAGE->set_heading("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
cae83708 764
765 $a->user = fullname($user);
766 $a->course = $course->fullname;
1c7b8b93 767 $a->type = get_string('course');
cae83708 768 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
1c7b8b93
NC
769 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
770 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
771
772 // Remove the userid from the URL to inform the blog_menu block correctly
773 $blogurl->remove_params(array('userid'));
cae83708 774 } else
775
1c7b8b93 776 // Case 7: Blog entries by members of a group, associated with that group's course
9366362a 777 if (!empty($groupid) && empty($modid) && empty($entryid)) {
1c7b8b93 778 $blogurl->param('courseid', $course->id);
e14de6f9 779
1c7b8b93
NC
780 $PAGE->navbar->add($strblogentries, $blogurl);
781 $blogurl->remove_params(array('courseid'));
782 $blogurl->param('groupid', $groupid);
783 $PAGE->navbar->add($group->name, $blogurl);
cae83708 784
c5dc10ee 785 $PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
786 $PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
cae83708 787
788 $a->group = $group->name;
789 $a->course = $course->fullname;
1c7b8b93 790 $a->type = get_string('course');
cae83708 791 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
1c7b8b93
NC
792 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
793 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
cae83708 794 } else
795
1c7b8b93 796 // Case 8: Blog entries by members of a group, associated with an activity in that course
9366362a 797 if (!empty($groupid) && !empty($modid) && empty($entryid)) {
cae83708 798 $headers['cm'] = $cm;
1c7b8b93
NC
799 $blogurl->param('modid', $modid);
800 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 801
1c7b8b93
NC
802 $blogurl->param('groupid', $groupid);
803 $PAGE->navbar->add($group->name, $blogurl);
cae83708 804
c5dc10ee 805 $PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
806 $PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
cae83708 807
808 $a->group = $group->name;
809 $a->mod = $cm->name;
1c7b8b93 810 $a->type = get_string('modulename', $cm->modname);
cae83708 811 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
1c7b8b93
NC
812 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
813 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 814
815 } else
816
1c7b8b93 817 // Case 9: All blog entries associated with an activity
9366362a 818 if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
c5dc10ee 819 $PAGE->set_cm($cm, $course);
1c7b8b93
NC
820 $blogurl->param('modid', $modid);
821 $PAGE->navbar->add($strblogentries, $blogurl);
c5dc10ee 822 $PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
823 $PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
cae83708 824 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
1c7b8b93
NC
825 $a->type = get_string('modulename', $cm->modname);
826 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
827 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 828 } else
829
1c7b8b93 830 // Case 10: All blog entries associated with a course
9366362a 831 if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
1c7b8b93
NC
832 $blogurl->param('courseid', $courseid);
833 $PAGE->navbar->add($strblogentries, $blogurl);
c5dc10ee 834 $PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
835 $PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
1c7b8b93 836 $a->type = get_string('course');
cae83708 837 $headers['heading'] = get_string('blogentriesabout', 'blog', $course->fullname);
1c7b8b93
NC
838 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
839 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
840 $blogurl->remove_params(array('userid'));
841 }
842
843 if (!in_array($action, array('edit', 'add'))) {
844 // Append Tag info
845 if (!empty($tagid)) {
846 $headers['filters']['tag'] = $tagid;
847 $blogurl->param('tagid', $tagid);
848 $tagrec = $DB->get_record('tag', array('id'=>$tagid));
849 $PAGE->navbar->add($tagrec->name, $blogurl);
850 } elseif (!empty($tag)) {
851 $blogurl->param('tag', $tag);
852 $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl);
853 }
240075cd 854
1c7b8b93
NC
855 // Append Search info
856 if (!empty($search)) {
857 $headers['filters']['search'] = $search;
858 $blogurl->param('search', $search);
859 $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
860 }
ee00eb8c 861 }
862
cae83708 863 // Append edit mode info
864 if (!empty($action) && $action == 'add') {
9366362a 865 if (empty($modid) && empty($courseid)) {
866 if (empty($user)) {
867 $user = $USER;
868 }
869 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$site->id");
870 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93
NC
871 $blogurl->param('userid', $user->id);
872 $PAGE->navbar->add($strblogentries, $blogurl);
9366362a 873 }
c5dc10ee 874 $PAGE->navbar->add(get_string('addnewentry', 'blog'));
cae83708 875 } else if (!empty($action) && $action == 'edit') {
c5dc10ee 876 $PAGE->navbar->add(get_string('editentry', 'blog'));
240075cd 877 }
ee00eb8c 878
1c7b8b93
NC
879 if (empty($headers['url'])) {
880 $headers['url'] = $blogurl;
881 }
cae83708 882 return $headers;
883}
23677261 884
1c7b8b93
NC
885/**
886 * Function used by the navigation system to provide links to blog preferences and external blogs.
887 * @param object $settingsnav The settings_navigation object
888 * @return navigation key
889 */
23677261 890function blog_extend_settings_navigation($settingsnav) {
891 global $USER, $PAGE, $FULLME, $CFG, $DB, $OUTPUT;
3406acde 892 $blog = $settingsnav->add(get_string('blogadministration', 'blog'));
23677261 893 $blog->forceopen = true;
b73d1ca4 894
23677261 895 $blog->add(get_string('preferences', 'blog'), new moodle_url('preferences.php'), navigation_node::TYPE_SETTING);
1c7b8b93
NC
896
897 if ($CFG->useexternalblogs && $CFG->maxexternalblogsperuser > 0 && has_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM))) {
898 $blog->add(get_string('externalblogs', 'blog'), new moodle_url('external_blogs.php'), navigation_node::TYPE_SETTING);
23677261 899 }
900
3406acde 901 return $blog;
23677261 902}
1c7b8b93
NC
903
904/**
905 * Shortcut function for getting a count of blog entries associated with a course or a module
906 * @param int $courseid The ID of the course
907 * @param int $cmid The ID of the course_modules
908 * @return string The number of associated entries
909 */
910function blog_get_associated_count($courseid, $cmid=null) {
911 global $DB;
912 $context = get_context_instance(CONTEXT_COURSE, $courseid);
913 if ($cmid) {
914 $context = get_context_instance(CONTEXT_MODULE, $cmid);
915 }
916 return $DB->count_records('blog_association', array('contextid' => $context->id));
27bad0a6 917}