MDL-31640 course search: Coding style clean up

This commit is contained in:
Sam Hemelryk 2012-03-21 09:49:38 +13:00
parent 1a381535e6
commit f520dcd9ba

View file

@ -1,34 +1,54 @@
<?php <?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/// Displays external information about a course /**
* Displays external information about a course
* @package core
* @category course
* @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("../config.php"); require_once("../config.php");
require_once("lib.php"); require_once($CFG->dirroot.'/course/lib.php');
$search = optional_param('search', '', PARAM_RAW); // search words $search = optional_param('search', '', PARAM_RAW); // search words
$page = optional_param('page', 0, PARAM_INT); // which page to show $page = optional_param('page', 0, PARAM_INT); // which page to show
$perpage = optional_param('perpage', 10, PARAM_INT); // how many per page $perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
$moveto = optional_param('moveto', 0, PARAM_INT); // move to category $moveto = optional_param('moveto', 0, PARAM_INT); // move to category
$edit = optional_param('edit', -1, PARAM_BOOL); $edit = optional_param('edit', -1, PARAM_BOOL);
$hide = optional_param('hide', 0, PARAM_INT); $hide = optional_param('hide', 0, PARAM_INT);
$show = optional_param('show', 0, PARAM_INT); $show = optional_param('show', 0, PARAM_INT);
$blocklist = optional_param('blocklist', 0, PARAM_INT); $blocklist = optional_param('blocklist', 0, PARAM_INT);
$modulelist= optional_param('modulelist', '', PARAM_PLUGIN); $modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
// List of minimum capabilities which user need to have for editing/moving course // List of minimum capabilities which user need to have for editing/moving course
$capabilities = array('moodle/course:create', 'moodle/category:manage'); $capabilities = array('moodle/course:create', 'moodle/category:manage');
// List of category id's in which current user has course:create and category:manage capability. // List of category id's in which current user has course:create and category:manage capability.
$usercatlist = array(); $usercatlist = array();
// List of parent category id's // List of parent category id's
$catparentlist = array(); $catparentlist = array();
//Populate usercatlist with list of category id's with required capabilities. // Populate usercatlist with list of category id's with required capabilities.
make_categories_list($usercatlist, $catparentlist, $capabilities); make_categories_list($usercatlist, $catparentlist, $capabilities);
$search = trim(strip_tags($search)); // trim & clean raw searched string $search = trim(strip_tags($search)); // trim & clean raw searched string
if ($search) { if ($search) {
$searchterms = explode(" ", $search); // Search for words independently $searchterms = explode(" ", $search); // Search for words independently
foreach ($searchterms as $key => $searchterm) { foreach ($searchterms as $key => $searchterm) {
if (strlen($searchterm) < 2) { if (strlen($searchterm) < 2) {
@ -36,29 +56,29 @@
} }
} }
$search = trim(implode(" ", $searchterms)); $search = trim(implode(" ", $searchterms));
} }
$site = get_site(); $site = get_site();
$urlparams = array(); $urlparams = array();
foreach (array('search', 'page', 'blocklist', 'modulelist', 'edit') as $param) { foreach (array('search', 'page', 'blocklist', 'modulelist', 'edit') as $param) {
if (!empty($$param)) { if (!empty($$param)) {
$urlparams[$param] = $$param; $urlparams[$param] = $$param;
} }
} }
if ($perpage != 10) { if ($perpage != 10) {
$urlparams['perpage'] = $perpage; $urlparams['perpage'] = $perpage;
} }
$PAGE->set_url('/course/search.php', $urlparams); $PAGE->set_url('/course/search.php', $urlparams);
$PAGE->set_context(context_system::instance()); $PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard'); $PAGE->set_pagelayout('standard');
if ($CFG->forcelogin) { if ($CFG->forcelogin) {
require_login(); require_login();
} }
//Editing is possible if user have system or category level create and manage capability // Editing is possible if user has system or category level create and manage capability
if (can_edit_in_category() || !empty($usercatlist)) { if (can_edit_in_category() || !empty($usercatlist)) {
if ($edit !== -1) { if ($edit !== -1) {
$USER->editing = $edit; $USER->editing = $edit;
} }
@ -68,43 +88,43 @@
if ($perpage != 99999) { if ($perpage != 99999) {
$perpage = 30; $perpage = 30;
} }
} else { } else {
$adminediting = false; $adminediting = false;
} }
/// Editing functions // Editing functions
if (has_capability('moodle/course:visibility', context_system::instance())) { if (has_capability('moodle/course:visibility', context_system::instance())) {
/// Hide or show a course // Hide or show a course
if ($hide or $show and confirm_sesskey()) { if (($hide || $show) && confirm_sesskey()) {
if ($hide) { if ($hide) {
$course = $DB->get_record("course", array("id"=>$hide)); $course = $DB->get_record("course", array("id" => $hide));
$visible = 0; $visible = 0;
} else { } else {
$course = $DB->get_record("course", array("id"=>$show)); $course = $DB->get_record("course", array("id" => $show));
$visible = 1; $visible = 1;
} }
if ($course) { if ($course) {
$DB->set_field("course", "visible", $visible, array("id"=>$course->id)); $DB->set_field("course", "visible", $visible, array("id" => $course->id));
}
} }
} }
}
$displaylist = array(); $displaylist = array();
$parentlist = array(); $parentlist = array();
make_categories_list($displaylist, $parentlist); make_categories_list($displaylist, $parentlist);
$strcourses = get_string("courses"); $strcourses = new lang_string("courses");
$strsearch = get_string("search"); $strsearch = new lang_string("search");
$strsearchresults = get_string("searchresults"); $strsearchresults = new lang_string("searchresults");
$strcategory = get_string("category"); $strcategory = new lang_string("category");
$strselect = get_string("select"); $strselect = new lang_string("select");
$strselectall = get_string("selectall"); $strselectall = new lang_string("selectall");
$strdeselectall = get_string("deselectall"); $strdeselectall = new lang_string("deselectall");
$stredit = get_string("edit"); $stredit = new lang_string("edit");
$strfrontpage = get_string('frontpage', 'admin'); $strfrontpage = new lang_string('frontpage', 'admin');
$strnovalidcourses = get_string('novalidcourses'); $strnovalidcourses = new lang_string('novalidcourses');
if (empty($search) and empty($blocklist) and empty($modulelist) and empty($moveto) and ($edit != -1)) { if (empty($search) and empty($blocklist) and empty($modulelist) and empty($moveto) and ($edit != -1)) {
$PAGE->navbar->add($strcourses, new moodle_url('/course/index.php')); $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
$PAGE->navbar->add($strsearch); $PAGE->navbar->add($strsearch);
$PAGE->set_title("$site->fullname : $strsearch"); $PAGE->set_title("$site->fullname : $strsearch");
@ -122,15 +142,15 @@
echo $OUTPUT->box_end(); echo $OUTPUT->box_end();
echo $OUTPUT->footer(); echo $OUTPUT->footer();
exit; exit;
} }
$courses = array(); $courses = array();
if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
if (!$destcategory = $DB->get_record("course_categories", array("id" => $moveto))) { if (!$destcategory = $DB->get_record("course_categories", array("id" => $moveto))) {
print_error('cannotfindcategory', '', '', $moveto); print_error('cannotfindcategory', '', '', $moveto);
} }
//User should have manage and create capablity on destination category. // User should have manage and create capablity on destination category.
require_capability('moodle/category:manage', context_coursecat::instance($moveto)); require_capability('moodle/category:manage', context_coursecat::instance($moveto));
require_capability('moodle/course:create', context_coursecat::instance($moveto)); require_capability('moodle/course:create', context_coursecat::instance($moveto));
@ -140,16 +160,19 @@
// user must have category:manage and course:create capability for the course to be moved. // user must have category:manage and course:create capability for the course to be moved.
$coursecontext = context_course::instance($courseid); $coursecontext = context_course::instance($courseid);
foreach ($capabilities as $capability) { foreach ($capabilities as $capability) {
// Require capability here will result in a fatal error should the user not
// have the requried category ensuring that no moves occur if they are
// trying to move multiple courses.
require_capability($capability, $coursecontext); require_capability($capability, $coursecontext);
array_push($courses, $courseid); array_push($courses, $courseid);
} }
} }
} }
move_courses($courses, $moveto); move_courses($courses, $moveto);
} }
// get list of courses containing blocks if required // get list of courses containing blocks if required
if (!empty($blocklist) and confirm_sesskey()) { if (!empty($blocklist) and confirm_sesskey()) {
$blockname = $DB->get_field('block', 'name', array('id' => $blocklist)); $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
$courses = array(); $courses = array();
$courses = $DB->get_records_sql(" $courses = $DB->get_records_sql("
@ -160,7 +183,7 @@
WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)", WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
array($blockname)); array($blockname));
$totalcount = count($courses); $totalcount = count($courses);
//Keep only chunk of array which you want to display // Keep only chunk of array which you want to display
if ($totalcount > $perpage) { if ($totalcount > $perpage) {
$courses = array_chunk($courses, $perpage, true); $courses = array_chunk($courses, $perpage, true);
$courses = $courses[$page]; $courses = $courses[$page];
@ -168,7 +191,7 @@
foreach ($courses as $course) { foreach ($courses as $course) {
$courses[$course->id] = $course; $courses[$course->id] = $course;
} }
} elseif (!empty($modulelist) and confirm_sesskey()) { // get list of courses containing modules } elseif (!empty($modulelist) and confirm_sesskey()) { // get list of courses containing modules
$modulename = $modulelist; $modulename = $modulelist;
$sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c" $sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
." WHERE module.course=c.id"; ." WHERE module.course=c.id";
@ -180,7 +203,7 @@
$lastcourse = $page*$perpage + $perpage -1; $lastcourse = $page*$perpage + $perpage -1;
$i = 0; $i = 0;
foreach ($courseids as $courseid) { foreach ($courseids as $courseid) {
if ($i>= $firstcourse && $i<=$lastcourse) { if ($i >= $firstcourse && $i <= $lastcourse) {
$courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id)); $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
} }
$i++; $i++;
@ -190,41 +213,41 @@
else { else {
$totalcount = 0; $totalcount = 0;
} }
} else if (!empty($searchterm)) { //Donot do search for empty search request. } else if (!empty($searchterm)) {
$courses = get_courses_search($searchterms, "fullname ASC", // Donot do search for empty search request.
$page, $perpage, $totalcount); $courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount);
} }
$searchform = ''; $searchform = '';
//Turn editing should be visible if user have system or category level capability // Turn editing should be visible if user have system or category level capability
if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) { if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
if ($PAGE->user_is_editing()) { if ($PAGE->user_is_editing()) {
$string = get_string("turneditingoff"); $string = new lang_string("turneditingoff");
$edit = "off"; $edit = "off";
} else { } else {
$string = get_string("turneditingon"); $string = new lang_string("turneditingon");
$edit = "on"; $edit = "on";
} }
$params = array_merge($urlparams, array('sesskey' => sesskey(), 'edit' => $edit)); $params = array_merge($urlparams, array('sesskey' => sesskey(), 'edit' => $edit));
$aurl = new moodle_url("$CFG->wwwroot/course/search.php", $params); $aurl = new moodle_url("$CFG->wwwroot/course/search.php", $params);
$searchform = $OUTPUT->single_button($aurl, $string, 'get'); $searchform = $OUTPUT->single_button($aurl, $string, 'get');
} else { } else {
$searchform = print_course_search($search, true, "navbar"); $searchform = print_course_search($search, true, "navbar");
} }
$PAGE->navbar->add($strcourses, new moodle_url('/course/index.php')); $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
$PAGE->navbar->add($strsearch, new moodle_url('/course/search.php')); $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
if (!empty($search)) { if (!empty($search)) {
$PAGE->navbar->add(s($search)); $PAGE->navbar->add(s($search));
} }
$PAGE->set_title("$site->fullname : $strsearchresults"); $PAGE->set_title("$site->fullname : $strsearchresults");
$PAGE->set_heading($site->fullname); $PAGE->set_heading($site->fullname);
$PAGE->set_button($searchform); $PAGE->set_button($searchform);
echo $OUTPUT->header(); echo $OUTPUT->header();
$lastcategory = -1; $lastcategory = -1;
if ($courses) { if ($courses) {
echo $OUTPUT->heading("$strsearchresults: $totalcount"); echo $OUTPUT->heading("$strsearchresults: $totalcount");
$encodedsearch = urlencode($search); $encodedsearch = urlencode($search);
@ -251,7 +274,8 @@
print_course($course, $search); print_course($course, $search);
echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
} }
} else { //editing mode } else {
// Editing mode
echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n"; echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n"; echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n"; echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
@ -352,7 +376,7 @@
echo "<br />"; echo "<br />";
echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n"; echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n"; echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
//Select box should only show categories in which user has min capability to move course. // Select box should only show categories in which user has min capability to move course.
echo html_writer::select($usercatlist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid')); echo html_writer::select($usercatlist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid'));
$PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false)); $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
echo "</td>\n</tr>\n"; echo "</td>\n</tr>\n";
@ -362,42 +386,42 @@
print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink); print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
} else { } else {
if (!empty($search)) { if (!empty($search)) {
echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search))); echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
} }
else { else {
echo $OUTPUT->heading( $strnovalidcourses ); echo $OUTPUT->heading($strnovalidcourses);
}
} }
}
echo "<br /><br />"; echo "<br /><br />";
print_course_search($search); print_course_search($search);
echo $OUTPUT->footer(); echo $OUTPUT->footer();
/** /**
* Print a list navigation bar * Print a list navigation bar
* Display page numbers, and a link for displaying all entries * Display page numbers, and a link for displaying all entries
* @param integer $totalcount - number of entry to display * @param int $totalcount number of entry to display
* @param integer $page - page number * @param int $page page number
* @param integer $perpage - number of entry per page * @param int $perpage number of entry per page
* @param string $encodedsearch * @param string $encodedsearch
* @param string $modulelink - module name * @param string $modulelink module name
*/ */
function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) { function print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink) {
global $OUTPUT; global $OUTPUT;
echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage"); echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
//display // display
if ($perpage != 99999 && $totalcount > $perpage) { if ($perpage != 99999 && $totalcount > $perpage) {
echo "<center><p>"; echo "<center><p>";
echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>"; echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
echo "</p></center>"; echo "</p></center>";
} else if ($perpage === 99999) { } else if ($perpage === 99999) {
$defaultperpage = 10; $defaultperpage = 10;
//If user has course:create or category:manage capability the show 30 records. // If user has course:create or category:manage capability the show 30 records.
$capabilities = array('moodle/course:create', 'moodle/category:manage'); $capabilities = array('moodle/course:create', 'moodle/category:manage');
if (has_any_capability($capabilities, context_system::instance())) { if (has_any_capability($capabilities, context_system::instance())) {
$defaultperpage = 30; $defaultperpage = 30;
@ -407,6 +431,4 @@
echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>"; echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
echo "</p></center>"; echo "</p></center>";
} }
} }