source: moodle/trunk/fuentes/badges/criteria_form.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 3.9 KB
Line 
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 * Form classes for editing badges criteria
19 *
20 * @package    core
21 * @subpackage badges
22 * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->libdir . '/formslib.php');
30require_once($CFG->libdir . '/badgeslib.php');
31
32/**
33 * Form to edit badge criteria.
34 *
35 */
36class edit_criteria_form extends moodleform {
37    public function definition() {
38        global $DB;
39        $mform = $this->_form;
40        $criteria = $this->_customdata['criteria'];
41        $addcourse = $this->_customdata['addcourse'];
42        $course = $this->_customdata['course'];
43
44        // Get course selector first if it's a new courseset criteria.
45        if (($criteria->id == 0 || $addcourse) && $criteria->criteriatype == BADGE_CRITERIA_TYPE_COURSESET) {
46            $criteria->get_courses($mform);
47        } else {
48            if ($criteria->id == 0 && $criteria->criteriatype == BADGE_CRITERIA_TYPE_COURSE) {
49                $mform->addElement('hidden', 'course', $course);
50                $mform->setType('course', PARAM_INT);
51            }
52            list($none, $message) = $criteria->get_options($mform);
53
54            if ($none) {
55                $mform->addElement('html', html_writer::tag('div', $message));
56                $mform->addElement('submit', 'cancel', get_string('continue'));
57            } else {
58                $mform->addElement('header', 'description_header', get_string('description'));
59                $mform->addElement('editor', 'description', '', null, null);
60                $mform->setType('description', PARAM_RAW);
61                $mform->setDefault('description', array(
62                        'text' => $criteria->description,
63                        'format' => $criteria->descriptionformat
64                    )
65                );
66
67                $mform->closeHeaderBefore('buttonar');
68                $this->add_action_buttons(true, get_string('save', 'badges'));
69            }
70        }
71    }
72
73    /**
74     * Validates form data
75     */
76    public function validation($data, $files) {
77        global $OUTPUT;
78        $errors = parent::validation($data, $files);
79        $addcourse = $this->_customdata['addcourse'];
80
81        if (!$addcourse && isset($this->_customdata['criteria']->required_param)) {
82            $required = $this->_customdata['criteria']->required_param;
83            $pattern1 = '/^' . $required . '_(\d+)$/';
84            $pattern2 = '/^' . $required . '_(\w+)$/';
85
86            $ok = false;
87            foreach ($data as $key => $value) {
88                if ((preg_match($pattern1, $key) || preg_match($pattern2, $key)) && !($value === 0 || $value == '0')) {
89                    $ok = true;
90                }
91            }
92
93            $warning = $this->_form->createElement('html',
94                    $OUTPUT->notification(get_string('error:parameter', 'badges'), 'notifyproblem'), 'submissionerror');
95
96            if (!$ok) {
97                $errors['formerrors'] = 'Error';
98                $this->_form->insertElementBefore($warning, 'first_header');
99            }
100        }
101        return $errors;
102    }
103}
Note: See TracBrowser for help on using the repository browser.