diff --git a/blog/blogpage.php b/blog/blogpage.php index 0df6f869690..66cf8cbfddc 100644 --- a/blog/blogpage.php +++ b/blog/blogpage.php @@ -49,7 +49,7 @@ class page_blog extends page_base { } // I need to determine how best to utilize this function. Most init // is already done before we get here in blogFilter and blogInfo - $this->bloginfo =& new BlogInfo($this->id); + //$this->bloginfo =& new BlogInfo($this->id); if ($this->courseid == 0 || $this->courseid == 1 || !is_numeric($this->courseid) ) { $this->courseid = ''; $courserecord = NULL; diff --git a/blog/edit.html b/blog/edit.html index bac1c9614a1..91f7479bb32 100755 --- a/blog/edit.html +++ b/blog/edit.html @@ -1,6 +1,5 @@ courseid); + if (!isset($post->groupid)) { $post->groupid = 0; } @@ -130,7 +129,7 @@ - body, $post->courseid); ?> + body); ?> diff --git a/blog/edit.php b/blog/edit.php index 7b9d40dee03..4ac9568cf7e 100755 --- a/blog/edit.php +++ b/blog/edit.php @@ -2,7 +2,6 @@ require_once('../config.php'); include_once('lib.php'); -include_once('class.BlogInfo.php'); require_login(); $userid = optional_param('userid', 0, PARAM_INT); @@ -31,9 +30,9 @@ if (!empty($userid) && $userid != 0) { } } else if ( blog_isLoggedIn() ) { //the user is logged in and have not specified a blog - so they will be editing their own - $tempBlogInfo = blog_user_bloginfo(); - $userid = $tempBlogInfo->userid; - unset($tempBlogInfo); //free memory from temp object - bloginfo will be created again in the included header + //$tempBlogInfo = blog_user_bloginfo(); + $userid = $USER->id;//$tempBlogInfo->userid; + //unset($tempBlogInfo); //free memory from temp object - bloginfo will be created again in the included header } else { error(get_string('noblogspecified', 'blog') .'' .get_string('viewentries', 'blog') .''); } @@ -42,12 +41,10 @@ $pageNavigation = 'edit'; include($CFG->dirroot .'/blog/header.php'); -//print_object($PAGE->bloginfo); //debug - //check if user is in blog's acl -if ( !blog_user_has_rights($PAGE->bloginfo) ) { +if ( !blog_user_has_rights($editid) ) { if ($editid != '') { - $blogEntry = $PAGE->bloginfo->get_blog_entry_by_id($editid); + $blogEntry = get_record('post','id',$editid); if (! (isteacher($blogEntry->$entryCourseId)) ) { // error( get_string('notallowedtoedit'.' You do not teach in this course.', 'blog'), $CFG->wwwroot .'/login/index.php'); error( get_string('notallowedtoedit', 'blog'), $CFG->wwwroot .'/login/index.php'); @@ -61,21 +58,20 @@ if ( !blog_user_has_rights($PAGE->bloginfo) ) { if (isset($act) && ($act == 'del') && confirm_sesskey()) { - $postid = required_param('postid', PARAM_INT); + $postid = required_param('editid', PARAM_INT); if (optional_param('confirm',0,PARAM_INT)) { - do_delete($PAGE->bloginfo, $postid); + do_delete($postid); } else { /// prints blog entry and what confirmation form echo '
'; echo ''; echo ''; - echo ''; + echo ''; echo ''; print_string('blogdeleteconfirm', 'blog'); $post = get_record('post', 'id', $postid); - $entry = new BlogEntry($post); - blog_print_entry($entry); + blog_print_entry($post); echo '
'; echo ' '; echo ' '; @@ -96,24 +92,16 @@ if (($post = data_submitted( get_referer() )) && confirm_sesskey()) { if (!empty($post->editform)) { //make sure we're processing the edit form here //print_object($post); //debug - ///these varaibles needs to be changed because of the javascript hack - ///post->courseid - ///post->groupid - $post->courseid = $post->realcourse; //might not need either, if javascript re-written - $post->groupid = $post->realgroup; //might not need - $courseid = $post->realcourse; - //end of yu's code - if (!$post->etitle or !$post->body) { $post->error = get_string('emptymessage', 'forum'); } if ($post->act == 'save') { - do_save($post, $PAGE->bloginfo); + do_save($post); } else if ($post->act == 'update') { - do_update($post, $PAGE->bloginfo); + do_update($post); } else if ($post->act == 'del') { $postid = required_param('postid', PARAM_INT); - do_delete($PAGE->bloginfo, $postid); + do_delete($postid); } } } else { @@ -123,7 +111,6 @@ if (($post = data_submitted( get_referer() )) && confirm_sesskey()) { $post->userid = $USER->id; $post->body = ''; $post->format = $defaultformat; - $post->categoryid = array(1); $post->publishstate = 'draft'; $post->courseid = $courseid; @@ -132,16 +119,15 @@ if (($post = data_submitted( get_referer() )) && confirm_sesskey()) { if ($editid != '') { // User is editing a post // ensure that editing is allowed first - admin users can edit any posts - $blogEntry = $PAGE->bloginfo->get_blog_entry_by_id($editid); + $blogEntry = get_record('post','id',$editid); //using an unformatted entry body here so that extra formatting information is not stored in the db - $post->body = $blogEntry->get_unformatted_entry_body(); - $post->etitle = $blogEntry->entryTitle; + $post->body = $blogEntry->summary; + $post->etitle = $blogEntry->subject; $post->postid = $editid; - $post->userid = $PAGE->bloginfo->userid; - $post->categoryid = $blogEntry->entryCategoryIds; - $post->format = $blogEntry->entryFormat; - $post->publishstate = $blogEntry->entryPublishState; + $post->userid = $blogEntry->userid; + $post->format = $blogEntry->format; + $post->publishstate = $blogEntry->publishstate; } if (isset($post->postid) && ($post->postid != -1) ) { @@ -179,27 +165,34 @@ include($CFG->dirroot .'/blog/footer.php'); * takes $bloginfo_arg argument as reference to a blogInfo object. * also takes the postid - the id of the entry to be removed */ -function do_delete(&$bloginfo_arg, $postid) { - global $CFG; +function do_delete($postid) { + global $CFG, $USER; // make sure this user is authorized to delete this entry. // cannot use $post->pid because it may not have been initialized yet. Also the pid may be in get format rather than post. - if ($bloginfo_arg->delete_blog_entry_by_id($postid)) { - //echo "bloginfo_arg:"; //debug - print_object($bloginfo_arg); //debug - //echo "pid to delete:".$postid; //debug - delete_records('blog_tag_instance', 'entryid', $postid); - print ''. get_string('entrydeleted', 'blog') .'

'; + // check ownership + $post = get_record('post','id',$postid); - //record a log message of this entry deletion - if ($site = get_site()) { - add_to_log($site->id, 'blog', 'delete', 'index.php?userid='. $bloginfo_arg->userid, 'deleted blog entry with entry id# '. $postid); + if (($USER->id == $post->userid) || (blog_is_blog_admin($post->userid)) || (isadmin())) { + + if (delete_records('post','id',$postid)) { + //echo "bloginfo_arg:"; //debug + //print_object($bloginfo_arg); //debug + //echo "pid to delete:".$postid; //debug + delete_records('blog_tag_instance', 'entryid', $postid); + print ''. get_string('entrydeleted', 'blog') .'

'; + + //record a log message of this entry deletion + if ($site = get_site()) { + add_to_log($site->id, 'blog', 'delete', 'index.php?userid='. $bloginfo_arg->userid, 'deleted blog entry with entry id# '. $postid); + } } - } else { + } + else { error(get_string('entryerrornotyours', 'blog')); } //comment out this redirect to debug the deletion of entries - redirect($CFG->wwwroot .'/blog/index.php?userid='. $bloginfo_arg->userid); + redirect($CFG->wwwroot .'/blog/index.php?userid='. $post->userid); } /** @@ -208,7 +201,7 @@ function do_delete(&$bloginfo_arg, $postid) { * @param object $post argument is a reference to the post object which is used to store information for the form * @param object $bloginfo_arg argument is reference to a blogInfo object. */ -function do_save(&$post, &$bloginfo_arg) { +function do_save($post) { global $USER, $CFG; // echo 'Debug: Post object in do_save function of edit.php
'; //debug // print_object($post); //debug @@ -217,42 +210,58 @@ function do_save(&$post, &$bloginfo_arg) { $post->error = get_string('nomessagebodyerror', 'blog'); } else { + /// Write a blog entry into database + $blogEntry = new object; + $blogEntry->subject = addslashes($post->etitle); + $blogEntry->summary = addslashes($post->body); + $blogEntry->module = 'blog'; + $blogEntry->userid = $USER->id; + $blogEntry->format = $post->format; + $blogEntry->publishstate = $post->publishstate; + $blogEntry->lastmodified = time(); + $blogEntry->created = time(); + // Insert the new blog entry. - $entryID = $bloginfo_arg->insert_blog_entry($post->etitle, $post->body, $USER->id, $post->format, $post->publishstate, $courseid, $groupid); + $entryID = insert_record('post',$blogEntry); // print 'Debug: created a new entry - entryId = '.$entryID.'
'; //debug // echo 'Debug: do_save() in edit.php calling blog_do_*back_pings
'."\n"; //debug + if ($entryID) { - $otags = optional_param('otags','', PARAM_INT); - $ptags = optional_param('ptags','', PARAM_INT); + /// Creates a unique hash. I don't know what this is for (Yu) + $dataobject = new object; + $dataobject->uniquehash = md5($blogEntry->userid.$CFG->wwwroot.$entryID); + update_record('post', $dataobject); + /// Associate tags with entries + $otags = optional_param('otags','', PARAM_INT); + $ptags = optional_param('ptags','', PARAM_INT); - $tag = NULL; - $tag->entryid = $entryID; - $tag->groupid = $post->groupid; - $tag->courseid = $post->courseid; - $tag->userid = $USER->id; - $tag->timemodified = time(); + $tag = NULL; + $tag->entryid = $entryID; + $tag->userid = $USER->id; + $tag->timemodified = time(); - /// Add tags information - foreach ($otags as $otag) { - $tag->tagid = $otag; - insert_record('blog_tag_instance',$tag); + /// Add tags information + foreach ($otags as $otag) { + $tag->tagid = $otag; + insert_record('blog_tag_instance',$tag); + } + + foreach ($ptags as $ptag) { + $tag->tagid = $ptag; + insert_record('blog_tag_instance',$tag); + } + + print ''. get_string('entrysaved', 'blog') .'
'; } - - foreach ($ptags as $ptag) { - $tag->tagid = $ptag; - insert_record('blog_tag_instance',$tag); - } - - print ''. get_string('entrysaved', 'blog') .'
'; //record a log message of this entry addition if ($site = get_site()) { add_to_log($site->id, 'blog', 'add', 'archive.php?userid='. $bloginfo_arg->userid .'&postid='. $entryID, 'created new blog entry with entry id# '. $entryID); } //to debug this save function comment out the following redirect code if ($courseid == SITEID || $courseid == 0 || $courseid == '') { - redirect($CFG->wwwroot .'/blog/index.php?userid='. $bloginfo_arg->userid); + redirect($CFG->wwwroot .'/blog/index.php?userid='. $blogEntry->userid); } else { redirect($CFG->wwwroot .'/course/view.php?id='. $courseid); } @@ -264,28 +273,29 @@ function do_save(&$post, &$bloginfo_arg) { * @param . $bloginfo_arg argument is reference to a blogInfo object. * @todo complete documenting this function. enable trackback and pingback between entries on the same server */ -function do_update(&$post, &$bloginfo) { - +function do_update($post) { + // here post = data_submitted(); global $CFG, $USER; - - $blogentry = $bloginfo->get_blog_entry_by_id($post->postid); - echo "id id ".$post->postid; + $blogEntry = get_record('post','id',$post->postid); +// echo "id id ".$post->postid; // print_object($blogentry); //debug - $blogentry->set_body($post->body); - $blogentry->set_format($post->format); - $blogentry->set_publishstate($post->publishstate); //we don't care about the return value here + $blogEntry->subject = addslashes($post->etitle); + $blogEntry->summary = addslashes($post->body); + if ($blogEntry->summary == '
') { + $blogEntry->summary = ''; + } + $blogEntry->format = $post->format; + $blogEntry->publishstate = $post->publishstate; //we don't care about the return value here - if ( !$error = $blogentry->save() ) { - delete_records('blog_tag_instance', 'entryid', $blogentry->entryId); + if ( update_record('post',$blogEntry)) { + delete_records('blog_tag_instance', 'entryid', $blogEntry->id); $otags = optional_param('otags','', PARAM_INT); $ptags = optional_param('ptags','', PARAM_INT); $tag = NULL; - $tag->entryid = $blogentry->entryId; - $tag->groupid = $post->groupid; - $tag->courseid = $post->courseid; + $tag->entryid = $blogEntry->id; $tag->userid = $USER->id; $tag->timemodified = time(); @@ -310,7 +320,7 @@ function do_update(&$post, &$bloginfo) { add_to_log($site->id, 'blog', 'update', 'archive.php?userid='. $bloginfo->userid .'&postid='. $post->postid, 'updated existing blog entry with entry id# '. $post->postid); } - redirect($CFG->wwwroot .'/blog/index.php?userid='. $bloginfo->userid); + redirect($CFG->wwwroot .'/blog/index.php?userid='. $blogEntry->userid); } else { // get_string('', 'blog') //Daryl Hawes note: localize this line $post->error = 'There was an error updating this post in the database: '. $error; diff --git a/blog/index.php b/blog/index.php index ad3621b53b1..7c5617d7241 100755 --- a/blog/index.php +++ b/blog/index.php @@ -27,7 +27,6 @@ $filtertype = optional_param('filtertype', '', PARAM_ALPHA); $filterselect = optional_param('filterselect', 0, PARAM_INT); /// overwrite filter code here -/// the the following code does the rights checkings? if ($filtertype) { switch ($filtertype) { @@ -84,7 +83,6 @@ if ($filtertype) { $filterselect = ''; } - /// rights checking switch ($filtertype) { @@ -135,7 +133,7 @@ if ($limit == 'none') { $limit = get_user_preferences('blogpagesize',10); } -$blogFilter =& new BlogFilter($userid, $postid, $limit, $start,$filtertype, $filterselect, $tagid, $tag); +//$blogFilter =& new BlogFilter($userid, $postid, $limit, $start,$filtertype, $filterselect, $tagid, $tag); include($CFG->dirroot .'/blog/header.php'); @@ -147,7 +145,9 @@ if (!$course) { } require_once($CFG->dirroot .'/user/tabs.php'); -blog_print_html_formatted_entries($blogFilter, $filtertype, $filterselect); +$blogpage = optional_param('blogpage',0,PARAM_INT); + +blog_print_html_formatted_entries($userid, $postid, $limit, ($blogpage * $limit) ,$filtertype, $filterselect, $tagid, $tag, $filtertype, $filterselect); include($CFG->dirroot .'/blog/footer.php'); diff --git a/blog/lib.php b/blog/lib.php index fd31104517f..2843a6b1a67 100755 --- a/blog/lib.php +++ b/blog/lib.php @@ -1,381 +1,599 @@ dirroot .'/blog/class.BlogInfo.php'); -require_once($CFG->dirroot .'/blog/class.BlogEntry.php'); -require_once($CFG->dirroot .'/blog/class.BlogFilter.php'); -require_once($CFG->libdir .'/blocklib.php'); -require_once($CFG->libdir .'/pagelib.php'); -require_once('rsslib.php'); -require_once($CFG->dirroot .'/blog/blogpage.php'); + /** + * Library of functions and constants for blog + */ -/* blog access level constant declaration */ -define ('BLOG_USER_LEVEL', 1); -define ('BLOG_GROUP_LEVEL', 2); -define ('BLOG_COURSE_LEVEL', 3); -define ('BLOG_SITE_LEVEL', 4); -define ('BLOG_GLOBAL_LEVEL', 5); + require_once($CFG->libdir .'/blocklib.php'); + require_once($CFG->libdir .'/pagelib.php'); + require_once('rsslib.php'); + require_once($CFG->dirroot .'/blog/blogpage.php'); -/** - * Definition of blogcourse page type (blog page with course id present). - */ -//not used at the moment, and may not need to be -define('PAGE_BLOG_COURSE_VIEW', 'blog_course-view'); + /* blog access level constant declaration */ + define ('BLOG_USER_LEVEL', 1); + define ('BLOG_GROUP_LEVEL', 2); + define ('BLOG_COURSE_LEVEL', 3); + define ('BLOG_SITE_LEVEL', 4); + define ('BLOG_GLOBAL_LEVEL', 5); -$BLOG_YES_NO_MODES = array ( '0' => get_string('no'), - '1' => get_string('yes') ); + /** + * Definition of blogcourse page type (blog page with course id present). + */ + //not used at the moment, and may not need to be + define('PAGE_BLOG_COURSE_VIEW', 'blog_course-view'); -//set default setting for $CFG->blog_* vars used by blog's blocks -//if they are not already. Otherwise errors are thrown -//when an attempt is made to use an empty var. -if (empty($SESSION->blog_editing_enabled)) { - $SESSION->blog_editing_enabled = false; -} + $BLOG_YES_NO_MODES = array ( '0' => get_string('no'), + '1' => get_string('yes') ); -/** - * Verify that a user is logged in based on the session - * @return bool True if user has a valid login session - */ -function blog_isLoggedIn() { - global $USER; - if (!isguest() && isset($USER) and isset($USER->id) and $USER->id) { - return 1; + //set default setting for $CFG->blog_* vars used by blog's blocks + //if they are not already. Otherwise errors are thrown + //when an attempt is made to use an empty var. + if (empty($SESSION->blog_editing_enabled)) { + $SESSION->blog_editing_enabled = false; } - return 0; -} -/** - * blog_user_has_rights - returns true if user is the blog's owner or a moodle admin. - * - * @param BlogInfo blogInfo - a BlogInfo object passed by reference. This object represents the blog being accessed. - * @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. - */ -function blog_user_has_rights(&$bloginfo, $uid='') { - global $USER; - if (isset($bloginfo) && isset($bloginfo->userid)) { + /** + * Verify that a user is logged in based on the session + * @return bool True if user has a valid login session + */ + function blog_isLoggedIn() { + global $USER; + if (!isguest() && isset($USER) and isset($USER->id) and $USER->id) { + return 1; + } + return 0; + } + + /** + * blog_user_has_rights - returns true if user is the blog's owner or a moodle admin. + * + * @param BlogInfo blogInfo - a BlogInfo object passed by reference. This object represents the blog being accessed. + * @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. + */ + function blog_user_has_rights($entryID, $uid='') { + global $USER; + if ($uid == '') { if ( isset($USER) && isset($USER->id) ) { $uid = $USER->id; } - } + } if ($uid == '') { //if uid is still empty then the user is not logged in return false; } if (blog_is_blog_admin($uid) || isadmin()) { return true; - } - } - return false; -} - -/** - * Determines whether a user is an admin for a blog - * @param int $blog_userid The id of the blog being checked - */ -function blog_is_blog_admin($blog_userid) { - global $USER; + } + $blogEntry = get_record('post','id',$entryID); + + return ($blogEntry->userid == $uid); - //moodle admins are admins - if (isadmin()) { - return true; - } - if ( empty($USER) || !isset($USER->id) ) { - return false; - } - if ( empty($blog_userid)) { - return true; } - // Return true if the user is an admin for this blog - if ($blog_userid == $USER->id) { - return true; - } else { - return false; - } -} - -/** - * Adaptation of isediting in moodlelib.php for blog module - * @return bool - */ -function blog_isediting() { - global $SESSION; - if (! isset($SESSION->blog_editing_enabled)) { - $SESSION->blog_editing_enabled = false; - } - return ($SESSION->blog_editing_enabled); -} - -/** - * blog_user_bloginfo - * - * returns a blogInfo object if the user has a blog in the acl table - * This function stores the currently logged in user's bloginfo object - * statically - do not release/unset the returned object. - * added by Daryl Hawes for moodle integration - * $userid - if no userid specified it will attempt to use the logged in user's id - */ -function blog_user_bloginfo($userid='') { -//Daryl Hawes note: not sure that this attempt at optimization is correct -// static $bloginfosingleton; //store the logged in user's bloginfo in a static var - global $USER; - if ($userid == '') { + /** + * Determines whether a user is an admin for a blog + * @param int $blog_userid The id of the blog being checked + */ + function blog_is_blog_admin($blog_userid) { global $USER; - if (!isset($USER) || !isset($USER->id)) { + + //moodle admins are admins + if (isadmin()) { + return true; + } + if ( empty($USER) || !isset($USER->id) ) { + return false; + } + if ( empty($blog_userid)) { + return true; + } + + // Return true if the user is an admin for this blog + if ($blog_userid == $USER->id) { + return true; + } else { + return false; + } + } + + /** + * Adaptation of isediting in moodlelib.php for blog module + * @return bool + */ + function blog_isediting() { + global $SESSION; + if (! isset($SESSION->blog_editing_enabled)) { + $SESSION->blog_editing_enabled = false; + } + return ($SESSION->blog_editing_enabled); + } + + /** + * This function is in lib and not in BlogInfo because entries being searched + * might be found in any number of blogs rather than just one. + * + * $@param BlogFilter blogFilter - a BlogFilter object containing the settings for finding appropriate entries for display + */ + function blog_print_html_formatted_entries($userid, $postid, $limit, $start, $filtertype, $filterselect, $tagid, $tag, $filtertype, $filterselect) { + global $CFG, $USER; + + $blogpage = optional_param('blogpage', 0, PARAM_INT); + $bloglimit = get_user_preferences('blogpagesize',10); // expose as user pref when MyMoodle comes around + + // First let's see if the batchpublish form has submitted data + $post = data_submitted(); + + $morelink = '
  '; + // show personal or general heading block as applicable + echo '

'; + //show blog title - blog tagline + print "
"; //don't print title. blog_get_title_text(); + + $blogEntries = fetch_entries($userid, $postid, $limit, $start, $filtertype, $filterselect, $tagid, $tag, $sort='lastmodified DESC', $limit=true); + + //$blogFilter->get_filtered_entries(); + // show page next/previous links if applicable + + print_paging_bar(get_viewable_entry_count($userid, $postid, $limit, $start,$filtertype, $filterselect, $tagid, $tag, $sort='lastmodified DESC'), $blogpage, $bloglimit, get_baseurl($filtertype, $filterselect), 'blogpage'); + + blog_rss_print_link($filtertype, $filterselect, $tag); + print '
'; + + if (blog_isLoggedIn()) { + //the user's blog is enabled and they are viewing their own blog + $addlink = '
'; + $addlink .= ''. get_string('addnewentry', 'blog').''; + $addlink .='
'; + echo $addlink; + } + + if (isset($blogEntries) ) { + + $count = 0; + foreach ($blogEntries as $blogEntry) { + blog_print_entry($blogEntry, 'list', $filtertype, $filterselect); //print this entry. + $count++; + } + if (!$count) { + print '
'. get_string('noentriesyet', 'blog') .'

'; + + } + + print $morelink.'
'."\n"; + return; } - $userid = $USER->id; + + $output = '
'. get_string('noentriesyet', 'blog') .'

'; + print $output; } - $thisbloginfo = new BlogInfo($userid); -/* if (isset($USER) && $USER->id == $userid) { - $bloginfosingleton = $thisbloginfo; - }*/ - return $thisbloginfo; -} + /** + * This function is in lib and not in BlogInfo because entries being searched + * might be found in any number of blogs rather than just one. + * + * This function builds an array which can be used by the included + * template file, making predefined and nicely formatted variables available + * to the template. Template creators will not need to become intimate + * with the internal objects and vars of moodle blog nor will they need to worry + * about properly formatting their data + * + * @param BlogEntry blogEntry - a hopefully fully populated BlogEntry object + * @param string viewtype Default is 'full'. If 'full' then display this blog entry + * in its complete form (eg. archive page). If anything other than 'full' + * display the entry in its abbreviated format (eg. index page) + */ + function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filterselect='', $mode='loud') { + global $CFG, $THEME, $USER; -/** - * This function is in lib and not in BlogInfo because entries being searched - * might be found in any number of blogs rather than just one. - * - * $@param BlogFilter blogFilter - a BlogFilter object containing the settings for finding appropriate entries for display - */ -function blog_print_html_formatted_entries(&$blogFilter, $filtertype, $filterselect) { - global $CFG, $USER; + $template['body'] = get_formatted_entry_body($blogEntry->summary, $blogEntry->format); + $template['countofextendedbody'] = 0; - $blogpage = optional_param('blogpage', 0, PARAM_INT); - $bloglimit = get_user_preferences('blogpagesize',8); // expose as user pref when MyMoodle comes around + $template['title'] = ''; + //enclose the title in nolink tags so that moodle formatting doesn't autolink the text + $template['title'] .= ''. stripslashes_safe($blogEntry->subject); + $template['title'] .= ''; - // First let's see if the batchpublish form has submitted data - $post = data_submitted(); + // add editing controls if allowed + $template['userid'] = $blogEntry->userid; + $template['author'] = fullname(get_record('user','id',$blogEntry->userid)); + $template['lastmod'] = userdate($blogEntry->lastmodified); + $template['created'] = userdate($blogEntry->created); + $template['publishtomenu'] = get_publish_to_menu($blogEntry, true, true); + //forum style printing of blogs + blog_print_entry_content ($template, $blogEntry->id, $filtertype, $filterselect, $mode); - $morelink = '
  '; - // show personal or general heading block as applicable - echo '
'; - //show blog title - blog tagline - print "
"; //don't print title. blog_get_title_text(); - - if ($blogpage != 0) { - // modify the blog filter to fetch the entries we care about right now - $oldstart = $blogFilter->fetchstart; - $blogFilter->fetchstart = $blogpage * $bloglimit; - unset($blogFilter->filtered_entries); - } - $blogEntries = $blogFilter->get_filtered_entries(); - // show page next/previous links if applicable - print_paging_bar($blogFilter->get_viewable_entry_count(), $blogpage, $bloglimit, $blogFilter->baseurl, 'blogpage'); - - blog_rss_print_link($filtertype, $filterselect, $blogFilter->tag); - print '
'; - - if (blog_isLoggedIn()) { - //the user's blog is enabled and they are viewing their own blog - $addlink = '
'; - $addlink .= $blogFilter->get_complete_link($CFG->wwwroot .'/blog/edit.php', get_string('addnewentry', 'blog')); - $addlink .='
'; - echo $addlink; } - if (isset($blogEntries) ) { - - $count = 0; - foreach ($blogEntries as $blogEntry) { - blog_print_entry($blogEntry, 'list', $filtertype, $filterselect); //print this entry. - $count++; - } - if (!$count) { - print '
'. get_string('noentriesyet', 'blog') .'

'; - - } - - print $morelink.'
'."\n"; - - if ($blogpage != 0) { - //put the blogFilter back the way we found it - $blogFilter->fetchstart = $oldstart; - unset($blogFilter->filtered_entries); - $blogFilter->fetch_entries(); - } - - return; - } - - $output = '
'. get_string('noentriesyet', 'blog') .'

'; - - print $output; - unset($blogFilter->filtered_entries); -} - -/** - * This function is in lib and not in BlogInfo because entries being searched - * might be found in any number of blogs rather than just one. - * - * This function builds an array which can be used by the included - * template file, making predefined and nicely formatted variables available - * to the template. Template creators will not need to become intimate - * with the internal objects and vars of moodle blog nor will they need to worry - * about properly formatting their data - * - * @param BlogEntry blogEntry - a hopefully fully populated BlogEntry object - * @param string viewtype Default is 'full'. If 'full' then display this blog entry - * in its complete form (eg. archive page). If anything other than 'full' - * display the entry in its abbreviated format (eg. index page) - */ -function blog_print_entry(&$blogEntry, $viewtype='full', $filtertype='', $filterselect='', $mode='loud') { - global $CFG, $THEME, $USER; - static $bloginfoarray; - - if (isset($bloginfoarray) && $bloginfocache[$blogEntry->entryuserid]) { - $bloginfo = $bloginfocache[$blogEntry->entryuserid]; - } else { - $bloginfocache[$blogEntry->entryuserid] = new BlogInfo($blogEntry->entryuserid); - $bloginfo = $bloginfocache[$blogEntry->entryuserid]; - } - - $template['body'] = $blogEntry->get_formatted_entry_body(); - $template['countofextendedbody'] = 0; - - $template['title'] = ''; - //enclose the title in nolink tags so that moodle formatting doesn't autolink the text - $template['title'] .= ''. stripslashes_safe($blogEntry->entryTitle); - $template['title'] .= ''; - - // add editing controls if allowed - $template['userid'] = $blogEntry->entryuserid; - $template['author'] = $blogEntry->entryAuthorName; - $template['lastmod'] = $blogEntry->formattedEntryLastModified; - $template['created'] = $blogEntry->formattedEntryCreated; - $template['publishtomenu'] = $blogEntry->get_publish_to_menu(true, true); //forum style printing of blogs - blog_print_entry_content ($template, $blogEntry->entryId, $filtertype, $filterselect, $mode); + function blog_print_entry_content ($template, $entryid, $filtertype='', $filterselect='', $mode='loud') { + global $USER, $CFG, $course, $ME; -} + $stredit = get_string('edit'); + $strdelete = get_string('delete'); -//forum style printing of blogs -function blog_print_entry_content ($template, $entryid, $filtertype='', $filterselect='', $mode='loud') { - global $USER, $CFG, $course, $ME; + $user = get_record('user','id',$template['userid']); - $stredit = get_string('edit'); - $strdelete = get_string('delete'); + echo '
'; - $user = get_record('user','id',$template['userid']); + echo ''; - echo '
'; + print_user_picture($template['userid'], SITEID, $user->picture); + echo '
'; + echo ''; - echo ''; + echo ''; + /// Actual content - echo '
'.$template['title'].'
'; + $fullname = fullname($user, isteacher($template['userid'])); + $by->name = ''.$fullname.''; + $by->date = $template['lastmod']; + print_string('bynameondate', 'forum', $by); + echo '
'; - print_user_picture($template['userid'], SITEID, $user->picture); - echo '
'; - echo '
'.$template['title'].'
'; - $fullname = fullname($user, isteacher($template['userid'])); - $by->name = ''.$fullname.''; - $by->date = $template['lastmod']; - print_string('bynameondate', 'forum', $by); - echo '
'; + echo ''."\n"; -/// Actual content + // Print whole message + echo format_text($template['body']); - echo ''."\n"; + /// Links to tags - // Print whole message - echo format_text($template['body']); - -/// Links to tags - - if ($blogtags = get_records_sql('SELECT t.* FROM '.$CFG->prefix.'tags t, '.$CFG->prefix.'blog_tag_instance ti - WHERE t.id = ti.tagid - AND ti.entryid = '.$entryid)) { - echo '

'; - print_string('tags'); - echo ': '; - foreach ($blogtags as $key => $blogtag) { - $taglist[] = ''.$blogtag->text.''; - } - echo implode(', ', $taglist); - } - -/// Commands - - echo '

'; - - if (isset($USER->id)) { - if (($template['userid'] == $USER->id) or isteacher($course->id)) { - echo ''.$stredit.''; - } - - if (($template['userid'] == $USER->id) or isteacher($course->id)) { - echo '| '.$strdelete.''; - } - } - - echo '
'; - - echo '
'."\n\n"; -} - -/** - * Use this function to retrieve a list of publish states available for - * the currently logged in user. - * - * @return array This function returns an array ideal for sending to moodles' - * choose_from_menu function. - */ -function blog_applicable_publish_states($courseid='') { - global $CFG; - - // everyone gets draft access - $options = array ( 'draft' => get_string('publishtonoone', 'blog') ); - $options['site'] = get_string('publishtosite', 'blog'); - $options['public'] = get_string('publishtoworld', 'blog'); - - return $options; -} - -/// Checks to see if a user can view the blogs of another user. -/// He can do so, if he is admin, in any same non-spg course, -/// or spg group, but same group member -function blog_user_can_view_user_post($targetuserid) { - - global $CFG; - - $canview = 0; //bad start - - if (isadmin()) { - return true; - } - - $usercourses = get_my_courses($targetuserid); - foreach ($usercourses as $usercourse) { - /// if viewer and user sharing same non-spg course, then grant permission - if (groupmode($usercourse)!= SEPARATEGROUPS){ - if (isstudent($usercourse->id) || isteacher($usercourse->id)) { - $canview = 1; - return $canview; + if ($blogtags = get_records_sql('SELECT t.* FROM '.$CFG->prefix.'tags t, '.$CFG->prefix.'blog_tag_instance ti + WHERE t.id = ti.tagid + AND ti.entryid = '.$entryid)) { + echo '

'; + print_string('tags'); + echo ': '; + foreach ($blogtags as $key => $blogtag) { + $taglist[] = ''.$blogtag->text.''; } - } else { - /// now we need every group the user is in, and check to see if view is a member - if ($usergroups = user_group($usercourse->id, $targetuserid)) { - foreach ($usergroups as $usergroup) { - if (ismember($usergroup->id)) { - $canview = 1; - return $canview; + echo implode(', ', $taglist); + } + + /// Commands + + echo '

'; + + if (isset($USER->id)) { + if (($template['userid'] == $USER->id) or isteacher($course->id)) { + echo ''.$stredit.''; + } + + if (($template['userid'] == $USER->id) or isteacher($course->id)) { + echo '| '.$strdelete.''; + } + } + + echo '
'; + + echo '
'."\n\n"; + } + + /** + * Use this function to retrieve a list of publish states available for + * the currently logged in user. + * + * @return array This function returns an array ideal for sending to moodles' + * choose_from_menu function. + */ + function blog_applicable_publish_states($courseid='') { + global $CFG; + + // everyone gets draft access + $options = array ( 'draft' => get_string('publishtonoone', 'blog') ); + $options['site'] = get_string('publishtosite', 'blog'); + $options['public'] = get_string('publishtoworld', 'blog'); + + return $options; + } + + /// Checks to see if a user can view the blogs of another user. + /// He can do so, if he is admin, in any same non-spg course, + /// or spg group, but same group member + function blog_user_can_view_user_post($targetuserid) { + + global $CFG; + + $canview = 0; //bad start + + if (isadmin()) { + return true; + } + + $usercourses = get_my_courses($targetuserid); + foreach ($usercourses as $usercourse) { + /// if viewer and user sharing same non-spg course, then grant permission + if (groupmode($usercourse)!= SEPARATEGROUPS){ + if (isstudent($usercourse->id) || isteacher($usercourse->id)) { + $canview = 1; + return $canview; + } + } else { + /// now we need every group the user is in, and check to see if view is a member + if ($usergroups = user_group($usercourse->id, $targetuserid)) { + foreach ($usergroups as $usergroup) { + if (ismember($usergroup->id)) { + $canview = 1; + return $canview; + } } } } } + + if (!$canview && $CFG->bloglevel < BLOG_SITE_LEVEL) { + error ('you can not view this user\'s blogs'); + } + + return $canview; } - if (!$canview && $CFG->bloglevel < BLOG_SITE_LEVEL) { - error ('you can not view this user\'s blogs'); + + /// moved from BlogEntry class + function get_formatted_entry_body($body, $format) { + global $CFG; + include_once($CFG->libdir .'/weblib.php'); + if ($format) { + return format_text($body, $format); + } + return stripslashes_safe($body); } - return $canview; -} + + /// moved from BlogEntry class + function get_publish_to_menu($blogEntry, $return=true, $includehelp=true) { + $menu = ''; + if (user_can_change_publish_state($blogEntry) && blog_isediting() ) { + $menu .= '
'. get_string('publishto', 'blog').': '; + $options = blog_applicable_publish_states(); + $menu .= choose_from_menu($options, $this->entryuserid .'-'. $this->entryId, $this->entryPublishState, '', '', '0', true); + $menu .= "\n".'
'."\n"; + /// batch publish might not be needed + if ($includehelp) { + $menu .= helpbutton('batch_publish', get_string('batchpublish', 'blog'), 'blog', true, false, '', true); + } + } + + if ($return) { + return $menu; + } + print $menu; + } + + + /** + * This function will determine if the user is logged in and + * able to make changes to the publish state of this entry + * + * @return bool True if user is allowed to change publish state + */ + function user_can_change_publish_state($blogEntry) { + // figure out who the currently logged in user is. + // to change any publish state one must be logged in + global $USER; + if ( !isset($USER) || empty($USER) || !isset($USER->id) ) { + // only site members are allowed to edit entries + return 'Only site members are allowed to edit entries'; + } else { + $uid = $USER->id; + } + if ( ($uid == $blogEntry->userid) || (blog_is_blog_admin($blogEntry->userid)) || (isadmin())) { + return true; + } + return false; + } + +/// Filter Class functions + + function fetch_entries($userid, $postid='', $fetchlimit=10, $fetchstart='', $filtertype='', $filterselect='', $tagid='', $tag ='', $sort='lastmodified DESC', $limit=true) { + + global $CFG, $USER; + + if (!isset($USER->id)) { + $USER->id = 0; //hack, for guests + } + + /// set the tag id for searching + if ($tagid) { + $tag = $tagid; + } else if ($tag) { + if ($tagrec = get_record_sql('SELECT * FROM '.$CFG->prefix.'tags WHERE text LIKE "'.$tag.'"')) { + $tag = $tagrec->id; + } else { + $tag = -1; //no records found + } + } + + // If we have specified an ID + // Just return 1 entry + if ($postid) { + + if ($post = get_record('post', 'id', $postid)) { + + if ($user = get_record('user', 'id', $post->userid)) { + $post->email = $user->email; + $post->firstname = $user->firstname; + $post->lastname = $user->lastname; + } + + $this->filtered_entries = $post; + return $this->filtered_entries; + } + } + + if ($tag) { + $tagtablesql = $CFG->prefix.'blog_tag_instance bt, '; + $tagquerysql = ' AND bt.entryid = p.id AND bt.tagid = '.$tag.' '; + } else { + $tagtablesql = ''; + $tagquerysql = ''; + } + + + /**************************************** + * depending on the type, there are 4 * + * different possible sqls * + ****************************************/ + + $requiredfields = 'p.*, u.firstname,u.lastname,u.email'; + + switch ($filtertype) { + + case 'site': + + if (!isguest() && isloggedin()) { + + $SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql + .$CFG->prefix.'user u + WHERE p.userid = u.id '.$tagquerysql.' + AND (p.publishstate = \'site\' OR p.publishstate = \'public\' OR p.userid = '.$USER->id.') + AND u.deleted = 0'; + + } else { + + $SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql + .$CFG->prefix.'user u + WHERE p.userid = u.id '.$tagquerysql.' + AND p.publishstate = \'public\' + AND u.deleted = 0'; + } + + break; + + case 'course': + if ($filterselect != SITEID) { + $SQL = '(SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql + .$CFG->prefix.'user_students s, '.$CFG->prefix.'user u + WHERE p.userid = s.userid '.$tagquerysql.' + AND s.course = '.$filterselect.' + AND u.id = p.userid + AND (p.publishstate = \'site\' OR p.publishstate = \'public\' OR p.userid = '.$USER->id.')) + + UNION + + (SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql + .$CFG->prefix.'user_teachers t, '.$CFG->prefix.'user u + WHERE p.userid = t.userid '.$tagquerysql.' + AND t.course = '.$filterselect.' + AND u.id = p.userid + AND (p.publishstate = \'site\' OR p.publishstate = \'public\' OR p.userid = '.$USER->id.'))'; //this will break for postgres, i think + } else { + + if (isloggedin()) { + + $SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql + .$CFG->prefix.'user u + WHERE p.userid = u.id '.$tagquerysql.' + AND (p.publishstate = \'site\' OR p.publishstate = \'public\' OR p.userid = '.$USER->id.') + AND u.deleted = 0'; + + } else { + + $SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql + .$CFG->prefix.'user u + WHERE p.userid = u.id '.$tagquerysql.' + AND p.publishstate = \'public\' + AND u.deleted = 0'; + } + + } + + break; + + case 'group': + + $SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql + .$CFG->prefix.'groups_members m, '.$CFG->prefix.'user u + WHERE p.userid = m.userid '.$tagquerysql.' + AND u.id = p.userid + AND m.groupid = '.$filterselect.' + AND (p.publishstate = \'site\' OR p.publishstate = \'public\' OR p.userid = '.$USER->id.')'; + + break; + + case 'user': + + $SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql + .$CFG->prefix.'user u + WHERE p.userid = u.id '.$tagquerysql.' + AND u.id = '.$filterselect.' + AND (p.publishstate = \'site\' OR p.publishstate = \'public\' OR p.userid = '.$USER->id.')'; + + break; + + + } + + if ($fetchstart !== '' && $limit) { + $limit = sql_paging_limit($fetchstart, $fetchlimit); + } else { + $limit = ''; + } + + $orderby = ' ORDER BY '. $sort .' '; + + //echo 'Debug: BlogFilter fetch_entries() sql="'. $SQL . $orderby . $limit .'"
'. $this->categoryid; //debug + + $records = get_records_sql($SQL . $orderby . $limit); + +// print_object($records); //debug + + if (empty($records)) { + return array(); + } + + return $records; + } + + /** + * get the count of viewable entries, easiest way is to count fetch_entries + * this is used for print_paging_bar + */ + function get_viewable_entry_count($userid, $postid='', $fetchlimit=10, $fetchstart='', $filtertype='', $filterselect='', $tagid='', $tag ='', $sort='lastmodified DESC') { + + $blogEntries = fetch_entries($userid, $postid, $fetchlimit, $fetchstart,$filtertype, $filterselect, $tagid, $tag, $sort='lastmodified DESC', false); + return count($blogEntries); + } + + /// Find the base url from $_GET variables + function get_baseurl($filtertype, $filterselect) { + + $getcopy = $_GET; + + unset($getcopy['blogpage']); + + $strippedurl = strip_querystring(qualified_me()); + if(!empty($getcopy)) { + $first = false; + $querystring = ''; + foreach($getcopy as $var => $val) { + if(!$first) { + $first = true; + if ($var != 'filterselect' && $var != 'filtertype') { + $querystring .= '?'.$var.'='.$val; + $hasparam = true; + } else { + $querystring .= '?'; + } + } else { + if ($var != 'filterselect' && $var != 'filtertype') { + $querystring .= '&'.$var.'='.$val; + $hasparam = true; + } + } + } + if (isset($hasparam)) { + $querystring .= '&'; + } else { + $querystring = '?'; + } + } else { + $querystring = '?'; + } + + return strip_querystring(qualified_me()) . $querystring. 'filtertype='.$filtertype.'&filterselect='.$filterselect.'&'; + + } ?> diff --git a/blog/preferences.php b/blog/preferences.php index 3d1dcfc56ad..bf83ff72026 100755 --- a/blog/preferences.php +++ b/blog/preferences.php @@ -26,7 +26,6 @@ error(get_string('noguestpost', 'blog'), $referrer); } $userid = $USER->id; - $bloginfo =& new BlogInfo($userid); /// If data submitted, then process and store. diff --git a/blog/tags.php b/blog/tags.php index 4c7ce12eb4c..7d559393942 100755 --- a/blog/tags.php +++ b/blog/tags.php @@ -1,11 +1,15 @@ text = $otag; $tag->type = 'official'; $tagid = insert_record('tags', $tag); + + /// write newly added tags back into window opener echo ''; - } else { + } else { // tag already exist notify(get_string('tagalready')); } break; - case 'addpersonal': /// everyone can add + case 'addpersonal': /// everyone can add personal tags if (!confirm_sesskey() || isguest() || !isset($USER->id)) { error ('you can not add tags'); } @@ -38,19 +44,21 @@ switch ($mode) { $tag->text = $ptag; $tag->type = 'personal'; $tagid = insert_record('tags', $tag); + + /// write newly added tags back into window opener echo ''; - } else { + } else { //tag already exist notify(get_string('tagalready')); } - //write back to window.opener + break; - case 'delete': + case 'delete': /// delete a tag if (!confirm_sesskey()) { error('you can not delete tags'); } @@ -61,6 +69,7 @@ switch ($mode) { $blogtag = get_record('tags','id',$tag); + // you can only delete your own tags, or you have to be an admin if (!isadmin() and $USER->id != $blogtag->userid) { notify('no right to delete'); continue; @@ -96,10 +105,9 @@ switch ($mode) { '; } } - //write back to window.opener break; - default: + default: // just displaying tags form break; }