Merge branch 'w17_MDL-32574_m23_adodb' of git://github.com/skodak/moodle
[moodle.git] / course / search.php
blob246577cd0344d3b70aeaaeeb1b6017fd43b944d1
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 $courses = $DB->get_records_sql("
179 SELECT * FROM {course} WHERE id IN (
180 SELECT DISTINCT ctx.instanceid
181 FROM {context} ctx
182 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
183 WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
184 array($blockname));
185 $totalcount = count($courses);
186 // Keep only chunk of array which you want to display
187 if ($totalcount > $perpage) {
188 $courses = array_chunk($courses, $perpage, true);
189 $courses = $courses[$page];
191 foreach ($courses as $course) {
192 $courses[$course->id] = $course;
194 } elseif (!empty($modulelist) and confirm_sesskey()) { // get list of courses containing modules
195 $modulename = $modulelist;
196 $sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
197 ." WHERE module.course=c.id";
199 $courseids = $DB->get_records_sql($sql);
200 $courses = array();
201 if (!empty($courseids)) {
202 $firstcourse = $page*$perpage;
203 $lastcourse = $page*$perpage + $perpage -1;
204 $i = 0;
205 foreach ($courseids as $courseid) {
206 if ($i >= $firstcourse && $i <= $lastcourse) {
207 $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
209 $i++;
211 $totalcount = count($courseids);
213 else {
214 $totalcount = 0;
216 } else if (!empty($searchterm)) {
217 // Donot do search for empty search request.
218 $courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount);
221 $searchform = '';
222 // Turn editing should be visible if user have system or category level capability
223 if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) {
224 if ($PAGE->user_is_editing()) {
225 $string = new lang_string("turneditingoff");
226 $edit = "off";
227 } else {
228 $string = new lang_string("turneditingon");
229 $edit = "on";
231 $params = array_merge($urlparams, array('sesskey' => sesskey(), 'edit' => $edit));
232 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", $params);
233 $searchform = $OUTPUT->single_button($aurl, $string, 'get');
234 } else {
235 $searchform = print_course_search($search, true, "navbar");
238 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
239 $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
240 if (!empty($search)) {
241 $PAGE->navbar->add(s($search));
243 $PAGE->set_title("$site->fullname : $strsearchresults");
244 $PAGE->set_heading($site->fullname);
245 $PAGE->set_button($searchform);
247 echo $OUTPUT->header();
249 $lastcategory = -1;
250 if ($courses) {
251 echo $OUTPUT->heading("$strsearchresults: $totalcount");
252 $encodedsearch = urlencode($search);
254 // add the module/block parameter to the paging bar if they exists
255 $modulelink = "";
256 if (!empty($modulelist) and confirm_sesskey()) {
257 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
258 } else if (!empty($blocklist) and confirm_sesskey()) {
259 $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
262 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
264 // Show list of courses
265 if (!$adminediting) { //Not editing mode
266 foreach ($courses as $course) {
267 // front page don't belong to any category and block can exist.
268 if ($course->category > 0) {
269 $course->summary .= "<br /><p class=\"category\">";
270 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
271 $course->summary .= $displaylist[$course->category];
272 $course->summary .= "</a></p>";
274 print_course($course, $search);
275 echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
277 } else {
278 // Editing mode
279 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
280 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
281 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
282 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
283 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
284 if (!empty($modulelist) and confirm_sesskey()) {
285 echo "<input type=\"hidden\" name=\"modulelist\" value=\"$modulelist\" /></div>\n";
286 } else if (!empty($blocklist) and confirm_sesskey()) {
287 echo "<input type=\"hidden\" name=\"blocklist\" value=\"$blocklist\" /></div>\n";
289 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
290 echo "<th scope=\"col\">$strcourses</th>\n";
291 echo "<th scope=\"col\">$strcategory</th>\n";
292 echo "<th scope=\"col\">$strselect</th>\n";
293 echo "<th scope=\"col\">$stredit</th></tr>\n";
295 foreach ($courses as $course) {
297 $coursecontext = context_course::instance($course->id);
299 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
301 // are we displaying the front page (courseid=1)?
302 if ($course->id == 1) {
303 echo "<tr>\n";
304 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
306 // can't do anything else with the front page
307 echo " <td>&nbsp;</td>\n"; // category place
308 echo " <td>&nbsp;</td>\n"; // select place
309 echo " <td>&nbsp;</td>\n"; // edit place
310 echo "</tr>\n";
311 continue;
314 echo "<tr>\n";
315 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
316 . highlight($search, format_string($course->fullname)) . "</a></td>\n";
317 echo "<td>".$displaylist[$course->category]."</td>\n";
318 echo "<td>\n";
320 // If user has all required capabilities to move course then show selectable checkbox
321 if (has_all_capabilities($capabilities, $coursecontext)) {
322 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
323 } else {
324 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
327 echo "</td>\n";
328 echo "<td>\n";
330 // checks whether user can update course settings
331 if (has_capability('moodle/course:update', $coursecontext)) {
332 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
333 " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
336 // checks whether user can do role assignment
337 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
338 echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
339 echo '<img src="'.$OUTPUT->pix_url('i/users') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
342 // checks whether user can delete course
343 if (has_capability('moodle/course:delete', $coursecontext)) {
344 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
345 " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
348 // checks whether user can change visibility
349 if (has_capability('moodle/course:visibility', $coursecontext)) {
350 if (!empty($course->visible)) {
351 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".
352 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
353 } else {
354 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".
355 " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
359 // checks whether user can do site backup
360 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
361 $backupurl = new moodle_url('/backup/backup.php', array('id' => $course->id));
362 echo "<a title=\"".get_string("backup")."\" href=\"".$backupurl."\">\n<img".
363 " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
366 // checks whether user can do restore
367 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
368 $restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
369 echo "<a title=\"".get_string("restore")."\" href=\"".$restoreurl."\">\n<img".
370 " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
373 echo "</td>\n</tr>\n";
375 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
376 echo "<br />";
377 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
378 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
379 // Select box should only show categories in which user has min capability to move course.
380 echo html_writer::select($usercatlist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid'));
381 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
382 echo "</td>\n</tr>\n";
383 echo "</table>\n</form>";
387 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
389 } else {
390 if (!empty($search)) {
391 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
393 else {
394 echo $OUTPUT->heading($strnovalidcourses);
398 echo "<br /><br />";
400 print_course_search($search);
402 echo $OUTPUT->footer();
405 * Print a list navigation bar
406 * Display page numbers, and a link for displaying all entries
407 * @param int $totalcount number of entry to display
408 * @param int $page page number
409 * @param int $perpage number of entry per page
410 * @param string $encodedsearch
411 * @param string $modulelink module name
413 function print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink) {
414 global $OUTPUT;
415 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
417 // display
418 if ($perpage != 99999 && $totalcount > $perpage) {
419 echo "<center><p>";
420 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
421 echo "</p></center>";
422 } else if ($perpage === 99999) {
423 $defaultperpage = 10;
424 // If user has course:create or category:manage capability the show 30 records.
425 $capabilities = array('moodle/course:create', 'moodle/category:manage');
426 if (has_any_capability($capabilities, context_system::instance())) {
427 $defaultperpage = 30;
430 echo "<center><p>";
431 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
432 echo "</p></center>";