MDL-37009 function print_course_search() is moved to course renderer
[moodle.git] / course / category.php
CommitLineData
58b45fd7 1<?php
f73cea54
SH
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.
13//
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/>.
16
17/**
18 * Displays the top level category or all courses
f73cea54
SH
19 *
20 * @package core
21 * @subpackage course
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require_once("../config.php");
27require_once($CFG->dirroot.'/course/lib.php');
4b30377d 28require_once($CFG->libdir.'/textlib.class.php');
4e0b6025 29require_once($CFG->libdir. '/coursecatlib.php');
f73cea54
SH
30
31$id = required_param('id', PARAM_INT); // Category id
32$page = optional_param('page', 0, PARAM_INT); // which page to show
f73cea54 33
2c49fb4c 34$perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page
98068e1d 35
f73cea54
SH
36if (empty($id)) {
37 print_error("unknowcategory");
38}
39
40$PAGE->set_category_by_id($id);
41$PAGE->set_url(new moodle_url('/course/category.php', array('id' => $id)));
42// This is sure to be the category context
43$context = $PAGE->context;
44// And the object has been loaded for us no need for another DB call
45$category = $PAGE->category;
46
2c49fb4c 47if ($CFG->forcelogin) {
f73cea54 48 require_login();
f73cea54
SH
49}
50
51if (!$category->visible) {
52 require_capability('moodle/category:viewhiddencategories', $context);
53}
54
f73cea54
SH
55// Prepare the standard URL params for this page. We'll need them later.
56$urlparams = array('id' => $id);
57if ($page) {
58 $urlparams['page'] = $page;
59}
60if ($perpage) {
61 $urlparams['perpage'] = $perpage;
62}
63
64// Begin output
2c49fb4c 65$PAGE->set_pagelayout('coursecategory');
f4b571ab 66$courserenderer = $PAGE->get_renderer('core', 'course');
2c49fb4c
MG
67$site = get_site();
68$PAGE->set_title("$site->shortname: $category->name");
69$PAGE->set_heading($site->fullname);
f4b571ab 70$PAGE->set_button($courserenderer->course_search_form('', 'navbar'));
2c49fb4c 71echo $OUTPUT->header();
f73cea54 72
1fb3d044 73/// Print the category selector
4e0b6025 74$displaylist = coursecat::make_categories_list();
1fb3d044 75
f73cea54
SH
76echo '<div class="categorypicker">';
77$select = new single_select(new moodle_url('/course/category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
78$select->set_label(get_string('categories').':');
79echo $OUTPUT->render($select);
80echo '</div>';
1fb3d044 81
82/// Print current category description
2c49fb4c
MG
83echo $OUTPUT->box_start();
84$options = new stdClass;
85$options->noclean = true;
86$options->para = false;
87$options->overflowdiv = true;
88if (!isset($category->descriptionformat)) {
89 $category->descriptionformat = FORMAT_MOODLE;
90}
91$text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
92echo format_text($text, $category->descriptionformat, $options);
93echo $OUTPUT->box_end();
f73cea54
SH
94
95// Print out all the sub-categories
96// In order to view hidden subcategories the user must have the viewhiddencategories
97// capability in the current category.
98if (has_capability('moodle/category:viewhiddencategories', $context)) {
99 $categorywhere = '';
100} else {
101 $categorywhere = 'AND cc.visible = 1';
102}
103// We're going to preload the context for the subcategory as we know that we
104// need it later on for formatting.
e141bc81
SH
105
106$ctxselect = context_helper::get_preload_record_columns_sql('ctx');
107$sql = "SELECT cc.*, $ctxselect
f73cea54 108 FROM {course_categories} cc
e141bc81
SH
109 JOIN {context} ctx ON cc.id = ctx.instanceid
110 WHERE cc.parent = :parentid AND
111 ctx.contextlevel = :contextlevel
f73cea54
SH
112 $categorywhere
113 ORDER BY cc.sortorder ASC";
e141bc81 114$subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id, 'contextlevel' => CONTEXT_COURSECAT));
f73cea54
SH
115// Prepare a table to display the sub categories.
116$table = new html_table;
117$table->attributes = array('border' => '0', 'cellspacing' => '2', 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter category_subcategories');
118$table->head = array(new lang_string('subcategories'));
119$table->data = array();
96e78552 120$baseurl = new moodle_url('/course/category.php');
f73cea54
SH
121foreach ($subcategories as $subcategory) {
122 // Preload the context we will need it to format the category name shortly.
e141bc81 123 context_helper::preload_from_record($subcategory);
9a5e297b 124 $context = context_coursecat::instance($subcategory->id);
f73cea54
SH
125 // Prepare the things we need to create a link to the subcategory
126 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
f73cea54
SH
127 $text = format_string($subcategory->name, true, array('context' => $context));
128 // Add the subcategory to the table
7a3a0a30
SH
129 $baseurl->param('id', $subcategory->id);
130 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
f73cea54
SH
131}
132
133$subcategorieswereshown = (count($table->data) > 0);
134if ($subcategorieswereshown) {
135 echo html_writer::table($table);
136}
137
3b7bfbb5 138// Print out all the courses.
f73cea54
SH
139$courses = get_courses_page($category->id, 'c.sortorder ASC',
140 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
141 $totalcount, $page*$perpage, $perpage);
142$numcourses = count($courses);
143
3b7bfbb5
FM
144// We can consider that we are using pagination when the total count of courses is different than the one returned.
145$pagingmode = $totalcount != $numcourses;
146
f73cea54 147if (!$courses) {
3b7bfbb5 148 // There is no course to display.
f73cea54
SH
149 if (empty($subcategorieswereshown)) {
150 echo $OUTPUT->heading(get_string("nocoursesyet"));
09deab06 151 }
2c49fb4c 152} else if ($numcourses <= $CFG->courseswithsummarieslimit and !$pagingmode) {
3b7bfbb5
FM
153 // We display courses with their summaries as we have not reached the limit, also we are not
154 // in paging mode and not allowed to edit either.
f73cea54
SH
155 echo $OUTPUT->box_start('courseboxes');
156 print_courses($category);
157 echo $OUTPUT->box_end();
f73cea54 158} else {
3b7bfbb5 159 // The conditions above have failed, we display a basic list of courses with paging/editing options.
f73cea54 160 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
c2cb4545 161
f73cea54
SH
162 echo '<form id="movecourses" action="category.php" method="post"><div>';
163 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
eb610eb9 164 echo '<table border="0" cellspacing="2" cellpadding="4" class="generaltable boxaligncenter"><tr>';
f73cea54 165 echo '<th class="header" scope="col">'.get_string('courses').'</th>';
2c49fb4c 166 echo '<th class="header" scope="col">&nbsp;</th>';
f73cea54 167 echo '</tr>';
c2cb4545 168
f73cea54 169 $count = 0;
c2cb4545 170
f73cea54
SH
171 // Checking if we are at the first or at the last page, to allow courses to
172 // be moved up and down beyond the paging border
173 if ($totalcount > $perpage) {
174 $atfirstpage = ($page == 0);
175 if ($perpage > 0) {
176 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
3d95bdb7 177 } else {
f73cea54 178 $atlastpage = true;
d2b6ba70 179 }
f73cea54
SH
180 } else {
181 $atfirstpage = true;
182 $atlastpage = true;
183 }
d2b6ba70 184
96e78552 185 $baseurl = new moodle_url('/course/category.php', $urlparams + array('sesskey' => sesskey()));
f73cea54 186 foreach ($courses as $acourse) {
9a5e297b 187 $coursecontext = context_course::instance($acourse->id);
c2cb4545 188
f73cea54
SH
189 $count++;
190 $up = ($count > 1 || !$atfirstpage);
191 $down = ($count < $numcourses || !$atlastpage);
c2cb4545 192
f73cea54
SH
193 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
194 echo '<tr>';
195 $coursename = get_course_display_name_for_list($acourse);
196 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
2c49fb4c
MG
197 echo '<td align="right">';
198 // print enrol info
199 if ($icons = enrol_get_course_info_icons($acourse)) {
200 foreach ($icons as $pix_icon) {
201 echo $OUTPUT->render($pix_icon);
7b0d5cd5 202 }
f2bb0045 203 }
2c49fb4c
MG
204 if (!empty($acourse->summary)) {
205 $url = new moodle_url("/course/info.php?id=$acourse->id");
206 echo $OUTPUT->action_link($url, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
207 new popup_action('click', $url, 'courseinfo'), array('title'=>get_string('summary')));
208 }
209 echo "</td>";
f73cea54 210 echo "</tr>";
c2cb4545 211 }
e295df44 212
f73cea54
SH
213 echo '</table>';
214 echo '</div></form>';
215 echo '<br />';
216}
77eddcd5 217
f73cea54 218echo '<div class="buttons">';
f73cea54
SH
219if (has_capability('moodle/course:create', $context)) {
220 // Print button to create a new course
221 $url = new moodle_url('/course/edit.php', array('category' => $category->id, 'returnto' => 'category'));
222 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
223}
e295df44 224
f73cea54 225if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
9a5e297b 226 print_course_request_buttons(context_system::instance());
f73cea54
SH
227}
228echo '</div>';
e295df44 229
7a3a0a30 230echo $OUTPUT->footer();