Explicit link to home page
[moodle.git] / blog / lib.php
CommitLineData
4a173181 1<?php //$Id$
2
3/**
4 * Library of functions and constants for blog
5 */
752c0c4d 6
4a173181 7require_once($CFG->dirroot .'/blog/class.BlogInfo.php');
8require_once($CFG->dirroot .'/blog/class.BlogEntry.php');
9require_once($CFG->dirroot .'/blog/class.BlogFilter.php');
10require_once($CFG->libdir .'/blocklib.php');
11require_once($CFG->libdir .'/pagelib.php');
516194d0 12require_once('rsslib.php');
4a173181 13require_once($CFG->dirroot .'/blog/blogpage.php');
14
9154b440 15/* blog access level constant declaration */
16define ('BLOG_USER_LEVEL', 1);
17define ('BLOG_GROUP_LEVEL', 2);
18define ('BLOG_COURSE_LEVEL', 3);
19define ('BLOG_SITE_LEVEL', 4);
20define ('BLOG_GLOBAL_LEVEL', 5);
21
4a173181 22/**
23 * Definition of blogcourse page type (blog page with course id present).
24 */
25//not used at the moment, and may not need to be
26define('PAGE_BLOG_COURSE_VIEW', 'blog_course-view');
27
28$BLOG_YES_NO_MODES = array ( '0' => get_string('no'),
29 '1' => get_string('yes') );
30
31//set default setting for $CFG->blog_* vars used by blog's blocks
32//if they are not already. Otherwise errors are thrown
33//when an attempt is made to use an empty var.
34if (empty($SESSION->blog_editing_enabled)) {
35 $SESSION->blog_editing_enabled = false;
36}
4a173181 37
4a173181 38/**
39 * Verify that a user is logged in based on the session
40 * @return bool True if user has a valid login session
41 */
42function blog_isLoggedIn() {
43 global $USER;
44 if (!isguest() && isset($USER) and isset($USER->id) and $USER->id) {
45 return 1;
46 }
47 return 0;
48}
49
4a173181 50/**
51 * blog_user_has_rights - returns true if user is the blog's owner or a moodle admin.
52 *
53 * @param BlogInfo blogInfo - a BlogInfo object passed by reference. This object represents the blog being accessed.
54 * @param int uid - numeric user id of the user whose rights are being tested against this blogInfo. If no uid is specified then the uid of the currently logged in user will be used.
55 */
56function blog_user_has_rights(&$bloginfo, $uid='') {
57 global $USER;
58 if (isset($bloginfo) && isset($bloginfo->userid)) {
59 if ($uid == '') {
60 if ( isset($USER) && isset($USER->id) ) {
61 $uid = $USER->id;
62 }
63 }
64 if ($uid == '') {
65 //if uid is still empty then the user is not logged in
66 return false;
67 }
68 if (blog_is_blog_admin($uid) || isadmin()) {
69 return true;
70 }
71 }
72 return false;
73}
74
75/**
76 * Determines whether a user is an admin for a blog
77 * @param int $blog_userid The id of the blog being checked
78 */
79function blog_is_blog_admin($blog_userid) {
80 global $USER;
81
82 //moodle admins are admins
83 if (isadmin()) {
84 return true;
85 }
86 if ( empty($USER) || !isset($USER->id) ) {
87 return false;
88 }
89 if ( empty($blog_userid)) {
90 return true;
91 }
92
93 // Return true if the user is an admin for this blog
94 if ($blog_userid == $USER->id) {
95 return true;
96 } else {
97 return false;
98 }
99}
100
101/**
102 * Adaptation of isediting in moodlelib.php for blog module
103 * @return bool
104 */
105function blog_isediting() {
106 global $SESSION;
107 if (! isset($SESSION->blog_editing_enabled)) {
108 $SESSION->blog_editing_enabled = false;
109 }
110 return ($SESSION->blog_editing_enabled);
111}
112
113/**
924f20e2 114 * blog_user_bloginfo
4a173181 115 *
924f20e2 116 * returns a blogInfo object if the user has a blog in the acl table
117 * This function stores the currently logged in user's bloginfo object
118 * statically - do not release/unset the returned object.
119 * added by Daryl Hawes for moodle integration
120 * $userid - if no userid specified it will attempt to use the logged in user's id
4a173181 121 */
924f20e2 122function blog_user_bloginfo($userid='') {
123//Daryl Hawes note: not sure that this attempt at optimization is correct
124// static $bloginfosingleton; //store the logged in user's bloginfo in a static var
125 global $USER;
126 if ($userid == '') {
127 global $USER;
128 if (!isset($USER) || !isset($USER->id)) {
129 return;
4a173181 130 }
924f20e2 131 $userid = $USER->id;
4a173181 132 }
133
924f20e2 134 $thisbloginfo = new BlogInfo($userid);
135/* if (isset($USER) && $USER->id == $userid) {
136 $bloginfosingleton = $thisbloginfo;
137 }*/
138 return $thisbloginfo;
4a173181 139}
140
4a173181 141/**
142 * This function is in lib and not in BlogInfo because entries being searched
143 * might be found in any number of blogs rather than just one.
144 *
145 * $@param BlogFilter blogFilter - a BlogFilter object containing the settings for finding appropriate entries for display
146 */
147function blog_print_html_formatted_entries(&$blogFilter, $filtertype, $filterselect) {
148 global $CFG, $USER;
d7dfcee1 149
5c625033 150 $blogpage = optional_param('blogpage', 0, PARAM_INT);
4a173181 151 $bloglimit = get_user_preferences('blogpagesize',8); // expose as user pref when MyMoodle comes around
152
153 // First let's see if the batchpublish form has submitted data
154 $post = data_submitted();
4a173181 155
4a173181 156 $morelink = '<br />&nbsp;&nbsp;';
157 // show personal or general heading block as applicable
158 echo '<div class="headingblock header blog">';
159 //show blog title - blog tagline
160 print "<br />"; //don't print title. blog_get_title_text();
161
162 if ($blogpage != 0) {
163 // modify the blog filter to fetch the entries we care about right now
164 $oldstart = $blogFilter->fetchstart;
165 $blogFilter->fetchstart = $blogpage * $bloglimit;
166 unset($blogFilter->filtered_entries);
167 }
168 $blogEntries = $blogFilter->get_filtered_entries();
169 // show page next/previous links if applicable
170 print_paging_bar($blogFilter->get_viewable_entry_count(), $blogpage, $bloglimit, $blogFilter->baseurl, 'blogpage');
516194d0 171
490201c7 172 blog_rss_print_link($filtertype, $filterselect, $blogFilter->tag);
4a173181 173 print '</div>';
4fc47966 174
175 if (blog_isLoggedIn()) {
176 //the user's blog is enabled and they are viewing their own blog
177 $addlink = '<div align="center">';
178 $addlink .= $blogFilter->get_complete_link($CFG->wwwroot .'/blog/edit.php', get_string('addnewentry', 'blog'));
179 $addlink .='</div>';
180 echo $addlink;
181 }
182
4a173181 183 if (isset($blogEntries) ) {
184
4a173181 185 $count = 0;
186 foreach ($blogEntries as $blogEntry) {
187 blog_print_entry($blogEntry, 'list', $filtertype, $filterselect); //print this entry.
188 $count++;
189 }
190 if (!$count) {
191 print '<br /><center>'. get_string('noentriesyet', 'blog') .'</center><br />';
192
4a173181 193 }
194
195 print $morelink.'<br />'."\n";
196
197 if ($blogpage != 0) {
198 //put the blogFilter back the way we found it
199 $blogFilter->fetchstart = $oldstart;
200 unset($blogFilter->filtered_entries);
201 $blogFilter->fetch_entries();
202 }
203
204 return;
205 }
206
207 $output = '<br /><center>'. get_string('noentriesyet', 'blog') .'</center><br />';
4fc47966 208
4a173181 209 print $output;
210 unset($blogFilter->filtered_entries);
211}
212
4a173181 213/**
214 * This function is in lib and not in BlogInfo because entries being searched
215 * might be found in any number of blogs rather than just one.
216 *
217 * This function builds an array which can be used by the included
218 * template file, making predefined and nicely formatted variables available
219 * to the template. Template creators will not need to become intimate
220 * with the internal objects and vars of moodle blog nor will they need to worry
221 * about properly formatting their data
222 *
223 * @param BlogEntry blogEntry - a hopefully fully populated BlogEntry object
224 * @param string viewtype Default is 'full'. If 'full' then display this blog entry
225 * in its complete form (eg. archive page). If anything other than 'full'
226 * display the entry in its abbreviated format (eg. index page)
227 */
1e1ba8c2 228function blog_print_entry(&$blogEntry, $viewtype='full', $filtertype='', $filterselect='', $mode='loud') {
4a173181 229 global $CFG, $THEME, $USER;
230 static $bloginfoarray;
231
232 if (isset($bloginfoarray) && $bloginfocache[$blogEntry->entryuserid]) {
233 $bloginfo = $bloginfocache[$blogEntry->entryuserid];
234 } else {
235 $bloginfocache[$blogEntry->entryuserid] = new BlogInfo($blogEntry->entryuserid);
236 $bloginfo = $bloginfocache[$blogEntry->entryuserid];
237 }
4a173181 238
239 $template['body'] = $blogEntry->get_formatted_entry_body();
240 $template['countofextendedbody'] = 0;
241
4a173181 242 $template['title'] = '<a name="'. $blogEntry->entryId .'"></a>';
243 //enclose the title in nolink tags so that moodle formatting doesn't autolink the text
244 $template['title'] .= '<span class="nolink">'. stripslashes_safe($blogEntry->entryTitle);
245 $template['title'] .= '</span>';
246
247 // add editing controls if allowed
4a173181 248 $template['userid'] = $blogEntry->entryuserid;
4a173181 249 $template['author'] = $blogEntry->entryAuthorName;
250 $template['lastmod'] = $blogEntry->formattedEntryLastModified;
251 $template['created'] = $blogEntry->formattedEntryCreated;
252 $template['publishtomenu'] = $blogEntry->get_publish_to_menu(true, true);
4a173181 253 //forum style printing of blogs
1e1ba8c2 254 blog_print_entry_content ($template, $blogEntry->entryId, $filtertype, $filterselect, $mode);
4a173181 255
256}
257
258//forum style printing of blogs
1e1ba8c2 259function blog_print_entry_content ($template, $entryid, $filtertype='', $filterselect='', $mode='loud') {
4a173181 260 global $USER, $CFG, $course, $ME;
261
262 $stredit = get_string('edit');
263 $strdelete = get_string('delete');
264
265 $user = get_record('user','id',$template['userid']);
266
267 echo '<div align="center"><table cellspacing="0" class="forumpost" width="100%">';
268
269 echo '<tr class="header"><td class="picture left">';
20fc0f26 270 print_user_picture($template['userid'], SITEID, $user->picture);
4a173181 271 echo '</td>';
272
273 echo '<td class="topic starter"><div class="subject">'.$template['title'].'</div><div class="author">';
274 $fullname = fullname($user, isteacher($template['userid']));
275 $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
276 $user->id.'&amp;course='.$course->id.'">'.$fullname.'</a>';
277 $by->date = $template['lastmod'];
278 print_string('bynameondate', 'forum', $by);
279 echo '</div></td></tr>';
280
281 echo '<tr><td class="left side">';
4a173181 282
283/// Actual content
284
285 echo '</td><td class="content">'."\n";
286
287 // Print whole message
288 echo format_text($template['body']);
289
290/// Links to tags
291
417195c1 292 if ($blogtags = get_records_sql('SELECT t.* FROM '.$CFG->prefix.'tags t, '.$CFG->prefix.'blog_tag_instance ti
293 WHERE t.id = ti.tagid
294 AND ti.entryid = '.$entryid)) {
4a173181 295 echo '<p />';
86b67cc2 296 print_string('tags');
297 echo ': ';
d7dfcee1 298 foreach ($blogtags as $key => $blogtag) {
299 $taglist[] = '<a href="index.php?courseid='.$course->id.'&amp;filtertype='.$filtertype.'&amp;filterselect='.$filterselect.'&amp;tagid='.$blogtag->id.'">'.$blogtag->text.'</a>';
4a173181 300 }
d7dfcee1 301 echo implode(', ', $taglist);
4a173181 302 }
303
304/// Commands
305
306 echo '<div class="commands">';
307
308 if (isset($USER->id)) {
309 if (($template['userid'] == $USER->id) or isteacher($course->id)) {
1a3f39f1 310 echo '<a href="'.$CFG->wwwroot.'/blog/edit.php?editid='.$entryid.'&amp;sesskey='.sesskey().'">'.$stredit.'</a>';
4a173181 311 }
312
313 if (($template['userid'] == $USER->id) or isteacher($course->id)) {
1a3f39f1 314 echo '| <a href="'.$CFG->wwwroot.'/blog/edit.php?act=del&amp;postid='.$entryid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
4a173181 315 }
316 }
317
318 echo '</div>';
319
320 echo '</td></tr></table></div>'."\n\n";
321}
322
323/**
324 * Use this function to retrieve a list of publish states available for
325 * the currently logged in user.
326 *
327 * @return array This function returns an array ideal for sending to moodles'
328 * choose_from_menu function.
329 */
330function blog_applicable_publish_states($courseid='') {
331 global $CFG;
332
333 // everyone gets draft access
334 $options = array ( 'draft' => get_string('publishtonoone', 'blog') );
1e1ba8c2 335 $options['site'] = get_string('publishtosite', 'blog');
336 $options['public'] = get_string('publishtoworld', 'blog');
924f20e2 337
4a173181 338 return $options;
339}
340
516194d0 341/// Checks to see if a user can view the blogs of another user.
342/// He can do so, if he is admin, in any same non-spg course,
343/// or spg group, but same group member
344function blog_user_can_view_user_post($targetuserid) {
345
ebb73f49 346 global $CFG;
347
516194d0 348 $canview = 0; //bad start
349
350 if (isadmin()) {
351 return true;
352 }
353
354 $usercourses = get_my_courses($targetuserid);
355 foreach ($usercourses as $usercourse) {
356 /// if viewer and user sharing same non-spg course, then grant permission
357 if (groupmode($usercourse)!= SEPARATEGROUPS){
358 if (isstudent($usercourse->id) || isteacher($usercourse->id)) {
359 $canview = 1;
360 return $canview;
361 }
362 } else {
363 /// now we need every group the user is in, and check to see if view is a member
364 if ($usergroups = user_group($usercourse->id, $targetuserid)) {
365 foreach ($usergroups as $usergroup) {
366 if (ismember($usergroup->id)) {
367 $canview = 1;
368 return $canview;
369 }
370 }
371 }
372 }
373 }
374
375 if (!$canview && $CFG->bloglevel < BLOG_SITE_LEVEL) {
376 error ('you can not view this user\'s blogs');
377 }
378
379 return $canview;
380}
4a173181 381?>