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