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));
14 $url->param('action', $action);
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 if ($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));
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);
47 redirect("view.php?id=$cm->id");
51 $PAGE->set_title($choice->name);
52 $PAGE->set_heading($course->fullname);
54 /// Submit any new data if there is any
55 if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) {
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");
63 // Redirection after all POSTs breaks block editing, we need to be more specific!
64 if ($choice->allowmultiple) {
65 $answer = optional_param_array('answer', array(), PARAM_INT);
67 $answer = optional_param('answer', '', PARAM_INT);
71 choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
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())));
82 // Completion and trigger events.
83 choice_view($choice, $course, $cm, $context);
85 echo $OUTPUT->header();
86 echo $OUTPUT->heading(format_string($choice->name), 2, null);
88 if ($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');
96 /// Display the choice and possibly results
98 $eventdata['objectid'] = $choice->id;
99 $eventdata['context'] = $context;
101 /// Check to see if groups are being used in this choice
102 $groupmode = groups_get_activity_groupmode($cm);
105 groups_get_activity_group($cm, true);
106 groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/view.php?id='.$id);
109 // Check if we want to include responses from inactive users.
110 $onlyactive = $choice->includeinactive ? false : true;
112 $allresponses = choice_get_response_data($choice, $cm, $groupmode, $onlyactive); // Big function, approx 6 SQL calls per user.
115 if (has_capability('mod/choice:readresponses', $context)) {
116 choice_show_reportlink($allresponses, $cm);
119 echo '<div class="clearer"></div>';
121 if ($choice->intro) {
122 echo $OUTPUT->box(format_module_intro('choice', $choice, $cm->id), 'generalbox', 'intro');
126 $current = choice_get_my_response($choice);
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.
128 if (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));
134 echo $OUTPUT->box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".implode('; ', $choicetexts), 'generalbox', 'yourselection');
139 if ($choice->timeclose !=0) {
140 if ($choice->timeopen > $timenow ) {
141 if ($choice->showpreview) {
142 echo $OUTPUT->box(get_string('previewonly', 'choice', userdate($choice->timeopen)), 'generalbox alert');
144 echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet");
145 echo $OUTPUT->footer();
148 } else if ($timenow > $choice->timeclose) {
149 echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired");
154 if ( (!$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
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;
162 $choiceformshown = false;
165 if (!$choiceformshown) {
166 $sitecontext = context_system::instance();
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();
175 $SESSION->enrolcancel = get_local_referer(false);
177 $coursecontext = context_course::instance($course->id);
178 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
180 echo $OUTPUT->box_start('generalbox', 'notice');
181 echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
182 echo $OUTPUT->container_start('continuebutton');
183 echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
184 echo $OUTPUT->container_end();
185 echo $OUTPUT->box_end();
190 // print the results at the bottom of the screen
191 if (choice_can_view_results($choice, $current, $choiceopen)) {
193 if (!empty($choice->showunanswered)) {
194 $choice->option[0] = get_string('notanswered', 'choice');
195 $choice->maxanswers[0] = 0;
197 $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
198 $renderer = $PAGE->get_renderer('mod_choice');
199 echo $renderer->display_result($results);
201 } else if (!$choiceformshown) {
202 echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
205 echo $OUTPUT->footer();