1 |
efrain |
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 for editing steps.
|
|
|
19 |
*
|
|
|
20 |
* @package tool_usertours
|
|
|
21 |
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace tool_usertours\local\forms;
|
|
|
26 |
|
|
|
27 |
use stdClass;
|
|
|
28 |
use tool_usertours\helper;
|
|
|
29 |
use tool_usertours\step;
|
|
|
30 |
|
|
|
31 |
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
|
|
|
32 |
|
|
|
33 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Form for editing steps.
|
|
|
37 |
*
|
|
|
38 |
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class editstep extends \moodleform {
|
|
|
42 |
/**
|
|
|
43 |
* @var tool_usertours\step $step
|
|
|
44 |
*/
|
|
|
45 |
protected $step;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* @var int Display the step's content by using Moodle language string.
|
|
|
49 |
*/
|
|
|
50 |
private const CONTENTTYPE_LANGSTRING = 0;
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* @var int Display the step's content by entering it manually.
|
|
|
54 |
*/
|
|
|
55 |
private const CONTENTTYPE_MANUAL = 1;
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Create the edit step form.
|
|
|
59 |
*
|
|
|
60 |
* @param string $target The target of the form.
|
|
|
61 |
* @param step $step The step being editted.
|
|
|
62 |
*/
|
|
|
63 |
public function __construct($target, \tool_usertours\step $step) {
|
|
|
64 |
$this->step = $step;
|
|
|
65 |
|
|
|
66 |
parent::__construct($target);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Form definition.
|
|
|
71 |
*/
|
|
|
72 |
public function definition() {
|
|
|
73 |
global $CFG;
|
|
|
74 |
|
|
|
75 |
$mform = $this->_form;
|
|
|
76 |
|
|
|
77 |
$mform->addElement('header', 'heading_target', get_string('target_heading', 'tool_usertours'));
|
|
|
78 |
$types = [];
|
|
|
79 |
foreach (\tool_usertours\target::get_target_types() as $value => $type) {
|
|
|
80 |
$types[$value] = get_string('target_' . $type, 'tool_usertours');
|
|
|
81 |
}
|
|
|
82 |
$mform->addElement('select', 'targettype', get_string('targettype', 'tool_usertours'), $types);
|
|
|
83 |
$mform->addHelpButton('targettype', 'targettype', 'tool_usertours');
|
|
|
84 |
|
|
|
85 |
// The target configuration.
|
|
|
86 |
foreach (\tool_usertours\target::get_target_types() as $value => $type) {
|
|
|
87 |
$targetclass = \tool_usertours\target::get_classname($type);
|
|
|
88 |
$targetclass::add_config_to_form($mform);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// Content of the step.
|
|
|
92 |
$mform->addElement('header', 'heading_content', get_string('content_heading', 'tool_usertours'));
|
|
|
93 |
$mform->addElement('textarea', 'title', get_string('title', 'tool_usertours'));
|
|
|
94 |
$mform->addRule('title', get_string('required'), 'required', null, 'client');
|
|
|
95 |
$mform->setType('title', PARAM_TEXT);
|
|
|
96 |
$mform->addHelpButton('title', 'title', 'tool_usertours');
|
|
|
97 |
|
|
|
98 |
// Content type.
|
|
|
99 |
$typeoptions = [
|
|
|
100 |
static::CONTENTTYPE_LANGSTRING => get_string('content_type_langstring', 'tool_usertours'),
|
|
|
101 |
static::CONTENTTYPE_MANUAL => get_string('content_type_manual', 'tool_usertours'),
|
|
|
102 |
];
|
|
|
103 |
$mform->addElement('select', 'contenttype', get_string('content_type', 'tool_usertours'), $typeoptions);
|
|
|
104 |
$mform->addHelpButton('contenttype', 'content_type', 'tool_usertours');
|
|
|
105 |
$mform->setDefault('contenttype', static::CONTENTTYPE_MANUAL);
|
|
|
106 |
|
|
|
107 |
// Language identifier.
|
|
|
108 |
$mform->addElement('textarea', 'contentlangstring', get_string('moodle_language_identifier', 'tool_usertours'));
|
|
|
109 |
$mform->setType('contentlangstring', PARAM_TEXT);
|
|
|
110 |
$mform->hideIf('contentlangstring', 'contenttype', 'eq', static::CONTENTTYPE_MANUAL);
|
|
|
111 |
|
|
|
112 |
$editoroptions = [
|
|
|
113 |
'subdirs' => 1,
|
|
|
114 |
'maxbytes' => $CFG->maxbytes,
|
|
|
115 |
'maxfiles' => EDITOR_UNLIMITED_FILES,
|
|
|
116 |
'changeformat' => 1,
|
|
|
117 |
'trusttext' => true,
|
|
|
118 |
];
|
11 |
efrain |
119 |
$mform->addElement('editor', 'content', get_string('content', 'tool_usertours'), null, $editoroptions);
|
|
|
120 |
$mform->addHelpButton('content', 'content', 'tool_usertours');
|
|
|
121 |
$mform->hideIf('content', 'contenttype', 'eq', static::CONTENTTYPE_LANGSTRING);
|
1 |
efrain |
122 |
|
|
|
123 |
// Add the step configuration.
|
|
|
124 |
$mform->addElement('header', 'heading_options', get_string('options_heading', 'tool_usertours'));
|
|
|
125 |
// All step configuration is defined in the step.
|
|
|
126 |
$this->step->add_config_to_form($mform);
|
|
|
127 |
|
|
|
128 |
// And apply any form constraints.
|
|
|
129 |
foreach (\tool_usertours\target::get_target_types() as $value => $type) {
|
|
|
130 |
$targetclass = \tool_usertours\target::get_classname($type);
|
|
|
131 |
$targetclass::add_disabled_constraints_to_form($mform);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
$this->add_action_buttons();
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Validate the database on the submitted content type.
|
|
|
139 |
*
|
|
|
140 |
* @param array $data array of ("fieldname"=>value) of submitted data
|
|
|
141 |
* @param array $files array of uploaded files "element_name"=>tmp_file_path
|
|
|
142 |
* @return array of "element_name"=>"error_description" if there are errors,
|
|
|
143 |
* or an empty array if everything is OK (true allowed for backwards compatibility too).
|
|
|
144 |
*/
|
|
|
145 |
public function validation($data, $files): array {
|
|
|
146 |
$errors = parent::validation($data, $files);
|
|
|
147 |
|
|
|
148 |
if ($data['contenttype'] == static::CONTENTTYPE_LANGSTRING) {
|
|
|
149 |
if (!isset($data['contentlangstring']) || trim($data['contentlangstring']) == '') {
|
|
|
150 |
$errors['contentlangstring'] = get_string('required');
|
|
|
151 |
} else {
|
|
|
152 |
$splitted = explode(',', trim($data['contentlangstring']), 2);
|
|
|
153 |
$langid = $splitted[0];
|
|
|
154 |
$langcomponent = $splitted[1];
|
|
|
155 |
if (!get_string_manager()->string_exists($langid, $langcomponent)) {
|
|
|
156 |
$errors['contentlangstring'] = get_string('invalid_lang_id', 'tool_usertours');
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
// Validate manually entered text content. Validation logic derived from \MoodleQuickForm_Rule_Required::validate()
|
|
|
162 |
// without the checking of the "strictformsrequired" admin setting.
|
|
|
163 |
if ($data['contenttype'] == static::CONTENTTYPE_MANUAL) {
|
|
|
164 |
$value = $data['content']['text'] ?? '';
|
|
|
165 |
|
|
|
166 |
// All tags except img, canvas and hr, plus all forms of whitespaces.
|
|
|
167 |
$stripvalues = [
|
|
|
168 |
'#</?(?!img|canvas|hr).*?>#im',
|
|
|
169 |
'#(\xc2\xa0|\s| )#',
|
|
|
170 |
];
|
|
|
171 |
$value = preg_replace($stripvalues, '', (string)$value);
|
|
|
172 |
if (empty($value)) {
|
|
|
173 |
$errors['contenthtmlgrp'] = get_string('required');
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
return $errors;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Load in existing data as form defaults. Usually new entry defaults are stored directly in
|
|
|
182 |
* form definition (new entry form); this function is used to load in data where values
|
|
|
183 |
* already exist and data is being edited (edit entry form).
|
|
|
184 |
*
|
|
|
185 |
* @param stdClass|array $data object or array of default values
|
|
|
186 |
*/
|
|
|
187 |
public function set_data($data): void {
|
|
|
188 |
$data = (object) $data;
|
|
|
189 |
if (!isset($data->contenttype)) {
|
|
|
190 |
if (!empty($data->content['text']) && helper::is_language_string_from_input($data->content['text'])) {
|
|
|
191 |
$data->contenttype = static::CONTENTTYPE_LANGSTRING;
|
|
|
192 |
$data->contentlangstring = $data->content['text'];
|
|
|
193 |
|
|
|
194 |
// Empty the editor content.
|
|
|
195 |
$data->content = ['text' => ''];
|
|
|
196 |
} else {
|
|
|
197 |
$data->contenttype = static::CONTENTTYPE_MANUAL;
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
parent::set_data($data);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Return submitted data if properly submitted or returns NULL if validation fails or
|
|
|
205 |
* if there is no submitted data.
|
|
|
206 |
*
|
|
|
207 |
* @return object|null submitted data; NULL if not valid or not submitted or cancelled
|
|
|
208 |
*/
|
|
|
209 |
public function get_data(): ?object {
|
|
|
210 |
$data = parent::get_data();
|
|
|
211 |
if ($data) {
|
|
|
212 |
if ($data->contenttype == static::CONTENTTYPE_LANGSTRING) {
|
|
|
213 |
$data->content = [
|
|
|
214 |
'text' => $data->contentlangstring,
|
|
|
215 |
'format' => FORMAT_MOODLE,
|
|
|
216 |
];
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
return $data;
|
|
|
220 |
}
|
|
|
221 |
}
|