From d50704bf19dfdcf063fb2a613f9c808702872d76 Mon Sep 17 00:00:00 2001 From: vyshane Date: Mon, 25 Sep 2006 08:25:58 +0000 Subject: [PATCH] Fix for MDL-6212. Forum searches now respect the qanda forums. In these forums, the user should not be able to see posts from discussions where the user has not yet posted, unless a post is the first post in the discussion (the question), or unless the user has the capabilitiy mod/forum:viewqandawithoutposting. --- mod/forum/lib.php | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 7cac0ccb3f..dca3531a3a 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -1110,7 +1110,7 @@ function forum_get_child_posts($parent, $forumid) { */ function forum_get_readable_forums($userid, $courseid=0) { - global $CFG; + global $CFG, $USER; if (!$forummod = get_record('modules', 'name', 'forum')) { error('The forum module is not installed'); @@ -1172,12 +1172,31 @@ function forum_get_readable_forums($userid, $courseid=0) { } else { $forum->accessallgroups = true; } - + if (has_capability('mod/forum:viewdiscussion', $forumcontext)) { - + $forum->viewhiddentimedposts = has_capability('mod/forum:viewhiddentimedposts', $forumcontext); + if ($forum->type == 'qanda' + && !has_capability('mod/forum:viewqandawithoutposting', $forumcontext)) { + + // We need to check whether the user has posted in the qanda forum. + $haspostedsql = "SELECT DISTINCT(d.id) AS id, + d.name + FROM {$CFG->prefix}forum_posts AS p, + {$CFG->prefix}forum_discussions AS d + WHERE p.discussion = d.id + AND d.forum = {$forum->id} + AND p.userid = {$USER->id}"; + + $discussionspostedin = get_records_sql($haspostedsql); + $forum->onlydiscussions = array(); // Holds discussion ids for the discussions + // the user is allowed to see in this forum. + foreach ($discussionspostedin as $d) { + array_push($forum->onlydiscussions, $d->id); + } + } array_push($readableforums, $forum); } } @@ -1223,6 +1242,18 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5 OR ((d.timestart = 0 OR d.timestart <= $now) AND (d.timeend = 0 OR d.timeend > $now)) )"; } + if (isset($forums[$i]->onlydiscussions)) { + // This is a qanda forum. + if (is_array($forums[$i]->onlydiscussions)) { + // Show question posts as well as posts from discussions in + // which the user has posted a reply. + $onlydiscussions = implode(' OR d.id = ', $forums[$i]->onlydiscussions); + $selectdiscussion .= " AND ((d.id = $onlydiscussions) OR p.parent = 0)"; + } else { + // Show only the question posts. + $selectdiscussion .= ' AND (p.parent = 0)'; + } + } if (!$forums[$i]->accessallgroups) { if (!empty($forums[$i]->accessgroup)) { $selectdiscussion .= " AND (d.groupid = {$forums[$i]->accessgroup}"; @@ -1300,7 +1331,7 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5 ORDER BY p.modified DESC"; $totalcount = count_records_sql($countsql); - + return get_records_sql($searchsql, $limitfrom, $limitnum); } @@ -2830,7 +2861,7 @@ function forum_post_subscription($post) { function forum_user_has_posted_discussion($forumid, $userid) { - if ($discussions = forum_get_discussions($forumid, "", $userid)) { + if ($discussions = forum_get_discussions($forumid, '', $userid)) { return true; } else { return false; -- 2.39.5