MDL-58490 block_myoverview: separate course and activity completion
[moodle.git] / blocks / myoverview / classes / output / main.php
CommitLineData
9ac2e865
RW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Class containing data for my overview block.
19 *
20 * @package block_myoverview
21 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace block_myoverview\output;
25defined('MOODLE_INTERNAL') || die();
26
27use renderable;
28use renderer_base;
29use templatable;
3f0c421b
RW
30use core_completion\progress;
31
9ac2e865
RW
32/**
33 * Class containing data for my overview block.
34 *
35 * @copyright 2017 Simey Lameze <simey@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class main implements renderable, templatable {
39
40 /**
41 * Export this data so it can be used as the context for a mustache template.
42 *
43 * @param \renderer_base $output
44 * @return stdClass
45 */
46 public function export_for_template(renderer_base $output) {
4a995a1f
SL
47 global $USER;
48
a7f9abc8 49 $courses = enrol_get_my_courses('*', 'fullname ASC');
3f0c421b
RW
50 $coursesprogress = [];
51
52 foreach ($courses as $course) {
3f0c421b 53
4a995a1f
SL
54 $completion = new \completion_info($course);
55
56 // First, let's make sure completion is enabled.
57 if (!$completion->is_enabled()) {
58 continue;
59 }
60
61 $percentage = progress::get_course_progress_percentage($course);
3f0c421b
RW
62 if (!is_null($percentage)) {
63 $percentage = floor($percentage);
64 }
65
4a995a1f
SL
66 $coursesprogress[$course->id]['completed'] = $completion->is_course_complete($USER->id);
67 $coursesprogress[$course->id]['progress'] = $percentage;
3f0c421b
RW
68 }
69
3f0c421b 70 $coursesview = new courses_view($courses, $coursesprogress);
01a95b86
DW
71 $nocoursesurl = $output->image_url('courses', 'block_myoverview')->out();
72 $noeventsurl = $output->image_url('activities', 'block_myoverview')->out();
9ac2e865
RW
73
74 return [
9ac2e865 75 'coursesview' => $coursesview->export_for_template($output),
cbd3c4fe
RW
76 'urls' => [
77 'nocourses' => $nocoursesurl,
78 'noevents' => $noeventsurl
79 ]
9ac2e865
RW
80 ];
81 }
82}