weekly release 3.1dev
[moodle.git] / mod / feedback / view.php
CommitLineData
84973212 1<?php
9c5bc7a5
AG
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/>.
c70ad9f7 16
84973212 17/**
18 * the first page to view the feedback
19 *
20 * @author Andreas Grabs
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9714baa1 22 * @package mod_feedback
84973212 23 */
24require_once("../../config.php");
25require_once("lib.php");
26
27$id = required_param('id', PARAM_INT);
28$courseid = optional_param('courseid', false, PARAM_INT);
29
84973212 30$current_tab = 'view';
31
32if (! $cm = get_coursemodule_from_id('feedback', $id)) {
33 print_error('invalidcoursemodule');
34}
35
36if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
37 print_error('coursemisconf');
38}
39
40if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
41 print_error('invalidcoursemodule');
42}
43
2c979976 44$context = context_module::instance($cm->id);
c987dbad
AG
45
46$feedback_complete_cap = false;
47
9c5bc7a5 48if (has_capability('mod/feedback:complete', $context)) {
c987dbad
AG
49 $feedback_complete_cap = true;
50}
84973212 51
2a94a2f6 52if (!empty($CFG->feedback_allowfullanonymous)
8d8e6694 53 AND $course->id == SITEID
2a94a2f6
MG
54 AND $feedback->anonymous == FEEDBACK_ANONYMOUS_YES
55 AND (!isloggedin() OR isguestuser())) {
56 // Guests are allowed to complete fully anonymous feedback without having 'mod/feedback:complete' capability.
c987dbad 57 $feedback_complete_cap = true;
84973212 58}
59
60//check whether the feedback is located and! started from the mainsite
9c5bc7a5 61if ($course->id == SITEID AND !$courseid) {
84973212 62 $courseid = SITEID;
63}
64
9c5bc7a5
AG
65if ($feedback->anonymous != FEEDBACK_ANONYMOUS_YES) {
66 if ($course->id == SITEID) {
cdbea7ee 67 require_login($course, true);
9c5bc7a5 68 } else {
cdbea7ee 69 require_login($course, true, $cm);
da329996 70 }
84973212 71} else {
9c5bc7a5 72 if ($course->id == SITEID) {
84973212 73 require_course_login($course, true);
9c5bc7a5 74 } else {
84973212 75 require_course_login($course, true, $cm);
c70ad9f7 76 }
84973212 77}
78
2a94a2f6
MG
79if ($course->id == SITEID) {
80 $PAGE->set_context($context);
81 $PAGE->set_cm($cm, $course);
82 $PAGE->set_pagelayout('incourse');
83}
84$PAGE->set_url('/mod/feedback/view.php', array('id'=>$cm->id, 'do_show'=>'view'));
85$PAGE->set_title($feedback->name);
86$PAGE->set_heading($course->fullname);
87
88// Check whether the feedback is mapped to the given courseid.
89if ($course->id == SITEID AND !has_capability('mod/feedback:edititems', $context)) {
90 if ($DB->get_records('feedback_sitecourse_map', array('feedbackid' => $feedback->id))) {
91 $params = array('feedbackid' => $feedback->id, 'courseid' => $courseid);
92 if (!$DB->get_record('feedback_sitecourse_map', $params)) {
93 if ($courseid == SITEID) {
94 echo $OUTPUT->header();
95 echo $OUTPUT->notification(get_string('cannotaccess', 'mod_feedback'));
96 echo $OUTPUT->footer();
97 exit;
98 } else {
99 print_error('invalidcoursemodule');
100 }
101 }
102 }
103}
104
84973212 105//check whether the given courseid exists
9c5bc7a5
AG
106if ($courseid AND $courseid != SITEID) {
107 if ($course2 = $DB->get_record('course', array('id'=>$courseid))) {
84973212 108 require_course_login($course2); //this overwrites the object $course :-(
109 $course = $DB->get_record("course", array("id"=>$cm->course)); // the workaround
9c5bc7a5 110 } else {
84973212 111 print_error('invalidcourseid');
c70ad9f7 112 }
84973212 113}
114
3f6fd941
AA
115// Trigger module viewed event.
116$event = \mod_feedback\event\course_module_viewed::create(array(
117 'objectid' => $feedback->id,
118 'context' => $context,
bc293202 119 'anonymous' => ($feedback->anonymous == FEEDBACK_ANONYMOUS_YES),
3f6fd941 120 'other' => array(
bc293202 121 'anonymous' => $feedback->anonymous // Deprecated.
fbc4b778
MG
122 )
123));
3f6fd941
AA
124$event->add_record_snapshot('course_modules', $cm);
125$event->add_record_snapshot('course', $course);
126$event->add_record_snapshot('feedback', $feedback);
127$event->trigger();
84973212 128
129/// Print the page header
130$strfeedbacks = get_string("modulenameplural", "feedback");
131$strfeedback = get_string("modulename", "feedback");
84973212 132echo $OUTPUT->header();
133
134//ishidden check.
135//feedback in courses
9c5bc7a5
AG
136$cap_viewhiddenactivities = has_capability('moodle/course:viewhiddenactivities', $context);
137if ((empty($cm->visible) and !$cap_viewhiddenactivities) AND $course->id != SITEID) {
84973212 138 notice(get_string("activityiscurrentlyhidden"));
139}
140
141//ishidden check.
142//feedback on mainsite
9c5bc7a5 143if ((empty($cm->visible) and !$cap_viewhiddenactivities) AND $courseid == SITEID) {
84973212 144 notice(get_string("activityiscurrentlyhidden"));
145}
146
147/// Print the main part of the page
148///////////////////////////////////////////////////////////////////////////
149///////////////////////////////////////////////////////////////////////////
150///////////////////////////////////////////////////////////////////////////
151
38b5a5e2 152$previewimg = $OUTPUT->pix_icon('t/preview', get_string('preview'));
846cebe5
RW
153$previewlnk = new moodle_url('/mod/feedback/print.php', array('id' => $id));
154$preview = html_writer::link($previewlnk, $previewimg);
84973212 155
846cebe5
RW
156echo $OUTPUT->heading(format_string($feedback->name) . $preview);
157
158// Print the tabs.
159require('tabs.php');
38b5a5e2
AG
160
161//show some infos to the feedback
9c5bc7a5 162if (has_capability('mod/feedback:edititems', $context)) {
38b5a5e2 163 //get the groupid
9c5bc7a5 164 $groupselect = groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/feedback/view.php?id='.$cm->id, true);
38b5a5e2
AG
165 $mygroupid = groups_get_activity_group($cm);
166
167 echo $OUTPUT->box_start('boxaligncenter boxwidthwide');
168 echo $groupselect.'<div class="clearer">&nbsp;</div>';
169 $completedscount = feedback_get_completeds_group_count($feedback, $mygroupid);
170 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5
AG
171 echo '<span class="feedback_info">';
172 echo get_string('completed_feedbacks', 'feedback').': ';
173 echo '</span>';
174 echo '<span class="feedback_info_value">';
175 echo $completedscount;
176 echo '</span>';
38b5a5e2 177 echo $OUTPUT->box_end();
9c5bc7a5
AG
178
179 $params = array('feedback'=>$feedback->id, 'hasvalue'=>1);
180 $itemscount = $DB->count_records('feedback_item', $params);
38b5a5e2 181 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5
AG
182 echo '<span class="feedback_info">';
183 echo get_string('questions', 'feedback').': ';
184 echo '</span>';
185 echo '<span class="feedback_info_value">';
186 echo $itemscount;
187 echo '</span>';
38b5a5e2 188 echo $OUTPUT->box_end();
2396a414 189
9c5bc7a5 190 if ($feedback->timeopen) {
38b5a5e2 191 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5
AG
192 echo '<span class="feedback_info">';
193 echo get_string('feedbackopen', 'feedback').': ';
194 echo '</span>';
195 echo '<span class="feedback_info_value">';
196 echo userdate($feedback->timeopen);
197 echo '</span>';
38b5a5e2
AG
198 echo $OUTPUT->box_end();
199 }
9c5bc7a5 200 if ($feedback->timeclose) {
38b5a5e2 201 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5 202 echo '<span class="feedback_info">';
9b108a86 203 echo get_string('feedbackclose', 'feedback').': ';
9c5bc7a5
AG
204 echo '</span>';
205 echo '<span class="feedback_info_value">';
206 echo userdate($feedback->timeclose);
207 echo '</span>';
38b5a5e2
AG
208 echo $OUTPUT->box_end();
209 }
210 echo $OUTPUT->box_end();
211}
212
9c5bc7a5 213if (has_capability('mod/feedback:edititems', $context)) {
846cebe5 214 echo $OUTPUT->heading(get_string('description', 'feedback'), 3);
38b5a5e2 215}
846cebe5 216echo $OUTPUT->box_start('generalbox boxwidthwide');
84973212 217$options = (object)array('noclean'=>true);
218echo format_module_intro('feedback', $feedback, $cm->id);
219echo $OUTPUT->box_end();
220
9c5bc7a5 221if (has_capability('mod/feedback:edititems', $context)) {
abd2899c
AG
222 require_once($CFG->libdir . '/filelib.php');
223
224 $page_after_submit_output = file_rewrite_pluginfile_urls($feedback->page_after_submit,
225 'pluginfile.php',
226 $context->id,
227 'mod_feedback',
228 'page_after_submit',
229 0);
230
846cebe5 231 echo $OUTPUT->heading(get_string("page_after_submit", "feedback"), 3);
84973212 232 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
abd2899c 233 echo format_text($page_after_submit_output,
9c5bc7a5
AG
234 $feedback->page_after_submitformat,
235 array('overflowdiv'=>true));
236
84973212 237 echo $OUTPUT->box_end();
238}
c70ad9f7 239
9c5bc7a5 240if ( (intval($feedback->publish_stats) == 1) AND
2396a414 241 ( has_capability('mod/feedback:viewanalysepage', $context)) AND
c987dbad 242 !( has_capability('mod/feedback:viewreports', $context)) ) {
9c5bc7a5
AG
243
244 $params = array('userid'=>$USER->id, 'feedback'=>$feedback->id);
245 if ($multiple_count = $DB->count_records('feedback_tracking', $params)) {
246 $url_params = array('id'=>$id, 'courseid'=>$courseid);
247 $analysisurl = new moodle_url('/mod/feedback/analysis.php', $url_params);
5dee2210 248 echo '<div class="mdl-align"><a href="'.$analysisurl->out().'">';
84973212 249 echo get_string('completed_feedbacks', 'feedback').'</a>';
250 echo '</div>';
c70ad9f7 251 }
84973212 252}
c70ad9f7 253
84973212 254//####### mapcourse-start
9c5bc7a5
AG
255if (has_capability('mod/feedback:mapcourse', $context)) {
256 if ($feedback->course == SITEID) {
84973212 257 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
258 echo '<div class="mdl-align">';
259 echo '<form action="mapcourse.php" method="get">';
9d092aa9 260 echo '<fieldset>';
84973212 261 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
262 echo '<input type="hidden" name="id" value="'.$id.'" />';
263 echo '<button type="submit">'.get_string('mapcourses', 'feedback').'</button>';
e8790d59 264 echo $OUTPUT->help_icon('mapcourse', 'feedback');
9d092aa9 265 echo '</fieldset>';
84973212 266 echo '</form>';
267 echo '<br />';
268 echo '</div>';
269 echo $OUTPUT->box_end();
da329996 270 }
84973212 271}
272//####### mapcourse-end
da329996 273
84973212 274//####### completed-start
9c5bc7a5 275if ($feedback_complete_cap) {
d4b1d58c 276 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
84973212 277 //check, whether the feedback is open (timeopen, timeclose)
278 $checktime = time();
9c5bc7a5
AG
279 if (($feedback->timeopen > $checktime) OR
280 ($feedback->timeclose < $checktime AND $feedback->timeclose > 0)) {
281
846cebe5 282 echo $OUTPUT->notification(get_string('feedback_is_not_open', 'feedback'));
9c5bc7a5 283 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
d4b1d58c 284 echo $OUTPUT->box_end();
84973212 285 echo $OUTPUT->footer();
286 exit;
c70ad9f7 287 }
d4b1d58c 288
84973212 289 //check multiple Submit
290 $feedback_can_submit = true;
9c5bc7a5
AG
291 if ($feedback->multiple_submit == 0 ) {
292 if (feedback_is_already_submitted($feedback->id, $courseid)) {
84973212 293 $feedback_can_submit = false;
c70ad9f7 294 }
295 }
9c5bc7a5 296 if ($feedback_can_submit) {
84973212 297 //if the user is not known so we cannot save the values temporarly
9c5bc7a5 298 if (!isloggedin() or isguestuser()) {
84973212 299 $completefile = 'complete_guest.php';
300 $guestid = sesskey();
9c5bc7a5 301 } else {
84973212 302 $completefile = 'complete.php';
303 $guestid = false;
c70ad9f7 304 }
9c5bc7a5
AG
305 $url_params = array('id'=>$id, 'courseid'=>$courseid, 'gopage'=>0);
306 $completeurl = new moodle_url('/mod/feedback/'.$completefile, $url_params);
2396a414 307
9c5bc7a5
AG
308 $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid, $guestid);
309 if ($feedbackcompletedtmp) {
310 if ($startpage = feedback_get_page_to_continue($feedback->id, $courseid, $guestid)) {
5dee2210 311 $completeurl->param('gopage', $startpage);
c70ad9f7 312 }
5dee2210 313 echo '<a href="'.$completeurl->out().'">'.get_string('continue_the_form', 'feedback').'</a>';
9c5bc7a5 314 } else {
5dee2210 315 echo '<a href="'.$completeurl->out().'">'.get_string('complete_the_form', 'feedback').'</a>';
84973212 316 }
9c5bc7a5 317 } else {
846cebe5 318 echo $OUTPUT->notification(get_string('this_feedback_is_already_submitted', 'feedback'));
9c5bc7a5 319 if ($courseid) {
84973212 320 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$courseid);
9c5bc7a5 321 } else {
84973212 322 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
c70ad9f7 323 }
c70ad9f7 324 }
84973212 325 echo $OUTPUT->box_end();
326}
327//####### completed-end
c70ad9f7 328
84973212 329/// Finish the page
330///////////////////////////////////////////////////////////////////////////
331///////////////////////////////////////////////////////////////////////////
332///////////////////////////////////////////////////////////////////////////
c70ad9f7 333
84973212 334echo $OUTPUT->footer();
c70ad9f7 335