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