Merge branch 'MDL-48035-master-enfix' of git://github.com/mudrd8mz/moodle
[moodle.git] / blocks / course_list / block_course_list.php
blobdf725de08487875c32ff1fe6838ea4a0f039c284
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 * Course list block.
20 * @package block_course_list
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 include_once($CFG->dirroot . '/course/lib.php');
26 include_once($CFG->libdir . '/coursecatlib.php');
28 class block_course_list extends block_list {
29 function init() {
30 $this->title = get_string('pluginname', 'block_course_list');
33 function has_config() {
34 return true;
37 function get_content() {
38 global $CFG, $USER, $DB, $OUTPUT;
40 if($this->content !== NULL) {
41 return $this->content;
44 $this->content = new stdClass;
45 $this->content->items = array();
46 $this->content->icons = array();
47 $this->content->footer = '';
49 $icon = '<img src="' . $OUTPUT->pix_url('i/course') . '" class="icon" alt="" />';
51 $adminseesall = true;
52 if (isset($CFG->block_course_list_adminview)) {
53 if ( $CFG->block_course_list_adminview == 'own'){
54 $adminseesall = false;
58 if (empty($CFG->disablemycourses) and isloggedin() and !isguestuser() and
59 !(has_capability('moodle/course:update', context_system::instance()) and $adminseesall)) { // Just print My Courses
60 // As this is producing navigation sort order should default to $CFG->navsortmycoursessort instead
61 // of using the default.
62 if (!empty($CFG->navsortmycoursessort)) {
63 $sortorder = 'visible DESC, ' . $CFG->navsortmycoursessort . ' ASC';
64 } else {
65 $sortorder = 'visible DESC, sortorder ASC';
67 if ($courses = enrol_get_my_courses(NULL, $sortorder)) {
68 foreach ($courses as $course) {
69 $coursecontext = context_course::instance($course->id);
70 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
71 $this->content->items[]="<a $linkcss title=\"" . format_string($course->shortname, true, array('context' => $coursecontext)) . "\" ".
72 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".$icon.format_string(get_course_display_name_for_list($course)). "</a>";
74 $this->title = get_string('mycourses');
75 /// If we can update any course of the view all isn't hidden, show the view all courses link
76 if (has_capability('moodle/course:update', context_system::instance()) || empty($CFG->block_course_list_hideallcourseslink)) {
77 $this->content->footer = "<a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a> ...";
80 $this->get_remote_courses();
81 if ($this->content->items) { // make sure we don't return an empty list
82 return $this->content;
86 $categories = coursecat::get(0)->get_children(); // Parent = 0 ie top-level categories only
87 if ($categories) { //Check we have categories
88 if (count($categories) > 1 || (count($categories) == 1 && $DB->count_records('course') > 200)) { // Just print top level category links
89 foreach ($categories as $category) {
90 $categoryname = $category->get_formatted_name();
91 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
92 $this->content->items[]="<a $linkcss href=\"$CFG->wwwroot/course/index.php?categoryid=$category->id\">".$icon . $categoryname . "</a>";
94 /// If we can update any course of the view all isn't hidden, show the view all courses link
95 if (has_capability('moodle/course:update', context_system::instance()) || empty($CFG->block_course_list_hideallcourseslink)) {
96 $this->content->footer .= "<a href=\"$CFG->wwwroot/course/index.php\">".get_string('fulllistofcourses').'</a> ...';
98 $this->title = get_string('categories');
99 } else { // Just print course names of single category
100 $category = array_shift($categories);
101 $courses = get_courses($category->id);
103 if ($courses) {
104 foreach ($courses as $course) {
105 $coursecontext = context_course::instance($course->id);
106 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
108 $this->content->items[]="<a $linkcss title=\""
109 . format_string($course->shortname, true, array('context' => $coursecontext))."\" ".
110 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">"
111 .$icon. format_string(get_course_display_name_for_list($course), true, array('context' => context_course::instance($course->id))) . "</a>";
113 /// If we can update any course of the view all isn't hidden, show the view all courses link
114 if (has_capability('moodle/course:update', context_system::instance()) || empty($CFG->block_course_list_hideallcourseslink)) {
115 $this->content->footer .= "<a href=\"$CFG->wwwroot/course/index.php\">".get_string('fulllistofcourses').'</a> ...';
117 $this->get_remote_courses();
118 } else {
120 $this->content->icons[] = '';
121 $this->content->items[] = get_string('nocoursesyet');
122 if (has_capability('moodle/course:create', context_coursecat::instance($category->id))) {
123 $this->content->footer = '<a href="'.$CFG->wwwroot.'/course/edit.php?category='.$category->id.'">'.get_string("addnewcourse").'</a> ...';
125 $this->get_remote_courses();
127 $this->title = get_string('courses');
131 return $this->content;
134 function get_remote_courses() {
135 global $CFG, $USER, $OUTPUT;
137 if (!is_enabled_auth('mnet')) {
138 // no need to query anything remote related
139 return;
142 $icon = '<img src="'.$OUTPUT->pix_url('i/mnethost') . '" class="icon" alt="" />';
144 // shortcut - the rest is only for logged in users!
145 if (!isloggedin() || isguestuser()) {
146 return false;
149 if ($courses = get_my_remotecourses()) {
150 $this->content->items[] = get_string('remotecourses','mnet');
151 $this->content->icons[] = '';
152 foreach ($courses as $course) {
153 $this->content->items[]="<a title=\"" . format_string($course->shortname, true) . "\" ".
154 "href=\"{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$course->hostid}&amp;wantsurl=/course/view.php?id={$course->remoteid}\">"
155 .$icon. format_string(get_course_display_name_for_list($course)) . "</a>";
157 // if we listed courses, we are done
158 return true;
161 if ($hosts = get_my_remotehosts()) {
162 $this->content->items[] = get_string('remotehosts', 'mnet');
163 $this->content->icons[] = '';
164 foreach($USER->mnet_foreign_host_array as $somehost) {
165 $this->content->items[] = $somehost['count'].get_string('courseson','mnet').'<a title="'.$somehost['name'].'" href="'.$somehost['url'].'">'.$icon.$somehost['name'].'</a>';
167 // if we listed hosts, done
168 return true;
171 return false;
175 * Returns the role that best describes the course list block.
177 * @return string
179 public function get_aria_role() {
180 return 'navigation';