weekly release 4.5dev+
[moodle.git] / mod / choice / view.php
blob9f9b8b6a711ccb453811ea47253aa173997daf8d
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 throw new \moodle_exception('invalidcoursemodule');
22 $cm = cm_info::create($cm);
24 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
25 throw new \moodle_exception('coursemisconf');
28 require_course_login($course, false, $cm);
30 if (!$choice = choice_get_choice($cm->instance)) {
31 throw new \moodle_exception('invalidcoursemodule');
34 $strchoice = get_string('modulename', 'choice');
35 $strchoices = get_string('modulenameplural', 'choice');
37 $context = context_module::instance($cm->id);
39 list($choiceavailable, $warnings) = choice_get_availability_status($choice);
41 if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate
42 and $choiceavailable) {
43 $answercount = $DB->count_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
44 if ($answercount > 0) {
45 $choiceanswers = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id),
46 '', 'id');
47 $todelete = array_keys($choiceanswers);
48 choice_delete_responses($todelete, $choice, $cm, $course);
49 redirect("view.php?id=$cm->id");
53 $PAGE->set_title($choice->name);
54 $PAGE->set_heading($course->fullname);
56 /// Submit any new data if there is any
57 if (data_submitted() && !empty($action) && confirm_sesskey()) {
58 $timenow = time();
59 if (has_capability('mod/choice:deleteresponses', $context)) {
60 if ($action === 'delete') {
61 // Some responses need to be deleted.
62 choice_delete_responses($attemptids, $choice, $cm, $course);
63 redirect("view.php?id=$cm->id");
65 if (preg_match('/^choose_(\d+)$/', $action, $actionmatch)) {
66 // Modify responses of other users.
67 $newoptionid = (int)$actionmatch[1];
68 choice_modify_responses($userids, $attemptids, $newoptionid, $choice, $cm, $course);
69 redirect("view.php?id=$cm->id");
73 // Redirection after all POSTs breaks block editing, we need to be more specific!
74 if ($choice->allowmultiple) {
75 $answer = optional_param_array('answer', array(), PARAM_INT);
76 } else {
77 $answer = optional_param('answer', '', PARAM_INT);
80 if (!$choiceavailable) {
81 $reason = current(array_keys($warnings));
82 throw new moodle_exception($reason, 'choice', '', $warnings[$reason]);
85 if ($answer && is_enrolled($context, null, 'mod/choice:choose')) {
86 choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
87 redirect(new moodle_url('/mod/choice/view.php',
88 array('id' => $cm->id, 'notify' => 'choicesaved', 'sesskey' => sesskey())));
89 } else if (empty($answer) and $action === 'makechoice') {
90 // We cannot use the 'makechoice' alone because there might be some legacy renderers without it,
91 // outdated renderers will not get the 'mustchoose' message - bad luck.
92 redirect(new moodle_url('/mod/choice/view.php',
93 array('id' => $cm->id, 'notify' => 'mustchooseone', 'sesskey' => sesskey())));
97 // Completion and trigger events.
98 choice_view($choice, $course, $cm, $context);
100 $PAGE->add_body_class('limitedwidth');
102 echo $OUTPUT->header();
104 if ($notify and confirm_sesskey()) {
105 if ($notify === 'choicesaved') {
106 echo $OUTPUT->notification(get_string('choicesaved', 'choice'), 'notifysuccess');
107 } else if ($notify === 'mustchooseone') {
108 echo $OUTPUT->notification(get_string('mustchooseone', 'choice'), 'notifyproblem');
112 /// Display the choice and possibly results
113 $eventdata = array();
114 $eventdata['objectid'] = $choice->id;
115 $eventdata['context'] = $context;
117 /// Check to see if groups are being used in this choice
118 $groupmode = groups_get_activity_groupmode($cm);
120 // Check if we want to include responses from inactive users.
121 $onlyactive = $choice->includeinactive ? false : true;
123 $allresponses = choice_get_response_data($choice, $cm, $groupmode, $onlyactive); // Big function, approx 6 SQL calls per user.
126 if (has_capability('mod/choice:readresponses', $context) && !$PAGE->has_secondary_navigation()) {
127 choice_show_reportlink($allresponses, $cm);
130 echo '<div class="clearer"></div>';
132 $timenow = time();
133 $current = choice_get_my_response($choice);
134 //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.
135 if (isloggedin() && (!empty($current)) &&
136 (empty($choice->allowupdate) || ($timenow > $choice->timeclose)) ) {
137 $choicetexts = array();
138 foreach ($current as $c) {
139 $choicetexts[] = format_string(choice_get_option_text($choice, $c->optionid));
141 echo $OUTPUT->box(get_string("yourselection", "choice") . ": " . implode('; ', $choicetexts), 'generalbox', 'yourselection');
144 /// Print the form
145 $choiceopen = true;
146 if ((!empty($choice->timeopen)) && ($choice->timeopen > $timenow)) {
147 if ($choice->showpreview) {
148 echo $OUTPUT->box(get_string('previewing', 'choice'), 'generalbox alert');
149 } else {
150 echo $OUTPUT->footer();
151 exit;
153 } else if ((!empty($choice->timeclose)) && ($timenow > $choice->timeclose)) {
154 $choiceopen = false;
157 if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
159 // Show information on how the results will be published to students.
160 $publishinfo = null;
161 switch ($choice->showresults) {
162 case CHOICE_SHOWRESULTS_NOT:
163 $publishinfo = get_string('publishinfonever', 'choice');
164 break;
166 case CHOICE_SHOWRESULTS_AFTER_ANSWER:
167 if ($choice->publish == CHOICE_PUBLISH_ANONYMOUS) {
168 $publishinfo = get_string('publishinfoanonafter', 'choice');
169 } else {
170 $publishinfo = get_string('publishinfofullafter', 'choice');
172 break;
174 case CHOICE_SHOWRESULTS_AFTER_CLOSE:
175 if ($choice->publish == CHOICE_PUBLISH_ANONYMOUS) {
176 $publishinfo = get_string('publishinfoanonclose', 'choice');
177 } else {
178 $publishinfo = get_string('publishinfofullclose', 'choice');
180 break;
182 default:
183 // No need to inform the user in the case of CHOICE_SHOWRESULTS_ALWAYS since it's already obvious that the results are
184 // being published.
185 break;
188 // Show info if necessary.
189 if (!empty($publishinfo)) {
190 echo $OUTPUT->notification($publishinfo, 'info');
193 // They haven't made their choice yet or updates allowed and choice is open.
194 $options = choice_prepare_options($choice, $USER, $cm, $allresponses);
195 $renderer = $PAGE->get_renderer('mod_choice');
196 echo $renderer->display_options($options, $cm->id, $choice->display, $choice->allowmultiple);
197 $choiceformshown = true;
198 } else {
199 $choiceformshown = false;
202 if (!$choiceformshown) {
203 $sitecontext = context_system::instance();
205 if (isguestuser()) {
206 // Guest account
207 echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
208 get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id)));
209 } else if (!is_enrolled($context)) {
210 // Only people enrolled can make a choice
211 $SESSION->wantsurl = qualified_me();
212 $SESSION->enrolcancel = get_local_referer(false);
214 $coursecontext = context_course::instance($course->id);
215 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
217 echo $OUTPUT->box_start('generalbox', 'notice');
218 echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
219 echo $OUTPUT->container_start('continuebutton');
220 echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
221 echo $OUTPUT->container_end();
222 echo $OUTPUT->box_end();
227 // print the results at the bottom of the screen
228 if (choice_can_view_results($choice, $current, $choiceopen)) {
229 $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
230 $renderer = $PAGE->get_renderer('mod_choice');
231 if ($results->publish) { // If set to publish full results, display a heading for the responses section.
232 echo html_writer::tag('h3', format_string(get_string("responses", "choice")), ['class' => 'mt-4']);
235 if ($groupmode) { // If group mode is enabled, display the groups selector.
236 groups_get_activity_group($cm, true);
237 $groupsactivitymenu = groups_print_activity_menu($cm, new moodle_url('/mod/choice/view.php', ['id' => $id]),
238 true);
239 echo html_writer::div($groupsactivitymenu, 'mt-3 mb-1');
242 $resultstable = $renderer->display_result($results);
243 echo $OUTPUT->box($resultstable);
245 } else if (!$choiceformshown) {
246 echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
249 echo $OUTPUT->footer();