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 * Plan page output.
19 *
20 * @package    tool_lp
21 * @copyright  2015 Frédéric Massart - FMCorz.net
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace tool_lp\output;
25defined('MOODLE_INTERNAL') || die();
26
27use renderable;
28use templatable;
29use stdClass;
30use moodle_url;
31use core_competency\api;
32use core_competency\external\performance_helper;
33use core_competency\plan;
34use core_competency\external\competency_exporter;
35use core_competency\external\plan_exporter;
36use tool_lp\external\competency_path_exporter;
37
38/**
39 * Plan page class.
40 *
41 * @package    tool_lp
42 * @copyright  2015 Frédéric Massart - FMCorz.net
43 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45class plan_page implements renderable, templatable {
46
47    /** @var plan */
48    protected $plan;
49
50    /**
51     * Construct.
52     *
53     * @param plan $plan
54     */
55    public function __construct($plan) {
56        $this->plan = $plan;
57    }
58
59    /**
60     * Export the data.
61     *
62     * @param renderer_base $output
63     * @return stdClass
64     */
65    public function export_for_template(\renderer_base $output) {
66        $planexporter = new plan_exporter($this->plan, array('template' => $this->plan->get_template()));
67
68        $data = new stdClass();
69        $data->plan = $planexporter->export($output);
70        $data->competencies = array();
71        $data->pluginbaseurl = (new moodle_url('/admin/tool/lp'))->out(false);
72        $data->contextid = $this->plan->get_context()->id;
73
74        if ($data->plan->iscompleted) {
75            $ucproperty = 'usercompetencyplan';
76            $ucexporter = 'core_competency\\external\\user_competency_plan_exporter';
77        } else {
78            $ucproperty = 'usercompetency';
79            $ucexporter = 'core_competency\\external\\user_competency_exporter';
80        }
81
82        $helper = new performance_helper();
83        $pclist = api::list_plan_competencies($this->plan);
84        $proficientcount = 0;
85        foreach ($pclist as $pc) {
86            $comp = $pc->competency;
87            $usercomp = $pc->$ucproperty;
88
89            $compcontext = $helper->get_context_from_competency($comp);
90            $framework = $helper->get_framework_from_competency($comp);
91            $scale = $helper->get_scale_from_competency($comp);
92
93            // Prepare the data.
94            $record = new stdClass();
95            $exporter = new competency_exporter($comp, array('context' => $compcontext));
96            $record->competency = $exporter->export($output);
97
98            // Competency path.
99            $exporter = new competency_path_exporter([
100                'ancestors' => $comp->get_ancestors(),
101                'framework' => $framework,
102                'context' => $compcontext
103            ]);
104            $record->comppath = $exporter->export($output);
105
106            $exporter = new $ucexporter($usercomp, array('scale' => $scale));
107            $record->$ucproperty = $exporter->export($output);
108
109            $data->competencies[] = $record;
110            if ($usercomp->get('proficiency')) {
111                $proficientcount++;
112            }
113        }
114        $data->competencycount = count($data->competencies);
115        $data->proficientcompetencycount = $proficientcount;
116        if ($data->competencycount) {
117            $data->proficientcompetencypercentage = ((float) $proficientcount / (float) $data->competencycount) * 100.0;
118        } else {
119            $data->proficientcompetencypercentage = 0.0;
120        }
121        $data->proficientcompetencypercentageformatted = format_float($data->proficientcompetencypercentage);
122        return $data;
123    }
124}
125