navigation MDL-20651 Added config setting to hide course categories on the navigation
[moodle.git] / course / search.php
CommitLineData
24f2b65a 1<?php // $Id$
38a10939 2
3/// Displays external information about a course
4
5 require_once("../config.php");
6 require_once("lib.php");
7
616ad119 8 $search = optional_param('search', '', PARAM_RAW); // search words
9 $page = optional_param('page', 0, PARAM_INT); // which page to show
10 $perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
11 $moveto = optional_param('moveto', 0, PARAM_INT); // move to category
12 $edit = optional_param('edit', -1, PARAM_BOOL);
13 $hide = optional_param('hide', 0, PARAM_INT);
14 $show = optional_param('show', 0, PARAM_INT);
15 $blocklist = optional_param('blocklist', 0, PARAM_INT);
16 $modulelist= optional_param('modulelist', '', PARAM_ALPHAEXT);
6c843bbc 17
18 $PAGE->set_url('course/search.php', compact('search', 'page', 'perpage', 'blocklist', 'modulelist', 'edit'));
19 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
9cc78ee1 20 $search = trim(strip_tags($search)); // trim & clean raw searched string
38a10939 21
a8b56716 22 if ($search) {
23 $searchterms = explode(" ", $search); // Search for words independently
24 foreach ($searchterms as $key => $searchterm) {
25 if (strlen($searchterm) < 2) {
26 unset($searchterms[$key]);
27 }
28 }
29 $search = trim(implode(" ", $searchterms));
30 }
31
38a10939 32 $site = get_site();
33
680a65a0 34 $urlparams = array();
35 foreach (array('search', 'page', 'blocklist', 'modulelist') as $param) {
36 if (!empty($$param)) {
37 $urlparams[$param] = $$param;
38 }
39 }
40 if ($perpage != 10) {
41 $urlparams['perpage'] = $perpage;
42 }
43 $PAGE->set_url('course/search.php', $urlparams);
44 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
45
4f006bc1 46 if ($CFG->forcelogin) {
47 require_login();
48 }
49
680a65a0 50 if (can_edit_in_category()) {
cd7d03fa 51 if ($edit !== -1) {
830dd6e9 52 $USER->editing = $edit;
861efb19 53 }
830dd6e9 54 $adminediting = $PAGE->user_is_editing();
04c53106 55 } else {
56 $adminediting = false;
0be6f678 57 }
861efb19 58
fa18e81b 59/// Editing functions
8e480396 60 if (has_capability('moodle/course:visibility', get_context_instance(CONTEXT_SYSTEM))) {
fa18e81b 61 /// Hide or show a course
cd7d03fa 62 if ($hide or $show and confirm_sesskey()) {
63 if ($hide) {
6bb08163 64 $course = $DB->get_record("course", array("id"=>$hide));
fa18e81b 65 $visible = 0;
66 } else {
6bb08163 67 $course = $DB->get_record("course", array("id"=>$show));
fa18e81b 68 $visible = 1;
69 }
70 if ($course) {
6bb08163 71 if (! $DB->set_field("course", "visible", $visible, array("id"=>$course->id))) {
e6db3026 72 echo $OUTPUT->notification("Could not update that course!");
fa18e81b 73 }
74 }
75 }
fa18e81b 76 }
77
8e480396 78 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM)) && $perpage != 99999) {
861efb19 79 $perpage = 30;
80 }
81
38a10939 82 $displaylist = array();
83 $parentlist = array();
8ed5dd63 84 make_categories_list($displaylist, $parentlist);
38a10939 85
86 $strcourses = get_string("courses");
c571f3fc 87 $strsearch = get_string("search");
38a10939 88 $strsearchresults = get_string("searchresults");
89 $strcategory = get_string("category");
861efb19 90 $strselect = get_string("select");
91 $strselectall = get_string("selectall");
92 $strdeselectall = get_string("deselectall");
93 $stredit = get_string("edit");
616ad119 94 $strfrontpage = get_string('frontpage', 'admin');
594bb20b 95 $strnovalidcourses = get_string('novalidcourses');
38a10939 96
616ad119 97 if (empty($search) and empty($blocklist) and empty($modulelist)) {
91152a35 98 $PAGE->navbar->add($strcourses, new moodle_url($CFG->wwwroot.'/course/index.php'));
0a122046 99 $PAGE->navbar->add($strsearch);
100 $PAGE->set_title("$site->fullname : $strsearch");
101 $PAGE->set_heading($site->fullname);
0be6f678 102
0a122046 103 echo $OUTPUT->header();
e6db3026 104 echo $OUTPUT->box_start();
a8b56716 105 echo "<center>";
106 echo "<br />";
107 print_course_search("", false, "plain");
108 echo "<br /><p>";
109 print_string("searchhelp");
110 echo "</p>";
111 echo "</center>";
e6db3026 112 echo $OUTPUT->box_end();
d60c1124 113 echo $OUTPUT->footer();
c571f3fc 114 exit;
115 }
116
294ce987 117 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
6bb08163 118 if (! $destcategory = $DB->get_record("course_categories", array("id"=>$data->moveto))) {
f581f8d6 119 print_error('cannotfindcategory', '', '', $data->moveto);
861efb19 120 }
0be6f678 121
122 $courses = array();
861efb19 123 foreach ( $data as $key => $value ) {
124 if (preg_match('/^c\d+$/', $key)) {
125 array_push($courses, substr($key, 1));
126 }
127 }
128 move_courses($courses, $data->moveto);
129 }
130
616ad119 131 // get list of courses containing blocks if required
132 if (!empty($blocklist) and confirm_sesskey()) {
f474a4e5 133 $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
134 $courses = $DB->get_recordset_sql("
135 SELECT * FROM {course} WHERE id IN (
136 SELECT DISTINCT ctx.instanceid
137 FROM {context} ctx
138 JOIN {block_instances} bi ON bi.contextid = ctx.id
139 WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
140 array($blockname));
616ad119 141 $courses = array();
f474a4e5 142 foreach ($courses as $course) {
143 $courses[$course->id] = $course;
616ad119 144 }
f474a4e5 145 $totalcount = count($courses);
616ad119 146 }
147 // get list of courses containing modules if required
148 elseif (!empty($modulelist) and confirm_sesskey()) {
149 $modulename = $modulelist;
67da7e65 150 $sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
d0b7da69 151 ." WHERE module.course=c.id";
616ad119 152
d0b7da69 153 $courseids = $DB->get_records_sql($sql);
e48580bd 154 $courses = array();
155 if (!empty($courseids)) {
156 $firstcourse = $page*$perpage;
157 $lastcourse = $page*$perpage + $perpage -1;
158 $i = 0;
159 foreach ($courseids as $courseid) {
160 if ($i>= $firstcourse && $i<=$lastcourse) {
161 $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
162 }
163 $i++;
616ad119 164 }
e48580bd 165 $totalcount = count($courseids);
166 }
167 else {
168 $totalcount = 0;
616ad119 169 }
170 }
171 else {
04c53106 172 $courses = get_courses_search($searchterms, "fullname ASC",
026579bb 173 $page, $perpage, $totalcount);
616ad119 174 }
861efb19 175
a8b56716 176 $searchform = print_course_search($search, true, "navbar");
177
8e480396 178 if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
c351150f 179 $searchform = '';
180 // not sure if this capability is the best here
181 if (has_capability('moodle/category:manage', get_context_instance(CONTEXT_SYSTEM))) {
182 if ($PAGE->user_is_editing()) {
183 $string = get_string("turneditingoff");
184 $edit = "off";
185 $perpage = 30;
186 } else {
187 $string = get_string("turneditingon");
188 $edit = "on";
189 }
190
191 $form = new html_form();
192 $form->url = new moodle_url("$CFG->wwwroot/course/search.php", array(
193 'edit' => $edit,
194 'sesskey' => sesskey(),
195 'search' => s($search, true),
196 'page' => $page,
197 'perpage' => $perpage));
198 $form->method = 'get';
199 $form->button->text = s($string);
200 $searchform = $OUTPUT->button($form);
201 }
861efb19 202 }
861efb19 203
91152a35 204 $PAGE->navbar->add($strcourses, new moodle_url($CFG->wwwroot.'/course/index.php'));
205 $PAGE->navbar->add($strsearch, new moodle_url($CFG->wwwroot.'/course/search.php'));
7d2a0492 206 if (!empty($search)) {
0a122046 207 $PAGE->navbar->add(s($search));
7d2a0492 208 }
0a122046 209 $PAGE->set_title("$site->fullname : $strsearchresults");
210 $PAGE->set_heading($site->fullname);
211 $PAGE->set_button($searchform);
0be6f678 212
0a122046 213 echo $OUTPUT->header();
38a10939 214
38a10939 215 $lastcategory = -1;
861efb19 216 if ($courses) {
7c5286cd 217 echo $OUTPUT->heading("$strsearchresults: $totalcount");
6bb08163 218 $encodedsearch = urlencode($search);
04c53106 219
d0b7da69 220 ///add the module parameter to the paging bar if they exists
221 $modulelink = "";
222 if (!empty($modulelist) and confirm_sesskey()) {
973d2660 223 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
ee0378bd 224 }
0be6f678 225
04c53106 226 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
227
228 if (!$adminediting) {
861efb19 229 foreach ($courses as $course) {
8bdc9cac
SH
230
231 if (isset($course->context)) {
232 $coursecontext = $course->context;
233 } else {
234 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
235 }
236
eb1f8f1d 237 $course->summary .= "<br /><p class=\"category\">";
861efb19 238 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
239 $course->summary .= $displaylist[$course->category];
240 $course->summary .= "</a></p>";
04c53106 241 print_course($course, $search);
c0d8e936 242 $spacer = new html_image();
243 $spacer->height = 5;
244 $spacer->width = 5;
245 echo $OUTPUT->spacer($spacer) . '<br />';
861efb19 246 }
04c53106 247 } else {
248 /// Show editing UI.
be5539d8 249 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
973d2660 250 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
6bb08163 251 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
be5539d8 252 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
253 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
254 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
255 echo "<th scope=\"col\">$strcourses</th>\n";
256 echo "<th scope=\"col\">$strcategory</th>\n";
257 echo "<th scope=\"col\">$strselect</th>\n";
258 echo "<th scope=\"col\">$stredit</th></tr>\n";
a044c05d 259
04c53106 260 foreach ($courses as $course) {
261
026579bb 262 if (isset($course->context)) {
263 $coursecontext = $course->context;
264 } else {
265 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
266 }
616ad119 267
861efb19 268 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
616ad119 269
270 // are we displaying the front page (courseid=1)?
271 if ($course->id == 1) {
be5539d8 272 echo "<tr>\n";
273 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
616ad119 274
275 // can't do anything else with the front page
be5539d8 276 echo " <td>&nbsp;</td>\n"; // category place
277 echo " <td>&nbsp;</td>\n"; // select place
278 echo " <td>&nbsp;</td>\n"; // edit place
279 echo "</tr>\n";
616ad119 280 continue;
281 }
282
be5539d8 283 echo "<tr>\n";
0be6f678 284 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
04c53106 285 . highlight($search, format_string($course->fullname)) . "</a></td>\n";
be5539d8 286 echo "<td>".$displaylist[$course->category]."</td>\n";
287 echo "<td>\n";
0be6f678 288
698fa439 289 // this is ok since this will get inherited from course category context
290 // if it is set
8ed5dd63 291 if (has_capability('moodle/category:manage', $coursecontext)) {
be5539d8 292 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
698fa439 293 } else {
be5539d8 294 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
698fa439 295 }
0be6f678 296
be5539d8 297 echo "</td>\n";
298 echo "<td>\n";
0be6f678 299
698fa439 300 // checks whether user can update course settings
301 if (has_capability('moodle/course:update', $coursecontext)) {
be5539d8 302 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
666e8458 303 " src=\"" . $OUTPUT->old_icon_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
698fa439 304 }
305
306 // checks whether user can do role assignment
a044c05d 307 if (has_capability('moodle/role:assign', $coursecontext)) {
1ed33201 308 echo'<a title="'.get_string('assignroles', 'role').'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$coursecontext->id.'">';
6b608f8f 309 echo '<img src="'.$OUTPUT->old_icon_url('i/roles') . '" class="iconsmall" alt="'.get_string('assignroles', 'role').'" /></a> ' . "\n";
0be6f678 310 }
698fa439 311
312 // checks whether user can delete course
0be6f678 313 if (has_capability('moodle/course:delete', $coursecontext)) {
be5539d8 314 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
666e8458 315 " src=\"" . $OUTPUT->old_icon_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
0be6f678 316 }
698fa439 317
318 // checks whether user can change visibility
319 if (has_capability('moodle/course:visibility', $coursecontext)) {
320 if (!empty($course->visible)) {
973d2660 321 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".
666e8458 322 " src=\"" . $OUTPUT->old_icon_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
698fa439 323 } else {
973d2660 324 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".
666e8458 325 " src=\"" . $OUTPUT->old_icon_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
698fa439 326 }
0be6f678 327 }
698fa439 328
329 // checks whether user can do site backup
330 if (has_capability('moodle/site:backup', $coursecontext)) {
be5539d8 331 echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\">\n<img".
666e8458 332 " src=\"" . $OUTPUT->old_icon_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
861efb19 333 }
0be6f678 334
698fa439 335 // checks whether user can do restore
336 if (has_capability('moodle/site:restore', $coursecontext)) {
be5539d8 337 echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&amp;wdir=/backupdata\">\n<img".
666e8458 338 " src=\"" . $OUTPUT->old_icon_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
698fa439 339 }
340
be5539d8 341 echo "</td>\n</tr>\n";
861efb19 342 }
be5539d8 343 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
861efb19 344 echo "<br />";
345 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
f6b6861d 346 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
7b1f2c82 347 $select = new html_select();
49c8c8d2 348 $select->options = $displaylist;
349 $select->name = "moveto";
350 $select->label = get_string("moveselectedcoursesto");
351 $select->add_action('change', 'submit_form_by_id', array('id' => 'movecourses'));
352 echo $OUTPUT->select($select);
be5539d8 353 echo "</td>\n</tr>\n";
354 echo "</table>\n</form>";
861efb19 355
38a10939 356 }
357
d0b7da69 358 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
a8b56716 359
38a10939 360 } else {
594bb20b 361 if (!empty($search)) {
7c5286cd 362 echo $OUTPUT->heading(get_string("nocoursesfound", s($search)));
594bb20b 363 }
364 else {
7c5286cd 365 echo $OUTPUT->heading( $strnovalidcourses );
594bb20b 366 }
38a10939 367 }
368
369 echo "<br /><br />";
370
371 print_course_search($search);
372
d60c1124 373 echo $OUTPUT->footer();
38a10939 374
d0b7da69 375 /**
376 * Print a list navigation bar
377 * Display page numbers, and a link for displaying all entries
378 * @param integer $totalcount - number of entry to display
379 * @param integer $page - page number
380 * @param integer $perpage - number of entry per page
381 * @param string $encodedsearch
382 * @param string $modulelink - module name
383 */
384 function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
176e8420 385 global $OUTPUT;
386 $pagingbar = moodle_paging_bar::make($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
176e8420 387 echo $OUTPUT->paging_bar($pagingbar);
d0b7da69 388
389 //display
390 if ($perpage != 99999 && $totalcount > $perpage) {
391 echo "<center><p>";
392 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
393 echo "</p></center>";
394 }
395 }
38a10939 396
397?>