MDL-22015 core_string_manager cache can be disabled by setting $CFG->disablelangdiskcache
[moodle.git] / mod / feedback / lib.php
CommitLineData
32be99dc 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Library of functions and constants for module feedback
20 * includes the main-part of feedback-functions
21 *
22 * @package mod-feedback
23 * @copyright Andreas Grabs
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27/** Include eventslib.php */
3b120e46 28require_once($CFG->libdir.'/eventslib.php');
9f42276c
SH
29/** Include calendar/lib.php */
30require_once($CFG->dirroot.'/calendar/lib.php');
3b120e46 31
c70ad9f7 32
c70ad9f7 33define('FEEDBACK_ANONYMOUS_YES', 1);
34define('FEEDBACK_ANONYMOUS_NO', 2);
35define('FEEDBACK_MIN_ANONYMOUS_COUNT_IN_GROUP', 2);
36define('FEEDBACK_DECIMAL', '.');
37define('FEEDBACK_THOUSAND', ',');
38define('FEEDBACK_RESETFORM_RESET', 'feedback_reset_data_');
39define('FEEDBACK_RESETFORM_DROP', 'feedback_drop_feedback_');
40define('FEEDBACK_MAX_PIX_LENGTH', '400'); //max. Breite des grafischen Balkens in der Auswertung
8a23b38c 41define('FEEDBACK_DEFAULT_PAGE_COUNT', 20);
c70ad9f7 42
29081d02 43/**
32be99dc 44 * @uses FEATURE_GROUPS
45 * @uses FEATURE_GROUPINGS
46 * @uses FEATURE_GROUPMEMBERSONLY
47 * @uses FEATURE_MOD_INTRO
48 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
49 * @uses FEATURE_GRADE_HAS_GRADE
50 * @uses FEATURE_GRADE_OUTCOMES
29081d02 51 * @param string $feature FEATURE_xx constant for requested feature
52 * @return mixed True if module supports feature, null if doesn't know
53 */
54function feedback_supports($feature) {
55 switch($feature) {
42f103be 56 case FEATURE_GROUPS: return true;
57 case FEATURE_GROUPINGS: return true;
58 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 59 case FEATURE_MOD_INTRO: return true;
29081d02 60 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
42f103be 61 case FEATURE_GRADE_HAS_GRADE: return false;
62 case FEATURE_GRADE_OUTCOMES: return false;
cb3dcd78 63 case FEATURE_BACKUP_MOODLE2: return true;
42f103be 64
29081d02 65 default: return null;
66 }
67}
3b120e46 68
c70ad9f7 69/**
32be99dc 70 * this will create a new instance and return the id number
71 * of the new instance.
72 *
73 * @global object
74 * @param object $feedback the object given by mod_feedback_mod_form
75 * @return int
76 */
c70ad9f7 77function feedback_add_instance($feedback) {
c18269c7 78 global $DB;
c70ad9f7 79
80 $feedback->timemodified = time();
81 $feedback->id = '';
82
83 //check if openenable and/or closeenable is set and set correctly to save in db
84 if(empty($feedback->openenable)) {
85 $feedback->timeopen = 0;
86 }
87 if(empty($feedback->closeenable)) {
88 $feedback->timeclose = 0;
89 }
c184660d 90 if(empty($feedback->site_after_submit)) {
91 $feedback->site_after_submit = '';
92 }
c70ad9f7 93
94 //saving the feedback in db
a8f3a651 95 $feedbackid = $DB->insert_record("feedback", $feedback);
37b86a39 96
97 $feedback->id = $feedbackid;
98
c70ad9f7 99 feedback_set_events($feedback);
37b86a39 100
c70ad9f7 101 return $feedbackid;
102}
103
104/**
32be99dc 105 * this will update a given instance
106 *
107 * @global object
108 * @param object $feedback the object given by mod_feedback_mod_form
109 * @return boolean
110 */
c70ad9f7 111function feedback_update_instance($feedback) {
c18269c7 112 global $DB;
c70ad9f7 113
114 $feedback->timemodified = time();
115 $feedback->id = $feedback->instance;
116
117 //check if openenable and/or closeenable is set and set correctly to save in db
118 if(empty($feedback->openenable)) {
119 $feedback->timeopen = 0;
120 }
121 if(empty($feedback->closeenable)) {
122 $feedback->timeclose = 0;
123 }
c184660d 124 if(empty($feedback->site_after_submit)) {
125 $feedback->site_after_submit = '';
126 }
c70ad9f7 127
128 //save the feedback into the db
9d749339 129 $DB->update_record("feedback", $feedback);
c70ad9f7 130
131 //create or update the new events
132 feedback_set_events($feedback);
37b86a39 133
c70ad9f7 134 return true;
135}
136
a59ff6b0
AG
137/**
138 * Serves the files included in feedback items like label. Implements needed access control ;-)
139 *
140 * @param object $course
141 * @param object $cminfo
142 * @param object $context
143 * @param string $filearea
144 * @param array $args
145 * @param bool $forcedownload
146 * @return bool false if file not found, does not return if found - justsend the file
147 */
148function feedback_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) {
149 global $CFG, $DB;
150
151 if (!$cminfo->uservisible) {
152 return false;
153 }
14832313
AG
154
155 if($filearea === 'feedback_template') {
156 $usedcontext = get_context_instance(CONTEXT_COURSE, $course->id);
157 }else {
158 $usedcontext = $context;
159 }
a59ff6b0 160
14832313 161 if ($filearea === 'feedback_item' OR $filearea === 'feedback_template') {
a59ff6b0
AG
162 $itemid = (int)array_shift($args);
163
164 if (!$cm = get_coursemodule_from_instance('feedback', $cminfo->instance, $course->id)) {
165 return false;
166 }
167
168 require_course_login($course, true, $cm);
169
170 if (!$item = $DB->get_record('feedback_item', array('id'=>$itemid))) {
171 return false;
172 }
173
174 if (!$feedback = $DB->get_record('feedback', array('id'=>$cminfo->instance))) {
175 return false;
176 }
177
178 if (!has_capability('mod/feedback:view', $context)) {
179 return false;
180 }
181
182 if ($item->feedback == $cminfo->instance) {
14832313 183 $filecontext = $usedcontext;
a59ff6b0
AG
184 } else {
185 return false;
186 }
187
188 $relativepath = '/'.implode('/', $args);
189 $fullpath = $filecontext->id.$filearea.$itemid.$relativepath;
190
191 $fs = get_file_storage();
192 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
193 return false;
194 }
195
196 // finally send the file
197 send_stored_file($file, 0, 0, true); // download MUST be forced - security!
198 }
199
200 return false;
201}
202
203
c70ad9f7 204/**
32be99dc 205 * this will delete a given instance.
206 * all referenced data also will be deleted
207 *
208 * @global object
209 * @param int $id the instanceid of feedback
210 * @return boolean
211 */
c70ad9f7 212function feedback_delete_instance($id) {
c18269c7 213 global $DB;
214
c70ad9f7 215 //get all referenced items
c18269c7 216 $feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$id));
37b86a39 217
c70ad9f7 218 //deleting all referenced items and values
219 if (is_array($feedbackitems)){
220 foreach($feedbackitems as $feedbackitem){
c18269c7 221 $DB->delete_records("feedback_value", array("item"=>$feedbackitem->id));
222 $DB->delete_records("feedback_valuetmp", array("item"=>$feedbackitem->id));
c70ad9f7 223 }
14832313
AG
224 if($delitems = $DB->get_records("feedback_item", array("feedback"=>$id))) {
225 foreach($delitems as $delitem) {
226 feedback_delete_item($delitem->id, false);
227 }
228 }
c70ad9f7 229 }
37b86a39 230
c70ad9f7 231 //deleting the referenced tracking data
c18269c7 232 $DB->delete_records('feedback_tracking', array('feedback'=>$id));
37b86a39 233
c70ad9f7 234 //deleting the completeds
c18269c7 235 $DB->delete_records("feedback_completed", array("feedback"=>$id));
37b86a39 236
c70ad9f7 237 //deleting the unfinished completeds
c18269c7 238 $DB->delete_records("feedback_completedtmp", array("feedback"=>$id));
37b86a39 239
c70ad9f7 240 //deleting old events
c18269c7 241 $DB->delete_records('event', array('modulename'=>'feedback', 'instance'=>$id));
242 return $DB->delete_records("feedback", array("id"=>$id));
c70ad9f7 243}
244
245/**
32be99dc 246 * this is called after deleting all instances if the course will be deleted.
247 * only templates have to be deleted
248 *
249 * @global object
250 * @param object $course
251 * @return boolean
252 */
c70ad9f7 253function feedback_delete_course($course) {
0085fff8 254 global $DB;
255
c70ad9f7 256 //delete all templates of given course
0085fff8 257 return $DB->delete_records('feedback_template', array('course'=>$course->id));
c70ad9f7 258}
259
260/**
32be99dc 261 * Return a small object with summary information about what a
262 * user has done with a given particular instance of this module
263 * Used for user activity reports.
264 * $return->time = the time they did it
265 * $return->info = a short text description
266 *
267 * @param object $course
268 * @param object $user
269 * @param object $mod
270 * @param object $feedback
271 * @return object
272 */
c70ad9f7 273function feedback_user_outline($course, $user, $mod, $feedback) {
0085fff8 274 return null;
c70ad9f7 275}
276
6133bd16 277/**
278 * Returns all users who has completed a specified feedback since a given time
279 * many thanks to Manolescu Dorel, who contributed these two functions
32be99dc 280 *
281 * @global object
282 * @global object
283 * @global object
284 * @global object
285 * @uses CONTEXT_MODULE
286 * @param array $activities Passed by reference
287 * @param int $index Passed by reference
288 * @param int $timemodified Timestamp
289 * @param int $courseid
290 * @param int $cmid
291 * @param int $userid
292 * @param int $groupid
293 * @return void
6133bd16 294 */
295function feedback_get_recent_mod_activity(&$activities, &$index, $timemodified, $courseid, $cmid, $userid="", $groupid="") {
296 global $CFG, $COURSE, $USER, $DB;
297
298 if ($COURSE->id == $courseid) {
299 $course = $COURSE;
300 } else {
76b7b592 301 $course = $DB->get_record('course', array('id'=>$courseid));
6133bd16 302 }
303
304 $modinfo =& get_fast_modinfo($course);
305
306 $cm = $modinfo->cms[$cmid];
69f6d0b4
AG
307
308 $sqlargs = array();
6133bd16 309
69f6d0b4
AG
310 $sql = " SELECT fk . * , fc . * , u.firstname, u.lastname, u.email, u.picture
311 FROM {feedback_completed} fc
312 JOIN {feedback} fk ON fk.id = fc.feedback
313 JOIN {user} u ON u.id = fc.userid ";
6133bd16 314
315 if ($groupid) {
69f6d0b4 316 $sql .= " JOIN {groups_members} gm ON gm.userid=u.id ";
6133bd16 317 }
69f6d0b4
AG
318
319 $sql .= " WHERE fc.timemodified > ? AND fk.id = ? ";
320 $sqlargs[] = $timemodified;
321 $sqlargs[] = $cm->instace;
d4b1d58c 322
69f6d0b4
AG
323 if ($userid) {
324 $sql .= " AND u.id = ? ";
325 $sqlargs[] = $userid;
6133bd16 326 }
327
69f6d0b4
AG
328 if ($groupid) {
329 $sql .= " AND gm.groupid = ? ";
330 $sqlargs[] = $groupid;
331 }
332
333 if (!$feedbackitems = $DB->get_records_sql($sql, $sqlargs)) {
334 return;
335 }
6133bd16 336
337 $cm_context = get_context_instance(CONTEXT_MODULE, $cm->id);
338 $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
339 $viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context);
340 $groupmode = groups_get_activity_groupmode($cm, $course);
341
342 if (is_null($modinfo->groups)) {
343 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
344 }
345
346 $aname = format_string($cm->name,true);
347 foreach ($feedbackitems as $feedbackitem) {
348 if ($feedbackitem->userid != $USER->id) {
d4b1d58c 349
350 if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
6133bd16 351 $usersgroups = groups_get_all_groups($course->id, $feedbackitem->userid, $cm->groupingid);
352 if (!is_array($usersgroups)) {
353 continue;
354 }
355 $usersgroups = array_keys($usersgroups);
356 $interset = array_intersect($usersgroups, $modinfo->groups[$cm->id]);
357 if (empty($intersect)) {
358 continue;
359 }
360 }
361 }
362
363 $tmpactivity = new object();
364
365 $tmpactivity->type = 'feedback';
366 $tmpactivity->cmid = $cm->id;
367 $tmpactivity->name = $aname;
368 $tmpactivity->sectionnum= $cm->sectionnum;
369 $tmpactivity->timestamp = $feedbackitem->timemodified;
d4b1d58c 370
6133bd16 371 $tmpactivity->content->feedbackid = $feedbackitem->id;
372 $tmpactivity->content->feedbackuserid = $feedbackitem->userid;
d4b1d58c 373
6133bd16 374 $tmpactivity->user->userid = $feedbackitem->userid;
375 $tmpactivity->user->fullname = fullname($feedbackitem, $viewfullnames);
376 $tmpactivity->user->picture = $feedbackitem->picture;
d4b1d58c 377
6133bd16 378 $activities[$index++] = $tmpactivity;
379 }
380
381 return;
382}
383
384/**
385 * Prints all users who has completed a specified feedback since a given time
386 * many thanks to Manolescu Dorel, who contributed these two functions
32be99dc 387 *
388 * @global object
389 * @param object $activity
390 * @param int $courseid
391 * @param string $detail
392 * @param array $modnames
393 * @return void Output is echo'd
6133bd16 394 */
395function feedback_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
e63f88c9 396 global $CFG, $OUTPUT;
6133bd16 397
398 echo '<table border="0" cellpadding="3" cellspacing="0" class="forum-recent">';
399
400 echo "<tr><td class=\"userpicture\" valign=\"top\">";
812dbaf7 401 echo $OUTPUT->user_picture($activity->user, array('courseid'=>$courseid));
6133bd16 402 echo "</td><td>";
403
404 if ($detail) {
405 $modname = $modnames[$activity->type];
406 echo '<div class="title">';
b5d0cafc 407 echo "<img src=\"" . $OUTPUT->pix_url('icon', $activity->type) . "\" ".
6133bd16 408 "class=\"icon\" alt=\"$modname\" />";
409 echo "<a href=\"$CFG->wwwroot/mod/feedback/view.php?id={$activity->cmid}\">{$activity->name}</a>";
410 echo '</div>';
411 }
d4b1d58c 412
78946b9b 413 echo '<div class="title">';
6133bd16 414 echo '</div>';
d4b1d58c 415
6133bd16 416 echo '<div class="user">';
417 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->userid}&amp;course=$courseid\">"
418 ."{$activity->user->fullname}</a> - ".userdate($activity->timestamp);
419 echo '</div>';
420
421 echo "</td></tr></table>";
422
423 return;
424}
425
426
c70ad9f7 427/**
32be99dc 428 * Print a detailed representation of what a user has done with
429 * a given particular instance of this module, for user activity reports.
d4b1d58c 430 *
32be99dc 431 * @param object $course
432 * @param object $user
433 * @param object $mod
434 * @param object $feedback
435 * @return bool
436 */
c70ad9f7 437function feedback_user_complete($course, $user, $mod, $feedback) {
c70ad9f7 438 return true;
439}
440
32be99dc 441/**
442 * @return bool true
443 */
c70ad9f7 444function feedback_cron () {
445 return true;
446}
447
32be99dc 448/**
449 * @return bool false
450 */
c70ad9f7 451function feedback_get_participants($feedbackid) {
c70ad9f7 452 return false;
453}
454
32be99dc 455
456/**
457 * @return bool false
458 */
c70ad9f7 459function feedback_scale_used ($feedbackid,$scaleid) {
0085fff8 460 return false;
c70ad9f7 461}
462
1c57ce25 463/**
464 * This function is used by the reset_course_userdata function in moodlelib.
465 * This function will remove all responses from the specified feedback
466 * and clean up any related data.
32be99dc 467 *
468 * @global object
469 * @global object
470 * @uses FEEDBACK_RESETFORM_RESET
471 * @uses FEEDBACK_RESETFORM_DROP
472 * @param object $data the data submitted from the reset course.
1c57ce25 473 * @return array status array
c70ad9f7 474 */
1c57ce25 475function feedback_reset_userdata($data) {
0085fff8 476 global $CFG, $DB;
37b86a39 477
c70ad9f7 478 $resetfeedbacks = array();
479 $dropfeedbacks = array();
1c57ce25 480 $status = array();
481 $componentstr = get_string('modulenameplural', 'feedback');
37b86a39 482
c70ad9f7 483 //get the relevant entries from $data
484 foreach($data as $key => $value) {
485 switch(true) {
486 case substr($key, 0, strlen(FEEDBACK_RESETFORM_RESET)) == FEEDBACK_RESETFORM_RESET:
487 if($value == 1) {
488 $templist = explode('_', $key);
489 if(isset($templist[3]))$resetfeedbacks[] = intval($templist[3]);
490 }
491 break;
492 case substr($key, 0, strlen(FEEDBACK_RESETFORM_DROP)) == FEEDBACK_RESETFORM_DROP:
493 if($value == 1) {
494 $templist = explode('_', $key);
495 if(isset($templist[3]))$dropfeedbacks[] = intval($templist[3]);
496 }
497 break;
498 }
499 }
37b86a39 500
c70ad9f7 501 //reset the selected feedbacks
502 foreach($resetfeedbacks as $id) {
0085fff8 503 $feedback = $DB->get_record('feedback', array('id'=>$id));
c70ad9f7 504 feedback_delete_all_completeds($id);
1c57ce25 505 $status[] = array('component'=>$componentstr.':'.$feedback->name, 'item'=>get_string('resetting_data','feedback'), 'error'=>false);
c70ad9f7 506 }
37b86a39 507
c70ad9f7 508 //drop the selected feedbacks
1c57ce25 509 // foreach($dropfeedbacks as $id) {
510 // $cm = get_coursemodule_from_instance('feedback', $id);
511 // feedback_delete_instance($id);
512 // feedback_delete_course_module($cm->id);
513 // $status[] = array('component'=>$componentstr, 'item'=>get_string('drop_feedback','feedback'), 'error'=>false);
514 // }
515 return $status;
516}
517
518/**
519 * Called by course/reset.php
32be99dc 520 *
521 * @global object
522 * @uses FEEDBACK_RESETFORM_RESET
523 * @param object $mform form passed by reference
1c57ce25 524 */
525function feedback_reset_course_form_definition(&$mform) {
0085fff8 526 global $COURSE, $DB;
527
1c57ce25 528 $mform->addElement('header', 'feedbackheader', get_string('modulenameplural', 'feedback'));
37b86a39 529
0085fff8 530 if(!$feedbacks = $DB->get_records('feedback', array('course'=>$COURSE->id), 'name')){
1c57ce25 531 return;
c70ad9f7 532 }
1c57ce25 533
534 $mform->addElement('static', 'hint', get_string('resetting_data','feedback'));
535 foreach($feedbacks as $feedback) {
536 $mform->addElement('checkbox', FEEDBACK_RESETFORM_RESET.$feedback->id, $feedback->name);
537 // $mform->addElement('checkbox', FEEDBACK_RESETFORM_DROP.$feedback->id, get_string('drop_feedback','feedback'));
538 }
539}
540
541/**
542 * Course reset form defaults.
32be99dc 543 *
544 * @global object
545 * @uses FEEDBACK_RESETFORM_RESET
546 * @param object $course
1c57ce25 547 */
548function feedback_reset_course_form_defaults($course) {
0085fff8 549 global $DB;
550
1c57ce25 551 $return = array();
0085fff8 552 if(!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id), 'name')){
1c57ce25 553 return;
554 }
555 foreach($feedbacks as $feedback) {
556 $return[FEEDBACK_RESETFORM_RESET.$feedback->id] = true;
557 // $return[FEEDBACK_RESETFORM_DROP.$feedback->id] = false;
558 }
559 return $return;
c70ad9f7 560}
561
37b86a39 562/**
c70ad9f7 563 * Called by course/reset.php and shows the formdata by coursereset.
564 * it prints checkboxes for each feedback available at the given course
565 * there are two checkboxes: 1) delete userdata and keep the feedback 2) delete userdata and drop the feedback
32be99dc 566 *
567 * @global object
568 * @uses FEEDBACK_RESETFORM_RESET
569 * @uses FEEDBACK_RESETFORM_DROP
c70ad9f7 570 * @param object $course
571 * @return void
572 */
573function feedback_reset_course_form($course) {
be96a472 574 global $DB, $OUTPUT;
0085fff8 575
c70ad9f7 576 echo get_string('resetting_feedbacks', 'feedback'); echo ':<br />';
0085fff8 577 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id), 'name')) {
578 return;
579 }
37b86a39 580
c70ad9f7 581 foreach($feedbacks as $feedback) {
582 echo '<p>';
583 echo get_string('name','feedback').': '.$feedback->name.'<br />';
2f0e96e4 584 echo html_writer::checkbox(FEEDBACK_RESETFORM_RESET.$feedback->id, 1, true, get_string('resetting_data','feedback'));
be96a472 585 echo '<br />';
2f0e96e4 586 echo html_writer::checkbox(FEEDBACK_RESETFORM_DROP.$feedback->id, 1, false, get_string('drop_feedback','feedback'));
c70ad9f7 587 echo '</p>';
588 }
589}
590
37b86a39 591/**
32be99dc 592 * This creates new events given as timeopen and closeopen by $feedback.
593 *
594 * @global object
595 * @param object $feedback
596 * @return void
c70ad9f7 597 */
598function feedback_set_events($feedback) {
c18269c7 599 global $DB;
600
c70ad9f7 601 // adding the feedback to the eventtable (I have seen this at quiz-module)
c18269c7 602 $DB->delete_records('event', array('modulename'=>'feedback', 'instance'=>$feedback->id));
c70ad9f7 603
50aa3fcc 604 if (!isset($feedback->coursemodule)) {
605 $cm = get_coursemodule_from_id('feedback', $feedback->id);
606 $feedback->coursemodule = $cm->id;
607 }
608
c70ad9f7 609 // the open-event
610 if($feedback->timeopen > 0) {
611 $event = NULL;
50aa3fcc 612 $event->name = get_string('start', 'feedback').' '.$feedback->name;
613 $event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
c70ad9f7 614 $event->courseid = $feedback->course;
615 $event->groupid = 0;
50aa3fcc 616 $event->userid = 0;
617 $event->modulename = 'feedback';
c18269c7 618 $event->instance = $feedback->id;
c70ad9f7 619 $event->eventtype = 'open';
620 $event->timestart = $feedback->timeopen;
621 $event->visible = instance_is_visible('feedback', $feedback);
622 if($feedback->timeclose > 0) {
623 $event->timeduration = ($feedback->timeclose - $feedback->timeopen);
624 } else {
625 $event->timeduration = 0;
626 }
37b86a39 627
9f42276c 628 calendar_event::create($event);
c70ad9f7 629 }
630
631 // the close-event
632 if($feedback->timeclose > 0) {
633 $event = NULL;
50aa3fcc 634 $event->name = get_string('stop', 'feedback').' '.$feedback->name;
635 $event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
c70ad9f7 636 $event->courseid = $feedback->course;
637 $event->groupid = 0;
50aa3fcc 638 $event->userid = 0;
639 $event->modulename = 'feedback';
c18269c7 640 $event->instance = $feedback->id;
c70ad9f7 641 $event->eventtype = 'close';
642 $event->timestart = $feedback->timeclose;
643 $event->visible = instance_is_visible('feedback', $feedback);
644 $event->timeduration = 0;
645
9f42276c 646 calendar_event::create($event);
c70ad9f7 647 }
648}
649
37b86a39 650/**
c70ad9f7 651 * this function is called by {@link feedback_delete_userdata()}
652 * it drops the feedback-instance from the course_module table
32be99dc 653 *
654 * @global object
c70ad9f7 655 * @param int $id the id from the coursemodule
656 * @return boolean
657 */
658function feedback_delete_course_module($id) {
0085fff8 659 global $DB;
660
661 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
c70ad9f7 662 return true;
663 }
0085fff8 664 return $DB->delete_records('course_modules', array('id'=>$cm->id));
c70ad9f7 665}
666
667
668
669////////////////////////////////////////////////
670//functions to handle capabilities
671////////////////////////////////////////////////
672
37b86a39 673/**
c70ad9f7 674 * returns the context-id related to the given coursemodule-id
32be99dc 675 *
676 * @staticvar object $context
c70ad9f7 677 * @param int $cmid the coursemodule-id
678 * @return object $context
679 */
680function feedback_get_context($cmid) {
681 static $context;
37b86a39 682
c70ad9f7 683 if(isset($context)) return $context;
37b86a39 684
c70ad9f7 685 if (!$context = get_context_instance(CONTEXT_MODULE, $cmid)) {
686 print_error('badcontext');
687 }
688 return $context;
689}
690
37b86a39 691/**
c70ad9f7 692 * returns true if the current role is faked by switching role feature
32be99dc 693 *
694 * @global object
c70ad9f7 695 * @return boolean
696 */
697function feedback_check_is_switchrole(){
698 global $USER;
699 if(isset($USER->switchrole) AND is_array($USER->switchrole) AND count($USER->switchrole) > 0) {
700 return true;
701 }
702 return false;
703}
704
d4b1d58c 705/**
248bafd6 706 * count users which have the completed a feedback
32be99dc 707 *
708 * @global object
709 * @uses CONTEXT_MODULE
710 * @param object $cm
711 * @param int $group single groupid
712 * @return object the userrecords
c70ad9f7 713 */
248bafd6
AG
714function feedback_count_complete_users($cm, $group = false) {
715 global $DB;
716
717 $params = array(FEEDBACK_ANONYMOUS_NO, $cm->instance);
718
719 $fromgroup = '';
720 $wheregroup = '';
721 if($group) {
722 $fromgroup = ', {groups_members} g';
723 $wheregroup = ' AND g.groupid = ? AND g.userid = c.userid';
724 $params[] = $group;
725 }
726
727 $sql = 'SELECT COUNT(u.id) FROM {user} u, {feedback_completed} c'.$fromgroup.'
728 WHERE anonymous_response = ? AND u.id = c.userid AND c.feedback = ?
729 '.$wheregroup;
730 ;
731
732 return $DB->count_records_sql($sql, $params);
733
734}
735
736/**
737 * get users which have the completed a feedback
738 *
739 * @global object
740 * @uses CONTEXT_MODULE
741 * @param object $cm
742 * @param int $group single groupid
743 * @param string $sort a table field
744 * @return object the userrecords
745 */
746function feedback_get_complete_users($cm, $group = false, $where, $sort = '', $startpage = false, $pagecount = false) {
a600ad35 747 global $DB;
d4b1d58c 748
a600ad35 749 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
c70ad9f7 750 print_error('badcontext');
751 }
37b86a39 752
afdb1920 753 $params = array(FEEDBACK_ANONYMOUS_NO, $cm->instance);
a600ad35 754
755 $fromgroup = '';
756 $wheregroup = '';
757 if($group) {
758 $fromgroup = ', {groups_members} g';
b68bff82 759 $wheregroup = ' AND g.groupid = ? AND g.userid = c.userid';
a600ad35 760 $params[] = $group;
761 }
248bafd6
AG
762
763 if($sort) {
764 $sortsql = ' ORDER BY '.$sort;
765 }else {
766 $sortsql = '';
767 }
768
184aa328 769 $sql = 'SELECT DISTINCT u.* FROM {user} u, {feedback_completed} c'.$fromgroup.'
248bafd6
AG
770 WHERE '.$where.' anonymous_response = ? AND u.id = c.userid AND c.feedback = ?
771 '.$wheregroup.$sortsql;
772 ;
773
774 if($startpage === false OR $pagecount === false) {
775 $startpage = false;
776 $pagecount = false;
777 }
778 return $DB->get_records_sql($sql, $params, $startpage, $pagecount);
c70ad9f7 779}
780
37b86a39 781/**
32be99dc 782 * get users which have the viewreports-capability
783 *
784 * @uses CONTEXT_MODULE
785 * @param int $cmid
786 * @param mixed $groups single groupid or array of groupids - group(s) user is in
787 * @return object the userrecords
c70ad9f7 788 */
789function feedback_get_viewreports_users($cmid, $groups = false) {
790
791 if (!$context = get_context_instance(CONTEXT_MODULE, $cmid)) {
792 print_error('badcontext');
793 }
37b86a39 794
c70ad9f7 795 //description of the call below: get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='', $limitnum='', $groups='', $exceptions='', $doanything=true)
796 return get_users_by_capability($context, 'mod/feedback:viewreports', '', 'lastname', '', '', $groups, '', false);
797}
798
37b86a39 799/**
32be99dc 800 * get users which have the receivemail-capability
801 *
802 * @uses CONTEXT_MODULE
803 * @param int $cmid
804 * @param mixed $groups single groupid or array of groupids - group(s) user is in
805 * @return object the userrecords
c184660d 806 */
807function feedback_get_receivemail_users($cmid, $groups = false) {
808
809 if (!$context = get_context_instance(CONTEXT_MODULE, $cmid)) {
810 print_error('badcontext');
811 }
37b86a39 812
c184660d 813 //description of the call below: get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='', $limitnum='', $groups='', $exceptions='', $doanything=true)
c65e59ba 814 return get_users_by_capability($context, 'mod/feedback:receivemail', '', 'lastname', '', '', $groups, '', false);
c184660d 815}
816
c70ad9f7 817////////////////////////////////////////////////
818//functions to handle the templates
819////////////////////////////////////////////////
820////////////////////////////////////////////////
821
37b86a39 822/**
32be99dc 823 * creates a new template-record.
824 *
825 * @global object
826 * @param int $courseid
827 * @param string $name the name of template shown in the templatelist
828 * @param int $ispublic 0:privat 1:public
829 * @return int the new templateid
c70ad9f7 830 */
831function feedback_create_template($courseid, $name, $ispublic = 0) {
0085fff8 832 global $DB;
833
834 $templ = new object();
835 $templ->course = $courseid;
836 $templ->name = $name;
c70ad9f7 837 $templ->ispublic = $ispublic;
0085fff8 838
14832313
AG
839 $templid = $DB->insert_record('feedback_template', $templ);
840 return $DB->get_record('feedback_template', array('id'=>$templid));
c70ad9f7 841}
842
37b86a39 843/**
32be99dc 844 * creates new template items.
845 * all items will be copied and the attribute feedback will be set to 0
846 * and the attribute template will be set to the new templateid
847 *
848 * @global object
849 * @param object $feedback
850 * @param string $name the name of template shown in the templatelist
851 * @param int $ispublic 0:privat 1:public
852 * @return boolean
c70ad9f7 853 */
854function feedback_save_as_template($feedback, $name, $ispublic = 0) {
0085fff8 855 global $DB;
14832313 856 $fs = get_file_storage();
0085fff8 857
858 if (!$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
c70ad9f7 859 return false;
860 }
37b86a39 861
0085fff8 862 if (!$newtempl = feedback_create_template($feedback->course, $name, $ispublic)) {
c70ad9f7 863 return false;
864 }
14832313
AG
865
866 //files in the template_item are in the context of the current course
867 //files in the feedback_item are in the feedback_context of the feedback
868 $c_context = get_context_instance(CONTEXT_COURSE, $newtempl->course);
869 $cm = get_coursemodule_from_instance('feedback', $feedback->id);
870 $f_context = get_context_instance(CONTEXT_MODULE, $cm->id);
871
c70ad9f7 872 //create items of this new template
873 foreach($feedbackitems as $item) {
14832313
AG
874 $t_item = clone($item);
875
876 unset($t_item->id);
877 $t_item->feedback = 0;
878 $t_item->template = $newtempl->id;
879 $t_item->id = $DB->insert_record('feedback_item', $t_item);
880 //copy all included files to the feedback_template filearea
881 if ($itemfiles = $fs->get_area_files($f_context->id, 'feedback_item', $item->id, "id", false)) {
882 foreach($itemfiles as $ifile) {
883 $file_record = new object();
884 $file_record->contextid = $c_context->id;
885 $file_record->filearea = 'course_summary';
886 $file_record->itemid = $t_item->id;
887 $fs->create_file_from_storedfile($file_record, $ifile);
888 }
889 }
c70ad9f7 890 }
891 return true;
892}
893
37b86a39 894/**
32be99dc 895 * deletes all feedback_items related to the given template id
896 *
897 * @global object
898 * @param int $id the templateid
899 * @return void
c70ad9f7 900 */
901function feedback_delete_template($id) {
0085fff8 902 global $DB;
903
14832313
AG
904 $template = $DB->get_record("feedback_template", array("id"=>$id));
905
906 //deleting the files from the item
907 $fs = get_file_storage();
908 $context = get_context_instance(CONTEXT_COURSE, $template->course);
909
910
911 if($t_items = $DB->get_records("feedback_item", array("template"=>$id))) {
912 foreach($t_items as $t_item) {
913 if ($templatefiles = $fs->get_area_files($context->id, 'course_summary', $t_item->id, "id", false)) {
914 $fs->delete_area_files($context->id, 'course_summary', $t_item->id);
915 }
916 }
917 }
0085fff8 918 $DB->delete_records("feedback_template", array("id"=>$id));
c70ad9f7 919}
920
37b86a39 921/**
32be99dc 922 * creates new feedback_item-records from template.
923 * if $deleteold is set true so the existing items of the given feedback will be deleted
924 * if $deleteold is set false so the new items will be appanded to the old items
925 *
926 * @global object
927 * @param object $feedback
928 * @param int $templateid
929 * @param boolean $deleteold
c70ad9f7 930 */
931function feedback_items_from_template($feedback, $templateid, $deleteold = false) {
0085fff8 932 global $DB;
14832313 933 $fs = get_file_storage();
0085fff8 934
c70ad9f7 935 //get all templateitems
0085fff8 936 if(!$templitems = $DB->get_records('feedback_item', array('template'=>$templateid))) {
c70ad9f7 937 return false;
938 }
37b86a39 939
14832313
AG
940 //files in the template_item are in the context of the current course
941 //files in the feedback_item are in the feedback_context of the feedback
942 $c_context = get_context_instance(CONTEXT_COURSE, $feedback->course);
943 $cm = get_coursemodule_from_instance('feedback', $feedback->id);
944 $f_context = get_context_instance(CONTEXT_MODULE, $cm->id);
945
c70ad9f7 946 //if deleteold then delete all old items before
947 //get all items
948 if($deleteold) {
0085fff8 949 if($feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
c70ad9f7 950 //delete all items of this feedback
951 foreach($feedbackitems as $item) {
952 feedback_delete_item($item->id, false);
953 }
954 //delete tracking-data
0085fff8 955 $DB->delete_records('feedback_tracking', array('feedback'=>$feedback->id));
956 $DB->delete_records('feedback_completed', array('feedback'=>$feedback->id));
957 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedback->id));
c70ad9f7 958 }
194dd279 959 $positionoffset = 0;
0085fff8 960 } else {
c70ad9f7 961 //if the old items are kept the new items will be appended
962 //therefor the new position has an offset
0085fff8 963 $positionoffset = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
c70ad9f7 964 }
37b86a39 965
14832313
AG
966 foreach($templitems as $t_item) {
967 $item = clone($t_item);
968 unset($item->id);
969 $item->feedback = $feedback->id;
970 $item->template = 0;
971 $item->position = $item->position + $positionoffset;
972
973 $item->id = $DB->insert_record('feedback_item', $item);
974
975 //TODO: moving the files to the new items
976 if ($templatefiles = $fs->get_area_files($c_context->id, 'course_summary', $t_item->id, "id", false)) {
977 foreach($templatefiles as $tfile) {
978 $file_record = new object();
979 $file_record->contextid = $f_context->id;
980 $file_record->filearea = 'feedback_item';
981 $file_record->itemid = $item->id;
982 $fs->create_file_from_storedfile($file_record, $tfile);
983 }
984 }
c70ad9f7 985 }
986}
987
37b86a39 988/**
32be99dc 989 * get the list of available templates.
990 * if the $onlyown param is set true so only templates from own course will be served
991 * this is important for droping templates
992 *
993 * @global object
994 * @param object $course
995 * @param boolean $onlyown
996 * @return array the template recordsets
c70ad9f7 997 */
998function feedback_get_template_list($course, $onlyown = false) {
0085fff8 999 global $DB;
1000
1001 if ($onlyown) {
1002 $templates = $DB->get_records('feedback_template', array('course'=>$course->id));
c70ad9f7 1003 } else {
0085fff8 1004 $templates = $DB->get_records_select('feedback_template', 'course = ? OR ispublic = 1', array($course->id));
c70ad9f7 1005 }
1006 return $templates;
1007}
1008
1009////////////////////////////////////////////////
1010//Handling der Items
1011////////////////////////////////////////////////
1012////////////////////////////////////////////////
1013
37b86a39 1014/**
32be99dc 1015 * load the available item plugins from given subdirectory of $CFG->dirroot
1016 * the default is "mod/feedback/item"
1017 *
1018 * @global object
1019 * @param string $dir the subdir
1020 * @return array pluginnames as string
c70ad9f7 1021 */
1022function feedback_load_feedback_items($dir = 'mod/feedback/item') {
1023 global $CFG;
1024 $names =get_list_of_plugins($dir);
1025 $ret_names = array();
1026
1027 foreach($names as $name) {
1028 require_once($CFG->dirroot.'/'.$dir.'/'.$name.'/lib.php');
1029 if(class_exists('feedback_item_'.$name)) {
1030 $ret_names[] = $name;
1031 }
1032 }
1033 return $ret_names;
1034}
1035
37b86a39 1036/**
32be99dc 1037 * load the available item plugins to use as dropdown-options
1038 *
1039 * @global object
1040 * @return array pluginnames as string
c70ad9f7 1041 */
1042function feedback_load_feedback_items_options() {
1043 global $CFG;
37b86a39 1044
c70ad9f7 1045 $feedback_options = array("pagebreak" => get_string('add_pagebreak', 'feedback'));
37b86a39 1046
c70ad9f7 1047 if (!$feedback_names = feedback_load_feedback_items('mod/feedback/item')) {
1048 return array();
1049 }
37b86a39 1050
c70ad9f7 1051 foreach($feedback_names as $fn) {
1052 $feedback_options[$fn] = get_string($fn,'feedback');
1053 }
1054 asort($feedback_options);
37b86a39 1055 $feedback_options = array_merge( array(' ' => get_string('select')), $feedback_options );
c70ad9f7 1056 return $feedback_options;
1057}
1058
37b86a39 1059/**
32be99dc 1060 * creates a new item-record
1061 *
1062 * @global object
1063 * @param object $data the data from edit_item_form
1064 * @return int the new itemid
c70ad9f7 1065 */
1066function feedback_create_item($data) {
0085fff8 1067 global $DB;
1068
c70ad9f7 1069 $item = new object;
0085fff8 1070 $item->feedback = $data->feedbackid;
c70ad9f7 1071
1072 $item->template=0;
1073 if (isset($data->templateid)) {
78946b9b 1074 $item->template = intval($data->templateid);
37b86a39 1075 }
c70ad9f7 1076
1077 $itemname = trim($data->itemname);
0085fff8 1078 $item->name = ($itemname ? $data->itemname : get_string('no_itemname', 'feedback'));
37b86a39 1079
d185ff44
SH
1080 if (!empty($data->itemlabel)) {
1081 $item->label = trim($data->itemlabel);
1082 } else {
1083 $item->label = get_string('no_itemlabel', 'feedback');
1084 }
b7a47958 1085
c70ad9f7 1086 //get the used class from item-typ
1087 $itemclass = 'feedback_item_'.$data->typ;
1088 //get the instance of the item class
1089 $itemobj = new $itemclass();
a59ff6b0
AG
1090 // $item->presentation = $itemobj->get_presentation($data);
1091 $item->presentation = ''; //the date comes from postupdate() of the itemobj
37b86a39 1092
c70ad9f7 1093 $item->hasvalue = $itemobj->get_hasvalue();
37b86a39 1094
c70ad9f7 1095 $item->typ = $data->typ;
1096 $item->position = $data->position;
1097
1098 $item->required=0;
ab5bd34d 1099 if (!empty($data->required)) {
1100 $item->required = $data->required;
37b86a39 1101 }
c70ad9f7 1102
a59ff6b0
AG
1103 $item->id = $DB->insert_record('feedback_item', $item);
1104
1105 //move all itemdata to the data
1106 $data->id = $item->id;
1107 $data->feedback = $item->feedback;
1108 $data->name = $item->name;
1109 $data->label = $item->label;
1110 $data->required = $item->required;
1111 return $itemobj->postupdate($data);
c70ad9f7 1112}
1113
37b86a39 1114/**
32be99dc 1115 * save the changes of a given item.
1116 *
1117 * @global object
1118 * @param object $item
32be99dc 1119 * @return boolean
c70ad9f7 1120 */
a59ff6b0 1121function feedback_update_item($item){
0085fff8 1122 global $DB;
0085fff8 1123 return $DB->update_record("feedback_item", $item);
c70ad9f7 1124}
1125
37b86a39 1126/**
32be99dc 1127 * deletes a item and also deletes all related values
1128 *
1129 * @global object
1130 * @param int $itemid
1131 * @param boolean $renumber should the kept items renumbered Yes/No
1132 * @return void
c70ad9f7 1133 */
1134function feedback_delete_item($itemid, $renumber = true){
0085fff8 1135 global $DB;
14832313
AG
1136
1137
0085fff8 1138 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
14832313
AG
1139
1140 //deleting the files from the item
1141 $fs = get_file_storage();
1142 if (!$cm = get_coursemodule_from_instance('feedback', $item->feedback)) {
1143 return false;
1144 }
1145 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1146
1147 if ($itemfiles = $fs->get_area_files($context->id, 'feedback_item', $item->id, "id", false)) {
1148 $fs->delete_area_files($context->id, 'feedback_item', $item->id);
1149 }
1150
0085fff8 1151 $DB->delete_records("feedback_value", array("item"=>$itemid));
1152 $DB->delete_records("feedback_valuetmp", array("item"=>$itemid));
1153 $DB->delete_records("feedback_item", array("id"=>$itemid));
c70ad9f7 1154 if($renumber) {
1155 feedback_renumber_items($item->feedback);
1156 }
1157}
1158
37b86a39 1159/**
32be99dc 1160 * deletes all items of the given feedbackid
1161 *
1162 * @global object
1163 * @param int $feedbackid
1164 * @return void
c70ad9f7 1165 */
1166function feedback_delete_all_items($feedbackid){
0085fff8 1167 global $DB;
1168
1169 if(!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid))) {
c70ad9f7 1170 return;
1171 }
1172 foreach($items as $item) {
1173 feedback_delete_item($item->id, false);
1174 }
0085fff8 1175 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedbackid));
1176 $DB->delete_records('feedback_completed', array('feedback'=>$feedbackid));
c70ad9f7 1177}
1178
37b86a39 1179/**
32be99dc 1180 * this function toggled the item-attribute required (yes/no)
1181 *
1182 * @global object
1183 * @param object $item
1184 * @return boolean
c70ad9f7 1185 */
1186function feedback_switch_item_required($item) {
0085fff8 1187 global $DB;
1188
c70ad9f7 1189 if($item->required == 1) {
1190 $item->required = 0;
1191 } else {
1192 $item->required = 1;
1193 }
0085fff8 1194 $item->name = $item->name;
1195 $item->presentation = $item->presentation;
1196 return $DB->update_record('feedback_item', $item);
c70ad9f7 1197}
1198
37b86a39 1199/**
32be99dc 1200 * renumbers all items of the given feedbackid
1201 *
1202 * @global object
1203 * @param int $feedbackid
1204 * @return void
c70ad9f7 1205 */
1206function feedback_renumber_items($feedbackid){
0085fff8 1207 global $DB;
1208
1209 $items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position');
c70ad9f7 1210 $pos = 1;
1211 if($items) {
1212 foreach($items as $item){
1213 $item->position = $pos;
1214 $pos++;
1215 feedback_update_item($item);
1216 }
1217 }
1218}
1219
37b86a39 1220/**
32be99dc 1221 * this decreases the position of the given item
1222 *
1223 * @global object
1224 * @param object $item
1225 * @return void
c70ad9f7 1226 */
1227function feedback_moveup_item($item){
0085fff8 1228 global $DB;
1229
a59ff6b0
AG
1230 if($item->position == 1) {
1231 return true;
1232 }
1233
1234 if(!$items = $DB->get_records('feedback_item', array('feedback'=>$item->feedback), 'position')) {
1235 return false;
1236 }
1237
1238 $itembefore = null;
1239 foreach($items as $i) {
1240 if($i->id == $item->id) {
1241 if(is_null($itembefore)) {
1242 return true;
1243 }
1244 $itembefore->position = $item->position;
1245 $item->position--;
1246 feedback_update_item($itembefore);
1247 feedback_update_item($item);
1248 feedback_renumber_items($item->feedback);
1249 return true;
1250 }
1251 $itembefore = $i;
1252 }
1253 return false;
c70ad9f7 1254}
1255
37b86a39 1256/**
32be99dc 1257 * this increased the position of the given item
1258 *
1259 * @global object
1260 * @param object $item
1261 * @return void
c70ad9f7 1262 */
1263function feedback_movedown_item($item){
0085fff8 1264 global $DB;
1265
a59ff6b0
AG
1266 if(!$items = $DB->get_records('feedback_item', array('feedback'=>$item->feedback), 'position')) {
1267 return false;
c70ad9f7 1268 }
a59ff6b0
AG
1269
1270 $movedownitem = null;
1271 foreach($items as $i) {
1272 if(!is_null($movedownitem) AND $movedownitem->id == $item->id) {
1273 $movedownitem->position = $i->position;
1274 $i->position--;
1275 feedback_update_item($movedownitem);
1276 feedback_update_item($i);
1277 feedback_renumber_items($item->feedback);
1278 return true;
1279 }
1280 $movedownitem = $i;
1281 }
1282 return false;
c70ad9f7 1283}
1284
37b86a39 1285/**
32be99dc 1286 * here the position of the given item will be set to the value in $pos
1287 *
1288 * @global object
1289 * @param object $moveitem
1290 * @param int $pos
1291 * @return boolean
c70ad9f7 1292 */
1293function feedback_move_item($moveitem, $pos){
0085fff8 1294 global $DB;
1295
0085fff8 1296 if (!$allitems = $DB->get_records('feedback_item', array('feedback'=>$moveitem->feedback), 'position')) {
c70ad9f7 1297 return false;
1298 }
0085fff8 1299 if (is_array($allitems)) {
c70ad9f7 1300 $index = 1;
1301 foreach($allitems as $item) {
c70ad9f7 1302 if($index == $pos) {
c70ad9f7 1303 $index++;
1304 }
a59ff6b0
AG
1305 if($item->id == $moveitem->id) {
1306 $moveitem->position = $pos;
1307 feedback_update_item($moveitem);
1308 continue;
1309 }
c70ad9f7 1310 $item->position = $index;
1311 feedback_update_item($item);
1312 $index++;
1313 }
c70ad9f7 1314 return true;
1315 }
1316 return false;
1317}
1318
9d5fbd65
AG
1319/**
1320 * prints the given item.
1321 * if $readonly is set true so the ouput only is for showing responses and not for editing or completing.
1322 * each item-class has an own print_item function implemented.
1323 *
1324 * @param object $item the item what we want to print out
1325 * @return void
1326 */
1327function feedback_print_item_preview($item){
1328 global $CFG;
1329 if($item->typ == 'pagebreak') {
1330 return;
1331 }
1332 //get the class of the given item-typ
1333 $itemclass = 'feedback_item_'.$item->typ;
1334 if (!class_exists($itemclass)) {
1335 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1336 }
1337 //get the instance of the item-class
1338 $itemobj = new $itemclass();
1339 $itemobj->print_item_preview($item);
1340}
1341
37b86a39 1342/**
32be99dc 1343 * prints the given item.
1344 * if $readonly is set true so the ouput only is for showing responses and not for editing or completing.
1345 * each item-class has an own print_item function implemented.
1346 *
1347 * @param object $item the item what we want to print out
1348 * @param mixed $value the value if $readonly is set true and we showing responses
32be99dc 1349 * @param boolean $highlightrequire if this set true and the value are false on completing so the item will be highlighted
1350 * @return void
c70ad9f7 1351 */
9d5fbd65 1352function feedback_print_item_complete($item, $value = false, $highlightrequire = false){
ab5bd34d 1353 global $CFG;
9d5fbd65
AG
1354 if($item->typ == 'pagebreak') {
1355 return;
1356 }
37b86a39 1357
c70ad9f7 1358 //get the class of the given item-typ
1359 $itemclass = 'feedback_item_'.$item->typ;
ab5bd34d 1360 if (!class_exists($itemclass)) {
1361 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1362 }
c70ad9f7 1363 //get the instance of the item-class
1364 $itemobj = new $itemclass();
9d5fbd65
AG
1365 $itemobj->print_item_complete($item, $value, $highlightrequire);
1366}
1367
1368/**
1369 * prints the given item.
1370 * if $readonly is set true so the ouput only is for showing responses and not for editing or completing.
1371 * each item-class has an own print_item function implemented.
1372 *
1373 * @param object $item the item what we want to print out
1374 * @param mixed $value the value if $readonly is set true and we showing responses
1375 * @return void
1376 */
1377function feedback_print_item_show_value($item, $value = false){
1378 global $CFG;
1379 if($item->typ == 'pagebreak') {
1380 return;
1381 }
1382
1383 //get the class of the given item-typ
1384 $itemclass = 'feedback_item_'.$item->typ;
1385 if (!class_exists($itemclass)) {
1386 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1387 }
1388 //get the instance of the item-class
1389 $itemobj = new $itemclass();
1390 $itemobj->print_item_show_value($item, $value);
c70ad9f7 1391}
1392
37b86a39 1393/**
32be99dc 1394 * if the user completes a feedback and there is a pagebreak so the values are saved temporary.
1395 * the values are saved permanently not until the user click on save button
1396 *
1397 * @global object
1398 * @param object $feedbackcompleted
1399 * @return object temporary saved completed-record
c70ad9f7 1400 */
0085fff8 1401function feedback_set_tmp_values($feedbackcompleted) {
1402 global $DB;
1403
c70ad9f7 1404 //first we create a completedtmp
1405 $tmpcpl = new object();
1406 foreach($feedbackcompleted as $key => $value) {
1407 $tmpcpl->{$key} = $value;
1408 }
1409 // $tmpcpl = $feedbackcompleted;
0085fff8 1410 unset($tmpcpl->id);
c70ad9f7 1411 $tmpcpl->timemodified = time();
7826abc7 1412 $tmpcpl->id = $DB->insert_record('feedback_completedtmp', $tmpcpl);
c70ad9f7 1413 //get all values of original-completed
0085fff8 1414 if(!$values = $DB->get_records('feedback_value', array('completed'=>$feedbackcompleted->id))) {
c70ad9f7 1415 return;
1416 }
1417 foreach($values as $value) {
0085fff8 1418 unset($value->id);
c70ad9f7 1419 $value->completed = $tmpcpl->id;
0085fff8 1420 $DB->insert_record('feedback_valuetmp', $value);
c70ad9f7 1421 }
1422 return $tmpcpl;
1423}
1424
37b86a39 1425/**
32be99dc 1426 * this saves the temporary saved values permanently
1427 *
1428 * @global object
1429 * @param object $feedbackcompletedtmp the temporary completed
1430 * @param object $feedbackcompleted the target completed
1431 * @param int $userid
1432 * @return int the id of the completed
c70ad9f7 1433 */
1434function feedback_save_tmp_values($feedbackcompletedtmp, $feedbackcompleted, $userid) {
0085fff8 1435 global $DB;
1436
c70ad9f7 1437 $tmpcplid = $feedbackcompletedtmp->id;
1438 if(!$feedbackcompleted) {
1439
1440 //first we create a completedtmp
1441 $newcpl = new object();
1442 foreach($feedbackcompletedtmp as $key => $value) {
1443 $newcpl->{$key} = $value;
1444 }
1445
0085fff8 1446 unset($newcpl->id);
c70ad9f7 1447 $newcpl->userid = $userid;
1448 $newcpl->timemodified = time();
7826abc7 1449 $newcpl->id = $DB->insert_record('feedback_completed', $newcpl);
c70ad9f7 1450 //get all values of tmp-completed
0085fff8 1451 if(!$values = $DB->get_records('feedback_valuetmp', array('completed'=>$feedbackcompletedtmp->id))) {
c70ad9f7 1452 return false;
1453 }
1454
1455 foreach($values as $value) {
0085fff8 1456 unset($value->id);
c70ad9f7 1457 $value->completed = $newcpl->id;
0085fff8 1458 $DB->insert_record('feedback_value', $value);
c70ad9f7 1459 }
1460 //drop all the tmpvalues
0085fff8 1461 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1462 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
c70ad9f7 1463 return $newcpl->id;
0085fff8 1464
1465 } else {
c70ad9f7 1466 //first drop all existing values
0085fff8 1467 $DB->delete_records('feedback_value', array('completed'=>$feedbackcompleted->id));
c70ad9f7 1468 //update the current completed
1469 $feedbackcompleted->timemodified = time();
0085fff8 1470 $DB->update_record('feedback_completed', $feedbackcompleted);
c70ad9f7 1471 //save all the new values from feedback_valuetmp
1472 //get all values of tmp-completed
0085fff8 1473 if(!$values = $DB->get_records('feedback_valuetmp', array('completed'=>$feedbackcompletedtmp->id))) {
c70ad9f7 1474 return false;
1475 }
1476 foreach($values as $value) {
0085fff8 1477 unset($value->id);
c70ad9f7 1478 $value->completed = $feedbackcompleted->id;
0085fff8 1479 $DB->insert_record('feedback_value', $value);
c70ad9f7 1480 }
1481 //drop all the tmpvalues
0085fff8 1482 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1483 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
c70ad9f7 1484 return $feedbackcompleted->id;
1485 }
1486}
1487
37b86a39 1488/**
32be99dc 1489 * deletes the given temporary completed and all related temporary values
1490 *
1491 * @global object
1492 * @param int $tmpcplid
1493 * @return void
c70ad9f7 1494 */
1495function feedback_delete_completedtmp($tmpcplid) {
0085fff8 1496 global $DB;
1497
1498 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1499 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
c70ad9f7 1500}
1501
1502////////////////////////////////////////////////
1503////////////////////////////////////////////////
1504////////////////////////////////////////////////
1505//functions to handle the pagebreaks
1506////////////////////////////////////////////////
1507
37b86a39 1508/**
32be99dc 1509 * this creates a pagebreak.
1510 * a pagebreak is a special kind of item
1511 *
1512 * @global object
1513 * @param int $feedbackid
1514 * @return mixed false if there already is a pagebreak on last position or the id of the pagebreak-item
c70ad9f7 1515 */
1516function feedback_create_pagebreak($feedbackid) {
0085fff8 1517 global $DB;
1518
c70ad9f7 1519 //check if there already is a pagebreak on the last position
0085fff8 1520 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
c70ad9f7 1521 if($lastposition == feedback_get_last_break_position($feedbackid)) {
1522 return false;
1523 }
37b86a39 1524
c70ad9f7 1525 $item = new object();
c70ad9f7 1526 $item->feedback = $feedbackid;
1527
1528 $item->template=0;
1529
1530 $item->name = '';
37b86a39 1531
c70ad9f7 1532 $item->presentation = '';
1533 $item->hasvalue = 0;
37b86a39 1534
c70ad9f7 1535 $item->typ = 'pagebreak';
1536 $item->position = $lastposition + 1;
1537
1538 $item->required=0;
1539
0085fff8 1540 return $DB->insert_record('feedback_item', $item);
c70ad9f7 1541}
1542
37b86a39 1543/**
32be99dc 1544 * get all positions of pagebreaks in the given feedback
1545 *
1546 * @global object
1547 * @param int $feedbackid
1548 * @return array all ordered pagebreak positions
c70ad9f7 1549 */
1550function feedback_get_all_break_positions($feedbackid) {
534792cd 1551 global $DB;
1552
1553 if(!$allbreaks = $DB->get_records_menu('feedback_item', array('typ'=>'pagebreak', 'feedback'=>$feedbackid), 'position', 'id, position')) return false;
c70ad9f7 1554 return array_values($allbreaks);
1555}
1556
37b86a39 1557/**
32be99dc 1558 * get the position of the last pagebreak
d4b1d58c 1559 *
32be99dc 1560 * @param int $feedbackid
1561 * @return int the position of the last pagebreak
c70ad9f7 1562 */
1563function feedback_get_last_break_position($feedbackid) {
1564 if(!$allbreaks = feedback_get_all_break_positions($feedbackid)) return false;
1565 return $allbreaks[count($allbreaks) - 1];
1566}
1567
37b86a39 1568/**
32be99dc 1569 * this returns the position where the user can continue the completing.
1570 *
1571 * @global object
1572 * @global object
1573 * @global object
1574 * @param int $feedbackid
1575 * @param int $courseid
1576 * @param string $guestid this id will be saved temporary and is unique
1577 * @return int the position to continue
c70ad9f7 1578 */
1579function feedback_get_page_to_continue($feedbackid, $courseid = false, $guestid) {
0085fff8 1580 global $CFG, $USER, $DB;
37b86a39 1581
c70ad9f7 1582 //is there any break?
37b86a39 1583
c70ad9f7 1584 if(!$allbreaks = feedback_get_all_break_positions($feedbackid)) return false;
0085fff8 1585
1586 $params = array();
c70ad9f7 1587 if($courseid) {
0085fff8 1588 $courseselect = "fv.course_id = :courseid";
1589 $params['courseid'] = $courseid;
c70ad9f7 1590 }else {
1591 $courseselect = "1";
1592 }
37b86a39 1593
c70ad9f7 1594 if($guestid) {
0085fff8 1595 $userselect = "AND fc.guestid = :guestid";
c70ad9f7 1596 $usergroup = "GROUP BY fc.guestid";
0085fff8 1597 $params['guestid'] = $guestid;
c70ad9f7 1598 }else {
0085fff8 1599 $userselect = "AND fc.userid = :userid";
c70ad9f7 1600 $usergroup = "GROUP BY fc.userid";
0085fff8 1601 $params['userid'] = $USER->id;
c70ad9f7 1602 }
0085fff8 1603
c70ad9f7 1604 $sql = "SELECT MAX(fi.position)
abf1c50f 1605 FROM {feedback_completedtmp} fc, {feedback_valuetmp} fv, {feedback_item} fi
0085fff8 1606 WHERE fc.id = fv.completed
1607 $userselect
1608 AND fc.feedback = :feedbackid
1609 AND $courseselect
c70ad9f7 1610 AND fi.id = fv.item
0085fff8 1611 $usergroup";
1612 $params['feedbackid'] = $feedbackid;
1613
1614 $lastpos = $DB->get_field_sql($sql, $params);
c70ad9f7 1615
1616 //the index of found pagebreak is the searched pagenumber
1617 foreach($allbreaks as $pagenr => $br) {
1618 if($lastpos < $br) return $pagenr;
1619 }
1620 return count($allbreaks);
1621}
1622
1623////////////////////////////////////////////////
1624////////////////////////////////////////////////
1625////////////////////////////////////////////////
1626//functions to handle the values
1627////////////////////////////////////////////////
1628
37b86a39 1629/**
32be99dc 1630 * this saves the values of an completed.
1631 * if the param $tmp is set true so the values are saved temporary in table feedback_valuetmp
1632 * if there is already a completed and the userid is set so the values are updated
1633 * on all other things new value records will be created
d4b1d58c 1634 *
32be99dc 1635 * @global object
32be99dc 1636 * @param int $userid
1637 * @param boolean $tmp
1638 * @return mixed false on error or the completeid
c70ad9f7 1639 */
6a471fe0 1640function feedback_save_values($usrid, $tmp = false) {
0085fff8 1641 global $DB;
6a471fe0
AG
1642
1643 $completedid = optional_param('completedid', false, PARAM_INT);
0085fff8 1644
c70ad9f7 1645 $tmpstr = $tmp ? 'tmp' : '';
6a471fe0
AG
1646 $time = time();
1647 $timemodified = mktime(0, 0, 0, date('m', $time),date('d', $time),date('Y', $time));
1648
c70ad9f7 1649 if($usrid == 0) {
6a471fe0 1650 return feedback_create_values($usrid, $timemodified, $tmp);
c70ad9f7 1651 }
6a471fe0
AG
1652 if(!$completedid or !$completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid))) {
1653 return feedback_create_values($usrid, $timemodified, $tmp);
c70ad9f7 1654 }else{
1655 $completed->timemodified = $timemodified;
6a471fe0 1656 return feedback_update_values($completed, $tmp);
c70ad9f7 1657 }
1658}
1659
37b86a39 1660/**
32be99dc 1661 * this saves the values from anonymous user such as guest on the main-site
1662 *
1663 * @global object
32be99dc 1664 * @param string $guestid the unique guestidentifier
1665 * @return mixed false on error or the completeid
c70ad9f7 1666 */
6a471fe0 1667function feedback_save_guest_values($guestid) {
0085fff8 1668 global $DB;
1669
6a471fe0
AG
1670 $completedid = optional_param('completedid', false, PARAM_INT);
1671
c70ad9f7 1672 $timemodified = time();
6a471fe0
AG
1673 if(!$completed = $DB->get_record('feedback_completedtmp', array('id'=>$completedid))) {
1674 return feedback_create_values(0, $timemodified, true, $guestid);
c70ad9f7 1675 }else {
1676 $completed->timemodified = $timemodified;
6a471fe0 1677 return feedback_update_values($completed, true);
c70ad9f7 1678 }
1679}
1680
37b86a39 1681/**
32be99dc 1682 * get the value from the given item related to the given completed.
1683 * the value can come as temporary or as permanently value. the deciding is done by $tmp
1684 *
1685 * @global object
1686 * @param int $completeid
1687 * @param int $itemid
1688 * @param boolean $tmp
1689 * @return mixed the value, the type depends on plugin-definition
c70ad9f7 1690 */
1691function feedback_get_item_value($completedid, $itemid, $tmp = false) {
0085fff8 1692 global $DB;
1693
c70ad9f7 1694 $tmpstr = $tmp ? 'tmp' : '';
0085fff8 1695 return $DB->get_field('feedback_value'.$tmpstr, 'value', array('completed'=>$completedid, 'item'=>$itemid));
c70ad9f7 1696}
1697
37b86a39 1698/**
32be99dc 1699 * this function checks the correctness of values.
1700 * the rules for this are implemented in the class of each item.
1701 * it can be the required attribute or the value self e.g. numeric.
1702 * the params first/lastitem are given to determine the visible range between pagebreaks.
1703 *
1704 * @global object
32be99dc 1705 * @param int $firstitem the position of firstitem for checking
1706 * @param int $lastitem the position of lastitem for checking
1707 * @return boolean
c70ad9f7 1708 */
6a471fe0 1709function feedback_check_values($firstitem, $lastitem) {
ab5bd34d 1710 global $DB, $CFG;
0085fff8 1711
6a471fe0
AG
1712 $feedbackid = optional_param('feedbackid', 0, PARAM_INT);
1713
c70ad9f7 1714 //get all items between the first- and lastitem
0085fff8 1715 $select = "feedback = ?
1716 AND position >= ?
1717 AND position <= ?
c70ad9f7 1718 AND hasvalue = 1";
6a471fe0 1719 $params = array($feedbackid, $firstitem, $lastitem);
0085fff8 1720 if(!$feedbackitems = $DB->get_records_select('feedback_item', $select, $params)) {
c70ad9f7 1721 //if no values are given so no values can be wrong ;-)
1722 return true;
1723 }
1724
1725 foreach($feedbackitems as $item) {
1726 //the name of the input field of the completeform is given in a special form:
1727 //<item-typ>_<item-id> eg. numeric_234
1728 //this is the key to get the value for the correct item
1729 $formvalname = $item->typ . '_' . $item->id;
6a471fe0
AG
1730
1731 $value = optional_param($formvalname, NULL, PARAM_RAW);
1732
c70ad9f7 1733 //check if the value is set
6a471fe0 1734 if(is_null($value) AND $item->required == 1) {
c70ad9f7 1735 return false;
1736 }
37b86a39 1737
c70ad9f7 1738 //get the class of the item-typ
1739 $itemclass = 'feedback_item_'.$item->typ;
ab5bd34d 1740
1741 if (!class_exists($itemclass)) {
1742 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1743 }
1744
c70ad9f7 1745 //get the instance of the item-class
1746 $itemobj = new $itemclass();
37b86a39 1747
c70ad9f7 1748 //now we let check the value by the item-class
1749 if(!$itemobj->check_value($value, $item)) {
1750 return false;
1751 }
1752 }
1753 //if no wrong values so we can return true
1754 return true;
1755}
1756
37b86a39 1757/**
32be99dc 1758 * this function create a complete-record and the related value-records.
1759 * depending on the $tmp (true/false) the values are saved temporary or permanently
1760 *
1761 * @global object
32be99dc 1762 * @param int $userid
1763 * @param int $timemodified
1764 * @param boolean $tmp
1765 * @param string $guestid a unique identifier to save temporary data
1766 * @return mixed false on error or the completedid
c70ad9f7 1767 */
6a471fe0 1768function feedback_create_values($usrid, $timemodified, $tmp = false, $guestid = false){
0085fff8 1769 global $DB;
1770
6a471fe0
AG
1771 $feedbackid = optional_param('feedbackid', false, PARAM_INT);
1772 $anonymous_response = optional_param('anonymous_response', false, PARAM_INT);
1773 $courseid = optional_param('courseid', false, PARAM_INT);
1774
c70ad9f7 1775 $tmpstr = $tmp ? 'tmp' : '';
1776 //first we create a new completed record
0085fff8 1777 $completed = new object();
6a471fe0 1778 $completed->feedback = $feedbackid;
0085fff8 1779 $completed->userid = $usrid;
1780 $completed->guestid = $guestid;
1781 $completed->timemodified = $timemodified;
6a471fe0 1782 $completed->anonymous_response = $anonymous_response;
37b86a39 1783
a8f3a651 1784 $completedid = $DB->insert_record('feedback_completed'.$tmpstr, $completed);
37b86a39 1785
0085fff8 1786 $completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid));
c70ad9f7 1787
6a471fe0 1788 //the keys are in the form like abc_xxx
c70ad9f7 1789 //with explode we make an array with(abc, xxx) and (abc=typ und xxx=itemnr)
6a471fe0
AG
1790
1791 //get the items of the feedback
1792 if(!$allitems = $DB->get_records('feedback_item', array('feedback'=>$completed->feedback))) {
1793 return false;
1794 }
1795 foreach($allitems as $item) {
1796 $keyname = $item->typ.'_'.$item->id;
1797 $itemvalue = optional_param($keyname, NULL, PARAM_RAW);
1798 if(is_null($itemvalue)) {
1799 continue;
c70ad9f7 1800 }
6a471fe0
AG
1801 $value = new object();
1802 $value->item = $item->id;
1803 $value->completed = $completed->id;
1804 $value->course_id = $courseid;
1805
1806 //get the class of item-typ
1807 $itemclass = 'feedback_item_'.$item->typ;
1808
1809 //get the instance of item-class
1810 if (!class_exists($itemclass)) {
1811 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1812 }
1813 $itemobj = new $itemclass();
1814
1815 //the kind of values can be absolutely different so we run create_value directly by the item-class
1816 $value->value = $itemobj->create_value($itemvalue);
1817 $DB->insert_record('feedback_value'.$tmpstr, $value);
c70ad9f7 1818 }
6a471fe0 1819 return $completed->id;
c70ad9f7 1820}
1821
37b86a39 1822/**
32be99dc 1823 * this function updates a complete-record and the related value-records.
1824 * depending on the $tmp (true/false) the values are saved temporary or permanently
1825 *
1826 * @global object
32be99dc 1827 * @param object $completed
1828 * @param boolean $tmp
1829 * @return int the completedid
c70ad9f7 1830 */
6a471fe0 1831function feedback_update_values($completed, $tmp = false) {
0085fff8 1832 global $DB;
6a471fe0 1833 $courseid = optional_param('courseid', false, PARAM_INT);
0085fff8 1834
c70ad9f7 1835 $tmpstr = $tmp ? 'tmp' : '';
37b86a39 1836
0085fff8 1837 $DB->update_record('feedback_completed'.$tmpstr, $completed);
c70ad9f7 1838 //get the values of this completed
0085fff8 1839 $values = $DB->get_records('feedback_value'.$tmpstr, array('completed'=>$completed->id));
37b86a39 1840
6a471fe0
AG
1841 //get the items of the feedback
1842 if(!$allitems = $DB->get_records('feedback_item', array('feedback'=>$completed->feedback))) {
1843 return false;
1844 }
1845 foreach($allitems as $item) {
1846 $keyname = $item->typ.'_'.$item->id;
1847 $itemvalue = optional_param($keyname, NULL, PARAM_RAW);
1848 //is the itemvalue set (could be a subset of items because pagebreak)?
1849 if(is_null($itemvalue)) {
1850 continue;
1851 }
1852
1853 $newvalue = new object();
1854 $newvalue->item = $item->id;
1855 $newvalue->completed = $completed->id;
1856 $newvalue->course_id = $courseid;
37b86a39 1857
6a471fe0
AG
1858 //get the class of item-typ
1859 $itemclass = 'feedback_item_'.$item->typ;
1860
1861 //get the instance of item-class
1862 if (!class_exists($itemclass)) {
1863 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1864 }
1865 $itemobj = new $itemclass();
1866 //the kind of values can be absolutely different so we run create_value directly by the item-class
1867 $newvalue->value = $itemobj->create_value($itemvalue);
1868
1869 //check, if we have to create or update the value
1870 $exist = false;
1871 foreach($values as $value){
1872 if($value->item == $newvalue->item){
1873 $newvalue->id = $value->id;
1874 $exist = true;
1875 break;
1876 }
1877 }
1878 if($exist){
1879 $DB->update_record('feedback_value'.$tmpstr, $newvalue);
1880 }else {
1881 $DB->insert_record('feedback_value'.$tmpstr, $newvalue);
c70ad9f7 1882 }
1883 }
1884
1885 return $completed->id;
1886}
1887
37b86a39 1888/**
32be99dc 1889 * get the values of an item depending on the given groupid.
1890 * if the feedback is anonymous so the values are shuffled
1891 *
1892 * @global object
1893 * @global object
1894 * @param object $item
1895 * @param int $groupid
1896 * @param int $courseid
1897 * @return array the value-records
c70ad9f7 1898 */
79473294 1899function feedback_get_group_values($item, $groupid = false, $courseid = false, $ignore_empty = false){
0085fff8 1900 global $CFG, $DB;
c70ad9f7 1901
1902 //if the groupid is given?
0085fff8 1903 if (intval($groupid) > 0) {
79473294
AG
1904 if($ignore_empty) {
1905 $ignore_empty_select = "AND fbc.value != ''";
1906 }
1907 else {
1908 $ignore_empty_select = "";
1909 }
1910
c70ad9f7 1911 $query = 'SELECT fbv . *
abf1c50f 1912 FROM {feedback_value} fbv, {feedback_completed} fbc, {groups_members} gm
0085fff8 1913 WHERE fbv.item = ?
37b86a39 1914 AND fbv.completed = fbc.id
1915 AND fbc.userid = gm.userid
79473294 1916 '.$ignore_empty_select.'
0085fff8 1917 AND gm.groupid = ?
1918 ORDER BY fbc.timemodified';
1919 $values = $DB->get_records_sql($query, array($item->id, $groupid));
1920
1921 } else {
79473294
AG
1922 if($ignore_empty) {
1923 $ignore_empty_select = "AND value != ''";
1924 }
1925 else {
1926 $ignore_empty_select = "";
1927 }
1928
c70ad9f7 1929 if ($courseid) {
79473294 1930 $values = $DB->get_records_select('feedback_value', "item = ? AND course_id = ? ".$ignore_empty_select, array($item->id, $courseid));
c70ad9f7 1931 } else {
79473294 1932 $values = $DB->get_records_select('feedback_value', "item = ? ".$ignore_empty_select, array($item->id));
c70ad9f7 1933 }
37b86a39 1934 }
0085fff8 1935 if ($DB->get_field('feedback', 'anonymous', array('id'=>$item->feedback)) == FEEDBACK_ANONYMOUS_YES) {
c70ad9f7 1936 if(is_array($values))
1937 shuffle($values);
1938 }
1939 return $values;
1940}
1941
37b86a39 1942/**
32be99dc 1943 * check for multiple_submit = false.
1944 * if the feedback is global so the courseid must be given
1945 *
1946 * @global object
1947 * @global object
1948 * @param int $feedbackid
1949 * @param int $courseid
1950 * @return boolean true if the feedback already is submitted otherwise false
c70ad9f7 1951 */
1952function feedback_is_already_submitted($feedbackid, $courseid = false) {
534792cd 1953 global $USER, $DB;
37b86a39 1954
534792cd 1955 if (!$trackings = $DB->get_records_menu('feedback_tracking', array('userid'=>$USER->id, 'feedback'=>$feedbackid), '', 'id, completed')) {
c70ad9f7 1956 return false;
1957 }
1958
1959 if($courseid) {
534792cd 1960 $select = 'completed IN ('.implode(',',$trackings).') AND course_id = ?';
1961 if(!$values = $DB->get_records_select('feedback_value', $select, array($courseid))) {
c70ad9f7 1962 return false;
1963 }
1964 }
1965
1966 return true;
1967}
1968
37b86a39 1969/**
32be99dc 1970 * if the completion of a feedback will be continued eg. by pagebreak or by multiple submit so the complete must be found.
1971 * if the param $tmp is set true so all things are related to temporary completeds
1972 *
1973 * @global object
1974 * @global object
1975 * @global object
1976 * @param int $feedbackid
1977 * @param boolean $tmp
1978 * @param int $courseid
1979 * @param string $guestid
1980 * @return int the id of the found completed
c70ad9f7 1981 */
1982function feedback_get_current_completed($feedbackid, $tmp = false, $courseid = false, $guestid = false) {
0085fff8 1983 global $USER, $CFG, $DB;
37b86a39 1984
c70ad9f7 1985 $tmpstr = $tmp ? 'tmp' : '';
37b86a39 1986
c70ad9f7 1987 if(!$courseid) {
1988 if($guestid) {
0085fff8 1989 return $DB->get_record('feedback_completed'.$tmpstr, array('feedback'=>$feedbackid, 'guestid'=>$guestid));
c70ad9f7 1990 }else {
0085fff8 1991 return $DB->get_record('feedback_completed'.$tmpstr, array('feedback'=>$feedbackid, 'userid'=>$USER->id));
c70ad9f7 1992 }
1993 }
37b86a39 1994
0085fff8 1995 $params = array();
37b86a39 1996
0085fff8 1997 if ($guestid) {
1998 $userselect = "AND fc.guestid = :guestid";
1999 $params['guestid'] = $guestid;
c70ad9f7 2000 }else {
0085fff8 2001 $userselect = "AND fc.userid = :userid";
2002 $params['userid'] = $USER->id;
c70ad9f7 2003 }
2004 //if courseid is set the feedback is global. there can be more than one completed on one feedback
31c0fa53 2005 $sql = "SELECT DISTINCT fc.*
abf1c50f 2006 FROM {feedback_value{$tmpstr}} fv, {feedback_completed{$tmpstr}} fc
0085fff8 2007 WHERE fv.course_id = :courseid
c70ad9f7 2008 AND fv.completed = fc.id
0085fff8 2009 $userselect
2010 AND fc.feedback = :feedbackid";
2011 $params['courseid'] = intval($courseid);
2012 $params['feedbackid'] = $feedbackid;
c70ad9f7 2013
d90e0ef3 2014 if (!$sqlresult = $DB->get_records_sql($sql, $params)) {
0085fff8 2015 return false;
2016 }
c70ad9f7 2017 foreach($sqlresult as $r) {
0085fff8 2018 return $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$r->id));
c70ad9f7 2019 }
2020}
2021
37b86a39 2022/**
32be99dc 2023 * get the completeds depending on the given groupid.
2024 *
2025 * @global object
2026 * @global object
2027 * @param object $feedback
2028 * @param int $groupid
2029 * @return mixed array of found completeds otherwise false
c70ad9f7 2030 */
59f89d80 2031function feedback_get_completeds_group($feedback, $groupid = false, $courseid = false) {
0085fff8 2032 global $CFG, $DB;
2033
2034 if (intval($groupid) > 0){
2035 $query = "SELECT fbc.*
abf1c50f 2036 FROM {feedback_completed} fbc, {groups_members} gm
0085fff8 2037 WHERE fbc.feedback = ?
2038 AND gm.groupid = ?
2039 AND fbc.userid = gm.userid";
2040 if ($values = $DB->get_records_sql($query, array($feedback->id, $groupid))) {
c70ad9f7 2041 return $values;
0085fff8 2042 } else {
2043 return false;
2044 }
2045 } else {
59f89d80 2046 if($courseid) {
2047 $query = "SELECT DISTINCT fbc.*
abf1c50f 2048 FROM {feedback_completed} fbc, {feedback_value} fbv
59f89d80 2049 WHERE fbc.id = fbv.completed
2050 AND fbc.feedback = ?
2051 AND fbv.course_id = ?
2052 ORDER BY random_response";
2053 if ($values = $DB->get_records_sql($query, array($feedback->id, $courseid))) {
2054 return $values;
2055 } else {
2056 return false;
2057 }
2058 }else {
2059 if ($values = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id))) {
2060 return $values;
2061 } else {
2062 return false;
2063 }
0085fff8 2064 }
c70ad9f7 2065 }
2066}
2067
37b86a39 2068/**
32be99dc 2069 * get the count of completeds depending on the given groupid.
2070 *
2071 * @global object
2072 * @global object
2073 * @param object $feedback
2074 * @param int $groupid
2075 * @param int $courseid
2076 * @return mixed count of completeds or false
c70ad9f7 2077 */
2078function feedback_get_completeds_group_count($feedback, $groupid = false, $courseid = false) {
0085fff8 2079 global $CFG, $DB;
2080
2081 if ($courseid > 0 AND !$groupid <= 0) {
2082 $sql = "SELECT id, COUNT(item) AS ci
37b86a39 2083 FROM {feedback_value}
0085fff8 2084 WHERE course_id = ?
2085 GROUP BY item ORDER BY ci DESC";
2086 if ($foundrecs = $DB->get_records_sql($sql, array($courseid))) {
c70ad9f7 2087 $foundrecs = array_values($foundrecs);
2088 return $foundrecs[0]->ci;
2089 }
2090 return false;
2091 }
2092 if($values = feedback_get_completeds_group($feedback, $groupid)) {
2093 return sizeof($values);
2094 }else {
2095 return false;
2096 }
2097}
2098
2099/* get the own groupid.
2100@param object $course
2101@param object $cm
2102function feedback_get_groupid($course, $cm) {
2103 $groupmode = groupmode($course, $cm);
37b86a39 2104
c70ad9f7 2105 //get groupid
e3c7f155 2106 if($groupmode > 0 && !has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
c70ad9f7 2107 if($mygroupid = mygroupid($course->id)) {
2108 return $mygroupid[0]; //get the first groupid
2109 }
2110 }else {
2111 return false;
2112 }
2113}
2114 */
2115
37b86a39 2116/**
32be99dc 2117 * deletes all completed-recordsets from a feedback.
2118 * all related data such as values also will be deleted
2119 *
2120 * @global object
2121 * @param int $feedbackid
2122 * @return void
c70ad9f7 2123 */
2124function feedback_delete_all_completeds($feedbackid) {
0085fff8 2125 global $DB;
2126
2127 if (!$completeds = $DB->get_records('feedback_completed', array('feedback'=>$feedbackid))) {
2128 return;
2129 }
c70ad9f7 2130 foreach($completeds as $completed) {
2131 feedback_delete_completed($completed->id);
2132 }
2133}
2134
37b86a39 2135/**
32be99dc 2136 * deletes a completed given by completedid.
2137 * all related data such values or tracking data also will be deleted
d4b1d58c 2138 *
32be99dc 2139 * @global object
2140 * @param int $completedid
2141 * @return boolean
c70ad9f7 2142 */
2143function feedback_delete_completed($completedid) {
0085fff8 2144 global $DB;
2145
2146 if (!$completed = $DB->get_record('feedback_completed', array('id'=>$completedid))) {
c70ad9f7 2147 return false;
2148 }
2149 //first we delete all related values
0085fff8 2150 $DB->delete_records('feedback_value', array('completed'=>$completed->id));
37b86a39 2151
c70ad9f7 2152 //now we delete all tracking data
0085fff8 2153 if($tracking = $DB->get_record('feedback_tracking', array('completed'=>$completed->id, 'feedback'=>$completed->feedback))) {
2154 $DB->delete_records('feedback_tracking', array('completed'=>$completed->id));
c70ad9f7 2155 }
37b86a39 2156
c70ad9f7 2157 //last we delete the completed-record
0085fff8 2158 return $DB->delete_records('feedback_completed', array('id'=>$completed->id));
c70ad9f7 2159}
2160
2161////////////////////////////////////////////////
2162////////////////////////////////////////////////
2163////////////////////////////////////////////////
2164//functions to handle sitecourse mapping
2165////////////////////////////////////////////////
2166
37b86a39 2167/**
32be99dc 2168 * checks if the course and the feedback is in the table feedback_sitecourse_map.
2169 *
2170 * @global object
2171 * @param int $feedbackid
2172 * @param int $courseid
2173 * @return int the count of records
c70ad9f7 2174 */
2175function feedback_is_course_in_sitecourse_map($feedbackid, $courseid) {
0085fff8 2176 global $DB;
2177 return $DB->count_records('feedback_sitecourse_map', array('feedbackid'=>$feedbackid, 'courseid'=>$courseid));
c70ad9f7 2178}
2179
37b86a39 2180/**
32be99dc 2181 * checks if the feedback is in the table feedback_sitecourse_map.
2182 *
2183 * @global object
2184 * @param int $feedbackid
2185 * @return boolean
c70ad9f7 2186 */
2187function feedback_is_feedback_in_sitecourse_map($feedbackid) {
0085fff8 2188 global $Db;
2189 return $DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$feedbackid));
c70ad9f7 2190}
2191
37b86a39 2192/**
32be99dc 2193 * gets the feedbacks from table feedback_sitecourse_map.
2194 * this is used to show the global feedbacks on the feedback block
2195 * all feedbacks with the following criteria will be selected:<br />
2196 *
2197 * 1) all feedbacks which id are listed together with the courseid in sitecoursemap and<br />
2198 * 2) all feedbacks which not are listed in sitecoursemap
2199 *
2200 * @global object
2201 * @param int $courseid
2202 * @return array the feedback-records
c70ad9f7 2203 */
2204function feedback_get_feedbacks_from_sitecourse_map($courseid) {
0085fff8 2205 global $DB;
37b86a39 2206
c70ad9f7 2207 //first get all feedbacks listed in sitecourse_map with named courseid
0085fff8 2208 $sql = "SELECT f.id AS id, cm.id AS cmid, f.name AS name, f.timeopen AS timeopen, f.timeclose AS timeclose
2209 FROM {feedback} f, {course_modules} cm, {feedback_sitecourse_map} sm, {modules} m
2210 WHERE f.id = cm.instance
2211 AND f.course = '".SITEID."'
37b86a39 2212 AND m.id = cm.module
0085fff8 2213 AND m.name = 'feedback'
37b86a39 2214 AND sm.courseid = ?
0085fff8 2215 AND sm.feedbackid = f.id";
37b86a39 2216
0085fff8 2217 if (!$feedbacks1 = $DB->get_records_sql($sql, array($courseid))) {
c70ad9f7 2218 $feedbacks1 = array();
2219 }
37b86a39 2220
c70ad9f7 2221 //second get all feedbacks not listed in sitecourse_map
2222 $feedbacks2 = array();
0085fff8 2223 $sql = "SELECT f.id AS id, cm.id AS cmid, f.name AS name, f.timeopen AS timeopen, f.timeclose AS timeclose
2224 FROM {feedback} f, {course_modules} cm, {modules} m
2225 WHERE f.id = cm.instance
2226 AND f.course = '".SITEID."'
2227 AND m.id = cm.module
2228 AND m.name = 'feedback'";
2229 if (!$allfeedbacks = $DB->get_records_sql($sql)) {
c70ad9f7 2230 $allfeedbacks = array();
2231 }
2232 foreach($allfeedbacks as $a) {
0085fff8 2233 if(!$DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$a->id))) {
c70ad9f7 2234 $feedbacks2[] = $a;
2235 }
2236 }
37b86a39 2237
c70ad9f7 2238 return array_merge($feedbacks1, $feedbacks2);
37b86a39 2239
c70ad9f7 2240}
2241
37b86a39 2242/**
32be99dc 2243 * gets the courses from table feedback_sitecourse_map.
2244 *
2245 * @global object
2246 * @param int $feedbackid
2247 * @return array the course-records
c70ad9f7 2248 */
2249function feedback_get_courses_from_sitecourse_map($feedbackid) {
0085fff8 2250 global $DB;
37b86a39 2251
0085fff8 2252 $sql = "SELECT f.id, f.courseid, c.fullname, c.shortname
2253 FROM {feedback_sitecourse_map} f, {course} c
2254 WHERE c.id = f.courseid
2255 AND f.feedbackid = ?
2256 ORDER BY c.fullname";
37b86a39 2257
0085fff8 2258 return $DB->get_records_sql($sql, array($feedbackid));
37b86a39 2259
c70ad9f7 2260}
2261
37b86a39 2262/**
32be99dc 2263 * removes non existing courses or feedbacks from sitecourse_map.
2264 * it shouldn't be called all too often
2265 * a good place for it could be the mapcourse.php or unmapcourse.php
d4b1d58c 2266 *
32be99dc 2267 * @global object
2268 * @return void
c70ad9f7 2269 */
2270function feedback_clean_up_sitecourse_map() {
0085fff8 2271 global $DB;
2272
2273 $maps = $DB->get_records('feedback_sitecourse_map');
c70ad9f7 2274 foreach($maps as $map) {
0085fff8 2275 if (!$DB->get_record('course', array('id'=>$map->courseid))) {
2276 $DB->delete_records('feedback_sitecourse_map', array('courseid'=>$map->courseid, 'feedbackid'=>$map->feedbackid));
c70ad9f7 2277 continue;
2278 }
0085fff8 2279 if (!$DB->get_record('feedback', array('id'=>$map->feedbackid))) {
2280 $DB->delete_records('feedback_sitecourse_map', array('courseid'=>$map->courseid, 'feedbackid'=>$map->feedbackid));
c70ad9f7 2281 continue;
2282 }
37b86a39 2283
c70ad9f7 2284 }
2285}
2286
2287////////////////////////////////////////////////
2288////////////////////////////////////////////////
2289////////////////////////////////////////////////
2290//not relatable functions
2291////////////////////////////////////////////////
2292
37b86a39 2293/**
32be99dc 2294 * prints the option items of a selection-input item (dropdownlist).
2295 * @param int $startval the first value of the list
2296 * @param int $endval the last value of the list
2297 * @param int $selectval which item should be selected
2298 * @param int $interval the stepsize from the first to the last value
2299 * @return void
c70ad9f7 2300 */
2301function feedback_print_numeric_option_list($startval, $endval, $selectval = '', $interval = 1){
2302 for($i = $startval; $i <= $endval; $i += $interval){
2303 if($selectval == ($i)){
2304 $selected = 'selected="selected"';
2305 }else{
2306 $selected = '';
2307 }
2308 echo '<option '.$selected.'>'.$i.'</option>';
2309 }
2310}
2311
37b86a39 2312/**
32be99dc 2313 * sends an email to the teachers of the course where the given feedback is placed.
2314 *
2315 * @global object
2316 * @global object
2317 * @uses FEEDBACK_ANONYMOUS_NO
2318 * @uses FORMAT_PLAIN
2319 * @param object $cm the coursemodule-record
2320 * @param object $feedback
2321 * @param object $course
2322 * @param int $userid
2323 * @return void
c70ad9f7 2324 */
c184660d 2325function feedback_send_email($cm, $feedback, $course, $userid) {
c6307ef2 2326 global $CFG, $DB;
37b86a39 2327
c70ad9f7 2328 if ($feedback->email_notification == 0) { // No need to do anything
2329 return;
2330 }
37b86a39 2331
0085fff8 2332 $user = $DB->get_record('user', array('id'=>$userid));
37b86a39 2333
84973212 2334 if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
2335 $groupmode = $cm->groupmode;
2336 } else {
2337 $groupmode = $course->groupmode;
2338 }
2339
2340 if ($groupmode == SEPARATEGROUPS) {
c6307ef2 2341 $groups = $DB->get_records_sql_menu("SELECT g.name, g.id
2342 FROM {groups} g, {groups_members} m
2343 WHERE g.courseid = ?
2344 AND g.id = m.groupid
2345 AND m.userid = ?
2346 ORDER BY name ASC", array($course->id, $userid));
c70ad9f7 2347 $groups = array_values($groups);
37b86a39 2348
c184660d 2349 $teachers = feedback_get_receivemail_users($cm->id, $groups);
c70ad9f7 2350 } else {
c184660d 2351 $teachers = feedback_get_receivemail_users($cm->id);
c70ad9f7 2352 }
37b86a39 2353
c70ad9f7 2354 if ($teachers) {
2355
2356 $strfeedbacks = get_string('modulenameplural', 'feedback');
2357 $strfeedback = get_string('modulename', 'feedback');
2358 $strcompleted = get_string('completed', 'feedback');
2359 $printusername = $feedback->anonymous == FEEDBACK_ANONYMOUS_NO ? fullname($user) : get_string('anonymous_user', 'feedback');
37b86a39 2360
c70ad9f7 2361 foreach ($teachers as $teacher) {
0085fff8 2362 $info = new object();
c70ad9f7 2363 $info->username = $printusername;
2364 $info->feedback = format_string($feedback->name,true);
2365 $info->url = $CFG->wwwroot.'/mod/feedback/show_entries.php?id='.$cm->id.'&userid='.$userid.'&do_show=showentries';
2366
2367 $postsubject = $strcompleted.': '.$info->username.' -> '.$feedback->name;
c184660d 2368 $posttext = feedback_send_email_text($info, $course);
2369 $posthtml = ($teacher->mailformat == 1) ? feedback_send_email_html($info, $course, $cm) : '';
37b86a39 2370
c70ad9f7 2371 if($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
3b120e46 2372 $eventdata = new object();
2373 $eventdata->modulename = 'feedback';
2374 $eventdata->userfrom = $user;
2375 $eventdata->userto = $teacher;
2376 $eventdata->subject = $postsubject;
2377 $eventdata->fullmessage = $posttext;
2378 $eventdata->fullmessageformat = FORMAT_PLAIN;
2379 $eventdata->fullmessagehtml = $posthtml;
2380 $eventdata->smallmessage = '';
7c7d3afa 2381 message_send($eventdata);
c70ad9f7 2382 }else {
3b120e46 2383 $eventdata = new object();
2384 $eventdata->modulename = 'feedback';
2385 $eventdata->userfrom = $teacher;
2386 $eventdata->userto = $teacher;
2387 $eventdata->subject = $postsubject;
2388 $eventdata->fullmessage = $posttext;
2389 $eventdata->fullmessageformat = FORMAT_PLAIN;
2390 $eventdata->fullmessagehtml = $posthtml;
2391 $eventdata->smallmessage = '';
7c7d3afa 2392 message_send($eventdata);
c70ad9f7 2393 }
2394 }
2395 }
2396}
2397
37b86a39 2398/**
32be99dc 2399 * sends an email to the teachers of the course where the given feedback is placed.
2400 *
2401 * @global object
2402 * @uses FORMAT_PLAIN
2403 * @param object $cm the coursemodule-record
2404 * @param object $feedback
2405 * @param object $course
2406 * @return void
c70ad9f7 2407 */
c184660d 2408function feedback_send_email_anonym($cm, $feedback, $course) {
c70ad9f7 2409 global $CFG;
37b86a39 2410
c70ad9f7 2411 if ($feedback->email_notification == 0) { // No need to do anything
2412 return;
2413 }
37b86a39 2414
c184660d 2415 $teachers = feedback_get_receivemail_users($cm->id);
c70ad9f7 2416
2417 if ($teachers) {
2418
2419 $strfeedbacks = get_string('modulenameplural', 'feedback');
2420 $strfeedback = get_string('modulename', 'feedback');
2421 $strcompleted = get_string('completed', 'feedback');
2422 $printusername = get_string('anonymous_user', 'feedback');
37b86a39 2423
c70ad9f7 2424 foreach ($teachers as $teacher) {
0085fff8 2425 $info = new object();
c70ad9f7 2426 $info->username = $printusername;
2427 $info->feedback = format_string($feedback->name,true);
2428 $info->url = $CFG->wwwroot.'/mod/feedback/show_entries_anonym.php?id='.$cm->id;
2429
2430 $postsubject = $strcompleted.': '.$info->username.' -> '.$feedback->name;
c184660d 2431 $posttext = feedback_send_email_text($info, $course);
2432 $posthtml = ($teacher->mailformat == 1) ? feedback_send_email_html($info, $course, $cm) : '';
37b86a39 2433
3b120e46 2434 $eventdata = new object();
2435 $eventdata->modulename = 'feedback';
2436 $eventdata->userfrom = $teacher;
2437 $eventdata->userto = $teacher;
2438 $eventdata->subject = $postsubject;
2439 $eventdata->fullmessage = $posttext;
2440 $eventdata->fullmessageformat = FORMAT_PLAIN;
2441 $eventdata->fullmessagehtml = $posthtml;
2442 $eventdata->smallmessage = '';
7c7d3afa 2443 message_send($eventdata);
c70ad9f7 2444 }
2445 }
2446}
2447
37b86a39 2448/**
32be99dc 2449 * send the text-part of the email
2450 *
2451 * @param object $info includes some infos about the feedback you want to send
2452 * @param object $course
2453 * @return string the text you want to post
c70ad9f7 2454 */
c184660d 2455function feedback_send_email_text($info, $course) {
c70ad9f7 2456 $posttext = $course->shortname.' -> '.get_string('modulenameplural', 'feedback').' -> '.
2457 $info->feedback."\n";
2458 $posttext .= '---------------------------------------------------------------------'."\n";
2459 $posttext .= get_string("emailteachermail", "feedback", $info)."\n";
2460 $posttext .= '---------------------------------------------------------------------'."\n";
2461 return $posttext;
2462}
2463
2464
37b86a39 2465/**
32be99dc 2466 * send the html-part of the email
2467 *
2468 * @global object
2469 * @param object $info includes some infos about the feedback you want to send
2470 * @param object $course
2471 * @return string the text you want to post
c70ad9f7 2472 */
c184660d 2473function feedback_send_email_html($info, $course, $cm) {
c70ad9f7 2474 global $CFG;
2475 $posthtml = '<p><font face="sans-serif">'.
5dee2210
AG
2476 '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->shortname.'</a> ->'.
2477 '<a href="'.$CFG->wwwroot.'/mod/feedback/index.php?id='.$course->id.'">'.get_string('modulenameplural', 'feedback').'</a> ->'.
2478 '<a href="'.$CFG->wwwroot.'/mod/feedback/view.php?id='.$cm->id.'">'.$info->feedback.'</a></font></p>';
c70ad9f7 2479 $posthtml .= '<hr /><font face="sans-serif">';
2480 $posthtml .= '<p>'.get_string('emailteachermailhtml', 'feedback', $info).'</p>';
2481 $posthtml .= '</font><hr />';
2482 return $posthtml;
2483}
2484
37b86a39 2485/**
32be99dc 2486 * print some errors to inform users about this.
2487 *
2488 * @global object
2489 * @return void
c70ad9f7 2490 */
2491function feedback_print_errors() {
37b86a39 2492
53b1f5da 2493 global $SESSION, $OUTPUT;
37b86a39 2494
f1b9e2df 2495 feedback_init_feedback_session();
2496
c70ad9f7 2497 if(empty($SESSION->feedback->errors)) {
78946b9b 2498 return;
c70ad9f7 2499 }
2500
d4b1d58c 2501 echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter boxwidthnormal');
53b1f5da 2502 echo $OUTPUT->heading(get_string('handling_error', 'feedback'));
c70ad9f7 2503
2504 echo '<p align="center"><b><font color="black"><pre>';
2505 print_r($SESSION->feedback->errors) . "\n";
2506 echo '</pre></font></b></p>';
37b86a39 2507
d4b1d58c 2508 echo $OUTPUT->box_end();
c70ad9f7 2509 echo '<br /><br />';
2510 $SESSION->feedback->errors = array(); //remove errors
37b86a39 2511}
c70ad9f7 2512
32be99dc 2513/**
2514 * @param string $url
2515 * @return string
2516 */
c184660d 2517function feedback_encode_target_url($url) {
2518 if (strpos($url, '?')) {
2519 list($part1, $part2) = explode('?', $url, 2); //maximal 2 parts
2520 return $part1 . '?' . htmlentities($part2);
2521 } else {
2522 return $url;
2523 }
2524}
ab5bd34d 2525
0b29477b
SH
2526/**
2527 * Adds module specific settings to the settings block
2528 *
2529 * @param settings_navigation $settings The settings navigation object
2530 * @param navigation_node $feedbacknode The node to add module settings to
2531 */
2532function feedback_extend_settings_navigation(settings_navigation $settings, navigation_node $feedbacknode) {
2533 global $PAGE, $DB;
ab5bd34d 2534
c987dbad 2535 if (!$context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id)) {
3406acde 2536 print_error('badcontext');
c987dbad
AG
2537 }
2538
3406acde
SH
2539 if (has_capability('mod/feedback:edititems', $context)) {
2540 $questionnode = $feedbacknode->add(get_string('questions', 'feedback'));
2541 $questionnode->add(get_string('edit_items', 'feedback'), new moodle_url('/mod/feedback/edit.php', array('id'=>$PAGE->cm->id, 'do_show'=>'edit')));
2542 $questionnode->add(get_string('export_questions', 'feedback'), new moodle_url('/mod/feedback/export.php', array('id'=>$PAGE->cm->id, 'action'=>'exportfile')));
2543 $questionnode->add(get_string('import_questions', 'feedback'), new moodle_url('/mod/feedback/import.php', array('id'=>$PAGE->cm->id)));
2544 $questionnode->add(get_string('templates', 'feedback'), new moodle_url('/mod/feedback/edit.php', array('id'=>$PAGE->cm->id, 'do_show'=>'templates')));
ab5bd34d 2545 }
2546
3406acde 2547 if (has_capability('mod/feedback:viewreports', $context)) {
0b29477b 2548 $feedback = $DB->get_record('feedback', array('id'=>$PAGE->cm->instance));
ab5bd34d 2549 if($feedback->course == SITEID){
0b29477b 2550 $feedbacknode->add(get_string('analysis', 'feedback'), new moodle_url('/mod/feedback/analysis_course.php', array('id'=>$PAGE->cm->id, 'course'=>$PAGE->course->id,'do_show'=>'analysis')));
ab5bd34d 2551 }else {
0b29477b 2552 $feedbacknode->add(get_string('analysis', 'feedback'), new moodle_url('/mod/feedback/analysis.php', array('id'=>$PAGE->cm->id, 'course'=>$PAGE->course->id,'do_show'=>'analysis')));
ab5bd34d 2553 }
ab5bd34d 2554
0b29477b 2555 $feedbacknode->add(get_string('show_entries', 'feedback'), new moodle_url('/mod/feedback/show_entries.php', array('id'=>$PAGE->cm->id, 'do_show'=>'showentries')));
ab5bd34d 2556 }
c987dbad 2557}
f1b9e2df 2558
2559function feedback_init_feedback_session() {
2560 //initialize the feedback-Session - not nice at all!!
2561 global $SESSION;
2562 if (!empty($SESSION)) {
2563 if (!isset($SESSION->feedback) OR !is_object($SESSION->feedback)) {
2564 $SESSION->feedback = new object();
2565 }
2566 }
ab5bd34d 2567}