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