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 competency page. Lists everything known about a user competency.
19 *
20 * @package    tool_lp
21 * @copyright  2015 Damyon Wiese
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require(__DIR__ . '/../../../config.php');
26
27$userid = optional_param('userid', 0, PARAM_INT);
28$competencyid = required_param('competencyid', PARAM_INT);
29$courseid = required_param('courseid', PARAM_INT);
30
31require_login(null, false);
32if (isguestuser()) {
33    throw new require_login_exception('Guests are not allowed here.');
34}
35\core_competency\api::require_enabled();
36
37$course = $DB->get_record('course', array('id' => $courseid));
38$context = context_course::instance($courseid);
39$currentgroup = groups_get_course_group($course, true);
40if (empty($userid)) {
41    $gradable = get_enrolled_users($context, 'moodle/competency:coursecompetencygradable', $currentgroup, 'u.id', null, 0, 1);
42    if (empty($gradable)) {
43        $userid = 0;
44    } else {
45        $userid = array_pop($gradable)->id;
46    }
47} else {
48    $gradable = get_enrolled_users($context, 'moodle/competency:coursecompetencygradable', $currentgroup, 'u.id');
49    if (count($gradable) == 0) {
50        $userid = 0;
51    } else if (!in_array($userid, array_keys($gradable))) {
52        $userid = array_shift($gradable)->id;
53    }
54}
55
56$params = array('userid' => $userid, 'competencyid' => $competencyid, 'courseid' => $courseid);
57$url = new moodle_url('/admin/tool/lp/user_competency_in_course.php', $params);
58
59if ($userid > 0) {
60    $usercontext = context_user::instance($userid);
61    $user = $DB->get_record('user', array('id' => $userid));
62}
63$competency = new \core_competency\competency($competencyid);
64
65// Does a permissions check for us.
66if ($userid > 0) {
67    $usercompetencycourses = \core_competency\api::list_user_competencies_in_course($courseid, $userid);
68}
69$subtitle = $competency->get('shortname') . ' <em>' . $competency->get('idnumber') . '</em>';
70
71list($title, $subtitle) = \tool_lp\page_helper::setup_for_course($url, $course, $subtitle);
72
73$output = $PAGE->get_renderer('tool_lp');
74if ($userid > 0) {
75    $userheading = array(
76        'heading' => fullname($user),
77        'user' => $user,
78        'usercontext' => $usercontext
79    );
80}
81echo $output->header();
82if ($userid > 0) {
83    echo $OUTPUT->context_header($userheading, 3);
84}
85
86$baseurl = new moodle_url('/admin/tool/lp/user_competency_in_course.php');
87$nav = new \tool_lp\output\user_competency_course_navigation($userid, $competencyid, $courseid, $baseurl);
88echo $output->render($nav);
89if ($userid > 0) {
90    $page = new \tool_lp\output\user_competency_summary_in_course($userid, $competencyid, $courseid);
91    echo $output->render($page);
92
93    // Trigger the viewed event.
94    $uc = \core_competency\api::get_user_competency_in_course($courseid, $userid, $competencyid);
95    \core_competency\api::user_competency_viewed_in_course($uc, $courseid);
96} else {
97    echo $output->container('', 'clearfix');
98    echo $output->notify_problem(get_string('noparticipants', 'tool_lp'));
99}
100
101echo $output->footer();
102