commit 4189ad3018c5942423e2727e1641d110d8e3cf08 Author: Jay Knight Date: Thu Aug 19 13:22:45 2010 -0500 MDL-23548 Don't list courses that you can't do anything with diff --git a/admin/settings/appearance.php b/admin/settings/appearance.php index 5ff571d..2d7c25d 100644 --- a/admin/settings/appearance.php +++ b/admin/settings/appearance.php @@ -86,6 +86,7 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page HOMEPAGE_USER => get_string('userpreference', 'admin') ); $temp->add(new admin_setting_configselect('defaulthomepage', get_string('defaulthomepage', 'admin'), get_string('configdefaulthomepage', 'admin'), HOMEPAGE_SITE, $choices)); + $temp->add(new admin_setting_configcheckbox('hidenotenrollable', get_string('hidenotenrollable', 'admin'), get_string('confighidenotenrollable', 'admin'), 0)); $temp->add(new admin_setting_configcheckbox('navshowcategories', get_string('navshowcategories', 'admin'), get_string('confignavshowcategories', 'admin'), 1)); $temp->add(new admin_setting_configcheckbox('navshowallcourses', get_string('navshowallcourses', 'admin'), get_string('confignavshowallcourses', 'admin'), 0)); $temp->add(new admin_setting_configtext('navcourselimit',get_string('navcourselimit','admin'),get_string('confignavcourselimit', 'admin'),20,PARAM_INT)); diff --git a/course/lib.php b/course/lib.php index 506b93e..941bbba 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2198,8 +2198,25 @@ function print_courses($category) { echo html_writer::start_tag('ul', array('class'=>'unlist')); foreach ($courses as $course) { $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - if ($course->visible == 1 || has_capability('moodle/course:viewhiddencourses', $coursecontext)) { - echo html_writer::start_tag('li'); + $instances = enrol_get_instances($course->id, true); + $plugins = enrol_get_plugins(true); + $self_enrollable = false; + foreach ($instances as $instance) { + if ($instance->enrol == 'guest') { + $self_enrollable = true; + break; + } + if (!isset($plugins[$instance->enrol])) { + continue; + } + $plugin = $plugins[$instance->enrol]; + if ($plugin->show_enrolme_link($instance)) { + $self_enrollable = true; + break; + } + } + if (($course->visible == 1 || has_capability('moodle/course:viewhiddencourses', $coursecontext)) && (!$CFG->hidenotenrollable || is_enrolled($coursecontext) || $self_enrollable || has_capability('moodle/course:viewhiddencourses', $coursecontext))) { + echo html_writer::start_tag('li'); print_course($course); echo html_writer::end_tag('li'); } diff --git a/lang/en/admin.php b/lang/en/admin.php index 5731813..03f9b0d 100755 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -227,6 +227,7 @@ $string['configgooglemapkey'] = 'You need to enter a special key to use Google M $string['configgradebookroles'] = 'This setting allows you to control who appears on the gradebook. Users need to have at least one of these roles in a course to be shown in the gradebook for that course.'; $string['configgradeexport'] = 'Choose which gradebook export formats are your primary methods for exporting grades. Chosen plugins will then set and use a "last exported" field for every grade. For example, this might result in exported records being identified as being "new" or "updated". If you are not sure about this then leave everything unchecked.'; $string['confighiddenuserfields'] = 'Select which user information fields you wish to hide from other users other than course teachers/admins. This will increase student privacy. Hold CTRL key to select multiple fields.'; +$string['confighidenotenrollable'] = 'Hide courses that are the user is not enrolled in and can\'t self-enrol in'; $string['configidnumber'] = 'This option specifies whether (a) Users are not be asked for an ID number at all, (b) Users are asked for an ID number but can leave it blank or (c) Users are asked for an ID Number and cannot leave it blank. If given the User\'s ID number is displayed in their Profile.'; $string['configintcachemax'] = 'For internal cache only. Maximum number of records to keep in the cache. Recommended value: 50. Use lower values to reduce memory usage.'; $string['configintro'] = 'On this page you can specify a number of configuration variables that help make Moodle work properly on your server. Don\'t worry too much about it - the defaults will usually work fine and you can always come back to this page later and change these settings.'; @@ -561,6 +562,7 @@ $string['hiddenuserfields'] = 'Hide user fields'; $string['hidefromall'] = 'Hide from all users'; $string['hidefromnone'] = 'Hide from nobody'; $string['hidefromstudents'] = 'Hide from students'; +$string['hidenotenrollable'] = 'Hide non-enrollable courses.'; $string['htmleditor'] = 'HTML editor'; $string['htmleditorsettings'] = 'HTML editor settings'; $string['htmlsettings'] = 'HTML settings'; diff --git a/lib/datalib.php b/lib/datalib.php index 19fed2b..81db061 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -388,12 +388,25 @@ function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") // loop throught them foreach ($courses as $course) { context_instance_preload($course); - if (isset($course->visible) && $course->visible <= 0) { - // for hidden courses, require visibility check - if (has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { - $visiblecourses [$course->id] = $course; - } - } else { + $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $instances = enrol_get_instances($course->id, true); + $plugins = enrol_get_plugins(true); + $self_enrollable = false; + foreach ($instances as $instance) { + if ($instance->enrol == 'guest') { + $self_enrollable = true; + break; + } + if (!isset($plugins[$instance->enrol])) { + continue; + } + $plugin = $plugins[$instance->enrol]; + if ($plugin->show_enrolme_link($instance)) { + $self_enrollable = true; + break; + } + } + if (($course->visible == 1 || has_capability('moodle/course:viewhiddencourses', $coursecontext)) && (!$CFG->hidenotenrollable || is_enrolled($coursecontext) || $self_enrollable || has_capability('moodle/course:viewhiddencourses', $coursecontext))) { $visiblecourses [$course->id] = $course; } } diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 485dfeb..0bfd8d5 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -979,6 +979,8 @@ class global_navigation extends navigation_node { // Load the course associated with the page into the navigation $course = $this->page->course; $coursenode = $this->load_course($course); + if (!$coursenode) + break; // If the user is not enrolled then we only want to show the // course node and not populate it. $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); @@ -1516,6 +1518,9 @@ class global_navigation extends navigation_node { if (!$coursenode) { $coursenode = $this->load_course($course); } + if (!$coursenode) { + return false; + } } $baseargs['course'] = $course->id; $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); @@ -1803,7 +1808,25 @@ class global_navigation extends navigation_node { public function add_course(stdClass $course, $forcegeneric = false) { global $CFG; $canviewhidden = has_capability('moodle/course:viewhiddencourses', $this->page->context); - if ($course->id !== SITEID && !$canviewhidden && !$course->visible) { + $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $instances = enrol_get_instances($course->id, true); + $plugins = enrol_get_plugins(true); + $self_enrollable = false; + foreach ($instances as $instance) { + if ($instance->enrol == 'guest') { + $self_enrollable = true; + break; + } + if (!isset($plugins[$instance->enrol])) { + continue; + } + $plugin = $plugins[$instance->enrol]; + if ($plugin->show_enrolme_link($instance)) { + $self_enrollable = true; + break; + } + } + if ($course->id !== SITEID && ((!$canviewhidden && !$course->visible) || ($CFG->hidenotenrollable && !is_enrolled($coursecontext) && !$self_enrollable && !$canviewhidden))) { return false; }