-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock_course_ascendants.php
401 lines (353 loc) · 13.9 KB
/
block_course_ascendants.php
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?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/>.
/**
* @package block_course_ascendants
* @category blocks
* @copyright 2012 onwards Valery Fremaux ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* the block course ascendants
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/blocks/course_ascendants/listlib.php');
class block_course_ascendants extends block_base {
public function init() {
$this->title = get_string('title', 'block_course_ascendants');
}
public function has_config() {
return true;
}
public function applicable_formats() {
return array('course' => true, 'mod' => false, 'tag' => false, 'my' => false);
}
public function instance_allow_config() {
return true;
}
public function instance_allow_multiple() {
return false;
}
/**
* Serialize and store config data
*/
public function instance_config_save($data, $nolongerused = false) {
if (!isset($data->showdescription)) {
$data->showdescription = 0;
}
parent::instance_config_save($data, false);
}
public function get_content() {
global $COURSE, $PAGE, $USER;
if ($this->content !== null) {
return $this->content;
}
if (!isset($this->config)) {
$this->config = new StdClass();
}
if (!isset($this->config->showcategories)) {
$this->config->showcategories = false;
}
$coursecontext = context_course::instance($COURSE->id);
if (@$this->config->arrangeby == 1) {
// Execute block micro controller if arranging by local plan.
if ($what = optional_param('what', '', PARAM_TEXT)) {
if ($what == 'asc-down') {
$downcourse = required_param('downcourse', PARAM_INT);
$blockid = required_param('blockid', PARAM_INT);
list_down($blockid, $downcourse);
}
if ($what == 'asc-up') {
$upcourse = required_param('upcourse', PARAM_INT);
$blockid = required_param('blockid', PARAM_INT);
list_up($blockid, $upcourse);
}
}
}
// Fetch direct ascendants that are metas who point the current course as descendant.
$categories = array();
$this->read_category_tree(0 + @$this->config->coursescopestartcategory, $categories);
$renderer = $PAGE->get_renderer('block_course_ascendants');
$this->content = new stdClass;
$this->content->footer = '';
$this->content->text = '';
if (@$this->config->arrangeby == 0) {
// Scan directly category results and output them in lbock space.
if ($categories) {
foreach ($categories as $cat) {
if ($this->config->showcategories && !empty($cat->courses)) {
$filteredprevcat = $cat->name;
if (!empty($this->config->catstringfilter)) {
$pattern = $this->config->catstringfilter;
preg_match("/$pattern/", $filteredprevcat, $matches);
$filteredprevcat = $matches[0];
}
$this->content->text .= '<div class="category"><b>'.$filteredprevcat.'</b></div>';
}
if (!empty($cat->courses)) {
$this->content->text .= '<div clas="courses">';
foreach ($cat->courses as $ascendant) {
$asccontext = context_course::instance($ascendant->id);
if (!is_enrolled($asccontext, $USER->id, '', true) &&
!is_viewing($asccontext, $USER->id) &&
!is_siteadmin($USER->id)) {
continue;
}
$this->content->text .= $renderer->courserow($ascendant, $this);
}
$this->content->text .= '</div>';
}
}
}
} else {
// Prescan categories, sort them by local order and finally output them.
if ($categories) {
$flatcourses = array();
foreach ($categories as $cat) {
if (!empty($cat->courses)) {
foreach ($cat->courses as $cid => $ascendant) {
$flatcourses[$cid] = $ascendant;
}
}
}
uasort($flatcourses, 'sort_by_localorder');
$this->content->text .= '<div class="courses">';
if (!empty($flatcourses)) {
foreach ($flatcourses as $c) {
$this->content->text .= $renderer->courserow($c, $this, count($flatcourses));
}
}
$this->content->text .= '</div>';
}
}
if (has_capability('moodle/course:manageactivities', $coursecontext)) {
$manageascendantsstr = get_string('manageascendants', 'block_course_ascendants');
$params = array('course' => $COURSE->id, 'id' => $this->instance->id, 'sesskey' => sesskey());
$linkurl = new moodle_url('/blocks/course_ascendants/assign.php', $params);
$this->content->footer = '<a href="'.$linkurl.'">'.$manageascendantsstr.'</a>';
}
return $this->content;
}
/**
* Reads category tree in correct order.
*/
public function read_category_tree($catstart, &$categories, $seeunbound = false, $seeinvisible = false) {
global $DB;
static $level = 0;
if ($catstart != 0 && $level == 0) {
$cat = $DB->get_record('course_categories', array('id' => $catstart), 'id,name,visible');
if (!$cat->visible && !$seeinvisible) {
return;
}
if ($ascendants = $this->get_ascendants($catstart, $seeunbound)) {
$cat->courses = array();
foreach ($ascendants as $asc) {
$context = context_course::instance($asc->id);
if ($asc->visible || has_capability('moodle/course:viewhiddencourses', $context) || $seeinvisible) {
$cat->courses[$asc->id] = $asc;
}
}
}
$categories[] = $cat;
}
// Get in subcats.
$select = "parent = ? ";
$fields = 'id,name,visible';
if ($catlevel = $DB->get_records_select('course_categories', $select, array($catstart), 'sortorder', $fields)) {
foreach ($catlevel as $cat) {
$catcontext = context_coursecat::instance($cat->id);
if ((!$cat->visible &&
!has_capability('moodle/category:viewhiddencategories', $catcontext)) &&
!$seeinvisible) {
continue;
}
if ($ascendants = $this->get_ascendants($cat->id, $seeunbound)) {
$cat->courses = array();
foreach ($ascendants as $asc) {
$context = context_course::instance($asc->id);
if ($asc->visible || has_capability('moodle/course:viewhiddencourses', $context) || $seeinvisible) {
$cat->courses[$asc->id] = $asc;
}
}
}
$categories[] = $cat;
$level++;
$this->read_category_tree($cat->id, $categories, $seeunbound, $seeinvisible);
$level--;
}
}
}
/**
* get all potential or effective ascendants
* an ascendant course is a course having a metacourse enrolment
* instance bound to us, either it is active or not or no enrol instance at all (but could have).
* @param int $catid the root category where to search
* @param bool $seeunbound
*/
public function get_ascendants($catid, $seeunbound, $courseid = null) {
global $COURSE, $DB, $USER;
// Getting all meta enrols that point me.
if (is_null($courseid)) {
$courseid = $COURSE->id;
}
$catclause = ($catid) ? " AND c.category = $catid " : '';
if ($seeunbound) {
$params = array($this->instance->id, $USER->id, $courseid);
$sql = "
SELECT DISTINCT
c.id,
c.category,
c.shortname,
c.fullname,
c.sortorder,
c.summary,
c.visible,
c.enablecompletion,
bca.sortorder as localorder,
cc.timecompleted as completioncompleted,
ula.timeaccess as completionenrolled,
e.id as isbound
FROM
{course} c
LEFT JOIN
{block_course_ascendants} bca
ON
bca.courseid = c.id AND
bca.blockid = ?
LEFT JOIN
{course_completions} cc
ON
cc.course = c.id AND
cc.userid = ?
LEFT JOIN
{enrol} e
ON
c.id = e.courseid AND
e.enrol = 'meta' AND
e.customint1 = ? AND
e.status = 0
LEFT JOIN
{user_lastaccess} ula
ON
ula.userid = cc.userid AND
ula.courseid = c.id
WHERE
1 = 1
$catclause
";
$ascendants = $DB->get_records_sql($sql, $params);
} else {
$params = array($this->instance->id, $USER->id, $courseid);
$sql = "
SELECT DISTINCT
c.id,
c.category,
c.shortname,
c.fullname,
c.sortorder,
c.summary,
c.visible,
c.enablecompletion,
bca.sortorder as localorder,
cc.timecompleted as completioncompleted,
ula.timeaccess as completionenrolled
FROM
{course} c
LEFT JOIN
{enrol} e
ON
c.id = e.courseid AND
e.enrol = 'meta'
LEFT JOIN
{block_course_ascendants} bca
ON
bca.courseid = c.id AND
bca.blockid = ?
LEFT JOIN
{course_completions} cc
ON
cc.course = c.id AND
cc.userid = ?
LEFT JOIN
{user_lastaccess} ula
ON
ula.userid = cc.userid AND
ula.courseid = c.id
WHERE
e.customint1 = ? AND status = 0
$catclause
";
$ascendants = $DB->get_records_sql($sql, $params);
}
return $ascendants;
}
/**
* Same but recursively
*/
public function get_all_ascendants($catid, $seeunbound, $courseid = null) {
global $DB;
$ascendants = array();
$ascendants = $this->get_ascendants($catid, $seeunbound, $courseid);
$childcats = $DB->get_records('course_categories', array('parent' => $catid));
if ($childcats) {
foreach ($childcats as $c) {
$ascendants = $ascendants + $this->get_all_ascendants($c->id, $seeunbound, $courseid);
}
}
return $ascendants;
}
/**
*
*/
public function user_can_edit() {
global $COURSE;
$context = context_course::instance($COURSE->id);
if (has_capability('block/course_ascendants:configure', $context)) {
return true;
}
return false;
}
/**
* tests if full course group exists
*
*/
public function has_course_group() {
global $COURSE, $DB;
return $DB->record_exists('groups', array('courseid' => $COURSE->id, 'name' => $COURSE->shortname));
}
/**
* registeres a "full course" (all participants) group
*
*/
public function make_course_group() {
global $COURSE, $DB;
if (!$this->has_course_group()) {
$groupobj->courseid = $COURSE->id;
$groupobj->name = $COURSE->shortname;
$groupobj->description = get_string('fullcourse', 'block_course_ascendants');
$groupobj->timecreated = time();
$groupobj->timemodified = time();
$fullgroupid = $DB->insert_record('groups', $groupobj);
}
}
}
function sort_by_localorder(&$a, &$b) {
if ($a->localorder > $b->localorder) {
return 1;
}
if ($a->localorder < $b->localorder) {
return -1;
}
return 0;
}