mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
main.php
604 lines (548 loc) · 23.2 KB
/
main.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<?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/>.
/**
* Class containing data for my overview block.
*
* @package block_myoverview
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_myoverview\output;
defined('MOODLE_INTERNAL') || die();
use core_competency\url;
use renderable;
use renderer_base;
use templatable;
use stdClass;
require_once($CFG->dirroot . '/blocks/myoverview/lib.php');
/**
* Class containing data for my overview block.
*
* @copyright 2018 Bas Brands <bas@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class main implements renderable, templatable {
/**
* Store the grouping preference.
*
* @var string String matching the grouping constants defined in myoverview/lib.php
*/
private $grouping;
/**
* Store the sort preference.
*
* @var string String matching the sort constants defined in myoverview/lib.php
*/
private $sort;
/**
* Store the view preference.
*
* @var string String matching the view/display constants defined in myoverview/lib.php
*/
private $view;
/**
* Store the paging preference.
*
* @var string String matching the paging constants defined in myoverview/lib.php
*/
private $paging;
/**
* Store the display categories config setting.
*
* @var boolean
*/
private $displaycategories;
/**
* Store the configuration values for the myoverview block.
*
* @var array Array of available layouts matching view/display constants defined in myoverview/lib.php
*/
private $layouts;
/**
* Store a course grouping option setting
*
* @var boolean
*/
private $displaygroupingallincludinghidden;
/**
* Store a course grouping option setting.
*
* @var boolean
*/
private $displaygroupingall;
/**
* Store a course grouping option setting.
*
* @var boolean
*/
private $displaygroupinginprogress;
/**
* Store a course grouping option setting.
*
* @var boolean
*/
private $displaygroupingfuture;
/**
* Store a course grouping option setting.
*
* @var boolean
*/
private $displaygroupingpast;
/**
* Store a course grouping option setting.
*
* @var boolean
*/
private $displaygroupingfavourites;
/**
* Store a course grouping option setting.
*
* @var boolean
*/
private $displaygroupinghidden;
/**
* Store a course grouping option setting.
*
* @var bool
*/
private $displaygroupingcustomfield;
/**
* Store the custom field used by customfield grouping.
*
* @var string
*/
private $customfiltergrouping;
/**
* Store the selected custom field value to group by.
*
* @var string
*/
private $customfieldvalue;
/**
* main constructor.
* Initialize the user preferences
*
* @param string $grouping Grouping user preference
* @param string $sort Sort user preference
* @param string $view Display user preference
* @param int $paging
* @param string $customfieldvalue
*
* @throws \dml_exception
*/
public function __construct($grouping, $sort, $view, $paging, $customfieldvalue = null) {
global $CFG;
// Get plugin config.
$config = get_config('block_myoverview');
// Build the course grouping option name to check if the given grouping is enabled afterwards.
$groupingconfigname = 'displaygrouping'.$grouping;
// Check the given grouping and remember it if it is enabled.
if ($grouping && $config->$groupingconfigname == true) {
$this->grouping = $grouping;
// Otherwise fall back to another grouping in a reasonable order.
// This is done to prevent one-time UI glitches in the case when a user has chosen a grouping option previously which
// was then disabled by the admin in the meantime.
} else {
$this->grouping = $this->get_fallback_grouping($config);
}
unset ($groupingconfigname);
// Remember which custom field value we were using, if grouping by custom field.
$this->customfieldvalue = $customfieldvalue;
// Check and remember the given sorting.
if ($sort) {
$this->sort = $sort;
} else if ($CFG->courselistshortnames) {
$this->sort = BLOCK_MYOVERVIEW_SORTING_SHORTNAME;
} else {
$this->sort = BLOCK_MYOVERVIEW_SORTING_TITLE;
}
// In case sorting remembered is shortname and display extended course names not checked,
// we should revert sorting to title.
if (!$CFG->courselistshortnames && $sort == BLOCK_MYOVERVIEW_SORTING_SHORTNAME) {
$this->sort = BLOCK_MYOVERVIEW_SORTING_TITLE;
}
// Check and remember the given view.
$this->view = $view ? $view : BLOCK_MYOVERVIEW_VIEW_CARD;
// Check and remember the given page size, `null` indicates no page size set
// while a `0` indicates a paging size of `All`.
if (!is_null($paging) && $paging == BLOCK_MYOVERVIEW_PAGING_ALL) {
$this->paging = BLOCK_MYOVERVIEW_PAGING_ALL;
} else {
$this->paging = $paging ? $paging : BLOCK_MYOVERVIEW_PAGING_12;
}
// Check and remember if the course categories should be shown or not.
if (!$config->displaycategories) {
$this->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_OFF;
} else {
$this->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_ON;
}
// Get and remember the available layouts.
$this->set_available_layouts();
$this->view = $view ? $view : reset($this->layouts);
// Check and remember if the particular grouping options should be shown or not.
$this->displaygroupingallincludinghidden = $config->displaygroupingallincludinghidden;
$this->displaygroupingall = $config->displaygroupingall;
$this->displaygroupinginprogress = $config->displaygroupinginprogress;
$this->displaygroupingfuture = $config->displaygroupingfuture;
$this->displaygroupingpast = $config->displaygroupingpast;
$this->displaygroupingfavourites = $config->displaygroupingfavourites;
$this->displaygroupinghidden = $config->displaygroupinghidden;
$this->displaygroupingcustomfield = ($config->displaygroupingcustomfield && $config->customfiltergrouping);
$this->customfiltergrouping = $config->customfiltergrouping;
// Check and remember if the grouping selector should be shown at all or not.
// It will be shown if more than 1 grouping option is enabled.
$displaygroupingselectors = array($this->displaygroupingallincludinghidden,
$this->displaygroupingall,
$this->displaygroupinginprogress,
$this->displaygroupingfuture,
$this->displaygroupingpast,
$this->displaygroupingfavourites,
$this->displaygroupinghidden);
$displaygroupingselectorscount = count(array_filter($displaygroupingselectors));
if ($displaygroupingselectorscount > 1 || $this->displaygroupingcustomfield) {
$this->displaygroupingselector = true;
} else {
$this->displaygroupingselector = false;
}
unset ($displaygroupingselectors, $displaygroupingselectorscount);
}
/**
* Determine the most sensible fallback grouping to use (in cases where the stored selection
* is no longer available).
* @param object $config
* @return string
*/
private function get_fallback_grouping($config) {
if ($config->displaygroupingall == true) {
return BLOCK_MYOVERVIEW_GROUPING_ALL;
}
if ($config->displaygroupingallincludinghidden == true) {
return BLOCK_MYOVERVIEW_GROUPING_ALLINCLUDINGHIDDEN;
}
if ($config->displaygroupinginprogress == true) {
return BLOCK_MYOVERVIEW_GROUPING_INPROGRESS;
}
if ($config->displaygroupingfuture == true) {
return BLOCK_MYOVERVIEW_GROUPING_FUTURE;
}
if ($config->displaygroupingpast == true) {
return BLOCK_MYOVERVIEW_GROUPING_PAST;
}
if ($config->displaygroupingfavourites == true) {
return BLOCK_MYOVERVIEW_GROUPING_FAVOURITES;
}
if ($config->displaygroupinghidden == true) {
return BLOCK_MYOVERVIEW_GROUPING_HIDDEN;
}
if ($config->displaygroupingcustomfield == true) {
return BLOCK_MYOVERVIEW_GROUPING_CUSTOMFIELD;
}
// In this case, no grouping option is enabled and the grouping is not needed at all.
// But it's better not to leave $this->grouping unset for any unexpected case.
return BLOCK_MYOVERVIEW_GROUPING_ALLINCLUDINGHIDDEN;
}
/**
* Set the available layouts based on the config table settings,
* if none are available, defaults to the cards view.
*
* @throws \dml_exception
*
*/
public function set_available_layouts() {
if ($config = get_config('block_myoverview', 'layouts')) {
$this->layouts = explode(',', $config);
} else {
$this->layouts = array(BLOCK_MYOVERVIEW_VIEW_CARD);
}
}
/**
* Get the user preferences as an array to figure out what has been selected.
*
* @return array $preferences Array with the pref as key and value set to true
*/
public function get_preferences_as_booleans() {
$preferences = [];
$preferences[$this->sort] = true;
$preferences[$this->grouping] = true;
// Only use the user view/display preference if it is in available layouts.
if (in_array($this->view, $this->layouts)) {
$preferences[$this->view] = true;
} else {
$preferences[reset($this->layouts)] = true;
}
return $preferences;
}
/**
* Format a layout into an object for export as a Context variable to template.
*
* @param string $layoutname
*
* @return \stdClass $layout an object representation of a layout
* @throws \coding_exception
*/
public function format_layout_for_export($layoutname) {
$layout = new stdClass();
$layout->id = $layoutname;
$layout->name = get_string($layoutname, 'block_myoverview');
$layout->active = $this->view == $layoutname ? true : false;
$layout->arialabel = get_string('aria:' . $layoutname, 'block_myoverview');
return $layout;
}
/**
* Get the available layouts formatted for export.
*
* @return array an array of objects representing available layouts
*/
public function get_formatted_available_layouts_for_export() {
return array_map(array($this, 'format_layout_for_export'), $this->layouts);
}
/**
* Get the list of values to add to the grouping dropdown
* @return object[] containing name, value and active fields
*/
public function get_customfield_values_for_export() {
global $DB, $USER;
if (!$this->displaygroupingcustomfield) {
return [];
}
$fieldid = $DB->get_field('customfield_field', 'id', ['shortname' => $this->customfiltergrouping]);
if (!$fieldid) {
return [];
}
$courses = enrol_get_all_users_courses($USER->id, true);
if (!$courses) {
return [];
}
list($csql, $params) = $DB->get_in_or_equal(array_keys($courses), SQL_PARAMS_NAMED);
$select = "instanceid $csql AND fieldid = :fieldid";
$params['fieldid'] = $fieldid;
$distinctablevalue = $DB->sql_compare_text('value');
$values = $DB->get_records_select_menu('customfield_data', $select, $params, '',
"DISTINCT $distinctablevalue, $distinctablevalue AS value2");
\core_collator::asort($values, \core_collator::SORT_NATURAL);
$values = array_filter($values);
if (!$values) {
return [];
}
$field = \core_customfield\field_controller::create($fieldid);
$isvisible = $field->get_configdata_property('visibility') == \core_course\customfield\course_handler::VISIBLETOALL;
// Only visible fields to everybody supporting course grouping will be displayed.
if (!$field->supports_course_grouping() || !$isvisible) {
return []; // The field shouldn't have been selectable in the global settings, but just skip it now.
}
$values = $field->course_grouping_format_values($values);
$customfieldactive = ($this->grouping === BLOCK_MYOVERVIEW_GROUPING_CUSTOMFIELD);
$ret = [];
foreach ($values as $value => $name) {
$ret[] = (object)[
'name' => $name,
'value' => $value,
'active' => ($customfieldactive && ($this->customfieldvalue == $value)),
];
}
return $ret;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param \renderer_base $output
* @return array Context variables for the template
* @throws \coding_exception
*
*/
public function export_for_template(renderer_base $output) {
global $CFG, $USER;
$nocoursesurl = $output->image_url('courses', 'block_myoverview')->out();
$newcourseurl = '';
$coursecat = \core_course_category::user_top();
if ($coursecat && ($category = \core_course_category::get_nearest_editable_subcategory($coursecat, ['create']))) {
$newcourseurl = new \moodle_url('/course/edit.php', ['category' => $category->id]);
}
$customfieldvalues = $this->get_customfield_values_for_export();
$selectedcustomfield = '';
if ($this->grouping == BLOCK_MYOVERVIEW_GROUPING_CUSTOMFIELD) {
foreach ($customfieldvalues as $field) {
if ($field->value == $this->customfieldvalue) {
$selectedcustomfield = $field->name;
break;
}
}
// If the selected custom field value has not been found (possibly because the field has
// been changed in the settings) find a suitable fallback.
if (!$selectedcustomfield) {
$this->grouping = $this->get_fallback_grouping(get_config('block_myoverview'));
if ($this->grouping == BLOCK_MYOVERVIEW_GROUPING_CUSTOMFIELD) {
// If the fallback grouping is still customfield, then select the first field.
$firstfield = reset($customfieldvalues);
if ($firstfield) {
$selectedcustomfield = $firstfield->name;
$this->customfieldvalue = $firstfield->value;
}
}
}
}
$preferences = $this->get_preferences_as_booleans();
$availablelayouts = $this->get_formatted_available_layouts_for_export();
$sort = '';
if ($this->sort == BLOCK_MYOVERVIEW_SORTING_SHORTNAME) {
$sort = 'shortname';
} else {
$sort = $this->sort == BLOCK_MYOVERVIEW_SORTING_TITLE ? 'fullname' : 'ul.timeaccess desc';
}
$defaultvariables = [
'totalcoursecount' => count(enrol_get_all_users_courses($USER->id, true)),
'nocoursesimg' => $nocoursesurl,
'newcourseurl' => $newcourseurl,
'grouping' => $this->grouping,
'sort' => $sort,
// If the user preference display option is not available, default to first available layout.
'view' => in_array($this->view, $this->layouts) ? $this->view : reset($this->layouts),
'paging' => $this->paging,
'layouts' => $availablelayouts,
'displaycategories' => $this->displaycategories,
'displaydropdown' => (count($availablelayouts) > 1) ? true : false,
'displaygroupingallincludinghidden' => $this->displaygroupingallincludinghidden,
'displaygroupingall' => $this->displaygroupingall,
'displaygroupinginprogress' => $this->displaygroupinginprogress,
'displaygroupingfuture' => $this->displaygroupingfuture,
'displaygroupingpast' => $this->displaygroupingpast,
'displaygroupingfavourites' => $this->displaygroupingfavourites,
'displaygroupinghidden' => $this->displaygroupinghidden,
'displaygroupingselector' => $this->displaygroupingselector,
'displaygroupingcustomfield' => $this->displaygroupingcustomfield && $customfieldvalues,
'customfieldname' => $this->customfiltergrouping,
'customfieldvalue' => $this->customfieldvalue,
'customfieldvalues' => $customfieldvalues,
'selectedcustomfield' => $selectedcustomfield,
'showsortbyshortname' => $CFG->courselistshortnames,
];
return array_merge($defaultvariables, $preferences);
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param \renderer_base $output
* @return array Context variables for the template
* @throws \coding_exception
*
*/
public function export_for_zero_state_template(renderer_base $output) {
global $CFG, $DB;
$nocoursesimg = $output->image_url('courses', 'block_myoverview');
$coursecat = \core_course_category::user_top();
if ($coursecat) {
$category = \core_course_category::get_nearest_editable_subcategory($coursecat, ['moodle/course:request']);
if ($category && $category->can_request_course()) {
// Add Request a course button.
$button = new \single_button(
new \moodle_url('/course/request.php', ['category' => $category->id]),
get_string('requestcourse'),
'post',
\single_button::BUTTON_PRIMARY
);
return $this->generate_zero_state_data(
$nocoursesimg,
[$button],
['title' => 'zero_request_title', 'intro' => 'zero_request_intro']
);
}
$totalcourses = $DB->count_records_select('course', 'category > 0');
if (!$totalcourses && ($category = \core_course_category::get_nearest_editable_subcategory($coursecat, ['create']))) {
// Add Quickstart guide and Create course buttons.
$quickstarturl = $CFG->coursecreationguide;
if ($quickstarturl) {
$quickstartbutton = new \single_button(
new \moodle_url($quickstarturl, ['lang' => current_language()]),
get_string('viewquickstart', 'block_myoverview'),
);
$buttons = [$quickstartbutton->export_for_template($output)];
}
$createbutton = new \single_button(
new \moodle_url('/course/edit.php', ['category' => $category->id]),
get_string('createcourse', 'block_myoverview'),
'post',
\single_button::BUTTON_PRIMARY
);
$buttons[] = $createbutton->export_for_template($output);
return $this->generate_zero_state_data(
$nocoursesimg,
$buttons,
['title' => 'zero_nocourses_title', 'intro' => 'zero_nocourses_intro']
);
}
if ($categorytocreate = \core_course_category::get_nearest_editable_subcategory($coursecat, ['create'])) {
$createbutton = new \single_button(
new \moodle_url('/course/edit.php', ['category' => $categorytocreate->id]),
get_string('createcourse', 'block_myoverview'),
'post',
\single_button::BUTTON_PRIMARY
);
$buttons = [$createbutton->export_for_template($output)];
if ($categorytomanage = \core_course_category::get_nearest_editable_subcategory($coursecat, ['manage'])) {
// Add a Manage course button.
$managebutton = new \single_button(
new \moodle_url('/course/management.php', ['category' => $categorytomanage->id]),
get_string('managecourses')
);
$buttons[] = $managebutton->export_for_template($output);
return $this->generate_zero_state_data(
$nocoursesimg,
array_reverse($buttons),
['title' => 'zero_default_title', 'intro' => 'zero_default_intro']
);
}
return $this->generate_zero_state_data(
$nocoursesimg,
$buttons,
['title' => 'zero_default_title', 'intro' => 'zero_default_intro']
);
}
}
return $this->generate_zero_state_data(
$nocoursesimg,
[],
['title' => 'zero_default_title', 'intro' => 'zero_default_intro']
);
}
/**
* Generate the state zero data.
*
* @param \moodle_url $imageurl The URL to the image to show
* @param \single_button[] $buttons
* @param array $strings Title and intro strings for the zero state if needed.
* @return array Context variables for the template
*/
private function generate_zero_state_data(\moodle_url $imageurl, array $buttons, array $strings) {
global $CFG;
// Documentation data.
$dochref = new \moodle_url($CFG->docroot, ['lang' => current_language()]);
$quickstart = new \moodle_url($CFG->coursecreationguide, ['lang' => current_language()]);
$docparams = [
'quickhref' => $quickstart->out(),
'quicktitle' => get_string('viewquickstart', 'block_myoverview'),
'quicktarget' => '_blank',
'dochref' => $dochref->out(),
'doctitle' => get_string('documentation'),
'doctarget' => $CFG->doctonewwindow ? '_blank' : '_self',
];
return [
'nocoursesimg' => $imageurl->out(),
'title' => ($strings['title']) ? get_string($strings['title'], 'block_myoverview') : '',
'intro' => ($strings['intro']) ? get_string($strings['intro'], 'block_myoverview', $docparams) : '',
'buttons' => $buttons,
];
}
}