Merge branch 'wip-mdl-29274-m21' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / course / search.php
blob03af3900a1669c9df60389948b93381ea65aa0f9
1 <?php
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 $PAGE->set_url('/course/search.php', compact('search', 'page', 'perpage', 'blocklist', 'modulelist', 'edit'));
19 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
20 $search = trim(strip_tags($search)); // trim & clean raw searched string
22 if ($search) {
23 $searchterms = explode(" ", $search); // Search for words independently
24 foreach ($searchterms as $key => $searchterm) {
25 if (strlen($searchterm) < 2) {
26 unset($searchterms[$key]);
29 $search = trim(implode(" ", $searchterms));
32 $site = get_site();
34 $urlparams = array();
35 foreach (array('search', 'page', 'blocklist', 'modulelist') as $param) {
36 if (!empty($$param)) {
37 $urlparams[$param] = $$param;
40 if ($perpage != 10) {
41 $urlparams['perpage'] = $perpage;
43 $PAGE->set_url('/course/search.php', $urlparams);
44 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
45 $PAGE->set_pagelayout('standard');
47 if ($CFG->forcelogin) {
48 require_login();
51 if (can_edit_in_category()) {
52 if ($edit !== -1) {
53 $USER->editing = $edit;
55 $adminediting = $PAGE->user_is_editing();
56 } else {
57 $adminediting = false;
60 /// Editing functions
61 if (has_capability('moodle/course:visibility', get_context_instance(CONTEXT_SYSTEM))) {
62 /// Hide or show a course
63 if ($hide or $show and confirm_sesskey()) {
64 if ($hide) {
65 $course = $DB->get_record("course", array("id"=>$hide));
66 $visible = 0;
67 } else {
68 $course = $DB->get_record("course", array("id"=>$show));
69 $visible = 1;
71 if ($course) {
72 $DB->set_field("course", "visible", $visible, array("id"=>$course->id));
77 $capabilities = array('moodle/course:create', 'moodle/category:manage');
78 if (has_any_capability($capabilities, get_context_instance(CONTEXT_SYSTEM)) && ($perpage != 99999)) {
79 $perpage = 30;
82 $displaylist = array();
83 $parentlist = array();
84 make_categories_list($displaylist, $parentlist);
86 $strcourses = get_string("courses");
87 $strsearch = get_string("search");
88 $strsearchresults = get_string("searchresults");
89 $strcategory = get_string("category");
90 $strselect = get_string("select");
91 $strselectall = get_string("selectall");
92 $strdeselectall = get_string("deselectall");
93 $stredit = get_string("edit");
94 $strfrontpage = get_string('frontpage', 'admin');
95 $strnovalidcourses = get_string('novalidcourses');
97 if (empty($search) and empty($blocklist) and empty($modulelist)) {
98 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
99 $PAGE->navbar->add($strsearch);
100 $PAGE->set_title("$site->fullname : $strsearch");
101 $PAGE->set_heading($site->fullname);
103 echo $OUTPUT->header();
104 echo $OUTPUT->box_start();
105 echo "<center>";
106 echo "<br />";
107 print_course_search("", false, "plain");
108 echo "<br /><p>";
109 print_string("searchhelp");
110 echo "</p>";
111 echo "</center>";
112 echo $OUTPUT->box_end();
113 echo $OUTPUT->footer();
114 exit;
117 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
118 if (! $destcategory = $DB->get_record("course_categories", array("id"=>$data->moveto))) {
119 print_error('cannotfindcategory', '', '', $data->moveto);
122 $courses = array();
123 foreach ( $data as $key => $value ) {
124 if (preg_match('/^c\d+$/', $key)) {
125 array_push($courses, substr($key, 1));
128 move_courses($courses, $data->moveto);
131 // get list of courses containing blocks if required
132 if (!empty($blocklist) and confirm_sesskey()) {
133 $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
134 $courses = array();
135 $courses = $DB->get_records_sql("
136 SELECT * FROM {course} WHERE id IN (
137 SELECT DISTINCT ctx.instanceid
138 FROM {context} ctx
139 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
140 WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
141 array($blockname));
142 $totalcount = count($courses);
143 //Keep only chunk of array which you want to display
144 if ($totalcount > $perpage) {
145 $courses = array_chunk($courses, $perpage, true);
146 $courses = $courses[$page];
148 foreach ($courses as $course) {
149 $courses[$course->id] = $course;
152 // get list of courses containing modules if required
153 elseif (!empty($modulelist) and confirm_sesskey()) {
154 $modulename = $modulelist;
155 $sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
156 ." WHERE module.course=c.id";
158 $courseids = $DB->get_records_sql($sql);
159 $courses = array();
160 if (!empty($courseids)) {
161 $firstcourse = $page*$perpage;
162 $lastcourse = $page*$perpage + $perpage -1;
163 $i = 0;
164 foreach ($courseids as $courseid) {
165 if ($i>= $firstcourse && $i<=$lastcourse) {
166 $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
168 $i++;
170 $totalcount = count($courseids);
172 else {
173 $totalcount = 0;
176 else {
177 $courses = get_courses_search($searchterms, "fullname ASC",
178 $page, $perpage, $totalcount);
181 $searchform = print_course_search($search, true, "navbar");
183 if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
184 $searchform = '';
185 // not sure if this capability is the best here
186 if (has_capability('moodle/category:manage', get_context_instance(CONTEXT_SYSTEM))) {
187 if ($PAGE->user_is_editing()) {
188 $string = get_string("turneditingoff");
189 $edit = "off";
190 } else {
191 $string = get_string("turneditingon");
192 $edit = "on";
195 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", array(
196 'edit' => $edit,
197 'sesskey' => sesskey(),
198 'search' => $search,
199 'page' => $page,
200 'perpage' => $perpage));
201 $searchform = $OUTPUT->single_button($aurl, $string, 'get');
205 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
206 $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
207 if (!empty($search)) {
208 $PAGE->navbar->add(s($search));
210 $PAGE->set_title("$site->fullname : $strsearchresults");
211 $PAGE->set_heading($site->fullname);
212 $PAGE->set_button($searchform);
214 echo $OUTPUT->header();
216 $lastcategory = -1;
217 if ($courses) {
218 echo $OUTPUT->heading("$strsearchresults: $totalcount");
219 $encodedsearch = urlencode($search);
221 // add the module/block parameter to the paging bar if they exists
222 $modulelink = "";
223 if (!empty($modulelist) and confirm_sesskey()) {
224 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
225 } else if (!empty($blocklist) and confirm_sesskey()) {
226 $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
229 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
231 if (!$adminediting) {
232 foreach ($courses as $course) {
234 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
236 $course->summary .= "<br /><p class=\"category\">";
237 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
238 $course->summary .= $displaylist[$course->category];
239 $course->summary .= "</a></p>";
240 print_course($course, $search);
241 echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
243 } else {
244 /// Show editing UI.
245 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
246 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
247 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
248 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
249 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
250 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
251 echo "<th scope=\"col\">$strcourses</th>\n";
252 echo "<th scope=\"col\">$strcategory</th>\n";
253 echo "<th scope=\"col\">$strselect</th>\n";
254 echo "<th scope=\"col\">$stredit</th></tr>\n";
256 foreach ($courses as $course) {
258 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
260 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
262 // are we displaying the front page (courseid=1)?
263 if ($course->id == 1) {
264 echo "<tr>\n";
265 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
267 // can't do anything else with the front page
268 echo " <td>&nbsp;</td>\n"; // category place
269 echo " <td>&nbsp;</td>\n"; // select place
270 echo " <td>&nbsp;</td>\n"; // edit place
271 echo "</tr>\n";
272 continue;
275 echo "<tr>\n";
276 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
277 . highlight($search, format_string($course->fullname)) . "</a></td>\n";
278 echo "<td>".$displaylist[$course->category]."</td>\n";
279 echo "<td>\n";
281 // this is ok since this will get inherited from course category context
282 // if it is set
283 if (has_capability('moodle/category:manage', $coursecontext)) {
284 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
285 } else {
286 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
289 echo "</td>\n";
290 echo "<td>\n";
292 // checks whether user can update course settings
293 if (has_capability('moodle/course:update', $coursecontext)) {
294 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
295 " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
298 // checks whether user can do role assignment
299 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
300 echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
301 echo '<img src="'.$OUTPUT->pix_url('i/users') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
304 // checks whether user can delete course
305 if (has_capability('moodle/course:delete', $coursecontext)) {
306 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
307 " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
310 // checks whether user can change visibility
311 if (has_capability('moodle/course:visibility', $coursecontext)) {
312 if (!empty($course->visible)) {
313 echo "<a title=\"".get_string("hide")."\" href=\"search.php?search=$encodedsearch&amp;perpage=$perpage&amp;page=$page&amp;hide=$course->id&amp;sesskey=".sesskey()."\">\n<img".
314 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
315 } else {
316 echo "<a title=\"".get_string("show")."\" href=\"search.php?search=$encodedsearch&amp;perpage=$perpage&amp;page=$page&amp;show=$course->id&amp;sesskey=".sesskey()."\">\n<img".
317 " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
321 // checks whether user can do site backup
322 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
323 echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\">\n<img".
324 " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
327 // checks whether user can do restore
328 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
329 echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&amp;wdir=/backupdata\">\n<img".
330 " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
333 echo "</td>\n</tr>\n";
335 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
336 echo "<br />";
337 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
338 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
339 echo html_writer::select($displaylist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid'));
340 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
341 echo "</td>\n</tr>\n";
342 echo "</table>\n</form>";
346 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
348 } else {
349 if (!empty($search)) {
350 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
352 else {
353 echo $OUTPUT->heading( $strnovalidcourses );
357 echo "<br /><br />";
359 print_course_search($search);
361 echo $OUTPUT->footer();
364 * Print a list navigation bar
365 * Display page numbers, and a link for displaying all entries
366 * @param integer $totalcount - number of entry to display
367 * @param integer $page - page number
368 * @param integer $perpage - number of entry per page
369 * @param string $encodedsearch
370 * @param string $modulelink - module name
372 function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
373 global $OUTPUT;
374 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
376 //display
377 if ($perpage != 99999 && $totalcount > $perpage) {
378 echo "<center><p>";
379 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
380 echo "</p></center>";
381 } else if ($perpage === 99999) {
382 $defaultperpage = 10;
383 //If user has course:create or category:manage capability the show 30 records.
384 $capabilities = array('moodle/course:create', 'moodle/category:manage');
385 if (has_any_capability($capabilities, get_context_instance(CONTEXT_SYSTEM))) {
386 $defaultperpage = 30;
389 echo "<center><p>";
390 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
391 echo "</p></center>";