1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* This script lists all the instances of questionnaire in a particular course
|
|
|
19 |
*
|
|
|
20 |
* @package mod_questionnaire
|
|
|
21 |
* @copyright 2016 Mike Churchward (mike.churchward@poetopensource.org)
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require_once("../../config.php");
|
|
|
26 |
require_once($CFG->dirroot.'/mod/questionnaire/locallib.php');
|
|
|
27 |
|
|
|
28 |
$id = required_param('id', PARAM_INT);
|
|
|
29 |
$PAGE->set_url('/mod/questionnaire/index.php', array('id' => $id));
|
|
|
30 |
if (! $course = $DB->get_record('course', array('id' => $id))) {
|
|
|
31 |
throw new \moodle_exception('Filter has not been set.', 'mod_questionnaire');
|
|
|
32 |
}
|
|
|
33 |
$coursecontext = context_course::instance($id);
|
|
|
34 |
require_login($course->id);
|
|
|
35 |
$PAGE->set_pagelayout('incourse');
|
|
|
36 |
|
|
|
37 |
$event = \mod_questionnaire\event\course_module_instance_list_viewed::create(array(
|
|
|
38 |
'context' => context_course::instance($course->id)));
|
|
|
39 |
$event->trigger();
|
|
|
40 |
|
|
|
41 |
// Print the header.
|
|
|
42 |
$strquestionnaires = get_string("modulenameplural", "questionnaire");
|
|
|
43 |
$PAGE->navbar->add($strquestionnaires);
|
|
|
44 |
$PAGE->set_title("$course->shortname: $strquestionnaires");
|
|
|
45 |
$PAGE->set_heading(format_string($course->fullname));
|
|
|
46 |
echo $OUTPUT->header();
|
|
|
47 |
|
|
|
48 |
// Get all the appropriate data.
|
|
|
49 |
if (!$questionnaires = get_all_instances_in_course("questionnaire", $course)) {
|
|
|
50 |
notice(get_string('thereareno', 'moodle', $strquestionnaires), "../../course/view.php?id=$course->id");
|
|
|
51 |
die;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
// Check if we need the closing date header.
|
|
|
55 |
$showclosingheader = false;
|
|
|
56 |
foreach ($questionnaires as $questionnaire) {
|
|
|
57 |
if ($questionnaire->closedate != 0) {
|
|
|
58 |
$showclosingheader = true;
|
|
|
59 |
}
|
|
|
60 |
if ($showclosingheader) {
|
|
|
61 |
break;
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// Configure table for displaying the list of instances.
|
|
|
66 |
$headings = array(get_string('name'));
|
|
|
67 |
$align = array('left');
|
|
|
68 |
|
|
|
69 |
if ($showclosingheader) {
|
|
|
70 |
array_push($headings, get_string('questionnairecloses', 'questionnaire'));
|
|
|
71 |
array_push($align, 'left');
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
array_unshift($headings, get_string('sectionname', 'format_'.$course->format));
|
|
|
75 |
array_unshift($align, 'left');
|
|
|
76 |
|
|
|
77 |
$showing = '';
|
|
|
78 |
|
|
|
79 |
// Current user role == admin or teacher.
|
|
|
80 |
if (has_capability('mod/questionnaire:viewsingleresponse', $coursecontext)) {
|
|
|
81 |
array_push($headings, get_string('responses', 'questionnaire'));
|
|
|
82 |
array_push($align, 'center');
|
|
|
83 |
$showing = 'stats';
|
|
|
84 |
array_push($headings, get_string('realm', 'questionnaire'));
|
|
|
85 |
array_push($align, 'left');
|
|
|
86 |
// Current user role == student.
|
|
|
87 |
} else if (has_capability('mod/questionnaire:submit', $coursecontext)) {
|
|
|
88 |
array_push($headings, get_string('status'));
|
|
|
89 |
array_push($align, 'left');
|
|
|
90 |
$showing = 'responses';
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$table = new html_table();
|
|
|
94 |
$table->head = $headings;
|
|
|
95 |
$table->align = $align;
|
|
|
96 |
|
|
|
97 |
// Populate the table with the list of instances.
|
|
|
98 |
$currentsection = '';
|
|
|
99 |
foreach ($questionnaires as $questionnaire) {
|
|
|
100 |
$cmid = $questionnaire->coursemodule;
|
|
|
101 |
$data = array();
|
|
|
102 |
$realm = $DB->get_field('questionnaire_survey', 'realm', array('id' => $questionnaire->sid));
|
|
|
103 |
// Template surveys should NOT be displayed as an activity to students.
|
|
|
104 |
if (!($realm == 'template' && !has_capability('mod/questionnaire:manage', context_module::instance($cmid)))) {
|
|
|
105 |
// Section number if necessary.
|
|
|
106 |
$strsection = '';
|
|
|
107 |
if ($questionnaire->section != $currentsection) {
|
|
|
108 |
$strsection = get_section_name($course, $questionnaire->section);
|
|
|
109 |
$currentsection = $questionnaire->section;
|
|
|
110 |
}
|
|
|
111 |
$data[] = $strsection;
|
|
|
112 |
// Show normal if the mod is visible.
|
|
|
113 |
$class = '';
|
|
|
114 |
if (!$questionnaire->visible) {
|
|
|
115 |
$class = ' class="dimmed"';
|
|
|
116 |
}
|
|
|
117 |
$data[] = "<a$class href=\"view.php?id=$cmid\">$questionnaire->name</a>";
|
|
|
118 |
|
|
|
119 |
// Close date.
|
|
|
120 |
if ($questionnaire->closedate) {
|
|
|
121 |
$data[] = userdate($questionnaire->closedate);
|
|
|
122 |
} else if ($showclosingheader) {
|
|
|
123 |
$data[] = '';
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
if ($showing == 'responses') {
|
|
|
127 |
$status = '';
|
|
|
128 |
if ($responses = questionnaire_get_user_responses($questionnaire->id, $USER->id, $complete = false)) {
|
|
|
129 |
foreach ($responses as $response) {
|
|
|
130 |
if ($response->complete == 'y') {
|
|
|
131 |
$status .= get_string('submitted', 'questionnaire').' '.userdate($response->submitted).'<br />';
|
|
|
132 |
} else {
|
|
|
133 |
$status .= get_string('attemptstillinprogress', 'questionnaire').' '.
|
|
|
134 |
userdate($response->submitted).'<br />';
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
$data[] = $status;
|
|
|
139 |
} else if ($showing == 'stats') {
|
|
|
140 |
$data[] = $DB->count_records('questionnaire_response', ['questionnaireid' => $questionnaire->id, 'complete' => 'y']);
|
|
|
141 |
if ($survey = $DB->get_record('questionnaire_survey', ['id' => $questionnaire->sid])) {
|
|
|
142 |
// For a public questionnaire, look for the original public questionnaire that it is based on.
|
|
|
143 |
if ($survey->realm == 'public') {
|
|
|
144 |
$strpreview = get_string('preview_questionnaire', 'questionnaire');
|
|
|
145 |
if ($survey->courseid != $course->id) {
|
|
|
146 |
$publicoriginal = '';
|
|
|
147 |
$originalcourse = $DB->get_record('course', ['id' => $survey->courseid]);
|
|
|
148 |
$originalcoursecontext = context_course::instance($survey->courseid);
|
|
|
149 |
$originalquestionnaire = $DB->get_record('questionnaire',
|
|
|
150 |
['sid' => $survey->id, 'course' => $survey->courseid]);
|
|
|
151 |
$cm = get_coursemodule_from_instance("questionnaire", $originalquestionnaire->id, $survey->courseid);
|
|
|
152 |
$context = context_course::instance($survey->courseid, MUST_EXIST);
|
|
|
153 |
$canvieworiginal = has_capability('mod/questionnaire:preview', $context, $USER->id, true);
|
|
|
154 |
// If current user can view questionnaires in original course,
|
|
|
155 |
// provide a link to the original public questionnaire.
|
|
|
156 |
if ($canvieworiginal) {
|
|
|
157 |
$publicoriginal = '<br />'.get_string('publicoriginal', 'questionnaire').' '.
|
|
|
158 |
'<a href="'.$CFG->wwwroot.'/mod/questionnaire/preview.php?id='.
|
|
|
159 |
$cm->id.'" title="'.$strpreview.']">'.$originalquestionnaire->name.' ['.
|
|
|
160 |
$originalcourse->fullname.']</a>';
|
|
|
161 |
} else {
|
|
|
162 |
// If current user is not enrolled as teacher in original course,
|
|
|
163 |
// only display the original public questionnaire's name and course name.
|
|
|
164 |
$publicoriginal = '<br />'.get_string('publicoriginal', 'questionnaire').' '.
|
|
|
165 |
$originalquestionnaire->name.' ['.$originalcourse->fullname.']';
|
|
|
166 |
}
|
|
|
167 |
$data[] = get_string($realm, 'questionnaire').' '.$publicoriginal;
|
|
|
168 |
} else {
|
|
|
169 |
// Original public questionnaire was created in current course.
|
|
|
170 |
// Find which courses it is used in.
|
|
|
171 |
$publiccopy = '';
|
|
|
172 |
$select = 'course != '.$course->id.' AND sid = '.$questionnaire->sid;
|
|
|
173 |
if ($copies = $DB->get_records_select('questionnaire', $select, null,
|
|
|
174 |
$sort = 'course ASC', $fields = 'id, course, name')) {
|
|
|
175 |
foreach ($copies as $copy) {
|
|
|
176 |
$copycourse = $DB->get_record('course', array('id' => $copy->course));
|
|
|
177 |
$select = 'course = '.$copycourse->id.' AND sid = '.$questionnaire->sid;
|
|
|
178 |
$copyquestionnaire = $DB->get_record('questionnaire',
|
|
|
179 |
array('id' => $copy->id, 'sid' => $survey->id, 'course' => $copycourse->id));
|
|
|
180 |
$cm = get_coursemodule_from_instance("questionnaire", $copyquestionnaire->id, $copycourse->id);
|
|
|
181 |
$context = context_course::instance($copycourse->id, MUST_EXIST);
|
|
|
182 |
$canviewcopy = has_capability('mod/questionnaire:view', $context, $USER->id, true);
|
|
|
183 |
if ($canviewcopy) {
|
|
|
184 |
$publiccopy .= '<br />'.get_string('publiccopy', 'questionnaire').' : '.
|
|
|
185 |
'<a href = "'.$CFG->wwwroot.'/mod/questionnaire/preview.php?id='.
|
|
|
186 |
$cm->id.'" title = "'.$strpreview.'">'.
|
|
|
187 |
$copyquestionnaire->name.' ['.$copycourse->fullname.']</a>';
|
|
|
188 |
} else {
|
|
|
189 |
// If current user does not have "view" capability in copy course,
|
|
|
190 |
// only display the copied public questionnaire's name and course name.
|
|
|
191 |
$publiccopy .= '<br />'.get_string('publiccopy', 'questionnaire').' : '.
|
|
|
192 |
$copyquestionnaire->name.' ['.$copycourse->fullname.']';
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
$data[] = get_string($realm, 'questionnaire').' '.$publiccopy;
|
|
|
197 |
}
|
|
|
198 |
} else {
|
|
|
199 |
$data[] = get_string($realm, 'questionnaire');
|
|
|
200 |
}
|
|
|
201 |
} else {
|
|
|
202 |
// If a questionnaire is a copy of a public questionnaire which has been deleted.
|
|
|
203 |
$data[] = get_string('removenotinuse', 'questionnaire');
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
$table->data[] = $data;
|
|
|
208 |
} // End of loop over questionnaire instances.
|
|
|
209 |
|
|
|
210 |
echo html_writer::table($table);
|
|
|
211 |
|
|
|
212 |
// Finish the page.
|
|
|
213 |
echo $OUTPUT->footer();
|