Skip to content
Browse files

MDL-28615 forum: Revised enrol_sharing_course to use the new enrol_ge…

…t_shared_courses and fixed permission checking in forum_user_can_see_post
  • Loading branch information...
1 parent dbde930 commit b38867187fe8c16d83085f8c4041ef943bdff0e3 Sam Hemelryk committed Oct 3, 2011
Showing with 25 additions and 46 deletions.
  1. +22 −35 lib/enrollib.php
  2. +3 −11 mod/forum/lib.php
View
57 lib/enrollib.php
@@ -226,39 +226,15 @@ function enrol_check_plugins($user) {
* The courses has to be visible and enrolments has to be active,
* timestart and timeend restrictions are ignored.
*
+ * This function calls {@see enrol_get_shared_courses()} setting checkexistsonly
+ * to true.
+ *
* @param stdClass|int $user1
* @param stdClass|int $user2
* @return bool
*/
function enrol_sharing_course($user1, $user2) {
- global $DB, $CFG;
-
- $user1 = !empty($user1->id) ? $user1->id : $user1;
- $user2 = !empty($user2->id) ? $user2->id : $user2;
-
- if (empty($user1) or empty($user2)) {
- return false;
- }
-
- if (!$plugins = explode(',', $CFG->enrol_plugins_enabled)) {
- return false;
- }
-
- list($plugins, $params) = $DB->get_in_or_equal($plugins, SQL_PARAMS_NAMED, 'ee');
- $params['enabled'] = ENROL_INSTANCE_ENABLED;
- $params['active1'] = ENROL_USER_ACTIVE;
- $params['active2'] = ENROL_USER_ACTIVE;
- $params['user1'] = $user1;
- $params['user2'] = $user2;
-
- $sql = "SELECT DISTINCT 'x'
- FROM {enrol} e
- JOIN {user_enrolments} ue1 ON (ue1.enrolid = e.id AND ue1.status = :active1 AND ue1.userid = :user1)
- JOIN {user_enrolments} ue2 ON (ue2.enrolid = e.id AND ue2.status = :active2 AND ue2.userid = :user2)
- JOIN {course} c ON (c.id = e.courseid AND c.visible = 1)
- WHERE e.status = :enabled AND e.enrol $plugins";
-
- return $DB->record_exists_sql($sql, $params);
+ return enrol_get_shared_courses($user1, $user2, false, true);
}
/**
@@ -267,11 +243,18 @@ function enrol_sharing_course($user1, $user2) {
* The courses has to be visible and enrolments has to be active,
* timestart and timeend restrictions are ignored.
*
+ * @global moodle_database $DB
* @param stdClass|int $user1
* @param stdClass|int $user2
- * @return array An array of courses that both users are enrolled in
+ * @param bool $preloadcontexts If set to true contexts for the returned courses
+ * will be preloaded.
+ * @param bool $checkexistsonly If set to true then this function will return true
+ * if the users share any courses and false if not.
+ * @return array|bool An array of courses that both users are enrolled in OR if
+ * $checkexistsonly set returns true if the users share any courses
+ * and false if not.
*/
-function enrol_get_shared_courses($user1, $user2, $preloadcontexts = false) {
+function enrol_get_shared_courses($user1, $user2, $preloadcontexts = false, $checkexistsonly = false) {
global $DB, $CFG;
$user1 = !empty($user1->id) ? $user1->id : $user1;
@@ -309,12 +292,16 @@ function enrol_get_shared_courses($user1, $user2, $preloadcontexts = false) {
WHERE e.status = :enabled AND e.enrol $plugins
) ec ON ec.id = c.id
$ctxjoin";
- $courses = $DB->get_records_sql($sql, $params);
- if ($preloadcontexts) {
- array_map('context_instance_preload', $courses);
- }
- return $courses;
+ if ($checkexistsonly) {
+ return $DB->record_exists_sql($sql, $params);
+ } else {
+ $courses = $DB->get_records_sql($sql, $params);
+ if ($preloadcontexts) {
+ array_map('context_instance_preload', $courses);
+ }
+ return $courses;
+ }
}
/**
View
14 mod/forum/lib.php
@@ -5069,17 +5069,9 @@ function forum_user_can_see_post($forum, $discussion, $post, $user=NULL, $cm=NUL
$user = $USER;
}
- if (!has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'), get_context_instance(CONTEXT_USER, $post->userid))) {
- if (isset($cm->cache->caps['mod/forum:viewdiscussion'])) {
- if (!$cm->cache->caps['mod/forum:viewdiscussion']) {
- return false;
- }
- } else {
- $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
- if (!has_capability('mod/forum:viewdiscussion', $modcontext, $user->id)) {
- return false;
- }
- }
+ $canviewdiscussion = !empty($cm->cache->caps['mod/forum:viewdiscussion']) || has_capability('mod/forum:viewdiscussion', $modcontext, $user->id);
+ if (!$canviewdiscussion && !has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'), get_context_instance(CONTEXT_USER, $post->userid))) {
+ return false;
}
if (isset($cm->uservisible)) {

0 comments on commit b388671

Please sign in to comment.
Something went wrong with that request. Please try again.