weekly release 2.9.3+
[moodle.git] / mod / choice / view.php
blobbbfef618e4d737dc4c1a5858afaa66ef1493390f
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_ALPHA);
9 $attemptids = optional_param_array('attemptid', array(), PARAM_INT); // array of attempt ids for delete action
10 $notify = optional_param('notify', '', PARAM_ALPHA);
12 $url = new moodle_url('/mod/choice/view.php', array('id'=>$id));
13 if ($action !== '') {
14 $url->param('action', $action);
16 $PAGE->set_url($url);
18 if (! $cm = get_coursemodule_from_id('choice', $id)) {
19 print_error('invalidcoursemodule');
22 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
23 print_error('coursemisconf');
26 require_course_login($course, false, $cm);
28 if (!$choice = choice_get_choice($cm->instance)) {
29 print_error('invalidcoursemodule');
32 $strchoice = get_string('modulename', 'choice');
33 $strchoices = get_string('modulenameplural', 'choice');
35 $context = context_module::instance($cm->id);
37 list($choiceavailable, $warnings) = choice_get_availability_status($choice);
39 if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate
40 and $choiceavailable) {
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));
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);
50 redirect("view.php?id=$cm->id");
54 $PAGE->set_title($choice->name);
55 $PAGE->set_heading($course->fullname);
57 // Mark viewed by user (if required)
58 $completion = new completion_info($course);
59 $completion->set_module_viewed($cm);
61 /// Submit any new data if there is any
62 if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) {
63 $timenow = time();
64 if (has_capability('mod/choice:deleteresponses', $context) && $action == 'delete') {
65 //some responses need to be deleted
66 choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
67 redirect("view.php?id=$cm->id");
70 // Redirection after all POSTs breaks block editing, we need to be more specific!
71 if ($choice->allowmultiple) {
72 $answer = optional_param_array('answer', array(), PARAM_INT);
73 } else {
74 $answer = optional_param('answer', '', PARAM_INT);
77 if (!$choiceavailable) {
78 $reason = current(array_keys($warnings));
79 throw new moodle_exception($reason, 'choice', '', $warnings[$reason]);
82 if ($answer) {
83 choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
84 redirect(new moodle_url('/mod/choice/view.php',
85 array('id' => $cm->id, 'notify' => 'choicesaved', 'sesskey' => sesskey())));
86 } else if (empty($answer) and $action === 'makechoice') {
87 // We cannot use the 'makechoice' alone because there might be some legacy renderers without it,
88 // outdated renderers will not get the 'mustchoose' message - bad luck.
89 redirect(new moodle_url('/mod/choice/view.php',
90 array('id' => $cm->id, 'notify' => 'mustchooseone', 'sesskey' => sesskey())));
94 echo $OUTPUT->header();
95 echo $OUTPUT->heading(format_string($choice->name), 2, null);
97 if ($notify and confirm_sesskey()) {
98 if ($notify === 'choicesaved') {
99 echo $OUTPUT->notification(get_string('choicesaved', 'choice'), 'notifysuccess');
100 } else if ($notify === 'mustchooseone') {
101 echo $OUTPUT->notification(get_string('mustchooseone', 'choice'), 'notifyproblem');
105 /// Display the choice and possibly results
106 $eventdata = array();
107 $eventdata['objectid'] = $choice->id;
108 $eventdata['context'] = $context;
110 $event = \mod_choice\event\course_module_viewed::create($eventdata);
111 $event->add_record_snapshot('course_modules', $cm);
112 $event->add_record_snapshot('course', $course);
113 $event->trigger();
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 = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
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 ($choice->timeclose !=0) {
154 if ($choice->timeopen > $timenow ) {
155 if ($choice->showpreview) {
156 echo $OUTPUT->box(get_string('previewonly', 'choice', userdate($choice->timeopen)), 'generalbox alert');
157 } else {
158 echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet");
159 echo $OUTPUT->footer();
160 exit;
162 } else if ($timenow > $choice->timeclose) {
163 echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired");
164 $choiceopen = false;
168 if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
169 // They haven't made their choice yet or updates allowed and choice is open
171 $options = choice_prepare_options($choice, $USER, $cm, $allresponses);
172 $renderer = $PAGE->get_renderer('mod_choice');
173 echo $renderer->display_options($options, $cm->id, $choice->display, $choice->allowmultiple);
174 $choiceformshown = true;
175 } else {
176 $choiceformshown = false;
179 if (!$choiceformshown) {
180 $sitecontext = context_system::instance();
182 if (isguestuser()) {
183 // Guest account
184 echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
185 get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id)));
186 } else if (!is_enrolled($context)) {
187 // Only people enrolled can make a choice
188 $SESSION->wantsurl = qualified_me();
189 $SESSION->enrolcancel = get_local_referer(false);
191 $coursecontext = context_course::instance($course->id);
192 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
194 echo $OUTPUT->box_start('generalbox', 'notice');
195 echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
196 echo $OUTPUT->container_start('continuebutton');
197 echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
198 echo $OUTPUT->container_end();
199 echo $OUTPUT->box_end();
204 // print the results at the bottom of the screen
205 if ( $choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or
206 ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and $current) or
207 ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and !$choiceopen)) {
209 if (!empty($choice->showunanswered)) {
210 $choice->option[0] = get_string('notanswered', 'choice');
211 $choice->maxanswers[0] = 0;
213 $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
214 $renderer = $PAGE->get_renderer('mod_choice');
215 echo $renderer->display_result($results);
217 } else if (!$choiceformshown) {
218 echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
221 echo $OUTPUT->footer();