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 for exporting competency_path data.
19 *
20 * @package    tool_lp
21 * @copyright  2016 Issam Taboubi <issam.taboubi@umontreal.ca>
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace tool_lp\external;
25defined('MOODLE_INTERNAL') || die();
26
27use renderer_base;
28use moodle_url;
29
30/**
31 * Class for exporting competency_path data.
32 *
33 * @copyright  2016 Issam Taboubi <issam.taboubi@umontreal.ca>
34 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class competency_path_exporter extends \core\external\exporter {
37
38    /**
39     * Constructor.
40     *
41     * @param array $related - related objects.
42     */
43    public function __construct($related) {
44        parent::__construct([], $related);
45    }
46
47    /**
48     * Return the list of properties.
49     *
50     * @return array
51     */
52    protected static function define_related() {
53        return [
54            'ancestors' => 'core_competency\\competency[]',
55            'framework' => 'core_competency\\competency_framework',
56            'context' => 'context'
57        ];
58    }
59
60    /**
61     * Return the list of additional properties used only for display.
62     *
63     * @return array - Keys with their types.
64     */
65    protected static function define_other_properties() {
66        return [
67            'ancestors' => [
68                'type' => path_node_exporter::read_properties_definition(),
69                'multiple' => true,
70            ],
71            'framework' => [
72                'type' => path_node_exporter::read_properties_definition()
73            ],
74            'pluginbaseurl' => [
75                'type' => PARAM_URL
76            ],
77            'pagecontextid' => [
78                'type' => PARAM_INT
79            ],
80            'showlinks' => [
81                'type' => PARAM_BOOL
82            ]
83        ];
84    }
85
86    /**
87     * Get the additional values to inject while exporting.
88     *
89     * @param renderer_base $output The renderer.
90     * @return array Keys are the property names, values are their values.
91     */
92    protected function get_other_values(renderer_base $output) {
93        $result = new \stdClass();
94        $ancestors = [];
95        $nodescount = count($this->related['ancestors']);
96        $i = 1;
97        $result->showlinks = \core_competency\api::show_links();
98        foreach ($this->related['ancestors'] as $competency) {
99            $exporter = new path_node_exporter([
100                    'id' => $competency->get('id'),
101                    'name' => $competency->get('idnumber'),
102                    'position' => $i,
103                    'first' => $i == 1,
104                    'last' => $i == $nodescount
105                ], [
106                    'context' => $this->related['context'],
107                ]
108            );
109            $ancestors[] = $exporter->export($output);
110            $i++;
111        }
112        $result->ancestors = $ancestors;
113        $exporter = new path_node_exporter([
114                'id' => $this->related['framework']->get('id'),
115                'name' => $this->related['framework']->get('shortname'),
116                'first' => 0,
117                'last' => 0,
118                'position' => -1
119            ], [
120                'context' => $this->related['context']
121            ]
122        );
123        $result->framework = $exporter->export($output);
124        $result->pluginbaseurl = (new moodle_url('/admin/tool/lp'))->out(true);
125        $result->pagecontextid = $this->related['context']->id;
126        return (array) $result;
127    }
128}
129