MDL-35073 badges: fix xmldb editor diff
[moodle.git] / badges / criteria_form.php
blob65cc718e7cfb00a941895a16ff2a0ce6d2793685
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.
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/>.
17 /**
18 * Form classes for editing badges criteria
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>
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/formslib.php');
30 require_once($CFG->libdir . '/badgeslib.php');
32 /**
33 * Form to edit badge criteria.
36 class 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'];
43 // Get course selector first if it's a new courseset criteria.
44 if (($criteria->id == 0 || $addcourse) && $criteria->criteriatype == BADGE_CRITERIA_TYPE_COURSESET) {
45 $criteria->get_courses($mform);
46 } else {
47 list($none, $message) = $criteria->get_options($mform);
49 if ($none) {
50 $mform->addElement('html', html_writer::tag('div', $message));
51 $mform->addElement('submit', 'cancel', get_string('continue'));
52 } else {
53 $mform->closeHeaderBefore('buttonar');
54 $this->add_action_buttons(true, get_string('save', 'badges'));
59 /**
60 * Validates form data
62 public function validation($data, $files) {
63 global $OUTPUT;
64 $errors = parent::validation($data, $files);
65 $addcourse = $this->_customdata['addcourse'];
67 if (!$addcourse) {
68 $required = $this->_customdata['criteria']->required_param;
69 $pattern1 = '/^' . $required . '_(\d+)$/';
70 $pattern2 = '/^' . $required . '_(\w+)$/';
72 $ok = false;
73 foreach ($data as $key => $value) {
74 if ((preg_match($pattern1, $key) || preg_match($pattern2, $key)) && !($value === 0 || $value == '0')) {
75 $ok = true;
79 $warning = $this->_form->createElement('html',
80 $OUTPUT->notification(get_string('error:parameter', 'badges'), 'notifyproblem'), 'submissionerror');
82 if (!$ok) {
83 $errors['formerrors'] = 'Error';
84 $this->_form->insertElementBefore($warning, 'first_header');
87 return $errors;