Automatic installer.php lang files by installer_builder (20081123)
[moodle.git] / course / search.php
blobf10e9a86c8a356fff88243f5b63f833cb1100e04
1 <?php // $Id$
3 /// Displays external information about a course
5 require_once("../config.php");
6 require_once("lib.php");
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);
18 $search = trim(strip_tags($search)); // trim & clean raw searched string
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]);
27 $search = trim(implode(" ", $searchterms));
30 $site = get_site();
32 if ($CFG->forcelogin) {
33 require_login();
36 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
37 if ($edit !== -1) {
38 $USER->categoryediting = $edit;
39 // If the edit mode we are leaving has higher per page than the one we are entering,
40 // with pages, chances are you will get a no courses found error. So when we are switching
41 // modes, set page to 0.
42 $page = 0;
46 /// Editing functions
48 if (has_capability('moodle/course:visibility', get_context_instance(CONTEXT_SYSTEM))) {
50 /// Hide or show a course
52 if ($hide or $show and confirm_sesskey()) {
53 if ($hide) {
54 $course = get_record("course", "id", $hide);
55 $visible = 0;
56 } else {
57 $course = get_record("course", "id", $show);
58 $visible = 1;
60 if ($course) {
61 if (! set_field("course", "visible", $visible, "id", $course->id)) {
62 notify("Could not update that course!");
69 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM)) && $perpage != 99999) {
70 $perpage = 30;
73 $displaylist = array();
74 $parentlist = array();
75 make_categories_list($displaylist, $parentlist, "");
77 $strcourses = get_string("courses");
78 $strsearch = get_string("search");
79 $strsearchresults = get_string("searchresults");
80 $strcategory = get_string("category");
81 $strselect = get_string("select");
82 $strselectall = get_string("selectall");
83 $strdeselectall = get_string("deselectall");
84 $stredit = get_string("edit");
85 $strfrontpage = get_string('frontpage', 'admin');
86 $strnovalidcourses = get_string('novalidcourses');
88 if (empty($search) and empty($blocklist) and empty($modulelist)) {
89 $navlinks = array();
90 $navlinks[] = array('name' => $strcourses, 'link' => "index.php", 'type' => 'misc');
91 $navlinks[] = array('name' => $strsearch, 'link' => null, 'type' => 'misc');
92 $navigation = build_navigation($navlinks);
94 print_header("$site->fullname : $strsearch", $site->fullname, $navigation, "", "");
95 print_simple_box_start("center");
96 echo "<center>";
97 echo "<br />";
98 print_course_search("", false, "plain");
99 echo "<br /><p>";
100 print_string("searchhelp");
101 echo "</p>";
102 echo "</center>";
103 print_simple_box_end();
104 print_footer();
105 exit;
108 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
110 if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
111 error("Error finding the category");
114 $courses = array();
115 foreach ( $data as $key => $value ) {
116 if (preg_match('/^c\d+$/', $key)) {
117 array_push($courses, substr($key, 1));
120 move_courses($courses, $data->moveto);
123 // get list of courses containing blocks if required
124 if (!empty($blocklist) and confirm_sesskey()) {
125 $blockid = $blocklist;
126 if (!$blocks = get_records('block_instance', 'blockid', $blockid)) {
127 error( "Could not read data for blockid=$blockid" );
130 // run through blocks and get (unique) courses
131 $courses = array();
132 foreach ($blocks as $block) {
133 $courseid = $block->pageid;
134 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
135 // and it should not show up on course search page
136 if ($courseid==0 || $block->pagetype != 'course-view') {
137 continue;
139 if (!$course = get_record('course', 'id', $courseid)) {
140 error( "Could not read data for courseid=$courseid" );
142 $courses[$courseid] = $course;
144 $totalcount = count( $courses );
146 // get list of courses containing modules if required
147 elseif (!empty($modulelist) and confirm_sesskey()) {
148 $modulename = $modulelist;
150 $sql = "SELECT DISTINCT c.id FROM {$CFG->prefix}".$modulelist." module, {$CFG->prefix}course c"
151 ." WHERE module.course=c.id";
153 $courseids = get_records_sql($sql);
155 $courses = array();
156 if (!empty($courseids)) {
157 $firstcourse = $page*$perpage;
158 $lastcourse = $page*$perpage + $perpage -1;
159 $i = 0;
160 foreach ($courseids as $courseid) {
161 if ($i>= $firstcourse && $i<=$lastcourse) {
162 $courses[$courseid->id] = get_record('course', 'id', $courseid->id);
164 $i++;
166 $totalcount = count($courseids);
168 else {
169 $totalcount = 0;
172 else {
173 $courses = get_courses_search($searchterms, "fullname ASC",
174 $page, $perpage, $totalcount);
177 $searchform = print_course_search($search, true, "navbar");
179 if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
180 $searchform .= update_categories_search_button($search,$page,$perpage);
183 $navlinks = array();
184 $navlinks[] = array('name' => $strcourses, 'link' => 'index.php', 'type' => 'misc');
185 $navlinks[] = array('name' => $strsearch, 'link' => 'search.php', 'type' => 'misc');
186 $navlinks[] = array('name' => "'".s($search, true)."'", 'link' => null, 'type' => 'misc');
187 $navigation = build_navigation($navlinks);
189 print_header("$site->fullname : $strsearchresults", $site->fullname, $navigation, "", "", "", $searchform);
192 $lastcategory = -1;
193 if ($courses) {
195 print_heading("$strsearchresults: $totalcount");
197 $encodedsearch = urlencode(stripslashes($search));
199 ///add the module parameter to the paging bar if they exists
200 $modulelink = "";
201 if (!empty($modulelist) and confirm_sesskey()) {
202 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".$USER->sesskey;
205 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
207 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
208 foreach ($courses as $course) {
209 $course->fullname = highlight("$search", $course->fullname);
210 $course->summary = highlight("$search", $course->summary);
211 $course->summary .= "<br /><p class=\"category\">";
212 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
213 $course->summary .= $displaylist[$course->category];
214 $course->summary .= "</a></p>";
215 print_course($course);
216 print_spacer(5,5);
218 } else { // slightly more sophisticated
220 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
221 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />\n";
222 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search, true)."\" />\n";
223 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
224 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
225 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
226 echo "<th scope=\"col\">$strcourses</th>\n";
227 echo "<th scope=\"col\">$strcategory</th>\n";
228 echo "<th scope=\"col\">$strselect</th>\n";
229 echo "<th scope=\"col\">$stredit</th></tr>\n";
231 foreach ($courses as $course) {
233 if (isset($course->context)) {
234 $coursecontext = $course->context;
235 } else {
236 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
239 $course->fullname = highlight("$search", $course->fullname);
240 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
242 // are we displaying the front page (courseid=1)?
243 if ($course->id == 1) {
244 echo "<tr>\n";
245 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
247 // can't do anything else with the front page
248 echo " <td>&nbsp;</td>\n"; // category place
249 echo " <td>&nbsp;</td>\n"; // select place
250 echo " <td>&nbsp;</td>\n"; // edit place
251 echo "</tr>\n";
252 continue;
255 echo "<tr>\n";
256 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
257 . format_string($course->fullname) . "</a></td>\n";
258 echo "<td>".$displaylist[$course->category]."</td>\n";
259 echo "<td>\n";
261 // this is ok since this will get inherited from course category context
262 // if it is set
263 if (has_capability('moodle/category:update', $coursecontext)) {
264 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
265 } else {
266 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
269 echo "</td>\n";
270 echo "<td>\n";
271 $pixpath = $CFG->pixpath;
273 // checks whether user can update course settings
274 if (has_capability('moodle/course:update', $coursecontext)) {
275 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
276 " src=\"$pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
279 // checks whether user can do role assignment
280 if (has_capability('moodle/role:assign', $coursecontext)) {
281 echo'<a title="'.get_string('assignroles', 'role').'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$coursecontext->id.'">';
282 echo '<img src="'.$CFG->pixpath.'/i/roles.gif" class="iconsmall" alt="'.get_string('assignroles', 'role').'" /></a> ' . "\n";
285 // checks whether user can delete course
286 if (has_capability('moodle/course:delete', $coursecontext)) {
287 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
288 " src=\"$pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
291 // checks whether user can change visibility
292 if (has_capability('moodle/course:visibility', $coursecontext)) {
293 if (!empty($course->visible)) {
294 echo "<a title=\"".get_string("hide")."\" href=\"search.php?search=$encodedsearch&amp;perpage=$perpage&amp;page=$page&amp;hide=$course->id&amp;sesskey=$USER->sesskey\">\n<img".
295 " src=\"$pixpath/t/hide.gif\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
296 } else {
297 echo "<a title=\"".get_string("show")."\" href=\"search.php?search=$encodedsearch&amp;perpage=$perpage&amp;page=$page&amp;show=$course->id&amp;sesskey=$USER->sesskey\">\n<img".
298 " src=\"$pixpath/t/show.gif\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
302 // checks whether user can do site backup
303 if (has_capability('moodle/site:backup', $coursecontext)) {
304 echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\">\n<img".
305 " src=\"$pixpath/t/backup.gif\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
308 // checks whether user can do restore
309 if (has_capability('moodle/site:restore', $coursecontext)) {
310 echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&amp;wdir=/backupdata\">\n<img".
311 " src=\"$pixpath/t/restore.gif\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
314 echo "</td>\n</tr>\n";
316 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
317 echo "<br />";
318 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
319 echo "<input type=\"button\" onclick=\"uncheckall()\" value=\"$strdeselectall\" />\n";
320 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: getElementById('movecourses').submit()");
321 echo "</td>\n</tr>\n";
322 echo "</table>\n</form>";
326 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
328 } else {
329 if (!empty($search)) {
330 print_heading(get_string("nocoursesfound", "", s($search, true)));
332 else {
333 print_heading( $strnovalidcourses );
337 echo "<br /><br />";
339 print_course_search($search);
341 print_footer();
344 * Print a list navigation bar
345 * Display page numbers, and a link for displaying all entries
346 * @param integer $totalcount - number of entry to display
347 * @param integer $page - page number
348 * @param integer $perpage - number of entry per page
349 * @param string $encodedsearch
350 * @param string $modulelink - module name
352 function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
353 print_paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&amp;perpage=$perpage&amp;",'page',($perpage == 99999));
355 //display
356 if ($perpage != 99999 && $totalcount > $perpage) {
357 echo "<center><p>";
358 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
359 echo "</p></center>";