MDL-29030 move user log reporting to report_log plugin
[moodle.git] / course / search.php
blob4433d0375bf359253f173105be4a72a9377c7f6c
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_PLUGIN);
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));
46 if ($CFG->forcelogin) {
47 require_login();
50 if (can_edit_in_category()) {
51 if ($edit !== -1) {
52 $USER->editing = $edit;
54 $adminediting = $PAGE->user_is_editing();
55 } else {
56 $adminediting = false;
59 /// Editing functions
60 if (has_capability('moodle/course:visibility', get_context_instance(CONTEXT_SYSTEM))) {
61 /// Hide or show a course
62 if ($hide or $show and confirm_sesskey()) {
63 if ($hide) {
64 $course = $DB->get_record("course", array("id"=>$hide));
65 $visible = 0;
66 } else {
67 $course = $DB->get_record("course", array("id"=>$show));
68 $visible = 1;
70 if ($course) {
71 $DB->set_field("course", "visible", $visible, array("id"=>$course->id));
76 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM)) && $perpage != 99999) {
77 $perpage = 30;
80 $displaylist = array();
81 $parentlist = array();
82 make_categories_list($displaylist, $parentlist);
84 $strcourses = get_string("courses");
85 $strsearch = get_string("search");
86 $strsearchresults = get_string("searchresults");
87 $strcategory = get_string("category");
88 $strselect = get_string("select");
89 $strselectall = get_string("selectall");
90 $strdeselectall = get_string("deselectall");
91 $stredit = get_string("edit");
92 $strfrontpage = get_string('frontpage', 'admin');
93 $strnovalidcourses = get_string('novalidcourses');
95 if (empty($search) and empty($blocklist) and empty($modulelist)) {
96 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
97 $PAGE->navbar->add($strsearch);
98 $PAGE->set_title("$site->fullname : $strsearch");
99 $PAGE->set_heading($site->fullname);
101 echo $OUTPUT->header();
102 echo $OUTPUT->box_start();
103 echo "<center>";
104 echo "<br />";
105 print_course_search("", false, "plain");
106 echo "<br /><p>";
107 print_string("searchhelp");
108 echo "</p>";
109 echo "</center>";
110 echo $OUTPUT->box_end();
111 echo $OUTPUT->footer();
112 exit;
115 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
116 if (! $destcategory = $DB->get_record("course_categories", array("id"=>$data->moveto))) {
117 print_error('cannotfindcategory', '', '', $data->moveto);
120 $courses = array();
121 foreach ( $data as $key => $value ) {
122 if (preg_match('/^c\d+$/', $key)) {
123 array_push($courses, substr($key, 1));
126 move_courses($courses, $data->moveto);
129 // get list of courses containing blocks if required
130 if (!empty($blocklist) and confirm_sesskey()) {
131 $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
132 $courses = array();
133 $courses = $DB->get_records_sql("
134 SELECT * FROM {course} WHERE id IN (
135 SELECT DISTINCT ctx.instanceid
136 FROM {context} ctx
137 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
138 WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
139 array($blockname));
140 foreach ($courses as $course) {
141 $courses[$course->id] = $course;
143 $totalcount = count($courses);
145 // get list of courses containing modules if required
146 elseif (!empty($modulelist) and confirm_sesskey()) {
147 $modulename = $modulelist;
148 $sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
149 ." WHERE module.course=c.id";
151 $courseids = $DB->get_records_sql($sql);
152 $courses = array();
153 if (!empty($courseids)) {
154 $firstcourse = $page*$perpage;
155 $lastcourse = $page*$perpage + $perpage -1;
156 $i = 0;
157 foreach ($courseids as $courseid) {
158 if ($i>= $firstcourse && $i<=$lastcourse) {
159 $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
161 $i++;
163 $totalcount = count($courseids);
165 else {
166 $totalcount = 0;
169 else {
170 $courses = get_courses_search($searchterms, "fullname ASC",
171 $page, $perpage, $totalcount);
174 $searchform = print_course_search($search, true, "navbar");
176 if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
177 $searchform = '';
178 // not sure if this capability is the best here
179 if (has_capability('moodle/category:manage', get_context_instance(CONTEXT_SYSTEM))) {
180 if ($PAGE->user_is_editing()) {
181 $string = get_string("turneditingoff");
182 $edit = "off";
183 $perpage = 30;
184 } else {
185 $string = get_string("turneditingon");
186 $edit = "on";
189 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", array(
190 'edit' => $edit,
191 'sesskey' => sesskey(),
192 'search' => $search,
193 'page' => $page,
194 'perpage' => $perpage));
195 $searchform = $OUTPUT->single_button($aurl, $string, 'get');
199 $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
200 $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
201 if (!empty($search)) {
202 $PAGE->navbar->add(s($search));
204 $PAGE->set_title("$site->fullname : $strsearchresults");
205 $PAGE->set_heading($site->fullname);
206 $PAGE->set_button($searchform);
208 echo $OUTPUT->header();
210 $lastcategory = -1;
211 if ($courses) {
212 echo $OUTPUT->heading("$strsearchresults: $totalcount");
213 $encodedsearch = urlencode($search);
215 ///add the module parameter to the paging bar if they exists
216 $modulelink = "";
217 if (!empty($modulelist) and confirm_sesskey()) {
218 $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
221 print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
223 if (!$adminediting) {
224 foreach ($courses as $course) {
226 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
228 $course->summary .= "<br /><p class=\"category\">";
229 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
230 $course->summary .= $displaylist[$course->category];
231 $course->summary .= "</a></p>";
232 print_course($course, $search);
233 echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
235 } else {
236 /// Show editing UI.
237 echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
238 echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
239 echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
240 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
241 echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
242 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
243 echo "<th scope=\"col\">$strcourses</th>\n";
244 echo "<th scope=\"col\">$strcategory</th>\n";
245 echo "<th scope=\"col\">$strselect</th>\n";
246 echo "<th scope=\"col\">$stredit</th></tr>\n";
248 foreach ($courses as $course) {
250 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
252 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
254 // are we displaying the front page (courseid=1)?
255 if ($course->id == 1) {
256 echo "<tr>\n";
257 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
259 // can't do anything else with the front page
260 echo " <td>&nbsp;</td>\n"; // category place
261 echo " <td>&nbsp;</td>\n"; // select place
262 echo " <td>&nbsp;</td>\n"; // edit place
263 echo "</tr>\n";
264 continue;
267 echo "<tr>\n";
268 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
269 . highlight($search, format_string($course->fullname)) . "</a></td>\n";
270 echo "<td>".$displaylist[$course->category]."</td>\n";
271 echo "<td>\n";
273 // this is ok since this will get inherited from course category context
274 // if it is set
275 if (has_capability('moodle/category:manage', $coursecontext)) {
276 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
277 } else {
278 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
281 echo "</td>\n";
282 echo "<td>\n";
284 // checks whether user can update course settings
285 if (has_capability('moodle/course:update', $coursecontext)) {
286 echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
287 " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
290 // checks whether user can do role assignment
291 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
292 echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
293 echo '<img src="'.$OUTPUT->pix_url('i/users') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
296 // checks whether user can delete course
297 if (has_capability('moodle/course:delete', $coursecontext)) {
298 echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
299 " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
302 // checks whether user can change visibility
303 if (has_capability('moodle/course:visibility', $coursecontext)) {
304 if (!empty($course->visible)) {
305 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".
306 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
307 } else {
308 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".
309 " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
313 // checks whether user can do site backup
314 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
315 echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\">\n<img".
316 " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
319 // checks whether user can do restore
320 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
321 echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&amp;wdir=/backupdata\">\n<img".
322 " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
325 echo "</td>\n</tr>\n";
327 echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
328 echo "<br />";
329 echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
330 echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
331 echo html_writer::select($displaylist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid'));
332 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
333 echo "</td>\n</tr>\n";
334 echo "</table>\n</form>";
338 print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
340 } else {
341 if (!empty($search)) {
342 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
344 else {
345 echo $OUTPUT->heading( $strnovalidcourses );
349 echo "<br /><br />";
351 print_course_search($search);
353 echo $OUTPUT->footer();
356 * Print a list navigation bar
357 * Display page numbers, and a link for displaying all entries
358 * @param integer $totalcount - number of entry to display
359 * @param integer $page - page number
360 * @param integer $perpage - number of entry per page
361 * @param string $encodedsearch
362 * @param string $modulelink - module name
364 function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
365 global $OUTPUT;
366 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
368 //display
369 if ($perpage != 99999 && $totalcount > $perpage) {
370 echo "<center><p>";
371 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
372 echo "</p></center>";