forked from ndunand/moodle-mod_choicegroup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.php
303 lines (253 loc) · 13 KB
/
view.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?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
* @subpackage choicegroup
* @copyright 2013 Université de Lausanne
* @author Nicolas Dunand <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("../../config.php");
require_once("lib.php");
require_once($CFG->dirroot.'/group/lib.php');
require_once($CFG->libdir . '/completionlib.php');
$id = required_param('id', PARAM_INT); // Course Module ID
$action = optional_param('action', '', PARAM_ALPHA);
$userids = optional_param_array('userid', array(), PARAM_INT); // array of attempt ids for delete action
$notify = optional_param('notify', '', PARAM_ALPHA);
$url = new moodle_url('/mod/choicegroup/view.php', array('id'=>$id));
if ($action !== '') {
$url->param('action', $action);
}
$PAGE->set_url($url);
if (! $cm = get_coursemodule_from_id('choicegroup', $id)) {
print_error('invalidcoursemodule');
}
if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
print_error('coursemisconf');
}
require_login($course, false, $cm);
$PAGE->requires->js_call_amd('mod_choicegroup/choicegroupdatadisplay', 'init');
if (!$choicegroup = choicegroup_get_choicegroup($cm->instance)) {
print_error('invalidcoursemodule');
}
$choicegroup_groups = choicegroup_get_groups($choicegroup);
$choicegroup_users = array();
$strchoicegroup = get_string('modulename', 'choicegroup');
$strchoicegroups = get_string('modulenameplural', 'choicegroup');
if (!$context = context_module::instance($cm->id)) {
print_error('badcontext');
}
$eventparams = array(
'context' => $context,
'objectid' => $choicegroup->id
);
$current = choicegroup_get_user_answer($choicegroup, $USER);
if ($action == 'delchoicegroup' and confirm_sesskey() and is_enrolled($context, null, 'mod/choicegroup:choose') and $choicegroup->allowupdate and !($choicegroup->timeclose and (time() > $choicegroup->timeclose))) {
// user wants to delete his own choice:
if ($current !== false) {
if (groups_is_member($current->id, $USER->id)) {
$currentgroup = $DB->get_record('groups', array('id' => $current->id), 'id,name', MUST_EXIST);
groups_remove_member($current->id, $USER->id);
$event = \mod_choicegroup\event\choice_removed::create($eventparams);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('choicegroup', $choicegroup);
$event->trigger();
}
$current = choicegroup_get_user_answer($choicegroup, $USER, false, true);
// Update completion state
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && $choicegroup->completionsubmit) {
$completion->update_state($cm, COMPLETION_INCOMPLETE);
}
}
}
$PAGE->set_title(format_string($choicegroup->name));
$PAGE->set_heading($course->fullname);
/// Mark as viewed
$completion=new completion_info($course);
$completion->set_module_viewed($cm);
/// Submit any new data if there is any
if (data_submitted() && is_enrolled($context, null, 'mod/choicegroup:choose') && confirm_sesskey()) {
if ($choicegroup->multipleenrollmentspossible == 1) {
$number_of_groups = optional_param('number_of_groups', '', PARAM_INT);
$enrollmentscount = 0;
if ($choicegroup->maxenrollments > 0) {
for ($i = 0; $i < $number_of_groups; $i++) {
$answer_value = optional_param('answer_' . $i, '', PARAM_INT);
if ($answer_value != '') {
$enrollmentscount++;
}
}
if ($enrollmentscount > $choicegroup->maxenrollments) {
redirect(new moodle_url('/mod/choicegroup/view.php',
array('id' => $cm->id, 'notify' => 'mustchoosemax', 'sesskey' => sesskey())));
}
}
for ($i = 0; $i < $number_of_groups; $i++) {
$answer_value = optional_param('answer_' . $i, '', PARAM_INT);
if ($answer_value != '') {
choicegroup_user_submit_response($answer_value, $choicegroup, $USER->id, $course, $cm);
} else {
$answer_value_group_id = optional_param('answer_'.$i.'_groupid', '', PARAM_INT);
if (groups_is_member($answer_value_group_id, $USER->id)) {
$answer_value_group = $DB->get_record('groups', array('id' => $answer_value_group_id), 'id,name', MUST_EXIST);
groups_remove_member($answer_value_group_id, $USER->id);
$event = \mod_choicegroup\event\choice_removed::create($eventparams);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('choicegroup', $choicegroup);
$event->trigger();
}
}
}
} else { // multipleenrollmentspossible != 1
$timenow = time();
if (has_capability('mod/choicegroup:deleteresponses', $context)) {
if ($action == 'delete') { //some responses need to be deleted
choicegroup_delete_responses($userids, $choicegroup, $cm, $course); //delete responses.
redirect("view.php?id=$cm->id");
}
}
$answer = optional_param('answer', '', PARAM_INT);
if (empty($answer)) {
redirect(new moodle_url('/mod/choicegroup/view.php',
array('id' => $cm->id, 'notify' => 'mustchooseone', 'sesskey' => sesskey())));
} else {
choicegroup_user_submit_response($answer, $choicegroup, $USER->id, $course, $cm);
redirect(new moodle_url('/mod/choicegroup/view.php',
array('id' => $cm->id, 'notify' => 'choicegroupsaved', 'sesskey' => sesskey())));
}
}
}
/// Display the choicegroup and possibly results
$event = \mod_choicegroup\event\course_module_viewed::create($eventparams);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('choicegroup', $choicegroup);
$event->trigger();
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($choicegroup->name));
if ($notify and confirm_sesskey()) {
if ($notify === 'choicegroupsaved') {
echo $OUTPUT->notification(get_string('choicegroupsaved', 'choicegroup'), 'notifysuccess');
} else if ($notify === 'mustchooseone') {
echo $OUTPUT->notification(get_string('mustchooseone', 'choicegroup'), 'notifyproblem');
} else if ($notify === 'mustchoosemax') {
echo $OUTPUT->notification(get_string('mustchoosemax', 'choicegroup', $choicegroup->maxenrollments), 'notifyproblem');
}
}
if (class_exists('\core_completion\cm_completion_details') && class_exists('\core\activity_dates')) {
// Show the activity dates and completion details.
$modinfo = get_fast_modinfo($course);
$cminfo = $modinfo->get_cm($cm->id);
$cmcompletion = \core_completion\cm_completion_details::get_instance($cminfo, $USER->id);
$activitydates = \core\activity_dates::get_dates_for_module($cminfo, $USER->id);
echo $OUTPUT->activity_information($cminfo, $cmcompletion, $activitydates);
}
/// Check to see if groups are being used in this choicegroup
$groupmode = groups_get_activity_groupmode($cm);
if ($groupmode) {
groups_get_activity_group($cm, true);
groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choicegroup/view.php?id='.$id);
}
// Big function, approx 6 SQL calls per user.
$allresponses = choicegroup_get_response_data($choicegroup, $cm, $groupmode, $choicegroup->onlyactive);
if (has_capability('mod/choicegroup:readresponses', $context)) {
choicegroup_show_reportlink($choicegroup, $allresponses, $cm);
}
echo '<div class="clearer"></div>';
if ($choicegroup->intro) {
echo $OUTPUT->box(format_module_intro('choicegroup', $choicegroup, $cm->id), 'generalbox', 'intro');
}
//if user has already made a selection, and they are not allowed to update it, show their selected answer.
if (isloggedin() && ($current !== false) ) {
if ($choicegroup->multipleenrollmentspossible == 1) {
$currents = choicegroup_get_user_answer($choicegroup, $USER, true, true);
$names = array();
if (is_array($currents)) {
foreach ($currents as $current) {
$names[] = format_string($current->name);
}
}
$formatted_names = join(' '.get_string("and", "choicegroup").' ', array_filter(array_merge(array(join(', ', array_slice($names, 0, -1))), array_slice($names, -1))));
echo $OUTPUT->box(get_string("yourselection", "choicegroup", userdate($choicegroup->timeopen)).": ".$formatted_names, 'generalbox', 'yourselection');
} else {
echo $OUTPUT->box(get_string("yourselection", "choicegroup", userdate($choicegroup->timeopen)).": ".format_string($current->name), 'generalbox', 'yourselection');
}
}
/// Print the form
$choicegroupopen = true;
$timenow = time();
if ($choicegroup->timeclose !=0) {
if ($choicegroup->timeopen > $timenow ) {
echo $OUTPUT->box(get_string("notopenyet", "choicegroup", userdate($choicegroup->timeopen)), "generalbox notopenyet");
echo $OUTPUT->footer();
exit;
} else if ($timenow > $choicegroup->timeclose) {
echo $OUTPUT->box(get_string("expired", "choicegroup", userdate($choicegroup->timeclose)), "generalbox expired");
$choicegroupopen = false;
}
}
$options = choicegroup_prepare_options($choicegroup, $USER, $cm, $allresponses);
$renderer = $PAGE->get_renderer('mod_choicegroup');
if ( (!$current or $choicegroup->allowupdate) and $choicegroupopen and is_enrolled($context, null, 'mod/choicegroup:choose')) {
// They haven't made their choicegroup yet or updates allowed and choicegroup is open
echo $renderer->display_options($options, $cm->id, $choicegroup->display, $choicegroup->publish, $choicegroup->limitanswers, $choicegroup->showresults, $current, $choicegroupopen, false, $choicegroup->multipleenrollmentspossible, $choicegroup->onlyactive);
} else {
// form can not be updated
echo $renderer->display_options($options, $cm->id, $choicegroup->display, $choicegroup->publish, $choicegroup->limitanswers, $choicegroup->showresults, $current, $choicegroupopen, true, $choicegroup->multipleenrollmentspossible, $choicegroup->onlyactive);
}
$choicegroupformshown = true;
$sitecontext = context_system::instance();
if (isguestuser()) {
// Guest account
echo $OUTPUT->confirm(get_string('noguestchoose', 'choicegroup').'<br /><br />'.get_string('liketologin'),
get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id)));
} else if (!is_enrolled($context)) {
// Only people enrolled can make a choicegroup
$SESSION->wantsurl = $FULLME;
$SESSION->enrolcancel = (!empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
$coursecontext = context_course::instance($course->id);
$courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
echo $OUTPUT->box_start('generalbox', 'notice');
echo '<p class="center">'. get_string('notenrolledchoose', 'choicegroup') .'</p>';
echo $OUTPUT->container_start('continuebutton');
echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
echo $OUTPUT->container_end();
echo $OUTPUT->box_end();
}
// print the results at the bottom of the screen
if ( $choicegroup->showresults == CHOICEGROUP_SHOWRESULTS_ALWAYS or
($choicegroup->showresults == CHOICEGROUP_SHOWRESULTS_AFTER_ANSWER and $current) or
($choicegroup->showresults == CHOICEGROUP_SHOWRESULTS_AFTER_CLOSE and !$choicegroupopen)) {
}
else if ($choicegroup->showresults == CHOICEGROUP_SHOWRESULTS_NOT) {
echo $OUTPUT->box(get_string('neverresultsviewable', 'choicegroup'));
}
else if ($choicegroup->showresults == CHOICEGROUP_SHOWRESULTS_AFTER_ANSWER && !$current) {
echo $OUTPUT->box(get_string('afterresultsviewable', 'choicegroup'));
}
else if ($choicegroup->showresults == CHOICEGROUP_SHOWRESULTS_AFTER_CLOSE and $choicegroupopen) {
echo $OUTPUT->box(get_string('notyetresultsviewable', 'choicegroup'));
}
else if (!$choicegroupformshown) {
echo $OUTPUT->box(get_string('noresultsviewable', 'choicegroup'));
}
echo $OUTPUT->footer();