Newer
Older
Matt Petro
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Helper functions for course_overview block
*
* @package block_course_overview_uwmoodle
* @copyright 2012 Adam Olley <adam.olley@netspot.com.au>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Display overview for courses
*
* @param array $courses courses for which overview needs to be shown
* @return array html overview
*/
function block_course_overview_uwmoodle_get_overviews($courses) {
$htmlarray = array();
if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
foreach ($modules as $fname) {
$fname($courses,$htmlarray);
}
}
return $htmlarray;
}
/**
* Returns shortname of activities in course
*
* @param int $courseid id of course for which activity shortname is needed
* @return string|bool list of child shortname
*/
function block_course_overview_uwmoodle_get_child_shortnames($courseid) {
global $DB;
$ctxselect = context_helper::get_preload_record_columns_sql('ctx');
$sql = "SELECT c.id, c.shortname, $ctxselect
FROM {enrol} e
JOIN {course} c ON (c.id = e.customint1)
JOIN {context} ctx ON (ctx.instanceid = e.customint1)
WHERE e.courseid = :courseid AND e.enrol = :method AND ctx.contextlevel = :contextlevel ORDER BY e.sortorder";
$params = array('method' => 'meta', 'courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE);
if ($results = $DB->get_records_sql($sql, $params)) {
$shortnames = array();
// Preload the context we will need it to format the category name shortly.
foreach ($results as $res) {
context_helper::preload_from_record($res);
$context = context_course::instance($res->id);
$shortnames[] = format_string($res->shortname, true, $context);
}
$total = count($shortnames);
$suffix = '';
if ($total > 10) {
$shortnames = array_slice($shortnames, 0, 10);
$diff = $total - count($shortnames);
if ($diff > 1) {
$suffix = get_string('shortnamesufixprural', 'block_course_overview_uwmoodle', $diff);
} else {
$suffix = get_string('shortnamesufixsingular', 'block_course_overview_uwmoodle', $diff);
}
}
$shortnames = get_string('shortnameprefix', 'block_course_overview_uwmoodle', implode('; ', $shortnames));
$shortnames .= $suffix;
}
return isset($shortnames) ? $shortnames : false;
}
/**
* Return sorted list of user courses
*
* @return array list of sorted courses grouped by term.
*/
function block_course_overview_uwmoodle_get_sorted_courses() {
Matt Petro
committed
$courses = enrol_get_my_courses('id, shortname, fullname, modinfo, sectioncache', 'fullname', 0);
$site = get_site();
if (array_key_exists($site->id,$courses)) {
unset($courses[$site->id]);
}
foreach ($courses as $c) {
if (isset($USER->lastcourseaccess[$c->id])) {
$courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
} else {
$courses[$c->id]->lastaccess = 0;
}
}
return $courses;
}
/**
* Organize courses into terms, maintaining existing sorting inside each term
*
* @param array $courses user courses
* @return array
*/
function block_course_overview_uwmoodle_group_courses_by_term($courses) {
global $DB;
// Find the termcodes for each course
$courseterms = array();
Matt Petro
committed
if (!empty($courses)) {
$courseids = array();
foreach ($courses as $course) {
$courseids[] = $course->id;
}
list ($insql, $inparams) = $DB->get_in_or_equal($courseids);
$select = "enrol='wisc' AND courseid $insql";
$enrols = $DB->get_records_select('enrol', $select, $inparams, '', 'id,courseid,customchar1');
foreach ($enrols as $enrol) {
$courseterms[$enrol->courseid][$enrol->customchar1] = true;
Matt Petro
committed
}
}
// Sort the courses by termcode. Note that one course can be in multiple terms.
Matt Petro
committed
$terms = array();
foreach ($courses as $course) {
if (!empty($courseterms[$course->id])) {
foreach ($courseterms[$course->id] as $termcode => $unused) {
$terms[$termcode][$course->id] = $course;
}
Matt Petro
committed
} else {
$terms[block_course_overview_uwmoodle::TERM_OTHER][$course->id] = $course;
}
}
return $terms;
}
function block_course_overview_uwmoodle_sort_term_array(&$terms) {
// Sort the terms with ongoing (code = 0) first, then in decreasing order
$cmp = function ($a, $b) {
if ($a == 0 || $b == 0) {
return $a-$b;
} else {
return $b-$a;
}
};
return uksort($terms, $cmp);
}
Matt Petro
committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Get the current term from our settings
*
* @return string termcode
*/
function block_course_overview_uwmoodle_get_current_term() {
$currentterm = get_config('block_course_overview_uwmoodle', 'currentterm');
if (!$currentterm) {
$currentterm = 1136; // Just set it to something in the past, until cron runs and updates the term.
}
return $currentterm;
}
/**
* Return a string representing the term (e.g. "Fall 2010")
* This function doesn't make any remote calls.
*
* @param string $termCode
* @return string $termName
*/
function block_course_overview_uwmoodle_get_term_name($termCode) {
$termCode = (string)$termCode;
$c = substr($termCode,0,1);
$yy = substr($termCode,1,2);
$year = 1900+100*$c+$yy;
$semester = substr($termCode,3,1);
switch($semester) {
case 2:
$name = sprintf("<span class='semester'>Fall</span> <span class='year'>%d</span>", $year-1);
Matt Petro
committed
break;
case 3:
$name = sprintf("<span class='semester'>Winter</span> <span class='year'>%d</span>", $year);
Matt Petro
committed
break;
case 4:
$name = sprintf("<span class='semester'>Spring</span> <span class='year'>%d</span>", $year);
Matt Petro
committed
break;
case 6:
$name = sprintf("<span class='semester'>Summer</span> <span class='year'>%d</span>", $year);
Matt Petro
committed
break;
default:
$name = "Ongoing";
Matt Petro
committed
}
return $name;
}