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