MDL-19676 MDL-14408 MDL-8776 Blog improvements
[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
1c7b8b93 70 if (!empty($USER->id) && $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:
95 if (!empty($USER->id)) { // not logged in viewers forbidden
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
b0e90a0c 230 $viewblogentriesurl = new moodle_url($CFG->wwwroot . '/blog/index.php');
c2ee4e87 231
cae83708 232 if (empty($context)) {
233 global $PAGE;
234 $context = $PAGE->get_context();
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
271/**
272 * This function encapsulates all the logic behind the complex
273 * navigation, titles and headings of the blog listing page, depending
1c7b8b93
NC
274 * on URL params. It looks at URL params and at the current context level.
275 * It builds and returns an array containing:
276 *
277 * 1. heading: The heading displayed above the blog entries
278 * 2. stradd: The text to be used as the "Add entry" link
279 * 3. strview: The text to be used as the "View entries" link
280 * 4. url: The moodle_url object used as the base for add and view links
281 * 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block
cae83708 282 *
c5dc10ee 283 * All other variables are set directly in $PAGE
cae83708 284 *
285 * It uses the current URL to build these variables.
286 * A number of mutually exclusive use cases are used to structure this function.
287 *
288 * @return array
289 */
290function blog_get_headers() {
291 global $CFG, $PAGE, $DB, $USER;
292
9366362a 293 $id = optional_param('id', null, PARAM_INT);
cae83708 294 $tag = optional_param('tag', null, PARAM_NOTAGS);
295 $tagid = optional_param('tagid', null, PARAM_INT);
296 $userid = optional_param('userid', null, PARAM_INT);
297 $modid = optional_param('modid', null, PARAM_INT);
298 $entryid = optional_param('entryid', null, PARAM_INT);
299 $groupid = optional_param('groupid', null, PARAM_INT);
300 $courseid = optional_param('courseid', null, PARAM_INT);
301 $search = optional_param('search', null, PARAM_RAW);
302 $action = optional_param('action', null, PARAM_ALPHA);
303 $confirm = optional_param('confirm', false, PARAM_BOOL);
304
1c7b8b93
NC
305 // Ignore userid when action == add
306 if ($action == 'add' && $userid) {
307 unset($userid);
308 $PAGE->url->remove_params(array('userid'));
309 } else if ($action == 'add' && $entryid) {
310 unset($entryid);
311 $PAGE->url->remove_params(array('entryid'));
312 }
313
314 $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
cae83708 315
1c7b8b93
NC
316 $blogurl = new moodle_url($CFG->wwwroot . '/blog/index.php');
317
318 // If the title is not yet set, it's likely that the context isn't set either, so skip this part
319 $pagetitle = $PAGE->title;
320 if (!empty($pagetitle)) {
321 $contexturl = blog_get_context_url();
322
323 // Look at the context URL, it may have additional params that are not in the current URL
324 if (!$blogurl->compare($contexturl)) {
325 $blogurl = $contexturl;
326 if (empty($courseid)) {
327 $courseid = $blogurl->param('courseid');
328 }
329 if (empty($modid)) {
330 $modid = $blogurl->param('modid');
331 }
332 }
333 }
334
335 $headers['stradd'] = get_string('addnewentry', 'blog');
336 $headers['strview'] = null;
cae83708 337
1c7b8b93
NC
338 $site = $DB->get_record('course', array('id' => SITEID));
339 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
cae83708 340 // Common Lang strings
341 $strparticipants = get_string("participants");
342 $strblogentries = get_string("blogentries", 'blog');
343
344 // Prepare record objects as needed
345 if (!empty($courseid)) {
1c7b8b93 346 $headers['filters']['course'] = $courseid;
cae83708 347 $course = $DB->get_record('course', array('id' => $courseid));
348 }
e96f2a77 349
cae83708 350 if (!empty($userid)) {
1c7b8b93 351 $headers['filters']['user'] = $userid;
cae83708 352 $user = $DB->get_record('user', array('id' => $userid));
516194d0 353 }
bbbf2d40 354
cae83708 355 if (!empty($groupid)) { // groupid always overrides courseid
1c7b8b93 356 $headers['filters']['group'] = $groupid;
cae83708 357 $group = $DB->get_record('groups', array('id' => $groupid));
358 $course = $DB->get_record('course', array('id' => $group->courseid));
359 }
bbbf2d40 360
1c7b8b93
NC
361 if (!empty($modid) && $CFG->useblogassociations && has_capability('moodle/blog:associatemodule', $sitecontext)) { // modid always overrides courseid, so the $course object may be reset here
362 $headers['filters']['module'] = $modid;
cae83708 363 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case
1c7b8b93
NC
364 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
365 $course = $DB->get_record('course', array('id' => $courseid));
cae83708 366 $cm = $DB->get_record('course_modules', array('id' => $modid));
367 $cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module));
368 $cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance));
c5dc10ee 369 $cm->context = get_context_instance(CONTEXT_MODULE, $modid);
1c7b8b93 370 $a->type = get_string('modulename', $cm->modname);
c5dc10ee 371 $PAGE->set_cm($cm, $course);
1c7b8b93
NC
372 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
373 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 374 }
b73d1ca4 375
1c7b8b93
NC
376 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
377 // Note: if action is set to 'add' or 'edit', we do this at the end
378 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
379 $PAGE->navbar->add($strblogentries, $blogurl);
4a8b890a 380 $PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
381 $PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
382 $headers['heading'] = get_string('siteblog', 'blog');
1c7b8b93 383 // $headers['strview'] = get_string('viewsiteentries', 'blog');
4a8b890a 384 }
c2ee4e87 385
1c7b8b93 386 // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information
cae83708 387 if (!empty($entryid)) {
1c7b8b93
NC
388 $headers['filters']['entry'] = $entryid;
389 $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id';
cae83708 390 $user = $DB->get_record_sql($sql, array($entryid));
1c7b8b93 391 $entry = $DB->get_record('post', array('id' => $entryid));
c2ee4e87 392
1c7b8b93 393 $blogurl->param('userid', $user->id);
c2ee4e87 394
cae83708 395 if (!empty($course)) {
9366362a 396 $mycourseid = $course->id;
1c7b8b93 397 $blogurl->param('courseid', $mycourseid);
c2ee4e87 398 } else {
9366362a 399 $mycourseid = $site->id;
c2ee4e87 400 }
e14de6f9 401
9366362a 402 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$mycourseid");
c5dc10ee 403 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93
NC
404 $PAGE->navbar->add($strblogentries, $blogurl);
405
406 $blogurl->remove_params('userid');
407 $PAGE->navbar->add($entry->subject, $blogurl);
c5dc10ee 408
409 $PAGE->set_title("$site->shortname: " . fullname($user) . ": $entry->subject");
410 $PAGE->set_heading("$site->shortname: " . fullname($user) . ": $entry->subject");
cae83708 411 $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user));
412
413 // We ignore tag and search params
1c7b8b93
NC
414 if (empty($action) || !$CFG->useblogassociations) {
415 $headers['url'] = $blogurl;
9366362a 416 return $headers;
417 }
c2ee4e87 418 }
240075cd 419
1c7b8b93
NC
420 // Case 3: A user's blog entries
421 if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
422 $blogurl->param('userid', $userid);
c5dc10ee 423 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$site->id");
424 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 425 $PAGE->navbar->add($strblogentries, $blogurl);
c5dc10ee 426 $PAGE->set_title("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
427 $PAGE->set_heading("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
cae83708 428 $headers['heading'] = get_string('userblog', 'blog', fullname($user));
1c7b8b93
NC
429 $headers['strview'] = get_string('viewuserentries', 'blog');
430
431 } else
cae83708 432
1c7b8b93
NC
433 // Case 4: No blog associations, no userid
434 if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) {
435 $PAGE->navbar->add($strblogentries, $blogurl);
436 $PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
437 $PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
438 $headers['heading'] = get_string('siteblog', 'blog');
cae83708 439 } else
440
1c7b8b93 441 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored)
9366362a 442 if (!empty($userid) && !empty($modid) && empty($entryid)) {
1c7b8b93
NC
443 $blogurl->param('userid', $userid);
444 $blogurl->param('modid', $modid);
cae83708 445
446 // Course module navigation is handled by build_navigation as the second param
447 $headers['cm'] = $cm;
c5dc10ee 448 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 449 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 450
c5dc10ee 451 $PAGE->set_title("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
452 $PAGE->set_heading("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
cae83708 453
454 $a->user = fullname($user);
455 $a->mod = $cm->name;
1c7b8b93 456 $a->type = get_string('modulename', $cm->modname);
cae83708 457 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
1c7b8b93
NC
458 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
459 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 460 } else
461
1c7b8b93 462 // Case 6: Blog entries associated with a course by a specific user
9366362a 463 if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
1c7b8b93
NC
464 $blogurl->param('userid', $userid);
465 $blogurl->param('courseid', $courseid);
cae83708 466
c5dc10ee 467 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$course->id");
468 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 469 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 470
c5dc10ee 471 $PAGE->set_title("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
472 $PAGE->set_heading("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
cae83708 473
474 $a->user = fullname($user);
475 $a->course = $course->fullname;
1c7b8b93 476 $a->type = get_string('course');
cae83708 477 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
1c7b8b93
NC
478 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
479 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
480
481 // Remove the userid from the URL to inform the blog_menu block correctly
482 $blogurl->remove_params(array('userid'));
cae83708 483 } else
484
1c7b8b93 485 // Case 7: Blog entries by members of a group, associated with that group's course
9366362a 486 if (!empty($groupid) && empty($modid) && empty($entryid)) {
1c7b8b93 487 $blogurl->param('courseid', $course->id);
e14de6f9 488
1c7b8b93
NC
489 $PAGE->navbar->add($strblogentries, $blogurl);
490 $blogurl->remove_params(array('courseid'));
491 $blogurl->param('groupid', $groupid);
492 $PAGE->navbar->add($group->name, $blogurl);
cae83708 493
c5dc10ee 494 $PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
495 $PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
cae83708 496
497 $a->group = $group->name;
498 $a->course = $course->fullname;
1c7b8b93 499 $a->type = get_string('course');
cae83708 500 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
1c7b8b93
NC
501 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
502 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
cae83708 503 } else
504
1c7b8b93 505 // Case 8: Blog entries by members of a group, associated with an activity in that course
9366362a 506 if (!empty($groupid) && !empty($modid) && empty($entryid)) {
cae83708 507 $headers['cm'] = $cm;
1c7b8b93
NC
508 $blogurl->param('modid', $modid);
509 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 510
1c7b8b93
NC
511 $blogurl->param('groupid', $groupid);
512 $PAGE->navbar->add($group->name, $blogurl);
cae83708 513
c5dc10ee 514 $PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
515 $PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
cae83708 516
517 $a->group = $group->name;
518 $a->mod = $cm->name;
1c7b8b93 519 $a->type = get_string('modulename', $cm->modname);
cae83708 520 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
1c7b8b93
NC
521 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
522 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 523
524 } else
525
1c7b8b93 526 // Case 9: All blog entries associated with an activity
9366362a 527 if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
c5dc10ee 528 $PAGE->set_cm($cm, $course);
1c7b8b93
NC
529 $blogurl->param('modid', $modid);
530 $PAGE->navbar->add($strblogentries, $blogurl);
c5dc10ee 531 $PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
532 $PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
cae83708 533 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
1c7b8b93
NC
534 $a->type = get_string('modulename', $cm->modname);
535 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
536 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 537 } else
538
1c7b8b93 539 // Case 10: All blog entries associated with a course
9366362a 540 if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
1c7b8b93
NC
541 $blogurl->param('courseid', $courseid);
542 $PAGE->navbar->add($strblogentries, $blogurl);
c5dc10ee 543 $PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
544 $PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
1c7b8b93 545 $a->type = get_string('course');
cae83708 546 $headers['heading'] = get_string('blogentriesabout', 'blog', $course->fullname);
1c7b8b93
NC
547 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
548 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
549 $blogurl->remove_params(array('userid'));
550 }
551
552 if (!in_array($action, array('edit', 'add'))) {
553 // Append Tag info
554 if (!empty($tagid)) {
555 $headers['filters']['tag'] = $tagid;
556 $blogurl->param('tagid', $tagid);
557 $tagrec = $DB->get_record('tag', array('id'=>$tagid));
558 $PAGE->navbar->add($tagrec->name, $blogurl);
559 } elseif (!empty($tag)) {
560 $blogurl->param('tag', $tag);
561 $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl);
562 }
240075cd 563
1c7b8b93
NC
564 // Append Search info
565 if (!empty($search)) {
566 $headers['filters']['search'] = $search;
567 $blogurl->param('search', $search);
568 $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
569 }
ee00eb8c 570 }
571
cae83708 572 // Append edit mode info
573 if (!empty($action) && $action == 'add') {
9366362a 574 if (empty($modid) && empty($courseid)) {
575 if (empty($user)) {
576 $user = $USER;
577 }
578 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$site->id");
579 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93
NC
580 $blogurl->param('userid', $user->id);
581 $PAGE->navbar->add($strblogentries, $blogurl);
9366362a 582 }
c5dc10ee 583 $PAGE->navbar->add(get_string('addnewentry', 'blog'));
cae83708 584 } else if (!empty($action) && $action == 'edit') {
c5dc10ee 585 $PAGE->navbar->add(get_string('editentry', 'blog'));
240075cd 586 }
ee00eb8c 587
1c7b8b93
NC
588 if (empty($headers['url'])) {
589 $headers['url'] = $blogurl;
590 }
cae83708 591 return $headers;
592}
23677261 593
1c7b8b93
NC
594/**
595 * Function used by the navigation system to provide links to blog preferences and external blogs.
596 * @param object $settingsnav The settings_navigation object
597 * @return navigation key
598 */
23677261 599function blog_extend_settings_navigation($settingsnav) {
600 global $USER, $PAGE, $FULLME, $CFG, $DB, $OUTPUT;
601 $blogkey = $settingsnav->add(get_string('blogadministration', 'blog'));
602 $blog = $settingsnav->get($blogkey);
603 $blog->forceopen = true;
b73d1ca4 604
23677261 605 $blog->add(get_string('preferences', 'blog'), new moodle_url('preferences.php'), navigation_node::TYPE_SETTING);
1c7b8b93
NC
606
607 if ($CFG->useexternalblogs && $CFG->maxexternalblogsperuser > 0 && has_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM))) {
608 $blog->add(get_string('externalblogs', 'blog'), new moodle_url('external_blogs.php'), navigation_node::TYPE_SETTING);
23677261 609 }
610
611 return $blogkey;
612}
1c7b8b93
NC
613
614/**
615 * Shortcut function for getting a count of blog entries associated with a course or a module
616 * @param int $courseid The ID of the course
617 * @param int $cmid The ID of the course_modules
618 * @return string The number of associated entries
619 */
620function blog_get_associated_count($courseid, $cmid=null) {
621 global $DB;
622 $context = get_context_instance(CONTEXT_COURSE, $courseid);
623 if ($cmid) {
624 $context = get_context_instance(CONTEXT_MODULE, $cmid);
625 }
626 return $DB->count_records('blog_association', array('contextid' => $context->id));
627}