forked from ndunand/moodle-mod_choicegroup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.php
181 lines (145 loc) · 7.81 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
<?php
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
$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);
if (!$choicegroup = choicegroup_get_choicegroup($cm->instance)) {
print_error('invalidcoursemodule');
}
$choicegroup_groups = choicegroup_get_groups($choicegroup);
$strchoicegroup = get_string('modulename', 'choicegroup');
$strchoicegroups = get_string('modulenameplural', 'choicegroup');
if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
print_error('badcontext');
}
$current = choicegroup_get_user_answer($choicegroup, $USER);
if ($action == 'delchoicegroup' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choicegroup:choose') and $choicegroup->allowupdate) {
// user wants to delete his own choice:
if ($current !== false) {
groups_remove_member($current->id, $USER->id);
$current = choicegroup_get_user_answer($choicegroup, $USER);
// 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()) {
$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("view.php?id=$cm->id", get_string('mustchooseone', 'choicegroup'));
} else {
choicegroup_user_submit_response($answer, $choicegroup, $USER->id, $course, $cm);
}
redirect("view.php?id=$cm->id", get_string('choicegroupsaved', 'choicegroup'));
} else {
echo $OUTPUT->header();
}
/// Display the choicegroup and possibly results
add_to_log($course->id, "choicegroup", "view", "view.php?id=$cm->id", $choicegroup->id, $cm->id);
/// 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);
}
$allresponses = choicegroup_get_response_data($choicegroup, $cm); // Big function, approx 6 SQL calls per user
if (has_capability('mod/choicegroup:readresponses', $context)) {
choicegroup_show_reportlink($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) ) {
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);
} else {
// form can not be updated
echo $renderer->display_options($options, $cm->id, $choicegroup->display, $choicegroup->publish, $choicegroup->limitanswers, $choicegroup->showresults, $current, $choicegroupopen, true);
}
$choicegroupformshown = true;
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
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 = get_context_instance(CONTEXT_COURSE, $course->id);
$courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
echo $OUTPUT->box_start('generalbox', 'notice');
echo '<p align="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();