Updated the 19 build version to 20100904
[moodle.git] / course / search.php
blob2f3d3c322ade49f744540f02a09f848835acbdc4
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 (update_category_button()) {
37 if ($edit !== -1) {
38 $USER->categoryediting = $edit;
40 $adminediting = !empty($USER->categoryediting);
41 } else {
42 $adminediting = false;
45 /// Editing functions
46 if (has_capability('moodle/course:visibility', get_context_instance(CONTEXT_SYSTEM))) {
47 /// Hide or show a course
48 if ($hide or $show and confirm_sesskey()) {
49 if ($hide) {
50 $course = get_record("course", "id", $hide);
51 $visible = 0;
52 } else {
53 $course = get_record("course", "id", $show);
54 $visible = 1;
56 if ($course) {
57 if (! set_field("course", "visible", $visible, "id", $course->id)) {
58 notify("Could not update that course!");
64 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM)) && $perpage != 99999) {
65 $perpage = 30;
68 $displaylist = array();
69 $parentlist = array();
70 make_categories_list($displaylist, $parentlist);
72 $strcourses = get_string("courses");
73 $strsearch = get_string("search");
74 $strsearchresults = get_string("searchresults");
75 $strcategory = get_string("category");
76 $strselect = get_string("select");
77 $strselectall = get_string("selectall");
78 $strdeselectall = get_string("deselectall");
79 $stredit = get_string("edit");
80 $strfrontpage = get_string('frontpage', 'admin');
81 $strnovalidcourses = get_string('novalidcourses');
83 if (empty($search) and empty($blocklist) and empty($modulelist)) {
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);
89 print_header("$site->fullname : $strsearch", $site->fullname, $navigation, "", "");
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();
99 print_footer();
100 exit;
103 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
104 if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
105 error("Error finding the category");
108 $courses = array();
109 foreach ( $data as $key => $value ) {
110 if (preg_match('/^c\d+$/', $key)) {
111 array_push($courses, substr($key, 1));
114 move_courses($courses, $data->moveto);
117 // get list of courses containing blocks if required
118 if (!empty($blocklist) and confirm_sesskey()) {
119 $blockid = $blocklist;
120 if (!$blocks = get_records('block_instance', 'blockid', $blockid)) {
121 error( "Could not read data for blockid=$blockid" );
124 // run through blocks and get (unique) courses
125 $courses = array();
126 foreach ($blocks as $block) {
127 $courseid = $block->pageid;
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') {
131 continue;
133 if (!$course = get_record('course', 'id', $courseid)) {
134 error( "Could not read data for courseid=$courseid" );
136 $courses[$courseid] = $course;
138 $totalcount = count( $courses );
140 // get list of courses containing modules if required
141 elseif (!empty($modulelist) and confirm_sesskey()) {
142 $modulename = $modulelist;
144 $sql = "SELECT DISTINCT c.id FROM {$CFG->prefix}".$modulelist." module, {$CFG->prefix}course c"
145 ." WHERE module.course=c.id";
147 $courseids = get_records_sql($sql);
149 $courses = array();
150 if (!empty($courseids)) {
151 $firstcourse = $page*$perpage;
152 $lastcourse = $page*$perpage + $perpage -1;
153 $i = 0;
154 foreach ($courseids as $courseid) {
155 if ($i>= $firstcourse && $i<=$lastcourse) {
156 $courses[$courseid->id] = get_record('course', 'id', $courseid->id);
158 $i++;
160 $totalcount = count($courseids);
162 else {
163 $totalcount = 0;
166 else {
167 $courses = get_courses_search($searchterms, "fullname ASC",
168 $page, $perpage, $totalcount);
171 $searchform = print_course_search($search, true, "navbar");
173 if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
174 $searchform .= update_categories_search_button($search,$page,$perpage);
177 $navlinks = array();
178 $navlinks[] = array('name' => $strcourses, 'link' => 'index.php', 'type' => 'misc');
179 $navlinks[] = array('name' => $strsearch, 'link' => 'search.php', 'type' => 'misc');
180 $navlinks[] = array('name' => "'".s($search, true)."'", 'link' => null, 'type' => 'misc');
181 $navigation = build_navigation($navlinks);
183 print_header("$site->fullname : $strsearchresults", $site->fullname, $navigation, "", "", "", $searchform);
185 $lastcategory = -1;
186 if ($courses) {
187 print_heading("$strsearchresults: $totalcount");
188 $encodedsearch = urlencode(stripslashes($search));
190 ///add the module parameter to the paging bar if they exists
191 $modulelink = "";
192 if (!empty($modulelist) and confirm_sesskey()) {
193 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".$USER->sesskey;
196 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
198 if (!$adminediting) {
199 /// Show browse view.
200 foreach ($courses as $course) {
201 $course->summary .= "<br /><p class=\"category\">";
202 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
203 $course->summary .= $displaylist[$course->category];
204 $course->summary .= "</a></p>";
205 print_course($course, $search);
206 print_spacer(5,5);
208 } else {
209 /// Show editing UI.
210 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
211 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />\n";
212 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search, true)."\" />\n";
213 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
214 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
215 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
216 echo "<th scope=\"col\">$strcourses</th>\n";
217 echo "<th scope=\"col\">$strcategory</th>\n";
218 echo "<th scope=\"col\">$strselect</th>\n";
219 echo "<th scope=\"col\">$stredit</th></tr>\n";
221 foreach ($courses as $course) {
223 if (isset($course->context)) {
224 $coursecontext = $course->context;
225 } else {
226 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
229 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
231 // are we displaying the front page (courseid=1)?
232 if ($course->id == 1) {
233 echo "<tr>\n";
234 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
236 // can't do anything else with the front page
237 echo " <td>&nbsp;</td>\n"; // category place
238 echo " <td>&nbsp;</td>\n"; // select place
239 echo " <td>&nbsp;</td>\n"; // edit place
240 echo "</tr>\n";
241 continue;
244 echo "<tr>\n";
245 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
246 . highlight($search, format_string($course->fullname)) . "</a></td>\n";
247 echo "<td>".$displaylist[$course->category]."</td>\n";
248 echo "<td>\n";
250 // this is ok since this will get inherited from course category context
251 // if it is set
252 if (has_capability('moodle/category:manage', $coursecontext)) {
253 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
254 } else {
255 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
258 echo "</td>\n";
259 echo "<td>\n";
260 $pixpath = $CFG->pixpath;
262 // checks whether user can update course settings
263 if (has_capability('moodle/course:update', $coursecontext)) {
264 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
265 " src=\"$pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
268 // checks whether user can do role assignment
269 if (has_capability('moodle/role:assign', $coursecontext)) {
270 echo'<a title="'.get_string('assignroles', 'role').'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$coursecontext->id.'">';
271 echo '<img src="'.$CFG->pixpath.'/i/roles.gif" class="iconsmall" alt="'.get_string('assignroles', 'role').'" /></a> ' . "\n";
274 // checks whether user can delete course
275 if (has_capability('moodle/course:delete', $coursecontext)) {
276 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
277 " src=\"$pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
280 // checks whether user can change visibility
281 if (has_capability('moodle/course:visibility', $coursecontext)) {
282 if (!empty($course->visible)) {
283 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".
284 " src=\"$pixpath/t/hide.gif\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
285 } else {
286 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".
287 " src=\"$pixpath/t/show.gif\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
291 // checks whether user can do site backup
292 if (has_capability('moodle/site:backup', $coursecontext)) {
293 echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\">\n<img".
294 " src=\"$pixpath/t/backup.gif\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
297 // checks whether user can do restore
298 if (has_capability('moodle/site:restore', $coursecontext)) {
299 echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&amp;wdir=/backupdata\">\n<img".
300 " src=\"$pixpath/t/restore.gif\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
303 echo "</td>\n</tr>\n";
305 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
306 echo "<br />";
307 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
308 echo "<input type=\"button\" onclick=\"uncheckall()\" value=\"$strdeselectall\" />\n";
309 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: getElementById('movecourses').submit()");
310 echo "</td>\n</tr>\n";
311 echo "</table>\n</form>";
315 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
317 } else {
318 if (!empty($search)) {
319 print_heading(get_string("nocoursesfound", "", s($search, true)));
321 else {
322 print_heading( $strnovalidcourses );
326 echo "<br /><br />";
328 print_course_search($search);
330 print_footer();
333 * Print a list navigation bar
334 * Display page numbers, and a link for displaying all entries
335 * @param integer $totalcount - number of entry to display
336 * @param integer $page - page number
337 * @param integer $perpage - number of entry per page
338 * @param string $encodedsearch
339 * @param string $modulelink - module name
341 function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
342 print_paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&amp;perpage=$perpage&amp;",'page',($perpage == 99999));
344 //display
345 if ($perpage != 99999 && $totalcount > $perpage) {
346 echo "<center><p>";
347 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
348 echo "</p></center>";