moodle_page: MDL-12212 reimplement user_is_editing, deprecate isediting
[moodle.git] / blog / lib.php
CommitLineData
4a173181 1<?php //$Id$
2
c2ee4e87 3 /**
4 * Library of functions and constants for blog
5 */
c2ee4e87 6 require_once($CFG->libdir .'/blocklib.php');
7 require_once($CFG->libdir .'/pagelib.php');
e96f2a77 8 require_once($CFG->dirroot .'/blog/rsslib.php');
c2ee4e87 9 require_once($CFG->dirroot .'/blog/blogpage.php');
18ff5a61 10 require_once($CFG->dirroot.'/tag/lib.php');
bbbf2d40 11
c2ee4e87 12 /**
13 * Definition of blogcourse page type (blog page with course id present).
14 */
15 //not used at the moment, and may not need to be
16 define('PAGE_BLOG_COURSE_VIEW', 'blog_course-view');
17
c2ee4e87 18
e96f2a77 19 /**
bbbf2d40 20 * Checks to see if user has visited blogpages before, if not, install 2
21 * default blocks (blog_menu and blog_tags).
22 */
8c9814f9 23 function blog_check_and_install_blocks() {
45df7de3 24 global $USER, $DB;
25
8c9814f9 26 if (isloggedin() && !isguest()) {
27 // if this user has not visited this page before
28 if (!get_user_preferences('blogpagesize')) {
29 // find the correct ids for blog_menu and blog_from blocks
45df7de3 30 $menublock = $DB->get_record('block', array('name'=>'blog_menu'));
31 $tagsblock = $DB->get_record('block', array('name'=>'blog_tags'));
8c9814f9 32 // add those 2 into block_instance page
33
34 // add blog_menu block
e96f2a77 35 $newblock = new object();
d02240f3 36 $newblock->blockid = $menublock->id;
37 $newblock->pageid = $USER->id;
38 $newblock->pagetype = 'blog-view';
39 $newblock->position = 'r';
40 $newblock->weight = 0;
41 $newblock->visible = 1;
45df7de3 42 $DB->insert_record('block_instance', $newblock);
8c9814f9 43
44 // add blog_tags menu
45 $newblock -> blockid = $tagsblock->id;
e96f2a77 46 $newblock -> weight = 1;
45df7de3 47 $DB->insert_record('block_instance', $newblock);
8c9814f9 48
49 // finally we set the page size pref
e96f2a77 50 set_user_preference('blogpagesize', 10);
8c9814f9 51 }
52 }
53 }
54
55
c2ee4e87 56 /**
57 * This function is in lib and not in BlogInfo because entries being searched
58 * might be found in any number of blogs rather than just one.
59 *
6524adcf 60 * $@param ...
c2ee4e87 61 */
e96f2a77 62 function blog_print_html_formatted_entries($postid, $filtertype, $filterselect, $tagid, $tag) {
6524adcf 63
c2ee4e87 64 global $CFG, $USER;
4a173181 65
e96f2a77 66 $blogpage = optional_param('blogpage', 0, PARAM_INT);
67 $bloglimit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT);
68 $start = $blogpage * $bloglimit;
c2ee4e87 69
d9d16e56 70 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
4a173181 71
c2ee4e87 72 $morelink = '<br />&nbsp;&nbsp;';
d02240f3 73
18d330b0 74 $totalentries = get_viewable_entry_count($postid, $bloglimit, $start, $filtertype, $filterselect, $tagid, $tag, $sort='created DESC');
75 $blogEntries = blog_fetch_entries($postid, $bloglimit, $start, $filtertype, $filterselect, $tagid, $tag, $sort='created DESC', true);
2753336a 76
77 print_paging_bar($totalentries, $blogpage, $bloglimit, get_baseurl($filtertype, $filterselect), 'blogpage');
c2ee4e87 78
bd70af13 79 if ($CFG->enablerssfeeds) {
80 blog_rss_print_link($filtertype, $filterselect, $tag);
81 }
c2ee4e87 82
e96f2a77 83 if (has_capability('moodle/blog:create', $sitecontext)) {
c2ee4e87 84 //the user's blog is enabled and they are viewing their own blog
fbe31d22 85 $addlink = '<div class="addbloglink">';
e96f2a77 86 $addlink .= '<a href="'.$CFG->wwwroot .'/blog/edit.php?action=add'.'">'. get_string('addnewentry', 'blog').'</a>';
87 $addlink .= '</div>';
c2ee4e87 88 echo $addlink;
4a173181 89 }
90
8ae1314c 91 if ($blogEntries) {
c2ee4e87 92
93 $count = 0;
94 foreach ($blogEntries as $blogEntry) {
95 blog_print_entry($blogEntry, 'list', $filtertype, $filterselect); //print this entry.
96 $count++;
97 }
d02240f3 98
2753336a 99 print_paging_bar($totalentries, $blogpage, $bloglimit, get_baseurl($filtertype, $filterselect), 'blogpage');
d02240f3 100
c2ee4e87 101 if (!$count) {
038bf621 102 print '<br /><div style="text-align:center">'. get_string('noentriesyet', 'blog') .'</div><br />';
c2ee4e87 103
104 }
d02240f3 105
c2ee4e87 106 print $morelink.'<br />'."\n";
c2ee4e87 107 return;
4a173181 108 }
109
038bf621 110 $output = '<br /><div style="text-align:center">'. get_string('noentriesyet', 'blog') .'</div><br />';
d02240f3 111
112 print $output;
2753336a 113
4a173181 114 }
115
bbbf2d40 116
c2ee4e87 117 /**
bbbf2d40 118 * This function is in lib and not in BlogInfo because entries being searched
119 * might be found in any number of blogs rather than just one.
c2ee4e87 120 *
121 * This function builds an array which can be used by the included
122 * template file, making predefined and nicely formatted variables available
123 * to the template. Template creators will not need to become intimate
124 * with the internal objects and vars of moodle blog nor will they need to worry
125 * about properly formatting their data
126 *
127 * @param BlogEntry blogEntry - a hopefully fully populated BlogEntry object
128 * @param string viewtype Default is 'full'. If 'full' then display this blog entry
129 * in its complete form (eg. archive page). If anything other than 'full'
130 * display the entry in its abbreviated format (eg. index page)
131 */
132 function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filterselect='', $mode='loud') {
11e7b506 133 global $USER, $CFG, $COURSE, $DB;
c2ee4e87 134
b3cbe3a3 135 $template['body'] = format_text($blogEntry->summary, $blogEntry->format);
565b14d2 136 $template['title'] = '<a id="b'. s($blogEntry->id) .'" />';
c2ee4e87 137 //enclose the title in nolink tags so that moodle formatting doesn't autolink the text
a8db060b 138 $template['title'] .= '<span class="nolink">'. format_string($blogEntry->subject) .'</span>';
c2ee4e87 139 $template['userid'] = $blogEntry->userid;
45df7de3 140 $template['author'] = fullname($DB->get_record('user', array('id'=>$blogEntry->userid)));
c2ee4e87 141 $template['created'] = userdate($blogEntry->created);
18d330b0 142
143 if($blogEntry->created != $blogEntry->lastmodified){
144 $template['lastmod'] = userdate($blogEntry->lastmodified);
145 }
172dd12c 146
bd70af13 147 $template['publishstate'] = $blogEntry->publishstate;
9ffcd15f 148
149 /// preventing user to browse blogs that they aren't supposed to see
6524adcf 150 /// This might not be too good since there are multiple calls per page
151
152 /*
9ffcd15f 153 if (!blog_user_can_view_user_post($template['userid'])) {
771dc7b2 154 print_error('cannotviewuserblog', 'blog');
6524adcf 155 }*/
c2ee4e87 156
157 $stredit = get_string('edit');
158 $strdelete = get_string('delete');
4a173181 159
45df7de3 160 $user = $DB->get_record('user', array('id'=>$template['userid']));
4a173181 161
bd70af13 162 /// Start printing of the blog
163
9fad492e 164 echo '<table cellspacing="0" class="forumpost blogpost blog'.$template['publishstate'].'" width="100%">';
4a173181 165
c2ee4e87 166 echo '<tr class="header"><td class="picture left">';
65bcf17b 167 print_user_picture($user, SITEID, $user->picture);
c2ee4e87 168 echo '</td>';
4a173181 169
c2ee4e87 170 echo '<td class="topic starter"><div class="subject">'.$template['title'].'</div><div class="author">';
bbbf2d40 171 $fullname = fullname($user, $template['userid']);
e96f2a77 172 $by = new object();
c2ee4e87 173 $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
e96f2a77 174 $user->id.'&amp;course='.$COURSE->id.'">'.$fullname.'</a>';
18d330b0 175 $by->date = $template['created'];
c2ee4e87 176 print_string('bynameondate', 'forum', $by);
177 echo '</div></td></tr>';
4a173181 178
c2ee4e87 179 echo '<tr><td class="left side">';
4a173181 180
c2ee4e87 181 /// Actual content
4a173181 182
c2ee4e87 183 echo '</td><td class="content">'."\n";
d02240f3 184
7d0e5a95 185 if ($blogEntry->attachment) {
186 echo '<div class="attachments">';
187 $attachedimages = blog_print_attachments($blogEntry);
188 echo '</div>';
189 } else {
190 $attachedimages = '';
191 }
4a173181 192
f4c85f46 193 switch ($template['publishstate']) {
194 case 'draft':
195 $blogtype = get_string('publishtonoone', 'blog');
196 break;
197 case 'site':
198 $blogtype = get_string('publishtosite', 'blog');
199 break;
200 case 'public':
201 $blogtype = get_string('publishtoworld', 'blog');
202 break;
203 default:
83fb561f 204 $blogtype = '';
f4c85f46 205 break;
206
e96f2a77 207 }
f4c85f46 208
e96f2a77 209 echo '<div class="audience">'.$blogtype.'</div>';
f4c85f46 210
c2ee4e87 211 // Print whole message
522f26f1 212 echo $template['body'];
d02240f3 213
7d0e5a95 214 /// Print attachments
215 echo $attachedimages;
c2ee4e87 216 /// Links to tags
217
537aa65c 218 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_csv('post', $blogEntry->id)) ) {
83fb561f 219 echo '<div class="tags">';
220 if ($blogtags) {
3d535996 221 print(get_string('tags', 'tag') .': '. $blogtags);
222 }
83fb561f 223 echo '</div>';
172dd12c 224 }
4a173181 225
c2ee4e87 226 /// Commands
4a173181 227
c2ee4e87 228 echo '<div class="commands">';
4a173181 229
e96f2a77 230 if (blog_user_can_edit_post($blogEntry)) {
231 echo '<a href="'.$CFG->wwwroot.'/blog/edit.php?action=edit&amp;id='.$blogEntry->id.'">'.$stredit.'</a>';
b05c2da9 232 echo '| <a href="'.$CFG->wwwroot.'/blog/edit.php?action=delete&amp;id='.$blogEntry->id.'">'.$strdelete.'</a> | ';
4a173181 233 }
c2ee4e87 234
a814fc66 235 echo '<a href="'.$CFG->wwwroot.'/blog/index.php?postid='.$blogEntry->id.'">'.get_string('permalink', 'blog').'</a>';
b05c2da9 236
c2ee4e87 237 echo '</div>';
238
18d330b0 239 if( isset($template['lastmod']) ){
240 echo '<div style="font-size: 55%;">';
241 echo ' [ '.get_string('modified').': '.$template['lastmod'].' ]';
242 echo '</div>';
243 }
244
9fad492e 245 echo '</td></tr></table>'."\n\n";
bd70af13 246
4a173181 247 }
d02240f3 248
45df7de3 249 /**
250 * Deletes all the user files in the attachments area for a post
45df7de3 251 */
172dd12c 252 function blog_delete_attachments($post) {
253 $fs = get_file_storage();
254 $fs->delete_area_files(SYSCONTEXTID, 'blog', $post->id);
2878babd 255 }
7d0e5a95 256
45df7de3 257 /**
258 * if return=html, then return a html string.
259 * if return=text, then return a text-only string.
260 * otherwise, print HTML for non-images, and return image HTML
261 */
7d0e5a95 262 function blog_print_attachments($blogentry, $return=NULL) {
7d0e5a95 263 global $CFG;
264
172dd12c 265 require_once($CFG->libdir.'/filelib.php');
266
267 $fs = get_file_storage();
268 $browser = get_file_browser();
269
270 $files = $fs->get_area_files(SYSCONTEXTID, 'blog', $blogentry->id);
7d0e5a95 271
272 $imagereturn = "";
273 $output = "";
274
172dd12c 275 $strattachment = get_string("attachment", "forum");
7d0e5a95 276
172dd12c 277 foreach ($files as $file) {
278 if ($file->is_directory()) {
279 continue;
280 }
7d0e5a95 281
172dd12c 282 $filename = $file->get_filename();
283 $ffurl = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/'.$blogentry->id.'/'.$filename);
284 $type = $file->get_mimetype();
285 $icon = mimeinfo_from_type("icon", $type);
286 $type = mimeinfo_from_type("type", $type);
7d0e5a95 287
172dd12c 288 $image = "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"\" />";
289
290 if ($return == "html") {
291 $output .= "<a href=\"$ffurl\">$image</a> ";
292 $output .= "<a href=\"$ffurl\">$filename</a><br />";
293
294 } else if ($return == "text") {
295 $output .= "$strattachment $filename:\n$ffurl\n";
296
297 } else {
298 if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) { // Image attachments don't get printed as links
299 $imagereturn .= "<br /><img src=\"$ffurl\" alt=\"\" />";
300 } else {
301 echo "<a href=\"$ffurl\">$image</a> ";
302 echo filter_text("<a href=\"$ffurl\">$filename</a><br />");
7d0e5a95 303 }
304 }
305 }
306
307 if ($return) {
308 return $output;
309 }
310
311 return $imagereturn;
312 }
7d0e5a95 313
bbbf2d40 314
c2ee4e87 315 /**
316 * Use this function to retrieve a list of publish states available for
317 * the currently logged in user.
318 *
319 * @return array This function returns an array ideal for sending to moodles'
320 * choose_from_menu function.
321 */
322 function blog_applicable_publish_states($courseid='') {
323 global $CFG;
324
325 // everyone gets draft access
c345209c 326 if ($CFG->bloglevel >= BLOG_USER_LEVEL) {
327 $options = array ( 'draft' => get_string('publishtonoone', 'blog') );
328 }
d02240f3 329
c345209c 330 if ($CFG->bloglevel > BLOG_USER_LEVEL) {
331 $options['site'] = get_string('publishtosite', 'blog');
332 }
d02240f3 333
c345209c 334 if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) {
335 $options['public'] = get_string('publishtoworld', 'blog');
336 }
c2ee4e87 337
338 return $options;
339 }
340
6524adcf 341
bbbf2d40 342 /**
343 * User can edit a blog entry if this is their own blog post and they have
11c467d9 344 * the capability moodle/blog:create, or if they have the capability
bbbf2d40 345 * moodle/blog:manageentries.
e96f2a77 346 *
347 * This also applies to deleting of posts.
bbbf2d40 348 */
e96f2a77 349 function blog_user_can_edit_post($blogEntry) {
6524adcf 350 global $CFG, $USER;
e96f2a77 351
d9d16e56 352 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
e96f2a77 353
354 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
355 return true; // can edit any blog post
356 }
357
358 if ($blogEntry->userid == $USER->id
359 and has_capability('moodle/blog:create', $sitecontext)) {
360 return true; // can edit own when having blog:create capability
361 }
362
363 return false;
6524adcf 364 }
c2ee4e87 365
4a173181 366
bbbf2d40 367 /**
368 * Checks to see if a user can view the blogs of another user.
e96f2a77 369 * Only blog level is checked here, the capabilities are enforced
370 * in blog/index.php
bbbf2d40 371 */
372 function blog_user_can_view_user_post($targetuserid, $blogEntry=null) {
45df7de3 373 global $CFG, $USER, $DB;
e96f2a77 374
375 if (empty($CFG->bloglevel)) {
376 return false; // blog system disabled
6524adcf 377 }
378
e96f2a77 379 if (!empty($USER->id) and $USER->id == $targetuserid) {
380 return true; // can view own posts in any case
6524adcf 381 }
e96f2a77 382
d9d16e56 383 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
e96f2a77 384 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
385 return true; // can manage all posts
c2ee4e87 386 }
d02240f3 387
11b03793 388 // coming for 1 post, make sure it's not a draft
e96f2a77 389 if ($blogEntry and $blogEntry->publishstate == 'draft') {
390 return false; // can not view draft of others
4a173181 391 }
d02240f3 392
11b03793 393 // coming for 1 post, make sure user is logged in, if not a public blog
394 if ($blogEntry && $blogEntry->publishstate != 'public' && !isloggedin()) {
d02240f3 395 return false;
11b03793 396 }
c2ee4e87 397
e96f2a77 398 switch ($CFG->bloglevel) {
399 case BLOG_GLOBAL_LEVEL:
400 return true;
401 break;
4a173181 402
e96f2a77 403 case BLOG_SITE_LEVEL:
404 if (!empty($USER->id)) { // not logged in viewers forbidden
405 return true;
406 }
407 return false;
408 break;
409
410 case BLOG_COURSE_LEVEL:
6872debc 411 $mycourses = array_keys(get_my_courses($USER->id));
e96f2a77 412 $usercourses = array_keys(get_my_courses($targetuserid));
413 $shared = array_intersect($mycourses, $usercourses);
414 if (!empty($shared)) {
415 return true;
416 }
417 return false;
418 break;
419
420 case BLOG_GROUP_LEVEL:
6872debc 421 $mycourses = array_keys(get_my_courses($USER->id));
e96f2a77 422 $usercourses = array_keys(get_my_courses($targetuserid));
423 $shared = array_intersect($mycourses, $usercourses);
424 foreach ($shared as $courseid) {
45df7de3 425 $course = $DB->get_record('course', array('id'=>$courseid));
e96f2a77 426 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
427 if (has_capability('moodle/site:accessallgroups', $coursecontext)
ffc536af 428 or groups_get_course_groupmode($course) != SEPARATEGROUPS) {
e96f2a77 429 return true;
430 } else {
2c386f82 431 if ($usergroups = groups_get_all_groups($courseid, $targetuserid)) {
e96f2a77 432 foreach ($usergroups as $usergroup) {
2c386f82 433 if (groups_is_member($usergroup->id)) {
e96f2a77 434 return true;
435 }
436 }
437 }
438 }
439 }
440 return false;
441 break;
442
443 case BLOG_USER_LEVEL:
444 default:
445 $personalcontext = get_context_instance(CONTEXT_USER, $targetuserid);
446 return has_capability('moodle/user:readuserblogs', $personalcontext);
447 break;
4a173181 448
c2ee4e87 449 }
c2ee4e87 450 }
4a173181 451
c2ee4e87 452
bbbf2d40 453 /**
454 * Main filter function.
455 */
9133f939 456 function blog_fetch_entries($postid='', $fetchlimit=10, $fetchstart='', $filtertype='', $filterselect='', $tagid='', $tag ='', $sort='lastmodified DESC', $limit=true) {
45df7de3 457 global $CFG, $USER, $DB;
d02240f3 458
2ea75482 459 /// the post table will be used for other things too
45df7de3 460 $typesql = "AND p.module = 'blog'";
c2ee4e87 461
c2ee4e87 462 /// set the tag id for searching
463 if ($tagid) {
464 $tag = $tagid;
465 } else if ($tag) {
45df7de3 466 if ($tagrec = $DB->get_record_sql("SELECT * FROM {tag} WHERE name LIKE ?", array($tag))) {
c2ee4e87 467 $tag = $tagrec->id;
468 } else {
469 $tag = -1; //no records found
516194d0 470 }
c2ee4e87 471 }
472
473 // If we have specified an ID
474 // Just return 1 entry
b13ee30e 475
c2ee4e87 476 if ($postid) {
45df7de3 477 if ($post = $DB->get_record('post', array('id'=>$postid))) {
c2ee4e87 478
11b03793 479 if (blog_user_can_view_user_post($post->userid, $post)) {
c2ee4e87 480
45df7de3 481 if ($user = $DB->get_record('user', array('id'=>$post->userid))) {
14148bc2 482 $post->email = $user->email;
483 $post->firstname = $user->firstname;
484 $post->lastname = $user->lastname;
485 }
486 $retarray[] = $post;
487 return $retarray;
488 } else {
489 return null;
490 }
e96f2a77 491
b13ee30e 492 } else { // bad postid
493 return null;
c2ee4e87 494 }
495 }
496
45df7de3 497 $params = array();
498
c2ee4e87 499 if ($tag) {
45df7de3 500 $tagtablesql = ", {tag_instance} ti";
501 $tagquerysql = "AND ti.itemid = p.id AND ti.tagid = :tag AND ti.itemtype = 'post'";
502 $params['tag'] = $tag;
516194d0 503 } else {
c2ee4e87 504 $tagtablesql = '';
505 $tagquerysql = '';
506 }
507
d9d16e56 508 if (isloggedin() && !has_capability('moodle/legacy:guest', get_context_instance(CONTEXT_SYSTEM), $USER->id, false)) {
45df7de3 509 $permissionsql = "AND (p.publishstate = 'site' OR p.publishstate = 'public' OR p.userid = :userid)";
510 $params['userid'] = $USER->id;
1fcf3935 511 } else {
45df7de3 512 $permissionsql = "AND p.publishstate = 'public'";
1d934d47 513 }
d02240f3 514
1d934d47 515 // fix for MDL-9165, use with readuserblogs capability in a user context can read that user's private blogs
516 // admins can see all blogs regardless of publish states, as described on the help page
d9d16e56 517 if (has_capability('moodle/user:readuserblogs', get_context_instance(CONTEXT_SYSTEM))) {
d02240f3 518 $permissionsql = '';
1d934d47 519 } else if ($filtertype=='user' && has_capability('moodle/user:readuserblogs', get_context_instance(CONTEXT_USER, $filterselect))) {
d02240f3 520 $permissionsql = '';
1fcf3935 521 }
c2ee4e87 522 /****************************************
523 * depending on the type, there are 4 *
524 * different possible sqls *
525 ****************************************/
526
45df7de3 527 $requiredfields = "p.*, u.firstname,u.lastname,u.email";
c2ee4e87 528
1fcf3935 529 if ($filtertype == 'course' && $filterselect == SITEID) { // Really a site
530 $filtertype = 'site';
531 }
532
c2ee4e87 533 switch ($filtertype) {
534
535 case 'site':
536
45df7de3 537 $SQL = "SELECT $requiredfields
538 FROM {post} p, {user} u $tagtablesql
539 WHERE p.userid = u.id $tagquerysql
540 AND u.deleted = 0
541 $permissionsql $typesql";
c2ee4e87 542
543 break;
544
545 case 'course':
1fcf3935 546 // all users with a role assigned
547 $context = get_context_instance(CONTEXT_COURSE, $filterselect);
d02240f3 548
df6d3c66 549 // MDL-10037, hidden users' blogs should not appear
550 if (has_capability('moodle/role:viewhiddenassigns', $context)) {
d02240f3 551 $hiddensql = '';
552 } else {
45df7de3 553 $hiddensql = 'AND ra.hidden = 0';
df6d3c66 554 }
c2ee4e87 555
45df7de3 556 $SQL = "SELECT $requiredfields
557 FROM {post} p, {user} u, {role_assignments} ra $tagtablesql
558 WHERE p.userid = ra.userid $tagquerysql
559 AND ra.contextid ".get_related_contexts_string($context)."
560 AND u.id = p.userid
561 AND u.deleted = 0
562 $hiddensql $permissionsql $typesql";
c2ee4e87 563
564 break;
565
566 case 'group':
567
45df7de3 568 $SQL = "SELECT $requiredfields
569 FROM {post} p, {user} u, {groups_members} gm $tagtablesql
570 WHERE p.userid = gm.userid AND u.id = p.userid $tagquerysql
571 AND gm.groupid = :groupid
572 AND u.deleted = 0
573 $permissionsql $typesql";
574 $params['groupid'] = $filterselect;
c2ee4e87 575 break;
576
577 case 'user':
e96f2a77 578
45df7de3 579 $SQL = "SELECT $requiredfields
580 FROM {post} p, {user} u $tagtablesql
581 WHERE p.userid = u.id $tagquerysql
582 AND u.id = :uid
583 AND u.deleted = 0
584 $permissionsql $typesql";
585 $params['uid'] = $filterselect;
c2ee4e87 586 break;
c2ee4e87 587 }
588
422770d8 589 $limitfrom = 0;
590 $limitnum = 0;
591
c2ee4e87 592 if ($fetchstart !== '' && $limit) {
422770d8 593 $limitfrom = $fetchstart;
594 $limitnum = $fetchlimit;
516194d0 595 }
c2ee4e87 596
45df7de3 597 $orderby = "ORDER BY $sort";
c2ee4e87 598
45df7de3 599 $records = $DB->get_records_sql("$SQL $orderby", $params, $limitfrom, $limitnum);
c2ee4e87 600
601 if (empty($records)) {
602 return array();
603 }
604
605 return $records;
516194d0 606 }
607
bbbf2d40 608
c2ee4e87 609 /**
9133f939 610 * get the count of viewable entries, easiest way is to count blog_fetch_entries
c2ee4e87 611 * this is used for print_paging_bar
9133f939 612 * this is not ideal, but because of the UNION in the sql in blog_fetch_entries,
c7da4357 613 * it is hard to use $DB->count_records_sql
c2ee4e87 614 */
e96f2a77 615 function get_viewable_entry_count($postid='', $fetchlimit=10,
bbbf2d40 616 $fetchstart='', $filtertype='', $filterselect='', $tagid='',
617 $tag ='', $sort='lastmodified DESC') {
c2ee4e87 618
9133f939 619 $blogEntries = blog_fetch_entries($postid, $fetchlimit,
bbbf2d40 620 $fetchstart, $filtertype, $filterselect, $tagid, $tag,
621 $sort='lastmodified DESC', false);
e96f2a77 622
c2ee4e87 623 return count($blogEntries);
516194d0 624 }
bbbf2d40 625
626
6524adcf 627 /// Find the base url from $_GET variables, for print_paging_bar
c2ee4e87 628 function get_baseurl($filtertype, $filterselect) {
629
630 $getcopy = $_GET;
631
632 unset($getcopy['blogpage']);
633
634 $strippedurl = strip_querystring(qualified_me());
635 if(!empty($getcopy)) {
636 $first = false;
637 $querystring = '';
638 foreach($getcopy as $var => $val) {
639 if(!$first) {
640 $first = true;
641 if ($var != 'filterselect' && $var != 'filtertype') {
642 $querystring .= '?'.$var.'='.$val;
643 $hasparam = true;
644 } else {
645 $querystring .= '?';
646 }
647 } else {
648 if ($var != 'filterselect' && $var != 'filtertype') {
649 $querystring .= '&amp;'.$var.'='.$val;
650 $hasparam = true;
651 }
652 }
653 }
654 if (isset($hasparam)) {
655 $querystring .= '&amp;';
656 } else {
657 $querystring = '?';
658 }
659 } else {
660 $querystring = '?';
661 }
516194d0 662
bbbf2d40 663 return strip_querystring(qualified_me()) . $querystring. 'filtertype='.
664 $filtertype.'&amp;filterselect='.$filterselect.'&amp;';
c2ee4e87 665
666 }
240075cd 667
668 /**
669 * Returns a list of all user ids who have used blogs in the site
670 * Used in backup of site courses.
671 */
672 function blog_get_participants() {
45df7de3 673 global $CFG, $DB;
240075cd 674
172dd12c 675 return $DB->get_records_sql("SELECT userid AS id
45df7de3 676 FROM {post}
677 WHERE module = 'blog' AND courseid = 0");
240075cd 678 }
0468976c 679?>