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