Merge branch 'MDL-63303_master-deleteduserfix' of https://github.com/markn86/moodle
[moodle.git] / course / search.php
blobe02bd24060809e3195a91882a0eea8e2b556a303
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Displays external information about a course
19 * @package core_course
20 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require_once("../config.php");
25 require_once($CFG->dirroot.'/course/lib.php');
27 $search = optional_param('search', '', PARAM_RAW); // search words
28 $page = optional_param('page', 0, PARAM_INT); // which page to show
29 $perpage = optional_param('perpage', '', PARAM_RAW); // how many per page, may be integer or 'all'
30 $blocklist = optional_param('blocklist', 0, PARAM_INT);
31 $modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
32 $tagid = optional_param('tagid', '', PARAM_INT); // searches for courses tagged with this tag id
34 // List of minimum capabilities which user need to have for editing/moving course
35 $capabilities = array('moodle/course:create', 'moodle/category:manage');
37 // Populate usercatlist with list of category id's with course:create and category:manage capabilities.
38 $usercatlist = core_course_category::make_categories_list($capabilities);
40 $search = trim(strip_tags($search)); // trim & clean raw searched string
42 $site = get_site();
44 $searchcriteria = array();
45 foreach (array('search', 'blocklist', 'modulelist', 'tagid') as $param) {
46 if (!empty($$param)) {
47 $searchcriteria[$param] = $$param;
50 $urlparams = array();
51 if ($perpage !== 'all' && !($perpage = (int)$perpage)) {
52 // default number of courses per page
53 $perpage = $CFG->coursesperpage;
54 } else {
55 $urlparams['perpage'] = $perpage;
57 if (!empty($page)) {
58 $urlparams['page'] = $page;
60 $PAGE->set_url('/course/search.php', $searchcriteria + $urlparams);
61 $PAGE->set_context(context_system::instance());
62 $PAGE->set_pagelayout('standard');
63 $courserenderer = $PAGE->get_renderer('core', 'course');
65 if ($CFG->forcelogin) {
66 require_login();
69 $strcourses = new lang_string("courses");
70 $strsearch = new lang_string("search");
71 $strsearchresults = new lang_string("searchresults");
72 $strnovalidcourses = new lang_string('novalidcourses');
74 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
75 $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
76 if (!empty($search)) {
77 $PAGE->navbar->add(s($search));
80 if (empty($searchcriteria)) {
81 // no search criteria specified, print page with just search form
82 $PAGE->set_title("$site->fullname : $strsearch");
83 } else {
84 // this is search results page
85 $PAGE->set_title("$site->fullname : $strsearchresults");
86 // Link to manage search results should be visible if user have system or category level capability
87 if ((can_edit_in_category() || !empty($usercatlist))) {
88 $aurl = new moodle_url('/course/management.php', $searchcriteria);
89 $searchform = $OUTPUT->single_button($aurl, get_string('managecourses'), 'get');
90 } else {
91 $searchform = $courserenderer->course_search_form($search, 'navbar');
93 $PAGE->set_button($searchform);
95 // Trigger event, courses searched.
96 $eventparams = array('context' => $PAGE->context, 'other' => array('query' => $search));
97 $event = \core\event\courses_searched::create($eventparams);
98 $event->trigger();
101 $PAGE->set_heading($site->fullname);
103 echo $OUTPUT->header();
104 echo $courserenderer->search_courses($searchcriteria);
105 echo $OUTPUT->footer();