weekly release 2.3dev
[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));
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) {
f685e830 71 $DB->set_field("course", "visible", $visible, array("id"=>$course->id));
fa18e81b 72 }
73 }
fa18e81b 74 }
75
8e480396 76 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM)) && $perpage != 99999) {
861efb19 77 $perpage = 30;
78 }
79
38a10939 80 $displaylist = array();
81 $parentlist = array();
8ed5dd63 82 make_categories_list($displaylist, $parentlist);
38a10939 83
84 $strcourses = get_string("courses");
c571f3fc 85 $strsearch = get_string("search");
38a10939 86 $strsearchresults = get_string("searchresults");
87 $strcategory = get_string("category");
861efb19 88 $strselect = get_string("select");
89 $strselectall = get_string("selectall");
90 $strdeselectall = get_string("deselectall");
91 $stredit = get_string("edit");
616ad119 92 $strfrontpage = get_string('frontpage', 'admin');
594bb20b 93 $strnovalidcourses = get_string('novalidcourses');
38a10939 94
616ad119 95 if (empty($search) and empty($blocklist) and empty($modulelist)) {
a6855934 96 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
0a122046 97 $PAGE->navbar->add($strsearch);
98 $PAGE->set_title("$site->fullname : $strsearch");
99 $PAGE->set_heading($site->fullname);
0be6f678 100
0a122046 101 echo $OUTPUT->header();
e6db3026 102 echo $OUTPUT->box_start();
a8b56716 103 echo "<center>";
104 echo "<br />";
105 print_course_search("", false, "plain");
106 echo "<br /><p>";
107 print_string("searchhelp");
108 echo "</p>";
109 echo "</center>";
e6db3026 110 echo $OUTPUT->box_end();
d60c1124 111 echo $OUTPUT->footer();
c571f3fc 112 exit;
113 }
114
294ce987 115 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
6bb08163 116 if (! $destcategory = $DB->get_record("course_categories", array("id"=>$data->moveto))) {
f581f8d6 117 print_error('cannotfindcategory', '', '', $data->moveto);
861efb19 118 }
0be6f678 119
120 $courses = array();
861efb19 121 foreach ( $data as $key => $value ) {
122 if (preg_match('/^c\d+$/', $key)) {
123 array_push($courses, substr($key, 1));
124 }
125 }
126 move_courses($courses, $data->moveto);
127 }
128
616ad119 129 // get list of courses containing blocks if required
130 if (!empty($blocklist) and confirm_sesskey()) {
f474a4e5 131 $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
ba138d47 132 $courses = array();
133 $courses = $DB->get_records_sql("
f474a4e5 134 SELECT * FROM {course} WHERE id IN (
135 SELECT DISTINCT ctx.instanceid
136 FROM {context} ctx
ba138d47 137 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
f474a4e5 138 WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
139 array($blockname));
f474a4e5 140 foreach ($courses as $course) {
141 $courses[$course->id] = $course;
616ad119 142 }
f474a4e5 143 $totalcount = count($courses);
616ad119 144 }
145 // get list of courses containing modules if required
146 elseif (!empty($modulelist) and confirm_sesskey()) {
147 $modulename = $modulelist;
67da7e65 148 $sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
d0b7da69 149 ." WHERE module.course=c.id";
616ad119 150
d0b7da69 151 $courseids = $DB->get_records_sql($sql);
e48580bd 152 $courses = array();
153 if (!empty($courseids)) {
154 $firstcourse = $page*$perpage;
155 $lastcourse = $page*$perpage + $perpage -1;
156 $i = 0;
157 foreach ($courseids as $courseid) {
158 if ($i>= $firstcourse && $i<=$lastcourse) {
159 $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
160 }
161 $i++;
616ad119 162 }
e48580bd 163 $totalcount = count($courseids);
164 }
165 else {
166 $totalcount = 0;
616ad119 167 }
168 }
169 else {
04c53106 170 $courses = get_courses_search($searchterms, "fullname ASC",
026579bb 171 $page, $perpage, $totalcount);
616ad119 172 }
861efb19 173
a8b56716 174 $searchform = print_course_search($search, true, "navbar");
175
8e480396 176 if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
c351150f 177 $searchform = '';
178 // not sure if this capability is the best here
179 if (has_capability('moodle/category:manage', get_context_instance(CONTEXT_SYSTEM))) {
180 if ($PAGE->user_is_editing()) {
181 $string = get_string("turneditingoff");
182 $edit = "off";
183 $perpage = 30;
184 } else {
185 $string = get_string("turneditingon");
186 $edit = "on";
187 }
188
5c2ed7e2 189 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", array(
aa6c1ced 190 'edit' => $edit,
c351150f 191 'sesskey' => sesskey(),
5c2ed7e2 192 'search' => $search,
c351150f 193 'page' => $page,
194 'perpage' => $perpage));
5c2ed7e2 195 $searchform = $OUTPUT->single_button($aurl, $string, 'get');
c351150f 196 }
861efb19 197 }
861efb19 198
a6855934
PS
199 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
200 $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
7d2a0492 201 if (!empty($search)) {
0a122046 202 $PAGE->navbar->add(s($search));
7d2a0492 203 }
0a122046 204 $PAGE->set_title("$site->fullname : $strsearchresults");
205 $PAGE->set_heading($site->fullname);
206 $PAGE->set_button($searchform);
0be6f678 207
0a122046 208 echo $OUTPUT->header();
38a10939 209
38a10939 210 $lastcategory = -1;
861efb19 211 if ($courses) {
7c5286cd 212 echo $OUTPUT->heading("$strsearchresults: $totalcount");
6bb08163 213 $encodedsearch = urlencode($search);
04c53106 214
d0b7da69 215 ///add the module parameter to the paging bar if they exists
216 $modulelink = "";
217 if (!empty($modulelist) and confirm_sesskey()) {
973d2660 218 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
ee0378bd 219 }
0be6f678 220
04c53106 221 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
222
223 if (!$adminediting) {
861efb19 224 foreach ($courses as $course) {
8bdc9cac 225
4f0c2d00 226 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
8bdc9cac 227
eb1f8f1d 228 $course->summary .= "<br /><p class=\"category\">";
861efb19 229 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
230 $course->summary .= $displaylist[$course->category];
231 $course->summary .= "</a></p>";
04c53106 232 print_course($course, $search);
1ba862ec 233 echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
861efb19 234 }
04c53106 235 } else {
236 /// Show editing UI.
be5539d8 237 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
973d2660 238 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
6bb08163 239 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
be5539d8 240 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
241 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
242 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
243 echo "<th scope=\"col\">$strcourses</th>\n";
244 echo "<th scope=\"col\">$strcategory</th>\n";
245 echo "<th scope=\"col\">$strselect</th>\n";
246 echo "<th scope=\"col\">$stredit</th></tr>\n";
a044c05d 247
04c53106 248 foreach ($courses as $course) {
249
4f0c2d00 250 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
616ad119 251
861efb19 252 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
616ad119 253
254 // are we displaying the front page (courseid=1)?
255 if ($course->id == 1) {
be5539d8 256 echo "<tr>\n";
257 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
616ad119 258
259 // can't do anything else with the front page
be5539d8 260 echo " <td>&nbsp;</td>\n"; // category place
261 echo " <td>&nbsp;</td>\n"; // select place
262 echo " <td>&nbsp;</td>\n"; // edit place
263 echo "</tr>\n";
616ad119 264 continue;
265 }
266
be5539d8 267 echo "<tr>\n";
0be6f678 268 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
04c53106 269 . highlight($search, format_string($course->fullname)) . "</a></td>\n";
be5539d8 270 echo "<td>".$displaylist[$course->category]."</td>\n";
271 echo "<td>\n";
0be6f678 272
698fa439 273 // this is ok since this will get inherited from course category context
274 // if it is set
8ed5dd63 275 if (has_capability('moodle/category:manage', $coursecontext)) {
be5539d8 276 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
698fa439 277 } else {
be5539d8 278 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
698fa439 279 }
0be6f678 280
be5539d8 281 echo "</td>\n";
282 echo "<td>\n";
0be6f678 283
698fa439 284 // checks whether user can update course settings
285 if (has_capability('moodle/course:update', $coursecontext)) {
be5539d8 286 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
b5d0cafc 287 " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
698fa439 288 }
289
290 // checks whether user can do role assignment
6163adcc
PS
291 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
292 echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
293 echo '<img src="'.$OUTPUT->pix_url('i/users') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
0be6f678 294 }
698fa439 295
296 // checks whether user can delete course
0be6f678 297 if (has_capability('moodle/course:delete', $coursecontext)) {
be5539d8 298 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
b5d0cafc 299 " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
0be6f678 300 }
698fa439 301
302 // checks whether user can change visibility
303 if (has_capability('moodle/course:visibility', $coursecontext)) {
304 if (!empty($course->visible)) {
973d2660 305 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 306 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
698fa439 307 } else {
973d2660 308 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 309 " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
698fa439 310 }
0be6f678 311 }
698fa439 312
313 // checks whether user can do site backup
d2940003 314 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
be5539d8 315 echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\">\n<img".
b5d0cafc 316 " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
861efb19 317 }
0be6f678 318
698fa439 319 // checks whether user can do restore
d2940003 320 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
be5539d8 321 echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&amp;wdir=/backupdata\">\n<img".
b5d0cafc 322 " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
698fa439 323 }
324
be5539d8 325 echo "</td>\n</tr>\n";
861efb19 326 }
be5539d8 327 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
861efb19 328 echo "<br />";
329 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
f6b6861d 330 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
edc28287
PS
331 echo html_writer::select($displaylist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid'));
332 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
be5539d8 333 echo "</td>\n</tr>\n";
334 echo "</table>\n</form>";
861efb19 335
38a10939 336 }
337
d0b7da69 338 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
a8b56716 339
38a10939 340 } else {
594bb20b 341 if (!empty($search)) {
413f6cc8 342 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
594bb20b 343 }
344 else {
7c5286cd 345 echo $OUTPUT->heading( $strnovalidcourses );
594bb20b 346 }
38a10939 347 }
348
349 echo "<br /><br />";
350
351 print_course_search($search);
352
d60c1124 353 echo $OUTPUT->footer();
38a10939 354
d0b7da69 355 /**
356 * Print a list navigation bar
357 * Display page numbers, and a link for displaying all entries
358 * @param integer $totalcount - number of entry to display
359 * @param integer $page - page number
360 * @param integer $perpage - number of entry per page
361 * @param string $encodedsearch
362 * @param string $modulelink - module name
363 */
364 function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
176e8420 365 global $OUTPUT;
929d7a83 366 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
d0b7da69 367
368 //display
369 if ($perpage != 99999 && $totalcount > $perpage) {
370 echo "<center><p>";
371 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
372 echo "</p></center>";
373 }
374 }
38a10939 375
aa6c1ced 376