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 * Form alignment for editing.
18 *
19 * @package    core
20 * @subpackage badges
21 * @copyright  2018 Tung Thai
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @author     Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
24 */
25defined('MOODLE_INTERNAL') || die();
26require_once($CFG->libdir . '/formslib.php');
27require_once($CFG->libdir . '/badgeslib.php');
28
29/**
30 * Form to edit alignment.
31 *
32 * @copyright  2018 Tung Thai
33 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 * @author     Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
35 */
36class alignment_form extends moodleform {
37
38    /**
39     * Defines the form.
40     */
41    public function definition() {
42        global $DB;
43        $mform = $this->_form;
44        $badge = $this->_customdata['badge'];
45        $action = $this->_customdata['action'];
46        $alignmentid = $this->_customdata['alignmentid'];
47        $mform->addElement('header', 'alignment', get_string('alignment', 'badges'));
48        $mform->addElement('text', 'targetname', get_string('targetname', 'badges'), array('size' => '70'));
49        $mform->setType('targetname', PARAM_TEXT);
50        $mform->addRule('targetname', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
51        $mform->addRule('targetname', null, 'required');
52        $mform->addHelpButton('targetname', 'targetname', 'badges');
53        $mform->addElement('text', 'targeturl', get_string('targeturl', 'badges'), array('size' => '70'));
54        $mform->setType('targeturl', PARAM_URL);
55        $mform->addRule('targeturl', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
56        $mform->addRule('targeturl', null, 'required');
57        $mform->addHelpButton('targeturl', 'targeturl', 'badges');
58        $mform->addElement('text', 'targetframework', get_string('targetframework', 'badges'), array('size' => '70'));
59        $mform->setType('targetframework', PARAM_TEXT);
60        $mform->addRule('targetframework', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
61        $mform->addHelpButton('targetframework', 'targetframework', 'badges');
62        $mform->addElement('text', 'targetcode', get_string('targetcode', 'badges'), array('size' => '70'));
63        $mform->setType('targetcode', PARAM_TEXT);
64        $mform->addRule('targetcode', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
65        $mform->addHelpButton('targetcode', 'targetcode', 'badges');
66        $mform->addElement('textarea', 'targetdescription', get_string('targetdescription', 'badges'),
67            'wrap="virtual" rows="8" cols="70"');
68        $this->add_action_buttons();
69        if ($action == 'edit' || $alignmentid) {
70            $alignment = new stdClass();
71            $alignment = $DB->get_record_select('badge_alignment', 'id = ?', array($alignmentid));
72            $this->set_data($alignment);
73            // Freeze all elements if badge is active or locked.
74            if ($badge->is_active() || $badge->is_locked()) {
75                $mform->hardFreezeAllVisibleExcept(array());
76            }
77        }
78    }
79
80    /**
81     * Validate the data from the form.
82     *
83     * @param  array $data form data
84     * @param  array $files form files
85     * @return array An array of error messages.
86     */
87    public function validation($data, $files) {
88        $errors = parent::validation($data, $files);
89        if (!empty($data['targeturl']) && !preg_match('@^https?://.+@', $data['targeturl'])) {
90            $errors['targeturl'] = get_string('invalidurl', 'badges');
91        }
92        return $errors;
93    }
94}
95