Merge branch 'wip-MDL-39456-m24' of git://github.com/samhemelryk/moodle into MOODLE_2...
[moodle.git] / course / search.php
blob721d9681b1e2ff74cac6d0c115acbf2986061f81
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');
28 $search = optional_param('search', '', PARAM_RAW); // search words
29 $page = optional_param('page', 0, PARAM_INT); // which page to show
30 $perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
31 $moveto = optional_param('moveto', 0, PARAM_INT); // move to category
32 $edit = optional_param('edit', -1, PARAM_BOOL);
33 $hide = optional_param('hide', 0, PARAM_INT);
34 $show = optional_param('show', 0, PARAM_INT);
35 $blocklist = optional_param('blocklist', 0, PARAM_INT);
36 $modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
38 // List of minimum capabilities which user need to have for editing/moving course
39 $capabilities = array('moodle/course:create', 'moodle/category:manage');
41 // List of category id's in which current user has course:create and category:manage capability.
42 $usercatlist = array();
44 // List of parent category id's
45 $catparentlist = array();
47 // Populate usercatlist with list of category id's with required capabilities.
48 make_categories_list($usercatlist, $catparentlist, $capabilities);
50 $search = trim(strip_tags($search)); // trim & clean raw searched string
51 if ($search) {
52 $searchterms = explode(" ", $search); // Search for words independently
53 foreach ($searchterms as $key => $searchterm) {
54 if (strlen($searchterm) < 2) {
55 unset($searchterms[$key]);
58 $search = trim(implode(" ", $searchterms));
61 $site = get_site();
63 $urlparams = array();
64 foreach (array('search', 'page', 'blocklist', 'modulelist', 'edit') as $param) {
65 if (!empty($$param)) {
66 $urlparams[$param] = $$param;
69 if ($perpage != 10) {
70 $urlparams['perpage'] = $perpage;
72 $PAGE->set_url('/course/search.php', $urlparams);
73 $PAGE->set_context(context_system::instance());
74 $PAGE->set_pagelayout('standard');
76 if ($CFG->forcelogin) {
77 require_login();
80 // Editing is possible if user has system or category level create and manage capability
81 if (can_edit_in_category() || !empty($usercatlist)) {
82 if ($edit !== -1) {
83 $USER->editing = $edit;
85 $adminediting = $PAGE->user_is_editing();
87 // Set perpage if user can edit in category
88 if ($perpage != 99999) {
89 $perpage = 30;
91 } else {
92 $adminediting = false;
95 // Editing functions
96 if (has_capability('moodle/course:visibility', context_system::instance())) {
97 // Hide or show a course
98 if (($hide || $show) && confirm_sesskey()) {
99 if ($hide) {
100 $course = $DB->get_record("course", array("id" => $hide));
101 $visible = 0;
102 } else {
103 $course = $DB->get_record("course", array("id" => $show));
104 $visible = 1;
106 if ($course) {
107 $DB->set_field("course", "visible", $visible, array("id" => $course->id));
112 $displaylist = array();
113 $parentlist = array();
114 make_categories_list($displaylist, $parentlist);
116 $strcourses = new lang_string("courses");
117 $strsearch = new lang_string("search");
118 $strsearchresults = new lang_string("searchresults");
119 $strcategory = new lang_string("category");
120 $strselect = new lang_string("select");
121 $strselectall = new lang_string("selectall");
122 $strdeselectall = new lang_string("deselectall");
123 $stredit = new lang_string("edit");
124 $strfrontpage = new lang_string('frontpage', 'admin');
125 $strnovalidcourses = new lang_string('novalidcourses');
127 if (empty($search) and empty($blocklist) and empty($modulelist) and empty($moveto) and ($edit != -1)) {
128 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
129 $PAGE->navbar->add($strsearch);
130 $PAGE->set_title("$site->fullname : $strsearch");
131 $PAGE->set_heading($site->fullname);
133 echo $OUTPUT->header();
134 echo $OUTPUT->box_start();
135 echo "<center>";
136 echo "<br />";
137 print_course_search("", false, "plain");
138 echo "<br /><p>";
139 print_string("searchhelp");
140 echo "</p>";
141 echo "</center>";
142 echo $OUTPUT->box_end();
143 echo $OUTPUT->footer();
144 exit;
147 $courses = array();
148 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
149 if (!$destcategory = $DB->get_record("course_categories", array("id" => $moveto))) {
150 print_error('cannotfindcategory', '', '', $moveto);
153 // User should have manage and create capablity on destination category.
154 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
155 require_capability('moodle/course:create', context_coursecat::instance($moveto));
157 foreach ( $data as $key => $value ) {
158 if (preg_match('/^c\d+$/', $key)) {
159 $courseid = substr($key, 1);
160 // user must have category:manage and course:create capability for the course to be moved.
161 $coursecontext = context_course::instance($courseid);
162 foreach ($capabilities as $capability) {
163 // Require capability here will result in a fatal error should the user not
164 // have the requried category ensuring that no moves occur if they are
165 // trying to move multiple courses.
166 require_capability($capability, $coursecontext);
167 array_push($courses, $courseid);
171 move_courses($courses, $moveto);
174 // get list of courses containing blocks if required
175 if (!empty($blocklist) and confirm_sesskey()) {
176 $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
177 $courses = array();
178 list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
179 $sql = "SELECT c.* $select FROM {course} c
180 $join JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
181 WHERE bi.blockname = ?";
182 $courses = $DB->get_records_sql($sql, array($blockname));
183 $totalcount = count($courses);
184 // Keep only chunk of array which you want to display
185 if ($totalcount > $perpage) {
186 $courses = array_chunk($courses, $perpage, true);
187 $courses = $courses[$page];
189 foreach ($courses as $course) {
190 $courses[$course->id] = $course;
192 } elseif (!empty($modulelist) and confirm_sesskey()) { // get list of courses containing modules
193 $modulename = $modulelist;
194 list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
195 $sql = "SELECT c.* $select FROM {course} c $join
196 WHERE c.id IN (SELECT DISTINCT cc.id FROM {".$modulelist."} module, {course} cc
197 WHERE module.course = cc.id)";
198 $courselist = $DB->get_records_sql($sql);
199 $courses = array();
200 if (!empty($courselist)) {
201 $firstcourse = $page*$perpage;
202 $lastcourse = $page*$perpage + $perpage -1;
203 $i = 0;
204 foreach ($courselist as $course) {
205 if ($i >= $firstcourse && $i <= $lastcourse) {
206 $courses[$course->id] = $course;
208 $i++;
211 $totalcount = count($courselist);
212 } else if (!empty($searchterm)) {
213 // Donot do search for empty search request.
214 $courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount);
217 $searchform = '';
218 // Turn editing should be visible if user have system or category level capability
219 if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
220 if ($PAGE->user_is_editing()) {
221 $string = new lang_string("turneditingoff");
222 $edit = "off";
223 } else {
224 $string = new lang_string("turneditingon");
225 $edit = "on";
227 $params = array_merge($urlparams, array('sesskey' => sesskey(), 'edit' => $edit));
228 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", $params);
229 $searchform = $OUTPUT->single_button($aurl, $string, 'get');
230 } else {
231 $searchform = print_course_search($search, true, "navbar");
234 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
235 $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
236 if (!empty($search)) {
237 $PAGE->navbar->add(s($search));
239 $PAGE->set_title("$site->fullname : $strsearchresults");
240 $PAGE->set_heading($site->fullname);
241 $PAGE->set_button($searchform);
243 echo $OUTPUT->header();
245 $lastcategory = -1;
246 if ($courses) {
247 echo $OUTPUT->heading("$strsearchresults: $totalcount");
248 $encodedsearch = urlencode($search);
250 // add the module/block parameter to the paging bar if they exists
251 $modulelink = "";
252 if (!empty($modulelist) and confirm_sesskey()) {
253 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
254 } else if (!empty($blocklist) and confirm_sesskey()) {
255 $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
258 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
260 // Show list of courses
261 if (!$adminediting) { //Not editing mode
262 foreach ($courses as $course) {
263 // front page don't belong to any category and block can exist.
264 if ($course->category > 0) {
265 $course->summary .= "<br /><p class=\"category\">";
266 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
267 $course->summary .= $displaylist[$course->category];
268 $course->summary .= "</a></p>";
270 print_course($course, $search);
271 echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
273 } else {
274 // Editing mode
275 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
276 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
277 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
278 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
279 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
280 if (!empty($modulelist) and confirm_sesskey()) {
281 echo "<input type=\"hidden\" name=\"modulelist\" value=\"$modulelist\" /></div>\n";
282 } else if (!empty($blocklist) and confirm_sesskey()) {
283 echo "<input type=\"hidden\" name=\"blocklist\" value=\"$blocklist\" /></div>\n";
285 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
286 echo "<th scope=\"col\">$strcourses</th>\n";
287 echo "<th scope=\"col\">$strcategory</th>\n";
288 echo "<th scope=\"col\">$strselect</th>\n";
289 echo "<th scope=\"col\">$stredit</th></tr>\n";
291 foreach ($courses as $course) {
293 context_helper::preload_from_record($course);
294 $coursecontext = context_course::instance($course->id);
296 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
298 // are we displaying the front page (courseid=1)?
299 if ($course->id == 1) {
300 echo "<tr>\n";
301 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
303 // can't do anything else with the front page
304 echo " <td>&nbsp;</td>\n"; // category place
305 echo " <td>&nbsp;</td>\n"; // select place
306 echo " <td>&nbsp;</td>\n"; // edit place
307 echo "</tr>\n";
308 continue;
311 echo "<tr>\n";
312 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
313 . highlight($search, $coursecontext->get_context_name(false)) . "</a></td>\n";
314 echo "<td>".$displaylist[$course->category]."</td>\n";
315 echo "<td>\n";
317 // If user has all required capabilities to move course then show selectable checkbox
318 if (has_all_capabilities($capabilities, $coursecontext)) {
319 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
320 } else {
321 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
324 echo "</td>\n";
325 echo "<td>\n";
327 // checks whether user can update course settings
328 if (has_capability('moodle/course:update', $coursecontext)) {
329 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
330 " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
333 // checks whether user can do role assignment
334 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
335 echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
336 echo '<img src="'.$OUTPUT->pix_url('i/enrolusers') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
339 // checks whether user can delete course
340 if (has_capability('moodle/course:delete', $coursecontext)) {
341 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
342 " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
345 // checks whether user can change visibility
346 if (has_capability('moodle/course:visibility', $coursecontext)) {
347 if (!empty($course->visible)) {
348 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".
349 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
350 } else {
351 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".
352 " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
356 // checks whether user can do site backup
357 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
358 $backupurl = new moodle_url('/backup/backup.php', array('id' => $course->id));
359 echo "<a title=\"".get_string("backup")."\" href=\"".$backupurl."\">\n<img".
360 " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
363 // checks whether user can do restore
364 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
365 $restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
366 echo "<a title=\"".get_string("restore")."\" href=\"".$restoreurl."\">\n<img".
367 " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
370 echo "</td>\n</tr>\n";
372 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
373 echo "<br />";
374 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
375 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
376 // Select box should only show categories in which user has min capability to move course.
377 echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
378 echo html_writer::select($usercatlist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid', 'class' => 'autosubmit'));
379 $PAGE->requires->yui_module('moodle-core-formautosubmit',
380 'M.core.init_formautosubmit',
381 array(array('selectid' => 'movetoid', 'nothing' => false))
383 echo "</td>\n</tr>\n";
384 echo "</table>\n</form>";
388 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
390 } else {
391 if (!empty($search)) {
392 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
394 else {
395 echo $OUTPUT->heading($strnovalidcourses);
399 echo "<br /><br />";
401 print_course_search($search);
403 echo $OUTPUT->footer();
406 * Print a list navigation bar
407 * Display page numbers, and a link for displaying all entries
408 * @param int $totalcount number of entry to display
409 * @param int $page page number
410 * @param int $perpage number of entry per page
411 * @param string $encodedsearch
412 * @param string $modulelink module name
414 function print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink) {
415 global $OUTPUT;
416 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
418 // display
419 if ($perpage != 99999 && $totalcount > $perpage) {
420 echo "<center><p>";
421 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
422 echo "</p></center>";
423 } else if ($perpage === 99999) {
424 $defaultperpage = 10;
425 // If user has course:create or category:manage capability the show 30 records.
426 $capabilities = array('moodle/course:create', 'moodle/category:manage');
427 if (has_any_capability($capabilities, context_system::instance())) {
428 $defaultperpage = 30;
431 echo "<center><p>";
432 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
433 echo "</p></center>";