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