Newer
Older
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
191
192
193
194
195
196
197
198
199
200
201
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?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/>.
/**
* Course overview block for UW Moodle
*
* This block improves on the course_summary block by presenting courses sorted by term.
* It also allows showing/hiding of terms and course news summaries.
*
* The block is dependent on the UW enrollment plugins.
*
* @author Matt Petro
*/
require_once($CFG->dirroot.'/lib/weblib.php');
class block_course_overview_uwmoodle extends block_base {
/**
* block initializations
*/
public function init() {
$this->title = get_string('pluginname', 'block_course_overview_uwmoodle');
}
const TERM_OTHER = 0; // UW "term_code" for non-timetable classes
/**
* block contents
*
* @return object
*/
public function get_content() {
global $USER, $CFG, $PAGE, $OUTPUT, $DB;
if($this->content !== NULL) {
return $this->content;
}
ob_start();
$courses = enrol_get_my_courses('id, shortname, modinfo, sectioncache', 'visible DESC,sortorder ASC', 0);
$site = get_site();
$course = $site; //just in case we need the old global $course hack
// Keep track of hideable items for initializing javascript
$yuiitems = array();
// Process GET parameters. Generally AJAX will be used instead of these.
$uwmterm = optional_param('uwmterm', null, PARAM_INT);
$uwmcourse = optional_param('uwmcourse', null, PARAM_INT);
$uwmshow = optional_param('uwmshow', 1, PARAM_INT);
if (!is_null($uwmterm)) {
set_user_preference('block_course_overview_uwmoodle-show-term-'.$uwmterm, $uwmshow);
}
if (!is_null($uwmcourse)) {
set_user_preference('block_course_overview_uwmoodle-show-coursenews-'.$uwmcourse, $uwmshow);
}
// Find the termcode for each course
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) {
if (empty($courses[$enrol->courseid]->term) || $courses[$enrol->courseid]->term < $enrol->customchar1) {
$courses[$enrol->courseid]->term = $enrol->customchar1;
}
}
}
// Sort courses by course shortname
uasort($courses, function($a,$b) { if ($a->shortname == $b->shortname) {return 0;} return ($a->shortname > $b->shortname)? 1 : -1;} );
// Organize courses into terms, maintaining shortname sorting inside each term
$terms = array();
foreach ($courses as $course) {
if (!empty($course->term)) {
$terms[$course->term][] = $course;
} else {
$terms[self::TERM_OTHER][] = $course;
}
}
// Sort terms, descending
ksort($terms);
$terms = array_reverse($terms, true);
// Update lastaccess for news summary
foreach ($courses as $c) {
if (isset($USER->lastcourseaccess[$c->id])) {
$courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
} else {
$courses[$c->id]->lastaccess = 0;
}
}
if (empty($courses)) {
echo get_string('nocourses','my');
} else {
// Get modinfo -- copied from print_overview()
$htmlarray = array();
if ($modules = $DB->get_records('modules')) {
foreach ($modules as $mod) {
if (file_exists($CFG->dirroot.'/mod/'.$mod->name.'/lib.php')) {
include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php');
$fname = $mod->name.'_print_overview';
if (function_exists($fname)) {
$fname($courses,$htmlarray);
}
}
}
}
// display courses
echo '<div class="courselist">';
echo '<ul class="treelist">';
foreach ($terms as $termcode=>$termcourses) {
$yuiitems[] = "term-$termcode";
$termname = $this->get_term_name($termcode);
$showterm = get_user_preferences("block_course_overview_uwmoodle-show-term-$termcode", 1);
if (!$showterm) {
$class = 'collapsed';
$ariaexpanded = "aria-expanded='false'";
} else {
$class = '';
$ariaexpanded = "aria-expanded='true'";
}
$aria = "$ariaexpanded aria-controls='uwm-{$this->instance->id}-term-$termcode-list' role='button'";
$showhidetermurl = $PAGE->url->out_as_local_url(true, array('uwmterm' => $termcode, 'uwmshow' => ($showterm)? 0 : 1 ));
echo "<li class='$class'>";
echo "<h3 class='term treeitem'>";
// The data-ajax attribute prevents the Moodle mobile theme from overriding the AJAX behavior
echo "<a class='showhide' data-ajax='false' $aria href='$showhidetermurl' id='uwm-{$this->instance->id}-term-$termcode'><span>$termname</span></a>";
echo "</h3>";
echo "<ul class='treelist' id='uwm-{$this->instance->id}-term-$termcode-list' role='region'>";
foreach ($termcourses as $course) {
$fullname = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
$hasnews = !empty($htmlarray[$course->id]);
$attributes = array('title' => s($fullname));
if (empty($course->visible)) {
$attributes['class'] = 'dimmed';
}
$newsclass = '';
$showhidehtml = '';
if ($hasnews) {
$shownews = get_user_preferences('block_course_overview_uwmoodle-show-coursenews-'.$course->id, 1);
$yuiitems[] = "coursenews-$course->id";
$showhidenewsurl = $PAGE->url->out_as_local_url(true, array('uwmcourse' => $course->id, 'uwmshow' => ($shownews)? 0 : 1));
if (!$shownews) {
$newsclass = 'collapsed';
$showhidetext = "Show";
$ariaexpanded = "aria-expanded='false'";
} else {
$newsclass = '';
$showhidetext = "Hide";
$ariaexpanded = "aria-expanded='true'";
}
$aria = "$ariaexpanded aria-controls='uwm-{$this->instance->id}-coursenews-{$course->id}-list' role='button'";
// The data-ajax attribute prevents the Moodle mobile theme from overriding the AJAX behavior
$showhidehtml = "<div class='shownews'><a data-ajax='false' $aria href='$showhidenewsurl' id='uwm-{$this->instance->id}-coursenews-$course->id' >";
$showhidehtml .= "<span class='treeitem'><span class='showhidetext'>$showhidetext</span> news";
$showhidehtml .= "</span></a></div>";
}
echo "<li class='$newsclass'>";
echo $OUTPUT->box_start("coursebox");
echo $OUTPUT->heading(html_writer::link(
new moodle_url('/course/view.php', array('id' => $course->id)), $fullname, $attributes) . $showhidehtml, 3);
if ($hasnews) {
echo "<ul class='treelist' id='uwm-{$this->instance->id}-coursenews-{$course->id}-list' role='region'>";
foreach ($htmlarray[$course->id] as $modname => $html) {
echo "<li>$html</li>";
}
echo "</ul>";
}
echo $OUTPUT->box_end();
echo "</li>";
}
echo "</ul>";
echo '</li>';
}
echo '</ul>';
echo "</div>";
}
// Initialize YUI module if needed
if (!empty($yuiitems)) {
$arguments = array(
'id' => $this->instance->id,
'items' => implode(' ', $yuiitems),
);
$PAGE->requires->yui_module('moodle-block_course_overview_uwmoodle-showhide', 'M.block_course_overview_uwmoodle.initialize', array($arguments));
foreach ($yuiitems as $item) {
user_preference_allow_ajax_update('block_course_overview_uwmoodle-show-'.$item, PARAM_INT);
}
}
// Get and end output buffer
$content = ob_get_contents();
ob_end_clean();
$this->content = new stdClass();
$this->content->footer = '';
$this->content->text = $content;
// Show "more courses" link if appropriate
if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM)) || empty($CFG->block_course_list_hideallcourseslink)) {
$this->content->footer = "<a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a> ...";
}
return $this->content;
}
/**
* Return a string representing the term (e.g. "Fall 2010")
* This function doesn't make any remote calls.
*
* @param string $termCode
* @return string $termName
*/
public function 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("Fall %d courses", $year-1);
break;
case 3:
$name = sprintf("Winter %d courses", $year);
break;
case 4:
$name = sprintf("Spring %d courses", $year);
break;
case 6:
$name = sprintf("Summer %d courses", $year);
break;
default:
$name = "Other courses";
}
return $name;
}
/**
* allow the block to have a configuration page
*
* @return boolean
*/
public function has_config() {
return false;
}
/**
* locations where block can be displayed
*
* @return array
*/
public function applicable_formats() {
return array('my-index'=>true);
}
}