Updated the HEAD build version to 20100423
[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
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
a6855934 316 $blogurl = new moodle_url('/blog/index.php');
1c7b8b93
NC
317
318 // If the title is not yet set, it's likely that the context isn't set either, so skip this part
6f5e0852 319 $pagetitle = $PAGE->title;
1c7b8b93
NC
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));
1c7b8b93 369 $a->type = get_string('modulename', $cm->modname);
c5dc10ee 370 $PAGE->set_cm($cm, $course);
1c7b8b93
NC
371 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
372 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 373 }
b73d1ca4 374
1c7b8b93
NC
375 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
376 // Note: if action is set to 'add' or 'edit', we do this at the end
377 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
378 $PAGE->navbar->add($strblogentries, $blogurl);
4a8b890a 379 $PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
380 $PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
381 $headers['heading'] = get_string('siteblog', 'blog');
1c7b8b93 382 // $headers['strview'] = get_string('viewsiteentries', 'blog');
4a8b890a 383 }
c2ee4e87 384
1c7b8b93 385 // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information
cae83708 386 if (!empty($entryid)) {
1c7b8b93
NC
387 $headers['filters']['entry'] = $entryid;
388 $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id';
cae83708 389 $user = $DB->get_record_sql($sql, array($entryid));
1c7b8b93 390 $entry = $DB->get_record('post', array('id' => $entryid));
c2ee4e87 391
1c7b8b93 392 $blogurl->param('userid', $user->id);
c2ee4e87 393
cae83708 394 if (!empty($course)) {
9366362a 395 $mycourseid = $course->id;
1c7b8b93 396 $blogurl->param('courseid', $mycourseid);
c2ee4e87 397 } else {
9366362a 398 $mycourseid = $site->id;
c2ee4e87 399 }
e14de6f9 400
9366362a 401 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$mycourseid");
c5dc10ee 402 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93
NC
403 $PAGE->navbar->add($strblogentries, $blogurl);
404
405 $blogurl->remove_params('userid');
406 $PAGE->navbar->add($entry->subject, $blogurl);
c5dc10ee 407
408 $PAGE->set_title("$site->shortname: " . fullname($user) . ": $entry->subject");
409 $PAGE->set_heading("$site->shortname: " . fullname($user) . ": $entry->subject");
cae83708 410 $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user));
411
412 // We ignore tag and search params
1c7b8b93
NC
413 if (empty($action) || !$CFG->useblogassociations) {
414 $headers['url'] = $blogurl;
9366362a 415 return $headers;
416 }
c2ee4e87 417 }
240075cd 418
1c7b8b93
NC
419 // Case 3: A user's blog entries
420 if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
421 $blogurl->param('userid', $userid);
c5dc10ee 422 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$site->id");
423 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 424 $PAGE->navbar->add($strblogentries, $blogurl);
c5dc10ee 425 $PAGE->set_title("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
426 $PAGE->set_heading("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
cae83708 427 $headers['heading'] = get_string('userblog', 'blog', fullname($user));
1c7b8b93
NC
428 $headers['strview'] = get_string('viewuserentries', 'blog');
429
430 } else
cae83708 431
1c7b8b93
NC
432 // Case 4: No blog associations, no userid
433 if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) {
434 $PAGE->navbar->add($strblogentries, $blogurl);
435 $PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
436 $PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
437 $headers['heading'] = get_string('siteblog', 'blog');
cae83708 438 } else
439
1c7b8b93 440 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored)
9366362a 441 if (!empty($userid) && !empty($modid) && empty($entryid)) {
1c7b8b93
NC
442 $blogurl->param('userid', $userid);
443 $blogurl->param('modid', $modid);
cae83708 444
445 // Course module navigation is handled by build_navigation as the second param
446 $headers['cm'] = $cm;
c5dc10ee 447 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 448 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 449
c5dc10ee 450 $PAGE->set_title("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
451 $PAGE->set_heading("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
cae83708 452
453 $a->user = fullname($user);
454 $a->mod = $cm->name;
1c7b8b93 455 $a->type = get_string('modulename', $cm->modname);
cae83708 456 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
1c7b8b93
NC
457 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
458 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 459 } else
460
1c7b8b93 461 // Case 6: Blog entries associated with a course by a specific user
9366362a 462 if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
1c7b8b93
NC
463 $blogurl->param('userid', $userid);
464 $blogurl->param('courseid', $courseid);
cae83708 465
c5dc10ee 466 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$course->id");
467 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93 468 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 469
c5dc10ee 470 $PAGE->set_title("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
471 $PAGE->set_heading("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
cae83708 472
473 $a->user = fullname($user);
474 $a->course = $course->fullname;
1c7b8b93 475 $a->type = get_string('course');
cae83708 476 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
1c7b8b93
NC
477 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
478 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
479
480 // Remove the userid from the URL to inform the blog_menu block correctly
481 $blogurl->remove_params(array('userid'));
cae83708 482 } else
483
1c7b8b93 484 // Case 7: Blog entries by members of a group, associated with that group's course
9366362a 485 if (!empty($groupid) && empty($modid) && empty($entryid)) {
1c7b8b93 486 $blogurl->param('courseid', $course->id);
e14de6f9 487
1c7b8b93
NC
488 $PAGE->navbar->add($strblogentries, $blogurl);
489 $blogurl->remove_params(array('courseid'));
490 $blogurl->param('groupid', $groupid);
491 $PAGE->navbar->add($group->name, $blogurl);
cae83708 492
c5dc10ee 493 $PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
494 $PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
cae83708 495
496 $a->group = $group->name;
497 $a->course = $course->fullname;
1c7b8b93 498 $a->type = get_string('course');
cae83708 499 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
1c7b8b93
NC
500 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
501 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
cae83708 502 } else
503
1c7b8b93 504 // Case 8: Blog entries by members of a group, associated with an activity in that course
9366362a 505 if (!empty($groupid) && !empty($modid) && empty($entryid)) {
cae83708 506 $headers['cm'] = $cm;
1c7b8b93
NC
507 $blogurl->param('modid', $modid);
508 $PAGE->navbar->add($strblogentries, $blogurl);
cae83708 509
1c7b8b93
NC
510 $blogurl->param('groupid', $groupid);
511 $PAGE->navbar->add($group->name, $blogurl);
cae83708 512
c5dc10ee 513 $PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
514 $PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
cae83708 515
516 $a->group = $group->name;
517 $a->mod = $cm->name;
1c7b8b93 518 $a->type = get_string('modulename', $cm->modname);
cae83708 519 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
1c7b8b93
NC
520 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
521 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 522
523 } else
524
1c7b8b93 525 // Case 9: All blog entries associated with an activity
9366362a 526 if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
c5dc10ee 527 $PAGE->set_cm($cm, $course);
1c7b8b93
NC
528 $blogurl->param('modid', $modid);
529 $PAGE->navbar->add($strblogentries, $blogurl);
c5dc10ee 530 $PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
531 $PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
cae83708 532 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
1c7b8b93
NC
533 $a->type = get_string('modulename', $cm->modname);
534 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
535 $headers['strview'] = get_string('viewallmodentries', 'blog', $a);
cae83708 536 } else
537
1c7b8b93 538 // Case 10: All blog entries associated with a course
9366362a 539 if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
1c7b8b93
NC
540 $blogurl->param('courseid', $courseid);
541 $PAGE->navbar->add($strblogentries, $blogurl);
c5dc10ee 542 $PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
543 $PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
1c7b8b93 544 $a->type = get_string('course');
cae83708 545 $headers['heading'] = get_string('blogentriesabout', 'blog', $course->fullname);
1c7b8b93
NC
546 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
547 $headers['strview'] = get_string('viewblogentries', 'blog', $a);
548 $blogurl->remove_params(array('userid'));
549 }
550
551 if (!in_array($action, array('edit', 'add'))) {
552 // Append Tag info
553 if (!empty($tagid)) {
554 $headers['filters']['tag'] = $tagid;
555 $blogurl->param('tagid', $tagid);
556 $tagrec = $DB->get_record('tag', array('id'=>$tagid));
557 $PAGE->navbar->add($tagrec->name, $blogurl);
558 } elseif (!empty($tag)) {
559 $blogurl->param('tag', $tag);
560 $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl);
561 }
240075cd 562
1c7b8b93
NC
563 // Append Search info
564 if (!empty($search)) {
565 $headers['filters']['search'] = $search;
566 $blogurl->param('search', $search);
567 $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
568 }
ee00eb8c 569 }
570
cae83708 571 // Append edit mode info
572 if (!empty($action) && $action == 'add') {
9366362a 573 if (empty($modid) && empty($courseid)) {
574 if (empty($user)) {
575 $user = $USER;
576 }
577 $PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$site->id");
578 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
1c7b8b93
NC
579 $blogurl->param('userid', $user->id);
580 $PAGE->navbar->add($strblogentries, $blogurl);
9366362a 581 }
c5dc10ee 582 $PAGE->navbar->add(get_string('addnewentry', 'blog'));
cae83708 583 } else if (!empty($action) && $action == 'edit') {
c5dc10ee 584 $PAGE->navbar->add(get_string('editentry', 'blog'));
240075cd 585 }
ee00eb8c 586
1c7b8b93
NC
587 if (empty($headers['url'])) {
588 $headers['url'] = $blogurl;
589 }
cae83708 590 return $headers;
591}
23677261 592
1c7b8b93
NC
593/**
594 * Function used by the navigation system to provide links to blog preferences and external blogs.
595 * @param object $settingsnav The settings_navigation object
596 * @return navigation key
597 */
23677261 598function blog_extend_settings_navigation($settingsnav) {
599 global $USER, $PAGE, $FULLME, $CFG, $DB, $OUTPUT;
3406acde 600 $blog = $settingsnav->add(get_string('blogadministration', 'blog'));
23677261 601 $blog->forceopen = true;
b73d1ca4 602
23677261 603 $blog->add(get_string('preferences', 'blog'), new moodle_url('preferences.php'), navigation_node::TYPE_SETTING);
1c7b8b93
NC
604
605 if ($CFG->useexternalblogs && $CFG->maxexternalblogsperuser > 0 && has_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM))) {
606 $blog->add(get_string('externalblogs', 'blog'), new moodle_url('external_blogs.php'), navigation_node::TYPE_SETTING);
23677261 607 }
608
3406acde 609 return $blog;
23677261 610}
1c7b8b93
NC
611
612/**
613 * Shortcut function for getting a count of blog entries associated with a course or a module
614 * @param int $courseid The ID of the course
615 * @param int $cmid The ID of the course_modules
616 * @return string The number of associated entries
617 */
618function blog_get_associated_count($courseid, $cmid=null) {
619 global $DB;
620 $context = get_context_instance(CONTEXT_COURSE, $courseid);
621 if ($cmid) {
622 $context = get_context_instance(CONTEXT_MODULE, $cmid);
623 }
624 return $DB->count_records('blog_association', array('contextid' => $context->id));
625}