MDL-51569 mod_choice: Check choice availability prior to do any action
[moodle.git] / mod / choice / view.php
CommitLineData
97dcb8f8 1<?php
f9903ed0 2
54bdf1d4
MA
3require_once("../../config.php");
4require_once("lib.php");
5require_once($CFG->libdir . '/completionlib.php');
f9903ed0 6
54bdf1d4
MA
7$id = required_param('id', PARAM_INT); // Course Module ID
8$action = optional_param('action', '', PARAM_ALPHA);
9$attemptids = optional_param_array('attemptid', array(), PARAM_INT); // array of attempt ids for delete action
5fb2315a 10$notify = optional_param('notify', '', PARAM_ALPHA);
407caeeb 11
54bdf1d4
MA
12$url = new moodle_url('/mod/choice/view.php', array('id'=>$id));
13if ($action !== '') {
14 $url->param('action', $action);
15}
16$PAGE->set_url($url);
97dcb8f8 17
54bdf1d4
MA
18if (! $cm = get_coursemodule_from_id('choice', $id)) {
19 print_error('invalidcoursemodule');
20}
f9903ed0 21
54bdf1d4
MA
22if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
23 print_error('coursemisconf');
24}
f9903ed0 25
54bdf1d4 26require_course_login($course, false, $cm);
f9903ed0 27
54bdf1d4
MA
28if (!$choice = choice_get_choice($cm->instance)) {
29 print_error('invalidcoursemodule');
30}
407caeeb 31
54bdf1d4
MA
32$strchoice = get_string('modulename', 'choice');
33$strchoices = get_string('modulenameplural', 'choice');
4e19d85b 34
54bdf1d4 35$context = context_module::instance($cm->id);
f9903ed0 36
3dd0ac55
JL
37list($choiceavailable, $warnings) = choice_get_availability_status($choice);
38
39if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate
40 and $choiceavailable) {
54bdf1d4
MA
41 $answercount = $DB->count_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
42 if ($answercount > 0) {
43 $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
bc499733 44
54bdf1d4
MA
45 // Update completion state
46 $completion = new completion_info($course);
47 if ($completion->is_enabled($cm) && $choice->completionsubmit) {
48 $completion->update_state($cm, COMPLETION_INCOMPLETE);
392763df 49 }
5fb2315a 50 redirect("view.php?id=$cm->id");
243ac1fe 51 }
54bdf1d4 52}
f9903ed0 53
54bdf1d4
MA
54$PAGE->set_title($choice->name);
55$PAGE->set_heading($course->fullname);
5550ae83 56
d12280bb 57/// Submit any new data if there is any
54bdf1d4
MA
58if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) {
59 $timenow = time();
cbb1677c
SH
60 if (has_capability('mod/choice:deleteresponses', $context) && $action == 'delete') {
61 //some responses need to be deleted
62 choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
63 redirect("view.php?id=$cm->id");
f9903ed0 64 }
65
5fb2315a 66 // Redirection after all POSTs breaks block editing, we need to be more specific!
54bdf1d4
MA
67 if ($choice->allowmultiple) {
68 $answer = optional_param_array('answer', array(), PARAM_INT);
69 } else {
70 $answer = optional_param('answer', '', PARAM_INT);
a61e4188 71 }
407caeeb 72
3dd0ac55
JL
73 if (!$choiceavailable) {
74 $reason = current(array_keys($warnings));
75 throw new moodle_exception($reason, 'choice', '', $warnings[$reason]);
76 }
77
5fb2315a 78 if ($answer) {
54bdf1d4 79 choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
5fb2315a
PS
80 redirect(new moodle_url('/mod/choice/view.php',
81 array('id' => $cm->id, 'notify' => 'choicesaved', 'sesskey' => sesskey())));
82 } else if (empty($answer) and $action === 'makechoice') {
83 // We cannot use the 'makechoice' alone because there might be some legacy renderers without it,
84 // outdated renderers will not get the 'mustchoose' message - bad luck.
85 redirect(new moodle_url('/mod/choice/view.php',
86 array('id' => $cm->id, 'notify' => 'mustchooseone', 'sesskey' => sesskey())));
f9903ed0 87 }
54bdf1d4 88}
f9903ed0 89
4c4d3b73
JL
90// Completion and trigger events.
91choice_view($choice, $course, $cm, $context);
92
5fb2315a
PS
93echo $OUTPUT->header();
94echo $OUTPUT->heading(format_string($choice->name), 2, null);
95
96if ($notify and confirm_sesskey()) {
97 if ($notify === 'choicesaved') {
98 echo $OUTPUT->notification(get_string('choicesaved', 'choice'), 'notifysuccess');
99 } else if ($notify === 'mustchooseone') {
100 echo $OUTPUT->notification(get_string('mustchooseone', 'choice'), 'notifyproblem');
101 }
102}
f9903ed0 103
54bdf1d4
MA
104/// Display the choice and possibly results
105$eventdata = array();
106$eventdata['objectid'] = $choice->id;
107$eventdata['context'] = $context;
108
54bdf1d4
MA
109/// Check to see if groups are being used in this choice
110$groupmode = groups_get_activity_groupmode($cm);
111
112if ($groupmode) {
113 groups_get_activity_group($cm, true);
114 groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/view.php?id='.$id);
115}
6de3eee0
TB
116
117// Check if we want to include responses from inactive users.
118$onlyactive = $choice->includeinactive ? false : true;
119
120$allresponses = choice_get_response_data($choice, $cm, $groupmode, $onlyactive); // Big function, approx 6 SQL calls per user.
54bdf1d4
MA
121
122
123if (has_capability('mod/choice:readresponses', $context)) {
124 choice_show_reportlink($allresponses, $cm);
125}
126
127echo '<div class="clearer"></div>';
128
129if ($choice->intro) {
130 echo $OUTPUT->box(format_module_intro('choice', $choice, $cm->id), 'generalbox', 'intro');
131}
132
133$timenow = time();
b87f31db 134$current = choice_get_my_response($choice);
54bdf1d4
MA
135//if user has already made a selection, and they are not allowed to update it or if choice is not open, show their selected answer.
136if (isloggedin() && (!empty($current)) &&
137 (empty($choice->allowupdate) || ($timenow > $choice->timeclose)) ) {
138 $choicetexts = array();
139 foreach ($current as $c) {
140 $choicetexts[] = format_string(choice_get_option_text($choice, $c->optionid));
cabf7c23 141 }
54bdf1d4
MA
142 echo $OUTPUT->box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".implode('; ', $choicetexts), 'generalbox', 'yourselection');
143}
d30772b2 144
5550ae83 145/// Print the form
54bdf1d4
MA
146$choiceopen = true;
147if ($choice->timeclose !=0) {
148 if ($choice->timeopen > $timenow ) {
c30dbd69
TB
149 if ($choice->showpreview) {
150 echo $OUTPUT->box(get_string('previewonly', 'choice', userdate($choice->timeopen)), 'generalbox alert');
151 } else {
152 echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet");
153 echo $OUTPUT->footer();
154 exit;
155 }
54bdf1d4
MA
156 } else if ($timenow > $choice->timeclose) {
157 echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired");
158 $choiceopen = false;
d30772b2 159 }
54bdf1d4
MA
160}
161
162if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
163// They haven't made their choice yet or updates allowed and choice is open
164
165 $options = choice_prepare_options($choice, $USER, $cm, $allresponses);
166 $renderer = $PAGE->get_renderer('mod_choice');
167 echo $renderer->display_options($options, $cm->id, $choice->display, $choice->allowmultiple);
168 $choiceformshown = true;
169} else {
170 $choiceformshown = false;
171}
172
173if (!$choiceformshown) {
174 $sitecontext = context_system::instance();
175
176 if (isguestuser()) {
177 // Guest account
178 echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
179 get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id)));
180 } else if (!is_enrolled($context)) {
181 // Only people enrolled can make a choice
182 $SESSION->wantsurl = qualified_me();
dcee0b94 183 $SESSION->enrolcancel = get_local_referer(false);
54bdf1d4
MA
184
185 $coursecontext = context_course::instance($course->id);
186 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
187
188 echo $OUTPUT->box_start('generalbox', 'notice');
189 echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
190 echo $OUTPUT->container_start('continuebutton');
b3cb7264 191 echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
54bdf1d4
MA
192 echo $OUTPUT->container_end();
193 echo $OUTPUT->box_end();
d30772b2 194
d30772b2 195 }
54bdf1d4 196}
d30772b2 197
54bdf1d4 198// print the results at the bottom of the screen
b87f31db 199if (choice_can_view_results($choice, $current, $choiceopen)) {
4e19d85b 200
54bdf1d4
MA
201 if (!empty($choice->showunanswered)) {
202 $choice->option[0] = get_string('notanswered', 'choice');
203 $choice->maxanswers[0] = 0;
6d6c9389 204 }
54bdf1d4
MA
205 $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
206 $renderer = $PAGE->get_renderer('mod_choice');
207 echo $renderer->display_result($results);
ec81373f 208
54bdf1d4
MA
209} else if (!$choiceformshown) {
210 echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
211}
a337045d 212
54bdf1d4 213echo $OUTPUT->footer();