moodle_page: MDL-12212 reimplement user_is_editing, deprecate isediting
[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()) {
119 $blockid = $blocklist;
6bb08163 120 if (!$blocks = $DB->get_records('block_instance', array('blockid'=>$blockid))) {
121 print_error('blockcannotread', '', '', $blockid);
616ad119 122 }
123
124 // run through blocks and get (unique) courses
125 $courses = array();
126 foreach ($blocks as $block) {
127 $courseid = $block->pageid;
deb2988f 128 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
129 // and it should not show up on course search page
130 if ($courseid==0 || $block->pagetype != 'course-view') {
616ad119 131 continue;
132 }
6bb08163 133 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
f581f8d6 134 print_error('invalidcourseid', '', '', $courseid);
616ad119 135 }
136 $courses[$courseid] = $course;
137 }
474991a1 138 $totalcount = count( $courses );
616ad119 139 }
140 // get list of courses containing modules if required
141 elseif (!empty($modulelist) and confirm_sesskey()) {
142 $modulename = $modulelist;
67da7e65 143 $sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
d0b7da69 144 ." WHERE module.course=c.id";
616ad119 145
d0b7da69 146 $courseids = $DB->get_records_sql($sql);
e48580bd 147 $courses = array();
148 if (!empty($courseids)) {
149 $firstcourse = $page*$perpage;
150 $lastcourse = $page*$perpage + $perpage -1;
151 $i = 0;
152 foreach ($courseids as $courseid) {
153 if ($i>= $firstcourse && $i<=$lastcourse) {
154 $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
155 }
156 $i++;
616ad119 157 }
e48580bd 158 $totalcount = count($courseids);
159 }
160 else {
161 $totalcount = 0;
616ad119 162 }
163 }
164 else {
04c53106 165 $courses = get_courses_search($searchterms, "fullname ASC",
026579bb 166 $page, $perpage, $totalcount);
616ad119 167 }
861efb19 168
a8b56716 169 $searchform = print_course_search($search, true, "navbar");
170
8e480396 171 if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
861efb19 172 $searchform .= update_categories_search_button($search,$page,$perpage);
173 }
861efb19 174
d02240f3 175 $navlinks = array();
176 $navlinks[] = array('name' => $strcourses, 'link' => 'index.php', 'type' => 'misc');
177 $navlinks[] = array('name' => $strsearch, 'link' => 'search.php', 'type' => 'misc');
6bb08163 178 $navlinks[] = array('name' => "'".s($search)."'", 'link' => null, 'type' => 'misc');
d02240f3 179 $navigation = build_navigation($navlinks);
0be6f678 180
d02240f3 181 print_header("$site->fullname : $strsearchresults", $site->fullname, $navigation, "", "", "", $searchform);
38a10939 182
38a10939 183 $lastcategory = -1;
861efb19 184 if ($courses) {
a8b56716 185 print_heading("$strsearchresults: $totalcount");
6bb08163 186 $encodedsearch = urlencode($search);
04c53106 187
d0b7da69 188 ///add the module parameter to the paging bar if they exists
189 $modulelink = "";
190 if (!empty($modulelist) and confirm_sesskey()) {
973d2660 191 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
ee0378bd 192 }
0be6f678 193
04c53106 194 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
195
196 if (!$adminediting) {
861efb19 197 foreach ($courses as $course) {
eb1f8f1d 198 $course->summary .= "<br /><p class=\"category\">";
861efb19 199 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
200 $course->summary .= $displaylist[$course->category];
201 $course->summary .= "</a></p>";
04c53106 202 print_course($course, $search);
861efb19 203 print_spacer(5,5);
204 }
04c53106 205 } else {
206 /// Show editing UI.
be5539d8 207 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
973d2660 208 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
6bb08163 209 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
be5539d8 210 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
211 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
212 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
213 echo "<th scope=\"col\">$strcourses</th>\n";
214 echo "<th scope=\"col\">$strcategory</th>\n";
215 echo "<th scope=\"col\">$strselect</th>\n";
216 echo "<th scope=\"col\">$stredit</th></tr>\n";
a044c05d 217
04c53106 218 foreach ($courses as $course) {
219
026579bb 220 if (isset($course->context)) {
221 $coursecontext = $course->context;
222 } else {
223 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
224 }
616ad119 225
861efb19 226 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
616ad119 227
228 // are we displaying the front page (courseid=1)?
229 if ($course->id == 1) {
be5539d8 230 echo "<tr>\n";
231 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
616ad119 232
233 // can't do anything else with the front page
be5539d8 234 echo " <td>&nbsp;</td>\n"; // category place
235 echo " <td>&nbsp;</td>\n"; // select place
236 echo " <td>&nbsp;</td>\n"; // edit place
237 echo "</tr>\n";
616ad119 238 continue;
239 }
240
be5539d8 241 echo "<tr>\n";
0be6f678 242 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
04c53106 243 . highlight($search, format_string($course->fullname)) . "</a></td>\n";
be5539d8 244 echo "<td>".$displaylist[$course->category]."</td>\n";
245 echo "<td>\n";
0be6f678 246
698fa439 247 // this is ok since this will get inherited from course category context
248 // if it is set
8ed5dd63 249 if (has_capability('moodle/category:manage', $coursecontext)) {
be5539d8 250 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
698fa439 251 } else {
be5539d8 252 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
698fa439 253 }
0be6f678 254
be5539d8 255 echo "</td>\n";
256 echo "<td>\n";
7f60ca5e 257 $pixpath = $CFG->pixpath;
0be6f678 258
698fa439 259 // checks whether user can update course settings
260 if (has_capability('moodle/course:update', $coursecontext)) {
be5539d8 261 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
262 " src=\"$pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
698fa439 263 }
264
265 // checks whether user can do role assignment
a044c05d 266 if (has_capability('moodle/role:assign', $coursecontext)) {
1ed33201 267 echo'<a title="'.get_string('assignroles', 'role').'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$coursecontext->id.'">';
a044c05d 268 echo '<img src="'.$CFG->pixpath.'/i/roles.gif" class="iconsmall" alt="'.get_string('assignroles', 'role').'" /></a> ' . "\n";
0be6f678 269 }
698fa439 270
271 // checks whether user can delete course
0be6f678 272 if (has_capability('moodle/course:delete', $coursecontext)) {
be5539d8 273 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
274 " src=\"$pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
0be6f678 275 }
698fa439 276
277 // checks whether user can change visibility
278 if (has_capability('moodle/course:visibility', $coursecontext)) {
279 if (!empty($course->visible)) {
973d2660 280 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".
be5539d8 281 " src=\"$pixpath/t/hide.gif\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
698fa439 282 } else {
973d2660 283 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".
be5539d8 284 " src=\"$pixpath/t/show.gif\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
698fa439 285 }
0be6f678 286 }
698fa439 287
288 // checks whether user can do site backup
289 if (has_capability('moodle/site:backup', $coursecontext)) {
be5539d8 290 echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\">\n<img".
291 " src=\"$pixpath/t/backup.gif\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
861efb19 292 }
0be6f678 293
698fa439 294 // checks whether user can do restore
295 if (has_capability('moodle/site:restore', $coursecontext)) {
be5539d8 296 echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&amp;wdir=/backupdata\">\n<img".
297 " src=\"$pixpath/t/restore.gif\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
698fa439 298 }
299
be5539d8 300 echo "</td>\n</tr>\n";
861efb19 301 }
be5539d8 302 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
861efb19 303 echo "<br />";
304 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
f6b6861d 305 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
b7dc2256 306 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: getElementById('movecourses').submit()");
be5539d8 307 echo "</td>\n</tr>\n";
308 echo "</table>\n</form>";
861efb19 309
38a10939 310 }
311
d0b7da69 312 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
a8b56716 313
38a10939 314 } else {
594bb20b 315 if (!empty($search)) {
6bb08163 316 print_heading(get_string("nocoursesfound", "", s($search)));
594bb20b 317 }
318 else {
319 print_heading( $strnovalidcourses );
320 }
38a10939 321 }
322
323 echo "<br /><br />";
324
325 print_course_search($search);
326
327 print_footer();
328
d0b7da69 329 /**
330 * Print a list navigation bar
331 * Display page numbers, and a link for displaying all entries
332 * @param integer $totalcount - number of entry to display
333 * @param integer $page - page number
334 * @param integer $perpage - number of entry per page
335 * @param string $encodedsearch
336 * @param string $modulelink - module name
337 */
338 function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
339 print_paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&amp;perpage=$perpage&amp;",'page',($perpage == 99999));
340
341 //display
342 if ($perpage != 99999 && $totalcount > $perpage) {
343 echo "<center><p>";
344 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
345 echo "</p></center>";
346 }
347 }
38a10939 348
349?>