MDL-69454 core_search: consistent frontpage course search
[moodle.git] / course / search.php
CommitLineData
aa6c1ced 1<?php
f520dcd9
SH
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.
13//
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/>.
16
17/**
18 * Displays external information about a course
5dc361e1 19 * @package core_course
f520dcd9
SH
20 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24require_once("../config.php");
25require_once($CFG->dirroot.'/course/lib.php');
26
36667548 27$q = optional_param('q', '', PARAM_RAW); // Global search words.
f520dcd9
SH
28$search = optional_param('search', '', PARAM_RAW); // search words
29$page = optional_param('page', 0, PARAM_INT); // which page to show
837d548e 30$perpage = optional_param('perpage', '', PARAM_RAW); // how many per page, may be integer or 'all'
f520dcd9
SH
31$blocklist = optional_param('blocklist', 0, PARAM_INT);
32$modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
9e76429d 33$tagid = optional_param('tagid', '', PARAM_INT); // searches for courses tagged with this tag id
f520dcd9 34
36667548
SG
35// Use global search.
36if ($q) {
37 $search = $q;
38}
39
f520dcd9
SH
40// List of minimum capabilities which user need to have for editing/moving course
41$capabilities = array('moodle/course:create', 'moodle/category:manage');
42
4e0b6025 43// Populate usercatlist with list of category id's with course:create and category:manage capabilities.
442f12f8 44$usercatlist = core_course_category::make_categories_list($capabilities);
f520dcd9
SH
45
46$search = trim(strip_tags($search)); // trim & clean raw searched string
a8b56716 47
f520dcd9 48$site = get_site();
38a10939 49
837d548e 50$searchcriteria = array();
9e76429d 51foreach (array('search', 'blocklist', 'modulelist', 'tagid') as $param) {
f520dcd9 52 if (!empty($$param)) {
837d548e 53 $searchcriteria[$param] = $$param;
680a65a0 54 }
f520dcd9 55}
837d548e
MG
56$urlparams = array();
57if ($perpage !== 'all' && !($perpage = (int)$perpage)) {
58 // default number of courses per page
59 $perpage = $CFG->coursesperpage;
60} else {
f520dcd9
SH
61 $urlparams['perpage'] = $perpage;
62}
837d548e
MG
63if (!empty($page)) {
64 $urlparams['page'] = $page;
65}
66$PAGE->set_url('/course/search.php', $searchcriteria + $urlparams);
f520dcd9
SH
67$PAGE->set_context(context_system::instance());
68$PAGE->set_pagelayout('standard');
f4b571ab 69$courserenderer = $PAGE->get_renderer('core', 'course');
f520dcd9
SH
70
71if ($CFG->forcelogin) {
72 require_login();
73}
74
f520dcd9
SH
75$strcourses = new lang_string("courses");
76$strsearch = new lang_string("search");
77$strsearchresults = new lang_string("searchresults");
f520dcd9
SH
78$strnovalidcourses = new lang_string('novalidcourses');
79
beff3806
MG
80$courseurl = core_course_category::user_top() ? new moodle_url('/course/index.php') : null;
81$PAGE->navbar->add($strcourses, $courseurl);
f520dcd9
SH
82$PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
83if (!empty($search)) {
84 $PAGE->navbar->add(s($search));
85}
cf5b731c 86
60047003
MG
87if (empty($searchcriteria)) {
88 // no search criteria specified, print page with just search form
89 $PAGE->set_title("$site->fullname : $strsearch");
f520dcd9 90} else {
60047003
MG
91 // this is search results page
92 $PAGE->set_title("$site->fullname : $strsearchresults");
93 // Link to manage search results should be visible if user have system or category level capability
94 if ((can_edit_in_category() || !empty($usercatlist))) {
5dc361e1 95 $aurl = new moodle_url('/course/management.php', $searchcriteria);
60047003
MG
96 $searchform = $OUTPUT->single_button($aurl, get_string('managecourses'), 'get');
97 } else {
968cd483 98 $searchform = $courserenderer->course_search_form($search);
f520dcd9 99 }
60047003 100 $PAGE->set_button($searchform);
768e3613
SB
101
102 // Trigger event, courses searched.
103 $eventparams = array('context' => $PAGE->context, 'other' => array('query' => $search));
104 $event = \core\event\courses_searched::create($eventparams);
105 $event->trigger();
f520dcd9
SH
106}
107
60047003 108$PAGE->set_heading($site->fullname);
f520dcd9 109
60047003
MG
110echo $OUTPUT->header();
111echo $courserenderer->search_courses($searchcriteria);
f520dcd9 112echo $OUTPUT->footer();