MDL-52980 tool_lp: Double check the name of the capability
[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;
30use \tool_lp\external\user_summary_exporter;
31use stdClass;
32
33/**
34 * User course navigation class.
35 *
36 * @package tool_lp
37 * @copyright 2015 Damyon Wiese
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class user_course_navigation implements renderable, templatable {
41
42 /** @var userid */
43 protected $userid;
44
45 /** @var courseid */
46 protected $courseid;
47
48 /** @var baseurl */
49 protected $baseurl;
50
51 /**
52 * Construct.
53 *
54 * @param $userid
55 * @param $courseid
56 * @param $baseurl
57 */
58 public function __construct($userid, $courseid, $baseurl) {
59 $this->userid = $userid;
60 $this->courseid = $courseid;
61 $this->baseurl = $baseurl;
62 }
63
64 /**
65 * Export the data.
66 *
67 * @param renderer_base $output
68 * @return stdClass
69 */
70 public function export_for_template(renderer_base $output) {
71 global $CFG, $DB, $SESSION, $PAGE;
72
73 $context = context_course::instance($this->courseid);
74
75 $data = new stdClass();
76 $data->userid = $this->userid;
77 $data->courseid = $this->courseid;
78 $data->baseurl = $this->baseurl;
a8007119 79 $data->groupselector = '';
21e0dd61
DW
80
81 if (has_capability('tool/lp:coursecompetencymanage', $context)) {
82 $course = $DB->get_record('course', array('id' => $this->courseid));
a8007119
DW
83 $currentgroup = groups_get_course_group($course, true);
84 if ($currentgroup !== false) {
85 $select = groups_allgroups_course_menu($course, $PAGE->url, true, $currentgroup);
86 $data->groupselector = $select;
87 }
21e0dd61
DW
88 // Fetch showactive.
89 $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
90 $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
91 $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
92
93 // Fetch current active group.
94 $groupmode = groups_get_course_groupmode($course);
21e0dd61
DW
95
96 $users = get_enrolled_users($context, 'tool/lp:coursecompetencygradable', $currentgroup,
97 'u.*', null, 0, 0, $showonlyactiveenrol);
98
99 $data->users = array();
100 foreach ($users as $user) {
101 $exporter = new user_summary_exporter($user);
102 $user = $exporter->export($output);
103 if ($user->id == $this->userid) {
104 $user->selected = true;
105 }
106 $data->users[] = $user;
107 }
108 $data->hasusers = true;
109 } else {
110 $data->users = array();
111 $data->hasusers = false;
112 }
113
114 return $data;
115 }
116}