From: skodak Date: Sun, 13 Apr 2008 19:15:02 +0000 (+0000) Subject: MDL-14113 patch for multiple issues - see tracker for subtasks; merged from MOODLE_19... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=90f4745c2223465a7040214626d01d6556e16154;p=moodle.git MDL-14113 patch for multiple issues - see tracker for subtasks; merged from MOODLE_19_STABLE --- diff --git a/course/lib.php b/course/lib.php index 04ef25af53..6ac809bfc0 100644 --- a/course/lib.php +++ b/course/lib.php @@ -942,7 +942,7 @@ function print_recent_activity($course) { $content = $print_recent_activity($course, $viewfullnames, $timestart) || $content; } } else { - debugging("Missing lib.php in lib/{$mod->name} - please reinstall files or uninstall the module"); + debugging("Missing lib.php in lib/{$modname} - please reinstall files or uninstall the module"); } } @@ -1255,7 +1255,6 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false, static $strmovehere; static $strmovefull; static $strunreadpostsone; - static $untracked; static $usetracking; static $groupings; @@ -1272,7 +1271,6 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false, include_once($CFG->dirroot.'/mod/forum/lib.php'); if ($usetracking = forum_tp_can_track_forums()) { $strunreadpostsone = get_string('unreadpostsone', 'forum'); - $untracked = forum_tp_get_untracked_forums($USER->id, $course->id); } $initialised = true; } @@ -1391,18 +1389,14 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false, } } if ($usetracking && $mod->modname == 'forum') { - if ($usetracking and !isset($untracked[$mod->instance])) { - $groupid = groups_get_activity_group($mod); - $unread = forum_tp_count_forum_unread_posts($USER->id, $mod->instance, $groupid); - if ($unread) { - echo ' '; - if ($unread == 1) { - echo $strunreadpostsone; - } else { - print_string('unreadpostsnumber', 'forum', $unread); - } - echo ' '; + if ($unread = forum_tp_count_forum_unread_posts($mod, $course)) { + echo ' '; + if ($unread == 1) { + echo $strunreadpostsone; + } else { + print_string('unreadpostsnumber', 'forum', $unread); } + echo ''; } } diff --git a/index.php b/index.php index 81ff29c419..d40094bd1e 100644 --- a/index.php +++ b/index.php @@ -197,7 +197,7 @@ if (!empty($USER->id)) { $SESSION->fromdiscussion = $CFG->wwwroot; - if (forum_is_subscribed($USER->id, $newsforum->id)) { + if (forum_is_subscribed($USER->id, $newsforum)) { $subtext = get_string('unsubscribe', 'forum'); } else { $subtext = get_string('subscribe', 'forum'); diff --git a/mod/forum/db/upgrade.php b/mod/forum/db/upgrade.php index 008e57dfc3..9ca8ec2e3a 100644 --- a/mod/forum/db/upgrade.php +++ b/mod/forum/db/upgrade.php @@ -53,6 +53,35 @@ function xmldb_forum_upgrade($oldversion=0) { $db->debug = true; } + if ($result && $oldversion < 2007101512) { + + /// Cleanup the forum subscriptions + notify('Removing stale forum subscriptions', 'notifysuccess'); + + $roles = get_roles_with_capability('moodle/course:view', CAP_ALLOW); + $roles = array_keys($roles); + $roles = implode(',', $roles); + + $sql = "SELECT fs.userid, f.id AS forumid + FROM {$CFG->prefix}forum f + JOIN {$CFG->prefix}course c ON c.id = f.course + JOIN {$CFG->prefix}context ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = ".CONTEXT_COURSE.") + JOIN {$CFG->prefix}forum_subscriptions fs ON fs.forum = f.id + LEFT JOIN {$CFG->prefix}role_assignments ra ON (ra.contextid = ctx.id AND ra.userid = fs.userid AND ra.roleid IN ($roles)) + WHERE ra.id IS NULL"; + + if ($rs = get_recordset_sql($sql)) { + $db->debug = false; + while ($remove = rs_fetch_next_record($rs)) { + delete_records('forum_subscriptions', 'userid', $remove->userid, 'forum', $remove->forumid); + echo '.'; + } + $db->debug = true; + rs_close($rs); + } + } + + return $result; } diff --git a/mod/forum/discuss.php b/mod/forum/discuss.php index 3f019815a3..f7702fcf20 100644 --- a/mod/forum/discuss.php +++ b/mod/forum/discuss.php @@ -14,11 +14,11 @@ $postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated. if (!$discussion = get_record('forum_discussions', 'id', $d)) { - print_error("Discussion ID was incorrect or no longer exists"); + error("Discussion ID was incorrect or no longer exists"); } if (!$course = get_record('course', 'id', $discussion->course)) { - print_error("Course ID is incorrect - discussion is faulty"); + error("Course ID is incorrect - discussion is faulty"); } if (!$forum = get_record('forum', 'id', $discussion->forum)) { @@ -26,7 +26,7 @@ } if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } require_course_login($course, true, $cm); @@ -38,7 +38,7 @@ if (!($USER->id == $discussion->userid || (($discussion->timestart == 0 || $discussion->timestart <= time()) && ($discussion->timeend == 0 || $discussion->timeend > time())))) { - print_error('Discussion ID was incorrect or no longer exists', '', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id"); + error('Discussion ID was incorrect or no longer exists', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id"); } } @@ -49,25 +49,26 @@ require_capability('mod/forum:movediscussions', $modcontext); if ($forum->type == 'single') { - print_error('Cannot move discussion from a simple single discussion forum', '', $return); + error('Cannot move discussion from a simple single discussion forum', $return); } if (!$forumto = get_record('forum', 'id', $move)) { - print_error('You can\'t move to that forum - it doesn\'t exist!', '', $return); + error('You can\'t move to that forum - it doesn\'t exist!', $return); } if (!$cmto = get_coursemodule_from_instance('forum', $forumto->id, $course->id)) { - print_error('Target forum not found in this course.', '', $return); + error('Target forum not found in this course.', $return); } if (!coursemodule_visible_for_user($cmto)) { - print_error('Forum not visible', '', $return); + error('Forum not visible', $return); } if (!forum_move_attachments($discussion, $forumto->id)) { notify("Errors occurred while moving attachment directories - check your file permissions"); } set_field('forum_discussions', 'forum', $forumto->id, 'id', $discussion->id); + set_field('forum_read', 'forumid', $forumto->id, 'discussionid', $discussion->id); add_to_log($course->id, 'forum', 'move discussion', "discuss.php?d=$discussion->id", $discussion->id, $cmto->id); require_once($CFG->libdir.'/rsslib.php'); @@ -77,8 +78,8 @@ // the regeneration of the feeds since the discussions have been // moved. if (!forum_rss_delete_file($forum) || !forum_rss_delete_file($forumto)) { - print_error('Could not purge the cached RSS feeds for the source and/or'. - 'destination forum(s) - check your file permissionsforums', '', $return); + error('Could not purge the cached RSS feeds for the source and/or'. + 'destination forum(s) - check your file permissionsforums', $return); } redirect($return.'&moved=-1&sesskey='.sesskey()); @@ -109,19 +110,18 @@ } if (! $post = forum_get_post_full($parent)) { - print_error("Discussion no longer exists", '', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id"); + error("Discussion no longer exists", "$CFG->wwwroot/mod/forum/view.php?f=$forum->id"); } if (!forum_user_can_view_post($post, $course, $cm, $forum, $discussion)) { - print_error('You do not have permissions to view this post', '', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id"); + error('You do not have permissions to view this post', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id"); } if ($mark == 'read' or $mark == 'unread') { - if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum) && - $CFG->forum_usermarksread) { + if ($CFG->forum_usermarksread && forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) { if ($mark == 'read') { - forum_tp_add_read_record($USER->id, $postid, $discussion->id, $forum->id); + forum_tp_add_read_record($USER->id, $postid); } else { // unread forum_tp_delete_read_records($USER->id, $postid); diff --git a/mod/forum/index.php b/mod/forum/index.php index b567c69efe..2dc2a8c937 100644 --- a/mod/forum/index.php +++ b/mod/forum/index.php @@ -9,11 +9,11 @@ if ($id) { if (! $course = get_record('course', 'id', $id)) { - print_error("Course ID is incorrect"); + error("Course ID is incorrect"); } } else { if (! $course = get_site()) { - print_error("Could not find a top-level course!"); + error("Could not find a top-level course!"); } } @@ -51,7 +51,7 @@ $generaltable->head = array ($strforum, $strdescription, $strdiscussions); $generaltable->align = array ('left', 'left', 'center'); - if ($usetracking = (!isguestuser() && forum_tp_can_track_forums())) { + if ($usetracking = forum_tp_can_track_forums()) { $untracked = forum_tp_get_untracked_forums($USER->id, $course->id); $generaltable->head[] = $strunreadposts; @@ -61,6 +61,8 @@ $generaltable->align[] = 'center'; } + $subscribed_forums = forum_get_subscribed_forums($course); + if ($can_subscribe = (!isguestuser() && has_capability('moodle/course:view', $coursecontext))) { $generaltable->head[] = $strsubscribed; $generaltable->align[] = 'center'; @@ -118,11 +120,12 @@ /// Do course wide subscribe/unsubscribe if (!is_null($subscribe) and !isguestuser() and !isguest()) { foreach ($modinfo->instances['forum'] as $forumid=>$cm) { - if (!forum_is_forcesubscribed($forumid)) { - $subscribed = forum_is_subscribed($USER->id, $forumid); + $forum = $forums[$forumid]; + if (!forum_is_forcesubscribed($forum)) { + $subscribed = forum_is_subscribed($USER->id, $forum); if ($subscribe && !$subscribed) { forum_subscribe($USER->id, $forumid); - } elseif (!$subscribe && $subscribed) { + } else if (!$subscribe && $subscribed) { forum_unsubscribe($USER->id, $forumid); } } @@ -147,70 +150,48 @@ $cm = $modinfo->instances['forum'][$forum->id]; $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $groupmode = groups_get_activity_groupmode($cm, $course); - $currentgroup = groups_get_activity_group($cm); - - if ($groupmode == SEPARATEGROUPS) { - $accessallgroups = has_capability('moodle/site:accessallgroups', $context); - } else { - $accessallgroups = true; - } - - $cantaccessagroup = !$accessallgroups and empty($currentgroup); - - // this is potentially wrong logic. could possibly check for if user has the right to hmmm - if ($cantaccessagroup) { - $count = ''; - - } if ($currentgroup) { - $count = count_records_select('forum_discussions', "forum = $forum->id AND (groupid = $currentgroup OR groupid = -1)"); - - } else { - $count = count_records('forum_discussions', 'forum', $forum->id); - } + $count = forum_count_discussions($forum, $cm, $course); if ($usetracking) { if ($forum->trackingtype == FORUM_TRACKING_OFF) { $unreadlink = '-'; $trackedlink = '-'; - } else if (($forum->trackingtype == FORUM_TRACKING_ON) || !isset($untracked[$forum->id])) { - $groupid = !$accessallgroups ? $currentgroup : false; - $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid); - if ($unread > 0) { - $unreadlink = ''.$unread.''; + + } else { + if (isset($untracked[$forum->id])) { + $unreadlink = '-'; + } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) { + $unreadlink = ''.$unread.''; $unreadlink .= ''.$strmarkallread.''; } else { - $unreadlink = ''.$unread.''; + $unreadlink = '0'; } + if ($forum->trackingtype == FORUM_TRACKING_ON) { + $trackedlink = $stryes; - if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) { - $trackedlink = print_single_button($CFG->wwwroot . '/mod/forum/settracking.php?id=' . $forum->id, '', $stryes, 'post', '_self', true, $strnotrackforum); } else { - $trackedlink = $stryes; + $options = array('id'=>$forum->id); + if (!isset($untracked[$forum->id])) { + $trackedlink = print_single_button($CFG->wwwroot.'/mod/forum/settracking.php', $options, $stryes, 'post', '_self', true, $strnotrackforum); + } else { + $trackedlink = print_single_button($CFG->wwwroot.'/mod/forum/settracking.php', $options, $strno, 'post', '_self', true, $strtrackforum); + } } - } else { - $unreadlink = '-'; - $trackedlink = print_single_button($CFG->wwwroot . '/mod/forum/settracking.php?id=' . $forum->id, '', $strno, 'post', '_self', true, $strtrackforum); } } $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost); $forumname = format_string($forum->name, true);; - if ($cantaccessagroup) { - $forumlink = $forumname; - $discussionlink = $count; + if ($cm->visible) { + $style = ''; } else { - if ($cm->visible) { - $style = ''; - } else { - $style = 'class="dimmed"'; - } - $forumlink = "id\" $style>".format_string($forum->name,true).""; - $discussionlink = "id\" $style>".$count.""; + $style = 'class="dimmed"'; } + $forumlink = "id\" $style>".format_string($forum->name,true).""; + $discussionlink = "id\" $style>".$count.""; $row = array ($forumlink, $forum->intro, $discussionlink); if ($usetracking) { @@ -219,21 +200,29 @@ } if ($can_subscribe) { - $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes, - 'unsubscribed' => $strno, 'forcesubscribed' => $stryes, - 'cantsubscribe' => '-'), $cantaccessagroup, false, true); + if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) { + $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes, + 'unsubscribed' => $strno, 'forcesubscribed' => $stryes, + 'cantsubscribe' => '-'), false, false, true, $subscribed_forums); + } else { + $row[] = '-'; + } } //If this forum has RSS activated, calculate it - if ($show_rss and $forum->rsstype and $forum->rssarticles) { - //Calculate the tolltip text - if ($forum->rsstype == 1) { - $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum', format_string($forum->name)); + if ($show_rss) { + if ($forum->rsstype and $forum->rssarticles) { + //Calculate the tolltip text + if ($forum->rsstype == 1) { + $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum', format_string($forum->name)); + } else { + $tooltiptext = get_string('rsssubscriberssposts', 'forum', format_string($forum->name)); + } + //Get html code for RSS link + $row[] = rss_get_link($course->id, $USER->id, 'forum', $forum->id, $tooltiptext); } else { - $tooltiptext = get_string('rsssubscriberssposts', 'forum', format_string($forum->name)); + $row[] = ' '; } - //Get html code for RSS link - $row[] = rss_get_link($course->id, $USER->id, 'forum', $forum->id, $tooltiptext); } $generaltable->data[] = $row; @@ -283,51 +272,35 @@ $cm = $modinfo->instances['forum'][$forum->id]; $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $groupmode = groups_get_activity_groupmode($cm, $course); - $currentgroup = groups_get_activity_group($cm); - - if ($groupmode == SEPARATEGROUPS) { - $accessallgroups = has_capability('moodle/site:accessallgroups', $context); - } else { - $accessallgroups = true; - } - - $cantaccessagroup = !$accessallgroups and empty($currentgroup); - - if ($cantaccessagroup) { - $count = ''; - - } if ($currentgroup) { - $count = count_records_select('forum_discussions', "forum = $forum->id AND (groupid = $currentgroup OR groupid = -1)"); - - } else { - $count = count_records('forum_discussions', 'forum', $forum->id); - } + $count = forum_count_discussions($forum, $cm, $course); if ($usetracking) { if ($forum->trackingtype == FORUM_TRACKING_OFF) { - $unreadlink = '-'; + $unreadlink = '-'; $trackedlink = '-'; - } else if (($forum->trackingtype == FORUM_TRACKING_ON) || - !isset($untracked[$forum->id])) { - $groupid = !$accessallgroups ? $currentgroup : false; - $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid); - if ($unread > 0) { + } else { + if (isset($untracked[$forum->id])) { + $unreadlink = '-'; + } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) { $unreadlink = ''.$unread.''; $unreadlink .= ''.$strmarkallread.''; } else { - $unreadlink = ''.$unread.''; + $unreadlink = '0'; } - if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) { - $trackedlink = print_single_button($CFG->wwwroot . '/mod/forum/settracking.php?id=' . $forum->id, '', $stryes, 'post', '_self', true, $strnotrackforum); - } else { + + if ($forum->trackingtype == FORUM_TRACKING_ON) { $trackedlink = $stryes; + + } else { + $options = array('id'=>$forum->id); + if (!isset($untracked[$forum->id])) { + $trackedlink = print_single_button($CFG->wwwroot.'/mod/forum/settracking.php', $options, $stryes, 'post', '_self', true, $strnotrackforum); + } else { + $trackedlink = print_single_button($CFG->wwwroot.'/mod/forum/settracking.php', $options, $strno, 'post', '_self', true, $strtrackforum); + } } - } else { - $unreadlink = '-'; - $trackedlink = print_single_button($CFG->wwwroot . '/mod/forum/settracking.php?id=' . $forum->id, '', $strno, 'post', '_self', true, $strtrackforum); } } @@ -345,18 +318,14 @@ } $forumname = format_string($forum->name,true);; - if ($cantaccessagroup) { - $forumlink = $forumname; - $discussionlink = $count; + + if ($cm->visible) { + $style = ''; } else { - if ($cm->visible) { - $style = ''; - } else { - $style = 'class="dimmed"'; - } - $forumlink = "id\" $style>".format_string($forum->name,true).""; - $discussionlink = "id\" $style>".$count.""; + $style = 'class="dimmed"'; } + $forumlink = "id\" $style>".format_string($forum->name,true).""; + $discussionlink = "id\" $style>".$count.""; $row = array ($printsection, $forumlink, $forum->intro, $discussionlink); if ($usetracking) { @@ -365,21 +334,29 @@ } if ($can_subscribe) { - $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes, - 'unsubscribed' => $strno, 'forcesubscribed' => $stryes, - 'cantsubscribe' => '-'), $cantaccessagroup, false, true); + if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) { + $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes, + 'unsubscribed' => $strno, 'forcesubscribed' => $stryes, + 'cantsubscribe' => '-'), false, false, true, $subscribed_forums); + } else { + $row[] = '-'; + } } //If this forum has RSS activated, calculate it - if ($show_rss and $forum->rsstype and $forum->rssarticles) { - //Calculate the tolltip text - if ($forum->rsstype == 1) { - $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum', format_string($forum->name)); + if ($show_rss) { + if ($forum->rsstype and $forum->rssarticles) { + //Calculate the tolltip text + if ($forum->rsstype == 1) { + $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum', format_string($forum->name)); + } else { + $tooltiptext = get_string('rsssubscriberssposts', 'forum', format_string($forum->name)); + } + //Get html code for RSS link + $row[] = rss_get_link($course->id, $USER->id, 'forum', $forum->id, $tooltiptext); } else { - $tooltiptext = get_string('rsssubscriberssposts', 'forum', format_string($forum->name)); + $row[] = ' '; } - //Get html code for RSS link - $row[] = rss_get_link($course->id, $USER->id, 'forum', $forum->id, $tooltiptext); } $learningtable->data[] = $row; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 178058e42b..25d3ed1184 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -33,6 +33,8 @@ define ('FORUM_AGGREGATE_SUM', 5); * (defined by the form in mod.html) this function * will create a new instance and return the id number * of the new instance. + * @param object $forum add forum instance (with magic quotes) + * @return int intance id */ function forum_add_instance($forum) { global $CFG; @@ -63,7 +65,7 @@ function forum_add_instance($forum) { $discussion->mailnow = false; if (! forum_add_discussion($discussion, $discussion->intro)) { - print_error('Could not add the discussion for this forum'); + error('Could not add the discussion for this forum'); } } @@ -85,7 +87,9 @@ function forum_add_instance($forum) { * Given an object containing all the necessary data, * (defined by the form in mod.html) this function * will update an existing instance with new data. -*/ + * @param object $forum forum instance (with magic quotes) + * @return bool success + */ function forum_update_instance($forum) { $forum->timemodified = time(); $forum->id = $forum->instance; @@ -114,11 +118,11 @@ function forum_update_instance($forum) { notify('Warning! There is more than one discussion in this forum - using the most recent'); $discussion = array_pop($discussions); } else { - print_error('Could not find the discussion in this forum'); + error('Could not find the discussion in this forum'); } } if (! $post = get_record('forum_posts', 'id', $discussion->firstpost)) { - print_error('Could not find the first post in this forum discussion'); + error('Could not find the first post in this forum discussion'); } $post->subject = $forum->name; @@ -126,18 +130,18 @@ function forum_update_instance($forum) { $post->modified = $forum->timemodified; if (! update_record('forum_posts', ($post))) { - print_error('Could not update the first post'); + error('Could not update the first post'); } $discussion->name = $forum->name; if (! update_record('forum_discussions', ($discussion))) { - print_error('Could not update the discussion'); + error('Could not update the discussion'); } } if (!update_record('forum', $forum)) { - print_error('Can not update forum'); + error('Can not update forum'); } $forum = stripslashes_recursive($forum); @@ -151,6 +155,8 @@ function forum_update_instance($forum) { * Given an ID of an instance of this module, * this function will permanently delete the instance * and any data that depends on it. + * @param int forum instance id + * @return bool success */ function forum_delete_instance($id) { @@ -188,7 +194,8 @@ function forum_delete_instance($id) { * Function to be run periodically according to the moodle cron * Finds all posts that have yet to be mailed out, and mails them * out to all subscribers -*/ + * @return void + */ function forum_cron() { global $CFG, $USER; @@ -217,7 +224,7 @@ function forum_cron() { $endtime = $timenow - $CFG->maxeditingtime; $starttime = $endtime - 48 * 3600; // Two days earlier - if ($posts = forum_get_unmailed_posts($starttime, $endtime)) { + if ($posts = forum_get_unmailed_posts($starttime, $endtime, $timenow)) { // Mark them all now as being mailed. It's unlikely but possible there // might be an error later so that a post is NOT actually mailed out, // but since mail isn't crucial, we can accept this risk. Doing it now @@ -314,7 +321,8 @@ function forum_cron() { // init caches $userto->viewfullnames = array(); $userto->canpost = array(); - $userto->tracking = array(); + $userto->markposts = array(); + $userto->enrolledin = array(); // reset the caches foreach ($coursemodules as $forumid=>$unused) { @@ -329,13 +337,22 @@ function forum_cron() { $discussion = $discussions[$post->discussion]; $forum = $forums[$discussion->forum]; $course = $courses[$forum->course]; - $cm = $coursemodules[$forum->id]; + $cm =& $coursemodules[$forum->id]; // Do some checks to see if we can bail out now if (!isset($subscribedusers[$forum->id][$userto->id])) { continue; // user does not subscribe to this forum } + // Verify user is enrollend in course - if not do not send any email + if (!isset($userto->enrolledin[$course->id])) { + $userto->enrolledin[$course->id] = has_capability('moodle/course:view', get_context_instance(CONTEXT_COURSE, $course->id)); + } + if (!$userto->enrolledin[$course->id]) { + // oops - this user should not receive anything from this course + continue; + } + // Get info about the sending user if (array_key_exists($post->userid, $users)) { // we might know him/her already $userfrom = $users[$post->userid]; @@ -394,7 +411,7 @@ function forum_cron() { $queue->userid = $userto->id; $queue->discussionid = $discussion->id; $queue->postid = $post->id; - $queue->timemodified = $post->modified; + $queue->timemodified = $post->created; if (!insert_record('forum_queue', $queue)) { mtrace("Error: mod/forum/cron.php: Could not queue for digest mail for id $post->id to user $userto->id ($userto->email) .. not trying again."); } @@ -438,22 +455,17 @@ function forum_cron() { } else { $mailcount[$post->id]++; - if (!isset($userto->tracking[$forum->id])) { - $userto->tracking[$forum->id] = !$CFG->forum_usermarksread - && forum_tp_can_track_forums($forum, $userto) - && forum_tp_is_tracked($forum, $userto->id); - } // Mark post as read if forum_usermarksread is set off - if ($userto->tracking[$forum->id]) { - if (!forum_tp_mark_post_read($userto->id, $post, $forum->id)) { - mtrace("Error: mod/forum/cron.php: Could not mark post $post->id read for user $userto->id". - " while sending email."); - } + if (!$CFG->forum_usermarksread) { + $userto->markposts[$post->id] = $post->id; } } mtrace('post '.$post->id. ': '.$post->subject); } + + // mark processed posts as read + forum_tp_mark_posts_read($userto, $userto->markposts); } } @@ -489,9 +501,6 @@ function forum_cron() { $timenow = time(); $digesttime = usergetmidnight($timenow, $sitetimezone) + ($CFG->digestmailtime * 3600); - // some debug code for 2digests per day problem - mtrace(' (Expected digest time today: '.$digesttime.' admin user zone:'.userdate($digesttime).' site zone:'.userdate($digesttime, '', $sitetimezone).')'); - // Delete any really old ones (normally there shouldn't be any) $weekago = $timenow - (7 * 24 * 3600); delete_records_select('forum_queue', "timemodified < $weekago"); @@ -578,6 +587,9 @@ function forum_cron() { @set_time_limit(120); // terminate if processing of any account takes longer than 2 minutes + $USER = $cronuser; + course_setup(SITEID); // reset cron user language, theme and timezone settings + mtrace(get_string('processingdigest', 'forum', $userid), '... '); // First of all delete all the queue entries for this user @@ -592,7 +604,7 @@ function forum_cron() { // init caches $userto->viewfullnames = array(); $userto->canpost = array(); - $userto->tracking = array(); + $userto->markposts = array(); $postsubject = get_string('digestmailsubject', 'forum', format_string($site->shortname, true)); @@ -610,8 +622,6 @@ function forum_cron() { $posthtml .= "\n\n"; $posthtml .= '

'.get_string('digestmailheader', 'forum', $headerdata).'



'; - $markread = array(); - foreach ($thesediscussions as $discussionid) { @set_time_limit(120); // to be reset for each post @@ -700,16 +710,9 @@ function forum_cron() { $posttext .= forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $userto, true); $posthtml .= forum_make_mail_post($course, $forum, $discussion, $post, $userfrom, $userto, false, $canreply, true, false); - if (!isset($userto->tracking[$forum->id])) { - $userto->tracking[$forum->id] = !$CFG->forum_usermarksread - && forum_tp_can_track_forums($forum, $userto) - && forum_tp_is_tracked($forum, $userto->id); - } - // Create an array of postid's for this user to mark as read. - if ($userto->tracking[$forum->id]) { - $markread[$post->id]->post = $post; - $markread[$post->id]->forumid = $forum->id; + if (!$CFG->forum_usermarksread) { + $userto->markposts[$post->id] = $post->id; } } } @@ -738,13 +741,8 @@ function forum_cron() { mtrace("success."); $usermailcount++; - // Mark post as read if forum_usermarksread is set off - foreach ($markread as $postinfo) { - if (!forum_tp_mark_post_read($userto->id, $postinfo->post, $postinfo->forumid)) { - mtrace("Error: mod/forum/cron.php: Could not mark post $postid read for user $userto->id". - " while sending digest email."); - } - } + // Mark post as read if forum_usermarksread is set off + forum_tp_mark_posts_read($userto, $userto->markposts); } } } @@ -791,7 +789,7 @@ function forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $u if (!isset($userto->viewfullnames[$forum->id])) { if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); $viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $userto->id); @@ -919,14 +917,13 @@ function forum_make_mail_html($course, $forum, $discussion, $post, $userfrom, $u * @return object A standard object with 2 variables: info (number of posts for this user) and time (last modified) */ function forum_user_outline($course, $user, $mod, $forum) { - - if ($posts = forum_get_user_posts($forum->id, $user->id)) { - $result = new object(); - $result->info = get_string("numposts", "forum", count($posts)); - - $lastpost = array_pop($posts); - $result->time = $lastpost->modified; - return $result; + if ($count = forum_count_user_posts($forum->id, $user->id)) { + if ($count->postcount > 0) { + $result = new object(); + $result->info = get_string("numposts", "forum", $count->postcount); + $result->time = $count->lastpost; + return $result; + } } return NULL; } @@ -941,11 +938,14 @@ function forum_user_complete($course, $user, $mod, $forum) { if ($posts = forum_get_user_posts($forum->id, $user->id)) { if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } - $discussions = get_records('forum_discussions', 'forum', $forum->id); // TODO: improve + $discussions = forum_get_user_involved_discussions($forum->id, $user->id); foreach ($posts as $post) { + if (!isset($discussions[$forum->discussion])) { + continue; + } $discussion = $discussions[$forum->discussion]; forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, false); } @@ -956,7 +956,7 @@ function forum_user_complete($course, $user, $mod, $forum) { } /** - * + * */ function forum_print_overview($courses,&$htmlarray) { global $USER, $CFG; @@ -1060,6 +1060,10 @@ function forum_print_overview($courses,&$htmlarray) { /** * Given a course and a date, prints a summary of all the new * messages posted in the course since that date + * @param object $course + * @param bool $viewfullnames capability + * @param int $timestart + * @return bool success */ function forum_print_recent_activity($course, $viewfullnames, $timestart) { global $CFG, $USER; @@ -1250,6 +1254,8 @@ function forum_get_user_grades($forum, $userid=0) { * * @param object $forum null means all forums * @param int $userid specific user only, 0 mean all + * @param boolean $nullifnone return null if grade does not exist + * @return void */ function forum_update_grades($forum=null, $userid=0, $nullifnone=true) { global $CFG; @@ -1339,6 +1345,8 @@ function forum_grade_item_delete($forum) { /** * Returns the users with data in one forum * (users with records in forum_subscriptions, forum_posts and forum_ratings, students) + * @param int $forumid + * @return mixed array or false if none */ function forum_get_participants($forumid) { @@ -1387,10 +1395,12 @@ function forum_get_participants($forumid) { } /** - * + * This function returns if a scale is being used by one forum + * @param int $forumid + * @param int $scaleid negative number + * @return bool */ function forum_scale_used ($forumid,$scaleid) { -//This function returns if a scale is being used by one forum $return = false; @@ -1423,21 +1433,24 @@ function forum_scale_used_anywhere($scaleid) { /** * Gets a post with all info ready for forum_print_post * Most of these joins are just to get the forum id + * @param int $postid + * @return mixed array of posts or false */ function forum_get_post_full($postid) { global $CFG; return get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture, u.imagealt - FROM {$CFG->prefix}forum_posts p - LEFT JOIN {$CFG->prefix}forum_discussions d ON p.discussion = d.id - LEFT JOIN {$CFG->prefix}user u ON p.userid = u.id - WHERE p.id = '$postid'"); + FROM {$CFG->prefix}forum_posts p + JOIN {$CFG->prefix}forum_discussions d ON p.discussion = d.id + LEFT JOIN {$CFG->prefix}user u ON p.userid = u.id + WHERE p.id = '$postid'"); } /** * Gets posts with all info ready for forum_print_post * We pass forumid in because we always know it so no need to make a * complicated join to find it out. + * @return mixed array of posts or false */ function forum_get_discussion_posts($discussion, $sort, $forumid) { global $CFG; @@ -1451,19 +1464,36 @@ function forum_get_discussion_posts($discussion, $sort, $forumid) { /** * Gets all posts in discussion including top parent. + * @param int $discussionid + * @param string $sort + * @param bool $tracking does user track the forum? + * @return array of posts */ -function forum_get_all_discussion_posts($discussionid, $sort) { - global $CFG; +function forum_get_all_discussion_posts($discussionid, $sort, $tracking=false) { + global $CFG, $USER; - if (!$posts = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.imagealt - FROM {$CFG->prefix}forum_posts p - LEFT JOIN {$CFG->prefix}user u ON p.userid = u.id - WHERE p.discussion = $discussionid - ORDER BY $sort")) { + if ($tracking) { + $now = time(); + $cutoffdate = $now - ($CFG->forum_oldpostdays * 24 * 3600); + $tr_sel = ", fr.id AS postread"; + $tr_join = "LEFT JOIN {$CFG->prefix}forum_read fr ON (fr.postid = p.id AND fr.userid = $USER->id)"; + } + + if (!$posts = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.imagealt $tr_sel + FROM {$CFG->prefix}forum_posts p + LEFT JOIN {$CFG->prefix}user u ON p.userid = u.id + $tr_join + WHERE p.discussion = $discussionid + ORDER BY $sort")) { return array(); } foreach ($posts as $pid=>$p) { + if ($tracking) { + if (forum_tp_is_post_old($p)) { + $posts[$pid]->postread = true; + } + } if (!$p->parent) { continue; } @@ -1508,7 +1538,7 @@ function forum_get_readable_forums($userid, $courseid=0) { global $CFG, $USER; if (!$forummod = get_record('modules', 'name', 'forum')) { - print_error('The forum module is not installed'); + error('The forum module is not installed'); } if ($courseid) { @@ -1760,6 +1790,11 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5 return get_records_sql($searchsql, $limitfrom, $limitnum); } +/** + * Returns a list of ratings for all posts in discussion + * @param object $discussion + * @return array of ratings or false + */ function forum_get_all_discussion_ratings($discussion) { global $CFG; return get_records_sql("SELECT r.id, r.userid, p.id AS postid, r.rating @@ -1771,6 +1806,9 @@ function forum_get_all_discussion_ratings($discussion) { /** * Returns a list of ratings for a particular post - sorted. + * @param int $postid + * @param string $sort + * @return array of ratings or false */ function forum_get_ratings($postid, $sort="u.firstname ASC") { global $CFG; @@ -1784,48 +1822,56 @@ function forum_get_ratings($postid, $sort="u.firstname ASC") { /** * Returns a list of all new posts that have not been mailed yet + * @param int $starttime - posts created after this time + * @param int $endtime - posts created before this + * @param int $now - used for timed discussions only */ -function forum_get_unmailed_posts($starttime, $endtime) { +function forum_get_unmailed_posts($starttime, $endtime, $now=null) { global $CFG; - $now = time(); - return get_records_sql("SELECT p.*, d.course - FROM {$CFG->prefix}forum_posts p, - {$CFG->prefix}forum_discussions d + + if (!empty($CFG->forum_enabletimedposts)) { + if (empty($now)) { + $now = time(); + } + $timedsql = "AND (d.timestart < $now AND (d.timeend = 0 OR d.timeend > $now))"; + } else { + $timedsql = ""; + } + + return get_records_sql("SELECT p.*, d.course, d.forum + FROM {$CFG->prefix}forum_posts p + JOIN {$CFG->prefix}forum_discussions d ON d.id = p.discussion WHERE p.mailed = 0 - AND (p.created >= '$starttime' OR d.timestart > 0) - AND (p.created < '$endtime' OR p.mailnow = 1) - AND p.discussion = d.id - AND ((d.timestart = 0 OR d.timestart <= '$now') - AND (d.timeend = 0 OR d.timeend > '$now')) + AND p.created >= $starttime + AND (p.created < $endtime OR p.mailnow = 1) + $timedsql ORDER BY p.modified ASC"); } /** * Marks posts before a certain time as being mailed already */ -function forum_mark_old_posts_as_mailed($endtime) { +function forum_mark_old_posts_as_mailed($endtime, $now=null) { global $CFG; -// Find out posts those are not showing immediately so we can exclude them - $now = time(); - $delayed_posts = get_records_sql("SELECT p.id, p.discussion - FROM {$CFG->prefix}forum_posts p, - {$CFG->prefix}forum_discussions d - WHERE p.mailed = 0 - AND p.discussion = d.id - AND d.timestart > '$now'"); - $delayed_ids = array(); - if ($delayed_posts) { - foreach ($delayed_posts as $post) { - $delayed_ids[] = $post->id; - } + if (empty($now)) { + $now = time(); + } + + if (empty($CFG->forum_enabletimedposts)) { + return execute_sql("UPDATE {$CFG->prefix}forum_posts + SET mailed = '1' + WHERE (created < $endtime OR mailnow = 1) + AND mailed = 0", false); + } else { - $delayed_ids[] = 0; + return execute_sql("UPDATE {$CFG->prefix}forum_posts + SET mailed = '1' + WHERE discussion NOT IN (SELECT d.id + FROM {$CFG->prefix}forum_discussions d + WHERE d.timestart > $now) + AND (created < $endtime OR mailnow = 1) + AND mailed = 0", false); } - return execute_sql("UPDATE {$CFG->prefix}forum_posts - SET mailed = '1' - WHERE id NOT IN (".implode(',',$delayed_ids).") - AND (created < '$endtime' OR mailnow = 1) - AND mailed ='0'", false); } /** @@ -1834,19 +1880,81 @@ function forum_mark_old_posts_as_mailed($endtime) { function forum_get_user_posts($forumid, $userid) { global $CFG; + $timedsql = ""; + if (!empty($CFG->forum_enabletimedposts)) { + $cm = get_coursemodule_from_instance('forum', $forumid); + if (!has_capability('mod/forum:viewhiddentimedposts' , get_context_instance(CONTEXT_MODULE, $cm->id))) { + $now = time(); + $timedsql = "AND (d.timestart < $now AND (d.timeend = 0 OR d.timeend > $now))"; + } + } + return get_records_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture, u.imagealt - FROM {$CFG->prefix}forum f, - {$CFG->prefix}forum_discussions d, - {$CFG->prefix}forum_posts p, - {$CFG->prefix}user u - WHERE f.id = '$forumid' - AND d.forum = f.id - AND p.discussion = d.id - AND p.userid = '$userid' - AND p.userid = u.id + FROM {$CFG->prefix}forum f + JOIN {$CFG->prefix}forum_discussions d ON d.forum = f.id + JOIN {$CFG->prefix}forum_posts p ON p.discussion = d.id + JOIN {$CFG->prefix}user u ON u.id = p.userid + WHERE f.id = $forumid + AND p.userid = $userid + $timedsql ORDER BY p.modified ASC"); } +/** + * Get all the discussions user participated in + * @param int $forumid + * @param int $userid + * @return array or false + */ +function forum_get_user_involved_discussions($forumid, $userid) { + global $CFG; + + $timedsql = ""; + if (!empty($CFG->forum_enabletimedposts)) { + $cm = get_coursemodule_from_instance('forum', $forumid); + if (!has_capability('mod/forum:viewhiddentimedposts' , get_context_instance(CONTEXT_MODULE, $cm->id))) { + $now = time(); + $timedsql = "AND (d.timestart < $now AND (d.timeend = 0 OR d.timeend > $now))"; + } + } + + return get_records_sql("SELECT DISTINCT d.* + FROM {$CFG->prefix}forum f + JOIN {$CFG->prefix}forum_discussions d ON d.forum = f.id + JOIN {$CFG->prefix}forum_posts p ON p.discussion = d.id + WHERE f.id = $forumid + AND p.userid = $userid + $timedsql"); +} + +/** + * Get all the posts for a user in a forum suitable for forum_print_post + * @param int $forumid + * @param int $userid + * @return array of counts or false + */ +function forum_count_user_posts($forumid, $userid) { + global $CFG; + + $timedsql = ""; + if (!empty($CFG->forum_enabletimedposts)) { + $cm = get_coursemodule_from_instance('forum', $forumid); + if (!has_capability('mod/forum:viewhiddentimedposts' , get_context_instance(CONTEXT_MODULE, $cm->id))) { + $now = time(); + $timedsql = "AND (d.timestart < $now AND (d.timeend = 0 OR d.timeend > $now))"; + } + } + + return get_records_sql("SELECT COUNT(p.id) AS postcount, MAX(p.modified) AS lastpost + FROM {$CFG->prefix}forum f + JOIN {$CFG->prefix}forum_discussions d ON d.forum = f.id + JOIN {$CFG->prefix}forum_posts p ON p.discussion = d.id + JOIN {$CFG->prefix}user u ON u.id = p.userid + WHERE f.id = $forumid + AND p.userid = $userid + $timedsql"); +} + /** * Given a log entry, return the forum post details for it. */ @@ -1899,28 +2007,125 @@ function forum_get_firstpost_from_discussion($discussionid) { } /** - * Returns an array of counts of replies to each discussion (optionally in one forum or course and/or user) + * Returns an array of counts of replies to each discussion */ -function forum_count_discussion_replies($forum='0', $course='0', $user='0') { +function forum_count_discussion_replies($forumid, $forumsort="", $limit=-1, $page=-1, $perpage=0) { global $CFG; - $forumselect = $courseselect = $userselect = ''; + if ($limit > 0) { + $limitfrom = 0; + $limitnum = $limit; + } else if ($page != -1) { + $limitfrom = $page*$perpage; + $limitnum = $perpage; + } else { + $limitfrom = 0; + $limitnum = 0; + } + + if ($forumsort == "") { + $orderby = ""; + $groupby = ""; - if ($forum) { - $forumselect = " AND d.forum = '$forum'"; + } else { + $orderby = "ORDER BY $forumsort"; + $groupby = ", ".strtolower($forumsort); + $groupby = str_replace('desc', '', $groupby); + $groupby = str_replace('asc', '', $groupby); + } + + $sql = "SELECT p.discussion, COUNT(p.id) AS replies, MAX(p.id) AS lastpostid + FROM {$CFG->prefix}forum_posts p + JOIN {$CFG->prefix}forum_discussions d ON p.discussion = d.id + WHERE p.parent > 0 AND d.forum = $forumid + GROUP BY p.discussion $groupby + $orderby"; + + if ($limitfrom == 0 and $limitnum == 0) { + return get_records_sql($sql); + } else { + return get_records_sql("SELECT * FROM ($sql) sq", $limitfrom, $limitnum); + } +} + +function forum_count_discussions($forum, $cm, $course) { + global $CFG, $USER; + + static $cache = array(); + + $now = round(time(), -2); // db cache friendliness + + if (!isset($cache[$course->id])) { + if (!empty($CFG->forum_enabletimedposts)) { + $timedsql = "AND d.timestart < $now AND (d.timeend = 0 OR d.timeend > $now)"; + } else { + $timedsql = ""; + } + + $sql = "SELECT f.id, COUNT(d.id) as dcount + FROM {$CFG->prefix}forum f + JOIN {$CFG->prefix}forum_discussions d ON d.forum = f.id + WHERE f.course = $course->id + $timedsql + GROUP BY f.id"; + + if ($counts = get_records_sql($sql)) { + foreach ($counts as $count) { + $counts[$count->id] = $count->dcount; + } + $cache[$course->id] = $counts; + } else { + $cache[$course->id] = array(); + } } - if ($course) { - $courseselect = " AND d.course = '$course'"; + + if (empty($cache[$course->id][$forum->id])) { + return 0; } - if ($user) { - $userselect = " AND d.userid = '$user'"; + + $groupmode = groups_get_activity_groupmode($cm, $course); + + if ($groupmode != SEPARATEGROUPS) { + return $cache[$course->id][$forum->id]; } - return get_records_sql("SELECT p.discussion, (count(*)) as replies, max(p.id) as lastpostid - FROM {$CFG->prefix}forum_posts p, - {$CFG->prefix}forum_discussions d - WHERE p.parent > 0 $forumselect $courseselect $userselect - AND p.discussion = d.id - GROUP BY p.discussion"); + + if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm))) { + return $cache[$course->id][$forum->id]; + } + + require_once($CFG->dirroot.'/course/lib.php'); + + $modinfo =& get_fast_modinfo($course); + if (is_null($modinfo->groups)) { + $modinfo->groups = groups_get_user_groups($course->id, $USER->id); + } + + if (empty($CFG->enablegroupings)) { + $mygroups = $modinfo->groups[0]; + } else { + $mygroups = $modinfo->groups[$cm->groupingid]; + } + + // add all groups posts + if (empty($mygroups)) { + $mygroups = array(-1=>-1); + } else { + $mygroups[-1] = -1; + } + $mygroups = implode(',', $mygroups); + + if (!empty($CFG->forum_enabletimedposts)) { + $timedsql = "AND d.timestart < $now AND (d.timeend = 0 OR d.timeend > $now)"; + } else { + $timedsql = ""; + } + + $sql = "SELECT COUNT(d.id) + FROM {$CFG->prefix}forum_discussions d + WHERE d.forum = $forum->id AND d.groupid IN ($mygroups) + $timedsql"; + + return get_field_sql($sql); } /** @@ -1957,37 +2162,41 @@ function forum_count_unrated_posts($discussionid, $userid) { /** * Get all discussions in a forum */ -function forum_get_discussions($cm, $forumsort="d.timemodified DESC", $fullpost=true, $currentgroup=-1, $limit=0, $userlastmodified=false) { +function forum_get_discussions($cm, $forumsort="d.timemodified DESC", $fullpost=true, $unused=-1, $limit=-1, $userlastmodified=false, $page=-1, $perpage=0) { global $CFG, $USER; $timelimit = ''; $modcontext = null; + $now = round(time(), -2); + if (!empty($CFG->forum_enabletimedposts)) { $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); if (!has_capability('mod/forum:viewhiddentimedposts', $modcontext)) { - $now = time(); - $timelimit = " AND ((d.timestart = 0 OR d.timestart <= '$now') AND (d.timeend = 0 OR d.timeend > '$now')"; - if (!empty($USER->id)) { - $timelimit .= " OR d.userid = '$USER->id'"; + $timelimit = " AND ((d.timestart = < $now) AND (d.timeend = 0 OR d.timeend > $now)"; + if (isloggedin()) { + $timelimit .= " OR d.userid = $USER->id"; } - $timelimit .= ')'; + $timelimit .= ")"; } } - $limitfrom = 0; - $limitnum = 0; - if ($limit) { - $limitnum = $limit; + if ($limit > 0) { + $limitfrom = 0; + $limitnum = $limit; + } else if ($page != -1) { + $limitfrom = $page*$perpage; + $limitnum = $perpage; + } else { + $limitfrom = 0; + $limitnum = 0; } - $groupmode = groups_get_activity_groupmode($cm); - if ($currentgroup == -1) { - $currentgroup = groups_get_activity_group($cm); - } + $groupmode = groups_get_activity_groupmode($cm); + $currentgroup = groups_get_activity_group($cm); if ($groupmode) { if (empty($modcontext)) { @@ -1996,7 +2205,7 @@ function forum_get_discussions($cm, $forumsort="d.timemodified DESC", $fullpost= if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) { if ($currentgroup) { - $groupselect = "AND (d.groupid = '$currentgroup' OR d.groupid = -1)"; + $groupselect = "AND (d.groupid = $currentgroup OR d.groupid = -1)"; } else { $groupselect = ""; } @@ -2004,7 +2213,7 @@ function forum_get_discussions($cm, $forumsort="d.timemodified DESC", $fullpost= } else { //seprate groups without access all if ($currentgroup) { - $groupselect = "AND (d.groupid = '$currentgroup' OR d.groupid = -1)"; + $groupselect = "AND (d.groupid = $currentgroup OR d.groupid = -1)"; } else { $groupselect = "AND d.groupid = -1"; } @@ -2028,20 +2237,141 @@ function forum_get_discussions($cm, $forumsort="d.timemodified DESC", $fullpost= $umtable = ""; } else { $umfields = ", um.firstname AS umfirstname, um.lastname AS umlastname"; - $umtable = " LEFT JOIN {$CFG->prefix}user um on (d.usermodified = um.id)"; + $umtable = " LEFT JOIN {$CFG->prefix}user um ON (d.usermodified = um.id)"; } - return get_records_sql("SELECT $postdata, d.name, d.timemodified, d.usermodified, d.groupid, d.timestart, d.timeend, - u.firstname, u.lastname, u.email, u.picture, u.imagealt $umfields - FROM {$CFG->prefix}forum_discussions d - JOIN {$CFG->prefix}forum_posts p ON p.discussion = d.id - JOIN {$CFG->prefix}user u ON p.userid = u.id - $umtable - WHERE d.forum = {$cm->instance} AND p.parent = 0 - $timelimit $groupselect - ORDER BY $forumsort", $limitfrom, $limitnum); + $sql = "SELECT $postdata, d.name, d.timemodified, d.usermodified, d.groupid, d.timestart, d.timeend, + u.firstname, u.lastname, u.email, u.picture, u.imagealt $umfields + FROM {$CFG->prefix}forum_discussions d + JOIN {$CFG->prefix}forum_posts p ON p.discussion = d.id + JOIN {$CFG->prefix}user u ON p.userid = u.id + $umtable + WHERE d.forum = {$cm->instance} AND p.parent = 0 + $timelimit $groupselect + ORDER BY $forumsort"; + return get_records_sql($sql, $limitfrom, $limitnum); } +function forum_get_discussions_unread($cm, $forumsort="d.timemodified DESC") { + global $CFG, $USER; + + $now = round(time(), -2); + + $groupmode = groups_get_activity_groupmode($cm); + $currentgroup = groups_get_activity_group($cm); + + if ($groupmode) { + $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + + if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) { + if ($currentgroup) { + $groupselect = "AND (d.groupid = $currentgroup OR d.groupid = -1)"; + } else { + $groupselect = ""; + } + + } else { + //seprate groups without access all + if ($currentgroup) { + $groupselect = "AND (d.groupid = $currentgroup OR d.groupid = -1)"; + } else { + $groupselect = "AND d.groupid = -1"; + } + } + } else { + $groupselect = ""; + } + + if (empty($forumsort)) { + $forumsort = "d.timemodified DESC"; + } + $groupby = strtolower($forumsort); + $groupby = str_replace('desc', '', $groupby); + $groupby = str_replace('asc', '', $groupby); + + $cutoffdate = $now - ($CFG->forum_oldpostdays*24*60*60); + + if (!empty($CFG->forum_enabletimedposts)) { + $timedsql = "AND d.timestart < $now AND (d.timeend = 0 OR d.timeend > $now)"; + } else { + $timedsql = ""; + } + + $sql = "SELECT d.id, COUNT(p.id) AS unread + FROM {$CFG->prefix}forum_discussions d + JOIN {$CFG->prefix}forum_posts p ON p.discussion = d.id + LEFT JOIN {$CFG->prefix}forum_read r ON (r.postid = p.id AND r.userid = $USER->id) + WHERE d.forum = {$cm->instance} + AND p.modified >= $cutoffdate AND r.id is NULL + $timedsql + $groupselect + GROUP BY d.id, $groupby + ORDER BY $forumsort"; + if ($unreads = get_records_sql($sql)) { + foreach ($unreads as $unread) { + $unreads[$unread->id] = $unread->unread; + } + return $unreads; + } else { + return array(); + } +} + +function forum_get_discussions_count($cm) { + global $CFG, $USER; + + $now = round(time(), -2); + + $groupmode = groups_get_activity_groupmode($cm); + $currentgroup = groups_get_activity_group($cm); + + if ($groupmode) { + $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + + if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) { + if ($currentgroup) { + $groupselect = "AND (d.groupid = $currentgroup OR d.groupid = -1)"; + } else { + $groupselect = ""; + } + + } else { + //seprate groups without access all + if ($currentgroup) { + $groupselect = "AND (d.groupid = $currentgroup OR d.groupid = -1)"; + } else { + $groupselect = "AND d.groupid = -1"; + } + } + } else { + $groupselect = ""; + } + + $cutoffdate = $now - ($CFG->forum_oldpostdays*24*60*60); + + $timelimit = ""; + + if (!empty($CFG->forum_enabletimedposts)) { + + $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + + if (!has_capability('mod/forum:viewhiddentimedposts', $modcontext)) { + $timelimit = " AND ((d.timestart = < $now) AND (d.timeend = 0 OR d.timeend > $now)"; + if (isloggedin()) { + $timelimit .= " OR d.userid = $USER->id"; + } + $timelimit .= ")"; + } + } + + $sql = "SELECT COUNT(d.id) + FROM {$CFG->prefix}forum_discussions d + JOIN {$CFG->prefix}forum_posts p ON p.discussion = d.id + WHERE d.forum = {$cm->instance} AND p.parent = 0 + $timelimit $groupselect"; + + return get_field_sql($sql); +} /** @@ -2206,7 +2536,7 @@ function forum_make_mail_post($course, $forum, $discussion, $post, $userfrom, $u if (!isset($userto->viewfullnames[$forum->id])) { if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); $viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $userto->id); @@ -2246,7 +2576,7 @@ function forum_make_mail_post($course, $forum, $discussion, $post, $userfrom, $u $groups = $userfrom->groups[$forum->id]; } else { if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } $group = groups_get_all_groups($course->id, $userfrom->id, $cm->groupingid); } @@ -2321,13 +2651,13 @@ function forum_make_mail_post($course, $forum, $discussion, $post, $userfrom, $u * If false, don't output anything at all. */ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=false, $reply=false, $link=false, - $ratings=NULL, $footer="", $highlight="", $post_read=-99, $dummyifcantsee=true) { + $ratings=NULL, $footer="", $highlight="", $post_read=null, $dummyifcantsee=true, $istracked=null) { global $USER, $CFG; static $stredit, $strdelete, $strreply, $strparent, $strprune; static $strpruneheading, $displaymode; - static $strmarkread, $strmarkunread, $istracked; + static $strmarkread, $strmarkunread; $post->course = $course->id; $post->forum = $forum->id; @@ -2394,28 +2724,22 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa $strmarkread = get_string('markread', 'forum'); $strmarkunread = get_string('markunread', 'forum'); - if (!empty($post->forum)) { - $istracked = (forum_tp_can_track_forums($forum) && - forum_tp_is_tracked($forum)); - } else { - $istracked = false; - } } + $read_style = ''; + // ignore trackign status if not tracked or tracked param missing if ($istracked) { - if ($post_read == -99) { // If we don't know yet... - // The front page can display a news item post to non-logged in users. This should - // always appear as 'read'. - $post_read = empty($USER) || forum_tp_is_post_read($USER->id, $post); + if (is_null($post_read)) { + debugging('fetching post_read info'); + $post_read = forum_tp_is_post_read($USER->id, $post); } + if ($post_read) { $read_style = ' read'; } else { $read_style = ' unread'; echo ''; } - } else { - $read_style = ''; } echo ''; @@ -2650,7 +2974,7 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa } echo ''."\n\n"; - if ($istracked && !$CFG->forum_usermarksread) { + if ($istracked && !$CFG->forum_usermarksread && !$post_read) { forum_tp_mark_post_read($USER->id, $post, $forum->id); } @@ -2682,7 +3006,7 @@ function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring="" if (empty($modcontext)) { if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); } @@ -3150,7 +3474,7 @@ function forum_print_mode_form($id, $mode, $forumtype='') { } /** - * + * */ function forum_search_form($course, $search='') { global $CFG; @@ -3171,7 +3495,7 @@ function forum_search_form($course, $search='') { /** - * + * */ function forum_set_return() { global $CFG, $SESSION; @@ -3191,7 +3515,7 @@ function forum_set_return() { /** - * + * */ function forum_go_back_to($default) { global $SESSION; @@ -3220,24 +3544,24 @@ function forum_file_area_name($post) { return false; } $forumid = $forum->id; - $courseid = $forum->course; + $courseid = $forum->course; } else { - $forumid = $post->forum; - $courseid = $post->course; + $forumid = $post->forum; + $courseid = $post->course; } return "$courseid/$CFG->moddata/forum/$forumid/$post->id"; } /** - * + * */ function forum_file_area($post) { return make_upload_directory( forum_file_area_name($post) ); } /** - * + * */ function forum_delete_old_attachments($post, $exception="") { @@ -3360,7 +3684,7 @@ function forum_print_attachments($post, $return=NULL) { */ /** - * + * */ function forum_add_attachment($post, $inputname,&$message) { @@ -3386,7 +3710,7 @@ function forum_add_attachment($post, $inputname,&$message) { } /** - * + * */ function forum_add_new_post($post,&$message) { @@ -3414,7 +3738,7 @@ function forum_add_new_post($post,&$message) { set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion); set_field("forum_discussions", "usermodified", $post->userid, "id", $post->discussion); - if (forum_tp_can_track_forums($post->forum) && forum_tp_is_tracked($post->forum)) { + if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) { forum_tp_mark_post_read($post->userid, $post, $post->forum); } @@ -3422,12 +3746,14 @@ function forum_add_new_post($post,&$message) { } /** - * + * */ function forum_update_post($post,&$message) { global $USER, $CFG; + $forum = get_record('forum', 'id', $post->forum); + $post->modified = time(); $updatediscussion = new object(); @@ -3451,7 +3777,7 @@ function forum_update_post($post,&$message) { unset($post->attachment); } - if (forum_tp_can_track_forums($post->forum) && forum_tp_is_tracked($post->forum)) { + if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) { forum_tp_mark_post_read($post->userid, $post, $post->forum); } @@ -3516,7 +3842,7 @@ function forum_add_discussion($discussion,&$message) { return 0; } - if (forum_tp_can_track_forums($post->forum) && forum_tp_is_tracked($post->forum)) { + if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) { forum_tp_mark_post_read($post->userid, $post, $post->forum); } @@ -3525,7 +3851,7 @@ function forum_add_discussion($discussion,&$message) { /** - * + * */ function forum_delete_discussion($discussion, $fulldelete=false) { // $discussion is a discussion record object @@ -3556,7 +3882,7 @@ function forum_delete_discussion($discussion, $fulldelete=false) { /** - * + * */ function forum_delete_post($post, $children=false) { if ($childposts = get_records('forum_posts', 'parent', $post->id)) { @@ -3589,7 +3915,7 @@ function forum_delete_post($post, $children=false) { } /** - * + * */ function forum_count_replies($post, $children=true) { $count = 0; @@ -3610,14 +3936,14 @@ function forum_count_replies($post, $children=true) { /** - * + * */ function forum_forcesubscribe($forumid, $value=1) { return set_field("forum", "forcesubscribe", $value, "id", $forumid); } /** - * + * */ function forum_is_forcesubscribed($forum) { if (isset($forum->forcesubscribe)) { // then we use that @@ -3628,13 +3954,33 @@ function forum_is_forcesubscribed($forum) { } /** - * + * */ -function forum_is_subscribed($userid, $forumid) { - if (forum_is_forcesubscribed($forumid)) { +function forum_is_subscribed($userid, $forum) { + if (is_numeric($forum)) { + $forum = get_record('forum', 'id', $forum); + } + if (forum_is_forcesubscribed($forum)) { return true; } - return record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid); + return record_exists("forum_subscriptions", "userid", $userid, "forum", $forum->id); +} + +function forum_get_subscribed_forums($course) { + global $USER, $CFG; + $sql = "SELECT f.id + FROM {$CFG->prefix}forum f + LEFT JOIN {$CFG->prefix}forum_subscriptions fs ON (fs.forum = f.id AND fs.userid = $USER->id) + WHERE f.forcesubscribe <> ".FORUM_DISALLOWSUBSCRIBE." + AND (f.forcesubscribe = ".FORUM_FORCESUBSCRIBE." OR fs.id IS NOT NULL)"; + if ($subscribed = get_records_sql($sql)) { + foreach ($subscribed as $s) { + $subscribed[$s->id] = $s->id; + } + return $subscribed; + } else { + return array(); + } } /** @@ -3701,7 +4047,7 @@ function forum_post_subscription($post) { * at the top of the function. * @param */ -function forum_get_subscribe_link($forum, $context, $messages = array(), $cantaccessagroup = false, $fakelink=true, $backtoindex=false) { +function forum_get_subscribe_link($forum, $context, $messages = array(), $cantaccessagroup = false, $fakelink=true, $backtoindex=false, $subscribed_forums=null) { global $CFG, $USER; $defaultmessages = array( 'subscribed' => get_string('unsubscribe', 'forum'), @@ -3712,14 +4058,19 @@ function forum_get_subscribe_link($forum, $context, $messages = array(), $cantac ); $messages = $messages + $defaultmessages; - if (forum_is_forcesubscribed($forum->id)) { + if (forum_is_forcesubscribed($forum)) { return $messages['forcesubscribed']; } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE && !has_capability('mod/forum:managesubscriptions', $context)) { return $messages['cantsubscribe']; } else if ($cantaccessagroup) { return $messages['cantaccessgroup']; } else { - if (forum_is_subscribed($USER->id, $forum->id)) { + if (is_null($subscribed_forums)) { + $subscribed = forum_is_subscribed($USER->id, $forum); + } else { + $subscribed = !empty($subscribed_forums[$forum->id]); + } + if ($subscribed) { $linktext = $messages['subscribed']; $linktitle = get_string('subscribestop', 'forum'); } else { @@ -3727,8 +4078,15 @@ function forum_get_subscribe_link($forum, $context, $messages = array(), $cantac $linktitle = get_string('subscribestart', 'forum'); } - $backtoindexlink=($backtoindex ? '&backtoindex=1' : ''); + $options = array(); + if ($backtoindex) { + $backtoindexlink = '&backtoindex=1'; + $options['backtoindex'] = 1; + } else { + $backtoindexlink = ''; + } $link = ''; + if ($fakelink) { $link .= '