-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_form.php
209 lines (165 loc) · 9.32 KB
/
mod_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?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/>.
/**
* Version information
*
* @package mod_evoting
* @copyright 2016 Cyberlearn
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once ($CFG -> dirroot . '/course/moodleform_mod.php');
require_once($CFG->dirroot . '/mod/evoting/lib.php');
require_once($CFG->dirroot.'/mod/evoting/locallib.php');
class mod_evoting_mod_form extends moodleform_mod {
/**
* Defines forms elements
*/
public function definition() {
global $CFG, $DB, $PAGE, $USER, $COURSE;
$mform = &$this -> _form;
$PAGE->requires->jquery();
$PAGE->requires->js('/mod/evoting/js/google-jsapi.js');
$PAGE->requires->strings_for_js(array('noaddChoice', 'confirmDelete'), 'evoting');
$PAGE->requires->js('/mod/evoting/js/mod_form.js');
$PAGE->requires->js_init_call('M.mod_evoting.form_init', array(optional_param('update', 0, PARAM_INT)));
$mform -> addElement('hidden', 'idcreator', $USER -> id);
$mform -> setType('idcreator', PARAM_TEXT);
$mform -> addElement('hidden', 'idCourse', $COURSE -> id);
$mform -> setType('idCourse', PARAM_TEXT);
// Adding the "general" fieldset, where all the common settings are showed.
$mform -> addElement('header', 'general', get_string('general', 'form'));
// Adding the standard "name" field.
$mform -> addElement('text', 'name', get_string('evotingname', 'evoting'), array('size' => '64'));
if (!empty($CFG -> formatstringstriptags)) {
$mform -> setType('name', PARAM_TEXT);
} else {
$mform -> setType('name', PARAM_CLEAN);
}
// Poll name required
$mform -> addRule('name', null, 'required', null, 'client');
$mform -> addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
// Adding the standard "Description" field.
$this -> standard_intro_elements(false, get_string('questiondescription', 'evoting'));
// Adding "Question" section.
$mform -> addElement('header', 'evotingfieldset', get_string('question', 'evoting'));
// Only add one question
$repeatQuestionArray = array();
// If Add Question (Default => 1 question is open) if update, questions from database are open
if ($this -> _instance) {
$repeatQuestionNo = $DB -> count_records('evoting_questions', array('evotingid' => $this -> _instance));
} else {
$repeatQuestionNo = 1;
}
// Create the field Name Question with hidden id
$repeatQuestionArray[] = $mform -> createElement('html','<div class="container_question">');
$repeatQuestionArray[]= $mform -> createElement('editor', 'questionname', get_string('questionno', 'evoting'), 'class="evotingQuestionTextarea"', evoting_get_editor_options($this->context));
$repeatQuestionArray[] = $mform -> createElement('hidden', 'questionnameid', 0);
$mform -> setType('questionnameid', PARAM_TEXT);
$mform->setType('questionname', PARAM_RAW);
$repeatQuestionArray[] = $mform -> createElement('html','<div class="container_choices">');
$repeatQuestionArray[] = $mform->createElement('html', '<table>');
$repeatQuestionArray[] = $mform->createElement('html', '<thead>');
$repeatQuestionArray[] = $mform->createElement('html',
'<tr class="header_choice">
<th class="header_label c1">'.
get_string('choice', 'evoting').'
</th>
<th class="header_text c2">'.
get_string('text', 'evoting').'
</th>
<th class="header_correct c3">'.
get_string('selectanswer', 'evoting').'
</th>
</tr>');
$repeatQuestionArray[] = $mform->createElement('html', '</thead>');
$repeatQuestionArray[] = $mform->createElement('html', '<tbody>');
// Create and set type for the field OptionName and OptionID
$maxChoice = get_config('evoting', 'evoting_number_choice');
$mform -> addElement('hidden', 'max_choice', $maxChoice);
$mform -> setType('max_choice', PARAM_INT);
for ($i = 0; $i < $maxChoice; $i++) {
$repeatQuestionArray[] = $mform->createElement('html', '<tr>');
$repeatQuestionArray[] = $mform->createElement('html', '<td class="c1"></td>');
$repeatQuestionArray[] = $mform->createElement('html', '<td class="c2">');
$repeatQuestionArray[] = $mform->createElement('text',
'option' . $i, $i + 1, 'class="evotingQuestionText" maxlength="70" ');
$repeatQuestionArray[] = $mform->createElement('hidden', 'optionid' . $i, 0);
$repeatQuestionArray[] = $mform->createElement('html', '</td>');
$repeatQuestionArray[] = $mform->createElement('html', '<td class="c3">');
$repeatQuestionArray[] = $mform->createElement('advcheckbox',
'right' . $i, '', '', array('group' => 1), array(0, 1));
$repeatQuestionArray[] = $mform->createElement('html', '</td>');
$repeatQuestionArray[] = $mform->createElement('html', '</tr>');
$mform -> setType('option' . $i, PARAM_TEXT);
$mform -> setType('optionid' . $i, PARAM_INT);
}
$repeatQuestionArray[] = $mform->createElement('html', '</tbody>');
$repeatQuestionArray[] = $mform->createElement('html', '</table>');
// Add choice question button
$repeatQuestionArray[] = $mform -> createElement('html','<div class="container_add_choice">');
$repeatQuestionArray[] = $mform -> createElement('link', 'addChoice', get_string('addChoice', 'evoting'), '#', get_string('addChoice', 'evoting'), 'class="addChoice"');
$repeatQuestionArray[] = $mform -> createElement('html','</div>');
$repeatQuestionArray[] = $mform -> createElement('html','</div>');
// Add delete question button
$repeatQuestionArray[] = $mform -> createElement('html','<div class="container_delete_question">');
$repeatQuestionArray[] = $mform -> createElement('link', 'deleteQuestion', get_string('deletequestion', 'evoting'), '#', get_string('deletequestion', 'evoting'), 'class="deleteButton"');
$repeatQuestionArray[] = $mform -> createElement('html','</div>');
// Add separator between questions
$repeatQuestionArray[] = $mform -> createElement('html','</div>');
// Array for Add question
$repeatelquestions = array();
// Make questions required and the first two options required
$repeatelquestions['questionname']['rule'] = 'required';
$repeatelquestions['option0']['rule'] = 'required';
$repeatelquestions['option1']['rule'] = 'required';
// Repeat question button
$this -> repeat_elements($repeatQuestionArray, $repeatQuestionNo, $repeatelquestions, 'question_repeats', 'question_add_fields', 1, get_string('addquestion', 'evoting'), false);
// Add standard elements, common to all modules.
$this -> standard_coursemodule_elements();
// Add standard buttons, common to all modules.
$this -> add_action_buttons();
}
/**
* Preprocess form data
* @param $default_values
*/
function data_preprocessing(&$default_values) {
global $DB, $USER, $CFG;
if (!empty($this -> _instance) && ($questions = $DB -> get_records('evoting_questions', array('evotingid' => $this -> _instance), 'id', 'id, name'))) {
$default_values['idcreator'] = $USER -> id;
// Get questions Name and ID when Update
$questions = array_values($questions);
$countQuestion = count($questions);
for ($i = 0; $i < $countQuestion; $i++) {
$default_values['questionnameid[' . $i . ']'] = $questions[$i] -> id;
$draftitem = file_get_submitted_draft_itemid('questioneditor' . $i);
$default_values['questionname[' . $i . ']']['format'] = 1;
$default_values['questionname[' . $i . ']']['text'] = file_prepare_draft_area($draftitem, $this->context->id, 'mod_evoting', 'questioneditor', $questions[$i] -> id, evoting_get_editor_options($this->context),$questions[$i] -> name);
$default_values['questionname[' . $i . ']']['itemid'] = $draftitem;
// Get Option ID and Text when Update
$options = $DB -> get_records('evoting_options', array('evotingquestionid' => $questions[$i] -> id), 'id', 'id,text,correct');
$options = array_values($options);
$countOptions = count($options);
for ($j = 0; $j < $countOptions; $j++) {
$default_values['optionid' . ($j) . '[' . $i . ']'] = $options[$j] -> id;
$default_values['option' . ($j) . '[' . $i . ']'] = $options[$j] -> text;
$default_values['right' . ($j) . '[' . $i . ']'] = $options[$j] -> correct;
}
}
}
}
}