From: skodak Date: Fri, 11 Jan 2008 13:09:51 +0000 (+0000) Subject: MDL-12926 Discussion list shows all discussion in separate mode if user not member... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=fffa8b358ff50f5033ece2bae879c90325267fd8;p=moodle.git MDL-12926 Discussion list shows all discussion in separate mode if user not member of any group and does not have accessallgroups; merged from MOODLE_19_STABLE --- diff --git a/blocks/news_items/block_news_items.php b/blocks/news_items/block_news_items.php index eefb3592a9..b93b350bb0 100644 --- a/blocks/news_items/block_news_items.php +++ b/blocks/news_items/block_news_items.php @@ -50,7 +50,7 @@ class block_news_items extends block_base { /// Get all the recent discussions we're allowed to see - if (! $discussions = forum_get_discussions($forum->id, 'p.modified DESC', 0, false, + if (! $discussions = forum_get_discussions($cm, 'p.modified DESC', false, $currentgroup, $COURSE->newsitems) ) { $text .= '('.get_string('nonews', 'forum').')'; $this->content->text = $text; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index c0efa0aae5..ef155954b3 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -1780,15 +1780,12 @@ function forum_count_unrated_posts($discussionid, $userid) { /** * Get all discussions in a forum */ -function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC", - $user=0, $fullpost=true, $currentgroup=-1, $limit=0, $userlastmodified=false) { +function forum_get_discussions($cm, $forumsort="d.timemodified DESC", $fullpost=true, $currentgroup=-1, $limit=0, $userlastmodified=false) { global $CFG, $USER; $timelimit = ''; - if (!$cm = get_coursemodule_from_instance('forum', $forum)) { - error('Course Module ID was incorrect'); - } + $modcontext = null; if (!empty($CFG->forum_enabletimedposts)) { @@ -1804,28 +1801,42 @@ function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC", } } - if ($user) { - $userselect = " AND u.id = '$user' "; - } else { - $userselect = ""; - } - $limitfrom = 0; $limitnum = 0; if ($limit) { $limitnum = $limit; } + $groupmode = groups_get_activity_groupmode($cm); if ($currentgroup == -1) { - $currentgroup = get_current_group($cm->course); + $currentgroup = groups_get_activity_group($cm); } - if ($currentgroup) { - $groupselect = " AND (d.groupid = '$currentgroup' OR d.groupid = -1) "; + if ($groupmode) { + if (empty($modcontext)) { + $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"; } @@ -1836,38 +1847,22 @@ function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC", } if (empty($userlastmodified)) { // We don't need to know this - $umfields = ''; - $umtable = ''; + $umfields = ""; + $umtable = ""; } else { - $umfields = ', um.firstname AS umfirstname, um.lastname AS umlastname'; - $umtable = ' LEFT JOIN '.$CFG->prefix.'user um on (d.usermodified = um.id)'; + $umfields = ", um.firstname AS umfirstname, um.lastname AS umlastname"; + $umtable = " LEFT JOIN {$CFG->prefix}user um on (d.usermodified = um.id)"; } - //TODO: there must be a nice way to do this that keeps both postgres and mysql 3.2x happy but I can't find it right now. - if ($CFG->dbfamily == 'postgres' || $CFG->dbfamily == 'mssql' || $CFG->dbfamily == 'oracle') { - return get_records_sql("SELECT $postdata, d.name, d.timemodified, d.usermodified, d.groupid, + return get_records_sql("SELECT $postdata, d.name, d.timemodified, d.usermodified, d.groupid, 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 + 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 = '$forum' - AND p.parent = 0 - $timelimit $groupselect $userselect + WHERE d.forum = {$cm->instance} AND p.parent = 0 + $timelimit $groupselect ORDER BY $forumsort", $limitfrom, $limitnum); - } else { // MySQL query. TODO: Check if this is needed (MySQL 4.1 should work with the above query) - return get_records_sql("SELECT $postdata, d.name, d.timemodified, d.usermodified, d.groupid, - u.firstname, u.lastname, u.email, u.picture, u.imagealt $umfields - FROM ({$CFG->prefix}forum_posts p, - {$CFG->prefix}user u, - {$CFG->prefix}forum_discussions d) - $umtable - WHERE d.forum = '$forum' - AND p.discussion = d.id - AND p.parent = 0 - AND p.userid = u.id $timelimit $groupselect $userselect - ORDER BY $forumsort", $limitfrom, $limitnum); - } } @@ -3388,14 +3383,19 @@ function forum_get_tracking_link($forum, $messages=array(), $fakelink=true) { /** - * + * Returns true if user created new discussion already + * @param int $forumid + * @param int $userid + * @return bool */ function forum_user_has_posted_discussion($forumid, $userid) { - if ($discussions = forum_get_discussions($forumid, '', $userid)) { - return true; - } else { - return false; - } + global $CFG; + + $sql = "SELECT 'x' + FROM {$CFG->prefix}forum_discussions d, {$CFG->prefix}forum_posts p + WHERE d.forum = $forumid AND p.discussion = d.id AND p.parent = 0"; + + return record_exists_sql($sql); } /** @@ -3708,7 +3708,7 @@ function forum_print_latest_discussions($course, $forum, $maxdiscussions=5, $dis $getuserlastmodified = ($displayformat == 'header'); - if (! $discussions = forum_get_discussions($forum->id, $sort, 0, $fullpost, $currentgroup,0,$getuserlastmodified) ) { + if (! $discussions = forum_get_discussions($cm, $sort, $fullpost, $currentgroup, 0, $getuserlastmodified) ) { echo '
'; if ($forum->type == 'news') { echo '('.get_string('nonews', 'forum').')';