Changed CONTEXT_SITE to CONTEXT_SYSTEM MDL-6506
[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
9cc78ee1 8 $search = optional_param('search', '', PARAM_RAW); // search words
9 $page = optional_param('page', 0, PARAM_INT); // which page to show
59518de1 10 $perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
40b4a063 11 $moveto = optional_param('moveto', 0, PARAM_INT); // move to category
cd7d03fa 12 $edit = optional_param('edit', -1, PARAM_BOOL);
13 $hide = optional_param('hide', 0, PARAM_INT);
14 $show = optional_param('show', 0, PARAM_INT);
38a10939 15
9cc78ee1 16 $search = trim(strip_tags($search)); // trim & clean raw searched string
38a10939 17
a8b56716 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 }
27
38a10939 28 $site = get_site();
29
4f006bc1 30 if ($CFG->forcelogin) {
31 require_login();
32 }
33
ae9e4c06 34 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
cd7d03fa 35 if ($edit !== -1) {
36 $USER->categoryediting = $edit;
23c690ef 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;
861efb19 41 }
9c37662f 42 }
861efb19 43
fa18e81b 44/// Editing functions
45
329f40b0 46 if (has_capablity('moodle/course:visibility', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
fa18e81b 47
48 /// Hide or show a course
49
cd7d03fa 50 if ($hide or $show and confirm_sesskey()) {
51 if ($hide) {
fa18e81b 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 }
64
65 }
66
9c37662f 67 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID)) && $perpage != 99999) {
861efb19 68 $perpage = 30;
69 }
70
38a10939 71 $displaylist = array();
72 $parentlist = array();
73 make_categories_list($displaylist, $parentlist, "");
74
75 $strcourses = get_string("courses");
c571f3fc 76 $strsearch = get_string("search");
38a10939 77 $strsearchresults = get_string("searchresults");
78 $strcategory = get_string("category");
861efb19 79 $strselect = get_string("select");
80 $strselectall = get_string("selectall");
81 $strdeselectall = get_string("deselectall");
82 $stredit = get_string("edit");
38a10939 83
c571f3fc 84 if (!$search) {
85 print_header("$site->fullname : $strsearch", $site->fullname,
86 "<a href=\"index.php\">$strcourses</a> -> $strsearch", "", "");
a8b56716 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();
c571f3fc 96 print_footer();
97 exit;
98 }
99
40b4a063 100 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
861efb19 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 }
114
115 $courses = get_courses_search($searchterms, "fullname ASC",
116 $page*$perpage, $perpage, $totalcount);
117
a8b56716 118 $searchform = print_course_search($search, true, "navbar");
119
ae9e4c06 120 if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
861efb19 121 $searchform .= update_categories_search_button($search,$page,$perpage);
122 }
123
124
38a10939 125 print_header("$site->fullname : $strsearchresults", $site->fullname,
f8a5159a 126 "<a href=\"index.php\">$strcourses</a> -> <a href=\"search.php\">$strsearch</a> -> '".s($search, true)."'", "", "", "", $searchform);
38a10939 127
38a10939 128
129 $lastcategory = -1;
861efb19 130 if ($courses) {
a8b56716 131
132 print_heading("$strsearchresults: $totalcount");
133
9cc78ee1 134 $encodedsearch = urlencode(stripslashes($search));
135 print_paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch&amp;perpage=$perpage&amp;",'page',($perpage == 99999));
ee0378bd 136
137 if ($perpage != 99999 && $totalcount > $perpage) {
138 echo "<center><p>";
9cc78ee1 139 echo "<a href=\"search.php?search=$encodedsearch&perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
ee0378bd 140 echo "</p></center>";
141 }
142
9c37662f 143 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
861efb19 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
155
861efb19 156 echo "<form name=\"movecourses\" action=\"search.php\" method=\"post\">";
157 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\">";
cd7d03fa 158 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search, true)."\">";
861efb19 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)) {
9cc78ee1 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".
861efb19 189 " src=\"$pixpath/t/hide.gif\" height=\"11\" width=\"11\" border=\"0\"></a> ";
190 } else {
9cc78ee1 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".
861efb19 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>";
209
38a10939 210 }
211
9cc78ee1 212 print_paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch&amp;perpage=$perpage&amp;",'page',($perpage == 99999));
861efb19 213
ee0378bd 214 if ($perpage != 99999 && $totalcount > $perpage) {
215 echo "<center><p>";
9cc78ee1 216 echo "<a href=\"search.php?search=$encoededsearch&perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
ee0378bd 217 echo "</p></center>";
218 }
a8b56716 219
38a10939 220 } else {
cd7d03fa 221 print_heading(get_string("nocoursesfound", "", s($search, true)));
38a10939 222 }
223
224 echo "<br /><br />";
225
226 print_course_search($search);
227
228 print_footer();
229
230
231?>
232