Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / course / search.php
blob38bac692e8652b998f5d8848329c2e3809e415c6
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Displays external information about a course
19 * @package core
20 * @category course
21 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once("../config.php");
26 require_once($CFG->dirroot.'/course/lib.php');
27 require_once($CFG->libdir.'/coursecatlib.php');
29 $search = optional_param('search', '', PARAM_RAW); // search words
30 $page = optional_param('page', 0, PARAM_INT); // which page to show
31 $perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
32 $moveto = optional_param('moveto', 0, PARAM_INT); // move to category
33 $edit = optional_param('edit', -1, PARAM_BOOL);
34 $hide = optional_param('hide', 0, PARAM_INT);
35 $show = optional_param('show', 0, PARAM_INT);
36 $blocklist = optional_param('blocklist', 0, PARAM_INT);
37 $modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
39 // List of minimum capabilities which user need to have for editing/moving course
40 $capabilities = array('moodle/course:create', 'moodle/category:manage');
42 // Populate usercatlist with list of category id's with course:create and category:manage capabilities.
43 $usercatlist = coursecat::make_categories_list($capabilities);
45 $search = trim(strip_tags($search)); // trim & clean raw searched string
46 if ($search) {
47 $searchterms = explode(" ", $search); // Search for words independently
48 foreach ($searchterms as $key => $searchterm) {
49 if (strlen($searchterm) < 2) {
50 unset($searchterms[$key]);
53 $search = trim(implode(" ", $searchterms));
56 $site = get_site();
58 $urlparams = array();
59 foreach (array('search', 'page', 'blocklist', 'modulelist', 'edit') as $param) {
60 if (!empty($$param)) {
61 $urlparams[$param] = $$param;
64 if ($perpage != 10) {
65 $urlparams['perpage'] = $perpage;
67 $PAGE->set_url('/course/search.php', $urlparams);
68 $PAGE->set_context(context_system::instance());
69 $PAGE->set_pagelayout('standard');
71 if ($CFG->forcelogin) {
72 require_login();
75 // Editing is possible if user has system or category level create and manage capability
76 if (can_edit_in_category() || !empty($usercatlist)) {
77 if ($edit !== -1) {
78 $USER->editing = $edit;
80 $adminediting = $PAGE->user_is_editing();
82 // Set perpage if user can edit in category
83 if ($perpage != 99999) {
84 $perpage = 30;
86 } else {
87 $adminediting = false;
90 // Editing functions
91 if (has_capability('moodle/course:visibility', context_system::instance())) {
92 // Hide or show a course
93 if (($hide || $show) && confirm_sesskey()) {
94 if ($hide) {
95 $course = $DB->get_record("course", array("id" => $hide));
96 $visible = 0;
97 } else {
98 $course = $DB->get_record("course", array("id" => $show));
99 $visible = 1;
101 if ($course) {
102 $DB->set_field("course", "visible", $visible, array("id" => $course->id));
107 $displaylist = coursecat::make_categories_list();
109 $strcourses = new lang_string("courses");
110 $strsearch = new lang_string("search");
111 $strsearchresults = new lang_string("searchresults");
112 $strcategory = new lang_string("category");
113 $strselect = new lang_string("select");
114 $strselectall = new lang_string("selectall");
115 $strdeselectall = new lang_string("deselectall");
116 $stredit = new lang_string("edit");
117 $strfrontpage = new lang_string('frontpage', 'admin');
118 $strnovalidcourses = new lang_string('novalidcourses');
120 if (empty($search) and empty($blocklist) and empty($modulelist) and empty($moveto) and ($edit != -1)) {
121 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
122 $PAGE->navbar->add($strsearch);
123 $PAGE->set_title("$site->fullname : $strsearch");
124 $PAGE->set_heading($site->fullname);
126 echo $OUTPUT->header();
127 echo $OUTPUT->box_start();
128 echo "<center>";
129 echo "<br />";
130 print_course_search("", false, "plain");
131 echo "<br /><p>";
132 print_string("searchhelp");
133 echo "</p>";
134 echo "</center>";
135 echo $OUTPUT->box_end();
136 echo $OUTPUT->footer();
137 exit;
140 $courses = array();
141 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
142 if (!$destcategory = $DB->get_record("course_categories", array("id" => $moveto))) {
143 print_error('cannotfindcategory', '', '', $moveto);
146 // User should have manage and create capablity on destination category.
147 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
148 require_capability('moodle/course:create', context_coursecat::instance($moveto));
150 foreach ( $data as $key => $value ) {
151 if (preg_match('/^c\d+$/', $key)) {
152 $courseid = substr($key, 1);
153 // user must have category:manage and course:create capability for the course to be moved.
154 $coursecontext = context_course::instance($courseid);
155 foreach ($capabilities as $capability) {
156 // Require capability here will result in a fatal error should the user not
157 // have the requried category ensuring that no moves occur if they are
158 // trying to move multiple courses.
159 require_capability($capability, $coursecontext);
160 array_push($courses, $courseid);
164 move_courses($courses, $moveto);
167 // get list of courses containing blocks if required
168 if (!empty($blocklist) and confirm_sesskey()) {
169 $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
170 $courses = array();
171 list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
172 $sql = "SELECT c.* $select FROM {course} c
173 $join JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
174 WHERE bi.blockname = ?";
175 $courses = $DB->get_records_sql($sql, array($blockname));
176 $totalcount = count($courses);
177 // Keep only chunk of array which you want to display
178 if ($totalcount > $perpage) {
179 $courses = array_chunk($courses, $perpage, true);
180 $courses = $courses[$page];
182 foreach ($courses as $course) {
183 $courses[$course->id] = $course;
185 } elseif (!empty($modulelist) and confirm_sesskey()) { // get list of courses containing modules
186 $modulename = $modulelist;
187 list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
188 $sql = "SELECT c.* $select FROM {course} c $join
189 WHERE c.id IN (SELECT DISTINCT cc.id FROM {".$modulelist."} module, {course} cc
190 WHERE module.course = cc.id)";
191 $courselist = $DB->get_records_sql($sql);
192 $courses = array();
193 if (!empty($courselist)) {
194 $firstcourse = $page*$perpage;
195 $lastcourse = $page*$perpage + $perpage -1;
196 $i = 0;
197 foreach ($courselist as $course) {
198 if ($i >= $firstcourse && $i <= $lastcourse) {
199 $courses[$course->id] = $course;
201 $i++;
204 $totalcount = count($courselist);
205 } else if (!empty($searchterm)) {
206 // Donot do search for empty search request.
207 $courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount);
210 $searchform = '';
211 // Turn editing should be visible if user have system or category level capability
212 if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
213 if ($PAGE->user_is_editing()) {
214 $string = new lang_string("turneditingoff");
215 $edit = "off";
216 } else {
217 $string = new lang_string("turneditingon");
218 $edit = "on";
220 $params = array_merge($urlparams, array('sesskey' => sesskey(), 'edit' => $edit));
221 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", $params);
222 $searchform = $OUTPUT->single_button($aurl, $string, 'get');
223 } else {
224 $searchform = print_course_search($search, true, "navbar");
227 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
228 $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
229 if (!empty($search)) {
230 $PAGE->navbar->add(s($search));
232 $PAGE->set_title("$site->fullname : $strsearchresults");
233 $PAGE->set_heading($site->fullname);
234 $PAGE->set_button($searchform);
236 echo $OUTPUT->header();
238 $lastcategory = -1;
239 if ($courses) {
240 echo $OUTPUT->heading("$strsearchresults: $totalcount");
241 $encodedsearch = urlencode($search);
243 // add the module/block parameter to the paging bar if they exists
244 $modulelink = "";
245 if (!empty($modulelist) and confirm_sesskey()) {
246 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
247 } else if (!empty($blocklist) and confirm_sesskey()) {
248 $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
251 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
253 // Show list of courses
254 if (!$adminediting) { //Not editing mode
255 foreach ($courses as $course) {
256 // front page don't belong to any category and block can exist.
257 if ($course->category > 0) {
258 $course->summary .= "<br /><p class=\"category\">";
259 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
260 $course->summary .= $displaylist[$course->category];
261 $course->summary .= "</a></p>";
263 print_course($course, $search);
264 echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
266 } else {
267 // Editing mode
268 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
269 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
270 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
271 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
272 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
273 if (!empty($modulelist) and confirm_sesskey()) {
274 echo "<input type=\"hidden\" name=\"modulelist\" value=\"$modulelist\" /></div>\n";
275 } else if (!empty($blocklist) and confirm_sesskey()) {
276 echo "<input type=\"hidden\" name=\"blocklist\" value=\"$blocklist\" /></div>\n";
278 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generaltable boxaligncenter\">\n<tr>\n";
279 echo "<th scope=\"col\">$strcourses</th>\n";
280 echo "<th scope=\"col\">$strcategory</th>\n";
281 echo "<th scope=\"col\">$strselect</th>\n";
282 echo "<th scope=\"col\">$stredit</th></tr>\n";
284 foreach ($courses as $course) {
286 context_helper::preload_from_record($course);
287 $coursecontext = context_course::instance($course->id);
289 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
291 // are we displaying the front page (courseid=1)?
292 if ($course->id == 1) {
293 echo "<tr>\n";
294 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
296 // can't do anything else with the front page
297 echo " <td>&nbsp;</td>\n"; // category place
298 echo " <td>&nbsp;</td>\n"; // select place
299 echo " <td>&nbsp;</td>\n"; // edit place
300 echo "</tr>\n";
301 continue;
304 echo "<tr>\n";
305 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
306 . highlight($search, $coursecontext->get_context_name(false)) . "</a></td>\n";
307 echo "<td>".$displaylist[$course->category]."</td>\n";
308 echo "<td>\n";
310 // If user has all required capabilities to move course then show selectable checkbox
311 if (has_all_capabilities($capabilities, $coursecontext)) {
312 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
313 } else {
314 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
317 echo "</td>\n";
318 echo "<td>\n";
320 // checks whether user can update course settings
321 if (has_capability('moodle/course:update', $coursecontext)) {
322 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
323 " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
326 // checks whether user can do role assignment
327 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
328 echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
329 echo '<img src="'.$OUTPUT->pix_url('i/enrolusers') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
332 // checks whether user can delete course
333 if (has_capability('moodle/course:delete', $coursecontext)) {
334 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
335 " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
338 // checks whether user can change visibility
339 if (has_capability('moodle/course:visibility', $coursecontext)) {
340 if (!empty($course->visible)) {
341 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".
342 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
343 } else {
344 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".
345 " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
349 // checks whether user can do site backup
350 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
351 $backupurl = new moodle_url('/backup/backup.php', array('id' => $course->id));
352 echo "<a title=\"".get_string("backup")."\" href=\"".$backupurl."\">\n<img".
353 " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
356 // checks whether user can do restore
357 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
358 $restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
359 echo "<a title=\"".get_string("restore")."\" href=\"".$restoreurl."\">\n<img".
360 " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
363 echo "</td>\n</tr>\n";
365 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
366 echo "<br />";
367 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
368 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
369 // Select box should only show categories in which user has min capability to move course.
370 echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
371 echo html_writer::select($usercatlist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid', 'class' => 'autosubmit'));
372 $PAGE->requires->yui_module('moodle-core-formautosubmit',
373 'M.core.init_formautosubmit',
374 array(array('selectid' => 'movetoid', 'nothing' => false))
376 echo "</td>\n</tr>\n";
377 echo "</table>\n</form>";
381 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
383 } else {
384 if (!empty($search)) {
385 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
387 else {
388 echo $OUTPUT->heading($strnovalidcourses);
392 echo "<br /><br />";
394 print_course_search($search);
396 echo $OUTPUT->footer();
399 * Print a list navigation bar
400 * Display page numbers, and a link for displaying all entries
401 * @param int $totalcount number of entry to display
402 * @param int $page page number
403 * @param int $perpage number of entry per page
404 * @param string $encodedsearch
405 * @param string $modulelink module name
407 function print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink) {
408 global $OUTPUT;
409 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
411 // display
412 if ($perpage != 99999 && $totalcount > $perpage) {
413 echo "<center><p>";
414 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
415 echo "</p></center>";
416 } else if ($perpage === 99999) {
417 $defaultperpage = 10;
418 // If user has course:create or category:manage capability the show 30 records.
419 $capabilities = array('moodle/course:create', 'moodle/category:manage');
420 if (has_any_capability($capabilities, context_system::instance())) {
421 $defaultperpage = 30;
424 echo "<center><p>";
425 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
426 echo "</p></center>";