Merge branch 'MDL-68434-master-enfix' of git://github.com/mudrd8mz/moodle
[moodle.git] / mod / choice / view.php
blob63f797893eac6b5e12ce6343099a2f0605666895
1 <?php
3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once($CFG->libdir . '/completionlib.php');
7 $id = required_param('id', PARAM_INT); // Course Module ID
8 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
9 $attemptids = optional_param_array('attemptid', array(), PARAM_INT); // Get array of responses to delete or modify.
10 $userids = optional_param_array('userid', array(), PARAM_INT); // Get array of users whose choices need to be modified.
11 $notify = optional_param('notify', '', PARAM_ALPHA);
13 $url = new moodle_url('/mod/choice/view.php', array('id'=>$id));
14 if ($action !== '') {
15 $url->param('action', $action);
17 $PAGE->set_url($url);
19 if (! $cm = get_coursemodule_from_id('choice', $id)) {
20 print_error('invalidcoursemodule');
23 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
24 print_error('coursemisconf');
27 require_course_login($course, false, $cm);
29 if (!$choice = choice_get_choice($cm->instance)) {
30 print_error('invalidcoursemodule');
33 $strchoice = get_string('modulename', 'choice');
34 $strchoices = get_string('modulenameplural', 'choice');
36 $context = context_module::instance($cm->id);
38 list($choiceavailable, $warnings) = choice_get_availability_status($choice);
40 if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate
41 and $choiceavailable) {
42 $answercount = $DB->count_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
43 if ($answercount > 0) {
44 $choiceanswers = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id),
45 '', 'id');
46 $todelete = array_keys($choiceanswers);
47 choice_delete_responses($todelete, $choice, $cm, $course);
48 redirect("view.php?id=$cm->id");
52 $PAGE->set_title($choice->name);
53 $PAGE->set_heading($course->fullname);
55 /// Submit any new data if there is any
56 if (data_submitted() && !empty($action) && confirm_sesskey()) {
57 $timenow = time();
58 if (has_capability('mod/choice:deleteresponses', $context)) {
59 if ($action === 'delete') {
60 // Some responses need to be deleted.
61 choice_delete_responses($attemptids, $choice, $cm, $course);
62 redirect("view.php?id=$cm->id");
64 if (preg_match('/^choose_(\d+)$/', $action, $actionmatch)) {
65 // Modify responses of other users.
66 $newoptionid = (int)$actionmatch[1];
67 choice_modify_responses($userids, $attemptids, $newoptionid, $choice, $cm, $course);
68 redirect("view.php?id=$cm->id");
72 // Redirection after all POSTs breaks block editing, we need to be more specific!
73 if ($choice->allowmultiple) {
74 $answer = optional_param_array('answer', array(), PARAM_INT);
75 } else {
76 $answer = optional_param('answer', '', PARAM_INT);
79 if (!$choiceavailable) {
80 $reason = current(array_keys($warnings));
81 throw new moodle_exception($reason, 'choice', '', $warnings[$reason]);
84 if ($answer && is_enrolled($context, null, 'mod/choice:choose')) {
85 choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
86 redirect(new moodle_url('/mod/choice/view.php',
87 array('id' => $cm->id, 'notify' => 'choicesaved', 'sesskey' => sesskey())));
88 } else if (empty($answer) and $action === 'makechoice') {
89 // We cannot use the 'makechoice' alone because there might be some legacy renderers without it,
90 // outdated renderers will not get the 'mustchoose' message - bad luck.
91 redirect(new moodle_url('/mod/choice/view.php',
92 array('id' => $cm->id, 'notify' => 'mustchooseone', 'sesskey' => sesskey())));
96 // Completion and trigger events.
97 choice_view($choice, $course, $cm, $context);
99 echo $OUTPUT->header();
100 echo $OUTPUT->heading(format_string($choice->name), 2, null);
102 if ($notify and confirm_sesskey()) {
103 if ($notify === 'choicesaved') {
104 echo $OUTPUT->notification(get_string('choicesaved', 'choice'), 'notifysuccess');
105 } else if ($notify === 'mustchooseone') {
106 echo $OUTPUT->notification(get_string('mustchooseone', 'choice'), 'notifyproblem');
110 /// Display the choice and possibly results
111 $eventdata = array();
112 $eventdata['objectid'] = $choice->id;
113 $eventdata['context'] = $context;
115 /// Check to see if groups are being used in this choice
116 $groupmode = groups_get_activity_groupmode($cm);
118 if ($groupmode) {
119 groups_get_activity_group($cm, true);
120 groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/view.php?id='.$id);
123 // Check if we want to include responses from inactive users.
124 $onlyactive = $choice->includeinactive ? false : true;
126 $allresponses = choice_get_response_data($choice, $cm, $groupmode, $onlyactive); // Big function, approx 6 SQL calls per user.
129 if (has_capability('mod/choice:readresponses', $context)) {
130 choice_show_reportlink($allresponses, $cm);
133 echo '<div class="clearer"></div>';
135 if ($choice->intro) {
136 echo $OUTPUT->box(format_module_intro('choice', $choice, $cm->id), 'generalbox', 'intro');
139 $timenow = time();
140 $current = choice_get_my_response($choice);
141 //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.
142 if (isloggedin() && (!empty($current)) &&
143 (empty($choice->allowupdate) || ($timenow > $choice->timeclose)) ) {
144 $choicetexts = array();
145 foreach ($current as $c) {
146 $choicetexts[] = format_string(choice_get_option_text($choice, $c->optionid));
148 echo $OUTPUT->box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".implode('; ', $choicetexts), 'generalbox', 'yourselection');
151 /// Print the form
152 $choiceopen = true;
153 if ((!empty($choice->timeopen)) && ($choice->timeopen > $timenow)) {
154 if ($choice->showpreview) {
155 echo $OUTPUT->box(get_string('previewonly', 'choice', userdate($choice->timeopen)), 'generalbox alert');
156 } else {
157 echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet");
158 echo $OUTPUT->footer();
159 exit;
161 } else if ((!empty($choice->timeclose)) && ($timenow > $choice->timeclose)) {
162 echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired");
163 $choiceopen = false;
166 if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
168 // Show information on how the results will be published to students.
169 $publishinfo = null;
170 switch ($choice->showresults) {
171 case CHOICE_SHOWRESULTS_NOT:
172 $publishinfo = get_string('publishinfonever', 'choice');
173 break;
175 case CHOICE_SHOWRESULTS_AFTER_ANSWER:
176 if ($choice->publish == CHOICE_PUBLISH_ANONYMOUS) {
177 $publishinfo = get_string('publishinfoanonafter', 'choice');
178 } else {
179 $publishinfo = get_string('publishinfofullafter', 'choice');
181 break;
183 case CHOICE_SHOWRESULTS_AFTER_CLOSE:
184 if ($choice->publish == CHOICE_PUBLISH_ANONYMOUS) {
185 $publishinfo = get_string('publishinfoanonclose', 'choice');
186 } else {
187 $publishinfo = get_string('publishinfofullclose', 'choice');
189 break;
191 default:
192 // No need to inform the user in the case of CHOICE_SHOWRESULTS_ALWAYS since it's already obvious that the results are
193 // being published.
194 break;
197 // Show info if necessary.
198 if (!empty($publishinfo)) {
199 echo $OUTPUT->notification($publishinfo, 'info');
202 // They haven't made their choice yet or updates allowed and choice is open.
203 $options = choice_prepare_options($choice, $USER, $cm, $allresponses);
204 $renderer = $PAGE->get_renderer('mod_choice');
205 echo $renderer->display_options($options, $cm->id, $choice->display, $choice->allowmultiple);
206 $choiceformshown = true;
207 } else {
208 $choiceformshown = false;
211 if (!$choiceformshown) {
212 $sitecontext = context_system::instance();
214 if (isguestuser()) {
215 // Guest account
216 echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
217 get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id)));
218 } else if (!is_enrolled($context)) {
219 // Only people enrolled can make a choice
220 $SESSION->wantsurl = qualified_me();
221 $SESSION->enrolcancel = get_local_referer(false);
223 $coursecontext = context_course::instance($course->id);
224 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
226 echo $OUTPUT->box_start('generalbox', 'notice');
227 echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
228 echo $OUTPUT->container_start('continuebutton');
229 echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
230 echo $OUTPUT->container_end();
231 echo $OUTPUT->box_end();
236 // print the results at the bottom of the screen
237 if (choice_can_view_results($choice, $current, $choiceopen)) {
238 $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
239 $renderer = $PAGE->get_renderer('mod_choice');
240 $resultstable = $renderer->display_result($results);
241 echo $OUTPUT->box($resultstable);
243 } else if (!$choiceformshown) {
244 echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
247 echo $OUTPUT->footer();