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