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 learning plan template competencies page
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 context_course;
27use renderable;
28use core_user;
29use templatable;
30use renderer_base;
31use moodle_url;
32use stdClass;
33use core_competency\api;
34use core_competency\external\user_competency_course_exporter;
35use core_user\external\user_summary_exporter;
36use core_competency\external\performance_helper;
37use core_competency\url;
38use core_competency\user_competency;
39use tool_lp\external\competency_summary_exporter;
40use core_course\external\course_summary_exporter;
41
42/**
43 * Class containing data for learning plan template competencies page
44 *
45 * @copyright  2015 Damyon Wiese
46 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 */
48class report implements renderable, templatable {
49
50    /** @var context $context */
51    protected $context;
52    /** @var int $courseid */
53    protected $courseid;
54    /** @var int $moduleid */
55    protected $moduleid;
56    /** @var array $competencies */
57    protected $competencies;
58
59    /**
60     * Construct this renderable.
61     *
62     * @param int $courseid The course id
63     * @param int $userid The user id
64     * @param int $moduleid The module id
65     */
66    public function __construct($courseid, $userid, $moduleid) {
67        $this->courseid = $courseid;
68        $this->userid = $userid;
69        $this->moduleid = $moduleid;
70        $this->context = context_course::instance($courseid);
71    }
72
73    /**
74     * Export this data so it can be used as the context for a mustache template.
75     *
76     * @param \renderer_base $output
77     * @return stdClass
78     */
79    public function export_for_template(renderer_base $output) {
80        global $DB;
81
82        $data = new stdClass();
83        $data->courseid = $this->courseid;
84        $data->moduleid = $this->moduleid;
85        if (empty($data->moduleid)) {
86            $data->moduleid = 0;
87        }
88
89        $course = $DB->get_record('course', array('id' => $this->courseid));
90        $coursecontext = context_course::instance($course->id);
91        $exporter = new course_summary_exporter($course, array('context' => $coursecontext));
92        $coursecompetencysettings = api::read_course_competency_settings($course->id);
93        $data->pushratingstouserplans = $coursecompetencysettings->get('pushratingstouserplans');
94        $data->course = $exporter->export($output);
95
96        $data->usercompetencies = array();
97        $user = core_user::get_user($this->userid);
98
99        $exporter = new user_summary_exporter($user);
100        $data->user = $exporter->export($output);
101        $data->usercompetencies = array();
102        $coursecompetencies = api::list_course_competencies($this->courseid);
103        $usercompetencycourses = api::list_user_competencies_in_course($this->courseid, $user->id);
104        if ($this->moduleid > 0) {
105            $modulecompetencies = api::list_course_module_competencies_in_course_module($this->moduleid);
106            foreach ($usercompetencycourses as $ucid => $usercompetency) {
107                $found = false;
108                foreach ($modulecompetencies as $mcid => $modulecompetency) {
109                    if ($modulecompetency->get('competencyid') == $usercompetency->get('competencyid')) {
110                        $found = true;
111                        break;
112                    }
113                }
114
115                if (!$found) {
116                    // We need to filter out this competency.
117                    unset($usercompetencycourses[$ucid]);
118                }
119            }
120        }
121
122        $helper = new performance_helper();
123        foreach ($usercompetencycourses as $usercompetencycourse) {
124            $onerow = new stdClass();
125            $competency = null;
126            foreach ($coursecompetencies as $coursecompetency) {
127                if ($coursecompetency['competency']->get('id') == $usercompetencycourse->get('competencyid')) {
128                    $competency = $coursecompetency['competency'];
129                    break;
130                }
131            }
132            if (!$competency) {
133                continue;
134            }
135
136            $framework = $helper->get_framework_from_competency($competency);
137            $scale = $helper->get_scale_from_competency($competency);
138
139            $exporter = new user_competency_course_exporter($usercompetencycourse, array('scale' => $scale));
140            $record = $exporter->export($output);
141            $onerow->usercompetencycourse = $record;
142            $exporter = new competency_summary_exporter(null, array(
143                'competency' => $competency,
144                'framework' => $framework,
145                'context' => $framework->get_context(),
146                'relatedcompetencies' => array(),
147                'linkedcourses' => array()
148            ));
149            $onerow->competency = $exporter->export($output);
150            array_push($data->usercompetencies, $onerow);
151        }
152
153        return $data;
154    }
155}
156