Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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