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/**
19 * Framework selector field.
20 *
21 * @package    tool_lp
22 * @copyright  2016 Frédéric Massart - FMCorz.net
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace tool_lp\form;
27
28use coding_exception;
29use MoodleQuickForm_autocomplete;
30use \core_competency\competency_framework;
31
32global $CFG;
33require_once($CFG->libdir . '/form/autocomplete.php');
34
35
36/**
37 * Form field type for choosing a framework.
38 *
39 * @package    tool_lp
40 * @copyright  2016 Frédéric Massart - FMCorz.net
41 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class framework_autocomplete extends MoodleQuickForm_autocomplete {
44
45    /** @var bool Only visible frameworks? */
46    protected $onlyvisible = false;
47
48    /**
49     * Constructor.
50     *
51     * @param string $elementName Element name
52     * @param mixed $elementLabel Label(s) for an element
53     * @param array $options Options to control the element's display
54     *                       Valid options are:
55     *                       - context context The context.
56     *                       - contextid int The context id.
57     *                       - multiple bool Whether or not the field accepts more than one values.
58     *                       - onlyvisible bool Whether or not only visible framework can be listed.
59     */
60    public function __construct($elementName = null, $elementLabel = null, $options = array()) {
61        $contextid = null;
62        if (!empty($options['contextid'])) {
63            $contextid = $options['contextid'];
64        } else if (!empty($options['context'])) {
65            $contextid = $options['context']->id;
66        }
67
68        $this->onlyvisible = !empty($options['onlyvisible']);
69
70        $validattributes = array(
71            'ajax' => 'tool_lp/frameworks_datasource',
72            'data-contextid' => $contextid,
73            'data-onlyvisible' => $this->onlyvisible ? '1' : '0',
74        );
75        if (!empty($options['multiple'])) {
76            $validattributes['multiple'] = 'multiple';
77        }
78
79        parent::__construct($elementName, $elementLabel, array(), $validattributes);
80    }
81
82    /**
83     * Set the value of this element.
84     *
85     * @param  string|array $value The value to set.
86     * @return boolean
87     */
88    public function setValue($value) {
89        global $DB;
90        $values = (array) $value;
91        $ids = array();
92
93        foreach ($values as $onevalue) {
94            if (!empty($onevalue) && (!$this->optionExists($onevalue)) &&
95                    ($onevalue !== '_qf__force_multiselect_submission')) {
96                array_push($ids, $onevalue);
97            }
98        }
99
100        if (empty($ids)) {
101            return $this->setSelected(array());
102        }
103
104        // Logic here is simulating API.
105        $toselect = array();
106        list($insql, $inparams) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED, 'param');
107        $frameworks = competency_framework::get_records_select("id $insql", $inparams, 'shortname');
108        foreach ($frameworks as $framework) {
109            if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'),
110                    $framework->get_context())) {
111                continue;
112            } else if ($this->onlyvisible && !$framework->get('visible')) {
113                continue;
114            }
115            $this->addOption($framework->get('shortname') . ' ' . $framework->get('idnumber'), $framework->get('id'));
116            array_push($toselect, $framework->get('id'));
117        }
118
119        return $this->setSelected($toselect);
120    }
121}
122