-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_harakat_form.php
169 lines (151 loc) · 6.39 KB
/
edit_harakat_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines the editing form for the harakat question type.
*
* @package qtype
* @subpackage harakat
* @copyright 2023 Meirza <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Short answer question editing form definition.
*
* @copyright 2023 Meirza <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_harakat_edit_form extends question_edit_form {
/**
* Get the list of form elements to repeat, one for each answer.
* @param object $mform the form being built.
* @param $label the label to use for each option.
* @param $gradeoptions the possible grades for each answer.
* @param $repeatedoptions reference to array of repeated options to fill
* @param $answersoption reference to return the name of $question->options
* field holding an array of answers
* @return array of form fields.
*/
protected function get_per_answer_fields($mform, $label, $gradeoptions,
&$repeatedoptions, &$answersoption) {
$repeated = array();
$answeroptions = array();
$answeroptions[] = $mform->createElement(
'textarea', 'answer', $label,
array('cols' => 60, 'rows' => 3, 'class' => 'form-control form-harakat-answer')
);
$answeroptions[] = $mform->createElement('hidden', 'fraction', '1');
$mform->setType('fraction', PARAM_RAW);
$repeated[] = $mform->createElement('group', 'answeroptions',
$label, $answeroptions, null, false);
$repeated[] = $mform->createElement('editor', 'feedback',
get_string('feedback', 'question'), array('rows' => 5), $this->editoroptions);
$repeatedoptions['answer']['type'] = PARAM_RAW;
$repeatedoptions['fraction']['default'] = 1;
$answersoption = 'answers';
return $repeated;
}
/**
* Question-type specific form fields.
*
* @param object $mform the form being built.
*/
protected function definition_inner($mform) {
$mform->addElement('static', 'answersinstruct', '',
get_string('filloutoneanswer', 'qtype_harakat'));
$mform->closeHeaderBefore('answersinstruct');
$this->add_per_answer_fields($mform, get_string('answerno', 'qtype_harakat', ''),
question_bank::fraction_options(), 1, 0);
$this->add_interactive_settings();
}
/**
* Add a set of form fields, obtained from get_per_answer_fields, to the form,
* one for each existing answer, with some blanks for some new ones.
* @param object $mform the form being built.
* @param $label the label to use for each option.
* @param $gradeoptions the possible grades for each answer.
* @param $minoptions the minimum number of answer blanks to display.
* Default QUESTION_NUMANS_START.
* @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
*/
protected function add_per_answer_fields(&$mform, $label, $gradeoptions,
$minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
$mform->addElement('header', 'answerhdr',
get_string('answers', 'qtype_harakat'), '');
$mform->setExpanded('answerhdr', 1);
$answersoption = '';
$repeatedoptions = array();
$repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions,
$repeatedoptions, $answersoption);
if (isset($this->question->options)) {
$repeatsatstart = count($this->question->options->$answersoption);
} else {
$repeatsatstart = $minoptions;
}
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
'noanswers', 'addanswers', $addoptions,
$this->get_more_choices_string(), true);
}
// We only need an answer.
protected function get_more_choices_string() {
return '';
}
/**
* Perform an preprocessing needed on the data passed to {@link set_data()}
* before it is used to initialise the form.
* @param object $question the data being passed to the form.
* @return object $question the modified data.
*/
protected function data_preprocessing($question) {
if (isset($question->options)) {
// Cleanup the answer from non-arabic script and diacritics.
$answer = reset($question->options->answers);
$cleanupanswer = preg_replace('/[^\x{0600}-\x{06FF} !@#$%^&*()]/u', '', $answer->answer);
$answer->answer = $cleanupanswer;
}
$question = parent::data_preprocessing($question);
$question = $this->data_preprocessing_answers($question);
$question = $this->data_preprocessing_hints($question);
return $question;
}
/**
* Validation input
*
* @return string|null validation feedback.
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
$answer = reset($data['answer']);
$trimmedanswer = trim($answer);
// Raise an error if the answer is not Arabic script and diacritics.
$isarabic = preg_match('/\p{Arabic}/u', $trimmedanswer);
if ($trimmedanswer !== '') {
if (!$isarabic) {
$errors['answeroptions[0]'] = get_string('notarabicanswers', 'qtype_harakat', 1);
}
} else {
$errors['answeroptions[0]'] = get_string('notenoughanswers', 'qtype_harakat', 1);
}
return $errors;
}
/**
* Question-type
*
* @return string question type name.
*/
public function qtype(): string {
return 'harakat';
}
}