forked from PoetOS/moodle-mod_questionnaire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.php
178 lines (151 loc) · 7.26 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
169
170
171
172
173
174
175
176
177
178
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once("../../config.php");
require_once($CFG->dirroot.'/mod/questionnaire/locallib.php');
require_once($CFG->libdir . '/completionlib.php');
require_once($CFG->dirroot.'/mod/questionnaire/questionnaire.class.php');
if (!isset($SESSION->questionnaire)) {
$SESSION->questionnaire = new stdClass();
}
$SESSION->questionnaire->current_tab = 'view';
$id = optional_param('id', null, PARAM_INT); // Course Module ID.
$a = optional_param('a', null, PARAM_INT); // Or questionnaire ID.
$sid = optional_param('sid', null, PARAM_INT); // Survey id.
list($cm, $course, $questionnaire) = questionnaire_get_standard_page_items($id, $a);
// Check login and get context.
require_course_login($course, true, $cm);
$context = context_module::instance($cm->id);
$url = new moodle_url($CFG->wwwroot.'/mod/questionnaire/view.php');
if (isset($id)) {
$url->param('id', $id);
} else {
$url->param('a', $a);
}
if (isset($sid)) {
$url->param('sid', $sid);
}
$PAGE->set_url($url);
$PAGE->set_context($context);
$questionnaire = new questionnaire(0, $questionnaire, $course, $cm);
// Add renderer and page objects to the questionnaire object for display use.
$questionnaire->add_renderer($PAGE->get_renderer('mod_questionnaire'));
$questionnaire->add_page(new \mod_questionnaire\output\viewpage());
$PAGE->set_title(format_string($questionnaire->name));
$PAGE->set_heading(format_string($course->fullname));
echo $questionnaire->renderer->header();
$questionnaire->page->add_to_page('questionnairename', format_string($questionnaire->name));
// Print the main part of the page.
if ($questionnaire->intro) {
$questionnaire->page->add_to_page('intro', format_module_intro('questionnaire', $questionnaire, $cm->id));
}
$cm = $questionnaire->cm;
$currentgroupid = groups_get_activity_group($cm);
if (!groups_is_member($currentgroupid, $USER->id)) {
$currentgroupid = 0;
}
if (!$questionnaire->is_active()) {
if ($questionnaire->capabilities->manage) {
$msg = 'removenotinuse';
} else {
$msg = 'notavail';
}
$questionnaire->page->add_to_page('message', get_string($msg, 'questionnaire'));
} else if ($questionnaire->survey->realm == 'template') {
// If this is a template survey, notify and exit.
$questionnaire->page->add_to_page('message', get_string('templatenotviewable', 'questionnaire'));
echo $questionnaire->renderer->render($questionnaire->page);
echo $questionnaire->renderer->footer($questionnaire->course);
exit();
} else if (!$questionnaire->is_open()) {
$questionnaire->page->add_to_page('message', get_string('notopen', 'questionnaire', userdate($questionnaire->opendate)));
} else if ($questionnaire->is_closed()) {
$questionnaire->page->add_to_page('message', get_string('closed', 'questionnaire', userdate($questionnaire->closedate)));
} else if (!$questionnaire->user_is_eligible($USER->id)) {
if ($questionnaire->questions) {
$questionnaire->page->add_to_page('message', get_string('noteligible', 'questionnaire'));
}
} else if (!$questionnaire->user_can_take($USER->id)) {
switch ($questionnaire->qtype) {
case QUESTIONNAIREDAILY:
$msgstring = ' '.get_string('today', 'questionnaire');
break;
case QUESTIONNAIREWEEKLY:
$msgstring = ' '.get_string('thisweek', 'questionnaire');
break;
case QUESTIONNAIREMONTHLY:
$msgstring = ' '.get_string('thismonth', 'questionnaire');
break;
default:
$msgstring = '';
break;
}
$questionnaire->page->add_to_page('message', get_string("alreadyfilled", "questionnaire", $msgstring));
} else if ($questionnaire->user_can_take($USER->id)) {
if ($questionnaire->questions) { // Sanity check.
if (!$questionnaire->user_has_saved_response($USER->id)) {
$questionnaire->page->add_to_page('complete',
'<a href="'.$CFG->wwwroot.htmlspecialchars('/mod/questionnaire/complete.php?' .
'id='.$questionnaire->cm->id).'">'.get_string('answerquestions', 'questionnaire').'</a>');
} else {
$questionnaire->page->add_to_page('complete',
'<a href="'.$CFG->wwwroot.htmlspecialchars('/mod/questionnaire/complete.php?' .
'id='.$questionnaire->cm->id.'&resume=1').'">'.get_string('resumesurvey', 'questionnaire').'</a>');
}
} else {
$questionnaire->page->add_to_page('message', get_string('noneinuse', 'questionnaire'));
}
}
if ($questionnaire->capabilities->editquestions && !$questionnaire->questions && $questionnaire->is_active()) {
$questionnaire->page->add_to_page('complete',
'<a href="'.$CFG->wwwroot.htmlspecialchars('/mod/questionnaire/questions.php?'.
'id='.$questionnaire->cm->id).'">'.'<strong>'.get_string('addquestions', 'questionnaire').'</strong></a>');
}
if (isguestuser()) {
$guestno = html_writer::tag('p', get_string('noteligible', 'questionnaire'));
$liketologin = html_writer::tag('p', get_string('liketologin'));
$questionnaire->page->add_to_page('guestuser',
$questionnaire->renderer->confirm($guestno."\n\n".$liketologin."\n", get_login_url(), get_local_referer(false)));
}
// Log this course module view.
// Needed for the event logging.
$context = context_module::instance($questionnaire->cm->id);
$anonymous = $questionnaire->respondenttype == 'anonymous';
$event = \mod_questionnaire\event\course_module_viewed::create(array(
'objectid' => $questionnaire->id,
'anonymous' => $anonymous,
'context' => $context
));
$event->trigger();
$usernumresp = $questionnaire->count_submissions($USER->id);
if ($questionnaire->capabilities->readownresponses && ($usernumresp > 0)) {
$argstr = 'instance='.$questionnaire->id.'&user='.$USER->id;
if ($usernumresp > 1) {
$titletext = get_string('viewyourresponses', 'questionnaire', $usernumresp);
} else {
$titletext = get_string('yourresponse', 'questionnaire');
$argstr .= '&byresponse=1&action=vresp';
}
$questionnaire->page->add_to_page('yourresponse',
'<a href="'.$CFG->wwwroot.htmlspecialchars('/mod/questionnaire/myreport.php?'.$argstr).'">'.$titletext.'</a>');
}
if ($questionnaire->can_view_all_responses($usernumresp)) {
$argstr = 'instance='.$questionnaire->id.'&group='.$currentgroupid;
$questionnaire->page->add_to_page('allresponses',
'<a href="'.$CFG->wwwroot.htmlspecialchars('/mod/questionnaire/report.php?'.$argstr).'">'.
get_string('viewallresponses', 'questionnaire').'</a>');
}
echo $questionnaire->renderer->render($questionnaire->page);
echo $questionnaire->renderer->footer();