From 752d035b41738026a369f2ae382e060ca54432e5 Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Wed, 12 Apr 2006 02:06:57 +0000 Subject: [PATCH] removing some classes --- blog/class.BlogEntry.php | 368 --------------------------------- blog/class.BlogFilter.php | 419 -------------------------------------- blog/class.BlogInfo.php | 301 --------------------------- 3 files changed, 1088 deletions(-) delete mode 100755 blog/class.BlogEntry.php delete mode 100755 blog/class.BlogFilter.php delete mode 100755 blog/class.BlogInfo.php diff --git a/blog/class.BlogEntry.php b/blog/class.BlogEntry.php deleted file mode 100755 index 0df5000dd0..0000000000 --- a/blog/class.BlogEntry.php +++ /dev/null @@ -1,368 +0,0 @@ -dirroot .'/blog/lib.php'); - -/** - * - * This class represents a single entry in a blog. Normally, you wouldn't - * need to call the constructor directly...you would instead make use of - * a BlogInfo object's entry retrieval methods in order to get an instance - * of BlogEntry. - * - * To create a new blogEntry object use BlogInfo's insert_blog_entry() function. - * - * @todo Ultimately this class might to be expanded to include a factory - * method for the creation of new BlogEntries (->create()). - * Better yet: - fix constructor to not have to take a record set - should be empty and use setters - verify that everything has getters/setters - make sure that both the ->save and ->update function operate properly on an entry that does not yet exist! - this way one could: - $newEntry = new BlogEntry(); - $newEntry->set_body('a post here'); - $newEntry->set_userid(2); - $newEntry->save(); - */ -class BlogEntry { - // member variables - var $entryId; // post.id - var $entryBody; // post.summary - var $entryTitle; // post.subject - var $entryFormat; // post.format - var $entryuserid; // post.author - var $entryPublishState; // post.publishstate - var $entryAuthorName; // blog_users.name - var $entryAuthorEmail; // blog_users.email - - //Daryl Hawes note: entryCategoryIds should be phased out as entryCategories is an - //associative array of $id => $name elements - var $entryCategoryIds = array(); // post.id -> blog_categories_entries.categoryid - var $entryCategories = array(); // post.id -> blog_categories_entries.categoryid -> blog_categories.catname - - var $entryLastModified; // last modification date post.lastmodified - var $formattedEntryLastModified; // post.lastmodified - var $entryCreated; // creation date post.created - var $formattedEntryCreated; // post.created - - /** - * Class constructor that will build a new instance of the object - * when given a reference to an object that contains - * all of the keys from a row in the post table - * Daryl Hawes note: constructor should be changed not to have to take in a database row! - * - * @param object $entrydetails reference to an object that contains - * all of the keys from a row in the post table - * @uses $CFG - * @uses $db - * @todo finish documenting this constructor - */ - function BlogEntry(&$entrydetails) { - global $db, $CFG; - - $this->entryId = $entrydetails->id; - - $this->entryBody = ereg_replace('', "'", stripslashes_safe($entrydetails->summary)); - - $strftimedaydatetime = get_string('strftimedaydatetime'); - $this->entryLastModified = $entrydetails->lastmodified; - $this->formattedEntryLastModified = userdate($this->entryLastModified, $strftimedaydatetime); - $this->entryCreated = $entrydetails->created; - $this->formattedEntryCreated = userdate($this->entryCreated, $strftimedaydatetime); - - $this->entryuserid = $entrydetails->userid; - - //added stripslashes_safe here for rss feeds. Will this conflict anywhere? - - $this->entryTitle = ereg_replace('', "'", stripslashes_safe($entrydetails->subject)); //subject, not title! - - $this->entryFormat = $entrydetails->format; - - if (isset($entrydetails->publishstate) ) { - $this->entryPublishState = $entrydetails->publishstate; - } else { - $this->entryPublishState = 'draft'; - } - if (isset($entrydetails->email)) { - $this->entryAuthorEmail = $entrydetails->email; - $this->entryAuthorName = fullname($entrydetails); // firstname and lastname defined - } else { - $user = get_record('user', 'id', $entrydetails->userid); - $this->entryAuthorEmail = $user->email; - $this->entryAuthorName = fullname($user); - } - } - - /** - * delete this entry - * - * @return bool Returns true on successful deletion - */ - function delete() { - if (! delete_records('post', 'userid', $this->entryuserid, 'id', $this->entryId)) { - print 'Could not find blog entry matching author with user id '. $this->entryuserid .' and entry with id '. $this->entryId ."\n"; - return false; - } - return true; - } - - /** - * get_formatted_entry_body - * getter for ->entryBody. - * - * @uses $CFG - * @return string Entry body/summary run through moodle's format_text formatter and - * with slashes stripped from database entry - */ - function get_formatted_entry_body() { - global $CFG; - include_once($CFG->libdir .'/weblib.php'); - if ( isset($this->entryFormat) ) { - return format_text($this->entryBody, $this->entryFormat); - } - return stripslashes_safe($this->entryBody); - } - - /** - * get_unformatted_entry_body - * getter for ->entryBody - * - * @return string Entry body/summary - raw string from database - */ - function get_unformatted_entry_body() { - return $this->entryBody; - } - - /** - * BlogEntry setters do not save to the database. - * To save changes call the BlogEntry->save() function when ready. - * - * @param string $body New entry summary - */ - function set_body($body) { - $this->entryBody = $body; - } - - /** - * BlogEntry setters do not save to the database. - * To save changes call the BlogEntry->save() function when ready. - * - * @param string $format Moodle format_text format type. - */ - function set_format($format) { - $this->entryFormat = $format; - } - - /** - * BlogEntry setters do not save to the database. - * To save changes call the BlogEntry->save() function when ready. - * - * @param int $userid The new author/owner's moodle user id - */ - function set_userid($userid) { - $this->entryuserid = $userid; - } - - /** - * BlogEntry setters do not save to the database. - * To save changes call the BlogEntry->save() function when ready. - * - * @param string $publishstate A new publish state for this entry. One of: - * enum('draft','teacher','course','group','site','public') - * @return bool True if new state is allowed (and applied), false if not. - */ - function set_publishstate($publishstate) { - $applicablestates = array_keys(blog_applicable_publish_states($this->entryCourseId)); - if (in_array($publishstate, $applicablestates)) { - $this->entryPublishState = $publishstate; - return true; - } - return false; - } - - - /** - * 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() { - // 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 == $this->entryuserid) || (blog_is_blog_admin($this->entryuserid)) || (isadmin()) - || (isset($this->entryCourseId) && isteacher($this->entryCourseId)) ) { - return true; - } - return false; - } - - /** - * added by Daryl Hawes for moodle integration - * - * @param int $uid The user attempting to view this entry - * @return bool - */ - function user_can_view($uid='') { - global $USER; - - //first allow access to any post for admin users - if ( isadmin() ) { - return true; - } - - //get the logged in user's id if needed - if ($uid == '') { - if ( isset($USER) && isset($USER->id)) { - $uid = $USER->id; - } - } - - if ($this->entryPublishState == 'public') { - return true; - } else if ($this->entryPublishState == 'draft') { - //only the owner is allowed to see their own draft message - if ($uid == $this->entryuserid) { - return true; - } - } else if ($this->entryPublishState == 'site') { - //user has a valid member id and user is not a guest of the site - if ( ! $uid == '' && ! isguest() ) { - return true; - } - } - - //nothing qualified - the user requesting access is not allowed to view this entry! - return false; - } - - - /** - * @param bool $return If true a string value is returned. If this variable is set to false - * Then this function will print out the menu code and exit. - * @param bool $includehelp If true a help button linking to the batch_publish page - * will be included in the returned string - * @return string|nil If the $return param is set to true a string is returned. - */ - function get_publish_to_menu($return=true, $includehelp=true) { - $menu = ''; - if ($this->user_can_change_publish_state() && 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"; - if ($includehelp) { - $menu .= helpbutton('batch_publish', get_string('batchpublish', 'blog'), 'blog', true, false, '', true); - } - } - if ($return) { - return $menu; - } - print $menu; - } - - /** - * Save this entry to the database. - * This function can be used outside the BlogEntry class. - * ex: - * - * $myBlogEntry = blogInfo->get_blog_entry_by_id(100); - * $myBlogEntry->set_title('New Title'); - * $myBlogEntry->save(); - * - * This function will handle all of the security and data integrity checking for you - * @return null|string Error string returned when an error is encountered. - */ - function save() { - //check if the user is authorized to make this change - // either they own this entry, are in the blog acl for this entry, - // are an admin user or they teach the course this entry is associated with - if ($this->user_can_change_publish_state()) { - $applicablestates = array_keys(blog_applicable_publish_states($this->entryCourseId)); - if (in_array($this->entryPublishState, $applicablestates)) { - // Yes they are authorized so update the entry. - if ( $this->_update() ) { - //print_object($this); //debug: - - //add a timestamp to the user preference for this userid to mark it updated - set_user_preference('bloglastmodified', time(), $this->entryuserid); - return; - } else { - $error = 'An error occured saving this entry'; - } - } else { - $error = 'Publish state '. $this->entryPublishState .' is not available for this user'; - } - } else { - $error = 'User not allowed to edit this entry'; - } - // must not have worked... - return $error; - } - - /** - * _update - * - * This function is internal to the BlogEntry class and should not be used elsewhere. - * It takes the currently set member variables and writes them to the database. - * @return boolean - */ - function _update() { - - global $db, $CFG; - // generate the modification date - $timenow = time(); - - //load up the data object with the latest data - $dataobject->id = intval($this->entryId); - $dataobject->summary = $this->entryBody; - $dataobject->content = $this->entryExtendedBody; - $dataobject->subject = $this->entryTitle; - $dataobject->format = intval($this->entryFormat); - $dataobject->userid = intval($this->entryuserid); - $dataobject->publishstate = $this->entryPublishState; - - $dataobject->lastmodified = $timenow; - - $dataobject->summary = ereg_replace("'", '', $dataobject->summary); - // The wysiwyg html editor adds a
tag to the extendedbody. - // cleanup the extendedbody first - if ($dataobject->content == '
') { - $dataobject->content = ''; - } - $dataobject->content= ereg_replace("'", '', $dataobject->content); - $dataobject->subject = ereg_replace("'", '', $dataobject->subject); - $dataobject->subject = addslashes($dataobject->subject); - $dataobject->summary = addslashes($dataobject->summary); - $dataobject->content = addslashes($dataobject->content); - - // First update the entry's categories. Remove all, then add back those passed in - $sql = 'DELETE FROM '. $CFG->prefix .'blog_categories_entries WHERE entryid='. $this->entryId; - $rs = $db->Execute($sql); - - // next update the entry itself - if (update_record('post', $dataobject)) { - return true; - } - //failure - return false; - } - -}//end class BlogEntry -?> diff --git a/blog/class.BlogFilter.php b/blog/class.BlogFilter.php deleted file mode 100755 index 81885f44ee..0000000000 --- a/blog/class.BlogFilter.php +++ /dev/null @@ -1,419 +0,0 @@ -dirroot.'/blog/lib.php'); - -/******************************************************************* - * This class represents a set of active filters to be applied - * in searching for or presenting blog entries. - * Retrieve filtered entries by calling get_filtered_entries - * rather than directly accessing the array as - * the function will fetch the entries for you if needed. - ******************************************************************/ -class BlogFilter { - // member variables - // you can use variable names directly to access properties. - //ie. $blogFilter->month - - var $fetchlimit; //max # of entries to read from database - var $fetchstart; //entry # to start reading from database at - var $max_entries; //maximum number of matching entries available in database - var $sort; - var $courseid; - var $userid; // moodle userid to specify a specific user's blog - var $postid; //id of a single blog entry - var $blogInfo; - var $memberlist; //do not access directly - use getter get_member_list() - var $filtered_entries = array(); - var $baseurl; - var $filtertype; - var $filterselect; - var $tag; - var $keywords = NULL; //array of $keywordtype = $keywordstring - - /** - * BlogFilter - * class constructor that will build a new instance of a BlogFilter object - * - * @param int $userid = the blog that the entries are to be found in. If 0 then all blogs are searched. - * @param int $courseid = if needed the entries can be restricted to those associated with a given course. - * @param int $postid = a specific blog entry that is being sought - */ - function BlogFilter($userid='', $postid='', $fetchlimit=10, $fetchstart='', $filtertype='', $filterselect='', $tagid='', $tag ='', $sort='lastmodified DESC') { - - global $CFG; //filter settings to be pass in for baseurl - - if (!empty($userid)) { -// print "creating blogInfo object for user with id '$userid'
"; //debug - $this->blogInfo =& new BlogInfo($userid); - } - if ( empty($this->blogInfo) || empty($this->blogInfo->userid)) { - unset($this->blogInfo); - } - - if (! is_numeric($userid) ) { - $this->userid = 0; - } else { - $this->userid = $userid; - } - if (!is_numeric($fetchstart) ) { - $this->fetchstart = 0; - } else { - $this->fetchstart = $fetchstart; - } - - $this->fetchlimit = $fetchlimit; - - $this->postid = $postid; - - $this->sort = $sort; - $this->filtertype = $filtertype; - $this->filterselect = $filterselect; - if ($tagid) { - $this->tag = $tagid; - } else if ($tag) { - if ($tagrec = get_record_sql('SELECT * FROM '.$CFG->prefix.'tags WHERE text LIKE "'.$tag.'"')) { - $this->tag = $tagrec->id; - } else { - $this->tag = -1; //no record found - } - } - // borrowed from Jon's table class - if(empty($this->baseurl)) { - - $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 = '?'; - } - - $this->baseurl = strip_querystring(qualified_me()) . $querystring. 'filtertype='.$filtertype.'&filterselect='.$filterselect.'&'; - - } - } - - /** - * borrowed from Jon's table class. - */ - function define_baseurl($url) { - if(!strpos($url, '?')) { - $this->baseurl = $url.'?'; - } - else { - $this->baseurl = $url.'&'; - } - } - - /** - * - */ - function set_filtered_entries(&$blogentries) { - $this->filtered_entries = $blogentries; - } - - /** - * @return array Blog entries based on current filters - */ - function get_filtered_entries() { - - if ( empty($this->filtered_entries) ) { - //no entries defined. try to fetch them. - $this->fetch_entries(); - } - - if (!empty($this->filtered_entries)) { - //we have entries - return them - return $this->filtered_entries; - } - //still no entries - they must all be filtered away or there simply are none. return null. - return NULL; - } - - /** - * Using the member variables build a where clause and sql statment - * and fetch the correct blog entries from the database. The entries - * are then stored in the filtered_entries member variable. - * - * @uses $CFG - * @uses $USER - * @limit, if limit is false, then return all records - */ - function fetch_entries($limit=true) { - global $CFG, $USER; - - - if (!isset($USER->id)) { - $USER->id = 0; //hack, for guests - } - - // if we have specified an ID - if ($this->postid) { - - if ($post = get_record('post', 'id', $this->postid)) { - - if ($user = get_record('user', 'id', $post->userid)) { - $post->email = $user->email; - $post->firstname = $user->firstname; - $post->lastname = $user->lastname; - } - - $blogEntry = new BlogEntry($post); - $blogEntries[] = $blogEntry; - - $this->filtered_entries = $blogEntries; - return $this->filtered_entries; - } - } - - - if ($this->tag) { - $tagtablesql = $CFG->prefix.'blog_tag_instance bt, '; - $tagquerysql = ' AND bt.entryid = p.id AND bt.tagid = '.$this->tag.' '; - } else { - $tagtablesql = ''; - $tagquerysql = ''; - } - - - /**************************************** - * depending on the type, there are 4 * - * different possible sqls * - ****************************************/ - - $requiredfields = 'p.*, u.firstname,u.lastname,u.email'; - - switch ($this->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 ($this->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 = '.$this->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 = '.$this->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 = '.$this->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 = '.$this->filterselect.' - AND (p.publishstate = \'site\' OR p.publishstate = \'public\' OR p.userid = '.$USER->id.')'; - - break; - - - } - - if ($this->fetchstart !== '' && $limit) { - $limit = sql_paging_limit($this->fetchstart, $this->fetchlimit); - } else { - $limit = ''; - } - - $orderby = ' ORDER BY '. $this->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(); - } else { - $blogEntries = array(); - foreach ($records as $record) { - $blogEntry = new BlogEntry($record); - $blogEntries[] = $blogEntry; - } - } - -// echo 'Debug: blog entries retrieved in fetch_entries function of BlogFilter class:
'; //debug -// print_object($blogEntries); //debug - - $this->filtered_entries = $blogEntries; - - return $this->filtered_entries; - } - - /** - * 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($where='', $hascats=false) { - $blogEntries = $this->fetch_entries(false); - return count($blogEntries); - } - - /** - * get count of entries as they have been fetched from the fully filtered query - */ - function get_filtered_entry_count() { - global $CFG; - //might need to use a count_records_sql. - $entries = $this->get_filtered_entries(); - return count($entries); - } - - /** - * Use this function to retrieve a link to a blog page (typically not the one - * you are currently processing) which contains the correct blog filter information - * to maintain the user's filtered view when progressing from page to page. - * - * The unused param is defined as either - * - * $unused = array('userid', 'courseid', 'groupid'); - * - * or - * - * $unused = 'startyear'; - * - * @param string $baseurl The url to be added to the full href before the getvars - * @param array|string $unused Can be an array of ivar names or a single variable name - * @return string A link to the specified baseurl along with the correct getvars for this filter. - */ - function get_complete_link($baseurl, $linktext, $unused='') { - $getargs = $this->get_getvars($unused); - $link = ''; - $link .= $linktext . ''; - return $link; - } - - /** - * The unused param is defined as either - * - * $unused = array('userid', 'courseid', 'groupid'); - * - * or - * - * $unused = 'startyear'; - * - * @param array|string $unused Can be an array of ivar names or a single variable name - */ - function get_getvars($unused) { - $getargs = '?'; - if(!is_array($unused)) { - $unused = array($unused); - } - if (!is_array($unused)) { - //argument is not an array, hopefully it's a string. wrap it in an array for comparisons below. - $unused = array($unused); - } - if (!in_array('limit', $unused)) { - $getargs .= '&limit=' . $this->fetchlimit; - } - if (!in_array('courseid', $unused)) { - $getargs .= '&courseid=' . $this->courseid; - } - if (!in_array('userid', $unused)) { - $getargs .= '&userid=' . $this->userid; - } - return $getargs; - } - -} //end class BlogFilter -?> diff --git a/blog/class.BlogInfo.php b/blog/class.BlogInfo.php deleted file mode 100755 index 48d62c119a..0000000000 --- a/blog/class.BlogInfo.php +++ /dev/null @@ -1,301 +0,0 @@ -get_blog_title(); - * - * The three most useful methods are those used to retrieve BlogEntries: - * - * $someBlogEntry = $myBlog->get_blog_entry_by_id(200); // fetch the 200th blog entry. - * $blogEntryList = $myBlog->get_last_N_entries(10); // fetch the 10 most recent - * // blog entries. - * foreach ($blogEntryList as $blogEntry) { - * print "Blog Entry Title: ($blogEntry->entryId) $blogEntry->get_blog_title()
"; - * } - */ - -global $CFG; -include_once($CFG->dirroot.'/blog/lib.php'); -include_once($CFG->dirroot.'/blog/class.BlogEntry.php'); - -class BlogInfo { - // member variables - var $userid; // moodle userid - var $blogtitle; // user preference blog_title - var $blogtagline; // user preference blog_tagline - var $blogtheme; // user preference blog_theme - id of the template being used for this blog. - - // lazy loading member variables - // DO NOT directly refer to these member vars. Instead use their - // getter functions to ensure they are loaded properly - var $blogadminuser = NULL; // moodle user object for this userid - var $blogadminname = NULL; // userid -> blog_users.name - var $blogadminemail = NULL; // userid -> blog_users.email - var $blogadminurl = NULL; // userid -> blog_users.url - var $blogEntries = NULL; // an array of entries for this blog. empty by default. - - /** - * constructor- used to create the BlogInfo instance - * and populate it with information about the blog. - */ - function BlogInfo($userid) { - global $CFG; - - if ($userid == 0 || empty($userid)) { - return NULL; - } - - $this->blogEntries = array(); - $this->userid = $userid; - - } - -////////// getters and setters /////////////// - - /** - * Use this function to get a single numbered BlogEntry object - * for this blog. - * @todo perhaps a member array could be used to store these fetched BlogEntry objects - * in case the same entry is requested from this same bloginfo object later - */ - function get_blog_entry_by_id($entryId) { - global $CFG; - - foreach ($this->blogEntries as $cachedentry) { - if ($cachedentry->entryId == $entryId) { - return $cachedentry; - } - } - $record = get_record('post', 'id', $entryId); //teachers should be able to edit people's blogs, right? - //$record = get_record('post', 'author', $this->userid, 'id', $entryId); - // may have zero entries. in that case, return null. - if (empty($record)) { - // the result set is empty. return null. - return NULL; - } else { - // create the new blog entry object... - $blogEntry = new BlogEntry($record); - //cache the blogEntry in member var for future use if needed - $this->blogEntries[] = $blogEntry; - $this->blogEntries = array_unique($this->blogEntries); - } - return $blogEntry; - } - - /** - * This function will remove the specified blog entry. It will - * perform any of the security checks necessary to ensure that the - * user is authorized to remove the entry. It will return false - * if there was an error trying to delete the entry. - * @param int $entryID The blog entry to delete by id - */ - function delete_blog_entry_by_id($entryId) { - // figure out who the currently logged in user is. - global $USER; - if ( !isset($USER) || empty($USER) || !isset($USER->id) ) { - return false; - } - $uid = $USER->id; - // retrieve the entry. - $blogEntry = $this->get_blog_entry_by_id($entryId); - - if (empty($blogEntry) ) { - return false; - } - - if (($uid == $blogEntry->entryuserid) || (blog_is_blog_admin($this->userid)) || (isadmin())) { - // yes, they are authorized, so remove the entry. - if ( $blogEntry->delete() ) { - unset($this->blogEntries[$blogEntry]); - return true; - } - } - // must not have worked... - return false; - } - - - /** - * Use this method to insert/create a new entry in the post table for - * this blog. The entry id of the new blog entry will be returned if the - * insertion is successful. - * @param string $title . - * @param string $body . - * @param string $extendedbody . - * @param int $userid . - * @param int $formatId . - * @param string $publishstate 'draft', 'teacher', 'course', 'group', 'site', 'public' - * @param int $courseid . - * @param int $groupid . - * @return int - */ - function insert_blog_entry($title, $body, $userid, $formatId, $publishstate='draft', $courseid='', $groupid='') { - global $CFG; - - // first, make sure the title and body are safe for insert. - $title = ereg_replace("'", '', $title); - $body = ereg_replace("'", '', $body); - // The wysiwyg html editor adds a
tag to the extendedbody. - // cleanup the extendedbody first - - $title = addslashes($title); - $body = addslashes($body); - - // come up with a new timestamp to insert. - // now insert the new entry. - $dataobject->summary = $body; - $dataobject->userid = $userid; - $dataobject->subject = $title; - $dataobject->format = $formatId; - $dataobject->module = 'blog'; - - $timenow = time(); - $dataobject->lastmodified = $timenow; - $dataobject->created = $timenow; - $dataobject->publishstate = $publishstate; - - $newentryid = insert_record('post', $dataobject); - - if ($newentryid) { - // entry was created and $newentryid is its entryid. - - // create a unique hash for this id that will be its alternate identifier - unset($dataobject); - $dataobject->id = $newentryid; - $dataobject->uniquehash = md5($userid.$CFG->wwwroot.$newentryid); - update_record('post', $dataobject); - - // now create category entries - if (!empty($categoryids)) { - foreach ($categoryids as $categoryid) { - $cat->entryid = $newentryid; - $cat->categoryid = $categoryid; - insert_record('blog_categories_entries', $cat); - } - } - // insert lastmodified into user pref so that recently modified blogs can be identified easily without joining tables - set_user_preference('bloglastmodified', $timenow, $this->userid); - return $newentryid; - } - return null; - } - - /** - * Discovers the number of entries for this blog - * @return int Entry count - */ - function get_entry_count() { - return count_records('post', 'userid', $this->userid); - } - - /** - * returns the N most recent BlogEntry objects - * for this blog - */ - function get_last_N_entries($n) { - return $this->get_blog_entries_by_range($n, 0); - } - - /** - * - */ - function get_blog_entries_by_range($limit, $start) { - global $USER; - - $sqlsnippet = 'userid='. $this->userid; - $sort = 'id DESC'; - $records = get_records_select('post', $sqlsnippet, $sort, '*', $start, $limit); - if (empty($records)) { - return array(); - } else { - $blogEntries = array(); - foreach($records as $record) { - $blogEntry = new BlogEntry($record); - //ensure that the user has rights to view this entry - if ($blogEntry->user_can_view() ) { - $blogEntries[] = $blogEntry; - } - } -// print_object($blogEntries); //debug - //cache the blogEntries in member var for future use if needed - $this->blogEntries = array_merge($this->blogEntries, $blogEntries); - $this->blogEntries = array_unique($this->blogEntries); - - return $blogEntries; - } - } - - /** - * update_blog_entry_by_id - * - * this funciton will update the selected blog entry after performing - * security checks to make sure the user is authorized to perform the update. - * Used by api.php - * @uses USER - */ - function update_blog_entry_by_id($entryId, $title, $body, $formatId, $categoryId, $publishstate='draft', $courseid='', $groupid='') { - // figure out who the currently logged in user is. - global $USER; - - if ( !isset($USER) || empty($USER) || !isset($USER->id) ) { - return false; - } else { - $uid = $USER->id; - } - $body = ereg_replace("'", '', $body); - $extendedbody = ereg_replace("'", '', $extendedbody); - $title = ereg_replace("'", '', $title); - $title = addslashes($title); - $body = addslashes($body); - $extendedbody = addslashes($extendedbody); - - // retrieve the entry - $blogEntry = $this->get_blog_entry_by_id($entryId); - //check if the user is authorized to make this change - if ( ($uid == $blogEntry->entryUserId) || (blog_is_blog_admin($this->userid)) || (isadmin()) ) { - // Yes they are authorized so update the entry. - if ( $blogEntry->update($title, $body, $extendedbody, $formatId, $categoryId, $publishstate, $courseid, $groupid) ) { - return true; - } - } - // must not have worked... - return false; - - } - -} -?> -- 2.39.5