2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Helper functions for course_overview block
20 * @package block_course_overview
21 * @copyright 2012 Adam Olley <adam.olley@netspot.com.au>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * Display overview for courses
28 * @param array $courses courses for which overview needs to be shown
29 * @return array html overview
31 function block_course_overview_get_overviews($courses) {
33 if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
34 foreach ($modules as $fname) {
35 $fname($courses,$htmlarray);
42 * Sets user preference for maximum courses to be displayed in course_overview block
44 * @param int $number maximum courses which should be visible
46 function block_course_overview_update_mynumber($number) {
47 set_user_preference('course_overview_number_of_courses', $number);
51 * Sets user course sorting preference in course_overview block
53 * @param array $sortorder sort order of course
55 function block_course_overview_update_myorder($sortorder) {
56 set_user_preference('course_overview_course_order', serialize($sortorder));
60 * Returns shortname of activities in course
62 * @param int $courseid id of course for which activity shortname is needed
63 * @return string|bool list of child shortname
65 function block_course_overview_get_child_shortnames($courseid) {
67 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
68 $sql = "SELECT c.id, c.shortname, $ctxselect
70 JOIN {course} c ON (c.id = e.customint1)
71 JOIN {context} ctx ON (ctx.instanceid = e.customint1)
72 WHERE e.courseid = :courseid AND e.enrol = :method AND ctx.contextlevel = :contextlevel ORDER BY e.sortorder";
73 $params = array('method' => 'meta', 'courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE);
75 if ($results = $DB->get_records_sql($sql, $params)) {
76 $shortnames = array();
77 // Preload the context we will need it to format the category name shortly.
78 foreach ($results as $res) {
79 context_helper::preload_from_record($res);
80 $context = context_course::instance($res->id);
81 $shortnames[] = format_string($res->shortname, true, $context);
83 $total = count($shortnames);
86 $shortnames = array_slice($shortnames, 0, 10);
87 $diff = $total - count($shortnames);
89 $suffix = get_string('shortnamesufixprural', 'block_course_overview', $diff);
91 $suffix = get_string('shortnamesufixsingular', 'block_course_overview', $diff);
94 $shortnames = get_string('shortnameprefix', 'block_course_overview', implode('; ', $shortnames));
95 $shortnames .= $suffix;
98 return isset($shortnames) ? $shortnames : false;
102 * Returns maximum number of courses which will be displayed in course_overview block
104 * @return int maximum number of courses
106 function block_course_overview_get_max_user_courses() {
107 // Get block configuration
108 $config = get_config('block_course_overview');
109 $limit = $config->defaultmaxcourses;
111 // If max course is not set then try get user preference
112 if (empty($config->forcedefaultmaxcourses)) {
113 $limit = get_user_preferences('course_overview_number_of_courses', $limit);
119 * Return sorted list of user courses
121 * @return array list of sorted courses and count of courses.
123 function block_course_overview_get_sorted_courses() {
126 $limit = block_course_overview_get_max_user_courses();
128 $courses = enrol_get_my_courses('id, shortname, fullname, modinfo, sectioncache');
131 if (array_key_exists($site->id,$courses)) {
132 unset($courses[$site->id]);
135 foreach ($courses as $c) {
136 if (isset($USER->lastcourseaccess[$c->id])) {
137 $courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
139 $courses[$c->id]->lastaccess = 0;
143 // Get remote courses.
144 $remotecourses = array();
145 if (is_enabled_auth('mnet')) {
146 $remotecourses = get_my_remotecourses();
148 // Remote courses will have -ve remoteid as key, so it can be differentiated from normal courses
149 foreach ($remotecourses as $id => $val) {
150 $remoteid = $val->remoteid * -1;
151 $val->id = $remoteid;
152 $courses[$remoteid] = $val;
156 if (!is_null($usersortorder = get_user_preferences('course_overview_course_order'))) {
157 $order = unserialize($usersortorder);
160 $sortedcourses = array();
162 // Get courses in sort order into list.
163 foreach ($order as $key => $cid) {
164 if (($counter >= $limit) && ($limit != 0)) {
168 // Make sure user is still enroled.
169 if (isset($courses[$cid])) {
170 $sortedcourses[$cid] = $courses[$cid];
174 // Append unsorted courses if limit allows
175 foreach ($courses as $c) {
176 if (($limit != 0) && ($counter >= $limit)) {
179 if (!in_array($c->id, $order)) {
180 $sortedcourses[$c->id] = $c;
185 // From list extract site courses for overview
186 $sitecourses = array();
187 foreach ($sortedcourses as $key => $course) {
188 if ($course->id > 0) {
189 $sitecourses[$key] = $course;
192 return array($sortedcourses, $sitecourses, count($courses));