forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
executable file
·627 lines (530 loc) · 25.1 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Core global functions for Blog.
*
* @package moodlecore
* @subpackage blog
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Library of functions and constants for blog
*/
require_once($CFG->dirroot .'/blog/rsslib.php');
require_once($CFG->dirroot.'/tag/lib.php');
/**
* User can edit a blog entry if this is their own blog entry and they have
* the capability moodle/blog:create, or if they have the capability
* moodle/blog:manageentries.
*
* This also applies to deleting of entries.
*/
function blog_user_can_edit_entry($blogentry) {
global $USER;
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
if (has_capability('moodle/blog:manageentries', $sitecontext)) {
return true; // can edit any blog entry
}
if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) {
return true; // can edit own when having blog:create capability
}
return false;
}
/**
* Checks to see if a user can view the blogs of another user.
* Only blog level is checked here, the capabilities are enforced
* in blog/index.php
*/
function blog_user_can_view_user_entry($targetuserid, $blogentry=null) {
global $CFG, $USER, $DB;
if (empty($CFG->bloglevel)) {
return false; // blog system disabled
}
if (!empty($USER->id) && $USER->id == $targetuserid) {
return true; // can view own entries in any case
}
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
if (has_capability('moodle/blog:manageentries', $sitecontext)) {
return true; // can manage all entries
}
// coming for 1 entry, make sure it's not a draft
if ($blogentry && $blogentry->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
return false; // can not view draft of others
}
// coming for 1 entry, make sure user is logged in, if not a public blog
if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) {
return false;
}
switch ($CFG->bloglevel) {
case BLOG_GLOBAL_LEVEL:
return true;
break;
case BLOG_SITE_LEVEL:
if (!empty($USER->id)) { // not logged in viewers forbidden
return true;
}
return false;
break;
case BLOG_USER_LEVEL:
default:
$personalcontext = get_context_instance(CONTEXT_USER, $targetuserid);
return has_capability('moodle/user:readuserblogs', $personalcontext);
break;
}
}
/**
* remove all associations for the blog entries of a particular user
* @param int userid - id of user whose blog associations will be deleted
*/
function blog_remove_associations_for_user($userid) {
global $DB;
$blogentries = blog_fetch_entries(array('user' => $userid), 'lasmodified DESC');
foreach ($blogentries as $entry) {
if (blog_user_can_edit_entry($entry)) {
blog_remove_associations_for_entry($entry->id);
}
}
}
/**
* remove all associations for the blog entries of a particular course
* @param int courseid - id of user whose blog associations will be deleted
*/
function blog_remove_associations_for_course($courseid) {
global $DB;
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$DB->delete_records('blog_association', array('contextid' => $context->id));
}
/**
* Given a record in the {blog_external} table, checks the blog's URL
* for new entries not yet copied into Moodle.
*
* @param object $externalblog
* @return boolean False if the Feed is invalid
*/
function blog_sync_external_entries($externalblog) {
global $CFG, $DB;
require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
$rssfile = new moodle_simplepie_file($externalblog->url);
$filetest = new SimplePie_Locator($rssfile);
if (!$filetest->is_feed($rssfile)) {
$externalblog->failedlastsync = 1;
$DB->update_record('blog_external', $externalblog);
return false;
} else if ($externalblog->failedlastsync) {
$externalblog->failedlastsync = 0;
$DB->update_record('blog_external', $externalblog);
}
// Delete all blog entries associated with this external blog
blog_delete_external_entries($externalblog);
$rss = new moodle_simplepie($externalblog->url);
if (empty($rss->data)) {
return null;
}
foreach ($rss->get_items() as $entry) {
// If filtertags are defined, use them to filter the entries by RSS category
if (!empty($externalblog->filtertags)) {
$containsfiltertag = false;
$categories = $entry->get_categories();
$filtertags = explode(',', $externalblog->filtertags);
$filtertags = array_map('trim', $filtertags);
$filtertags = array_map('strtolower', $filtertags);
foreach ($categories as $category) {
if (in_array(trim(strtolower($category->term)), $filtertags)) {
$containsfiltertag = true;
}
}
if (!$containsfiltertag) {
continue;
}
}
$newentry = new object();
$newentry->userid = $externalblog->userid;
$newentry->module = 'blog_external';
$newentry->content = $externalblog->id;
$newentry->uniquehash = $entry->get_permalink();
$newentry->publishstate = 'site';
$newentry->format = FORMAT_HTML;
$newentry->subject = $entry->get_title();
$newentry->summary = $entry->get_description();
$newentry->created = $entry->get_date('U');
$newentry->lastmodified = $entry->get_date('U');
$id = $DB->insert_record('post', $newentry);
// Set tags
if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) {
tag_set('post', $id, $tags);
}
}
$DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => mktime()));
}
/**
* Given an external blog object, deletes all related blog entries from the post table.
* NOTE: The external blog's id is saved as post.content, a field that is not oterhwise used by blog entries.
* @param object $externablog
*/
function blog_delete_external_entries($externalblog) {
global $DB;
require_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM));
$DB->delete_records('post', array('content' => $externalblog->id, 'module' => 'blog_external'));
}
/**
* Returns a URL based on the context of the current page.
* This URL points to blog/index.php and includes filter parameters appropriate for the current page.
*
* @param stdclass $context
* @return string
*/
function blog_get_context_url($context=null) {
global $CFG;
$viewblogentriesurl = new moodle_url('/blog/index.php');
if (empty($context)) {
global $PAGE;
$context = $PAGE->context;
}
// Change contextlevel to SYSTEM if viewing the site course
if ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID) {
$context->contextlevel = CONTEXT_SYSTEM;
}
$filterparam = '';
$strlevel = '';
switch ($context->contextlevel) {
case CONTEXT_SYSTEM:
case CONTEXT_BLOCK:
case CONTEXT_COURSECAT:
break;
case CONTEXT_COURSE:
$filterparam = 'courseid';
$strlevel = get_string('course');
break;
case CONTEXT_MODULE:
$filterparam = 'modid';
$strlevel = print_context_name($context);
break;
case CONTEXT_USER:
$filterparam = 'userid';
$strlevel = get_string('user');
break;
}
if (!empty($filterparam)) {
$viewblogentriesurl->param($filterparam, $context->instanceid);
}
return $viewblogentriesurl;
}
/**
* This function encapsulates all the logic behind the complex
* navigation, titles and headings of the blog listing page, depending
* on URL params. It looks at URL params and at the current context level.
* It builds and returns an array containing:
*
* 1. heading: The heading displayed above the blog entries
* 2. stradd: The text to be used as the "Add entry" link
* 3. strview: The text to be used as the "View entries" link
* 4. url: The moodle_url object used as the base for add and view links
* 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block
*
* All other variables are set directly in $PAGE
*
* It uses the current URL to build these variables.
* A number of mutually exclusive use cases are used to structure this function.
*
* @return array
*/
function blog_get_headers() {
global $CFG, $PAGE, $DB, $USER;
$id = optional_param('id', null, PARAM_INT);
$tag = optional_param('tag', null, PARAM_NOTAGS);
$tagid = optional_param('tagid', null, PARAM_INT);
$userid = optional_param('userid', null, PARAM_INT);
$modid = optional_param('modid', null, PARAM_INT);
$entryid = optional_param('entryid', null, PARAM_INT);
$groupid = optional_param('groupid', null, PARAM_INT);
$courseid = optional_param('courseid', null, PARAM_INT);
$search = optional_param('search', null, PARAM_RAW);
$action = optional_param('action', null, PARAM_ALPHA);
$confirm = optional_param('confirm', false, PARAM_BOOL);
// Ignore userid when action == add
if ($action == 'add' && $userid) {
unset($userid);
$PAGE->url->remove_params(array('userid'));
} else if ($action == 'add' && $entryid) {
unset($entryid);
$PAGE->url->remove_params(array('entryid'));
}
$headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
$blogurl = new moodle_url('/blog/index.php');
// If the title is not yet set, it's likely that the context isn't set either, so skip this part
$pagetitle = $PAGE->title;
if (!empty($pagetitle)) {
$contexturl = blog_get_context_url();
// Look at the context URL, it may have additional params that are not in the current URL
if (!$blogurl->compare($contexturl)) {
$blogurl = $contexturl;
if (empty($courseid)) {
$courseid = $blogurl->param('courseid');
}
if (empty($modid)) {
$modid = $blogurl->param('modid');
}
}
}
$headers['stradd'] = get_string('addnewentry', 'blog');
$headers['strview'] = null;
$site = $DB->get_record('course', array('id' => SITEID));
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
// Common Lang strings
$strparticipants = get_string("participants");
$strblogentries = get_string("blogentries", 'blog');
// Prepare record objects as needed
if (!empty($courseid)) {
$headers['filters']['course'] = $courseid;
$course = $DB->get_record('course', array('id' => $courseid));
}
if (!empty($userid)) {
$headers['filters']['user'] = $userid;
$user = $DB->get_record('user', array('id' => $userid));
}
if (!empty($groupid)) { // groupid always overrides courseid
$headers['filters']['group'] = $groupid;
$group = $DB->get_record('groups', array('id' => $groupid));
$course = $DB->get_record('course', array('id' => $group->courseid));
}
if (!empty($modid) && $CFG->useblogassociations && has_capability('moodle/blog:associatemodule', $sitecontext)) { // modid always overrides courseid, so the $course object may be reset here
$headers['filters']['module'] = $modid;
// A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case
$courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
$course = $DB->get_record('course', array('id' => $courseid));
$cm = $DB->get_record('course_modules', array('id' => $modid));
$cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module));
$cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance));
$cm->context = get_context_instance(CONTEXT_MODULE, $modid);
$a->type = get_string('modulename', $cm->modname);
$PAGE->set_cm($cm, $course);
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
$headers['strview'] = get_string('viewallmodentries', 'blog', $a);
}
// Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
// Note: if action is set to 'add' or 'edit', we do this at the end
if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
$PAGE->navbar->add($strblogentries, $blogurl);
$PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
$PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
$headers['heading'] = get_string('siteblog', 'blog');
// $headers['strview'] = get_string('viewsiteentries', 'blog');
}
// Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information
if (!empty($entryid)) {
$headers['filters']['entry'] = $entryid;
$sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id';
$user = $DB->get_record_sql($sql, array($entryid));
$entry = $DB->get_record('post', array('id' => $entryid));
$blogurl->param('userid', $user->id);
if (!empty($course)) {
$mycourseid = $course->id;
$blogurl->param('courseid', $mycourseid);
} else {
$mycourseid = $site->id;
}
$PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$mycourseid");
$PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
$PAGE->navbar->add($strblogentries, $blogurl);
$blogurl->remove_params('userid');
$PAGE->navbar->add($entry->subject, $blogurl);
$PAGE->set_title("$site->shortname: " . fullname($user) . ": $entry->subject");
$PAGE->set_heading("$site->shortname: " . fullname($user) . ": $entry->subject");
$headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user));
// We ignore tag and search params
if (empty($action) || !$CFG->useblogassociations) {
$headers['url'] = $blogurl;
return $headers;
}
}
// Case 3: A user's blog entries
if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
$blogurl->param('userid', $userid);
$PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$site->id");
$PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
$PAGE->navbar->add($strblogentries, $blogurl);
$PAGE->set_title("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
$PAGE->set_heading("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
$headers['heading'] = get_string('userblog', 'blog', fullname($user));
$headers['strview'] = get_string('viewuserentries', 'blog');
} else
// Case 4: No blog associations, no userid
if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) {
$PAGE->navbar->add($strblogentries, $blogurl);
$PAGE->set_title("$site->shortname: " . get_string('blog', 'blog'));
$PAGE->set_heading("$site->shortname: " . get_string('blog', 'blog'));
$headers['heading'] = get_string('siteblog', 'blog');
} else
// Case 5: Blog entries associated with an activity by a specific user (courseid ignored)
if (!empty($userid) && !empty($modid) && empty($entryid)) {
$blogurl->param('userid', $userid);
$blogurl->param('modid', $modid);
// Course module navigation is handled by build_navigation as the second param
$headers['cm'] = $cm;
$PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
$PAGE->navbar->add($strblogentries, $blogurl);
$PAGE->set_title("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
$PAGE->set_heading("$site->shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
$a->user = fullname($user);
$a->mod = $cm->name;
$a->type = get_string('modulename', $cm->modname);
$headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
$headers['strview'] = get_string('viewallmodentries', 'blog', $a);
} else
// Case 6: Blog entries associated with a course by a specific user
if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
$blogurl->param('userid', $userid);
$blogurl->param('courseid', $courseid);
$PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$course->id");
$PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
$PAGE->navbar->add($strblogentries, $blogurl);
$PAGE->set_title("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
$PAGE->set_heading("$site->shortname: $course->shortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
$a->user = fullname($user);
$a->course = $course->fullname;
$a->type = get_string('course');
$headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
$headers['strview'] = get_string('viewblogentries', 'blog', $a);
// Remove the userid from the URL to inform the blog_menu block correctly
$blogurl->remove_params(array('userid'));
} else
// Case 7: Blog entries by members of a group, associated with that group's course
if (!empty($groupid) && empty($modid) && empty($entryid)) {
$blogurl->param('courseid', $course->id);
$PAGE->navbar->add($strblogentries, $blogurl);
$blogurl->remove_params(array('courseid'));
$blogurl->param('groupid', $groupid);
$PAGE->navbar->add($group->name, $blogurl);
$PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
$PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog') . ": $group->name");
$a->group = $group->name;
$a->course = $course->fullname;
$a->type = get_string('course');
$headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
$headers['strview'] = get_string('viewblogentries', 'blog', $a);
} else
// Case 8: Blog entries by members of a group, associated with an activity in that course
if (!empty($groupid) && !empty($modid) && empty($entryid)) {
$headers['cm'] = $cm;
$blogurl->param('modid', $modid);
$PAGE->navbar->add($strblogentries, $blogurl);
$blogurl->param('groupid', $groupid);
$PAGE->navbar->add($group->name, $blogurl);
$PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
$PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
$a->group = $group->name;
$a->mod = $cm->name;
$a->type = get_string('modulename', $cm->modname);
$headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
$headers['strview'] = get_string('viewallmodentries', 'blog', $a);
} else
// Case 9: All blog entries associated with an activity
if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
$PAGE->set_cm($cm, $course);
$blogurl->param('modid', $modid);
$PAGE->navbar->add($strblogentries, $blogurl);
$PAGE->set_title("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
$PAGE->set_heading("$site->shortname: $course->shortname: $cm->name: " . get_string('blogentries', 'blog'));
$headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
$a->type = get_string('modulename', $cm->modname);
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
$headers['strview'] = get_string('viewallmodentries', 'blog', $a);
} else
// Case 10: All blog entries associated with a course
if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
$blogurl->param('courseid', $courseid);
$PAGE->navbar->add($strblogentries, $blogurl);
$PAGE->set_title("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
$PAGE->set_heading("$site->shortname: $course->shortname: " . get_string('blogentries', 'blog'));
$a->type = get_string('course');
$headers['heading'] = get_string('blogentriesabout', 'blog', $course->fullname);
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
$headers['strview'] = get_string('viewblogentries', 'blog', $a);
$blogurl->remove_params(array('userid'));
}
if (!in_array($action, array('edit', 'add'))) {
// Append Tag info
if (!empty($tagid)) {
$headers['filters']['tag'] = $tagid;
$blogurl->param('tagid', $tagid);
$tagrec = $DB->get_record('tag', array('id'=>$tagid));
$PAGE->navbar->add($tagrec->name, $blogurl);
} elseif (!empty($tag)) {
$blogurl->param('tag', $tag);
$PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl);
}
// Append Search info
if (!empty($search)) {
$headers['filters']['search'] = $search;
$blogurl->param('search', $search);
$PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
}
}
// Append edit mode info
if (!empty($action) && $action == 'add') {
if (empty($modid) && empty($courseid)) {
if (empty($user)) {
$user = $USER;
}
$PAGE->navbar->add($strparticipants, "$CFG->wwwroot/user/index.php?id=$site->id");
$PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
$blogurl->param('userid', $user->id);
$PAGE->navbar->add($strblogentries, $blogurl);
}
$PAGE->navbar->add(get_string('addnewentry', 'blog'));
} else if (!empty($action) && $action == 'edit') {
$PAGE->navbar->add(get_string('editentry', 'blog'));
}
if (empty($headers['url'])) {
$headers['url'] = $blogurl;
}
return $headers;
}
/**
* Function used by the navigation system to provide links to blog preferences and external blogs.
* @param object $settingsnav The settings_navigation object
* @return navigation key
*/
function blog_extend_settings_navigation($settingsnav) {
global $USER, $PAGE, $FULLME, $CFG, $DB, $OUTPUT;
$blogkey = $settingsnav->add(get_string('blogadministration', 'blog'));
$blog = $settingsnav->get($blogkey);
$blog->forceopen = true;
$blog->add(get_string('preferences', 'blog'), new moodle_url('preferences.php'), navigation_node::TYPE_SETTING);
if ($CFG->useexternalblogs && $CFG->maxexternalblogsperuser > 0 && has_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM))) {
$blog->add(get_string('externalblogs', 'blog'), new moodle_url('external_blogs.php'), navigation_node::TYPE_SETTING);
}
return $blogkey;
}
/**
* Shortcut function for getting a count of blog entries associated with a course or a module
* @param int $courseid The ID of the course
* @param int $cmid The ID of the course_modules
* @return string The number of associated entries
*/
function blog_get_associated_count($courseid, $cmid=null) {
global $DB;
$context = get_context_instance(CONTEXT_COURSE, $courseid);
if ($cmid) {
$context = get_context_instance(CONTEXT_MODULE, $cmid);
}
return $DB->count_records('blog_association', array('contextid' => $context->id));
}