weekly release 3.11.8+
[moodle.git] / mod / feedback / edit_form.php
blob583bc9e4c134b2694f2e20feb510abec6379e241
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 * prints the forms to choose an item-typ to create items and to choose a template to use
20 * @author Andreas Grabs
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package mod_feedback
25 //It must be included from a Moodle page
26 if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.');
30 require_once($CFG->libdir.'/formslib.php');
32 class feedback_edit_use_template_form extends moodleform {
34 /**
35 * Form definition
37 public function definition() {
38 $mform =& $this->_form;
40 $course = $this->_customdata['course'];
42 $elementgroup = array();
43 //headline
44 $mform->addElement('header', 'using_templates', get_string('using_templates', 'feedback'));
45 // hidden elements
46 $mform->addElement('hidden', 'id');
47 $mform->setType('id', PARAM_INT);
49 // visible elements
50 $templates_options = array();
51 $owntemplates = feedback_get_template_list($course, 'own');
52 $publictemplates = feedback_get_template_list($course, 'public');
54 $options = array();
55 if ($owntemplates or $publictemplates) {
56 $options[''] = array('' => get_string('choosedots'));
58 if ($owntemplates) {
59 $courseoptions = array();
60 foreach ($owntemplates as $template) {
61 $courseoptions[$template->id] = format_string($template->name);
63 $options[get_string('course')] = $courseoptions;
66 if ($publictemplates) {
67 $publicoptions = array();
68 foreach ($publictemplates as $template) {
69 $publicoptions[$template->id] = format_string($template->name);
71 $options[get_string('public', 'feedback')] = $publicoptions;
74 $attributes = 'onChange="M.core_formchangechecker.set_form_submitted(); this.form.submit()"';
75 $elementgroup[] = $mform->createElement('selectgroups',
76 'templateid',
77 get_string('using_templates', 'feedback'),
78 $options,
79 $attributes);
81 $elementgroup[] = $mform->createElement('submit',
82 'use_template',
83 get_string('use_this_template', 'feedback'),
84 array('class' => 'hiddenifjs'));
86 $mform->addGroup($elementgroup, 'elementgroup', '', array(' '), false);
87 } else {
88 $mform->addElement('static', 'info', get_string('no_templates_available_yet', 'feedback'));
91 $this->set_data(array('id' => $this->_customdata['id']));
95 class feedback_edit_create_template_form extends moodleform {
97 /**
98 * Form definition
100 public function definition() {
101 $mform =& $this->_form;
103 // hidden elements
104 $mform->addElement('hidden', 'id');
105 $mform->setType('id', PARAM_INT);
106 $mform->addElement('hidden', 'do_show');
107 $mform->setType('do_show', PARAM_ALPHANUMEXT);
108 $mform->setConstant('do_show', 'templates');
110 //headline
111 $mform->addElement('header', 'creating_templates', get_string('creating_templates', 'feedback'));
113 // visible elements
114 $elementgroup = array();
116 $elementgroup[] = $mform->createElement('text',
117 'templatename',
118 get_string('name', 'feedback'),
119 array('size'=>'40', 'maxlength'=>'200'));
121 if (has_capability('mod/feedback:createpublictemplate', context_system::instance())) {
122 $elementgroup[] = $mform->createElement('checkbox',
123 'ispublic', '',
124 get_string('public', 'feedback'));
128 $mform->addGroup($elementgroup,
129 'elementgroup',
130 get_string('name', 'feedback'),
131 array(' '),
132 false);
134 // Buttons.
135 $mform->addElement('submit', 'create_template', get_string('save_as_new_template', 'feedback'));
137 $mform->setType('templatename', PARAM_TEXT);
139 $this->set_data(array('id' => $this->_customdata['id']));
143 * Form validation
145 * @param array $data array of ("fieldname"=>value) of submitted data
146 * @param array $files array of uploaded files "element_name"=>tmp_file_path
147 * @return array of "element_name"=>"error_description" if there are errors,
148 * or an empty array if everything is OK (true allowed for backwards compatibility too).
150 public function validation($data, $files) {
151 $errors = parent::validation($data, $files);
152 if (!isset($data['templatename']) || trim(strval($data['templatename'])) === '') {
153 $errors['elementgroup'] = get_string('name_required', 'feedback');
155 return $errors;