MDL-14303, add missing cmidnumber parameter. see tracker
[moodle-linuxchix.git] / blog / lib.php
blob0946cdbcdef571059f642f3589308dddbfa19dac
1 <?php //$Id$
3 /**
4 * Library of functions and constants for blog
5 */
6 require_once($CFG->libdir .'/blocklib.php');
7 require_once($CFG->libdir .'/pagelib.php');
8 require_once($CFG->dirroot .'/blog/rsslib.php');
9 require_once($CFG->dirroot .'/blog/blogpage.php');
10 require_once($CFG->dirroot.'/tag/lib.php');
12 /**
13 * Definition of blogcourse page type (blog page with course id present).
15 //not used at the moment, and may not need to be
16 define('PAGE_BLOG_COURSE_VIEW', 'blog_course-view');
19 /**
20 * Checks to see if user has visited blogpages before, if not, install 2
21 * default blocks (blog_menu and blog_tags).
23 function blog_check_and_install_blocks() {
24 global $USER;
25 if (isloggedin() && !isguest()) {
26 // if this user has not visited this page before
27 if (!get_user_preferences('blogpagesize')) {
28 // find the correct ids for blog_menu and blog_from blocks
29 $menublock = get_record('block','name','blog_menu');
30 $tagsblock = get_record('block','name','blog_tags');
31 // add those 2 into block_instance page
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 insert_record('block_instance', $newblock);
43 // add blog_tags menu
44 $newblock -> blockid = $tagsblock->id;
45 $newblock -> weight = 1;
46 insert_record('block_instance', $newblock);
48 // finally we set the page size pref
49 set_user_preference('blogpagesize', 10);
55 /**
56 * Adaptation of isediting in moodlelib.php for blog module
57 * @return bool
59 function blog_isediting() {
60 global $SESSION;
62 return !empty($SESSION->blog_editing_enabled);
66 /**
67 * This function is in lib and not in BlogInfo because entries being searched
68 * might be found in any number of blogs rather than just one.
70 * $@param ...
72 function blog_print_html_formatted_entries($postid, $filtertype, $filterselect, $tagid, $tag) {
74 global $CFG, $USER;
76 $blogpage = optional_param('blogpage', 0, PARAM_INT);
77 $bloglimit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT);
78 $start = $blogpage * $bloglimit;
80 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
82 $morelink = '<br />&nbsp;&nbsp;';
84 $totalentries = get_viewable_entry_count($postid, $bloglimit, $start, $filtertype, $filterselect, $tagid, $tag, $sort='lastmodified DESC');
85 $blogEntries = blog_fetch_entries($postid, $bloglimit, $start, $filtertype, $filterselect, $tagid, $tag, $sort='lastmodified DESC', true);
87 print_paging_bar($totalentries, $blogpage, $bloglimit, get_baseurl($filtertype, $filterselect), 'blogpage');
89 if ($CFG->enablerssfeeds) {
90 blog_rss_print_link($filtertype, $filterselect, $tag);
93 if (has_capability('moodle/blog:create', $sitecontext)) {
94 //the user's blog is enabled and they are viewing their own blog
95 $addlink = '<div class="addbloglink">';
96 $addlink .= '<a href="'.$CFG->wwwroot .'/blog/edit.php?action=add'.'">'. get_string('addnewentry', 'blog').'</a>';
97 $addlink .= '</div>';
98 echo $addlink;
101 if ($blogEntries) {
103 $count = 0;
104 foreach ($blogEntries as $blogEntry) {
105 blog_print_entry($blogEntry, 'list', $filtertype, $filterselect); //print this entry.
106 $count++;
109 print_paging_bar($totalentries, $blogpage, $bloglimit, get_baseurl($filtertype, $filterselect), 'blogpage');
111 if (!$count) {
112 print '<br /><div style="text-align:center">'. get_string('noentriesyet', 'blog') .'</div><br />';
116 print $morelink.'<br />'."\n";
117 return;
120 $output = '<br /><div style="text-align:center">'. get_string('noentriesyet', 'blog') .'</div><br />';
122 print $output;
128 * This function is in lib and not in BlogInfo because entries being searched
129 * might be found in any number of blogs rather than just one.
131 * This function builds an array which can be used by the included
132 * template file, making predefined and nicely formatted variables available
133 * to the template. Template creators will not need to become intimate
134 * with the internal objects and vars of moodle blog nor will they need to worry
135 * about properly formatting their data
137 * @param BlogEntry blogEntry - a hopefully fully populated BlogEntry object
138 * @param string viewtype Default is 'full'. If 'full' then display this blog entry
139 * in its complete form (eg. archive page). If anything other than 'full'
140 * display the entry in its abbreviated format (eg. index page)
142 function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filterselect='', $mode='loud') {
144 global $USER, $CFG, $COURSE, $ME;
146 $template['body'] = format_text($blogEntry->summary, $blogEntry->format);
147 //$template['title'] = '<a name="'. $blogEntry->subject .'"></a>';
148 //enclose the title in nolink tags so that moodle formatting doesn't autolink the text
149 $template['title'] = '<span class="nolink">'.$blogEntry->subject.'</span>';
150 $template['userid'] = $blogEntry->userid;
151 $template['author'] = fullname(get_record('user','id',$blogEntry->userid));
152 $template['lastmod'] = userdate($blogEntry->lastmodified);
153 $template['created'] = userdate($blogEntry->created);
154 $template['publishstate'] = $blogEntry->publishstate;
156 /// preventing user to browse blogs that they aren't supposed to see
157 /// This might not be too good since there are multiple calls per page
160 if (!blog_user_can_view_user_post($template['userid'])) {
161 error ('you can not view this post');
164 $stredit = get_string('edit');
165 $strdelete = get_string('delete');
167 $user = get_record('user','id',$template['userid']);
169 /// Start printing of the blog
171 echo '<table cellspacing="0" class="forumpost blogpost blog'.$template['publishstate'].'" width="100%">';
173 echo '<tr class="header"><td class="picture left">';
174 print_user_picture($user, SITEID, $user->picture);
175 echo '</td>';
177 echo '<td class="topic starter"><div class="subject">'.$template['title'].'</div><div class="author">';
178 $fullname = fullname($user, $template['userid']);
179 $by = new object();
180 $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
181 $user->id.'&amp;course='.$COURSE->id.'">'.$fullname.'</a>';
182 $by->date = $template['lastmod'];
183 print_string('bynameondate', 'forum', $by);
184 echo '</div></td></tr>';
186 echo '<tr><td class="left side">';
188 /// Actual content
190 echo '</td><td class="content">'."\n";
192 if ($blogEntry->attachment) {
193 echo '<div class="attachments">';
194 $attachedimages = blog_print_attachments($blogEntry);
195 echo '</div>';
196 } else {
197 $attachedimages = '';
200 switch ($template['publishstate']) {
201 case 'draft':
202 $blogtype = get_string('publishtonoone', 'blog');
203 break;
204 case 'site':
205 $blogtype = get_string('publishtosite', 'blog');
206 break;
207 case 'public':
208 $blogtype = get_string('publishtoworld', 'blog');
209 break;
210 default:
211 $blogtype = '';
212 break;
216 echo '<div class="audience">'.$blogtype.'</div>';
218 // Print whole message
219 echo format_text($template['body']);
221 /// Print attachments
222 echo $attachedimages;
223 /// Links to tags
225 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_csv('post', $blogEntry->id)) ) {
226 echo '<div class="tags">';
227 if ($blogtags) {
228 print(get_string('tags', 'tag') .': '. $blogtags);
230 echo '</div>';
233 /// Commands
235 echo '<div class="commands">';
237 if (blog_user_can_edit_post($blogEntry)) {
238 echo '<a href="'.$CFG->wwwroot.'/blog/edit.php?action=edit&amp;id='.$blogEntry->id.'">'.$stredit.'</a>';
239 echo '| <a href="'.$CFG->wwwroot.'/blog/edit.php?action=delete&amp;id='.$blogEntry->id.'">'.$strdelete.'</a> | ';
242 echo '<a href="'.$CFG->wwwroot.'/blog/index.php?postid='.$blogEntry->id.'">'.get_string('permalink', 'blog').'</a>';
244 echo '</div>';
246 echo '</td></tr></table>'."\n\n";
250 function blog_file_area_name($blogentry) {
251 // Creates a directory file name, suitable for make_upload_directory()
252 global $CFG;
253 // $CFG->dataroot/blog/attachments/xxxx/file.jpg
254 return "blog/attachments/$blogentry->id";
257 function blog_file_area($blogentry) {
258 return make_upload_directory( blog_file_area_name($blogentry) );
261 function blog_delete_old_attachments($post, $exception="") {
262 // Deletes all the user files in the attachments area for a post
263 // EXCEPT for any file named $exception
265 if ($basedir = blog_file_area($post)) {
266 if ($files = get_directory_list($basedir)) {
267 foreach ($files as $file) {
268 if ($file != $exception) {
269 unlink("$basedir/$file");
270 notify("Existing file '$file' has been deleted!");
274 if (!$exception) { // Delete directory as well, if empty
275 rmdir("$basedir");
280 function blog_print_attachments($blogentry, $return=NULL) {
281 // if return=html, then return a html string.
282 // if return=text, then return a text-only string.
283 // otherwise, print HTML for non-images, and return image HTML
285 global $CFG;
287 $filearea = blog_file_area_name($blogentry);
289 $imagereturn = "";
290 $output = "";
292 if ($basedir = blog_file_area($blogentry)) {
293 if ($files = get_directory_list($basedir)) {
294 $strattachment = get_string("attachment", "forum");
295 foreach ($files as $file) {
296 include_once($CFG->libdir.'/filelib.php');
297 $icon = mimeinfo("icon", $file);
298 $type = mimeinfo("type", $file);
299 if ($CFG->slasharguments) {
300 $ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
301 } else {
302 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
304 $image = "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"\" />";
306 if ($return == "html") {
307 $output .= "<a href=\"$ffurl\">$image</a> ";
308 $output .= "<a href=\"$ffurl\">$file</a><br />";
310 } else if ($return == "text") {
311 $output .= "$strattachment $file:\n$ffurl\n";
313 } else {
314 if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) { // Image attachments don't get printed as links
315 $imagereturn .= "<br /><img src=\"$ffurl\" alt=\"\" />";
316 } else {
317 echo "<a href=\"$ffurl\">$image</a> ";
318 echo filter_text("<a href=\"$ffurl\">$file</a><br />");
325 if ($return) {
326 return $output;
329 return $imagereturn;
334 * Use this function to retrieve a list of publish states available for
335 * the currently logged in user.
337 * @return array This function returns an array ideal for sending to moodles'
338 * choose_from_menu function.
340 function blog_applicable_publish_states($courseid='') {
342 global $CFG;
344 // everyone gets draft access
345 if ($CFG->bloglevel >= BLOG_USER_LEVEL) {
346 $options = array ( 'draft' => get_string('publishtonoone', 'blog') );
349 if ($CFG->bloglevel > BLOG_USER_LEVEL) {
350 $options['site'] = get_string('publishtosite', 'blog');
353 if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) {
354 $options['public'] = get_string('publishtoworld', 'blog');
357 return $options;
362 * User can edit a blog entry if this is their own blog post and they have
363 * the capability moodle/blog:create, or if they have the capability
364 * moodle/blog:manageentries.
366 * This also applies to deleting of posts.
368 function blog_user_can_edit_post($blogEntry) {
370 global $CFG, $USER;
372 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
374 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
375 return true; // can edit any blog post
378 if ($blogEntry->userid == $USER->id
379 and has_capability('moodle/blog:create', $sitecontext)) {
380 return true; // can edit own when having blog:create capability
383 return false;
388 * Checks to see if a user can view the blogs of another user.
389 * Only blog level is checked here, the capabilities are enforced
390 * in blog/index.php
392 function blog_user_can_view_user_post($targetuserid, $blogEntry=null) {
393 global $CFG, $USER;
395 if (empty($CFG->bloglevel)) {
396 return false; // blog system disabled
399 if (!empty($USER->id) and $USER->id == $targetuserid) {
400 return true; // can view own posts in any case
403 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
404 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
405 return true; // can manage all posts
408 // coming for 1 post, make sure it's not a draft
409 if ($blogEntry and $blogEntry->publishstate == 'draft') {
410 return false; // can not view draft of others
413 // coming for 1 post, make sure user is logged in, if not a public blog
414 if ($blogEntry && $blogEntry->publishstate != 'public' && !isloggedin()) {
415 return false;
418 switch ($CFG->bloglevel) {
419 case BLOG_GLOBAL_LEVEL:
420 return true;
421 break;
423 case BLOG_SITE_LEVEL:
424 if (!empty($USER->id)) { // not logged in viewers forbidden
425 return true;
427 return false;
428 break;
430 case BLOG_COURSE_LEVEL:
431 $mycourses = array_keys(get_my_courses($USER->id));
432 $usercourses = array_keys(get_my_courses($targetuserid));
433 $shared = array_intersect($mycourses, $usercourses);
434 if (!empty($shared)) {
435 return true;
437 return false;
438 break;
440 case BLOG_GROUP_LEVEL:
441 $mycourses = array_keys(get_my_courses($USER->id));
442 $usercourses = array_keys(get_my_courses($targetuserid));
443 $shared = array_intersect($mycourses, $usercourses);
444 foreach ($shared as $courseid) {
445 $course = get_record('course', 'id', $courseid);
446 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
447 if (has_capability('moodle/site:accessallgroups', $coursecontext)
448 or groups_get_course_groupmode($course) != SEPARATEGROUPS) {
449 return true;
450 } else {
451 if ($usergroups = groups_get_all_groups($courseid, $targetuserid)) {
452 foreach ($usergroups as $usergroup) {
453 if (groups_is_member($usergroup->id)) {
454 return true;
460 return false;
461 break;
463 case BLOG_USER_LEVEL:
464 default:
465 $personalcontext = get_context_instance(CONTEXT_USER, $targetuserid);
466 return has_capability('moodle/user:readuserblogs', $personalcontext);
467 break;
474 * Main filter function.
476 function blog_fetch_entries($postid='', $fetchlimit=10, $fetchstart='', $filtertype='', $filterselect='', $tagid='', $tag ='', $sort='lastmodified DESC', $limit=true) {
478 global $CFG, $USER;
480 /// the post table will be used for other things too
481 $typesql = " AND p.module = 'blog' ";
483 /// set the tag id for searching
484 if ($tagid) {
485 $tag = $tagid;
486 } else if ($tag) {
487 if ($tagrec = get_record_sql('SELECT * FROM '.$CFG->prefix.'tag WHERE name LIKE "'.$tag.'"')) {
488 $tag = $tagrec->id;
489 } else {
490 $tag = -1; //no records found
494 // If we have specified an ID
495 // Just return 1 entry
497 if ($postid) {
499 if ($post = get_record('post', 'id', $postid)) {
501 if (blog_user_can_view_user_post($post->userid, $post)) {
503 if ($user = get_record('user', 'id', $post->userid)) {
504 $post->email = $user->email;
505 $post->firstname = $user->firstname;
506 $post->lastname = $user->lastname;
508 $retarray[] = $post;
509 return $retarray;
510 } else {
511 return null;
514 } else { // bad postid
515 return null;
519 if ($tag) {
520 $tagtablesql = $CFG->prefix.'tag_instance ti, ';
521 $tagquerysql = ' AND ti.itemid = p.id AND ti.tagid = '.$tag.' AND ti.itemtype = \'post\' ';
522 } else {
523 $tagtablesql = '';
524 $tagquerysql = '';
527 if (isloggedin() && !has_capability('moodle/legacy:guest', get_context_instance(CONTEXT_SYSTEM, SITEID), $USER->id, false)) {
528 $permissionsql = 'AND (p.publishstate = \'site\' OR p.publishstate = \'public\' OR p.userid = '.$USER->id.')';
529 } else {
530 $permissionsql = 'AND p.publishstate = \'public\'';
533 // fix for MDL-9165, use with readuserblogs capability in a user context can read that user's private blogs
534 // admins can see all blogs regardless of publish states, as described on the help page
535 if (has_capability('moodle/user:readuserblogs', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
536 $permissionsql = '';
537 } else if ($filtertype=='user' && has_capability('moodle/user:readuserblogs', get_context_instance(CONTEXT_USER, $filterselect))) {
538 $permissionsql = '';
540 /****************************************
541 * depending on the type, there are 4 *
542 * different possible sqls *
543 ****************************************/
545 $requiredfields = 'p.*, u.firstname,u.lastname,u.email';
547 if ($filtertype == 'course' && $filterselect == SITEID) { // Really a site
548 $filtertype = 'site';
551 switch ($filtertype) {
553 case 'site':
555 $SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql
556 .$CFG->prefix.'user u
557 WHERE p.userid = u.id '.$tagquerysql.'
558 AND u.deleted = 0
559 '.$permissionsql.$typesql;
561 break;
563 case 'course':
564 // all users with a role assigned
565 $context = get_context_instance(CONTEXT_COURSE, $filterselect);
567 // MDL-10037, hidden users' blogs should not appear
568 if (has_capability('moodle/role:viewhiddenassigns', $context)) {
569 $hiddensql = '';
570 } else {
571 $hiddensql = ' AND ra.hidden = 0 ';
574 $SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql
575 .$CFG->prefix.'role_assignments ra, '.$CFG->prefix.'user u
576 WHERE p.userid = ra.userid '.$tagquerysql.'
577 AND ra.contextid '.get_related_contexts_string($context).'
578 AND u.id = p.userid
579 AND u.deleted = 0
580 '.$hiddensql.$permissionsql.$typesql;
582 break;
584 case 'group':
586 $SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql
587 .$CFG->prefix.'groups_members gm, '.$CFG->prefix.'user u
588 WHERE p.userid = gm.userid AND u.id = p.userid '.$tagquerysql.'
589 AND gm.groupid = '.$filterselect.'
590 AND u.deleted = 0
591 '.$permissionsql.$typesql;
592 break;
594 case 'user':
596 $SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql
597 .$CFG->prefix.'user u
598 WHERE p.userid = u.id '.$tagquerysql.'
599 AND u.id = '.$filterselect.'
600 AND u.deleted = 0
601 '.$permissionsql.$typesql;
602 break;
605 $limitfrom = 0;
606 $limitnum = 0;
608 if ($fetchstart !== '' && $limit) {
609 $limitfrom = $fetchstart;
610 $limitnum = $fetchlimit;
613 $orderby = ' ORDER BY '. $sort .' ';
615 //global $db; $db->debug = true;
616 $records = get_records_sql($SQL . $orderby, $limitfrom, $limitnum);
617 //$db->debug = false;
619 if (empty($records)) {
620 return array();
623 return $records;
628 * get the count of viewable entries, easiest way is to count blog_fetch_entries
629 * this is used for print_paging_bar
630 * this is not ideal, but because of the UNION in the sql in blog_fetch_entries,
631 * it is hard to use count_records_sql
633 function get_viewable_entry_count($postid='', $fetchlimit=10,
634 $fetchstart='', $filtertype='', $filterselect='', $tagid='',
635 $tag ='', $sort='lastmodified DESC') {
637 $blogEntries = blog_fetch_entries($postid, $fetchlimit,
638 $fetchstart, $filtertype, $filterselect, $tagid, $tag,
639 $sort='lastmodified DESC', false);
641 return count($blogEntries);
645 /// Find the base url from $_GET variables, for print_paging_bar
646 function get_baseurl($filtertype, $filterselect) {
648 $getcopy = $_GET;
650 unset($getcopy['blogpage']);
652 $strippedurl = strip_querystring(qualified_me());
653 if(!empty($getcopy)) {
654 $first = false;
655 $querystring = '';
656 foreach($getcopy as $var => $val) {
657 if(!$first) {
658 $first = true;
659 if ($var != 'filterselect' && $var != 'filtertype') {
660 $querystring .= '?'.$var.'='.$val;
661 $hasparam = true;
662 } else {
663 $querystring .= '?';
665 } else {
666 if ($var != 'filterselect' && $var != 'filtertype') {
667 $querystring .= '&amp;'.$var.'='.$val;
668 $hasparam = true;
672 if (isset($hasparam)) {
673 $querystring .= '&amp;';
674 } else {
675 $querystring = '?';
677 } else {
678 $querystring = '?';
681 return strip_querystring(qualified_me()) . $querystring. 'filtertype='.
682 $filtertype.'&amp;filterselect='.$filterselect.'&amp;';
687 * Returns a list of all user ids who have used blogs in the site
688 * Used in backup of site courses.
690 function blog_get_participants() {
692 global $CFG;
694 return get_records_sql("SELECT userid as id
695 FROM {$CFG->prefix}post
696 WHERE module = 'blog'
697 AND courseid = 0");