MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / choice / view.php
blobe36863470bf43331f2fb032fa566f9b9ef84e29d
1 <?php // $Id$
3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = required_param('id', PARAM_INT); // Course Module ID
7 $action = optional_param('action', '', PARAM_ALPHA);
8 $attemptids = optional_param('attemptid', array(), PARAM_INT); // array of attempt ids for delete action
10 if (! $cm = get_coursemodule_from_id('choice', $id)) {
11 error("Course Module ID was incorrect");
14 if (! $course = get_record("course", "id", $cm->course)) {
15 error("Course is misconfigured");
18 require_course_login($course, false, $cm);
20 if (!$choice = choice_get_choice($cm->instance)) {
21 error("Course module is incorrect");
24 $strchoice = get_string('modulename', 'choice');
25 $strchoices = get_string('modulenameplural', 'choice');
27 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
28 print_error('badcontext');
31 if ($action == 'delchoice') {
32 if ($answer = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $USER->id)) {
33 //print_object($answer);
34 delete_records('choice_answers', 'id', $answer->id);
38 /// Submit any new data if there is any
40 if ($form = data_submitted() && has_capability('mod/choice:choose', $context)) {
41 $timenow = time();
42 if (has_capability('mod/choice:deleteresponses', $context)) {
43 if ($action == 'delete') { //some responses need to be deleted
44 choice_delete_responses($attemptids); //delete responses.
45 redirect("view.php?id=$cm->id");
48 $answer = optional_param('answer', '', PARAM_INT);
50 if (empty($answer)) {
51 redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
52 } else {
53 choice_user_submit_response($answer, $choice, $USER->id, $course->id, $cm);
55 redirect("view.php?id=$cm->id");
56 exit;
60 /// Display the choice and possibly results
61 $navlinks = array();
62 $navlinks[] = array('name' => $strchoices, 'link' => "index.php?id=$course->id", 'type' => 'activity');
63 $navlinks[] = array('name' => format_string($choice->name), 'link' => '', 'type' => 'activityinstance');
65 $navigation = build_navigation($navlinks);
67 print_header_simple(format_string($choice->name), "", $navigation, "", "", true,
68 update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
70 add_to_log($course->id, "choice", "view", "view.php?id=$cm->id", $choice->id, $cm->id);
72 /// Check to see if groups are being used in this choice
73 $groupmode = groups_get_activity_groupmode($cm);
74 groups_get_activity_group($cm, true);
75 groups_print_activity_menu($cm, 'view.php?id='.$id);
77 if (has_capability('mod/choice:readresponses', $context)) {
78 choice_show_reportlink($choice, $course->id, $cm->id, $groupmode);
81 echo '<div class="clearer"></div>';
83 if ($choice->text) {
84 print_box(format_text($choice->text, $choice->format), 'generalbox', 'intro');
87 //if user has already made a selection, and they are not allowed to update it, show their selected answer.
88 if (!empty($USER->id) && ($current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $USER->id)) &&
89 empty($choice->allowupdate) ) {
90 print_simple_box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".format_string(choice_get_option_text($choice, $current->optionid)), "center");
93 /// Print the form
95 if ($choice->timeopen > time() ) {
96 print_simple_box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "center");
97 print_footer($course);
98 exit;
101 if ( (!$current or $choice->allowupdate) and
102 ($choice->timeclose >= time() or $choice->timeclose == 0) and
103 has_capability('mod/choice:choose', $context) ) {
104 // They haven't made their choice yet or updates allowed and choice is open
106 echo '<form id="form" method="post" action="view.php">';
108 choice_show_form($choice, $USER, $cm);
110 echo '</form>';
112 $choiceformshown = true;
113 } else {
114 $choiceformshown = false;
119 if (!$choiceformshown) {
121 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
123 if (has_capability('moodle/legacy:guest', $sitecontext, NULL, false)) { // Guest on whole site
124 $wwwroot = $CFG->wwwroot.'/login/index.php';
125 if (!empty($CFG->loginhttps)) {
126 $wwwroot = str_replace('http:','https:', $wwwroot);
128 notice_yesno(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
129 $wwwroot, $_SERVER['HTTP_REFERER']);
131 } else if (has_capability('moodle/legacy:guest', $context, NULL, false)) { // Guest in this course only
132 $SESSION->wantsurl = $FULLME;
133 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
135 print_simple_box_start('center', '60%', '', 5, 'generalbox', 'notice');
136 echo '<p align="center">'. get_string('noguestchoose', 'choice') .'</p>';
137 echo '<div class="continuebutton">';
138 print_single_button($CFG->wwwroot.'/course/enrol.php?id='.$course->id, NULL,
139 get_string('enrolme', '', format_string($course->shortname)), 'post', $CFG->framename);
140 echo '</div>'."\n";
141 print_simple_box_end();
146 // print the results at the bottom of the screen
148 if ( $choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or
149 ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and $current ) or
150 ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and $choice->timeclose <= time() ) ) {
152 choice_show_results($choice, $course, $cm);
154 } else if (!$choiceformshown) {
155 print_simple_box(get_string('noresultsviewable', 'choice'), 'center');
159 print_footer($course);