MDL-30888 course-search: Fixed typo
[moodle.git] / course / search.php
CommitLineData
aa6c1ced 1<?php
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);
aff24313 16 $modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
aa6c1ced 17
a6855934 18 $PAGE->set_url('/course/search.php', compact('search', 'page', 'perpage', 'blocklist', 'modulelist', 'edit'));
6c843bbc 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 }
a6855934 43 $PAGE->set_url('/course/search.php', $urlparams);
680a65a0 44 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
f53f582d 45 $PAGE->set_pagelayout('standard');
680a65a0 46
4f006bc1 47 if ($CFG->forcelogin) {
48 require_login();
49 }
50
680a65a0 51 if (can_edit_in_category()) {
cd7d03fa 52 if ($edit !== -1) {
830dd6e9 53 $USER->editing = $edit;
861efb19 54 }
830dd6e9 55 $adminediting = $PAGE->user_is_editing();
04c53106 56 } else {
57 $adminediting = false;
0be6f678 58 }
861efb19 59
fa18e81b 60/// Editing functions
8e480396 61 if (has_capability('moodle/course:visibility', get_context_instance(CONTEXT_SYSTEM))) {
fa18e81b 62 /// Hide or show a course
cd7d03fa 63 if ($hide or $show and confirm_sesskey()) {
64 if ($hide) {
6bb08163 65 $course = $DB->get_record("course", array("id"=>$hide));
fa18e81b 66 $visible = 0;
67 } else {
6bb08163 68 $course = $DB->get_record("course", array("id"=>$show));
fa18e81b 69 $visible = 1;
70 }
71 if ($course) {
f685e830 72 $DB->set_field("course", "visible", $visible, array("id"=>$course->id));
fa18e81b 73 }
74 }
fa18e81b 75 }
76
cf5b731c
RT
77 $capabilities = array('moodle/course:create', 'moodle/category:manage');
78 if (has_any_capability($capabilities, 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)) {
a6855934 98 $PAGE->navbar->add($strcourses, new moodle_url('/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));
ba138d47 134 $courses = array();
135 $courses = $DB->get_records_sql("
f474a4e5 136 SELECT * FROM {course} WHERE id IN (
137 SELECT DISTINCT ctx.instanceid
138 FROM {context} ctx
ba138d47 139 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
f474a4e5 140 WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
141 array($blockname));
cf5b731c
RT
142 $totalcount = count($courses);
143 //Keep only chunk of array which you want to display
144 if ($totalcount > $perpage) {
145 $courses = array_chunk($courses, $perpage, true);
146 $courses = $courses[$page];
147 }
f474a4e5 148 foreach ($courses as $course) {
149 $courses[$course->id] = $course;
616ad119 150 }
151 }
152 // get list of courses containing modules if required
153 elseif (!empty($modulelist) and confirm_sesskey()) {
154 $modulename = $modulelist;
67da7e65 155 $sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
d0b7da69 156 ." WHERE module.course=c.id";
616ad119 157
d0b7da69 158 $courseids = $DB->get_records_sql($sql);
e48580bd 159 $courses = array();
160 if (!empty($courseids)) {
161 $firstcourse = $page*$perpage;
162 $lastcourse = $page*$perpage + $perpage -1;
163 $i = 0;
164 foreach ($courseids as $courseid) {
165 if ($i>= $firstcourse && $i<=$lastcourse) {
166 $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
167 }
168 $i++;
616ad119 169 }
e48580bd 170 $totalcount = count($courseids);
171 }
172 else {
173 $totalcount = 0;
616ad119 174 }
175 }
176 else {
04c53106 177 $courses = get_courses_search($searchterms, "fullname ASC",
026579bb 178 $page, $perpage, $totalcount);
616ad119 179 }
861efb19 180
a8b56716 181 $searchform = print_course_search($search, true, "navbar");
182
8e480396 183 if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
c351150f 184 $searchform = '';
185 // not sure if this capability is the best here
186 if (has_capability('moodle/category:manage', get_context_instance(CONTEXT_SYSTEM))) {
187 if ($PAGE->user_is_editing()) {
188 $string = get_string("turneditingoff");
189 $edit = "off";
c351150f 190 } else {
191 $string = get_string("turneditingon");
192 $edit = "on";
193 }
194
5c2ed7e2 195 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", array(
aa6c1ced 196 'edit' => $edit,
c351150f 197 'sesskey' => sesskey(),
5c2ed7e2 198 'search' => $search,
c351150f 199 'page' => $page,
200 'perpage' => $perpage));
5c2ed7e2 201 $searchform = $OUTPUT->single_button($aurl, $string, 'get');
c351150f 202 }
861efb19 203 }
861efb19 204
a6855934
PS
205 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
206 $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
7d2a0492 207 if (!empty($search)) {
0a122046 208 $PAGE->navbar->add(s($search));
7d2a0492 209 }
0a122046 210 $PAGE->set_title("$site->fullname : $strsearchresults");
211 $PAGE->set_heading($site->fullname);
212 $PAGE->set_button($searchform);
0be6f678 213
0a122046 214 echo $OUTPUT->header();
38a10939 215
38a10939 216 $lastcategory = -1;
861efb19 217 if ($courses) {
7c5286cd 218 echo $OUTPUT->heading("$strsearchresults: $totalcount");
6bb08163 219 $encodedsearch = urlencode($search);
04c53106 220
cf5b731c 221 // add the module/block parameter to the paging bar if they exists
d0b7da69 222 $modulelink = "";
223 if (!empty($modulelist) and confirm_sesskey()) {
973d2660 224 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
cf5b731c
RT
225 } else if (!empty($blocklist) and confirm_sesskey()) {
226 $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
ee0378bd 227 }
0be6f678 228
04c53106 229 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
230
231 if (!$adminediting) {
861efb19 232 foreach ($courses as $course) {
8bdc9cac 233
4f0c2d00 234 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
8bdc9cac 235
eb1f8f1d 236 $course->summary .= "<br /><p class=\"category\">";
861efb19 237 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
238 $course->summary .= $displaylist[$course->category];
239 $course->summary .= "</a></p>";
04c53106 240 print_course($course, $search);
1ba862ec 241 echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
861efb19 242 }
04c53106 243 } else {
244 /// Show editing UI.
be5539d8 245 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
973d2660 246 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
6bb08163 247 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
be5539d8 248 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
249 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
250 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
251 echo "<th scope=\"col\">$strcourses</th>\n";
252 echo "<th scope=\"col\">$strcategory</th>\n";
253 echo "<th scope=\"col\">$strselect</th>\n";
254 echo "<th scope=\"col\">$stredit</th></tr>\n";
a044c05d 255
04c53106 256 foreach ($courses as $course) {
257
4f0c2d00 258 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
616ad119 259
861efb19 260 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
616ad119 261
262 // are we displaying the front page (courseid=1)?
263 if ($course->id == 1) {
be5539d8 264 echo "<tr>\n";
265 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
616ad119 266
267 // can't do anything else with the front page
be5539d8 268 echo " <td>&nbsp;</td>\n"; // category place
269 echo " <td>&nbsp;</td>\n"; // select place
270 echo " <td>&nbsp;</td>\n"; // edit place
271 echo "</tr>\n";
616ad119 272 continue;
273 }
274
be5539d8 275 echo "<tr>\n";
0be6f678 276 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
04c53106 277 . highlight($search, format_string($course->fullname)) . "</a></td>\n";
be5539d8 278 echo "<td>".$displaylist[$course->category]."</td>\n";
279 echo "<td>\n";
0be6f678 280
698fa439 281 // this is ok since this will get inherited from course category context
282 // if it is set
8ed5dd63 283 if (has_capability('moodle/category:manage', $coursecontext)) {
be5539d8 284 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
698fa439 285 } else {
be5539d8 286 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
698fa439 287 }
0be6f678 288
be5539d8 289 echo "</td>\n";
290 echo "<td>\n";
0be6f678 291
698fa439 292 // checks whether user can update course settings
293 if (has_capability('moodle/course:update', $coursecontext)) {
be5539d8 294 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
b5d0cafc 295 " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
698fa439 296 }
297
298 // checks whether user can do role assignment
6163adcc
PS
299 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
300 echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
301 echo '<img src="'.$OUTPUT->pix_url('i/users') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
0be6f678 302 }
698fa439 303
304 // checks whether user can delete course
0be6f678 305 if (has_capability('moodle/course:delete', $coursecontext)) {
be5539d8 306 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
b5d0cafc 307 " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
0be6f678 308 }
698fa439 309
310 // checks whether user can change visibility
311 if (has_capability('moodle/course:visibility', $coursecontext)) {
312 if (!empty($course->visible)) {
973d2660 313 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".
b5d0cafc 314 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
698fa439 315 } else {
973d2660 316 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".
b5d0cafc 317 " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
698fa439 318 }
0be6f678 319 }
698fa439 320
321 // checks whether user can do site backup
d2940003 322 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
be5539d8 323 echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\">\n<img".
b5d0cafc 324 " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
861efb19 325 }
0be6f678 326
698fa439 327 // checks whether user can do restore
d2940003 328 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
be5539d8 329 echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&amp;wdir=/backupdata\">\n<img".
b5d0cafc 330 " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
698fa439 331 }
332
be5539d8 333 echo "</td>\n</tr>\n";
861efb19 334 }
be5539d8 335 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
861efb19 336 echo "<br />";
337 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
f6b6861d 338 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
edc28287
PS
339 echo html_writer::select($displaylist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid'));
340 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
be5539d8 341 echo "</td>\n</tr>\n";
342 echo "</table>\n</form>";
861efb19 343
38a10939 344 }
345
d0b7da69 346 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
a8b56716 347
38a10939 348 } else {
594bb20b 349 if (!empty($search)) {
413f6cc8 350 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
594bb20b 351 }
352 else {
7c5286cd 353 echo $OUTPUT->heading( $strnovalidcourses );
594bb20b 354 }
38a10939 355 }
356
357 echo "<br /><br />";
358
359 print_course_search($search);
360
d60c1124 361 echo $OUTPUT->footer();
38a10939 362
d0b7da69 363 /**
364 * Print a list navigation bar
365 * Display page numbers, and a link for displaying all entries
366 * @param integer $totalcount - number of entry to display
367 * @param integer $page - page number
368 * @param integer $perpage - number of entry per page
369 * @param string $encodedsearch
370 * @param string $modulelink - module name
371 */
372 function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
176e8420 373 global $OUTPUT;
929d7a83 374 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
d0b7da69 375
376 //display
377 if ($perpage != 99999 && $totalcount > $perpage) {
378 echo "<center><p>";
379 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
380 echo "</p></center>";
cf5b731c 381 } else if ($perpage === 99999) {
be09c3f3 382 $defaultperpage = 10;
cf5b731c
RT
383 //If user has course:create or category:manage capability the show 30 records.
384 $capabilities = array('moodle/course:create', 'moodle/category:manage');
385 if (has_any_capability($capabilities, get_context_instance(CONTEXT_SYSTEM))) {
be09c3f3 386 $defaultperpage = 30;
cf5b731c
RT
387 }
388
389 echo "<center><p>";
be09c3f3 390 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
cf5b731c 391 echo "</p></center>";
d0b7da69 392 }
393 }
38a10939 394
aa6c1ced 395