Merge branch 'wip-mdl-31640-new' of git://github.com/rajeshtaneja/moodle
[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
506cbfd7
RT
18 // List of minimum capabilities which user need to have for editing/moving course
19 $capabilities = array('moodle/course:create', 'moodle/category:manage');
20
21 // List of category id's in which current user has course:create and category:manage capability.
22 $usercatlist = array();
38a10939 23
506cbfd7
RT
24 // List of parent category id's
25 $catparentlist = array();
26
27 //Populate usercatlist with list of category id's with required capabilities.
28 make_categories_list($usercatlist, $catparentlist, $capabilities);
29
30 $search = trim(strip_tags($search)); // trim & clean raw searched string
a8b56716 31 if ($search) {
32 $searchterms = explode(" ", $search); // Search for words independently
33 foreach ($searchterms as $key => $searchterm) {
34 if (strlen($searchterm) < 2) {
35 unset($searchterms[$key]);
36 }
37 }
38 $search = trim(implode(" ", $searchterms));
39 }
40
38a10939 41 $site = get_site();
42
680a65a0 43 $urlparams = array();
506cbfd7 44 foreach (array('search', 'page', 'blocklist', 'modulelist', 'edit') as $param) {
680a65a0 45 if (!empty($$param)) {
46 $urlparams[$param] = $$param;
47 }
48 }
49 if ($perpage != 10) {
50 $urlparams['perpage'] = $perpage;
51 }
a6855934 52 $PAGE->set_url('/course/search.php', $urlparams);
fdfe7064 53 $PAGE->set_context(context_system::instance());
f53f582d 54 $PAGE->set_pagelayout('standard');
680a65a0 55
4f006bc1 56 if ($CFG->forcelogin) {
57 require_login();
58 }
59
506cbfd7
RT
60 //Editing is possible if user have system or category level create and manage capability
61 if (can_edit_in_category() || !empty($usercatlist)) {
cd7d03fa 62 if ($edit !== -1) {
830dd6e9 63 $USER->editing = $edit;
861efb19 64 }
830dd6e9 65 $adminediting = $PAGE->user_is_editing();
fdfe7064
RT
66
67 // Set perpage if user can edit in category
68 if ($perpage != 99999) {
69 $perpage = 30;
70 }
04c53106 71 } else {
72 $adminediting = false;
0be6f678 73 }
861efb19 74
fa18e81b 75/// Editing functions
fdfe7064 76 if (has_capability('moodle/course:visibility', context_system::instance())) {
fa18e81b 77 /// Hide or show a course
cd7d03fa 78 if ($hide or $show and confirm_sesskey()) {
79 if ($hide) {
6bb08163 80 $course = $DB->get_record("course", array("id"=>$hide));
fa18e81b 81 $visible = 0;
82 } else {
6bb08163 83 $course = $DB->get_record("course", array("id"=>$show));
fa18e81b 84 $visible = 1;
85 }
86 if ($course) {
f685e830 87 $DB->set_field("course", "visible", $visible, array("id"=>$course->id));
fa18e81b 88 }
89 }
fa18e81b 90 }
91
38a10939 92 $displaylist = array();
93 $parentlist = array();
8ed5dd63 94 make_categories_list($displaylist, $parentlist);
38a10939 95
96 $strcourses = get_string("courses");
c571f3fc 97 $strsearch = get_string("search");
38a10939 98 $strsearchresults = get_string("searchresults");
99 $strcategory = get_string("category");
861efb19 100 $strselect = get_string("select");
101 $strselectall = get_string("selectall");
102 $strdeselectall = get_string("deselectall");
103 $stredit = get_string("edit");
616ad119 104 $strfrontpage = get_string('frontpage', 'admin');
594bb20b 105 $strnovalidcourses = get_string('novalidcourses');
38a10939 106
506cbfd7 107 if (empty($search) and empty($blocklist) and empty($modulelist) and empty($moveto) and ($edit != -1)) {
a6855934 108 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
0a122046 109 $PAGE->navbar->add($strsearch);
110 $PAGE->set_title("$site->fullname : $strsearch");
111 $PAGE->set_heading($site->fullname);
0be6f678 112
0a122046 113 echo $OUTPUT->header();
e6db3026 114 echo $OUTPUT->box_start();
a8b56716 115 echo "<center>";
116 echo "<br />";
117 print_course_search("", false, "plain");
118 echo "<br /><p>";
119 print_string("searchhelp");
120 echo "</p>";
121 echo "</center>";
e6db3026 122 echo $OUTPUT->box_end();
d60c1124 123 echo $OUTPUT->footer();
c571f3fc 124 exit;
125 }
126
506cbfd7 127 $courses = array();
294ce987 128 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
506cbfd7
RT
129 if (!$destcategory = $DB->get_record("course_categories", array("id" => $moveto))) {
130 print_error('cannotfindcategory', '', '', $moveto);
861efb19 131 }
0be6f678 132
506cbfd7 133 //User should have manage and create capablity on destination category.
fdfe7064
RT
134 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
135 require_capability('moodle/course:create', context_coursecat::instance($moveto));
506cbfd7 136
861efb19 137 foreach ( $data as $key => $value ) {
138 if (preg_match('/^c\d+$/', $key)) {
506cbfd7
RT
139 $courseid = substr($key, 1);
140 // user must have category:manage and course:create capability for the course to be moved.
fdfe7064
RT
141 $coursecontext = context_course::instance($courseid);
142 foreach ($capabilities as $capability) {
143 require_capability($capability, $coursecontext);
506cbfd7 144 array_push($courses, $courseid);
506cbfd7 145 }
861efb19 146 }
147 }
506cbfd7 148 move_courses($courses, $moveto);
861efb19 149 }
150
616ad119 151 // get list of courses containing blocks if required
152 if (!empty($blocklist) and confirm_sesskey()) {
f474a4e5 153 $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
ba138d47 154 $courses = array();
155 $courses = $DB->get_records_sql("
f474a4e5 156 SELECT * FROM {course} WHERE id IN (
157 SELECT DISTINCT ctx.instanceid
158 FROM {context} ctx
ba138d47 159 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
f474a4e5 160 WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
161 array($blockname));
cf5b731c
RT
162 $totalcount = count($courses);
163 //Keep only chunk of array which you want to display
164 if ($totalcount > $perpage) {
165 $courses = array_chunk($courses, $perpage, true);
166 $courses = $courses[$page];
167 }
f474a4e5 168 foreach ($courses as $course) {
169 $courses[$course->id] = $course;
616ad119 170 }
506cbfd7 171 } elseif (!empty($modulelist) and confirm_sesskey()) { // get list of courses containing modules
616ad119 172 $modulename = $modulelist;
67da7e65 173 $sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
d0b7da69 174 ." WHERE module.course=c.id";
616ad119 175
d0b7da69 176 $courseids = $DB->get_records_sql($sql);
e48580bd 177 $courses = array();
178 if (!empty($courseids)) {
179 $firstcourse = $page*$perpage;
180 $lastcourse = $page*$perpage + $perpage -1;
181 $i = 0;
182 foreach ($courseids as $courseid) {
183 if ($i>= $firstcourse && $i<=$lastcourse) {
184 $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
185 }
186 $i++;
616ad119 187 }
e48580bd 188 $totalcount = count($courseids);
189 }
190 else {
191 $totalcount = 0;
616ad119 192 }
506cbfd7 193 } else if (!empty($searchterm)) { //Donot do search for empty search request.
04c53106 194 $courses = get_courses_search($searchterms, "fullname ASC",
026579bb 195 $page, $perpage, $totalcount);
616ad119 196 }
861efb19 197
506cbfd7
RT
198 $searchform = '';
199 //Turn editing should be visible if user have system or category level capability
200 if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
201 if ($PAGE->user_is_editing()) {
202 $string = get_string("turneditingoff");
203 $edit = "off";
204 } else {
205 $string = get_string("turneditingon");
206 $edit = "on";
c351150f 207 }
506cbfd7
RT
208 $params = array_merge($urlparams, array('sesskey' => sesskey(), 'edit' => $edit));
209 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", $params);
210 $searchform = $OUTPUT->single_button($aurl, $string, 'get');
211 } else {
212 $searchform = print_course_search($search, true, "navbar");
861efb19 213 }
861efb19 214
a6855934
PS
215 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
216 $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
7d2a0492 217 if (!empty($search)) {
0a122046 218 $PAGE->navbar->add(s($search));
7d2a0492 219 }
0a122046 220 $PAGE->set_title("$site->fullname : $strsearchresults");
221 $PAGE->set_heading($site->fullname);
222 $PAGE->set_button($searchform);
0be6f678 223
0a122046 224 echo $OUTPUT->header();
38a10939 225
38a10939 226 $lastcategory = -1;
861efb19 227 if ($courses) {
7c5286cd 228 echo $OUTPUT->heading("$strsearchresults: $totalcount");
6bb08163 229 $encodedsearch = urlencode($search);
04c53106 230
cf5b731c 231 // add the module/block parameter to the paging bar if they exists
d0b7da69 232 $modulelink = "";
233 if (!empty($modulelist) and confirm_sesskey()) {
973d2660 234 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
cf5b731c
RT
235 } else if (!empty($blocklist) and confirm_sesskey()) {
236 $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
ee0378bd 237 }
0be6f678 238
04c53106 239 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
240
506cbfd7
RT
241 // Show list of courses
242 if (!$adminediting) { //Not editing mode
861efb19 243 foreach ($courses as $course) {
506cbfd7
RT
244 // front page don't belong to any category and block can exist.
245 if ($course->category > 0) {
246 $course->summary .= "<br /><p class=\"category\">";
247 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
248 $course->summary .= $displaylist[$course->category];
249 $course->summary .= "</a></p>";
250 }
04c53106 251 print_course($course, $search);
1ba862ec 252 echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
861efb19 253 }
506cbfd7 254 } else { //editing mode
fdfe7064 255 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
973d2660 256 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
6bb08163 257 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
be5539d8 258 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
259 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
fdfe7064
RT
260 if (!empty($modulelist) and confirm_sesskey()) {
261 echo "<input type=\"hidden\" name=\"modulelist\" value=\"$modulelist\" /></div>\n";
262 } else if (!empty($blocklist) and confirm_sesskey()) {
263 echo "<input type=\"hidden\" name=\"blocklist\" value=\"$blocklist\" /></div>\n";
264 }
be5539d8 265 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
266 echo "<th scope=\"col\">$strcourses</th>\n";
267 echo "<th scope=\"col\">$strcategory</th>\n";
268 echo "<th scope=\"col\">$strselect</th>\n";
269 echo "<th scope=\"col\">$stredit</th></tr>\n";
a044c05d 270
04c53106 271 foreach ($courses as $course) {
272
fdfe7064 273 $coursecontext = context_course::instance($course->id);
616ad119 274
861efb19 275 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
616ad119 276
277 // are we displaying the front page (courseid=1)?
278 if ($course->id == 1) {
be5539d8 279 echo "<tr>\n";
280 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
616ad119 281
282 // can't do anything else with the front page
be5539d8 283 echo " <td>&nbsp;</td>\n"; // category place
284 echo " <td>&nbsp;</td>\n"; // select place
285 echo " <td>&nbsp;</td>\n"; // edit place
286 echo "</tr>\n";
616ad119 287 continue;
288 }
289
be5539d8 290 echo "<tr>\n";
0be6f678 291 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
04c53106 292 . highlight($search, format_string($course->fullname)) . "</a></td>\n";
be5539d8 293 echo "<td>".$displaylist[$course->category]."</td>\n";
294 echo "<td>\n";
0be6f678 295
506cbfd7
RT
296 // If user has all required capabilities to move course then show selectable checkbox
297 if (has_all_capabilities($capabilities, $coursecontext)) {
be5539d8 298 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
698fa439 299 } else {
be5539d8 300 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
698fa439 301 }
0be6f678 302
be5539d8 303 echo "</td>\n";
304 echo "<td>\n";
0be6f678 305
698fa439 306 // checks whether user can update course settings
307 if (has_capability('moodle/course:update', $coursecontext)) {
be5539d8 308 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
b5d0cafc 309 " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
698fa439 310 }
311
312 // checks whether user can do role assignment
6163adcc
PS
313 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
314 echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
315 echo '<img src="'.$OUTPUT->pix_url('i/users') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
0be6f678 316 }
698fa439 317
318 // checks whether user can delete course
0be6f678 319 if (has_capability('moodle/course:delete', $coursecontext)) {
be5539d8 320 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
b5d0cafc 321 " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
0be6f678 322 }
698fa439 323
324 // checks whether user can change visibility
325 if (has_capability('moodle/course:visibility', $coursecontext)) {
326 if (!empty($course->visible)) {
973d2660 327 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 328 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
698fa439 329 } else {
973d2660 330 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 331 " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
698fa439 332 }
0be6f678 333 }
698fa439 334
335 // checks whether user can do site backup
d2940003 336 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
87bef8f6
DS
337 $backupurl = new moodle_url('/backup/backup.php', array('id' => $course->id));
338 echo "<a title=\"".get_string("backup")."\" href=\"".$backupurl."\">\n<img".
b5d0cafc 339 " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
861efb19 340 }
0be6f678 341
698fa439 342 // checks whether user can do restore
d2940003 343 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
87bef8f6
DS
344 $restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
345 echo "<a title=\"".get_string("restore")."\" href=\"".$restoreurl."\">\n<img".
b5d0cafc 346 " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
698fa439 347 }
348
be5539d8 349 echo "</td>\n</tr>\n";
861efb19 350 }
be5539d8 351 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
861efb19 352 echo "<br />";
353 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
f6b6861d 354 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
506cbfd7
RT
355 //Select box should only show categories in which user has min capability to move course.
356 echo html_writer::select($usercatlist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid'));
edc28287 357 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
be5539d8 358 echo "</td>\n</tr>\n";
359 echo "</table>\n</form>";
861efb19 360
38a10939 361 }
362
d0b7da69 363 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
a8b56716 364
38a10939 365 } else {
594bb20b 366 if (!empty($search)) {
413f6cc8 367 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
594bb20b 368 }
369 else {
7c5286cd 370 echo $OUTPUT->heading( $strnovalidcourses );
594bb20b 371 }
38a10939 372 }
373
374 echo "<br /><br />";
375
376 print_course_search($search);
377
d60c1124 378 echo $OUTPUT->footer();
38a10939 379
d0b7da69 380 /**
381 * Print a list navigation bar
382 * Display page numbers, and a link for displaying all entries
383 * @param integer $totalcount - number of entry to display
384 * @param integer $page - page number
385 * @param integer $perpage - number of entry per page
386 * @param string $encodedsearch
387 * @param string $modulelink - module name
388 */
389 function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
176e8420 390 global $OUTPUT;
929d7a83 391 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
d0b7da69 392
393 //display
394 if ($perpage != 99999 && $totalcount > $perpage) {
395 echo "<center><p>";
396 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
397 echo "</p></center>";
cf5b731c 398 } else if ($perpage === 99999) {
be09c3f3 399 $defaultperpage = 10;
cf5b731c
RT
400 //If user has course:create or category:manage capability the show 30 records.
401 $capabilities = array('moodle/course:create', 'moodle/category:manage');
fdfe7064 402 if (has_any_capability($capabilities, context_system::instance())) {
be09c3f3 403 $defaultperpage = 30;
cf5b731c
RT
404 }
405
406 echo "<center><p>";
be09c3f3 407 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
cf5b731c 408 echo "</p></center>";
d0b7da69 409 }
410 }
38a10939 411
aa6c1ced 412