forked from PoetOS/moodle-mod_questionnaire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbsections.php
262 lines (243 loc) · 10.5 KB
/
fbsections.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?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/>.
require_once("../../config.php");
require_once($CFG->dirroot.'/mod/questionnaire/questionnaire.class.php');
$id = optional_param('id', 0, PARAM_INT);
$sid = optional_param('sid', 0, PARAM_INT);
if ($id) {
if (! $cm = get_coursemodule_from_id('questionnaire', $id)) {
print_error('invalidcoursemodule');
}
if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
print_error('coursemisconf');
}
if (! $questionnaire = $DB->get_record("questionnaire", array("id" => $cm->instance))) {
print_error('invalidcoursemodule');
}
}
// Check login and get context.
require_login($course->id, false, $cm);
$context = $cm ? context_module::instance($cm->id) : false;
$url = new moodle_url('/mod/questionnaire/fbsections.php');
if ($id !== 0) {
$url->param('id', $id);
}
if ($sid) {
$url->param('sid', $sid);
}
$questionnaire = new questionnaire(0, $questionnaire, $course, $cm);
$questions = $questionnaire->questions;
$sid = $questionnaire->survey->id;
$viewform = data_submitted($CFG->wwwroot."/mod/questionnaire/fbsections.php");
$feedbacksections = $questionnaire->survey->feedbacksections;
$errormsg = '';
// Check if there are any feedbacks stored in database already to use them to check
// the radio buttons on select questions in sections page.
if ($fbsections = $DB->get_records('questionnaire_fb_sections',
array('survey_id' => $sid)) ) {
$scorecalculation = '';
$questionsinsections = array();
for ($section = 1; $section <= $feedbacksections; $section++) {
// Retrieve the scorecalculation formula and the section heading only once.
foreach ($fbsections as $fbsection) {
if (isset($fbsection->scorecalculation) && $fbsection->section == $section) {
$scorecalculation = unserialize($fbsection->scorecalculation);
foreach ($scorecalculation as $qid => $key) {
$questionsinsections[$qid] = $section;
}
break;
}
}
}
// If Global Feedback (only 1 section) and no questions have yet been put in section 1 check all questions.
if (!empty($questionsinsections)) {
$vf = $questionsinsections;
}
}
if (data_submitted()) {
$vf = (array)$viewform;
if (isset($vf['savesettings'])) {
$action = 'savesettings';
unset($vf['savesettings']);
}
$scorecalculation = array();
$submittedvf = array();
foreach ($vf as $qs) {
$sectionqid = explode("_", $qs);
if ($sectionqid[0] != 0) {
$scorecalculation[$sectionqid[0]][$sectionqid[1]] = null;
$submittedvf[$sectionqid[1]] = $sectionqid[0];
}
}
$c = count($scorecalculation);
if ($c < $feedbacksections) {
$sectionsnotset = '';
for ($section = 1; $section <= $feedbacksections; $section++) {
if (!isset($scorecalculation[$section])) {
$sectionsnotset .= $section.' ';
}
}
$errormsg = get_string('sectionsnotset', 'questionnaire', $sectionsnotset);
$vf = $submittedvf;
} else {
for ($section = 1; $section <= $feedbacksections; $section++) {
$fbcalculation[$section] = serialize($scorecalculation[$section]);
}
$sections = $DB->get_records('questionnaire_fb_sections',
array('survey_id' => $questionnaire->survey->id), 'section DESC');
// Delete former feedbacks if number of feedbacksections has been reduced.
foreach ($sections as $section) {
if ($section->section > $feedbacksections) {
// Delete section record.
$DB->delete_records('questionnaire_fb_sections', array('survey_id' => $sid, 'section' => $section->section));
// Delete associated feedback records.
$DB->delete_records('questionnaire_feedback', array('section_id' => $section->section));
}
}
// Check if the number of feedback sections has been increased and insert new ones
// must also insert section heading!
for ($section = 1; $section <= $feedbacksections; $section++) {
if ($existsection = $DB->get_record('questionnaire_fb_sections',
array('survey_id' => $sid, 'section' => $section), '*', IGNORE_MULTIPLE) ) {
$DB->set_field('questionnaire_fb_sections', 'scorecalculation', serialize($scorecalculation[$section]),
array('survey_id' => $sid, 'section' => $section));
} else {
$feedbacksection = new stdClass();
$feedbacksection->survey_id = $sid;
$feedbacksection->section = $section;
$feedbacksection->scorecalculation = serialize($scorecalculation[$section]);
$feedbacksection->id = $DB->insert_record('questionnaire_fb_sections', $feedbacksection);
}
}
$currentsection = 1;
$SESSION->questionnaire->currentfbsection = 1;
redirect ($CFG->wwwroot.'/mod/questionnaire/fbsettings.php?id='.
$questionnaire->cm->id.'¤tsection='.$currentsection, '', 0);
}
}
$PAGE->set_url($url);
// Print the page header.
$PAGE->set_title(get_string('feedbackeditingsections', 'questionnaire'));
$PAGE->set_heading(format_string($course->fullname));
$PAGE->navbar->add(get_string('feedbackeditingsections', 'questionnaire'));
echo $OUTPUT->header();
echo '<form id="fbsections" method="post">';
$feedbacksections = $questionnaire->survey->feedbacksections + 1;
if ($errormsg != '') {
questionnaire_notify($errormsg);
}
$n = 0;
$bg = 'c0';
echo $OUTPUT->box_start();
echo $OUTPUT->help_icon('feedbacksectionsselect', 'questionnaire');
echo '<b>Sections:</b><br /><br />';
$formdata = new stdClass();
$descendantsdata = array();
foreach ($questionnaire->questions as $question) {
$qtype = $question->type_id;
$qname = $question->name;
$qprecise = $question->precise;
$required = $question->required;
$qid = $question->id;
// Questions to be included in feedback sections must be required, have a name
// and must not be child of a parent question.
if ($qtype != QUESPAGEBREAK && $qtype != QUESSECTIONTEXT) {
$n++;
}
$cannotuse = false;
$strcannotuse = '';
if ($qtype != QUESSECTIONTEXT && $qtype != QUESPAGEBREAK
&& ($qtype != QUESYESNO && $qtype != QUESRADIO && $qtype != QUESRATE
|| $required != 'y' || $qname == '' || $question->dependquestion != 0)) {
$cannotuse = true;
$qn = '<strong>'.$n.'</strong>';
if ($qname == '') {
$strcannotuse = get_string('missingname', 'questionnaire', $qn);
}
if ($required != 'y') {
if ($qname == '') {
$strcannotuse = get_string('missingnameandrequired', 'questionnaire', $qn);
} else {
$strcannotuse = get_string('missingrequired', 'questionnaire', $qn);
}
}
if ($question->dependquestion != 0) {
continue;
}
}
$qhasvalues = false;
if (!$cannotuse) {
if ($qtype == QUESRADIO || $qtype == QUESDROP) {
if ($choices = $DB->get_records('questionnaire_quest_choice', array('question_id' => $qid = $question->id))) {
foreach ($choices as $choice) {
if ($choice->value != null) {
$qhasvalues = true;
break;
}
}
}
}
// Valid questions in feedback sections can be of QUESNO type
// or of QUESRATE "normal" option type (i.e. not N/A nor nodupes).
if ($qtype == QUESYESNO || ($qtype == QUESRATE && ($qprecise == 0 || $qprecise == 3)) ) {
$qhasvalues = true;
}
if ($qhasvalues) {
$emptyisglobalfeedback = $questionnaire->survey->feedbacksections == 1 && empty($questionsinsections);
echo '<div style="margin-bottom:5px;">['.$qname.']</div>';
for ($i = 0; $i < $feedbacksections; $i++) {
$output = '<div style="float:left; padding-right:5px;">';
if ($i != 0) {
$output .= '<div class="'.$bg.'"><input type="radio" name="'.$n.'" id="'.$qid.'_'.$i.'" value="'.$i.'_'.
$qid.'"';
} else {
$output .= '<div class="'.$bg.'"><input type="radio" name="'.$n.'" id="'.$i.'" value="'.$i.'"';
}
if ($i == 0) {
$output .= ' checked="checked"';
}
// Question already present in this section OR this is a Global feedback and questions are not set yet.
if ((isset($vf[$qid]) && $vf[$qid] == $i) || $emptyisglobalfeedback) {
$output .= ' checked="checked"';
}
$output .= ' />';
$output .= '<label for="'.$qid.'_'.$i.'">'.'<div style="padding-left: 2px;">'.$i.'</div>'.'</label></div></div>';
echo $output;
if ($bg == 'c0') {
$bg = 'c1';
} else {
$bg = 'c0';
}
}
}
if ($qhasvalues || $qtype == QUESSECTIONTEXT) {
$question->survey_display($formdata, $descendantsdata = '', $qnum = $n, $blankquestionnaire = true);
}
} else {
echo '<div class="notifyproblem">';
echo $strcannotuse;
echo '</div>';
echo '<div class="qn-question">'.$question->content.'</div>';
}
}
// Submit/Cancel buttons.
$url = $CFG->wwwroot.'/mod/questionnaire/view.php?id='.$cm->id;
echo '<div><input type="submit" name="savesettings" value="'.get_string('feedbackeditmessages', 'questionnaire').'" />
<a href="'.$url.'">'.get_string('cancel').'</a>
</div>';
echo '</form>';
echo $OUTPUT->box_end();
echo $OUTPUT->footer($course);