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
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));
35 foreach (array('search', 'page', 'blocklist', 'modulelist') as $param) {
36 if (!empty($
$param)) {
37 $urlparams[$param] = $
$param;
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
) {
50 if (can_edit_in_category()) {
52 $USER->editing
= $edit;
54 $adminediting = $PAGE->user_is_editing();
56 $adminediting = false;
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()) {
64 $course = $DB->get_record("course", array("id"=>$hide));
67 $course = $DB->get_record("course", array("id"=>$show));
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) {
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();
105 print_course_search("", false, "plain");
107 print_string("searchhelp");
110 echo $OUTPUT->box_end();
111 echo $OUTPUT->footer();
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
);
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));
133 $courses = $DB->get_records_sql("
134 SELECT * FROM {course} WHERE id IN (
135 SELECT DISTINCT ctx.instanceid
137 JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
138 WHERE ctx.contextlevel = " . CONTEXT_COURSE
. " AND bi.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);
153 if (!empty($courseids)) {
154 $firstcourse = $page*$perpage;
155 $lastcourse = $page*$perpage +
$perpage -1;
157 foreach ($courseids as $courseid) {
158 if ($i>= $firstcourse && $i<=$lastcourse) {
159 $courses[$courseid->id
] = $DB->get_record('course', array('id'=> $courseid->id
));
163 $totalcount = count($courseids);
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
))) {
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");
185 $string = get_string("turneditingon");
189 $aurl = new moodle_url("$CFG->wwwroot/course/search.php", array(
191 'sesskey' => sesskey(),
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();
212 echo $OUTPUT->heading("$strsearchresults: $totalcount");
213 $encodedsearch = urlencode($search);
215 ///add the module parameter to the paging bar if they exists
217 if (!empty($modulelist) and confirm_sesskey()) {
218 $modulelink = "&modulelist=".$modulelist."&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
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) {
257 echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
259 // can't do anything else with the front page
260 echo " <td> </td>\n"; // category place
261 echo " <td> </td>\n"; // select place
262 echo " <td> </td>\n"; // edit place
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";
273 // this is ok since this will get inherited from course category context
275 if (has_capability('moodle/category:manage', $coursecontext)) {
276 echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
278 echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\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&perpage=$perpage&page=$page&hide=$course->id&sesskey=".sesskey()."\">\n<img".
306 " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
308 echo "<a title=\"".get_string("show")."\" href=\"search.php?search=$encodedsearch&perpage=$perpage&page=$page&show=$course->id&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&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";
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);
341 if (!empty($search)) {
342 echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
345 echo $OUTPUT->heading( $strnovalidcourses );
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) {
366 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
369 if ($perpage != 99999 && $totalcount > $perpage) {
371 echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
372 echo "</p></center>";