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 * Customfield Checkbox plugin
19 *
20 * @package   customfield_checkbox
21 * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace customfield_checkbox;
26
27use core_customfield\api;
28use core_customfield\output\field_data;
29
30defined('MOODLE_INTERNAL') || die;
31
32/**
33 * Class data
34 *
35 * @package customfield_checkbox
36 * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
37 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class data_controller extends \core_customfield\data_controller {
40
41    /**
42     * Return the name of the field where the information is stored
43     * @return string
44     */
45    public function datafield() : string {
46        return 'intvalue';
47    }
48
49    /**
50     * Add fields for editing a checkbox field.
51     *
52     * @param \MoodleQuickForm $mform
53     */
54    public function instance_form_definition(\MoodleQuickForm $mform) {
55        $field = $this->get_field();
56        $config = $field->get('configdata');
57        $elementname = $this->get_form_element_name();
58        // If checkbox is required (i.e. "agree to terms") then use 'checkbox' form element.
59        // The advcheckbox element cannot be used for required fields because advcheckbox elements always provide a value.
60        $isrequired = $field->get_configdata_property('required');
61        $mform->addElement($isrequired ? 'checkbox' : 'advcheckbox', $elementname, $this->get_field()->get_formatted_name());
62        $mform->setDefault($elementname, $config['checkbydefault']);
63        $mform->setType($elementname, PARAM_BOOL);
64        if ($isrequired) {
65            $mform->addRule($elementname, null, 'required', null, 'client');
66        }
67    }
68
69    /**
70     * Returns the default value as it would be stored in the database (not in human-readable format).
71     *
72     * @return mixed
73     */
74    public function get_default_value() {
75        return $this->get_field()->get_configdata_property('checkbydefault') ? 1 : 0;
76    }
77
78    /**
79     * Returns value in a human-readable format
80     *
81     * @return mixed|null value or null if empty
82     */
83    public function export_value() {
84        $value = $this->get_value();
85        return $value ? get_string('yes') : get_string('no');
86    }
87}
88