MDL-37009 moved editing functionality from course/search.php to course/manage.php
[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
19 * @package core
20 * @category course
21 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once("../config.php");
26require_once($CFG->dirroot.'/course/lib.php');
4e0b6025 27require_once($CFG->libdir.'/coursecatlib.php');
f520dcd9
SH
28
29$search = optional_param('search', '', PARAM_RAW); // search words
30$page = optional_param('page', 0, PARAM_INT); // which page to show
837d548e 31$perpage = optional_param('perpage', '', PARAM_RAW); // how many per page, may be integer or 'all'
f520dcd9
SH
32$blocklist = optional_param('blocklist', 0, PARAM_INT);
33$modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
34
35// List of minimum capabilities which user need to have for editing/moving course
36$capabilities = array('moodle/course:create', 'moodle/category:manage');
37
4e0b6025
MG
38// Populate usercatlist with list of category id's with course:create and category:manage capabilities.
39$usercatlist = coursecat::make_categories_list($capabilities);
f520dcd9
SH
40
41$search = trim(strip_tags($search)); // trim & clean raw searched string
a8b56716 42
f520dcd9 43$site = get_site();
38a10939 44
837d548e
MG
45$searchcriteria = array();
46foreach (array('search', 'blocklist', 'modulelist') as $param) {
f520dcd9 47 if (!empty($$param)) {
837d548e 48 $searchcriteria[$param] = $$param;
680a65a0 49 }
f520dcd9 50}
837d548e
MG
51$urlparams = array();
52if ($perpage !== 'all' && !($perpage = (int)$perpage)) {
53 // default number of courses per page
54 $perpage = $CFG->coursesperpage;
55} else {
f520dcd9
SH
56 $urlparams['perpage'] = $perpage;
57}
837d548e
MG
58if (!empty($page)) {
59 $urlparams['page'] = $page;
60}
61$PAGE->set_url('/course/search.php', $searchcriteria + $urlparams);
f520dcd9
SH
62$PAGE->set_context(context_system::instance());
63$PAGE->set_pagelayout('standard');
f4b571ab 64$courserenderer = $PAGE->get_renderer('core', 'course');
f520dcd9
SH
65
66if ($CFG->forcelogin) {
67 require_login();
68}
69
f520dcd9
SH
70$strcourses = new lang_string("courses");
71$strsearch = new lang_string("search");
72$strsearchresults = new lang_string("searchresults");
f520dcd9
SH
73$strnovalidcourses = new lang_string('novalidcourses');
74
837d548e
MG
75if (empty($searchcriteria)) {
76 // no search criteria specified, print page with just search form
f520dcd9
SH
77 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
78 $PAGE->navbar->add($strsearch);
79 $PAGE->set_title("$site->fullname : $strsearch");
80 $PAGE->set_heading($site->fullname);
861efb19 81
f520dcd9
SH
82 echo $OUTPUT->header();
83 echo $OUTPUT->box_start();
84 echo "<center>";
85 echo "<br />";
f4b571ab 86 echo $courserenderer->course_search_form('', 'plain');
f520dcd9
SH
87 echo "<br /><p>";
88 print_string("searchhelp");
89 echo "</p>";
90 echo "</center>";
91 echo $OUTPUT->box_end();
92 echo $OUTPUT->footer();
93 exit;
94}
95
837d548e
MG
96// get list of courses
97$searchoptions = array('recursive' => true, 'sort' => array('fullname' => 1));
98if ($perpage !== 'all') {
99 $searchoptions['offset'] = $page * $perpage;
100 $searchoptions['limit'] = $perpage;
f520dcd9 101}
837d548e
MG
102$courses = coursecat::get(0)->search_courses($searchcriteria, $searchoptions);
103$totalcount = coursecat::get(0)->search_courses_count($searchcriteria, $searchoptions);
f520dcd9
SH
104
105$searchform = '';
106// Turn editing should be visible if user have system or category level capability
107if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
837d548e
MG
108 $aurl = new moodle_url('/course/manage.php', $searchcriteria);
109 $searchform = $OUTPUT->single_button($aurl, get_string('managecourses'), 'get');
f520dcd9 110} else {
f4b571ab 111 $searchform = $courserenderer->course_search_form($search, 'navbar');
f520dcd9
SH
112}
113
114$PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
115$PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
116if (!empty($search)) {
117 $PAGE->navbar->add(s($search));
118}
119$PAGE->set_title("$site->fullname : $strsearchresults");
120$PAGE->set_heading($site->fullname);
121$PAGE->set_button($searchform);
122
123echo $OUTPUT->header();
124
f520dcd9
SH
125if ($courses) {
126 echo $OUTPUT->heading("$strsearchresults: $totalcount");
f520dcd9
SH
127
128 // add the module/block parameter to the paging bar if they exists
129 $modulelink = "";
130 if (!empty($modulelist) and confirm_sesskey()) {
131 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
132 } else if (!empty($blocklist) and confirm_sesskey()) {
133 $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
7d2a0492 134 }
0be6f678 135
837d548e 136 print_navigation_bar($totalcount, $page, $perpage, $searchcriteria);
38a10939 137
f520dcd9 138 // Show list of courses
837d548e 139 echo $courserenderer->courses_list($courses, $search, true);
698fa439 140
837d548e 141 print_navigation_bar($totalcount, $page, $perpage, $searchcriteria);
cf5b731c 142
f520dcd9
SH
143} else {
144 if (!empty($search)) {
145 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
d0b7da69 146 }
f520dcd9
SH
147 else {
148 echo $OUTPUT->heading($strnovalidcourses);
149 }
150}
151
152echo "<br /><br />";
153
f4b571ab 154echo $courserenderer->course_search_form($search);
f520dcd9
SH
155
156echo $OUTPUT->footer();
157
158/**
159 * Print a list navigation bar
160 * Display page numbers, and a link for displaying all entries
161 * @param int $totalcount number of entry to display
162 * @param int $page page number
163 * @param int $perpage number of entry per page
837d548e 164 * @param array $search
f520dcd9 165 */
837d548e
MG
166function print_navigation_bar($totalcount, $page, $perpage, $search) {
167 global $OUTPUT, $CFG;
168 $url = new moodle_url('/course/search.php', $search);
169 if ($perpage !== 'all' && $totalcount > $perpage) {
170 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url->out(false, array('perpage' => $perpage)));
f520dcd9 171 echo "<center><p>";
837d548e 172 echo html_writer::link($url->out(false, array('perpage' => 'all')), get_string("showall", "", $totalcount));
f520dcd9 173 echo "</p></center>";
837d548e 174 } else if ($perpage === 'all') {
f520dcd9 175 echo "<center><p>";
837d548e
MG
176 echo html_writer::link($url->out(false, array('perpage' => $CFG->coursesperpage)),
177 get_string("showperpage", "", $CFG->coursesperpage));
f520dcd9
SH
178 echo "</p></center>";
179 }
7266bd3e 180}