change in publish states
[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();
4a173181 153
4a173181 154 $morelink = '<br />&nbsp;&nbsp;';
155 // show personal or general heading block as applicable
156 echo '<div class="headingblock header blog">';
157 //show blog title - blog tagline
158 print "<br />"; //don't print title. blog_get_title_text();
159
160 if ($blogpage != 0) {
161 // modify the blog filter to fetch the entries we care about right now
162 $oldstart = $blogFilter->fetchstart;
163 $blogFilter->fetchstart = $blogpage * $bloglimit;
164 unset($blogFilter->filtered_entries);
165 }
166 $blogEntries = $blogFilter->get_filtered_entries();
167 // show page next/previous links if applicable
168 print_paging_bar($blogFilter->get_viewable_entry_count(), $blogpage, $bloglimit, $blogFilter->baseurl, 'blogpage');
169 print '</div>';
4fc47966 170
171 if (blog_isLoggedIn()) {
172 //the user's blog is enabled and they are viewing their own blog
173 $addlink = '<div align="center">';
174 $addlink .= $blogFilter->get_complete_link($CFG->wwwroot .'/blog/edit.php', get_string('addnewentry', 'blog'));
175 $addlink .='</div>';
176 echo $addlink;
177 }
178
4a173181 179 if (isset($blogEntries) ) {
180
4a173181 181 $count = 0;
182 foreach ($blogEntries as $blogEntry) {
183 blog_print_entry($blogEntry, 'list', $filtertype, $filterselect); //print this entry.
184 $count++;
185 }
186 if (!$count) {
187 print '<br /><center>'. get_string('noentriesyet', 'blog') .'</center><br />';
188
4a173181 189 }
190
191 print $morelink.'<br />'."\n";
192
193 if ($blogpage != 0) {
194 //put the blogFilter back the way we found it
195 $blogFilter->fetchstart = $oldstart;
196 unset($blogFilter->filtered_entries);
197 $blogFilter->fetch_entries();
198 }
199
200 return;
201 }
202
203 $output = '<br /><center>'. get_string('noentriesyet', 'blog') .'</center><br />';
4fc47966 204
4a173181 205 print $output;
206 unset($blogFilter->filtered_entries);
207}
208
4a173181 209/**
210 * This function is in lib and not in BlogInfo because entries being searched
211 * might be found in any number of blogs rather than just one.
212 *
213 * This function builds an array which can be used by the included
214 * template file, making predefined and nicely formatted variables available
215 * to the template. Template creators will not need to become intimate
216 * with the internal objects and vars of moodle blog nor will they need to worry
217 * about properly formatting their data
218 *
219 * @param BlogEntry blogEntry - a hopefully fully populated BlogEntry object
220 * @param string viewtype Default is 'full'. If 'full' then display this blog entry
221 * in its complete form (eg. archive page). If anything other than 'full'
222 * display the entry in its abbreviated format (eg. index page)
223 */
1e1ba8c2 224function blog_print_entry(&$blogEntry, $viewtype='full', $filtertype='', $filterselect='', $mode='loud') {
4a173181 225 global $CFG, $THEME, $USER;
226 static $bloginfoarray;
227
228 if (isset($bloginfoarray) && $bloginfocache[$blogEntry->entryuserid]) {
229 $bloginfo = $bloginfocache[$blogEntry->entryuserid];
230 } else {
231 $bloginfocache[$blogEntry->entryuserid] = new BlogInfo($blogEntry->entryuserid);
232 $bloginfo = $bloginfocache[$blogEntry->entryuserid];
233 }
4a173181 234
235 $template['body'] = $blogEntry->get_formatted_entry_body();
236 $template['countofextendedbody'] = 0;
237
4a173181 238 $template['title'] = '<a name="'. $blogEntry->entryId .'"></a>';
239 //enclose the title in nolink tags so that moodle formatting doesn't autolink the text
240 $template['title'] .= '<span class="nolink">'. stripslashes_safe($blogEntry->entryTitle);
241 $template['title'] .= '</span>';
242
243 // add editing controls if allowed
4a173181 244 $template['userid'] = $blogEntry->entryuserid;
4a173181 245 $template['author'] = $blogEntry->entryAuthorName;
246 $template['lastmod'] = $blogEntry->formattedEntryLastModified;
247 $template['created'] = $blogEntry->formattedEntryCreated;
248 $template['publishtomenu'] = $blogEntry->get_publish_to_menu(true, true);
4a173181 249 //forum style printing of blogs
1e1ba8c2 250 blog_print_entry_content ($template, $blogEntry->entryId, $filtertype, $filterselect, $mode);
4a173181 251
252}
253
254//forum style printing of blogs
1e1ba8c2 255function blog_print_entry_content ($template, $entryid, $filtertype='', $filterselect='', $mode='loud') {
4a173181 256 global $USER, $CFG, $course, $ME;
257
258 $stredit = get_string('edit');
259 $strdelete = get_string('delete');
260
261 $user = get_record('user','id',$template['userid']);
262
263 echo '<div align="center"><table cellspacing="0" class="forumpost" width="100%">';
264
265 echo '<tr class="header"><td class="picture left">';
20fc0f26 266 print_user_picture($template['userid'], SITEID, $user->picture);
4a173181 267 echo '</td>';
268
269 echo '<td class="topic starter"><div class="subject">'.$template['title'].'</div><div class="author">';
270 $fullname = fullname($user, isteacher($template['userid']));
271 $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
272 $user->id.'&amp;course='.$course->id.'">'.$fullname.'</a>';
273 $by->date = $template['lastmod'];
274 print_string('bynameondate', 'forum', $by);
275 echo '</div></td></tr>';
276
277 echo '<tr><td class="left side">';
4a173181 278
279/// Actual content
280
281 echo '</td><td class="content">'."\n";
282
283 // Print whole message
284 echo format_text($template['body']);
285
286/// Links to tags
287
417195c1 288 if ($blogtags = get_records_sql('SELECT t.* FROM '.$CFG->prefix.'tags t, '.$CFG->prefix.'blog_tag_instance ti
289 WHERE t.id = ti.tagid
290 AND ti.entryid = '.$entryid)) {
4a173181 291 echo '<p />';
86b67cc2 292 print_string('tags');
293 echo ': ';
4a173181 294 foreach ($blogtags as $blogtag) {
295 echo '<a href="index.php?courseid='.$course->id.'&amp;filtertype='.$filtertype.'&amp;filterselect='.$filterselect.'&amp;tagid='.$blogtag->id.'">'.$blogtag->text.'</a>, ';
296 }
297 }
298
299/// Commands
300
301 echo '<div class="commands">';
302
303 if (isset($USER->id)) {
304 if (($template['userid'] == $USER->id) or isteacher($course->id)) {
1a3f39f1 305 echo '<a href="'.$CFG->wwwroot.'/blog/edit.php?editid='.$entryid.'&amp;sesskey='.sesskey().'">'.$stredit.'</a>';
4a173181 306 }
307
308 if (($template['userid'] == $USER->id) or isteacher($course->id)) {
1a3f39f1 309 echo '| <a href="'.$CFG->wwwroot.'/blog/edit.php?act=del&amp;postid='.$entryid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
4a173181 310 }
311 }
312
313 echo '</div>';
314
315 echo '</td></tr></table></div>'."\n\n";
316}
317
318/**
319 * Use this function to retrieve a list of publish states available for
320 * the currently logged in user.
321 *
322 * @return array This function returns an array ideal for sending to moodles'
323 * choose_from_menu function.
324 */
325function blog_applicable_publish_states($courseid='') {
326 global $CFG;
327
328 // everyone gets draft access
329 $options = array ( 'draft' => get_string('publishtonoone', 'blog') );
1e1ba8c2 330 $options['site'] = get_string('publishtosite', 'blog');
331 $options['public'] = get_string('publishtoworld', 'blog');
924f20e2 332
4a173181 333 return $options;
334}
335
4a173181 336?>