-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathquestionnaire.php
238 lines (199 loc) · 9.33 KB
/
questionnaire.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
<?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/>.
/**
* The first page to view the verbal feedback.
*
* @package mod_verbalfeedback
* @copyright 2020 Kevin Tippenhauer <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_verbalfeedback\model\submission;
use mod_verbalfeedback\model\submission_status;
use mod_verbalfeedback\output\questionnaire;
use mod_verbalfeedback\repository\instance_repository;
use mod_verbalfeedback\repository\submission_repository;
require_once(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/weblib.php');
require_once($CFG->libdir . '/editorlib.php');
// The verbal feedback record id.
$id = required_param('instance', PARAM_INT);
$submissionid = optional_param('submission', 0, PARAM_INT);
$preview = optional_param('preview', false, PARAM_BOOL);
if ($submissionid == 0 && !$preview) {
\core\notification::error('If preview is not set, param \'submissionid\' is required.');
}
list ($course, $cm) = get_course_and_cm_from_instance($id, 'verbalfeedback');
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
$instancerepository = new instance_repository();
$submissionrepository = new submission_repository();
$submission = null;
$instance = null;
if ($preview) {
if (!$instance = $instancerepository->get_by_id($cm->instance)) {
throw new moodle_exception('errorverbalfeedbacknotfound', 'mod_verbalfeedback', $viewurl);
}
} else {
$submission = $submissionrepository->get_by_id($submissionid);
$instance = $instancerepository->get_by_id($submission->get_instance_id());
}
$PAGE->set_context($context);
$PAGE->set_cm($cm, $course);
$PAGE->set_pagelayout('incourse');
$PAGE->set_url('/mod/verbalfeedback/view.php', ['id' => $cm->id]);
$PAGE->set_heading($course->fullname);
$title = format_string($instance->get_name());
$PAGE->set_title($title);
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($title));
if ($preview) {
$edititemsurl = new moodle_url('/mod/verbalfeedback/edit_instance.php', ['id' => $cm->id]);
$previewinfo = get_string('previewinfo', 'verbalfeedback', $edititemsurl->out());
\core\notification::info($previewinfo);
$userheading = [
'heading' => 'Max Muster',
'user' => null,
'usercontext' => null,
];
$contextheader = $OUTPUT->context_header($userheading, 3);
$container = html_writer::div($contextheader, 'card-body');
echo html_writer::div($container, 'card');
$submission = new submission();
$submission->set_status(submission_status::IN_PROGRESS);
$submission->set_instance_id($instance->get_id());
$submission->set_from_user_id(0); // A non-existing user.
$questionnairedata = new questionnaire($context->id, $submission, true);
// Iterate and drop criteria with weight 0.
// First, let's filter our set of criteria inside the categories.
foreach ($questionnairedata->categories as $category) {
$filteredcriteria = array_filter($category->criteria, function($criterion) {
// Adding our criteria for a valid category.
return
property_exists($criterion, 'weight') // The property weight exists.
&& $criterion->weight != "0.00"; // And it's not '0.00'.
});
// If you want to keep the index of the entry just remove the next line.
$filteredcriteria = array_values($filteredcriteria);
// Overwrite the original criteria with the filtered set.
$category->criteria = $filteredcriteria;
}
// Iterate and drop categories with weight 0.
// Then, let's filter our set of categories.
$filteredcategories = array_filter($questionnairedata->categories, function($category) {
// Adding our criteria for a valid category.
return
property_exists($category, 'weight') // The property weight exists.
&& $category->weight != "0.00"; // And it's not '0.00'.
});
// If you want to keep the index of the entry just remove the next line.
$filteredcategories = array_values($filteredcategories);
// Overwrite the original categories with the filtered set.
$questionnairedata->categories = $filteredcategories;
$renderer = $PAGE->get_renderer('mod_verbalfeedback');
echo $renderer->render($questionnairedata);
add_editors_for_comments($questionnairedata, $context);
} else {
// Check if instance is already open.
$openmessage = $instance->is_open(true);
$isready = $instance->is_ready();
if ($isready && $openmessage === true) {
// Render user heading.
if ($submission->get_to_user_id() > 0) {
$touser = core_user::get_user($submission->get_to_user_id());
$userheading = [
'heading' => fullname($touser),
'user' => $touser,
'usercontext' => context_user::instance($submission->get_to_user_id()),
];
$contextheader = $OUTPUT->context_header($userheading, 3);
$container = html_writer::div($contextheader, 'card-body');
echo html_writer::div($container, 'card');
}
// Set status to in progress if pending.
if ($submission->get_status() == submission_status::PENDING) {
$submission->set_status(submission_status::PENDING);
$submissionrepository->save($submission);
}
// Verbalfeedback question list.
$questionnairedata = new questionnaire($context->id, $submission);
// Iterate and drop criteria with weight 0.
// First, let's filter our set of criteria inside the categories.
foreach ($questionnairedata->categories as $category) {
$filteredcriteria = array_filter($category->criteria, function($criterion) {
// Adding our criteria for a valid category.
return
property_exists($criterion, 'weight') // The property weight exists.
&& $criterion->weight != "0.00"; // And it's not '0.00'.
});
// If you want to keep the index of the entry just remove the next line.
$filteredcriteria = array_values($filteredcriteria);
// Overwrite the original criteria with the filtered set.
$category->criteria = $filteredcriteria;
}
// Iterate and drop categories with weight 0.
// Then, let's filter our set of categories.
$filteredcategories = array_filter($questionnairedata->categories, function($category) {
// Adding our criteria for a valid category.
return
property_exists($category, 'weight') // The property weight exists.
&& $category->weight != "0.00"; // And it's not '0.00'.
});
// If you want to keep the index of the entry just remove the next line.
$filteredcategories = array_values($filteredcategories);
// Overwrite the original categories with the filtered set.
$questionnairedata->categories = $filteredcategories;
$renderer = $PAGE->get_renderer('mod_verbalfeedback');
echo $renderer->render($questionnairedata);
add_editors_for_comments($questionnairedata, $context);
} else {
if ($isready) {
$message = get_string('instancenotready', 'mod_verbalfeedback');
} else {
$message = $openmessage;
}
\core\notification::error($message);
$viewurl = new moodle_url('/mod/verbalfeedback/view.php', ['id' => $cm->id]);
echo html_writer::link($viewurl, get_string('backtoverbalfeedbackdashboard', 'mod_verbalfeedback'));
}
}
echo $OUTPUT->footer();
/**
* Adds an atto editor field for comments
*
* @param array $questionnairedata The questionnaire data
* @param object $context The context
*/
function add_editors_for_comments($questionnairedata, $context) {
$fromuserid = "";
$touserid = "";
if (isset($questionnairedata->fromuserid)) {
$fromuserid = $questionnairedata->fromuserid;
}
if (isset($questionnairedata->touserid)) {
$touserid = $questionnairedata->touserid;
}
foreach ($questionnairedata->categories as $category) {
foreach ($category->criteria as $criteria) {
$criteriaid = $criteria->id;
$studentcommentelementid = "student-comment-{$criteriaid}-{$fromuserid}-{$touserid}";
$editor = editors_get_preferred_editor();
$editor->use_editor($studentcommentelementid, ['context' => $context, 'autosave' => false]);
$privatecommentelementid = "private-comment-{$criteriaid}-{$fromuserid}-{$touserid}";
$editor = editors_get_preferred_editor();
$editor->use_editor($privatecommentelementid, ['context' => $context, 'autosave' => false]);
}
}
}