MDL-9265:
[moodle-linuxchix.git] / course / search.php
blob500721dd9af04f93db0927af8c03b1c89e20cd3d
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, SITEID))) {
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, SITEID))) {
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, SITEID)) && $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 print_header("$site->fullname : $strsearch", $site->fullname,
90 "<a href=\"index.php\">$strcourses</a> -> $strsearch", "", "");
91 print_simple_box_start("center");
92 echo "<center>";
93 echo "<br />";
94 print_course_search("", false, "plain");
95 echo "<br /><p>";
96 print_string("searchhelp");
97 echo "</p>";
98 echo "</center>";
99 print_simple_box_end();
100 print_footer();
101 exit;
104 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
106 if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
107 error("Error finding the category");
110 $courses = array();
111 foreach ( $data as $key => $value ) {
112 if (preg_match('/^c\d+$/', $key)) {
113 array_push($courses, substr($key, 1));
116 move_courses($courses, $data->moveto);
119 // get list of courses containing blocks if required
120 if (!empty($blocklist) and confirm_sesskey()) {
121 $blockid = $blocklist;
122 if (!$blocks = get_records('block_instance', 'blockid', $blockid)) {
123 error( "Could not read data for blockid=$blockid" );
126 // run through blocks and get (unique) courses
127 $courses = array();
128 foreach ($blocks as $block) {
129 $courseid = $block->pageid;
130 if ($courseid==0) {
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;
143 if (!$modules = get_records($modulename)) {
144 error( "Could not read data for module=$modulename" );
147 // run through modules and get (unique) courses
148 $courses = array();
149 foreach ($modules as $module) {
150 $courseid = $module->course;
151 if ($courseid==0) {
152 continue;
154 if (!$course = get_record('course', 'id', $courseid)) {
155 error( "Could not read data for courseid=$courseid" );
157 $courses[$courseid] = $course;
159 $totalcount = count($courses);
161 else {
162 $courses = get_courses_search($searchterms, "fullname ASC",
163 $page*$perpage, $perpage, $totalcount);
166 $searchform = print_course_search($search, true, "navbar");
168 if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
169 $searchform .= update_categories_search_button($search,$page,$perpage);
173 print_header("$site->fullname : $strsearchresults", $site->fullname,
174 "<a href=\"index.php\">$strcourses</a> -> <a href=\"search.php\">$strsearch</a> -> '".s($search, true)."'", "", "", "", $searchform);
177 $lastcategory = -1;
178 if ($courses) {
180 print_heading("$strsearchresults: $totalcount");
182 $encodedsearch = urlencode(stripslashes($search));
183 print_paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch&amp;perpage=$perpage&amp;",'page',($perpage == 99999));
185 if ($perpage != 99999 && $totalcount > $perpage) {
186 echo "<center><p>";
187 echo "<a href=\"search.php?search=$encodedsearch&perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
188 echo "</p></center>";
191 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
192 foreach ($courses as $course) {
193 $course->fullname = highlight("$search", $course->fullname);
194 $course->summary = highlight("$search", $course->summary);
195 $course->summary .= "<br /><p class=\"category\">";
196 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
197 $course->summary .= $displaylist[$course->category];
198 $course->summary .= "</a></p>";
199 print_course($course);
200 print_spacer(5,5);
202 } else { // slightly more sophisticated
204 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
205 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />\n";
206 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search, true)."\" />\n";
207 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
208 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
209 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
210 echo "<th scope=\"col\">$strcourses</th>\n";
211 echo "<th scope=\"col\">$strcategory</th>\n";
212 echo "<th scope=\"col\">$strselect</th>\n";
213 echo "<th scope=\"col\">$stredit</th></tr>\n";
214 foreach ($courses as $course) {
216 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
218 $course->fullname = highlight("$search", $course->fullname);
219 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
221 // are we displaying the front page (courseid=1)?
222 if ($course->id == 1) {
223 echo "<tr>\n";
224 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
226 // can't do anything else with the front page
227 echo " <td>&nbsp;</td>\n"; // category place
228 echo " <td>&nbsp;</td>\n"; // select place
229 echo " <td>&nbsp;</td>\n"; // edit place
230 echo "</tr>\n";
231 continue;
234 echo "<tr>\n";
235 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
236 . format_string($course->fullname) . "</a></td>\n";
237 echo "<td>".$displaylist[$course->category]."</td>\n";
238 echo "<td>\n";
240 // this is ok since this will get inherited from course category context
241 // if it is set
242 if (has_capability('moodle/category:update', $coursecontext)) {
243 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
244 } else {
245 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
248 echo "</td>\n";
249 echo "<td>\n";
250 $pixpath = $CFG->pixpath;
252 // checks whether user can update course settings
253 if (has_capability('moodle/course:update', $coursecontext)) {
254 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
255 " src=\"$pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
258 // checks whether user can do role assignment
259 if (has_capability('moodle/role:assign', $coursecontext)) {
260 echo'<a title="'.get_string('assignroles', 'role').'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$coursecontext->id.'">';
261 echo '<img src="'.$CFG->pixpath.'/i/roles.gif" class="iconsmall" alt="'.get_string('assignroles', 'role').'" /></a> ' . "\n";
264 // checks whether user can delete course
265 if (has_capability('moodle/course:delete', $coursecontext)) {
266 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
267 " src=\"$pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
270 // checks whether user can change visibility
271 if (has_capability('moodle/course:visibility', $coursecontext)) {
272 if (!empty($course->visible)) {
273 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".
274 " src=\"$pixpath/t/hide.gif\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
275 } else {
276 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".
277 " src=\"$pixpath/t/show.gif\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
281 // checks whether user can do site backup
282 if (has_capability('moodle/site:backup', $coursecontext)) {
283 echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\">\n<img".
284 " src=\"$pixpath/t/backup.gif\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
287 // checks whether user can do restore
288 if (has_capability('moodle/site:restore', $coursecontext)) {
289 echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&amp;wdir=/backupdata\">\n<img".
290 " src=\"$pixpath/t/restore.gif\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
293 echo "</td>\n</tr>\n";
295 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
296 echo "<br />";
297 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
298 echo "<input type=\"button\" onclick=\"uncheckall()\" value=\"$strdeselectall\" />\n";
299 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: getElementById('movecourses').submit()");
300 echo "</td>\n</tr>\n";
301 echo "</table>\n</form>";
305 print_paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch&amp;perpage=$perpage&amp;",'page',($perpage == 99999));
307 if ($perpage != 99999 && $totalcount > $perpage) {
308 echo "<center><p>";
309 echo "<a href=\"search.php?search=$encodedsearch&perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
310 echo "</p></center>";
313 } else {
314 if (!empty($search)) {
315 print_heading(get_string("nocoursesfound", "", s($search, true)));
317 else {
318 print_heading( $strnovalidcourses );
322 echo "<br /><br />";
324 print_course_search($search);
326 print_footer();