MDL-37009 Display courses and categories tree on /course/index.php using new renderer
[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
31$perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
32$moveto = optional_param('moveto', 0, PARAM_INT); // move to category
33$edit = optional_param('edit', -1, PARAM_BOOL);
34$hide = optional_param('hide', 0, PARAM_INT);
35$show = optional_param('show', 0, PARAM_INT);
36$blocklist = optional_param('blocklist', 0, PARAM_INT);
37$modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
38
39// List of minimum capabilities which user need to have for editing/moving course
40$capabilities = array('moodle/course:create', 'moodle/category:manage');
41
4e0b6025
MG
42// Populate usercatlist with list of category id's with course:create and category:manage capabilities.
43$usercatlist = coursecat::make_categories_list($capabilities);
f520dcd9
SH
44
45$search = trim(strip_tags($search)); // trim & clean raw searched string
46if ($search) {
47 $searchterms = explode(" ", $search); // Search for words independently
48 foreach ($searchterms as $key => $searchterm) {
49 if (strlen($searchterm) < 2) {
50 unset($searchterms[$key]);
a8b56716 51 }
a8b56716 52 }
f520dcd9
SH
53 $search = trim(implode(" ", $searchterms));
54}
a8b56716 55
f520dcd9 56$site = get_site();
38a10939 57
f520dcd9
SH
58$urlparams = array();
59foreach (array('search', 'page', 'blocklist', 'modulelist', 'edit') as $param) {
60 if (!empty($$param)) {
61 $urlparams[$param] = $$param;
680a65a0 62 }
f520dcd9
SH
63}
64if ($perpage != 10) {
65 $urlparams['perpage'] = $perpage;
66}
67$PAGE->set_url('/course/search.php', $urlparams);
68$PAGE->set_context(context_system::instance());
69$PAGE->set_pagelayout('standard');
f4b571ab 70$courserenderer = $PAGE->get_renderer('core', 'course');
f520dcd9
SH
71
72if ($CFG->forcelogin) {
73 require_login();
74}
75
76// Editing is possible if user has system or category level create and manage capability
77if (can_edit_in_category() || !empty($usercatlist)) {
78 if ($edit !== -1) {
79 $USER->editing = $edit;
680a65a0 80 }
f520dcd9 81 $adminediting = $PAGE->user_is_editing();
680a65a0 82
f520dcd9
SH
83 // Set perpage if user can edit in category
84 if ($perpage != 99999) {
85 $perpage = 30;
4f006bc1 86 }
f520dcd9
SH
87} else {
88 $adminediting = false;
89}
90
91// Editing functions
92if (has_capability('moodle/course:visibility', context_system::instance())) {
93 // Hide or show a course
94 if (($hide || $show) && confirm_sesskey()) {
95 if ($hide) {
96 $course = $DB->get_record("course", array("id" => $hide));
97 $visible = 0;
98 } else {
99 $course = $DB->get_record("course", array("id" => $show));
100 $visible = 1;
861efb19 101 }
f520dcd9
SH
102 if ($course) {
103 $DB->set_field("course", "visible", $visible, array("id" => $course->id));
fdfe7064 104 }
0be6f678 105 }
f520dcd9
SH
106}
107
4e0b6025 108$displaylist = coursecat::make_categories_list();
f520dcd9
SH
109
110$strcourses = new lang_string("courses");
111$strsearch = new lang_string("search");
112$strsearchresults = new lang_string("searchresults");
113$strcategory = new lang_string("category");
114$strselect = new lang_string("select");
115$strselectall = new lang_string("selectall");
116$strdeselectall = new lang_string("deselectall");
117$stredit = new lang_string("edit");
118$strfrontpage = new lang_string('frontpage', 'admin');
119$strnovalidcourses = new lang_string('novalidcourses');
120
121if (empty($search) and empty($blocklist) and empty($modulelist) and empty($moveto) and ($edit != -1)) {
122 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
123 $PAGE->navbar->add($strsearch);
124 $PAGE->set_title("$site->fullname : $strsearch");
125 $PAGE->set_heading($site->fullname);
861efb19 126
f520dcd9
SH
127 echo $OUTPUT->header();
128 echo $OUTPUT->box_start();
129 echo "<center>";
130 echo "<br />";
f4b571ab 131 echo $courserenderer->course_search_form('', 'plain');
f520dcd9
SH
132 echo "<br /><p>";
133 print_string("searchhelp");
134 echo "</p>";
135 echo "</center>";
136 echo $OUTPUT->box_end();
137 echo $OUTPUT->footer();
138 exit;
139}
140
141$courses = array();
142if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
143 if (!$destcategory = $DB->get_record("course_categories", array("id" => $moveto))) {
144 print_error('cannotfindcategory', '', '', $moveto);
145 }
146
147 // User should have manage and create capablity on destination category.
148 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
149 require_capability('moodle/course:create', context_coursecat::instance($moveto));
150
151 foreach ( $data as $key => $value ) {
152 if (preg_match('/^c\d+$/', $key)) {
153 $courseid = substr($key, 1);
154 // user must have category:manage and course:create capability for the course to be moved.
155 $coursecontext = context_course::instance($courseid);
156 foreach ($capabilities as $capability) {
157 // Require capability here will result in a fatal error should the user not
158 // have the requried category ensuring that no moves occur if they are
159 // trying to move multiple courses.
160 require_capability($capability, $coursecontext);
161 array_push($courses, $courseid);
fa18e81b 162 }
163 }
fa18e81b 164 }
f520dcd9
SH
165 move_courses($courses, $moveto);
166}
fa18e81b 167
f520dcd9
SH
168// get list of courses containing blocks if required
169if (!empty($blocklist) and confirm_sesskey()) {
170 $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
171 $courses = array();
31f40864 172 list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
22898af7 173 $sql = "SELECT c.* $select FROM {course} c
31f40864
TL
174 $join JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
175 WHERE bi.blockname = ?";
176 $courses = $DB->get_records_sql($sql, array($blockname));
f520dcd9
SH
177 $totalcount = count($courses);
178 // Keep only chunk of array which you want to display
179 if ($totalcount > $perpage) {
180 $courses = array_chunk($courses, $perpage, true);
181 $courses = $courses[$page];
182 }
183 foreach ($courses as $course) {
184 $courses[$course->id] = $course;
c571f3fc 185 }
f520dcd9
SH
186} elseif (!empty($modulelist) and confirm_sesskey()) { // get list of courses containing modules
187 $modulename = $modulelist;
31f40864
TL
188 list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
189 $sql = "SELECT c.* $select FROM {course} c $join
190 WHERE c.id IN (SELECT DISTINCT cc.id FROM {".$modulelist."} module, {course} cc
191 WHERE module.course = cc.id)";
192 $courselist = $DB->get_records_sql($sql);
506cbfd7 193 $courses = array();
31f40864 194 if (!empty($courselist)) {
f520dcd9
SH
195 $firstcourse = $page*$perpage;
196 $lastcourse = $page*$perpage + $perpage -1;
197 $i = 0;
31f40864 198 foreach ($courselist as $course) {
f520dcd9 199 if ($i >= $firstcourse && $i <= $lastcourse) {
31f40864 200 $courses[$course->id] = $course;
861efb19 201 }
f520dcd9 202 $i++;
861efb19 203 }
616ad119 204 }
31f40864 205 $totalcount = count($courselist);
f520dcd9
SH
206} else if (!empty($searchterm)) {
207 // Donot do search for empty search request.
208 $courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount);
209}
210
211$searchform = '';
212// Turn editing should be visible if user have system or category level capability
213if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
214 if ($PAGE->user_is_editing()) {
215 $string = new lang_string("turneditingoff");
216 $edit = "off";
506cbfd7 217 } else {
f520dcd9
SH
218 $string = new lang_string("turneditingon");
219 $edit = "on";
861efb19 220 }
f520dcd9
SH
221 $params = array_merge($urlparams, array('sesskey' => sesskey(), 'edit' => $edit));
222 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", $params);
223 $searchform = $OUTPUT->single_button($aurl, $string, 'get');
224} else {
f4b571ab 225 $searchform = $courserenderer->course_search_form($search, 'navbar');
f520dcd9
SH
226}
227
228$PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
229$PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
230if (!empty($search)) {
231 $PAGE->navbar->add(s($search));
232}
233$PAGE->set_title("$site->fullname : $strsearchresults");
234$PAGE->set_heading($site->fullname);
235$PAGE->set_button($searchform);
236
237echo $OUTPUT->header();
238
239$lastcategory = -1;
240if ($courses) {
241 echo $OUTPUT->heading("$strsearchresults: $totalcount");
242 $encodedsearch = urlencode($search);
243
244 // add the module/block parameter to the paging bar if they exists
245 $modulelink = "";
246 if (!empty($modulelist) and confirm_sesskey()) {
247 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
248 } else if (!empty($blocklist) and confirm_sesskey()) {
249 $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
7d2a0492 250 }
0be6f678 251
f520dcd9 252 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
38a10939 253
f520dcd9
SH
254 // Show list of courses
255 if (!$adminediting) { //Not editing mode
f4b571ab 256 echo $courserenderer->courses_list($courses, $search, true);
f520dcd9
SH
257 } else {
258 // Editing mode
259 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
260 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
261 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
262 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
263 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
d0b7da69 264 if (!empty($modulelist) and confirm_sesskey()) {
f520dcd9 265 echo "<input type=\"hidden\" name=\"modulelist\" value=\"$modulelist\" /></div>\n";
cf5b731c 266 } else if (!empty($blocklist) and confirm_sesskey()) {
f520dcd9 267 echo "<input type=\"hidden\" name=\"blocklist\" value=\"$blocklist\" /></div>\n";
ee0378bd 268 }
eb610eb9 269 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generaltable boxaligncenter\">\n<tr>\n";
f520dcd9
SH
270 echo "<th scope=\"col\">$strcourses</th>\n";
271 echo "<th scope=\"col\">$strcategory</th>\n";
272 echo "<th scope=\"col\">$strselect</th>\n";
273 echo "<th scope=\"col\">$stredit</th></tr>\n";
0be6f678 274
f520dcd9 275 foreach ($courses as $course) {
616ad119 276
31f40864 277 context_helper::preload_from_record($course);
f520dcd9 278 $coursecontext = context_course::instance($course->id);
616ad119 279
f520dcd9 280 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
616ad119 281
f520dcd9
SH
282 // are we displaying the front page (courseid=1)?
283 if ($course->id == 1) {
be5539d8 284 echo "<tr>\n";
f520dcd9
SH
285 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
286
287 // can't do anything else with the front page
288 echo " <td>&nbsp;</td>\n"; // category place
289 echo " <td>&nbsp;</td>\n"; // select place
290 echo " <td>&nbsp;</td>\n"; // edit place
291 echo "</tr>\n";
292 continue;
293 }
0be6f678 294
f520dcd9
SH
295 echo "<tr>\n";
296 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
31f40864 297 . highlight($search, $coursecontext->get_context_name(false)) . "</a></td>\n";
f520dcd9
SH
298 echo "<td>".$displaylist[$course->category]."</td>\n";
299 echo "<td>\n";
0be6f678 300
f520dcd9
SH
301 // If user has all required capabilities to move course then show selectable checkbox
302 if (has_all_capabilities($capabilities, $coursecontext)) {
303 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
304 } else {
305 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
306 }
698fa439 307
f520dcd9
SH
308 echo "</td>\n";
309 echo "<td>\n";
698fa439 310
f520dcd9
SH
311 // checks whether user can update course settings
312 if (has_capability('moodle/course:update', $coursecontext)) {
313 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
314 " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
315 }
698fa439 316
f520dcd9
SH
317 // checks whether user can do role assignment
318 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
319 echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
c42651d6 320 echo '<img src="'.$OUTPUT->pix_url('i/enrolusers') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
f520dcd9 321 }
698fa439 322
f520dcd9
SH
323 // checks whether user can delete course
324 if (has_capability('moodle/course:delete', $coursecontext)) {
325 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
326 " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
327 }
0be6f678 328
f520dcd9
SH
329 // checks whether user can change visibility
330 if (has_capability('moodle/course:visibility', $coursecontext)) {
331 if (!empty($course->visible)) {
332 echo "<a title=\"".get_string("hide")."\" href=\"search.php?search=$encodedsearch&amp;perpage=$perpage&amp;page=$page&amp;hide=$course->id&amp;sesskey=".sesskey()."\">\n<img".
333 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
334 } else {
335 echo "<a title=\"".get_string("show")."\" href=\"search.php?search=$encodedsearch&amp;perpage=$perpage&amp;page=$page&amp;show=$course->id&amp;sesskey=".sesskey()."\">\n<img".
336 " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
698fa439 337 }
861efb19 338 }
861efb19 339
f520dcd9
SH
340 // checks whether user can do site backup
341 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
342 $backupurl = new moodle_url('/backup/backup.php', array('id' => $course->id));
343 echo "<a title=\"".get_string("backup")."\" href=\"".$backupurl."\">\n<img".
344 " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
345 }
38a10939 346
f520dcd9
SH
347 // checks whether user can do restore
348 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
349 $restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
350 echo "<a title=\"".get_string("restore")."\" href=\"".$restoreurl."\">\n<img".
351 " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
352 }
a8b56716 353
f520dcd9 354 echo "</td>\n</tr>\n";
594bb20b 355 }
f520dcd9
SH
356 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
357 echo "<br />";
358 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
359 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
360 // Select box should only show categories in which user has min capability to move course.
70ad5136 361 echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
7266bd3e
ARN
362 echo html_writer::select($usercatlist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid', 'class' => 'autosubmit'));
363 $PAGE->requires->yui_module('moodle-core-formautosubmit',
364 'M.core.init_formautosubmit',
365 array(array('selectid' => 'movetoid', 'nothing' => false))
366 );
f520dcd9
SH
367 echo "</td>\n</tr>\n";
368 echo "</table>\n</form>";
38a10939 369
f520dcd9 370 }
38a10939 371
f520dcd9 372 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
cf5b731c 373
f520dcd9
SH
374} else {
375 if (!empty($search)) {
376 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
d0b7da69 377 }
f520dcd9
SH
378 else {
379 echo $OUTPUT->heading($strnovalidcourses);
380 }
381}
382
383echo "<br /><br />";
384
f4b571ab 385echo $courserenderer->course_search_form($search);
f520dcd9
SH
386
387echo $OUTPUT->footer();
388
389/**
390 * Print a list navigation bar
391 * Display page numbers, and a link for displaying all entries
392 * @param int $totalcount number of entry to display
393 * @param int $page page number
394 * @param int $perpage number of entry per page
395 * @param string $encodedsearch
396 * @param string $modulelink module name
397 */
398function print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink) {
399 global $OUTPUT;
400 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
401
402 // display
403 if ($perpage != 99999 && $totalcount > $perpage) {
404 echo "<center><p>";
405 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
406 echo "</p></center>";
407 } else if ($perpage === 99999) {
408 $defaultperpage = 10;
409 // If user has course:create or category:manage capability the show 30 records.
410 $capabilities = array('moodle/course:create', 'moodle/category:manage');
411 if (has_any_capability($capabilities, context_system::instance())) {
412 $defaultperpage = 30;
413 }
38a10939 414
f520dcd9
SH
415 echo "<center><p>";
416 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
417 echo "</p></center>";
418 }
7266bd3e 419}