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
// 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("lib.php");
require_once("../config.php");
require_once($CFG->dirroot.'/course/lib.php');
$search = optional_param('search', '', PARAM_RAW); // search words
$page = optional_param('page', 0, PARAM_INT); // which page to show
$perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
$moveto = optional_param('moveto', 0, PARAM_INT); // move to category
$edit = optional_param('edit', -1, PARAM_BOOL);
$hide = optional_param('hide', 0, PARAM_INT);
$show = optional_param('show', 0, PARAM_INT);
$blocklist = optional_param('blocklist', 0, PARAM_INT);
$modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
$search = optional_param('search', '', PARAM_RAW); // search words
$page = optional_param('page', 0, PARAM_INT); // which page to show
$perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
$moveto = optional_param('moveto', 0, PARAM_INT); // move to category
$edit = optional_param('edit', -1, PARAM_BOOL);
$hide = optional_param('hide', 0, PARAM_INT);
$show = optional_param('show', 0, PARAM_INT);
$blocklist = optional_param('blocklist', 0, PARAM_INT);
$modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
// List of minimum capabilities which user need to have for editing/moving course
$capabilities = array('moodle/course:create', 'moodle/category:manage');
// List of minimum capabilities which user need to have for editing/moving course
$capabilities = array('moodle/course:create', 'moodle/category:manage');
// List of category id's in which current user has course:create and category:manage capability.
$usercatlist = array();
// List of category id's in which current user has course:create and category:manage capability.
$usercatlist = array();
// List of parent category id's
$catparentlist = array();
// List of parent category id's
$catparentlist = array();
//Populate usercatlist with list of category id's with required capabilities.
make_categories_list($usercatlist, $catparentlist, $capabilities);
// Populate usercatlist with list of category id's with required capabilities.
make_categories_list($usercatlist, $catparentlist, $capabilities);
$search = trim(strip_tags($search)); // trim & clean raw searched string
if ($search) {
$search = trim(strip_tags($search)); // trim & clean raw searched string
if ($search) {
$searchterms = explode(" ", $search); // Search for words independently
foreach ($searchterms as $key => $searchterm) {
if (strlen($searchterm) < 2) {
@ -36,29 +56,29 @@
}
}
$search = trim(implode(" ", $searchterms));
}
}
$site = get_site();
$site = get_site();
$urlparams = array();
foreach (array('search', 'page', 'blocklist', 'modulelist', 'edit') as $param) {
$urlparams = array();
foreach (array('search', 'page', 'blocklist', 'modulelist', 'edit') as $param) {
if (!empty($$param)) {
$urlparams[$param] = $$param;
}
}
if ($perpage != 10) {
}
if ($perpage != 10) {
$urlparams['perpage'] = $perpage;
}
$PAGE->set_url('/course/search.php', $urlparams);
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
}
$PAGE->set_url('/course/search.php', $urlparams);
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
if ($CFG->forcelogin) {
if ($CFG->forcelogin) {
require_login();
}
}
//Editing is possible if user have system or category level create and manage capability
if (can_edit_in_category() || !empty($usercatlist)) {
// Editing is possible if user has system or category level create and manage capability
if (can_edit_in_category() || !empty($usercatlist)) {
if ($edit !== -1) {
$USER->editing = $edit;
}
@ -68,43 +88,43 @@
if ($perpage != 99999) {
$perpage = 30;
}
} else {
} else {
$adminediting = false;
}
}
/// Editing functions
if (has_capability('moodle/course:visibility', context_system::instance())) {
/// Hide or show a course
if ($hide or $show and confirm_sesskey()) {
// Editing functions
if (has_capability('moodle/course:visibility', context_system::instance())) {
// Hide or show a course
if (($hide || $show) && confirm_sesskey()) {
if ($hide) {
$course = $DB->get_record("course", array("id"=>$hide));
$course = $DB->get_record("course", array("id" => $hide));
$visible = 0;
} else {
$course = $DB->get_record("course", array("id"=>$show));
$course = $DB->get_record("course", array("id" => $show));
$visible = 1;
}
if ($course) {
$DB->set_field("course", "visible", $visible, array("id"=>$course->id));
}
$DB->set_field("course", "visible", $visible, array("id" => $course->id));
}
}
}
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist);
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist);
$strcourses = get_string("courses");
$strsearch = get_string("search");
$strsearchresults = get_string("searchresults");
$strcategory = get_string("category");
$strselect = get_string("select");
$strselectall = get_string("selectall");
$strdeselectall = get_string("deselectall");
$stredit = get_string("edit");
$strfrontpage = get_string('frontpage', 'admin');
$strnovalidcourses = get_string('novalidcourses');
$strcourses = new lang_string("courses");
$strsearch = new lang_string("search");
$strsearchresults = new lang_string("searchresults");
$strcategory = new lang_string("category");
$strselect = new lang_string("select");
$strselectall = new lang_string("selectall");
$strdeselectall = new lang_string("deselectall");
$stredit = new lang_string("edit");
$strfrontpage = new lang_string('frontpage', 'admin');
$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($strsearch);
$PAGE->set_title("$site->fullname : $strsearch");
@ -122,15 +142,15 @@
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
exit;
}
}
$courses = array();
if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
$courses = array();
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))) {
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/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.
$coursecontext = context_course::instance($courseid);
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);
array_push($courses, $courseid);
}
}
}
move_courses($courses, $moveto);
}
}
// get list of courses containing blocks if required
if (!empty($blocklist) and confirm_sesskey()) {
// get list of courses containing blocks if required
if (!empty($blocklist) and confirm_sesskey()) {
$blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
$courses = array();
$courses = $DB->get_records_sql("
@ -160,7 +183,7 @@
WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
array($blockname));
$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) {
$courses = array_chunk($courses, $perpage, true);
$courses = $courses[$page];
@ -168,7 +191,7 @@
foreach ($courses as $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;
$sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
." WHERE module.course=c.id";
@ -180,7 +203,7 @@
$lastcourse = $page*$perpage + $perpage -1;
$i = 0;
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));
}
$i++;
@ -190,41 +213,41 @@
else {
$totalcount = 0;
}
} else if (!empty($searchterm)) { //Donot do search for empty search request.
$courses = get_courses_search($searchterms, "fullname ASC",
$page, $perpage, $totalcount);
}
} else if (!empty($searchterm)) {
// Donot do search for empty search request.
$courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount);
}
$searchform = '';
//Turn editing should be visible if user have system or category level capability
if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
$searchform = '';
// Turn editing should be visible if user have system or category level capability
if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
if ($PAGE->user_is_editing()) {
$string = get_string("turneditingoff");
$string = new lang_string("turneditingoff");
$edit = "off";
} else {
$string = get_string("turneditingon");
$string = new lang_string("turneditingon");
$edit = "on";
}
$params = array_merge($urlparams, array('sesskey' => sesskey(), 'edit' => $edit));
$aurl = new moodle_url("$CFG->wwwroot/course/search.php", $params);
$searchform = $OUTPUT->single_button($aurl, $string, 'get');
} else {
} else {
$searchform = print_course_search($search, true, "navbar");
}
}
$PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
$PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
if (!empty($search)) {
$PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
$PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
if (!empty($search)) {
$PAGE->navbar->add(s($search));
}
$PAGE->set_title("$site->fullname : $strsearchresults");
$PAGE->set_heading($site->fullname);
$PAGE->set_button($searchform);
}
$PAGE->set_title("$site->fullname : $strsearchresults");
$PAGE->set_heading($site->fullname);
$PAGE->set_button($searchform);
echo $OUTPUT->header();
echo $OUTPUT->header();
$lastcategory = -1;
if ($courses) {
$lastcategory = -1;
if ($courses) {
echo $OUTPUT->heading("$strsearchresults: $totalcount");
$encodedsearch = urlencode($search);
@ -251,7 +274,8 @@
print_course($course, $search);
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 "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
@ -352,7 +376,7 @@
echo "<br />";
echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\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'));
$PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
echo "</td>\n</tr>\n";
@ -362,42 +386,42 @@
print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
} else {
} else {
if (!empty($search)) {
echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
}
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
* Display page numbers, and a link for displaying all entries
* @param integer $totalcount - number of entry to display
* @param integer $page - page number
* @param integer $perpage - number of entry per page
* @param int $totalcount number of entry to display
* @param int $page page number
* @param int $perpage number of entry per page
* @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;
echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
//display
// display
if ($perpage != 99999 && $totalcount > $perpage) {
echo "<center><p>";
echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
echo "</p></center>";
} else if ($perpage === 99999) {
$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');
if (has_any_capability($capabilities, context_system::instance())) {
$defaultperpage = 30;
@ -407,6 +431,4 @@
echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
echo "</p></center>";
}
}
}