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