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($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_YES_NO_MODES = array ( '0' => get_string('no'),
'1' => get_string('yes') );
//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;
}
if (!(isset($CFG->blog_enable_trackback_in) )) {
$CFG->blog_enable_trackback_in = 0; //default is 0 == do not allow for site
}
if (!(isset($CFG->blog_enable_moderation) )) {
$CFG->blog_enable_moderation = 0; //default is 0 == do not enable blog moderation on this site
}
if (!(isset($CFG->blog_enable_pingback_in) )) {
$CFG->blog_enable_pingback_in = 0; //default is 0 == do not allow for site
}
if (!(isset($CFG->blog_enable_trackback_out) )) {
$CFG->blog_enable_trackback_out = 0; //default is 0 == do not allow for site
}
if (!(isset($CFG->blog_enable_pingback_out) )) {
$CFG->blog_enable_pingback_out = 0; //default is 0 == do not allow for site
}
if (!(isset($CFG->blog_enable_moderation) )) {
$CFG->blog_enable_moderation = 0; //default is 0 == do not turn on moderation for site
}
if (!(isset($CFG->blog_useweblog_rpc) )) {
$CFG->blog_useweblog_rpc = 0;//default is 0 == do not publish to weblogs.com
}
if (empty($CFG->blog_ratename) ) {
$CFG->blog_ratename = 'Rating'; //default name for entry ratings
}
if (empty($CFG->blog_default_title) ) {
$CFG->blog_default_title = 'Moodle Blog'; //default blog title
}
if (empty($CFG->blog_blogurl) ) {
$CFG->blog_blogurl = $CFG->wwwroot.'/blog/index.php';
}
if (!(isset($CFG->blog_enable_trackback) )) {
$CFG->blog_enable_trackback = 0;
}
if (!(isset($CFG->blog_enable_pingback) )) {
$CFG->blog_enable_pingback = 0;
}
if (empty($CFG->blog_default_fetch_num_entries) ) {
$CFG->blog_default_fetch_num_entries = 8;
}
/**
* 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
if ($userid == '') {
global $USER;
if (!isset($USER) || !isset($USER->id)) {
return;
}
$userid = $USER->id;
}
/* if (isset($USER) && $USER->id == $uid && !empty($bloginfosingleton)) {
return $bloginfosingleton;
}*/
$thisbloginfo = new BlogInfo($userid);
/* if (isset($USER) && $USER->id == $userid) {
$bloginfosingleton = $thisbloginfo;
}*/
return $thisbloginfo;
}
/**
* 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;
}
/**
* This function upgrades the blog's tables as needed.
* It's called from moodle/admin/index.php
*/
function blog_upgrade_blog_db($continueto) {
global $CFG, $db;
require_once($CFG->dirroot.'/blog/version.php'); // Get code versions
if (empty($CFG->blog_version)) { // Blog has never been installed.
$strdatabaseupgrades = get_string('databaseupgrades');
print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades,
'', '', false, ' ', ' ');
$db->debug=true;
if (modify_database($CFG->dirroot .'/blog/db/'. $CFG->dbtype .'.sql')) {
$db->debug = false;
if (set_config('blog_version', $blog_version)) {
notify(get_string('databasesuccess'), 'green');
notify(get_string('databaseupgradeblog', 'blog', $blog_version));
/// 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
require_once($CFG->libdir .'/pagelib.php');
require_once($CFG->dirroot .'/blog/blogpage.php');
page_map_class(PAGE_BLOG_VIEW, 'page_blog');
// Now, create our page object.
$page = page_create_object(PAGE_BLOG_VIEW, 0);
// Add default blocks to the new page type
blocks_repopulate_page($page);
print_continue($continueto);
exit;
} else {
error('Upgrade of blogging system failed! (Could not update version in config table)');
}
} else {
error('blog tables could NOT be set up successfully!');
}
}
if ($blog_version > $CFG->blog_version) { // Upgrade tables
$strdatabaseupgrades = get_string('databaseupgrades');
print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades);
require_once($CFG->dirroot. '/blog/db/'. $CFG->dbtype .'.php');
$db->debug=true;
if (blog_upgrade($CFG->blog_version)) {
$db->debug=false;
if (set_config('blog_version', $blog_version)) {
notify(get_string('databasesuccess'), 'green');
notify(get_string('databaseupgradeblocks', '', $blog_version));
print_continue($continueto);
exit;
} else {
error('Upgrade of blogging system failed! (Could not update version in config table)');
}
} else {
$db->debug=false;
error('Upgrade failed! See blog/version.php');
}
} else if ($blog_version < $CFG->blog_version) {
notify('WARNING!!! The blog code you are using is OLDER than the version that made these databases! ('. $version .' < '. $CFG->blog_version .')');
}
}
/**
* course_has_blog_entries
* Given a course id return true if there are blog entries from any user related to that course
* $courseid - the id for the course
* Daryl Hawes note: When forum entries start using post table this function will no longer behave as expected
* Since non blog posts will be associated with this course. Perhaps moduleid or another field could be wrangled
* into identifying the type of post?
*/
function blog_course_has_blog_entries($courseid) {
$entries = get_records('post', 'courseid', $courseid);
if (! empty($entries) ) {
return true;
}
return false;
}
/**
* Output a hidden html form used by text entry pages that require preview.php's functionality
*/
function blog_print_preview_form($userid=0, $categoryelement='', $return=false) {
$returnstring = "\n".'
'."\n";
if ($return) {
return $returnstring; //return the form as a string if requested
}
print $returnstring; //else just print the string and exit the function
}
/**
* 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)) {
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;
//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);
}
/**
* Turns a php string into a string ready for insert into an rss xml feed
*/
function blog_rss_content($str) {
$str = superhtmlentities($str);
$content = "\n";
return $content;
}
/**
* function found when searching on problem of smart quotes
* posted at http://daynah.php-princess.net/index.php?p=94
*/
function superhtmlentities($text) {
$entities = array(128 => 'euro', 130 => 'sbquo',
131 => 'fnof', 132 => 'bdquo', 133 => 'hellip',
134 => 'dagger', 135 => 'Dagger', 136 => 'circ',
137 => 'permil', 138 => 'Scaron', 139 => 'lsaquo',
140 => 'OElig', 145 => 'lsquo', 146 => 'rsquo',
147 => 'ldquo', 148 => 'rdquo', 149 => 'bull',
150 => 'ndash', 151 => 'mdash', 152 => 'tilde',
153 => 'trade', 154 => 'scaron', 155 => 'rsaquo',
156 => 'oelig', 159 => 'Yuml');
$new_text = '';
for($i = 0; $i < strlen($text); $i++) {
$num = ord($text{$i});
if (array_key_exists($num, $entities)) {
$new_text .= '&\\'.$entities[$num].';';
} else if ($num < 127 || $num > 159) {
$new_text .= $text{$i};
}
}
return htmlentities($new_text);
}
/**
* blog_get_recent_entries_byrange
* not in blogInfo because entries being searched
* can be found in any number of blogs rather than just one.
*
* Returns specified range of entries since a given time.
*
* In using this function be aware for your where clause that the tables being
* read are post (e) and blog_categories_entries (c) and the returned values
* will be e.*
*
* @param int $limit .
* @param int $start .
* @param string $where .
* @param string $orderby .
* @param bool $includeCategories .
*
* @return BlogEntries
*/
function blog_get_recent_entries_byrange($limit, $start, $where='', $orderby='lastmodified DESC', $includeCategories=false) {
global $CFG;
// echo 'Debug: where clause in blog_get_recent_entries_byrange: $where '; //debug
if ($includeCategories) {
$records = get_records_select('post e, '. $CFG->prefix .'blog_categories_entries c', $where, $orderby, '*', $start, $limit);
} else {
$records = get_records_select('post', $where, $orderby, '*', $start, $limit);
}
// print_object($records); //debug
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;
}
}
return $blogEntries;
}
}
/**
* returns a unix time stamp from year month and day
* used to get start and end dates for comparing against timestamps in databases
*/
function blog_get_month_time($y, $m, $d, $firstday=true) {
if ( !empty($y) && !empty($m) && !empty($d)) {
$time = mktime(0, 0, 0, $m, $d, $y);
} else if ( !empty($y) && !empty($m) ) {
$day = blog_mk_getLastDayofMonth($m, $y);
if ($firstday) {
$day = 1;
}
$time = mktime(0, 0, 0, $m, $day, $y);
} else {
$time = '';
}
return $time;
}
/**
* attempting to create a method useful for a popup form on the index
* page that will allow a user to filter entries based upon date
*
* This function will return a list, in order, formatted as "MM-YYYY" for each year and month
* in which this blog has entries.
* @param BlogFilter $blogFilter - a BlogFilter object with the current filter settings for this page
*/
function blog_get_year_month_of_viewable_entries(&$blogFilter) {
global $_SERVER;
$entries = $blogFilter->get_filtered_entries();
$datearray = array();
$datearray['m=&y='] = 'All Dates';
if ( !empty($entries) ) {
foreach ($entries as $entry) {
if ( $entry->user_can_view() ) {
// print_object($entry); //debug
//user is allowed to see this entry, so return its year/month information
$date = $entry->entryLastModified;
$truncDate = date("F Y", $date); // this will return January 2004 for the date.
$curmonth = date("m", $date); // this will return January 2004 for the date.
$curyear = date("Y", $date); // this will return January 2004 for the date.
$index = 'm='. $curmonth .'&y='. $curyear;
$datearray[$index] = $truncDate;
}
}
}
// print_object($datearray); //debug
if (is_numeric($blogFilter->startmonth) && is_numeric($blogFilter->startyear)) {
$selected = 'm='. $blogFilter->startmonth .'&y='. $blogFilter->startyear;
} else {
$selected = '';
}
$unused = array('startmonth', 'startyear');
$getvars = $blogFilter->get_getvars($unused);
//attach a random number to the popup function's form name
//becuase there may be multiple instances of this form on each page
$self = basename($_SERVER['PHP_SELF']);
$form = popup_form($self . $getvars .'&', $datearray, 'blog_date_selection'. mt_rand(), $selected, '', '', '', true);
return str_replace(''."\n";
}
*/
//yu: testing code
if (blog_isLoggedIn()) {
//the user's blog is enabled and they are viewing their own blog
$morelink .= $blogFilter->get_complete_link($CFG->wwwroot .'/blog/edit.php', get_string('addentries', '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') .'
';
$userbloginfo = blog_user_bloginfo();
if (blog_isLoggedIn()) {
//the user's blog is enabled and they are viewing their own blog
$output .= $blogFilter->get_complete_link($CFG->wwwroot .'/blog/edit.php', get_string('addentries', 'blog'));
}
print $output;
unset($blogFilter->filtered_entries);
}
/**
* What text should be displayed claiming ownership to the current blog entries?
* @uses $PAGE
* @return string
*/
function blog_get_title_text() {
global $PAGE; //hackish
if (isset($PAGE) && isset($PAGE->bloginfo)) {
$blogInfo = &$PAGE->bloginfo;
$title = $blogInfo->get_blog_title();
if($title != '') {
$displaytitle = $title;
$tagline = $blogInfo->get_blog_tagline();
if ($tagline != '') {
$displaytitle .= ' - '. $tagline;
}
}
}
if (isset($displaytitle)) {
return $displaytitle;
} else {
// Daryl Hawes - better wording would be good here, localize this line once the wording is selected.
return 'Combined Blog '. get_string('entries', 'blog') .' ';
}
}
/**
* blog_get_moodle_pix_path
*
* Returns the directory path to the current theme's pix folder.
* @return string
*/
function blog_get_moodle_pix_path(){
global $CFG, $THEME;
if (empty($THEME->custompix)) {
return $CFG->wwwroot.'/pix';
} else {
return $CFG->themedir.current_theme().'/pix';
}
}
/**
* 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) {
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['blogtitle'] = $bloginfo->blogtitle;
$template['blogtagline'] = $bloginfo->blogtagline;
$template['blogurl'] = $bloginfo->get_blogurl();
$template['body'] = $blogEntry->get_formatted_entry_body();
$template['countofextendedbody'] = 0;
if ($template['extendedbody'] = $blogEntry->get_formatted_entry_extended_body()) {
$template['extendedbody'] = $blogEntry->get_formatted_entry_extended_body();
$template['countofextendedbody'] = count_words($template->extendedbody);
} else {
$template['extendedbody'] = '';
}
if ($viewtype=='full' && !empty($template['extendedbody'])) {
$template['body'] .= '' . $template['extendedbody'];
} else if ( !empty($template->extendedbody) && $template->countofextendedbody != 0) {
$template['body'] .= ' '. get_string('moreelipses', 'blog') .' ('. $template['countofextendedbody'] .' words)';
}
$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['editbuttons'] = $blogEntry->get_formatted_edit_URL(true);
$template['editbuttons'] .= $blogEntry->get_formatted_delete_URL(true);
$template['courseid'] = $blogEntry->entryCourseId;
$template['userid'] = $blogEntry->entryuserid;
$template['authorviewurl'] = $CFG->wwwroot .'/user/view.php?course=1&id='. $template['userid'];
$template['moodlepix'] = blog_get_moodle_pix_path();
$template['author'] = $blogEntry->entryAuthorName;
$template['lastmod'] = $blogEntry->formattedEntryLastModified;
$template['created'] = $blogEntry->formattedEntryCreated;
$template['publishtomenu'] = $blogEntry->get_publish_to_menu(true, true);
$template['groupid'] = $blogEntry->entryGroupId;
//forum style printing of blogs
blog_print_entry_content ($template, $blogEntry->entryId, $filtertype, $filterselect);
}
//forum style printing of blogs
function blog_print_entry_content ($template, $entryid, $filtertype='', $filterselect='') {
global $USER, $CFG, $course, $ME;
$stredit = get_string('edit');
$strdelete = get_string('delete');
$user = get_record('user','id',$template['userid']);
echo '
'."\n";
// 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', 'blog');
foreach ($blogtags as $blogtag) {
echo ''.$blogtag->text.', ';
}
}
/// 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') );
if (is_numeric($courseid) && $courseid != SITEID && $course = get_record('course', 'id', $courseid, '', '', '', '', 'shortname') ) {
require_login($courseid);
// if we're viewing a course allow publishing to course teachers
$options['teacher'] = get_string('publishtoteachers', 'blog', $course->shortname);
if (!$CFG->blog_enable_moderation || isadmin() || isteacher($courseid) ) {
// only admins and teachers can publish to course members when moderation is enabled
$options['course'] = get_string('publishtocourse', 'blog', $course->shortname);
}
}
/*
//groups not supported quite yet - pseudocode:
if (isset($post->groupid) && $post->groupid != '') {
$options['group'] = 'Fellow group members and teachers can view';
}*/
if (!$CFG->blog_enable_moderation || isadmin() || (is_numeric($courseid) && isteacher($courseid)) ) {
// only admins and teachers can see site and public options when moderation is enabled
$options['site'] = get_string('publishtosite', 'blog');
$options['public'] = get_string('publishtoworld', 'blog');
}
return $options;
}
/**
* blog_get_entries_by_category - not in blogInfo because entries being searched
* can be found in any number of blogs rather than just one.
* catids can be an array of category ids or a single category id
* defined as either
* $catids = array(1, 2, 3, 4, 5);
* or
* $catids = 2;
*
* Used by rss.php
*/
function blog_get_entries_by_category($catids, $courseid=0, $limit=8, $start=0) {
$catsearch = ' e.id=c.entryid AND '; //only gets categories whose postid matches entries retrieved
if (blog_array_count($catids) > 0) {
$count = 0;
foreach ($catids as $catid) {
$catsearch .= 'c.categoryid='. $catid .' ';
$count += 1;
if (count($catids) != $count) {
$catsearch .= 'OR ';
}
}
} else {
$catsearch .= 'c.categoryid='. $catids;
}
$wherecourse = '';
if (is_numeric($courseid) && $courseid != 0 && $courseid != 1) {
$wherecourse = ' AND e.courseid='. $courseid;
}
$where = $catsearch.$wherecourse;
// echo 'Debug: where clause for blog_get_entries_by_category: '. $where; //debug
return blog_get_recent_entries_byrange($limit, $start, $where, '', true);
}
/**
* builds calendar with links to filter entries by date
* modified by Daryl Hawes to build links even when no userid is specified
* and to return the html as a string if desired
*/
function blog_draw_calendar(&$blogFilter, $return=false)
{
global $CFG;
// print_object($blogFilter);
if (!empty($blogFilter->startmonth)) {
$m = $blogFilter->startmonth;
} else {
$m = date('n', mktime());
$blogFilter->startmonth = $m;
}
if (!empty($blogFilter->startyear)) {
$y = $blogFilter->startyear;
} else {
$y = date('Y', mktime());
$blogFilter->startyear = $y;
}
$userid = $blogFilter->userid;
//create a string to represent a URL argument with userid info. If no userid then string is empty.
$useridString = '&userid='. $userid;
if ($userid == 0 || $userid == '') {
$useridString = '';
}
// calculate the weekday the first of the month is on
$tmpd = getdate(mktime(0, 0, 0, $m, 1, $y));
$monthname = $tmpd['month'];
$firstwday= $tmpd['wday'];
$today = date('Ymd', mktime());
$lastday = blog_mk_getLastDayofMonth($m, $y);
//determine next and previous month
if (($m - 1) < 1) { $pm = 12; } else { $pm = $m - 1; }
if (($m + 1) > 12) { $nm = 1; } else { $nm = $m + 1; }
if (strlen($pm) == 1) { $pm = '0'. $pm; };
if (strlen($nm) == 1) { $nm = '0'. $nm; };
$returnstring = "\n".'
'."\n";
$d = 1;
$wday = $firstwday;
$firstweek = true;
// loop through all the days of the month
while ( $d <= $lastday)
{
// set up blank days for first week
if ($firstweek) {
$returnstring .= '