iFIx up some styles for the MyMoodle overview
[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}
36if (!(isset($CFG->blog_enable_trackback_in) )) {
37 $CFG->blog_enable_trackback_in = 0; //default is 0 == do not allow for site
38}
39if (!(isset($CFG->blog_enable_moderation) )) {
40 $CFG->blog_enable_moderation = 0; //default is 0 == do not enable blog moderation on this site
41}
42if (!(isset($CFG->blog_enable_pingback_in) )) {
43 $CFG->blog_enable_pingback_in = 0; //default is 0 == do not allow for site
44}
45if (!(isset($CFG->blog_enable_trackback_out) )) {
46 $CFG->blog_enable_trackback_out = 0; //default is 0 == do not allow for site
47}
48if (!(isset($CFG->blog_enable_pingback_out) )) {
49 $CFG->blog_enable_pingback_out = 0; //default is 0 == do not allow for site
50}
51if (!(isset($CFG->blog_enable_moderation) )) {
52 $CFG->blog_enable_moderation = 0; //default is 0 == do not turn on moderation for site
53}
54if (!(isset($CFG->blog_useweblog_rpc) )) {
55 $CFG->blog_useweblog_rpc = 0;//default is 0 == do not publish to weblogs.com
56}
57if (empty($CFG->blog_ratename) ) {
58 $CFG->blog_ratename = 'Rating'; //default name for entry ratings
59}
60if (empty($CFG->blog_default_title) ) {
61 $CFG->blog_default_title = 'Moodle Blog'; //default blog title
62}
63if (empty($CFG->blog_blogurl) ) {
64 $CFG->blog_blogurl = $CFG->wwwroot.'/blog/index.php';
65}
66if (!(isset($CFG->blog_enable_trackback) )) {
67 $CFG->blog_enable_trackback = 0;
68}
69if (!(isset($CFG->blog_enable_pingback) )) {
70 $CFG->blog_enable_pingback = 0;
71}
72if (empty($CFG->blog_default_fetch_num_entries) ) {
73 $CFG->blog_default_fetch_num_entries = 8;
74}
75
76/**
77 * blog_user_bloginfo
78 *
79 * returns a blogInfo object if the user has a blog in the acl table
80 * This function stores the currently logged in user's bloginfo object
81 * statically - do not release/unset the returned object.
82 * added by Daryl Hawes for moodle integration
83 * $userid - if no userid specified it will attempt to use the logged in user's id
84 */
85function blog_user_bloginfo($userid='') {
86//Daryl Hawes note: not sure that this attempt at optimization is correct
87// static $bloginfosingleton; //store the logged in user's bloginfo in a static var
88
89 if ($userid == '') {
90 global $USER;
91 if (!isset($USER) || !isset($USER->id)) {
92 return;
93 }
94 $userid = $USER->id;
95 }
96
97/* if (isset($USER) && $USER->id == $uid && !empty($bloginfosingleton)) {
98 return $bloginfosingleton;
99 }*/
100
101 $thisbloginfo = new BlogInfo($userid);
102/* if (isset($USER) && $USER->id == $userid) {
103 $bloginfosingleton = $thisbloginfo;
104 }*/
105 return $thisbloginfo;
106}
107
108/**
109 * Verify that a user is logged in based on the session
110 * @return bool True if user has a valid login session
111 */
112function blog_isLoggedIn() {
113 global $USER;
114 if (!isguest() && isset($USER) and isset($USER->id) and $USER->id) {
115 return 1;
116 }
117 return 0;
118}
119
120/**
121 * This function upgrades the blog's tables as needed.
122 * It's called from moodle/admin/index.php
123 */
124function blog_upgrade_blog_db($continueto) {
125
126 global $CFG, $db;
127
128 require_once($CFG->dirroot.'/blog/version.php'); // Get code versions
129
130 if (empty($CFG->blog_version)) { // Blog has never been installed.
131 $strdatabaseupgrades = get_string('databaseupgrades');
132 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades,
133 '', '', false, '&nbsp;', '&nbsp;');
134
135 $db->debug=true;
136 if (modify_database($CFG->dirroot .'/blog/db/'. $CFG->dbtype .'.sql')) {
137 $db->debug = false;
138 if (set_config('blog_version', $blog_version)) {
139 notify(get_string('databasesuccess'), 'green');
140 notify(get_string('databaseupgradeblog', 'blog', $blog_version));
141
142 /// Added by Daryl Hawes - since blog has to be installed the first time before blog's blocks, and on first install creating default blocks when none exist is a bad idea, just reset blocks for blog index here
143 require_once($CFG->libdir .'/pagelib.php');
144 require_once($CFG->dirroot .'/blog/blogpage.php');
145 page_map_class(PAGE_BLOG_VIEW, 'page_blog');
146 // Now, create our page object.
147 $page = page_create_object(PAGE_BLOG_VIEW, 0);
148 // Add default blocks to the new page type
149 blocks_repopulate_page($page);
150
151 print_continue($continueto);
152 exit;
153 } else {
154 error('Upgrade of blogging system failed! (Could not update version in config table)');
155 }
156 } else {
157 error('blog tables could NOT be set up successfully!');
158 }
159 }
160
161
162 if ($blog_version > $CFG->blog_version) { // Upgrade tables
163 $strdatabaseupgrades = get_string('databaseupgrades');
164 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades);
165
166 require_once($CFG->dirroot. '/blog/db/'. $CFG->dbtype .'.php');
167
168 $db->debug=true;
169 if (blog_upgrade($CFG->blog_version)) {
170 $db->debug=false;
171 if (set_config('blog_version', $blog_version)) {
172 notify(get_string('databasesuccess'), 'green');
173 notify(get_string('databaseupgradeblocks', '', $blog_version));
174 print_continue($continueto);
175 exit;
176 } else {
177 error('Upgrade of blogging system failed! (Could not update version in config table)');
178 }
179 } else {
180 $db->debug=false;
181 error('Upgrade failed! See blog/version.php');
182 }
183
184 } else if ($blog_version < $CFG->blog_version) {
185 notify('WARNING!!! The blog code you are using is OLDER than the version that made these databases! ('. $version .' < '. $CFG->blog_version .')');
186 }
187}
188
189/**
190 * course_has_blog_entries
191 * Given a course id return true if there are blog entries from any user related to that course
192 * $courseid - the id for the course
193 * Daryl Hawes note: When forum entries start using post table this function will no longer behave as expected
194 * Since non blog posts will be associated with this course. Perhaps moduleid or another field could be wrangled
195 * into identifying the type of post?
196 */
197function blog_course_has_blog_entries($courseid) {
198 $entries = get_records('post', 'courseid', $courseid);
199 if (! empty($entries) ) {
200 return true;
201 }
202 return false;
203}
204
205/**
206 * Output a hidden html form used by text entry pages that require preview.php's functionality
207 */
208function blog_print_preview_form($userid=0, $categoryelement='<input type="hidden" name="categoryid[]">', $return=false) {
209
210 $returnstring = "\n".'<div id="prev" style="visibility:hidden;z-index:-1;position:absolute;display:none;">';
211 $returnstring .= "\n".'<form name="prev" action="preview.php" method="post" target="preview">';
212 $returnstring .= "\n".'<input type="hidden" name="etitle" />';
213 $returnstring .= "\n".'<input type="hidden" name="body" />';
4a173181 214 $returnstring .= "\n".'<input type="hidden" name="comm" />';
215 $returnstring .= "\n".'<input type="hidden" name="tem" />';
216 $returnstring .= "\n".'<input type="hidden" name="userid" value="'. $userid .'" />';
217
218// $returnstring .= '<input type="hidden" name="categoryid[]" value="'. $categoryid .'" />';
219 $returnstring .= "\n". $categoryelement;
220 $returnstring .= "\n".'<input type="hidden" name="format" />';
221 $returnstring .= "\n".'</form>';
222 $returnstring .= "\n".'</div>'."\n";
223
224 if ($return) {
225 return $returnstring; //return the form as a string if requested
226 }
227 print $returnstring; //else just print the string and exit the function
228}
229
230/**
231 * blog_user_has_rights - returns true if user is the blog's owner or a moodle admin.
232 *
233 * @param BlogInfo blogInfo - a BlogInfo object passed by reference. This object represents the blog being accessed.
234 * @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.
235 */
236function blog_user_has_rights(&$bloginfo, $uid='') {
237 global $USER;
238 if (isset($bloginfo) && isset($bloginfo->userid)) {
239 if ($uid == '') {
240 if ( isset($USER) && isset($USER->id) ) {
241 $uid = $USER->id;
242 }
243 }
244 if ($uid == '') {
245 //if uid is still empty then the user is not logged in
246 return false;
247 }
248 if (blog_is_blog_admin($uid) || isadmin()) {
249 return true;
250 }
251 }
252 return false;
253}
254
255/**
256 * Determines whether a user is an admin for a blog
257 * @param int $blog_userid The id of the blog being checked
258 */
259function blog_is_blog_admin($blog_userid) {
260 global $USER;
261
262 //moodle admins are admins
263 if (isadmin()) {
264 return true;
265 }
266 if ( empty($USER) || !isset($USER->id) ) {
267 return false;
268 }
269 if ( empty($blog_userid)) {
270 return true;
271 }
272
273 // Return true if the user is an admin for this blog
274 if ($blog_userid == $USER->id) {
275 return true;
276 } else {
277 return false;
278 }
279}
280
281/**
282 * Adaptation of isediting in moodlelib.php for blog module
283 * @return bool
284 */
285function blog_isediting() {
286 global $SESSION;
287 if (! isset($SESSION->blog_editing_enabled)) {
288 $SESSION->blog_editing_enabled = false;
289 }
290 return ($SESSION->blog_editing_enabled);
291}
292
293/**
294 * Turns a php string into a string ready for insert into an rss xml feed
295 */
296function blog_rss_content($str) {
297 $str = superhtmlentities($str);
298 $content = "<![CDATA[$str]]>\n";
299
300 return $content;
301}
302
303/**
304 * function found when searching on problem of smart quotes
305 * posted at http://daynah.php-princess.net/index.php?p=94
306 */
307function superhtmlentities($text) {
308 $entities = array(128 => 'euro', 130 => 'sbquo',
309 131 => 'fnof', 132 => 'bdquo', 133 => 'hellip',
310 134 => 'dagger', 135 => 'Dagger', 136 => 'circ',
311 137 => 'permil', 138 => 'Scaron', 139 => 'lsaquo',
312 140 => 'OElig', 145 => 'lsquo', 146 => 'rsquo',
313 147 => 'ldquo', 148 => 'rdquo', 149 => 'bull',
314 150 => 'ndash', 151 => 'mdash', 152 => 'tilde',
315 153 => 'trade', 154 => 'scaron', 155 => 'rsaquo',
316 156 => 'oelig', 159 => 'Yuml');
317
318 $new_text = '';
319 for($i = 0; $i < strlen($text); $i++) {
320 $num = ord($text{$i});
321 if (array_key_exists($num, $entities)) {
322 $new_text .= '&\\'.$entities[$num].';';
323 } else if ($num < 127 || $num > 159) {
324 $new_text .= $text{$i};
325 }
326 }
327 return htmlentities($new_text);
328}
329
330/**
331 * blog_get_recent_entries_byrange
332 * not in blogInfo because entries being searched
333 * can be found in any number of blogs rather than just one.
334 *
335 * Returns specified range of entries since a given time.
336 *
337 * In using this function be aware for your where clause that the tables being
338 * read are post (e) and blog_categories_entries (c) and the returned values
339 * will be e.*
340 *
341 * @param int $limit .
342 * @param int $start .
343 * @param string $where .
344 * @param string $orderby .
345 * @param bool $includeCategories .
346 *
347 * @return BlogEntries
348 */
349function blog_get_recent_entries_byrange($limit, $start, $where='', $orderby='lastmodified DESC', $includeCategories=false) {
350 global $CFG;
351
352// echo 'Debug: where clause in blog_get_recent_entries_byrange: $where<br />'; //debug
353
354 if ($includeCategories) {
355 $records = get_records_select('post e, '. $CFG->prefix .'blog_categories_entries c', $where, $orderby, '*', $start, $limit);
356 } else {
357 $records = get_records_select('post', $where, $orderby, '*', $start, $limit);
358 }
359// print_object($records); //debug
360
361 if (empty($records)) {
362 return array();
363 } else {
364 $blogEntries = array();
365 foreach($records as $record) {
366 $blogEntry = new BlogEntry($record);
367 //ensure that the user has rights to view this entry
368 if ($blogEntry->user_can_view() ) {
369 $blogEntries[] = $blogEntry;
370 }
371 }
372 return $blogEntries;
373 }
374}
375
376/**
377 * returns a unix time stamp from year month and day
378 * used to get start and end dates for comparing against timestamps in databases
379 */
380function blog_get_month_time($y, $m, $d, $firstday=true) {
381
382 if ( !empty($y) && !empty($m) && !empty($d)) {
383 $time = mktime(0, 0, 0, $m, $d, $y);
384 } else if ( !empty($y) && !empty($m) ) {
385 $day = blog_mk_getLastDayofMonth($m, $y);
386 if ($firstday) {
387 $day = 1;
388 }
389 $time = mktime(0, 0, 0, $m, $day, $y);
390 } else {
391 $time = '';
392 }
393 return $time;
394}
395
396/**
397 * attempting to create a method useful for a popup form on the index
398 * page that will allow a user to filter entries based upon date
399 *
400 * This function will return a list, in order, formatted as "MM-YYYY" for each year and month
401 * in which this blog has entries.
402 * @param BlogFilter $blogFilter - a BlogFilter object with the current filter settings for this page
403 */
404function blog_get_year_month_of_viewable_entries(&$blogFilter) {
405 global $_SERVER;
406
407 $entries = $blogFilter->get_filtered_entries();
408 $datearray = array();
409 $datearray['m=&amp;y='] = 'All Dates';
410
411 if ( !empty($entries) ) {
412 foreach ($entries as $entry) {
413 if ( $entry->user_can_view() ) {
414// print_object($entry); //debug
415 //user is allowed to see this entry, so return its year/month information
416 $date = $entry->entryLastModified;
417 $truncDate = date("F Y", $date); // this will return January 2004 for the date.
418 $curmonth = date("m", $date); // this will return January 2004 for the date.
419 $curyear = date("Y", $date); // this will return January 2004 for the date.
420 $index = 'm='. $curmonth .'&amp;y='. $curyear;
421 $datearray[$index] = $truncDate;
422 }
423 }
424 }
425// print_object($datearray); //debug
426
427 if (is_numeric($blogFilter->startmonth) && is_numeric($blogFilter->startyear)) {
428 $selected = 'm='. $blogFilter->startmonth .'&amp;y='. $blogFilter->startyear;
429 } else {
430 $selected = '';
431 }
432 $unused = array('startmonth', 'startyear');
433 $getvars = $blogFilter->get_getvars($unused);
434 //attach a random number to the popup function's form name
435 //becuase there may be multiple instances of this form on each page
436 $self = basename($_SERVER['PHP_SELF']);
437 $form = popup_form($self . $getvars .'&amp;', $datearray, 'blog_date_selection'. mt_rand(), $selected, '', '', '', true);
438 return str_replace('<form', '<form style="display: inline;"', $form);
439}
440
441/**
442 * pretty close to a direct copy of calendar/view.php calendar_course_filter_selector function
443 */
444function blog_course_filter_selector(&$blogFilter) {
445 global $USER, $_SERVER;
446
447 $getvars = $blogFilter->get_getvars('courseid');
448
449 if ( !isset($USER) || !isset($USER->id) ) {
450 return;
451 }
452
453 if (isguest($USER->id)) {
454 return '';
455 }
456
457 if (isadmin($USER->id)) {
458 $courses = get_courses('all', 'c.shortname');
459
460 } else {
461 $courses = get_my_courses($USER->id, 'shortname');
462 }
463
464 unset($courses[1]);
465
466 $courseoptions[1] = get_string('fulllistofcourses');
467 foreach ($courses as $course) {
468 // Verify that there are actually blog entries for this course before showing it as a selection.
469 if ($entries = count_records('post', 'courseid', $course->id)) {
470 $courseoptions[$course->id] = $course->shortname;
471 }
472 }
473
474 //if there were no courses added then simply return
475 if (count($courseoptions) == 1) {
476 return;
477 }
478
479 if (is_numeric($blogFilter->courseid)) {
480 $selected = $blogFilter->courseid;
481 } else {
482 $selected = '';
483 }
484 //attach a random number to the popup function's form name
485 //because there may be multiple instances of this form on each page
486 $self = basename($_SERVER['PHP_SELF']);
487 $form = popup_form($self . $getvars .'&amp;courseid=',
488 $courseoptions, 'blog_course_selection'. mt_rand(), $selected, '', '', '', true);
489
490 return str_replace('<form', '<form style="display: inline;"', $form);
491}
492
493/**
494 * build and return list of all member blogs
495 *
496 * @param stdObject $memberrecords An object of record entries as output from the get_member_list() function in BlogFilter (->id, ->title are the required variables).
497 * @param int $format - 0, 1 or 2; 0 = hyperlinked list of members, 1 = select element, 2 = select element wrapped in entire form
498 * @param bool $return indicates whether the function should return the text
499 * as a string or echo it directly to the page being rendered
500 * @param BlogFilter $blogFilter - a BlogFilter object with the details of the members listed in $memberrecords.
501 * @param string $hyperlink This is the target link to be used - there is a sensible default for each format.
502 */
503function blog_member_list(&$blogFilter, &$memberrecords, $format=1, $return=true, $hyperlink='') {
504 global $CFG, $USER;
505
506//echo "userid = $blogFilter->userid"; //debug
507//print_object($memberrecords); //debug
508 $returnstring = '';
509 if (!empty($memberrecords)) {
510 switch($format) {
511 case '0':
512 foreach($memberrecords as $record) {
513 if (empty($hyperlink)) {
514 $CFG->wwwroot .'/blog/index.php?userid='. $record->id;
515 }
516 $returnstring .= '<a href="'. $hyperlink . $record->id .'">'. stripslashes_safe($record->title) .'</a><br />';
517 }
518 break;
519 case '2':
520 $selected = '';
521 $options = array('' => 'All Member Blogs');
522 $formlink = $hyperlink; //TESTING
523 if (empty($hyperlink)) {
524 $getvars = $blogFilter->get_getvars('userid');
525 $self = basename($_SERVER['PHP_SELF']);
526 $formlink = $self . $getvars .'&amp;userid=';
527 }
528 foreach($memberrecords as $record) {
529 $id = $record->id;
530 if (blog_isLoggedIn() && $id == $USER->id ) {
531 $optiontitle = 'My Blog';
532 } else {
533 $optiontitle = stripslashes_safe($record->title);
534 }
535 $options[$id] = $optiontitle; //TESTING
536 if ( ($blogFilter->userid == $record->id) && ($blogFilter->userid != 0) ) {
537 $selected = $id;
538 }
539 }
540
541 //attach a random number to the popup function's form name
542 //becuase there may be multiple instances of this form on each page
543 $returnstring = popup_form($formlink,
544 $options, 'blog_member_list'. mt_rand(), $selected, '', '', '', true);
545 $returnstring = str_replace('<form', '<form style="display: inline;"', $returnstring);
546 break;
547
548 case '1':
549 default:
550 $returnstring = '<select name="userid">';
551 foreach($memberrecords as $record) {
552 $returnstring .= '<option value="'. $record->id .'"';
553 if ( ($record->id == $blogFilter->userid) && ($blogFilter->userid != 0) ) {
554 $returnstring .= ' selected';
555 }
556 $returnstring .= '>'. stripslashes_safe($record->title) .'</option>';
557 }
558 $returnstring .= '</select>';
559 break;
560 }
561
562 }
563 if ($return) {
564 return $returnstring;
565 }
566 print $returnstring;
567 return;
568}
569
570/**
571 * @param int courseid the selected course in popup
572 */
573function blog_get_course_selection_popup($courseid='') {
574 global $USER;
575 if ( !isset($USER) || !isset($USER->id) ) {
576 return;
577 }
578 if ( isadmin() ) {
579 $courses = get_courses(); //show admin users all courses
580 } else {
581 $courses = get_my_courses($USER->id) ; //get_my_courses is in datalib.php
582 }
583 //print_object($courses); //debug
584 $courseoptions = array();
585 foreach ($courses as $course) {
586 $courseoptions[$course->id] = $course->shortname;
587 }
588 return choose_from_menu($courseoptions, 'course_selection', $courseid, '', '', '0', true);
589}
590
591/**
592 * This function is in lib and not in BlogInfo because entries being searched
593 * might be found in any number of blogs rather than just one.
594 *
595 * $@param BlogFilter blogFilter - a BlogFilter object containing the settings for finding appropriate entries for display
596 */
597function blog_print_html_formatted_entries(&$blogFilter, $filtertype, $filterselect) {
598 global $CFG, $USER;
5c625033 599 $blogpage = optional_param('blogpage', 0, PARAM_INT);
4a173181 600 $bloglimit = get_user_preferences('blogpagesize',8); // expose as user pref when MyMoodle comes around
601
602 // First let's see if the batchpublish form has submitted data
603 $post = data_submitted();
604 if (!empty($post->batchpublish)) { //make sure we're processing the edit form here
605// print_object($post); //debug
606 foreach ($post as $key => $publishto) {
607 if ($key != 'batchpublish') {
608 $useridandentryid = explode('-', $key);
609 $userid = $useridandentryid[0];
610 $entryid = $useridandentryid[1];
611 $bloginfo = new BlogInfo($userid);
612 $blogentry = $bloginfo->get_blog_entry_by_id($entryid);
613 if ($blogentry->entryPublishState != $publishto) {
614 if (!$blogentry->set_publishstate($publishto)) {
615 echo 'Entry "'. $blogentry->entryTitle .'" could not be published.';
616 } else {
617 if ($error = $blogentry->save()) {
618 echo 'New publish setting for entry "'. $blogentry->entryTitle .'" could not be saved. ERROR:'. $error.':<br />';
619 }
620 }
621 }
622 }
623 }
624 }
625
626
627 $morelink = '<br />&nbsp;&nbsp;';
628 // show personal or general heading block as applicable
629 echo '<div class="headingblock header blog">';
630 //show blog title - blog tagline
631 print "<br />"; //don't print title. blog_get_title_text();
632
633 if ($blogpage != 0) {
634 // modify the blog filter to fetch the entries we care about right now
635 $oldstart = $blogFilter->fetchstart;
636 $blogFilter->fetchstart = $blogpage * $bloglimit;
637 unset($blogFilter->filtered_entries);
638 }
639 $blogEntries = $blogFilter->get_filtered_entries();
640 // show page next/previous links if applicable
641 print_paging_bar($blogFilter->get_viewable_entry_count(), $blogpage, $bloglimit, $blogFilter->baseurl, 'blogpage');
642 print '</div>';
643 if (isset($blogEntries) ) {
644
645 if (blog_isLoggedIn() && blog_isediting() ) {
646 print '<form name="batchpublishform" method="post" action="'. $blogFilter->baseurl .'" id="batchpublishform" enctype="multipart/form-data">';
647 }
648
649 $count = 0;
650 foreach ($blogEntries as $blogEntry) {
651 blog_print_entry($blogEntry, 'list', $filtertype, $filterselect); //print this entry.
652 $count++;
653 }
654 if (!$count) {
655 print '<br /><center>'. get_string('noentriesyet', 'blog') .'</center><br />';
656
657 if (blog_isLoggedIn()) {
658 $morelink = '<br />&nbsp;&nbsp;';
9154b440 659 $morelink .= $blogFilter->get_complete_link('<a href="'. $CFG->wwwroot .'/blog/edit.php', get_string('addnewentry', 'blog'))."\n";
4a173181 660
661 }
662 }
663/*
664 if (blog_isLoggedIn() && blog_isediting() ) {
665 //Daryl Hawes note: localize this submit button!
666 print '<div align="center"><input type="submit" value="Save these publish settings" id="batchpublish" name="batchpublish" /></div>'."\n";
667 print '</form>'."\n";
668 }
669*/
670 //yu: testing code
671 if (blog_isLoggedIn()) {
672 //the user's blog is enabled and they are viewing their own blog
9154b440 673 $morelink .= $blogFilter->get_complete_link($CFG->wwwroot .'/blog/edit.php', get_string('addnewentry', 'blog'));
4a173181 674 }
675
676 print $morelink.'<br />'."\n";
677
678 if ($blogpage != 0) {
679 //put the blogFilter back the way we found it
680 $blogFilter->fetchstart = $oldstart;
681 unset($blogFilter->filtered_entries);
682 $blogFilter->fetch_entries();
683 }
684
685 return;
686 }
687
688 $output = '<br /><center>'. get_string('noentriesyet', 'blog') .'</center><br />';
689 $userbloginfo = blog_user_bloginfo();
690
691 if (blog_isLoggedIn()) {
692 //the user's blog is enabled and they are viewing their own blog
9154b440 693 $output .= $blogFilter->get_complete_link($CFG->wwwroot .'/blog/edit.php', get_string('addnewentry', 'blog'));
4a173181 694 }
695 print $output;
696 unset($blogFilter->filtered_entries);
697}
698
699/**
700 * What text should be displayed claiming ownership to the current blog entries?
701 * @uses $PAGE
702 * @return string
703 */
704function blog_get_title_text() {
705 global $PAGE; //hackish
706
707 if (isset($PAGE) && isset($PAGE->bloginfo)) {
708 $blogInfo = &$PAGE->bloginfo;
709 $title = $blogInfo->get_blog_title();
710 if($title != '') {
711 $displaytitle = $title;
712 $tagline = $blogInfo->get_blog_tagline();
713 if ($tagline != '') {
714 $displaytitle .= ' - '. $tagline;
715 }
716 }
717 }
718 if (isset($displaytitle)) {
719 return $displaytitle;
720 } else {
721 // Daryl Hawes - better wording would be good here, localize this line once the wording is selected.
722 return 'Combined Blog '. get_string('entries', 'blog') .'<br />';
723 }
724}
725
726/**
727 * blog_get_moodle_pix_path
728 *
729 * Returns the directory path to the current theme's pix folder.
730 * @return string
731 */
732function blog_get_moodle_pix_path(){
733 global $CFG, $THEME;
734 if (empty($THEME->custompix)) {
735 return $CFG->wwwroot.'/pix';
736 } else {
737 return $CFG->themedir.current_theme().'/pix';
738 }
739}
740
741/**
742 * This function is in lib and not in BlogInfo because entries being searched
743 * might be found in any number of blogs rather than just one.
744 *
745 * This function builds an array which can be used by the included
746 * template file, making predefined and nicely formatted variables available
747 * to the template. Template creators will not need to become intimate
748 * with the internal objects and vars of moodle blog nor will they need to worry
749 * about properly formatting their data
750 *
751 * @param BlogEntry blogEntry - a hopefully fully populated BlogEntry object
752 * @param string viewtype Default is 'full'. If 'full' then display this blog entry
753 * in its complete form (eg. archive page). If anything other than 'full'
754 * display the entry in its abbreviated format (eg. index page)
755 */
1a3f39f1 756function blog_print_entry(&$blogEntry, $viewtype='full', $filtertype='', $filterselect='') {
4a173181 757 global $CFG, $THEME, $USER;
758 static $bloginfoarray;
759
760 if (isset($bloginfoarray) && $bloginfocache[$blogEntry->entryuserid]) {
761 $bloginfo = $bloginfocache[$blogEntry->entryuserid];
762 } else {
763 $bloginfocache[$blogEntry->entryuserid] = new BlogInfo($blogEntry->entryuserid);
764 $bloginfo = $bloginfocache[$blogEntry->entryuserid];
765 }
4a173181 766
767 $template['body'] = $blogEntry->get_formatted_entry_body();
768 $template['countofextendedbody'] = 0;
769
4a173181 770 $template['title'] = '<a name="'. $blogEntry->entryId .'"></a>';
771 //enclose the title in nolink tags so that moodle formatting doesn't autolink the text
772 $template['title'] .= '<span class="nolink">'. stripslashes_safe($blogEntry->entryTitle);
773 $template['title'] .= '</span>';
774
775 // add editing controls if allowed
4a173181 776 $template['courseid'] = $blogEntry->entryCourseId;
777 $template['userid'] = $blogEntry->entryuserid;
778 $template['authorviewurl'] = $CFG->wwwroot .'/user/view.php?course=1&amp;id='. $template['userid'];
779 $template['moodlepix'] = blog_get_moodle_pix_path();
780 $template['author'] = $blogEntry->entryAuthorName;
781 $template['lastmod'] = $blogEntry->formattedEntryLastModified;
782 $template['created'] = $blogEntry->formattedEntryCreated;
783 $template['publishtomenu'] = $blogEntry->get_publish_to_menu(true, true);
784 $template['groupid'] = $blogEntry->entryGroupId;
785 //forum style printing of blogs
786 blog_print_entry_content ($template, $blogEntry->entryId, $filtertype, $filterselect);
787
788}
789
790//forum style printing of blogs
791function blog_print_entry_content ($template, $entryid, $filtertype='', $filterselect='') {
792 global $USER, $CFG, $course, $ME;
793
794 $stredit = get_string('edit');
795 $strdelete = get_string('delete');
796
797 $user = get_record('user','id',$template['userid']);
798
799 echo '<div align="center"><table cellspacing="0" class="forumpost" width="100%">';
800
801 echo '<tr class="header"><td class="picture left">';
802 print_user_picture($template['userid'], $template['courseid'], $user->picture);
803 echo '</td>';
804
805 echo '<td class="topic starter"><div class="subject">'.$template['title'].'</div><div class="author">';
806 $fullname = fullname($user, isteacher($template['userid']));
807 $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
808 $user->id.'&amp;course='.$course->id.'">'.$fullname.'</a>';
809 $by->date = $template['lastmod'];
810 print_string('bynameondate', 'forum', $by);
811 echo '</div></td></tr>';
812
813 echo '<tr><td class="left side">';
814 if ($group = get_record('groups','id',$template['groupid'])) {
815 print_group_picture($group, $course->id, false, false, true);
816 } else {
817 echo '&nbsp;';
818 }
819
820/// Actual content
821
822 echo '</td><td class="content">'."\n";
823
824 // Print whole message
825 echo format_text($template['body']);
826
827/// Links to tags
828
417195c1 829 if ($blogtags = get_records_sql('SELECT t.* FROM '.$CFG->prefix.'tags t, '.$CFG->prefix.'blog_tag_instance ti
830 WHERE t.id = ti.tagid
831 AND ti.entryid = '.$entryid)) {
4a173181 832 echo '<p />';
86b67cc2 833 print_string('tags');
834 echo ': ';
4a173181 835 foreach ($blogtags as $blogtag) {
836 echo '<a href="index.php?courseid='.$course->id.'&amp;filtertype='.$filtertype.'&amp;filterselect='.$filterselect.'&amp;tagid='.$blogtag->id.'">'.$blogtag->text.'</a>, ';
837 }
838 }
839
840/// Commands
841
842 echo '<div class="commands">';
843
844 if (isset($USER->id)) {
845 if (($template['userid'] == $USER->id) or isteacher($course->id)) {
1a3f39f1 846 echo '<a href="'.$CFG->wwwroot.'/blog/edit.php?editid='.$entryid.'&amp;sesskey='.sesskey().'">'.$stredit.'</a>';
4a173181 847 }
848
849 if (($template['userid'] == $USER->id) or isteacher($course->id)) {
1a3f39f1 850 echo '| <a href="'.$CFG->wwwroot.'/blog/edit.php?act=del&amp;postid='.$entryid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
4a173181 851 }
852 }
853
854 echo '</div>';
855
856 echo '</td></tr></table></div>'."\n\n";
857}
858
859/**
860 * Use this function to retrieve a list of publish states available for
861 * the currently logged in user.
862 *
863 * @return array This function returns an array ideal for sending to moodles'
864 * choose_from_menu function.
865 */
866function blog_applicable_publish_states($courseid='') {
867 global $CFG;
868
869 // everyone gets draft access
870 $options = array ( 'draft' => get_string('publishtonoone', 'blog') );
871 if (is_numeric($courseid) && $courseid != SITEID && $course = get_record('course', 'id', $courseid, '', '', '', '', 'shortname') ) {
872 require_login($courseid);
873 // if we're viewing a course allow publishing to course teachers
874 $options['teacher'] = get_string('publishtoteachers', 'blog', $course->shortname);
875 if (!$CFG->blog_enable_moderation || isadmin() || isteacher($courseid) ) {
876 // only admins and teachers can publish to course members when moderation is enabled
877 $options['course'] = get_string('publishtocourse', 'blog', $course->shortname);
878 }
879 }
880 /*
881 //groups not supported quite yet - pseudocode:
882 if (isset($post->groupid) && $post->groupid != '') {
883 $options['group'] = 'Fellow group members and teachers can view';
884 }*/
885 if (!$CFG->blog_enable_moderation || isadmin() || (is_numeric($courseid) && isteacher($courseid)) ) {
886 // only admins and teachers can see site and public options when moderation is enabled
887 $options['site'] = get_string('publishtosite', 'blog');
888 $options['public'] = get_string('publishtoworld', 'blog');
889 }
890 return $options;
891}
892
893/**
894 * blog_get_entries_by_category - not in blogInfo because entries being searched
895 * can be found in any number of blogs rather than just one.
896 * catids can be an array of category ids or a single category id
897 * defined as either
898 * $catids = array(1, 2, 3, 4, 5);
899 * or
900 * $catids = 2;
901 *
902 * Used by rss.php
903 */
904function blog_get_entries_by_category($catids, $courseid=0, $limit=8, $start=0) {
905 $catsearch = ' e.id=c.entryid AND '; //only gets categories whose postid matches entries retrieved
906 if (blog_array_count($catids) > 0) {
907 $count = 0;
908 foreach ($catids as $catid) {
909 $catsearch .= 'c.categoryid='. $catid .' ';
910 $count += 1;
911 if (count($catids) != $count) {
912 $catsearch .= 'OR ';
913 }
914 }
915 } else {
916 $catsearch .= 'c.categoryid='. $catids;
917 }
918 $wherecourse = '';
919 if (is_numeric($courseid) && $courseid != 0 && $courseid != 1) {
920 $wherecourse = ' AND e.courseid='. $courseid;
921 }
922 $where = $catsearch.$wherecourse;
923// echo 'Debug: where clause for blog_get_entries_by_category: '. $where; //debug
924 return blog_get_recent_entries_byrange($limit, $start, $where, '', true);
925}
926
927/**
928 * builds calendar with links to filter entries by date
929 * modified by Daryl Hawes to build links even when no userid is specified
930 * and to return the html as a string if desired
931 */
932function blog_draw_calendar(&$blogFilter, $return=false)
933{
934 global $CFG;
935// print_object($blogFilter);
936
937 if (!empty($blogFilter->startmonth)) {
938 $m = $blogFilter->startmonth;
939 } else {
940 $m = date('n', mktime());
941 $blogFilter->startmonth = $m;
942 }
943 if (!empty($blogFilter->startyear)) {
944 $y = $blogFilter->startyear;
945 } else {
946 $y = date('Y', mktime());
947 $blogFilter->startyear = $y;
948 }
949 $userid = $blogFilter->userid;
950
951 //create a string to represent a URL argument with userid info. If no userid then string is empty.
952 $useridString = '&amp;userid='. $userid;
953 if ($userid == 0 || $userid == '') {
954 $useridString = '';
955 }
956
957 // calculate the weekday the first of the month is on
958 $tmpd = getdate(mktime(0, 0, 0, $m, 1, $y));
959 $monthname = $tmpd['month'];
960 $firstwday= $tmpd['wday'];
961 $today = date('Ymd', mktime());
962
963 $lastday = blog_mk_getLastDayofMonth($m, $y);
964
965 //determine next and previous month
966 if (($m - 1) < 1) { $pm = 12; } else { $pm = $m - 1; }
967 if (($m + 1) > 12) { $nm = 1; } else { $nm = $m + 1; }
968
969 if (strlen($pm) == 1) { $pm = '0'. $pm; };
970 if (strlen($nm) == 1) { $nm = '0'. $nm; };
971
972 $returnstring = "\n".'<table class="generaltable"><tr>'."\n";
973 $returnstring .= '<td style="text-align: left; width: 12%;">'."\n";
974
975 $currentyear = $y;
976 $currentmonth = $m;
977
978 if (($m - 1) < 1) {
979 $blogFilter->startyear = $y - 1;
980 } else {
981 $blogFilter->startyear = $y;
982 }
983
984 $blogFilter->startmonth = $pm;
985 $self = basename($_SERVER['PHP_SELF']);
986 $returnstring .= $blogFilter->get_complete_link( $self, '&lt;&lt;' , array('startday'))."\n";
987
988 $blogFilter->startyear = $currentyear;
989 $blogFilter->startmonth = $currentmonth;
990
991 $returnstring .= '</td><td style="text-align: center;">'."\n";
992// $returnstring .= $blogFilter->get_complete_link($CFG->wwwroot .'/blog/archive.php', $monthname .' '. $y, array('startday'));
993 $returnstring .= $blogFilter->get_complete_link($self, $monthname .' '. $y, array('startday'));
994
995 if (($m + 1) > 12) {
996 $blogFilter->startyear = $blogFilter->startyear + 1;
997 } else {
998 $blogFilter->startyear = $y;
999 }
1000
1001 $blogFilter->startmonth = $nm;
1002 $returnstring .= '</td><td style="text-align: right; width: 12%;">'."\n";
1003 $returnstring .= $blogFilter->get_complete_link( $self, '&gt;&gt;', array('startday'))."\n";
1004 $returnstring .= '</td></tr>'."\n";
1005
1006 $blogFilter->startyear = $currentyear;
1007 $blogFilter->startmonth = $currentmonth;
1008
1009 $returnstring .= '<tr><td colspan="3">'."\n";
1010 $returnstring .= '<table class="calendarmini"><thead>'."\n";
1011 $returnstring .= '<tr><td width="19" align="center" class="calday">'. get_string('calsun', 'blog') .'</td>'."\n";
1012 $returnstring .= '<td width="19" align="center" class="calday">'. get_string('calmon', 'blog') .'</td>'."\n";
1013 $returnstring .= '<td width="19" align="center" class="calday">'. get_string('caltue', 'blog') .'</td>'."\n";
1014 $returnstring .= '<td width="19" align="center" class="calday">'. get_string('calwed', 'blog') .'</td>'."\n";
1015 $returnstring .= '<td width="19" align="center" class="calday">'. get_string('calthu', 'blog') .'</td>'."\n";
1016 $returnstring .= '<td width="19" align="center" class="calday">'. get_string('calfri', 'blog') .'</td>'."\n";
1017 $returnstring .= '<td width="19" align="center" class="calday">'. get_string('calsat', 'blog') .'</td></tr></thead><tbody>'."\n";
1018
1019 $d = 1;
1020 $wday = $firstwday;
1021 $firstweek = true;
1022
1023 // loop through all the days of the month
1024 while ( $d <= $lastday)
1025 {
1026 // set up blank days for first week
1027 if ($firstweek) {
1028 $returnstring .= '<tr>'."\n";
1029 for ($i=1; $i <= $firstwday; $i++) {
1030 $returnstring .= '<td>&nbsp;</td>'."\n";
1031 }
1032 $firstweek = false;
1033 }
1034
1035 // Sunday start week with <tr>
1036 if ($wday==0) {
1037 $returnstring .= '<tr>'."\n";
1038 }
1039
1040 $mo = $m;
1041 if ($mo < 10) {
1042 if (!preg_match("/0\d/", $mo)) {
1043 $mo = '0'. $mo;
1044 }
1045 }
1046
1047 // Look for blog entries for this day
1048 $tstart = blog_get_month_time($y, $m, $d, true);
1049 $tend = blog_get_month_time($y, $m, $d + 1, false);
1050 $where = " lastmodified >= $tstart AND lastmodified <= $tend ";
1051
1052 if ($userid != 0 && $userid != '') {
1053 $where .= ' AND author = '. $userid .' ';
1054 }
1055
1056 $count = count_records_select('post', $where);
1057
1058//echo 'Where clause: '. $where .' | count:'. $count. '<br />'."\n"; //debug
1059 $da = $d;
1060 if($da < 10) {
1061 if(!preg_match("/0\d/", $da)) {
1062 $da = "0". $da;
1063 }
1064 }
1065 // check for event
1066 $showdate = $y . $mo . $da;
1067
1068 $returnstring .= '<td align=center';
1069 if ($showdate == $today) {
1070 $returnstring .= ' class="cal_today"';
1071 }
1072 if ($wday == 6 || $wday == 0) {
1073 $returnstring .= ' class="cal_weekend"';
1074 }
1075 $returnstring .= '>'."\n";
1076
1077 // if entries are found, output link to that day's entries
1078 if ($count > 0) {
1079 $blogFilter->startday = $d;
1080 $returnstring .= $blogFilter->get_complete_link($CFG->wwwroot .'/blog/index.php', $d);
1081 } else {
1082 $returnstring .= $d."\n";
1083 }
1084 $returnstring .= '</td>'."\n";
1085
1086 // Saturday end week with </tr>
1087 if ($wday == 6) {
1088 $returnstring .= '</tr>'."\n";
1089 }
1090
1091 $wday++;
1092 $wday = $wday % 7;
1093 $d++;
1094 }
1095
1096 if ($wday != 0) {
1097 for($i = $wday; $i < 7; $i++) {
1098 $returnstring .= '<td>&nbsp;</td>'."\n";
1099 }
1100 $returnstring .= '</tr>'."\n";
1101 }
1102
1103 $returnstring .= '</table>'."\n";
1104
1105 $returnstring .= '</td></tr></tbody></table>'."\n";
1106
1107 if ($return) {
1108 return $returnstring;
1109 }
1110 print $returnstring;
1111// end blog_draw_calendar function
1112}
1113
1114/**
1115 * get the last day of the month
1116 */
1117function blog_mk_getLastDayofMonth($mon, $year)
1118{
1119 for ($tday=28; $tday <= 31; $tday++)
1120 {
1121 $tdate = getdate(mktime(0, 0, 0, $mon, $tday, $year));
1122 if ($tdate['mon'] != $mon)
1123 { break; }
1124
1125 }
1126 $tday--;
1127
1128 return $tday;
1129}
1130
1131/**
1132 * blog_safeHTML
1133 * Clean up user input (currently unused, moodle's format_text() is preferable)
1134 */
1135function blog_safeHTML($html, $tags = 'b|br|i|u|ul|ol|li|p|a|blockquote|em|strong') {
1136// removes all tags that are considered unsafe
1137// Adapted from a function posted in the comments about the strip_tags()
1138// function on the php.net web site.
1139//
1140// This function is not perfect! It can be bypassed!
1141// Remove any nulls from the input
1142 $html = preg_replace('/\0/', '', $html);
1143
1144 // convert the ampersands to null characters (to save for later)
1145 $html = preg_replace('/&/', '\0', $html);
1146
1147 // convert the sharp brackets to their html code and escape special characters such as "
1148 $html=htmlspecialchars($html);
1149
1150 // restore the tags that are considered safe
1151 if ($tags) {
1152 // Fix start tags
1153 $html = preg_replace("/&lt;(($tags).*?)&gt;/i", '<$1>', $html);
1154 // Fix end tags
1155 $html = preg_replace("/&lt;\/($tags)&gt;/i", '</$1>', $html);
1156 // Fix quotes
1157 $html = preg_replace("/&quot;/", '"', $html);
1158 $html = addslashes($html);
1159 // Don't allow, e.g. <a href="javascript:evil_code">
1160 $html = preg_replace("/<($tags)([^>]*)>/ie", "'<$1' . stripslashes_safe(str_replace('javascript', 'hackerscript', '$2')) .'>'", $html);
1161 // Don't allow, e.g. <img src="foo.gif" onmouseover="evil_javascript">
1162 $html = preg_replace("/<($tags)([^>]*)>/ie", "'<$1' . stripslashes_safe(str_replace(' on', ' off', '$2')) .'>'", $html);
1163 $html = stripslashes_safe($html);
1164
1165 }
1166
1167 // restore the ampersands
1168 $html = preg_replace('/\0/', '&', $html);
1169
1170 return($html);
1171} // safeHTML
1172
1173// I don't like how the PHP count() function returns 1 if
1174// you pass it a scalar. So this is my custom function that
1175// will return 0 if the argument isn't an array.
1176function blog_array_count($arr) {
1177 if (!is_array($arr)) {
1178 return 0;
1179 }
1180
1181 return count($arr);
1182}
1183
1184/**
1185* check_dir_exists
1186 * Function to check if a directory exists
1187 * and, optionally, create it
1188 * copied from moodle/backup/lib.php
1189 */
1190if (! function_exists('check_dir_exists')) {
1191 function check_dir_exists($dir, $create=false) {
1192
1193 global $CFG;
1194
1195 $status = true;
1196 if (!is_dir($dir)) {
1197 if (!$create) {
1198 $status = false;
1199 } else {
1200 umask(0000);
1201 $status = mkdir ($dir, $CFG->directorypermissions);
1202 }
1203 }
1204 return $status;
1205 }
1206}
1207
1208/////////////// Time and Date display functions ///////////////
1209
1210/*
1211 * Returns the current time as a readable date string
1212 * using moodle's chosen full date display format from admin configuration.
1213 */
1214function blog_now() {
1215 $strftimedaydatetime = get_string('strftimedaydatetime');
1216 $date = userdate(time(), $strftimedaydatetime);
1217 return $date;
1218}
1219
1220/**
1221* UNIX timestamp to a readable format.
1222 * using moodle's chosen date format from admin configuration.
1223 */
1224function blog_format_date($datetime) {
1225 $strftimedate = get_string('strftimedate');
1226 $date = userdate($datetime, $strftimedate);
1227 return $date;
1228}
1229
1230/**
1231* converts unix timestamp to just a date
1232 * using moodle's chosen short date format from admin configuration.
1233 */
1234function blog_short_date($datetime) {
1235 $strftimedateshort = get_string('strftimedateshort');
1236 $date = userdate($datetime, $strftimedateshort);
1237 return $date;
1238}
1239
1240/**
1241* converts unix timestamp to just a date
1242 * using moodle's chosen time format from admin configuration.
1243 */
1244function blog_short_time($datetime) {
1245 $strftimetime = get_string('strftimetime');
1246 $time = userdate($datetime, $strftimedateshort);
1247 return $time;
1248}
1249
1250/////////////// Trackback functions ///////////////
1251
1252// Note: trackback specification
1253// http://www.movabletype.org/docs/mttrackback.html
1254
1255
1256/**
1257 * generate rdf for trackback autodiscovery
1258 */
1259function blog_get_trackback_rdf_string($blogEntry) {
1260
1261 global $CFG;
1262 $userid = $blogEntry->entryuserid;
1263 $entryid = $blogEntry->entryId;
1264 $blogInfo = new BlogInfo($userid);
1265
1266// echo 'in blog_get_trackback_rdf_string blogEntry:<br />'."\n"; //debug
1267// print_object($blogEntry); //debug
1268
1269 $rdf = "\n".'<!-- //RDF for trackback autodiscovery
1270<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
1271 xmlns:dc="http://purl.org/dc/elements/1.1/"
1272 xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
1273<rdf:Description
1274 rdf:about="'. htmlentities($CFG->wwwroot .'/blog/archive.php?userid='. $userid .'&amp;postid='. $entryid) .'"
1275 dc:identifier="'. htmlentities($CFG->wwwroot .'/blog/archive.php?userid='. $userid .'&amp;postid='. $entryid) .'"
1276 dc:title="'. $blogInfo->blogtitle .'"
1277 trackback:ping="';
1278 if ($CFG->slasharguments) {
1279 $rdf .= $CFG->wwwroot .'/blog/tb.php/'. $entryid;
1280 } else {
1281 $rdf .= $CFG->wwwroot .'/blog/tb.php?file=/'. $entryid;
1282 }
1283 $rdf .='" />
1284</rdf:RDF>
1285-->'."\n";
1286 unset($blogInfo); //clean up after ourselves
1287 return $rdf;
1288}
1289
1290
4a173181 1291/**
1292 * Return a list of trackbacks for a particular id
1293 */
1294function blog_list_trackbacks($postid) {
1295 global $db, $CFG;
1296
1297 //Daryl Hawes note: convert this sql statement to a moodle function call
1298 $sql = 'SELECT * FROM '. $CFG->prefix .'blog_trackback WHERE entryid = '. $postid;
1299 $res = $db->Execute($sql);
1300
1301 //iterate results
1302 $list = array();
1303 while( !$res->EOF && isset($res->fields) ) {
1304 $list[] = $res->fields;
1305 $res->MoveNext();
1306 }
1307
1308 return $list;
1309}
1310
4a173181 1311
1312/////////////////////// CATEGORY MANAGEMENT ////////////////////////////////////
1313
1314
1315
1316
1317/**
1318 *called by blog_category_list
1319 * @return string html links for each category
1320 */
1321function blog_get_html_display_for_categories(&$blogFilter, $shownumentries, &$records, $showseparators, $title, $section) {
1322 global $CFG, $editing;
1323 $returnstring ='';
1324 if ($showseparators) {
1325 //first show a separator if requested
1326 $returnstring .= $title .'<br />'."\n";
1327 }
1328 $editallowed = false;
1329
1330 if ($editing) {
1331 $isteacher = false;
1332 if (isset($blogFilter->courseid)) {
1333 $isteacher = isteacher($blogFilter->courseid);
1334 }
1335 if ( isadmin() ) {
1336 // admin is allowed to edit any categories on the site
1337 $editallowed = true;
1338 } else if ( ($section == 'course' || $section == 'group') && $isteacher ) {
1339 // teacher of course can modify course categories and group categories
1340 $editallowed = true;
1341 } else if ($section == 'personal' && blog_is_blog_admin($blogFilter->userid) ) {
1342 // user can modify their own blog categories
1343 $editallowed = true;
1344 }
1345 }
1346
1347 if (!isset($records) ) {
1348 return;
1349 }
1350
1351 foreach($records as $record) {
1352 $catcount = '';
1353 $categoryid = $record->id;
1354 $categoryname = $record->catname;
1355 if ($shownumentries) {
1356 $tempfilter =& new BlogFilter('', $categoryid);
1357 $catcount = ' (';
1358 $catcount .= $tempfilter->get_filtered_entry_count();
1359 $catcount .= ')';
1360 }
1361 $blogFilter->categoryid = $categoryid;
1362 $returnstring .= $blogFilter->get_complete_link($CFG->wwwroot .'/blog/index.php', stripslashes_safe($categoryname) . $catcount);
1363 if ($editallowed) {
1364 // note that only the 'act' and 'categoryid' vars are needed here because the me() function includes the
1365 // existing query string
1366 $returnstring .= '&nbsp;<a href="'. me() .'&amp;act=editcategory&amp;categoryid='. $categoryid .'">';
1367 $returnstring .= '<img src="'. $CFG->pixpath .'/t/edit.gif" alt="'. get_string('edit');
1368 $returnstring .= '" title="'. get_string('edit') .'" align="absmiddle" height="16" width="16" border="0" /></a>'."\n";
1369 if ($categoryid != 1) { //do not remove "General" sitewide category
1370 $returnstring .= '&nbsp;<a href="'. $CFG->wwwroot .'/blog/admin.php?act=delcategory&amp;categoryid='. $categoryid;
1371 $returnstring .= '&amp;userid='. $blogFilter->userid .'&amp;courseid='. $blogFilter->courseid .'&amp;groupid='. $blogFilter->groupid .'" onClick="return confirm(\''. get_string('confirmcategorydelete', 'blog') .'\');">';
1372 $returnstring .= '<img src="'. $CFG->pixpath .'/t/delete.gif" ALT="'. get_string('delete');
1373$returnstring .= '" title="'. get_string('delete') .'" align="absmiddle" border="0" /></a>'."\n";
1374 }
1375 }
1376 $returnstring .= '<br />'."\n";
1377 }
1378 return $returnstring;
1379}
1380
1381/**
1382 *
1383 */
1384function blog_get_popup_display_for_categories(&$blogFilter, $format, $shownumentries, &$records) {
1385 global $CFG;
1386
1387 if (!isset($records) ) {
1388 return;
1389 }
1390 $returnstring = '';
1391
1392 foreach ($records as $record) {
1393 if ($format == 2) {
1394 $value = $CFG->wwwroot .'/blog/index.php?categoryid='. $record->id;
1395 $value .= '&amp;userid='. $blogFilter->userid .'&amp;courseid='. $blogFilter->courseid .'&amp;groupid='. $blogFilter->groupid;
1396 } else {
1397 $value = $record->id;
1398 }
1399
1400 $returnstring .= '<option value="'. $value .'"';
1401 if ($record->id == $blogFilter->categoryid) {
1402 $returnstring .= ' selected';
1403 }
1404 $catcount = '';
1405 $categoryid = $record->id;
1406 $categoryname = $record->catname;
1407 if ($shownumentries) {
1408 $tempfilter =& new BlogFilter('', $categoryid);
1409 $tempfilter->userid = $blogFilter->userid;
1410 $catcount = ' (';
1411 //if we had an array of blogentry objects we could avoid a database call
1412 //and instead simply ask the blogentry objects to tell us which apply
1413 $catcount .= $tempfilter->get_filtered_entry_count();
1414 $catcount .= ')';
1415 }
1416 $returnstring .= '>' ."\n". stripslashes_safe($categoryname) . $catcount ."\n";
1417 $returnstring .= '</option>';
1418 }
1419 return $returnstring;
1420}
1421?>