forked from PoetOS/moodle-mod_questionnaire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
213 lines (195 loc) · 10.4 KB
/
index.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?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/>.
/**
* This script lists all the instances of questionnaire in a particular course
*
* @package mod_questionnaire
* @copyright 2016 Mike Churchward ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("../../config.php");
require_once($CFG->dirroot.'/mod/questionnaire/locallib.php');
$id = required_param('id', PARAM_INT);
$PAGE->set_url('/mod/questionnaire/index.php', array('id' => $id));
if (! $course = $DB->get_record('course', array('id' => $id))) {
throw new \moodle_exception('Filter has not been set.', 'mod_questionnaire');
}
$coursecontext = context_course::instance($id);
require_login($course->id);
$PAGE->set_pagelayout('incourse');
$event = \mod_questionnaire\event\course_module_instance_list_viewed::create(array(
'context' => context_course::instance($course->id)));
$event->trigger();
// Print the header.
$strquestionnaires = get_string("modulenameplural", "questionnaire");
$PAGE->navbar->add($strquestionnaires);
$PAGE->set_title("$course->shortname: $strquestionnaires");
$PAGE->set_heading(format_string($course->fullname));
echo $OUTPUT->header();
// Get all the appropriate data.
if (!$questionnaires = get_all_instances_in_course("questionnaire", $course)) {
notice(get_string('thereareno', 'moodle', $strquestionnaires), "../../course/view.php?id=$course->id");
die;
}
// Check if we need the closing date header.
$showclosingheader = false;
foreach ($questionnaires as $questionnaire) {
if ($questionnaire->closedate != 0) {
$showclosingheader = true;
}
if ($showclosingheader) {
break;
}
}
// Configure table for displaying the list of instances.
$headings = array(get_string('name'));
$align = array('left');
if ($showclosingheader) {
array_push($headings, get_string('questionnairecloses', 'questionnaire'));
array_push($align, 'left');
}
array_unshift($headings, get_string('sectionname', 'format_'.$course->format));
array_unshift($align, 'left');
$showing = '';
// Current user role == admin or teacher.
if (has_capability('mod/questionnaire:viewsingleresponse', $coursecontext)) {
array_push($headings, get_string('responses', 'questionnaire'));
array_push($align, 'center');
$showing = 'stats';
array_push($headings, get_string('realm', 'questionnaire'));
array_push($align, 'left');
// Current user role == student.
} else if (has_capability('mod/questionnaire:submit', $coursecontext)) {
array_push($headings, get_string('status'));
array_push($align, 'left');
$showing = 'responses';
}
$table = new html_table();
$table->head = $headings;
$table->align = $align;
// Populate the table with the list of instances.
$currentsection = '';
foreach ($questionnaires as $questionnaire) {
$cmid = $questionnaire->coursemodule;
$data = array();
$realm = $DB->get_field('questionnaire_survey', 'realm', array('id' => $questionnaire->sid));
// Template surveys should NOT be displayed as an activity to students.
if (!($realm == 'template' && !has_capability('mod/questionnaire:manage', context_module::instance($cmid)))) {
// Section number if necessary.
$strsection = '';
if ($questionnaire->section != $currentsection) {
$strsection = get_section_name($course, $questionnaire->section);
$currentsection = $questionnaire->section;
}
$data[] = $strsection;
// Show normal if the mod is visible.
$class = '';
if (!$questionnaire->visible) {
$class = ' class="dimmed"';
}
$data[] = "<a$class href=\"view.php?id=$cmid\">$questionnaire->name</a>";
// Close date.
if ($questionnaire->closedate) {
$data[] = userdate($questionnaire->closedate);
} else if ($showclosingheader) {
$data[] = '';
}
if ($showing == 'responses') {
$status = '';
if ($responses = questionnaire_get_user_responses($questionnaire->id, $USER->id, $complete = false)) {
foreach ($responses as $response) {
if ($response->complete == 'y') {
$status .= get_string('submitted', 'questionnaire').' '.userdate($response->submitted).'<br />';
} else {
$status .= get_string('attemptstillinprogress', 'questionnaire').' '.
userdate($response->submitted).'<br />';
}
}
}
$data[] = $status;
} else if ($showing == 'stats') {
$data[] = $DB->count_records('questionnaire_response', ['questionnaireid' => $questionnaire->id, 'complete' => 'y']);
if ($survey = $DB->get_record('questionnaire_survey', ['id' => $questionnaire->sid])) {
// For a public questionnaire, look for the original public questionnaire that it is based on.
if ($survey->realm == 'public') {
$strpreview = get_string('preview_questionnaire', 'questionnaire');
if ($survey->courseid != $course->id) {
$publicoriginal = '';
$originalcourse = $DB->get_record('course', ['id' => $survey->courseid]);
$originalcoursecontext = context_course::instance($survey->courseid);
$originalquestionnaire = $DB->get_record('questionnaire',
['sid' => $survey->id, 'course' => $survey->courseid]);
$cm = get_coursemodule_from_instance("questionnaire", $originalquestionnaire->id, $survey->courseid);
$context = context_course::instance($survey->courseid, MUST_EXIST);
$canvieworiginal = has_capability('mod/questionnaire:preview', $context, $USER->id, true);
// If current user can view questionnaires in original course,
// provide a link to the original public questionnaire.
if ($canvieworiginal) {
$publicoriginal = '<br />'.get_string('publicoriginal', 'questionnaire').' '.
'<a href="'.$CFG->wwwroot.'/mod/questionnaire/preview.php?id='.
$cm->id.'" title="'.$strpreview.']">'.$originalquestionnaire->name.' ['.
$originalcourse->fullname.']</a>';
} else {
// If current user is not enrolled as teacher in original course,
// only display the original public questionnaire's name and course name.
$publicoriginal = '<br />'.get_string('publicoriginal', 'questionnaire').' '.
$originalquestionnaire->name.' ['.$originalcourse->fullname.']';
}
$data[] = get_string($realm, 'questionnaire').' '.$publicoriginal;
} else {
// Original public questionnaire was created in current course.
// Find which courses it is used in.
$publiccopy = '';
$select = 'course != '.$course->id.' AND sid = '.$questionnaire->sid;
if ($copies = $DB->get_records_select('questionnaire', $select, null,
$sort = 'course ASC', $fields = 'id, course, name')) {
foreach ($copies as $copy) {
$copycourse = $DB->get_record('course', array('id' => $copy->course));
$select = 'course = '.$copycourse->id.' AND sid = '.$questionnaire->sid;
$copyquestionnaire = $DB->get_record('questionnaire',
array('id' => $copy->id, 'sid' => $survey->id, 'course' => $copycourse->id));
$cm = get_coursemodule_from_instance("questionnaire", $copyquestionnaire->id, $copycourse->id);
$context = context_course::instance($copycourse->id, MUST_EXIST);
$canviewcopy = has_capability('mod/questionnaire:view', $context, $USER->id, true);
if ($canviewcopy) {
$publiccopy .= '<br />'.get_string('publiccopy', 'questionnaire').' : '.
'<a href = "'.$CFG->wwwroot.'/mod/questionnaire/preview.php?id='.
$cm->id.'" title = "'.$strpreview.'">'.
$copyquestionnaire->name.' ['.$copycourse->fullname.']</a>';
} else {
// If current user does not have "view" capability in copy course,
// only display the copied public questionnaire's name and course name.
$publiccopy .= '<br />'.get_string('publiccopy', 'questionnaire').' : '.
$copyquestionnaire->name.' ['.$copycourse->fullname.']';
}
}
}
$data[] = get_string($realm, 'questionnaire').' '.$publiccopy;
}
} else {
$data[] = get_string($realm, 'questionnaire');
}
} else {
// If a questionnaire is a copy of a public questionnaire which has been deleted.
$data[] = get_string('removenotinuse', 'questionnaire');
}
}
}
$table->data[] = $data;
} // End of loop over questionnaire instances.
echo html_writer::table($table);
// Finish the page.
echo $OUTPUT->footer();