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
 
19
use moodleform;
20
use stdClass;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
require_once($CFG->libdir . '/formslib.php');
24
require_once($CFG->libdir . '/badgeslib.php');
25
 
26
/**
27
 * Form endorsement for editing.
28
 *
29
 * @package    core_badges
30
 * @copyright  2018 Tung Thai
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @author     Tung Thai
33
 */
34
class endorsement extends moodleform {
35
 
36
    /**
37
     * Defines the form.
38
     */
39
    public function definition() {
40
        $mform = $this->_form;
41
        $badge = $this->_customdata['badge'];
42
        $mform->addElement('header', 'endorsement', get_string('issuerdetails', 'badges'));
43
        $mform->addElement('text', 'issuername', get_string('issuername_endorsement', 'badges'), ['size' => '70']);
44
        $mform->setType('issuername', PARAM_TEXT);
45
        $mform->addRule('issuername', null, 'required');
46
        $mform->addHelpButton('issuername', 'issuername_endorsement', 'badges');
47
        $mform->addElement('text', 'issueremail', get_string('issueremail', 'badges'), ['size' => '70']);
48
        $mform->addRule('issueremail', null, 'required');
49
        $mform->setType('issueremail', PARAM_RAW);
50
        $mform->addHelpButton('issueremail', 'issueremail', 'badges');
51
        $mform->addElement('text', 'issuerurl', get_string('issuerurl', 'badges'), ['size' => '70']);
52
        $mform->setType('issuerurl', PARAM_URL);
53
        $mform->addRule('issuerurl', null, 'required');
54
        $mform->addHelpButton('issuerurl', 'issuerurl', 'badges');
55
        $mform->addElement('date_time_selector', 'dateissued',
56
                get_string('dateawarded', 'badges'));
57
        $mform->addElement('header', 'claim', get_string('claim', 'badges'));
58
        $mform->addElement('text', 'claimid', get_string('claimid', 'badges'), ['size' => '70']);
59
        $mform->setType('claimid', PARAM_URL);
60
        $mform->addRule('claimid', null, 'required');
61
        $mform->addElement('textarea', 'claimcomment', get_string('claimcomment', 'badges'), 'wrap="virtual" rows="8" cols="70"');
62
        $mform->setType('claimcomment', PARAM_NOTAGS);
63
        $endorsement = new stdClass();
64
        $endorsement = $badge->get_endorsement();
65
        if ($endorsement) {
66
            $mform->setDefault('dateissued', $endorsement->dateissued);
67
            $this->set_data($endorsement);
68
        }
69
        $this->add_action_buttons();
70
        // Freeze all elements if badge is active or locked.
71
        if ($badge->is_active() || $badge->is_locked()) {
72
            $mform->hardFreezeAllVisibleExcept([]);
73
        }
74
    }
75
 
76
    /**
77
     * Validates form data.
78
     *
79
     * @param array $data submitted data.
80
     * @param array $files submitted files.
81
     * @return array $errors An array of errors.
82
     */
83
    public function validation($data, $files) {
84
        $errors = parent::validation($data, $files);
85
        if ($data['issueremail'] && !validate_email($data['issueremail'])) {
86
            $errors['issueremail'] = get_string('invalidemail');
87
        }
88
        if ($data['issuerurl'] && !preg_match('@^https?://.+@', $data['issuerurl'])) {
89
            $errors['issuerurl'] = get_string('invalidurl', 'badges');
90
        }
91
        if ($data['claimid'] && !preg_match('@^https?://.+@', $data['claimid'])) {
92
            $errors['claimid'] = get_string('invalidurl', 'badges');
93
        }
94
        return $errors;
95
    }
96
}