From 5e83b20527b7773182d1e30c1998410c95d7f943 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 20 Oct 2014 19:00:10 -0700 Subject: [PATCH] MDL-47778 tool_monitor: changes made to get_user_courses_select() The changes - 1) Ensure courses exist before we loop to avoid PHP warning. 2) Checked that the number of courses to display do not exceed the max setting. 3) Used format_string() instead of format_text(). 4) Used the course context instead of system context. --- .../classes/output/managesubs/renderer.php | 5 +++-- .../classes/output/managesubs/rules.php | 20 ++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/admin/tool/monitor/classes/output/managesubs/renderer.php b/admin/tool/monitor/classes/output/managesubs/renderer.php index fe3f8e05bf8..98dccec4d65 100644 --- a/admin/tool/monitor/classes/output/managesubs/renderer.php +++ b/admin/tool/monitor/classes/output/managesubs/renderer.php @@ -71,8 +71,9 @@ class renderer extends \plugin_renderer_base { * @return string to display on the mangesubs page. */ protected function render_course_select(rules $renderable) { - $select = $renderable->get_user_courses_select(); - return $this->render($select);; + if ($select = $renderable->get_user_courses_select()) { + return $this->render($select); + } } /** diff --git a/admin/tool/monitor/classes/output/managesubs/rules.php b/admin/tool/monitor/classes/output/managesubs/rules.php index f891e90c1a1..debe732e9be 100644 --- a/admin/tool/monitor/classes/output/managesubs/rules.php +++ b/admin/tool/monitor/classes/output/managesubs/rules.php @@ -154,14 +154,24 @@ class rules extends \table_sql implements \renderable { /** * Gets a list of courses where the current user can subscribe to rules as a dropdown. * - * @return \single_select list of courses. + * @return \single_select|bool returns the list of courses, or false if the select box + * should not be displayed. */ public function get_user_courses_select() { - $courses = get_user_capability_course('tool/monitor:subscribe', null, true, 'fullname'); + global $DB; + + // If the number of courses on the site exceed the maximum drop down limit do not display the select box. + $numcourses = $DB->count_records('course'); + if ($numcourses > COURSE_MAX_COURSES_PER_DROPDOWN) { + return false; + } + $options = array(0 => get_string('site')); - $systemcontext = \context_system::instance(); - foreach ($courses as $course) { - $options[$course->id] = format_text($course->fullname, array('context' => $systemcontext)); + if ($courses = get_user_capability_course('tool/monitor:subscribe', null, true, 'fullname')) { + foreach ($courses as $course) { + $options[$course->id] = format_string($course->fullname, true, + array('context' => \context_course::instance($course->id))); + } } $url = new \moodle_url('/admin/tool/monitor/index.php'); $select = new \single_select($url, 'courseid', $options, $this->courseid);