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
11 $url = new moodle_url('/mod/choice/view.php', array('id'=>$id));
13 $url->param('action', $action);
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 $context = context_module
::instance($cm->id
);
36 if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate
) {
37 if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id
, 'userid' => $USER->id
))) {
38 $DB->delete_records('choice_answers', array('id' => $answer->id
));
40 // Update completion state
41 $completion = new completion_info($course);
42 if ($completion->is_enabled($cm) && $choice->completionsubmit
) {
43 $completion->update_state($cm, COMPLETION_INCOMPLETE
);
48 $PAGE->set_title(format_string($choice->name
));
49 $PAGE->set_heading($course->fullname
);
51 // Mark viewed by user (if required)
52 $completion = new completion_info($course);
53 $completion->set_module_viewed($cm);
55 /// Submit any new data if there is any
56 if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) {
58 if (has_capability('mod/choice:deleteresponses', $context)) {
59 if ($action == 'delete') { //some responses need to be deleted
60 choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
61 redirect("view.php?id=$cm->id");
64 $answer = optional_param('answer', '', PARAM_INT
);
67 redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
69 choice_user_submit_response($answer, $choice, $USER->id
, $course, $cm);
71 echo $OUTPUT->header();
72 echo $OUTPUT->notification(get_string('choicesaved', 'choice'),'notifysuccess');
74 echo $OUTPUT->header();
78 /// Display the choice and possibly results
79 add_to_log($course->id
, "choice", "view", "view.php?id=$cm->id", $choice->id
, $cm->id
);
81 /// Check to see if groups are being used in this choice
82 $groupmode = groups_get_activity_groupmode($cm);
85 groups_get_activity_group($cm, true);
86 groups_print_activity_menu($cm, $CFG->wwwroot
. '/mod/choice/view.php?id='.$id);
88 $allresponses = choice_get_response_data($choice, $cm, $groupmode); // Big function, approx 6 SQL calls per user
91 if (has_capability('mod/choice:readresponses', $context)) {
92 choice_show_reportlink($allresponses, $cm);
95 echo '<div class="clearer"></div>';
98 echo $OUTPUT->box(format_module_intro('choice', $choice, $cm->id
), 'generalbox', 'intro');
102 $current = false; // Initialise for later
103 //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.
104 if (isloggedin() && ($current = $DB->get_record('choice_answers', array('choiceid' => $choice->id
, 'userid' => $USER->id
))) &&
105 (empty($choice->allowupdate
) ||
($timenow > $choice->timeclose
)) ) {
106 echo $OUTPUT->box(get_string("yourselection", "choice", userdate($choice->timeopen
)).": ".format_string(choice_get_option_text($choice, $current->optionid
)), 'generalbox', 'yourselection');
111 if ($choice->timeclose
!=0) {
112 if ($choice->timeopen
> $timenow ) {
113 echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen
)), "generalbox notopenyet");
114 echo $OUTPUT->footer();
116 } else if ($timenow > $choice->timeclose
) {
117 echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose
)), "generalbox expired");
122 if ( (!$current or $choice->allowupdate
) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
123 // They haven't made their choice yet or updates allowed and choice is open
125 $options = choice_prepare_options($choice, $USER, $cm, $allresponses);
126 $renderer = $PAGE->get_renderer('mod_choice');
127 echo $renderer->display_options($options, $cm->id
, $choice->display
);
128 $choiceformshown = true;
130 $choiceformshown = false;
133 if (!$choiceformshown) {
134 $sitecontext = context_system
::instance();
138 echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
139 get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id
)));
140 } else if (!is_enrolled($context)) {
141 // Only people enrolled can make a choice
142 $SESSION->wantsurl
= qualified_me();
143 $SESSION->enrolcancel
= (!empty($_SERVER['HTTP_REFERER'])) ?
$_SERVER['HTTP_REFERER'] : '';
145 $coursecontext = context_course
::instance($course->id
);
146 $courseshortname = format_string($course->shortname
, true, array('context' => $coursecontext));
148 echo $OUTPUT->box_start('generalbox', 'notice');
149 echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
150 echo $OUTPUT->container_start('continuebutton');
151 echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id
)), get_string('enrolme', 'core_enrol', $courseshortname));
152 echo $OUTPUT->container_end();
153 echo $OUTPUT->box_end();
158 // print the results at the bottom of the screen
159 if ( $choice->showresults
== CHOICE_SHOWRESULTS_ALWAYS
or
160 ($choice->showresults
== CHOICE_SHOWRESULTS_AFTER_ANSWER
and $current) or
161 ($choice->showresults
== CHOICE_SHOWRESULTS_AFTER_CLOSE
and !$choiceopen)) {
163 if (!empty($choice->showunanswered
)) {
164 $choice->option
[0] = get_string('notanswered', 'choice');
165 $choice->maxanswers
[0] = 0;
167 $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
168 $renderer = $PAGE->get_renderer('mod_choice');
169 echo $renderer->display_result($results);
171 } else if (!$choiceformshown) {
172 echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
175 echo $OUTPUT->footer();