From d0a3c52fdfe66246b4f885d354462471732a9fab Mon Sep 17 00:00:00 2001 From: Nicholas Hoobin Date: Thu, 22 Sep 2016 11:36:00 +1000 Subject: [PATCH] Issue #10 - Implementation of faculty search. --- index.php | 18 ++++++++--- lang/en/local_extension.php | 1 + renderer.php | 64 +++++++++++++++++++++++++++++++++---- 3 files changed, 72 insertions(+), 11 deletions(-) diff --git a/index.php b/index.php index 4b108e63..1c99116a 100644 --- a/index.php +++ b/index.php @@ -35,6 +35,7 @@ $stateid = optional_param('state', 0, PARAM_INT); $contextid = optional_param('contextid', 0, PARAM_INT); $search = optional_param('search', '', PARAM_RAW); // Make sure it is processed with p() or s() when sending to output! +$faculty = optional_param('faculty', '', PARAM_RAW); // Make sure it is processed with p() or s() when sending to output! $PAGE->set_url('/local/extension/index.php', array( 'page' => $page, @@ -43,7 +44,8 @@ 'catid' => $categoryid, 'state' => $stateid, 'id' => $courseid, - 'search' => $search, + 'search' => s($search), + 'faculty' => s($faculty), )); require_login(); @@ -93,13 +95,14 @@ // Should use this variable so that we don't break stuff every time a variable is added or changed. $baseurl = new moodle_url('/local/extension/index.php', array( - 'id' => $courseid, - 'catid' => $categoryid, - 'search' => s($search) + 'id' => $courseid, + 'catid' => $categoryid, + 'search' => s($search), + 'faculty' => s($faculty), )); // New filter functionality, searching and listing of requests. -echo $renderer->render_index_search_controls($context, $categoryid, $courseid, $stateid, $baseurl, $search); +echo $renderer->render_index_search_controls($context, $categoryid, $courseid, $stateid, $baseurl, $search, $faculty); $table = new \local_extension\local\table\index($baseurl); @@ -202,6 +205,11 @@ $params['search3'] = "%$search%"; } +if (!empty($faculty)) { + $wheres[] = $DB->sql_like('c.shortname', ':faculty', false, false); + $params['faculty'] = "%$faculty%"; +} + list($twhere, $tparams) = $table->get_sql_where(); if ($twhere) { $wheres[] = $twhere; diff --git a/lang/en/local_extension.php b/lang/en/local_extension.php index 2b56f807..558bfe9e 100644 --- a/lang/en/local_extension.php +++ b/lang/en/local_extension.php @@ -164,6 +164,7 @@ $string['page_index_all'] = 'All'; $string['page_index_categories'] = 'Categories'; $string['page_index_courses'] = 'Courses'; +$string['page_index_faculties'] = 'Faculty'; $string['page_index_toomanycourses'] = 'Too many courses, please search or filter by category.'; $string['page_request_notriggersdefined'] = 'No extension triggers have been defined.'; $string['page_request_outofrange'] = 'Please reduce the look ahead range.'; diff --git a/renderer.php b/renderer.php index 4d6d9c5c..514689d1 100644 --- a/renderer.php +++ b/renderer.php @@ -561,9 +561,10 @@ public function render_request_search_controls($courseid, $cmid, $searchback, $s * @param int $stateid * @param \moodle_url $baseurl * @param string $search + * @param string $faculty * @return string */ - public function render_index_search_controls($context, $categoryid, $courseid, $stateid, $baseurl, $search) { + public function render_index_search_controls($context, $categoryid, $courseid, $stateid, $baseurl, $search, $faculty) { global $SITE; $systemcontext = context_system::instance(); @@ -607,6 +608,45 @@ public function render_index_search_controls($context, $categoryid, $courseid, $ $controlstable->data[0]->cells[] = $categorycell; } + // Display a list of faculties to filter by + if (!empty($categorylist) || $viewallrequests || $modifyrequeststatus) { + $options = array(); + $options['0'] = get_string('page_index_all', 'local_extension'); + + if (!empty($categoryid)) { + $cat = coursecat::get($categoryid); + $courses = $cat->get_courses(array('recursive' => true)); + } else { + $courses = coursecat::get(0)->get_courses(array('recursive' => true)); + + } + + foreach ($courses as $course) { + $re = "/^([A-Z]+)/i"; + if (preg_match($re, $course->shortname, $matches)) { + $options[$matches[1]] = $matches[1]; + } + + } + + $popupurl = new moodle_url('/local/extension/index.php', array( + 'catid' => $categoryid + )); + + $select = new single_select($popupurl, 'faculty', $options, $faculty, null, 'requestform'); + + $strcourses = get_string('page_index_faculties', 'local_extension'); + $html = html_writer::span($strcourses, '', array('id' => 'courses')); + $html .= $this->render($select); + + $categorycell = new html_table_cell(); + $categorycell->attributes['class'] = 'right'; + $categorycell->text = $html; + + $controlstable->data[0]->cells[] = $categorycell; + + } + // Display a list of all courses to filter by // TODO change this to categories that the user is enroled in. / has the cap to modify if (!empty($categorylist) || $viewallrequests || $modifyrequeststatus) { @@ -629,15 +669,26 @@ public function render_index_search_controls($context, $categoryid, $courseid, $ } else { - } */ + foreach ($courses as $course) { - $options[$course->id] = $course->fullname; + + if (!empty($faculty)) { + $re = "/^$faculty/i"; + if (preg_match($re, $course->shortname)) { + $options[$course->id] = $course->fullname; + } + + } else { + $options[$course->id] = $course->fullname; + + } } $popupurl = new moodle_url('/local/extension/index.php', array( - 'catid' => $categoryid + 'catid' => $categoryid, + 'faculty' => $faculty, )); $select = new single_select($popupurl, 'id', $options, $courseid, null, 'requestform'); @@ -674,7 +725,7 @@ public function render_index_search_controls($context, $categoryid, $courseid, $ } $popupurl = new moodle_url('/local/extension/index.php', array( - 'catid' => $categoryid + 'catid' => $categoryid, )); $select = new single_select($popupurl, 'id', $courselist, $courseid, null, 'requestform'); @@ -697,7 +748,8 @@ public function render_index_search_controls($context, $categoryid, $courseid, $ $popupurl = new moodle_url('/local/extension/index.php', array( 'catid' => $categoryid, - 'id' => $courseid + 'id' => $courseid, + 'faculty' => $faculty, )); $select = new single_select($popupurl, 'state', $statelist, $stateid, null, 'requestform');