MDL-13379 mnet: course overview at my home page displays remote courses, too
[moodle.git] / blocks / course_overview / block_course_overview.php
CommitLineData
03d9401e
MD
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Course overview block
20 *
21 * Currently, just a copy-and-paste from the old My Moodle.
22 *
23 * @package blocks
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once($CFG->dirroot.'/lib/weblib.php');
28require_once($CFG->dirroot . '/lib/formslib.php');
29
30class block_course_overview extends block_base {
31 /**
32 * block initializations
33 */
34 public function init() {
35 $this->title = get_string('pluginname', 'block_course_overview');
03d9401e
MD
36 }
37
38 /**
39 * block contents
40 *
41 * @return object
42 */
43 public function get_content() {
b081b41e 44 global $USER, $CFG;
03d9401e
MD
45 if($this->content !== NULL) {
46 return $this->content;
47 }
48
49 $this->content = new stdClass();
50 $this->content->text = '';
51 $this->content->footer = '';
52
53 $content = array();
54
55 // limits the number of courses showing up
56 $courses_limit = 21;
57 // FIXME: this should be a block setting, rather than a global setting
58 if (isset($CFG->mycoursesperpage)) {
59 $courses_limit = $CFG->mycoursesperpage;
60 }
61
62 $morecourses = false;
63 if ($courses_limit > 0) {
64 $courses_limit = $courses_limit + 1;
65 }
66
448d604c 67 $courses = enrol_get_my_courses('id, shortname', 'visible DESC,sortorder ASC', $courses_limit);
03d9401e
MD
68 $site = get_site();
69 $course = $site; //just in case we need the old global $course hack
70
45e87759
DM
71 if (is_enabled_auth('mnet')) {
72 $remote_courses = get_my_remotecourses();
73 }
74 if (empty($remote_courses)) {
75 $remote_courses = array();
76 }
77
78 if (($courses_limit > 0) && (count($courses)+count($remote_courses) >= $courses_limit)) {
79 // get rid of any remote courses that are above the limit
80 $remote_courses = array_slice($remote_courses, 0, $courses_limit - count($courses), true);
81 if (count($courses) >= $courses_limit) {
82 //remove the 'marker' course that we retrieve just to see if we have more than $courses_limit
83 array_pop($courses);
84 }
03d9401e
MD
85 $morecourses = true;
86 }
87
88
89 if (array_key_exists($site->id,$courses)) {
90 unset($courses[$site->id]);
91 }
92
93 foreach ($courses as $c) {
94 if (isset($USER->lastcourseaccess[$c->id])) {
95 $courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
96 } else {
97 $courses[$c->id]->lastaccess = 0;
98 }
99 }
100
45e87759 101 if (empty($courses) && empty($remote_courses)) {
03d9401e
MD
102 $content[] = get_string('nocourses','my');
103 } else {
104 ob_start();
b081b41e
AD
105
106 require_once $CFG->dirroot."/course/lib.php";
45e87759 107 print_overview($courses, $remote_courses);
b081b41e 108
03d9401e
MD
109 $content[] = ob_get_contents();
110 ob_end_clean();
111 }
112
113 // if more than 20 courses
114 if ($morecourses) {
115 $content[] = '<br />...';
116 }
117
118 $this->content->text = implode($content);
119
120 return $this->content;
121 }
122
123 /**
124 * allow the block to have a configuration page
125 *
126 * @return boolean
127 */
128 public function has_config() {
129 return false;
130 }
131
132 /**
133 * locations where block can be displayed
134 *
135 * @return array
136 */
137 public function applicable_formats() {
138 return array('my-index'=>true);
139 }
140}
141?>