Merge branch 'm21_MDL-28017' of git://github.com/danmarsden/moodle into MOODLE_21_STABLE
[moodle.git] / mod / choice / view.php
blob2e11c5e491e262b4b4a3c5ba47a2af9cdef129f5
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('attemptid', array(), PARAM_INT); // array of attempt ids for delete action
11 $url = new moodle_url('/mod/choice/view.php', array('id'=>$id));
12 if ($action !== '') {
13 $url->param('action', $action);
15 $PAGE->set_url($url);
17 if (! $cm = get_coursemodule_from_id('choice', $id)) {
18 print_error('invalidcoursemodule');
21 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
22 print_error('coursemisconf');
25 require_course_login($course, false, $cm);
27 if (!$choice = choice_get_choice($cm->instance)) {
28 print_error('invalidcoursemodule');
31 $strchoice = get_string('modulename', 'choice');
32 $strchoices = get_string('modulenameplural', 'choice');
34 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
35 print_error('badcontext');
38 if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate) {
39 if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) {
40 $DB->delete_records('choice_answers', array('id' => $answer->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);
50 $PAGE->set_title(format_string($choice->name));
51 $PAGE->set_heading($course->fullname);
53 // Mark viewed by user (if required)
54 $completion = new completion_info($course);
55 $completion->set_module_viewed($cm);
57 /// Submit any new data if there is any
58 if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) {
59 $timenow = time();
60 if (has_capability('mod/choice:deleteresponses', $context)) {
61 if ($action == 'delete') { //some responses need to be deleted
62 choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
63 redirect("view.php?id=$cm->id");
66 $answer = optional_param('answer', '', PARAM_INT);
68 if (empty($answer)) {
69 redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
70 } else {
71 choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
73 echo $OUTPUT->header();
74 echo $OUTPUT->notification(get_string('choicesaved', 'choice'),'notifysuccess');
75 } else {
76 echo $OUTPUT->header();
80 /// Display the choice and possibly results
81 add_to_log($course->id, "choice", "view", "view.php?id=$cm->id", $choice->id, $cm->id);
83 /// Check to see if groups are being used in this choice
84 $groupmode = groups_get_activity_groupmode($cm);
86 if ($groupmode) {
87 groups_get_activity_group($cm, true);
88 groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/view.php?id='.$id);
90 $allresponses = choice_get_response_data($choice, $cm, $groupmode); // Big function, approx 6 SQL calls per user
93 if (has_capability('mod/choice:readresponses', $context)) {
94 choice_show_reportlink($allresponses, $cm);
97 echo '<div class="clearer"></div>';
99 if ($choice->intro) {
100 echo $OUTPUT->box(format_module_intro('choice', $choice, $cm->id), 'generalbox', 'intro');
103 $current = false; // Initialise for later
104 //if user has already made a selection, and they are not allowed to update it, show their selected answer.
105 if (isloggedin() && ($current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) &&
106 empty($choice->allowupdate) ) {
107 echo $OUTPUT->box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".format_string(choice_get_option_text($choice, $current->optionid)), 'generalbox', 'yourselection');
110 /// Print the form
111 $choiceopen = true;
112 $timenow = time();
113 if ($choice->timeclose !=0) {
114 if ($choice->timeopen > $timenow ) {
115 echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet");
116 echo $OUTPUT->footer();
117 exit;
118 } else if ($timenow > $choice->timeclose) {
119 echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired");
120 $choiceopen = false;
124 if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
125 // They haven't made their choice yet or updates allowed and choice is open
127 $options = choice_prepare_options($choice, $USER, $cm, $allresponses);
128 $renderer = $PAGE->get_renderer('mod_choice');
129 echo $renderer->display_options($options, $cm->id, $choice->display);
130 $choiceformshown = true;
131 } else {
132 $choiceformshown = false;
135 if (!$choiceformshown) {
136 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
138 if (isguestuser()) {
139 // Guest account
140 echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
141 get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id)));
142 } else if (!is_enrolled($context)) {
143 // Only people enrolled can make a choice
144 $SESSION->wantsurl = $FULLME;
145 $SESSION->enrolcancel = (!empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
147 echo $OUTPUT->box_start('generalbox', 'notice');
148 echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
149 echo $OUTPUT->container_start('continuebutton');
150 echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', format_string($course->shortname)));
151 echo $OUTPUT->container_end();
152 echo $OUTPUT->box_end();
157 // print the results at the bottom of the screen
158 if ( $choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or
159 ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and $current) or
160 ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and !$choiceopen)) {
162 if (!empty($choice->showunanswered)) {
163 $choice->option[0] = get_string('notanswered', 'choice');
164 $choice->maxanswers[0] = 0;
166 $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
167 $renderer = $PAGE->get_renderer('mod_choice');
168 echo $renderer->display_result($results);
170 } else if (!$choiceformshown) {
171 echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
174 echo $OUTPUT->footer();