84fd0106e8020888ccbe0da22001f75ad393c2db
[moodle.git] / course / search.php
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);
16     $search = trim(strip_tags($search)); // trim & clean raw searched string
18     if ($search) {
19         $searchterms = explode(" ", $search);    // Search for words independently
20         foreach ($searchterms as $key => $searchterm) {
21             if (strlen($searchterm) < 2) {
22                 unset($searchterms[$key]);
23             }
24         }
25         $search = trim(implode(" ", $searchterms));
26     }
28     $site = get_site();
30     if ($CFG->forcelogin) {
31         require_login();
32     }
34     if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
35         if ($edit !== -1) {
36             $USER->categoryediting = $edit;
37             // If the edit mode we are leaving has higher per page than the one we are entering,
38             // with pages, chances are you will get a no courses found error. So when we are switching
39             // modes, set page to 0.
40             $page = 0;
41         }
42     } 
44 /// Editing functions
46     if (has_capablity('moodle/course:visibility', get_context_instance(CONTEXT_SITE, SITEID))) {
48     /// Hide or show a course
50         if ($hide or $show and confirm_sesskey()) {
51             if ($hide) {
52                 $course = get_record("course", "id", $hide);
53                 $visible = 0;
54             } else {
55                 $course = get_record("course", "id", $show);
56                 $visible = 1;
57             }
58             if ($course) {
59                 if (! set_field("course", "visible", $visible, "id", $course->id)) {
60                     notify("Could not update that course!");
61                 }
62             }
63         }
65     }
67     if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID)) && $perpage != 99999) {
68         $perpage = 30;
69     }
71     $displaylist = array();
72     $parentlist = array();
73     make_categories_list($displaylist, $parentlist, "");
75     $strcourses = get_string("courses");
76     $strsearch = get_string("search");
77     $strsearchresults = get_string("searchresults");
78     $strcategory = get_string("category");
79     $strselect   = get_string("select");
80     $strselectall = get_string("selectall");
81     $strdeselectall = get_string("deselectall");
82     $stredit = get_string("edit");
84     if (!$search) {
85         print_header("$site->fullname : $strsearch", $site->fullname, 
86                      "<a href=\"index.php\">$strcourses</a> -> $strsearch", "", "");
87         print_simple_box_start("center");
88         echo "<center>";
89         echo "<br />";
90         print_course_search("", false, "plain");
91         echo "<br /><p>";
92         print_string("searchhelp");
93         echo "</p>";
94         echo "</center>";
95         print_simple_box_end();
96         print_footer();
97         exit;
98     }
100     if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) {   // Some courses are being moved
101     
102         if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
103             error("Error finding the category");
104         }
105         
106         $courses = array();        
107         foreach ( $data as $key => $value ) {
108             if (preg_match('/^c\d+$/', $key)) {
109                 array_push($courses, substr($key, 1));
110             }
111         }
112         move_courses($courses, $data->moveto);
113     }
115     $courses = get_courses_search($searchterms, "fullname ASC", 
116                                   $page*$perpage, $perpage, $totalcount);
118     $searchform = print_course_search($search, true, "navbar");
120     if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
121         $searchform .= update_categories_search_button($search,$page,$perpage);
122     }
123  
125     print_header("$site->fullname : $strsearchresults", $site->fullname, 
126                  "<a href=\"index.php\">$strcourses</a> -> <a href=\"search.php\">$strsearch</a> -> '".s($search, true)."'", "", "", "", $searchform);
129     $lastcategory = -1;
130     if ($courses) {
132         print_heading("$strsearchresults: $totalcount");
134         $encodedsearch = urlencode(stripslashes($search));
135         print_paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch&amp;perpage=$perpage&amp;",'page',($perpage == 99999));
137         if ($perpage != 99999 && $totalcount > $perpage) {
138             echo "<center><p>";
139             echo "<a href=\"search.php?search=$encodedsearch&perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
140             echo "</p></center>";
141         }
143         if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
144             foreach ($courses as $course) {
145                 $course->fullname = highlight("$search", $course->fullname);
146                 $course->summary = highlight("$search", $course->summary);
147                 $course->summary .= "<br /><p align=\"right\">";
148                 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
149                 $course->summary .= $displaylist[$course->category];
150                 $course->summary .= "</a></p>";
151                 print_course($course);
152                 print_spacer(5,5);
153             }
154         } else { // slightly more sophisticated
156             echo "<form name=\"movecourses\" action=\"search.php\" method=\"post\">";
157             echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\">";
158             echo "<input type=\"hidden\" name=\"search\" value=\"".s($search, true)."\">";
159             echo "<input type=\"hidden\" name=\"page\" value=\"$page\">";
160             echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\">";
161             echo "<table align=\"center\" border=0 cellspacing=2 cellpadding=4 class=\"generalbox\"><tr>";
162             echo "<th>$strcourses</th>";
163             echo "<th>$strcategory</th>";
164             echo "<th>$strselect</th>";
165             echo "<th>$stredit</th>";
166             foreach ($courses as $course) {
167                 $course->fullname = highlight("$search", $course->fullname);
168                 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
169                 echo "<tr>";
170                 echo "<td><a $linkcss href=\"view.php?id=$course->id\">$course->fullname</a></td>";
171                 echo "<td>".$displaylist[$course->category]."</td>";
172                 echo "<td align=\"center\">";
173                 echo "<input type=\"checkbox\" name=\"c$course->id\">";
174                 echo "</td>";
175                 echo "<td>";
176                 if (empty($THEME->custompix)) {
177                     $pixpath = "$CFG->wwwroot/pix";
178                 } else {
179                     $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
180                 }
181                 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\"><img".
182                     " src=\"$pixpath/t/edit.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
183                 echo "<a title=\"".get_string("assignteachers")."\" href=\"$CFG->wwwroot/course/teacher.php?id=$course->id\"><img".
184                     " src=\"$pixpath/t/user.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
185                 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\"><img".
186                     " src=\"$pixpath/t/delete.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
187                 if (!empty($course->visible)) {
188                     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\"><img".
189                         " src=\"$pixpath/t/hide.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
190                 } else {
191                     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\"><img".
192                         " src=\"$pixpath/t/show.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
193                 }
194                 
195                 echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\"><img".
196                     " src=\"$pixpath/t/backup.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
197                 
198                 echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&wdir=/backupdata\"><img".
199                     " src=\"$pixpath/t/restore.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
200                 echo "</td></tr>";
201             }
202             echo "<tr><td colspan=\"4\" align=\"center\">";
203             echo "<br />";
204             echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
205             echo "<input type=\"button\" onclick=\"uncheckall()\" value=\"$strdeselectall\" />\n";
206             choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript:document.movecourses.submit()");
207             echo "</td></tr>";
208             echo "</table>";
210         }
212         print_paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch&amp;perpage=$perpage&amp;",'page',($perpage == 99999));
214         if ($perpage != 99999 && $totalcount > $perpage) {
215             echo "<center><p>";
216             echo "<a href=\"search.php?search=$encoededsearch&perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
217             echo "</p></center>";
218         }
220     } else {
221         print_heading(get_string("nocoursesfound", "", s($search, true)));
222     }
224     echo "<br /><br />";
226     print_course_search($search);
228     print_footer();
231 ?>