MDL-36259 - change case of table alias to match definition
[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();
defbd5dd 178 list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
568349fe 179 $sql = "SELECT c.* $select FROM {course} c
defbd5dd
TL
180 $join JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
181 WHERE bi.blockname = ?";
182 $courses = $DB->get_records_sql($sql, array($blockname));
f520dcd9
SH
183 $totalcount = count($courses);
184 // Keep only chunk of array which you want to display
185 if ($totalcount > $perpage) {
186 $courses = array_chunk($courses, $perpage, true);
187 $courses = $courses[$page];
188 }
189 foreach ($courses as $course) {
190 $courses[$course->id] = $course;
c571f3fc 191 }
f520dcd9
SH
192} elseif (!empty($modulelist) and confirm_sesskey()) { // get list of courses containing modules
193 $modulename = $modulelist;
defbd5dd
TL
194 list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
195 $sql = "SELECT c.* $select FROM {course} c $join
196 WHERE c.id IN (SELECT DISTINCT cc.id FROM {".$modulelist."} module, {course} cc
197 WHERE module.course = cc.id)";
198 $courselist = $DB->get_records_sql($sql);
506cbfd7 199 $courses = array();
defbd5dd 200 if (!empty($courselist)) {
f520dcd9
SH
201 $firstcourse = $page*$perpage;
202 $lastcourse = $page*$perpage + $perpage -1;
203 $i = 0;
defbd5dd 204 foreach ($courselist as $course) {
f520dcd9 205 if ($i >= $firstcourse && $i <= $lastcourse) {
defbd5dd 206 $courses[$course->id] = $course;
861efb19 207 }
f520dcd9 208 $i++;
861efb19 209 }
616ad119 210 }
defbd5dd 211 $totalcount = count($courselist);
f520dcd9
SH
212} else if (!empty($searchterm)) {
213 // Donot do search for empty search request.
214 $courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount);
215}
216
217$searchform = '';
218// Turn editing should be visible if user have system or category level capability
219if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
220 if ($PAGE->user_is_editing()) {
221 $string = new lang_string("turneditingoff");
222 $edit = "off";
506cbfd7 223 } else {
f520dcd9
SH
224 $string = new lang_string("turneditingon");
225 $edit = "on";
861efb19 226 }
f520dcd9
SH
227 $params = array_merge($urlparams, array('sesskey' => sesskey(), 'edit' => $edit));
228 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", $params);
229 $searchform = $OUTPUT->single_button($aurl, $string, 'get');
230} else {
231 $searchform = print_course_search($search, true, "navbar");
232}
233
234$PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
235$PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
236if (!empty($search)) {
237 $PAGE->navbar->add(s($search));
238}
239$PAGE->set_title("$site->fullname : $strsearchresults");
240$PAGE->set_heading($site->fullname);
241$PAGE->set_button($searchform);
242
243echo $OUTPUT->header();
244
245$lastcategory = -1;
246if ($courses) {
247 echo $OUTPUT->heading("$strsearchresults: $totalcount");
248 $encodedsearch = urlencode($search);
249
250 // add the module/block parameter to the paging bar if they exists
251 $modulelink = "";
252 if (!empty($modulelist) and confirm_sesskey()) {
253 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
254 } else if (!empty($blocklist) and confirm_sesskey()) {
255 $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
7d2a0492 256 }
0be6f678 257
f520dcd9 258 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
38a10939 259
f520dcd9
SH
260 // Show list of courses
261 if (!$adminediting) { //Not editing mode
262 foreach ($courses as $course) {
263 // front page don't belong to any category and block can exist.
264 if ($course->category > 0) {
265 $course->summary .= "<br /><p class=\"category\">";
266 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
267 $course->summary .= $displaylist[$course->category];
268 $course->summary .= "</a></p>";
269 }
270 print_course($course, $search);
271 echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
272 }
273 } else {
274 // Editing mode
275 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
276 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
277 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
278 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
279 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
d0b7da69 280 if (!empty($modulelist) and confirm_sesskey()) {
f520dcd9 281 echo "<input type=\"hidden\" name=\"modulelist\" value=\"$modulelist\" /></div>\n";
cf5b731c 282 } else if (!empty($blocklist) and confirm_sesskey()) {
f520dcd9 283 echo "<input type=\"hidden\" name=\"blocklist\" value=\"$blocklist\" /></div>\n";
ee0378bd 284 }
f520dcd9
SH
285 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
286 echo "<th scope=\"col\">$strcourses</th>\n";
287 echo "<th scope=\"col\">$strcategory</th>\n";
288 echo "<th scope=\"col\">$strselect</th>\n";
289 echo "<th scope=\"col\">$stredit</th></tr>\n";
0be6f678 290
f520dcd9 291 foreach ($courses as $course) {
616ad119 292
defbd5dd 293 context_helper::preload_from_record($course);
f520dcd9 294 $coursecontext = context_course::instance($course->id);
616ad119 295
f520dcd9 296 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
616ad119 297
f520dcd9
SH
298 // are we displaying the front page (courseid=1)?
299 if ($course->id == 1) {
be5539d8 300 echo "<tr>\n";
f520dcd9
SH
301 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
302
303 // can't do anything else with the front page
304 echo " <td>&nbsp;</td>\n"; // category place
305 echo " <td>&nbsp;</td>\n"; // select place
306 echo " <td>&nbsp;</td>\n"; // edit place
307 echo "</tr>\n";
308 continue;
309 }
0be6f678 310
f520dcd9
SH
311 echo "<tr>\n";
312 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
defbd5dd 313 . highlight($search, $coursecontext->get_context_name(false)) . "</a></td>\n";
f520dcd9
SH
314 echo "<td>".$displaylist[$course->category]."</td>\n";
315 echo "<td>\n";
0be6f678 316
f520dcd9
SH
317 // If user has all required capabilities to move course then show selectable checkbox
318 if (has_all_capabilities($capabilities, $coursecontext)) {
319 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
320 } else {
321 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
322 }
698fa439 323
f520dcd9
SH
324 echo "</td>\n";
325 echo "<td>\n";
698fa439 326
f520dcd9
SH
327 // checks whether user can update course settings
328 if (has_capability('moodle/course:update', $coursecontext)) {
329 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
330 " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
331 }
698fa439 332
f520dcd9
SH
333 // checks whether user can do role assignment
334 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
335 echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
336 echo '<img src="'.$OUTPUT->pix_url('i/users') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
337 }
698fa439 338
f520dcd9
SH
339 // checks whether user can delete course
340 if (has_capability('moodle/course:delete', $coursecontext)) {
341 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
342 " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
343 }
0be6f678 344
f520dcd9
SH
345 // checks whether user can change visibility
346 if (has_capability('moodle/course:visibility', $coursecontext)) {
347 if (!empty($course->visible)) {
348 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".
349 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
350 } else {
351 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".
352 " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
698fa439 353 }
861efb19 354 }
861efb19 355
f520dcd9
SH
356 // checks whether user can do site backup
357 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
358 $backupurl = new moodle_url('/backup/backup.php', array('id' => $course->id));
359 echo "<a title=\"".get_string("backup")."\" href=\"".$backupurl."\">\n<img".
360 " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
361 }
38a10939 362
f520dcd9
SH
363 // checks whether user can do restore
364 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
365 $restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
366 echo "<a title=\"".get_string("restore")."\" href=\"".$restoreurl."\">\n<img".
367 " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
368 }
a8b56716 369
f520dcd9 370 echo "</td>\n</tr>\n";
594bb20b 371 }
f520dcd9
SH
372 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
373 echo "<br />";
374 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
375 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
376 // Select box should only show categories in which user has min capability to move course.
0fa95a3c 377 echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
f520dcd9
SH
378 echo html_writer::select($usercatlist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid'));
379 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
380 echo "</td>\n</tr>\n";
381 echo "</table>\n</form>";
38a10939 382
f520dcd9 383 }
38a10939 384
f520dcd9 385 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
cf5b731c 386
f520dcd9
SH
387} else {
388 if (!empty($search)) {
389 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
d0b7da69 390 }
f520dcd9
SH
391 else {
392 echo $OUTPUT->heading($strnovalidcourses);
393 }
394}
395
396echo "<br /><br />";
397
398print_course_search($search);
399
400echo $OUTPUT->footer();
401
402/**
403 * Print a list navigation bar
404 * Display page numbers, and a link for displaying all entries
405 * @param int $totalcount number of entry to display
406 * @param int $page page number
407 * @param int $perpage number of entry per page
408 * @param string $encodedsearch
409 * @param string $modulelink module name
410 */
411function print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink) {
412 global $OUTPUT;
413 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
414
415 // display
416 if ($perpage != 99999 && $totalcount > $perpage) {
417 echo "<center><p>";
418 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
419 echo "</p></center>";
420 } else if ($perpage === 99999) {
421 $defaultperpage = 10;
422 // If user has course:create or category:manage capability the show 30 records.
423 $capabilities = array('moodle/course:create', 'moodle/category:manage');
424 if (has_any_capability($capabilities, context_system::instance())) {
425 $defaultperpage = 30;
426 }
38a10939 427
f520dcd9
SH
428 echo "<center><p>";
429 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
430 echo "</p></center>";
431 }
defbd5dd 432}