Newer
Older
Matt Petro
committed
<?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
*/
/**
* Adapted for course_overview_uwmoodle
*
* @author 2013 Matt Petro
*/
defined('MOODLE_INTERNAL') || die;
Matt Petro
committed
/**
* 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')) {
// Split courses list into batches with no more than MAX_MODINFO_CACHE_SIZE courses in one batch.
// Otherwise we exceed the cache limit in get_fast_modinfo() and rebuild it too often.
if (defined('MAX_MODINFO_CACHE_SIZE') && MAX_MODINFO_CACHE_SIZE > 0 && count($courses) > MAX_MODINFO_CACHE_SIZE) {
$batches = array_chunk($courses, MAX_MODINFO_CACHE_SIZE, true);
} else {
$batches = array($courses);
}
foreach ($batches as $courses) {
foreach ($modules as $fname) {
$fname($courses, $htmlarray);
Matt Petro
committed
}
Matt Petro
committed
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}
}
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() {
John Hoopes
committed
global $USER, $CFG;
Matt Petro
committed
$courses = enrol_get_my_courses(null, 'fullname', 0);
Matt Petro
committed
$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;
}
}
John Hoopes
committed
// get external courses
require_once($CFG->dirroot . '/enrol/wisc/lib/externalcourses/external_user_courses.php');
$externalcourses = new external_user_courses();
$ecourses = $externalcourses->get_user_courses($USER->email);
$courses = array_merge($courses, $ecourses);
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) {
John Hoopes
committed
if(get_class($course) !== 'external_course') { // skip external courses as we'll add those in after local courses
$courseids[] = $course->id;
}else{
// add external courses directly to the $courseterms array as we already have the term
// also skip external courses that have no term to be added to "other" term later
if(!empty($course->term)){
$courseterms[$course->id][$course->term] = true;
}
}
Matt Petro
committed
}
if(!empty($courseids)){
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;
}
/**
* Sort term array by ascending date
*
* @param array $terms keys are term_codes
* @return boolean
*/
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* 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;
}