MDL-52828 competencies: Breakdown report
[moodle.git] / report / competency / classes / output / user_course_navigation.php
CommitLineData
21e0dd61
DW
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 * User navigation class.
19 *
20 * @package report_competency
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace report_competency\output;
25
26use renderable;
27use renderer_base;
28use templatable;
29use context_course;
f5f02ac0 30use core_user\external\user_summary_exporter;
27737ae6 31use core_course\external\course_module_summary_exporter;
21e0dd61
DW
32use stdClass;
33
34/**
35 * User course navigation class.
36 *
96c2b847 37 * @package report_competency
21e0dd61
DW
38 * @copyright 2015 Damyon Wiese
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class user_course_navigation implements renderable, templatable {
42
43 /** @var userid */
44 protected $userid;
45
46 /** @var courseid */
47 protected $courseid;
48
27737ae6
DW
49 /** @var moduleid */
50 protected $moduleid;
51
21e0dd61
DW
52 /** @var baseurl */
53 protected $baseurl;
54
55 /**
56 * Construct.
57 *
96c2b847
FM
58 * @param int $userid
59 * @param int $courseid
27737ae6 60 * @param int $moduleid
96c2b847 61 * @param string $baseurl
21e0dd61 62 */
27737ae6 63 public function __construct($userid, $courseid, $baseurl, $moduleid) {
21e0dd61
DW
64 $this->userid = $userid;
65 $this->courseid = $courseid;
27737ae6 66 $this->moduleid = $moduleid;
21e0dd61
DW
67 $this->baseurl = $baseurl;
68 }
69
70 /**
71 * Export the data.
72 *
73 * @param renderer_base $output
74 * @return stdClass
75 */
76 public function export_for_template(renderer_base $output) {
77 global $CFG, $DB, $SESSION, $PAGE;
78
79 $context = context_course::instance($this->courseid);
80
81 $data = new stdClass();
82 $data->userid = $this->userid;
83 $data->courseid = $this->courseid;
27737ae6 84 $data->moduleid = $this->moduleid;
fde4592c
DW
85 if (empty($data->moduleid)) {
86 // Moduleid is optional.
87 $data->moduleid = 0;
88 }
21e0dd61 89 $data->baseurl = $this->baseurl;
a8007119 90 $data->groupselector = '';
21e0dd61 91
eeb47707
SG
92 if (has_any_capability(array('moodle/competency:usercompetencyview', 'moodle/competency:coursecompetencymanage'),
93 $context)) {
21e0dd61 94 $course = $DB->get_record('course', array('id' => $this->courseid));
a8007119
DW
95 $currentgroup = groups_get_course_group($course, true);
96 if ($currentgroup !== false) {
97 $select = groups_allgroups_course_menu($course, $PAGE->url, true, $currentgroup);
98 $data->groupselector = $select;
99 }
21e0dd61
DW
100 // Fetch showactive.
101 $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
102 $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
103 $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
104
105 // Fetch current active group.
106 $groupmode = groups_get_course_groupmode($course);
21e0dd61 107
a6a722d4 108 $users = get_enrolled_users($context, 'moodle/competency:coursecompetencygradable', $currentgroup,
21e0dd61
DW
109 'u.*', null, 0, 0, $showonlyactiveenrol);
110
111 $data->users = array();
112 foreach ($users as $user) {
113 $exporter = new user_summary_exporter($user);
114 $user = $exporter->export($output);
115 if ($user->id == $this->userid) {
116 $user->selected = true;
117 }
118 $data->users[] = $user;
119 }
120 $data->hasusers = true;
27737ae6
DW
121
122 $data->hasmodules = true;
123 $data->modules = array();
124 $empty = (object)['id' => 0, 'name' => get_string('nofiltersapplied')];
125 $data->modules[] = $empty;
126
127 $modinfo = get_fast_modinfo($this->courseid);
128 foreach ($modinfo->get_cms() as $cm) {
129 if ($cm->uservisible) {
130 $exporter = new course_module_summary_exporter(null, ['cm' => $cm]);
131 $module = $exporter->export($output);
132 if ($module->id == $this->moduleid) {
133 $module->selected = true;
134 }
135 $data->modules[] = $module;
136 $data->hasmodules = true;
137 }
138 }
139
21e0dd61
DW
140 } else {
141 $data->users = array();
142 $data->hasusers = false;
143 }
144
145 return $data;
146 }
147}