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 managecompetencyframeworks page
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 */
24namespace tool_lp\output;
25defined('MOODLE_INTERNAL') || die();
26
27use renderable;
28use templatable;
29use renderer_base;
30use single_button;
31use stdClass;
32use moodle_url;
33use context_system;
34use core_competency\api;
35use core_competency\competency;
36use core_competency\competency_framework;
37use core_competency\external\competency_framework_exporter;
38
39/**
40 * Class containing data for managecompetencies page
41 *
42 * @copyright  2015 Damyon Wiese
43 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45class manage_competencies_page implements renderable, templatable {
46
47    /** @var \core_competency\competency_framework $framework This competency framework. */
48    protected $framework = null;
49
50    /** @var \core_competency\competency[] $competencies List of competencies. */
51    protected $competencies = array();
52
53    /** @var string $search Text to search for. */
54    protected $search = '';
55
56    /** @var bool $canmanage Result of permissions checks. */
57    protected $canmanage = false;
58
59    /** @var moodle_url $pluginurlbase Base url to use constructing links. */
60    protected $pluginbaseurl = null;
61
62    /** @var context $pagecontext The page context. */
63    protected $pagecontext = null;
64
65    /** @var \core_competency\competency $competency The competency to show when the page loads. */
66    protected $competency = null;
67
68    /**
69     * Construct this renderable.
70     *
71     * @param \core_competency\competency_framework $framework Competency framework.
72     * @param string $search Search string.
73     * @param context $pagecontext The page context.
74     * @param \core_competency\competency $competency The core competency to show when the page loads.
75     */
76    public function __construct($framework, $search, $pagecontext, $competency) {
77        $this->framework = $framework;
78        $this->pagecontext = $pagecontext;
79        $this->search = $search;
80        $this->competency = $competency;
81        $addpage = new single_button(
82           new moodle_url('/admin/tool/lp/editcompetencyframework.php'),
83           get_string('addnewcompetency', 'tool_lp')
84        );
85        $this->navigation[] = $addpage;
86
87        $this->canmanage = has_capability('moodle/competency:competencymanage', $framework->get_context());
88    }
89
90    /**
91     * Export this data so it can be used as the context for a mustache template.
92     *
93     * @param renderer_base $output Renderer base.
94     * @return stdClass
95     */
96    public function export_for_template(renderer_base $output) {
97        $data = new stdClass();
98        $exporter = new competency_framework_exporter($this->framework);
99        $data->framework = $exporter->export($output);
100        $data->canmanage = $this->canmanage;
101        $data->search = $this->search;
102        $data->pagecontextid = $this->pagecontext->id;
103        $data->pluginbaseurl = (new moodle_url('/admin/tool/lp'))->out(true);
104
105        $data->competencyid = 0;
106        if ($this->competency) {
107            $data->competencyid = $this->competency->get('id');
108        }
109
110        $rulesmodules = array();
111        $rules = competency::get_available_rules();
112        foreach ($rules as $type => $rulename) {
113
114            $amd = null;
115            if ($type == 'core_competency\\competency_rule_all') {
116                $amd = 'tool_lp/competency_rule_all';
117            } else if ($type == 'core_competency\\competency_rule_points') {
118                $amd = 'tool_lp/competency_rule_points';
119            } else {
120                // We do not know how to display that rule.
121                continue;
122            }
123
124            $rulesmodules[] = [
125                'name' => (string) $rulename,
126                'type' => $type,
127                'amd' => $amd,
128            ];
129        }
130        $data->rulesmodules = json_encode(array_values($rulesmodules));
131
132        return $data;
133    }
134}
135