forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.php
169 lines (126 loc) · 6.3 KB
/
view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
require_once("../../config.php");
require_once("lib.php");
require_once($CFG->libdir . '/completionlib.php');
$id = required_param('id', PARAM_INT); // Course Module ID
$action = optional_param('action', '', PARAM_ALPHA);
$attemptids = optional_param('attemptid', array(), PARAM_INT); // array of attempt ids for delete action
$url = new moodle_url('/mod/choice/view.php', array('id'=>$id));
if ($action !== '') {
$url->param('action', $action);
}
$PAGE->set_url($url);
if (! $cm = get_coursemodule_from_id('choice', $id)) {
print_error('invalidcoursemodule');
}
if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
print_error('coursemisconf');
}
require_course_login($course, false, $cm);
if (!$choice = choice_get_choice($cm->instance)) {
print_error('invalidcoursemodule');
}
$strchoice = get_string('modulename', 'choice');
$strchoices = get_string('modulenameplural', 'choice');
if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
print_error('badcontext');
}
if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate) {
if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) {
//print_object($answer);
$DB->delete_records('choice_answers', array('id' => $answer->id));
}
}
$PAGE->set_title(format_string($choice->name));
echo $OUTPUT->header();
/// Submit any new data if there is any
if ($form = data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) {
$timenow = time();
if (has_capability('mod/choice:deleteresponses', $context)) {
if ($action == 'delete') { //some responses need to be deleted
choice_delete_responses($attemptids, $choice->id); //delete responses.
redirect("view.php?id=$cm->id");
}
}
$answer = optional_param('answer', '', PARAM_INT);
if (empty($answer)) {
redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
} else {
choice_user_submit_response($answer, $choice, $USER->id, $course->id, $cm);
}
echo $OUTPUT->notification(get_string('choicesaved', 'choice'),'notifysuccess');
}
/// Display the choice and possibly results
add_to_log($course->id, "choice", "view", "view.php?id=$cm->id", $choice->id, $cm->id);
/// Check to see if groups are being used in this choice
$groupmode = groups_get_activity_groupmode($cm);
if ($groupmode) {
groups_get_activity_group($cm, true);
groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/view.php?id='.$id);
}
$allresponses = choice_get_response_data($choice, $cm, $groupmode); // Big function, approx 6 SQL calls per user
if (has_capability('mod/choice:readresponses', $context)) {
choice_show_reportlink($allresponses, $cm);
}
echo '<div class="clearer"></div>';
if ($choice->intro) {
echo $OUTPUT->box(format_module_intro('choice', $choice, $cm->id), 'generalbox', 'intro');
}
$current = false; // Initialise for later
//if user has already made a selection, and they are not allowed to update it, show their selected answer.
if (isloggedin() && ($current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) &&
empty($choice->allowupdate) ) {
echo $OUTPUT->box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".format_string(choice_get_option_text($choice, $current->optionid)));
}
/// Print the form
$choiceopen = true;
$timenow = time();
if ($choice->timeclose !=0) {
if ($choice->timeopen > $timenow ) {
echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet");
echo $OUTPUT->footer();
exit;
} else if ($timenow > $choice->timeclose) {
echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired");
$choiceopen = false;
}
}
if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
// They haven't made their choice yet or updates allowed and choice is open
echo '<form id="form" method="post" action="view.php">';
choice_show_form($choice, $USER, $cm, $allresponses);
echo '</form>';
$choiceformshown = true;
} else {
$choiceformshown = false;
}
if (!$choiceformshown) {
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
if (isguestuser()) {
// Guest account
echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
get_login_url(), new moodle_url);
} else if (!is_enrolled($context)) {
// Only people enrolled can make a choice
$SESSION->wantsurl = $FULLME;
$SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
echo $OUTPUT->box_start('generalbox', 'notice');
echo '<p align="center">'. get_string('noguestchoose', 'choice') .'</p>';
echo $OUTPUT->container_start('continuebutton');
echo $OUTPUT->single_button(new moodle_url('/course/enrol.php?', array('id'=>$course->id)), get_string('enrolme', '', format_string($course->shortname)));
echo $OUTPUT->container_end();
echo $OUTPUT->box_end();
}
}
// print the results at the bottom of the screen
if ( $choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or
($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and $current ) or
($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and !$choiceopen ) ) {
choice_show_results($choice, $course, $cm, $allresponses); //show table with students responses.
} else if (!$choiceformshown) {
echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
}
echo $OUTPUT->footer();
/// Mark as viewed
$completion=new completion_info($course);
$completion->set_module_viewed($cm);