"MDL-22677, delete dependent comments and ratings in glossary module"
[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
8d7b8286
AG
705/**
706 * count users which have not completed the feedback
707 *
708 * @global object
709 * @uses CONTEXT_MODULE
710 * @param object $cm
711 * @param int $group single groupid
712 * @return object the userrecords
713 */
714function feedback_get_incomplete_users($cm, $group = false, $sort = '', $startpage = false, $pagecount = false) {
715 global $DB;
716
717 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
718
719 //first get all user who can complete this feedback
720 $cap = 'mod/feedback:complete';
721 $fields = 'u.id, u.username';
722 if(!$allusers = get_users_by_capability($context, $cap, $fields, $sort, '', '', $group, '', true)) {
723 return false;
724 }
725 $allusers = array_keys($allusers);
726
727 //now get all completeds
728 if(!$completedusers = $DB->get_records_menu('feedback_completed', array('feedback'=>$cm->instance), '', 'userid,id')){
729 return false;
730 }
731 $completedusers = array_keys($completedusers);
732
733 //now strike all completedusers from allusers
734 $allusers = array_diff($allusers, $completedusers);
735
736 //for paging I use array_slice()
737 if($startpage !== false AND $pagecount !== false) {
738 $allusers = array_slice($allusers, $startpage, $pagecount);
739 }
740
741 return $allusers;
742}
743
744/**
745 * count users which have not completed the feedback
746 *
747 * @global object
748 * @uses CONTEXT_MODULE
749 * @param object $cm
750 * @param int $group single groupid
751 * @return object the userrecords
752 */
753function feedback_count_incomplete_users($cm, $group = false) {
754 if($allusers = feedback_get_incomplete_users($cm, $group)) {
755 return count($allusers);
756 }
757 return 0;
758}
759
d4b1d58c 760/**
248bafd6 761 * count users which have the completed a feedback
32be99dc 762 *
763 * @global object
764 * @uses CONTEXT_MODULE
765 * @param object $cm
766 * @param int $group single groupid
767 * @return object the userrecords
c70ad9f7 768 */
248bafd6
AG
769function feedback_count_complete_users($cm, $group = false) {
770 global $DB;
771
772 $params = array(FEEDBACK_ANONYMOUS_NO, $cm->instance);
773
774 $fromgroup = '';
775 $wheregroup = '';
776 if($group) {
777 $fromgroup = ', {groups_members} g';
778 $wheregroup = ' AND g.groupid = ? AND g.userid = c.userid';
779 $params[] = $group;
780 }
781
782 $sql = 'SELECT COUNT(u.id) FROM {user} u, {feedback_completed} c'.$fromgroup.'
783 WHERE anonymous_response = ? AND u.id = c.userid AND c.feedback = ?
784 '.$wheregroup;
785 ;
786
787 return $DB->count_records_sql($sql, $params);
788
789}
790
791/**
792 * get users which have the completed a feedback
793 *
794 * @global object
795 * @uses CONTEXT_MODULE
796 * @param object $cm
797 * @param int $group single groupid
798 * @param string $sort a table field
799 * @return object the userrecords
800 */
801function feedback_get_complete_users($cm, $group = false, $where, $sort = '', $startpage = false, $pagecount = false) {
a600ad35 802 global $DB;
d4b1d58c 803
a600ad35 804 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
c70ad9f7 805 print_error('badcontext');
806 }
37b86a39 807
afdb1920 808 $params = array(FEEDBACK_ANONYMOUS_NO, $cm->instance);
a600ad35 809
810 $fromgroup = '';
811 $wheregroup = '';
812 if($group) {
813 $fromgroup = ', {groups_members} g';
b68bff82 814 $wheregroup = ' AND g.groupid = ? AND g.userid = c.userid';
a600ad35 815 $params[] = $group;
816 }
248bafd6
AG
817
818 if($sort) {
819 $sortsql = ' ORDER BY '.$sort;
820 }else {
821 $sortsql = '';
822 }
823
184aa328 824 $sql = 'SELECT DISTINCT u.* FROM {user} u, {feedback_completed} c'.$fromgroup.'
248bafd6
AG
825 WHERE '.$where.' anonymous_response = ? AND u.id = c.userid AND c.feedback = ?
826 '.$wheregroup.$sortsql;
827 ;
828
829 if($startpage === false OR $pagecount === false) {
830 $startpage = false;
831 $pagecount = false;
832 }
833 return $DB->get_records_sql($sql, $params, $startpage, $pagecount);
c70ad9f7 834}
835
37b86a39 836/**
32be99dc 837 * get users which have the viewreports-capability
838 *
839 * @uses CONTEXT_MODULE
840 * @param int $cmid
841 * @param mixed $groups single groupid or array of groupids - group(s) user is in
842 * @return object the userrecords
c70ad9f7 843 */
844function feedback_get_viewreports_users($cmid, $groups = false) {
845
846 if (!$context = get_context_instance(CONTEXT_MODULE, $cmid)) {
847 print_error('badcontext');
848 }
37b86a39 849
c70ad9f7 850 //description of the call below: get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='', $limitnum='', $groups='', $exceptions='', $doanything=true)
851 return get_users_by_capability($context, 'mod/feedback:viewreports', '', 'lastname', '', '', $groups, '', false);
852}
853
37b86a39 854/**
32be99dc 855 * get users which have the receivemail-capability
856 *
857 * @uses CONTEXT_MODULE
858 * @param int $cmid
859 * @param mixed $groups single groupid or array of groupids - group(s) user is in
860 * @return object the userrecords
c184660d 861 */
862function feedback_get_receivemail_users($cmid, $groups = false) {
863
864 if (!$context = get_context_instance(CONTEXT_MODULE, $cmid)) {
865 print_error('badcontext');
866 }
37b86a39 867
c184660d 868 //description of the call below: get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='', $limitnum='', $groups='', $exceptions='', $doanything=true)
c65e59ba 869 return get_users_by_capability($context, 'mod/feedback:receivemail', '', 'lastname', '', '', $groups, '', false);
c184660d 870}
871
c70ad9f7 872////////////////////////////////////////////////
873//functions to handle the templates
874////////////////////////////////////////////////
875////////////////////////////////////////////////
876
37b86a39 877/**
32be99dc 878 * creates a new template-record.
879 *
880 * @global object
881 * @param int $courseid
882 * @param string $name the name of template shown in the templatelist
883 * @param int $ispublic 0:privat 1:public
884 * @return int the new templateid
c70ad9f7 885 */
886function feedback_create_template($courseid, $name, $ispublic = 0) {
0085fff8 887 global $DB;
888
889 $templ = new object();
890 $templ->course = $courseid;
891 $templ->name = $name;
c70ad9f7 892 $templ->ispublic = $ispublic;
0085fff8 893
14832313
AG
894 $templid = $DB->insert_record('feedback_template', $templ);
895 return $DB->get_record('feedback_template', array('id'=>$templid));
c70ad9f7 896}
897
37b86a39 898/**
32be99dc 899 * creates new template items.
900 * all items will be copied and the attribute feedback will be set to 0
901 * and the attribute template will be set to the new templateid
902 *
903 * @global object
904 * @param object $feedback
905 * @param string $name the name of template shown in the templatelist
906 * @param int $ispublic 0:privat 1:public
907 * @return boolean
c70ad9f7 908 */
909function feedback_save_as_template($feedback, $name, $ispublic = 0) {
0085fff8 910 global $DB;
14832313 911 $fs = get_file_storage();
0085fff8 912
913 if (!$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
c70ad9f7 914 return false;
915 }
37b86a39 916
0085fff8 917 if (!$newtempl = feedback_create_template($feedback->course, $name, $ispublic)) {
c70ad9f7 918 return false;
919 }
14832313
AG
920
921 //files in the template_item are in the context of the current course
922 //files in the feedback_item are in the feedback_context of the feedback
923 $c_context = get_context_instance(CONTEXT_COURSE, $newtempl->course);
924 $cm = get_coursemodule_from_instance('feedback', $feedback->id);
925 $f_context = get_context_instance(CONTEXT_MODULE, $cm->id);
926
c70ad9f7 927 //create items of this new template
73043833
AG
928 //depend items we are storing temporary in an mapping list array(new id => dependitem)
929 //we also store a mapping of all items array(oldid => newid)
930 $dependitemsmap = array();
931 $itembackup = array();
c70ad9f7 932 foreach($feedbackitems as $item) {
73043833 933
14832313
AG
934 $t_item = clone($item);
935
936 unset($t_item->id);
937 $t_item->feedback = 0;
938 $t_item->template = $newtempl->id;
939 $t_item->id = $DB->insert_record('feedback_item', $t_item);
940 //copy all included files to the feedback_template filearea
941 if ($itemfiles = $fs->get_area_files($f_context->id, 'feedback_item', $item->id, "id", false)) {
942 foreach($itemfiles as $ifile) {
943 $file_record = new object();
944 $file_record->contextid = $c_context->id;
945 $file_record->filearea = 'course_summary';
946 $file_record->itemid = $t_item->id;
947 $fs->create_file_from_storedfile($file_record, $ifile);
948 }
949 }
73043833
AG
950
951 $itembackup[$item->id] = $t_item->id;
952 if($t_item->dependitem) {
953 $dependitemsmap[$t_item->id] = $t_item->dependitem;
954 }
955
c70ad9f7 956 }
73043833
AG
957
958 //remapping the dependency
959 foreach($dependitemsmap as $key => $dependitem) {
960 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
961 $newitem->dependitem = $itembackup[$newitem->dependitem];
962 $DB->update_record('feedback_item', $newitem);
963 }
964
c70ad9f7 965 return true;
966}
967
37b86a39 968/**
32be99dc 969 * deletes all feedback_items related to the given template id
970 *
971 * @global object
972 * @param int $id the templateid
973 * @return void
c70ad9f7 974 */
975function feedback_delete_template($id) {
0085fff8 976 global $DB;
977
14832313
AG
978 $template = $DB->get_record("feedback_template", array("id"=>$id));
979
980 //deleting the files from the item
981 $fs = get_file_storage();
982 $context = get_context_instance(CONTEXT_COURSE, $template->course);
983
984
985 if($t_items = $DB->get_records("feedback_item", array("template"=>$id))) {
986 foreach($t_items as $t_item) {
987 if ($templatefiles = $fs->get_area_files($context->id, 'course_summary', $t_item->id, "id", false)) {
988 $fs->delete_area_files($context->id, 'course_summary', $t_item->id);
989 }
990 }
991 }
0085fff8 992 $DB->delete_records("feedback_template", array("id"=>$id));
c70ad9f7 993}
994
37b86a39 995/**
32be99dc 996 * creates new feedback_item-records from template.
997 * if $deleteold is set true so the existing items of the given feedback will be deleted
998 * if $deleteold is set false so the new items will be appanded to the old items
999 *
1000 * @global object
1001 * @param object $feedback
1002 * @param int $templateid
1003 * @param boolean $deleteold
c70ad9f7 1004 */
1005function feedback_items_from_template($feedback, $templateid, $deleteold = false) {
0085fff8 1006 global $DB;
14832313 1007 $fs = get_file_storage();
0085fff8 1008
c70ad9f7 1009 //get all templateitems
0085fff8 1010 if(!$templitems = $DB->get_records('feedback_item', array('template'=>$templateid))) {
c70ad9f7 1011 return false;
1012 }
37b86a39 1013
14832313
AG
1014 //files in the template_item are in the context of the current course
1015 //files in the feedback_item are in the feedback_context of the feedback
1016 $c_context = get_context_instance(CONTEXT_COURSE, $feedback->course);
1017 $cm = get_coursemodule_from_instance('feedback', $feedback->id);
1018 $f_context = get_context_instance(CONTEXT_MODULE, $cm->id);
1019
c70ad9f7 1020 //if deleteold then delete all old items before
1021 //get all items
1022 if($deleteold) {
0085fff8 1023 if($feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
c70ad9f7 1024 //delete all items of this feedback
1025 foreach($feedbackitems as $item) {
1026 feedback_delete_item($item->id, false);
1027 }
1028 //delete tracking-data
0085fff8 1029 $DB->delete_records('feedback_tracking', array('feedback'=>$feedback->id));
1030 $DB->delete_records('feedback_completed', array('feedback'=>$feedback->id));
1031 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedback->id));
c70ad9f7 1032 }
194dd279 1033 $positionoffset = 0;
0085fff8 1034 } else {
c70ad9f7 1035 //if the old items are kept the new items will be appended
1036 //therefor the new position has an offset
0085fff8 1037 $positionoffset = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
c70ad9f7 1038 }
37b86a39 1039
73043833
AG
1040 //create items of this new template
1041 //depend items we are storing temporary in an mapping list array(new id => dependitem)
1042 //we also store a mapping of all items array(oldid => newid)
1043 $dependitemsmap = array();
1044 $itembackup = array();
14832313
AG
1045 foreach($templitems as $t_item) {
1046 $item = clone($t_item);
1047 unset($item->id);
1048 $item->feedback = $feedback->id;
1049 $item->template = 0;
1050 $item->position = $item->position + $positionoffset;
1051
1052 $item->id = $DB->insert_record('feedback_item', $item);
1053
1054 //TODO: moving the files to the new items
1055 if ($templatefiles = $fs->get_area_files($c_context->id, 'course_summary', $t_item->id, "id", false)) {
1056 foreach($templatefiles as $tfile) {
1057 $file_record = new object();
1058 $file_record->contextid = $f_context->id;
1059 $file_record->filearea = 'feedback_item';
1060 $file_record->itemid = $item->id;
1061 $fs->create_file_from_storedfile($file_record, $tfile);
1062 }
1063 }
73043833
AG
1064
1065 $itembackup[$t_item->id] = $item->id;
1066 if($item->dependitem) {
1067 $dependitemsmap[$item->id] = $item->dependitem;
1068 }
c70ad9f7 1069 }
73043833
AG
1070
1071 //remapping the dependency
1072 foreach($dependitemsmap as $key => $dependitem) {
1073 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
1074 $newitem->dependitem = $itembackup[$newitem->dependitem];
1075 $DB->update_record('feedback_item', $newitem);
1076 }
c70ad9f7 1077}
1078
37b86a39 1079/**
32be99dc 1080 * get the list of available templates.
1081 * if the $onlyown param is set true so only templates from own course will be served
1082 * this is important for droping templates
1083 *
1084 * @global object
1085 * @param object $course
1086 * @param boolean $onlyown
1087 * @return array the template recordsets
c70ad9f7 1088 */
1089function feedback_get_template_list($course, $onlyown = false) {
0085fff8 1090 global $DB;
1091
1092 if ($onlyown) {
1093 $templates = $DB->get_records('feedback_template', array('course'=>$course->id));
c70ad9f7 1094 } else {
0085fff8 1095 $templates = $DB->get_records_select('feedback_template', 'course = ? OR ispublic = 1', array($course->id));
c70ad9f7 1096 }
1097 return $templates;
1098}
1099
1100////////////////////////////////////////////////
1101//Handling der Items
1102////////////////////////////////////////////////
1103////////////////////////////////////////////////
1104
37b86a39 1105/**
32be99dc 1106 * load the available item plugins from given subdirectory of $CFG->dirroot
1107 * the default is "mod/feedback/item"
1108 *
1109 * @global object
1110 * @param string $dir the subdir
1111 * @return array pluginnames as string
c70ad9f7 1112 */
1113function feedback_load_feedback_items($dir = 'mod/feedback/item') {
1114 global $CFG;
1115 $names =get_list_of_plugins($dir);
1116 $ret_names = array();
1117
1118 foreach($names as $name) {
1119 require_once($CFG->dirroot.'/'.$dir.'/'.$name.'/lib.php');
1120 if(class_exists('feedback_item_'.$name)) {
1121 $ret_names[] = $name;
1122 }
1123 }
1124 return $ret_names;
1125}
1126
37b86a39 1127/**
32be99dc 1128 * load the available item plugins to use as dropdown-options
1129 *
1130 * @global object
1131 * @return array pluginnames as string
c70ad9f7 1132 */
1133function feedback_load_feedback_items_options() {
1134 global $CFG;
37b86a39 1135
c70ad9f7 1136 $feedback_options = array("pagebreak" => get_string('add_pagebreak', 'feedback'));
37b86a39 1137
c70ad9f7 1138 if (!$feedback_names = feedback_load_feedback_items('mod/feedback/item')) {
1139 return array();
1140 }
37b86a39 1141
c70ad9f7 1142 foreach($feedback_names as $fn) {
1143 $feedback_options[$fn] = get_string($fn,'feedback');
1144 }
1145 asort($feedback_options);
37b86a39 1146 $feedback_options = array_merge( array(' ' => get_string('select')), $feedback_options );
c70ad9f7 1147 return $feedback_options;
1148}
1149
73043833
AG
1150function feedback_get_depend_candidates_for_item($feedback, $item) {
1151 global $DB;
1152 //all items for dependitem
1153 $where = "feedback = ? AND typ != 'pagebreak' AND hasvalue = 1";
1154 $params = array($feedback->id);
1155 if(isset($item->id) AND $item->id) {
1156 $where .= ' AND id != ?';
1157 $params[] = $item->id;
1158 }
1159 $dependitems = array(0 => get_string('choose'));
1160 if(!$feedbackitems = $DB->get_records_select_menu('feedback_item', $where, $params, 'position', 'id, label')) {
1161 return $dependitems;
1162 }
1163 //adding the choose-option
1164 foreach($feedbackitems as $key=>$val) {
1165 $dependitems[$key] = $val;
1166 }
1167 return $dependitems;
1168}
1169
37b86a39 1170/**
32be99dc 1171 * creates a new item-record
1172 *
1173 * @global object
1174 * @param object $data the data from edit_item_form
1175 * @return int the new itemid
c70ad9f7 1176 */
1177function feedback_create_item($data) {
0085fff8 1178 global $DB;
1179
c70ad9f7 1180 $item = new object;
0085fff8 1181 $item->feedback = $data->feedbackid;
c70ad9f7 1182
1183 $item->template=0;
1184 if (isset($data->templateid)) {
78946b9b 1185 $item->template = intval($data->templateid);
37b86a39 1186 }
c70ad9f7 1187
1188 $itemname = trim($data->itemname);
0085fff8 1189 $item->name = ($itemname ? $data->itemname : get_string('no_itemname', 'feedback'));
37b86a39 1190
d185ff44
SH
1191 if (!empty($data->itemlabel)) {
1192 $item->label = trim($data->itemlabel);
1193 } else {
1194 $item->label = get_string('no_itemlabel', 'feedback');
1195 }
b7a47958 1196
c70ad9f7 1197 //get the used class from item-typ
1198 $itemclass = 'feedback_item_'.$data->typ;
1199 //get the instance of the item class
1200 $itemobj = new $itemclass();
a59ff6b0
AG
1201 // $item->presentation = $itemobj->get_presentation($data);
1202 $item->presentation = ''; //the date comes from postupdate() of the itemobj
37b86a39 1203
c70ad9f7 1204 $item->hasvalue = $itemobj->get_hasvalue();
37b86a39 1205
c70ad9f7 1206 $item->typ = $data->typ;
1207 $item->position = $data->position;
1208
1209 $item->required=0;
ab5bd34d 1210 if (!empty($data->required)) {
1211 $item->required = $data->required;
37b86a39 1212 }
c70ad9f7 1213
a59ff6b0
AG
1214 $item->id = $DB->insert_record('feedback_item', $item);
1215
1216 //move all itemdata to the data
1217 $data->id = $item->id;
1218 $data->feedback = $item->feedback;
1219 $data->name = $item->name;
1220 $data->label = $item->label;
1221 $data->required = $item->required;
1222 return $itemobj->postupdate($data);
c70ad9f7 1223}
1224
37b86a39 1225/**
32be99dc 1226 * save the changes of a given item.
1227 *
1228 * @global object
1229 * @param object $item
32be99dc 1230 * @return boolean
c70ad9f7 1231 */
a59ff6b0 1232function feedback_update_item($item){
0085fff8 1233 global $DB;
0085fff8 1234 return $DB->update_record("feedback_item", $item);
c70ad9f7 1235}
1236
37b86a39 1237/**
32be99dc 1238 * deletes a item and also deletes all related values
1239 *
1240 * @global object
1241 * @param int $itemid
1242 * @param boolean $renumber should the kept items renumbered Yes/No
1243 * @return void
c70ad9f7 1244 */
1245function feedback_delete_item($itemid, $renumber = true){
0085fff8 1246 global $DB;
14832313
AG
1247
1248
0085fff8 1249 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
14832313
AG
1250
1251 //deleting the files from the item
1252 $fs = get_file_storage();
1253 if (!$cm = get_coursemodule_from_instance('feedback', $item->feedback)) {
1254 return false;
1255 }
1256 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1257
1258 if ($itemfiles = $fs->get_area_files($context->id, 'feedback_item', $item->id, "id", false)) {
1259 $fs->delete_area_files($context->id, 'feedback_item', $item->id);
1260 }
1261
0085fff8 1262 $DB->delete_records("feedback_value", array("item"=>$itemid));
1263 $DB->delete_records("feedback_valuetmp", array("item"=>$itemid));
73043833
AG
1264
1265 //remove all depends
1266 $DB->set_field('feedback_item', 'dependvalue', '', array('dependitem'=>$itemid));
1267 $DB->set_field('feedback_item', 'dependitem', 0, array('dependitem'=>$itemid));
1268
0085fff8 1269 $DB->delete_records("feedback_item", array("id"=>$itemid));
c70ad9f7 1270 if($renumber) {
1271 feedback_renumber_items($item->feedback);
1272 }
1273}
1274
37b86a39 1275/**
32be99dc 1276 * deletes all items of the given feedbackid
1277 *
1278 * @global object
1279 * @param int $feedbackid
1280 * @return void
c70ad9f7 1281 */
1282function feedback_delete_all_items($feedbackid){
0085fff8 1283 global $DB;
1284
1285 if(!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid))) {
c70ad9f7 1286 return;
1287 }
1288 foreach($items as $item) {
1289 feedback_delete_item($item->id, false);
1290 }
0085fff8 1291 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedbackid));
1292 $DB->delete_records('feedback_completed', array('feedback'=>$feedbackid));
c70ad9f7 1293}
1294
37b86a39 1295/**
32be99dc 1296 * this function toggled the item-attribute required (yes/no)
1297 *
1298 * @global object
1299 * @param object $item
1300 * @return boolean
c70ad9f7 1301 */
1302function feedback_switch_item_required($item) {
0085fff8 1303 global $DB;
1304
c70ad9f7 1305 if($item->required == 1) {
1306 $item->required = 0;
1307 } else {
1308 $item->required = 1;
1309 }
0085fff8 1310 $item->name = $item->name;
1311 $item->presentation = $item->presentation;
1312 return $DB->update_record('feedback_item', $item);
c70ad9f7 1313}
1314
37b86a39 1315/**
32be99dc 1316 * renumbers all items of the given feedbackid
1317 *
1318 * @global object
1319 * @param int $feedbackid
1320 * @return void
c70ad9f7 1321 */
1322function feedback_renumber_items($feedbackid){
0085fff8 1323 global $DB;
1324
1325 $items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position');
c70ad9f7 1326 $pos = 1;
1327 if($items) {
1328 foreach($items as $item){
1329 $item->position = $pos;
1330 $pos++;
1331 feedback_update_item($item);
1332 }
1333 }
1334}
1335
37b86a39 1336/**
32be99dc 1337 * this decreases the position of the given item
1338 *
1339 * @global object
1340 * @param object $item
1341 * @return void
c70ad9f7 1342 */
1343function feedback_moveup_item($item){
0085fff8 1344 global $DB;
1345
a59ff6b0
AG
1346 if($item->position == 1) {
1347 return true;
1348 }
1349
1350 if(!$items = $DB->get_records('feedback_item', array('feedback'=>$item->feedback), 'position')) {
1351 return false;
1352 }
1353
1354 $itembefore = null;
1355 foreach($items as $i) {
1356 if($i->id == $item->id) {
1357 if(is_null($itembefore)) {
1358 return true;
1359 }
1360 $itembefore->position = $item->position;
1361 $item->position--;
1362 feedback_update_item($itembefore);
1363 feedback_update_item($item);
1364 feedback_renumber_items($item->feedback);
1365 return true;
1366 }
1367 $itembefore = $i;
1368 }
1369 return false;
c70ad9f7 1370}
1371
37b86a39 1372/**
32be99dc 1373 * this increased the position of the given item
1374 *
1375 * @global object
1376 * @param object $item
1377 * @return void
c70ad9f7 1378 */
1379function feedback_movedown_item($item){
0085fff8 1380 global $DB;
1381
a59ff6b0
AG
1382 if(!$items = $DB->get_records('feedback_item', array('feedback'=>$item->feedback), 'position')) {
1383 return false;
c70ad9f7 1384 }
a59ff6b0
AG
1385
1386 $movedownitem = null;
1387 foreach($items as $i) {
1388 if(!is_null($movedownitem) AND $movedownitem->id == $item->id) {
1389 $movedownitem->position = $i->position;
1390 $i->position--;
1391 feedback_update_item($movedownitem);
1392 feedback_update_item($i);
1393 feedback_renumber_items($item->feedback);
1394 return true;
1395 }
1396 $movedownitem = $i;
1397 }
1398 return false;
c70ad9f7 1399}
1400
37b86a39 1401/**
32be99dc 1402 * here the position of the given item will be set to the value in $pos
1403 *
1404 * @global object
1405 * @param object $moveitem
1406 * @param int $pos
1407 * @return boolean
c70ad9f7 1408 */
1409function feedback_move_item($moveitem, $pos){
0085fff8 1410 global $DB;
1411
0085fff8 1412 if (!$allitems = $DB->get_records('feedback_item', array('feedback'=>$moveitem->feedback), 'position')) {
c70ad9f7 1413 return false;
1414 }
0085fff8 1415 if (is_array($allitems)) {
c70ad9f7 1416 $index = 1;
1417 foreach($allitems as $item) {
c70ad9f7 1418 if($index == $pos) {
c70ad9f7 1419 $index++;
1420 }
a59ff6b0
AG
1421 if($item->id == $moveitem->id) {
1422 $moveitem->position = $pos;
1423 feedback_update_item($moveitem);
1424 continue;
1425 }
c70ad9f7 1426 $item->position = $index;
1427 feedback_update_item($item);
1428 $index++;
1429 }
c70ad9f7 1430 return true;
1431 }
1432 return false;
1433}
1434
9d5fbd65
AG
1435/**
1436 * prints the given item.
1437 * if $readonly is set true so the ouput only is for showing responses and not for editing or completing.
1438 * each item-class has an own print_item function implemented.
1439 *
1440 * @param object $item the item what we want to print out
1441 * @return void
1442 */
1443function feedback_print_item_preview($item){
1444 global $CFG;
1445 if($item->typ == 'pagebreak') {
1446 return;
1447 }
1448 //get the class of the given item-typ
1449 $itemclass = 'feedback_item_'.$item->typ;
1450 if (!class_exists($itemclass)) {
1451 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1452 }
1453 //get the instance of the item-class
1454 $itemobj = new $itemclass();
1455 $itemobj->print_item_preview($item);
1456}
1457
37b86a39 1458/**
32be99dc 1459 * prints the given item.
1460 * if $readonly is set true so the ouput only is for showing responses and not for editing or completing.
1461 * each item-class has an own print_item function implemented.
1462 *
1463 * @param object $item the item what we want to print out
1464 * @param mixed $value the value if $readonly is set true and we showing responses
32be99dc 1465 * @param boolean $highlightrequire if this set true and the value are false on completing so the item will be highlighted
1466 * @return void
c70ad9f7 1467 */
9d5fbd65 1468function feedback_print_item_complete($item, $value = false, $highlightrequire = false){
ab5bd34d 1469 global $CFG;
9d5fbd65
AG
1470 if($item->typ == 'pagebreak') {
1471 return;
1472 }
37b86a39 1473
c70ad9f7 1474 //get the class of the given item-typ
1475 $itemclass = 'feedback_item_'.$item->typ;
ab5bd34d 1476 if (!class_exists($itemclass)) {
1477 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1478 }
c70ad9f7 1479 //get the instance of the item-class
1480 $itemobj = new $itemclass();
9d5fbd65
AG
1481 $itemobj->print_item_complete($item, $value, $highlightrequire);
1482}
1483
1484/**
1485 * prints the given item.
1486 * if $readonly is set true so the ouput only is for showing responses and not for editing or completing.
1487 * each item-class has an own print_item function implemented.
1488 *
1489 * @param object $item the item what we want to print out
1490 * @param mixed $value the value if $readonly is set true and we showing responses
1491 * @return void
1492 */
1493function feedback_print_item_show_value($item, $value = false){
1494 global $CFG;
1495 if($item->typ == 'pagebreak') {
1496 return;
1497 }
1498
1499 //get the class of the given item-typ
1500 $itemclass = 'feedback_item_'.$item->typ;
1501 if (!class_exists($itemclass)) {
1502 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1503 }
1504 //get the instance of the item-class
1505 $itemobj = new $itemclass();
1506 $itemobj->print_item_show_value($item, $value);
c70ad9f7 1507}
1508
37b86a39 1509/**
32be99dc 1510 * if the user completes a feedback and there is a pagebreak so the values are saved temporary.
1511 * the values are saved permanently not until the user click on save button
1512 *
1513 * @global object
1514 * @param object $feedbackcompleted
1515 * @return object temporary saved completed-record
c70ad9f7 1516 */
0085fff8 1517function feedback_set_tmp_values($feedbackcompleted) {
1518 global $DB;
1519
c70ad9f7 1520 //first we create a completedtmp
1521 $tmpcpl = new object();
1522 foreach($feedbackcompleted as $key => $value) {
1523 $tmpcpl->{$key} = $value;
1524 }
1525 // $tmpcpl = $feedbackcompleted;
0085fff8 1526 unset($tmpcpl->id);
c70ad9f7 1527 $tmpcpl->timemodified = time();
7826abc7 1528 $tmpcpl->id = $DB->insert_record('feedback_completedtmp', $tmpcpl);
c70ad9f7 1529 //get all values of original-completed
0085fff8 1530 if(!$values = $DB->get_records('feedback_value', array('completed'=>$feedbackcompleted->id))) {
c70ad9f7 1531 return;
1532 }
1533 foreach($values as $value) {
0085fff8 1534 unset($value->id);
c70ad9f7 1535 $value->completed = $tmpcpl->id;
0085fff8 1536 $DB->insert_record('feedback_valuetmp', $value);
c70ad9f7 1537 }
1538 return $tmpcpl;
1539}
1540
37b86a39 1541/**
32be99dc 1542 * this saves the temporary saved values permanently
1543 *
1544 * @global object
1545 * @param object $feedbackcompletedtmp the temporary completed
1546 * @param object $feedbackcompleted the target completed
1547 * @param int $userid
1548 * @return int the id of the completed
c70ad9f7 1549 */
1550function feedback_save_tmp_values($feedbackcompletedtmp, $feedbackcompleted, $userid) {
0085fff8 1551 global $DB;
1552
c70ad9f7 1553 $tmpcplid = $feedbackcompletedtmp->id;
73043833 1554 if($feedbackcompleted) {
c70ad9f7 1555 //first drop all existing values
0085fff8 1556 $DB->delete_records('feedback_value', array('completed'=>$feedbackcompleted->id));
c70ad9f7 1557 //update the current completed
1558 $feedbackcompleted->timemodified = time();
0085fff8 1559 $DB->update_record('feedback_completed', $feedbackcompleted);
73043833
AG
1560 }else {
1561 $feedbackcompleted = clone($feedbackcompletedtmp);
1562 $feedbackcompleted->id = '';
1563 $feedbackcompleted->userid = $userid;
1564 $feedbackcompleted->timemodified = time();
1565 $feedbackcompleted->id = $DB->insert_record('feedback_completed', $feedbackcompleted);
1566 }
1567
1568 //save all the new values from feedback_valuetmp
1569 //get all values of tmp-completed
1570 if(!$values = $DB->get_records('feedback_valuetmp', array('completed'=>$feedbackcompletedtmp->id))) {
1571 return false;
1572 }
1573 foreach($values as $value) {
1574 //check if there are depend items
1575 $item = $DB->get_record('feedback_item', array('id'=>$value->item));
1576 if($item->dependitem > 0) {
1577 $check = feedback_compare_item_value($tmpcplid, $item->dependitem, $item->dependvalue, true);
1578 }else {
1579 $check = true;
c70ad9f7 1580 }
73043833 1581 if($check) {
0085fff8 1582 unset($value->id);
c70ad9f7 1583 $value->completed = $feedbackcompleted->id;
0085fff8 1584 $DB->insert_record('feedback_value', $value);
c70ad9f7 1585 }
c70ad9f7 1586 }
73043833
AG
1587 //drop all the tmpvalues
1588 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1589 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
1590 return $feedbackcompleted->id;
1591
c70ad9f7 1592}
1593
37b86a39 1594/**
32be99dc 1595 * deletes the given temporary completed and all related temporary values
1596 *
1597 * @global object
1598 * @param int $tmpcplid
1599 * @return void
c70ad9f7 1600 */
1601function feedback_delete_completedtmp($tmpcplid) {
0085fff8 1602 global $DB;
1603
1604 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1605 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
c70ad9f7 1606}
1607
1608////////////////////////////////////////////////
1609////////////////////////////////////////////////
1610////////////////////////////////////////////////
1611//functions to handle the pagebreaks
1612////////////////////////////////////////////////
1613
37b86a39 1614/**
32be99dc 1615 * this creates a pagebreak.
1616 * a pagebreak is a special kind of item
1617 *
1618 * @global object
1619 * @param int $feedbackid
1620 * @return mixed false if there already is a pagebreak on last position or the id of the pagebreak-item
c70ad9f7 1621 */
1622function feedback_create_pagebreak($feedbackid) {
0085fff8 1623 global $DB;
1624
c70ad9f7 1625 //check if there already is a pagebreak on the last position
0085fff8 1626 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
c70ad9f7 1627 if($lastposition == feedback_get_last_break_position($feedbackid)) {
1628 return false;
1629 }
37b86a39 1630
c70ad9f7 1631 $item = new object();
c70ad9f7 1632 $item->feedback = $feedbackid;
1633
1634 $item->template=0;
1635
1636 $item->name = '';
37b86a39 1637
c70ad9f7 1638 $item->presentation = '';
1639 $item->hasvalue = 0;
37b86a39 1640
c70ad9f7 1641 $item->typ = 'pagebreak';
1642 $item->position = $lastposition + 1;
1643
1644 $item->required=0;
1645
0085fff8 1646 return $DB->insert_record('feedback_item', $item);
c70ad9f7 1647}
1648
37b86a39 1649/**
32be99dc 1650 * get all positions of pagebreaks in the given feedback
1651 *
1652 * @global object
1653 * @param int $feedbackid
1654 * @return array all ordered pagebreak positions
c70ad9f7 1655 */
1656function feedback_get_all_break_positions($feedbackid) {
534792cd 1657 global $DB;
1658
1659 if(!$allbreaks = $DB->get_records_menu('feedback_item', array('typ'=>'pagebreak', 'feedback'=>$feedbackid), 'position', 'id, position')) return false;
c70ad9f7 1660 return array_values($allbreaks);
1661}
1662
37b86a39 1663/**
32be99dc 1664 * get the position of the last pagebreak
d4b1d58c 1665 *
32be99dc 1666 * @param int $feedbackid
1667 * @return int the position of the last pagebreak
c70ad9f7 1668 */
1669function feedback_get_last_break_position($feedbackid) {
73043833
AG
1670 if(!$allbreaks = feedback_get_all_break_positions($feedbackid)) {
1671 return false;
1672 }
c70ad9f7 1673 return $allbreaks[count($allbreaks) - 1];
1674}
1675
37b86a39 1676/**
32be99dc 1677 * this returns the position where the user can continue the completing.
1678 *
1679 * @global object
1680 * @global object
1681 * @global object
1682 * @param int $feedbackid
1683 * @param int $courseid
1684 * @param string $guestid this id will be saved temporary and is unique
1685 * @return int the position to continue
c70ad9f7 1686 */
1687function feedback_get_page_to_continue($feedbackid, $courseid = false, $guestid) {
0085fff8 1688 global $CFG, $USER, $DB;
37b86a39 1689
c70ad9f7 1690 //is there any break?
37b86a39 1691
c70ad9f7 1692 if(!$allbreaks = feedback_get_all_break_positions($feedbackid)) return false;
0085fff8 1693
1694 $params = array();
c70ad9f7 1695 if($courseid) {
0085fff8 1696 $courseselect = "fv.course_id = :courseid";
1697 $params['courseid'] = $courseid;
c70ad9f7 1698 }else {
1699 $courseselect = "1";
1700 }
37b86a39 1701
c70ad9f7 1702 if($guestid) {
0085fff8 1703 $userselect = "AND fc.guestid = :guestid";
c70ad9f7 1704 $usergroup = "GROUP BY fc.guestid";
0085fff8 1705 $params['guestid'] = $guestid;
c70ad9f7 1706 }else {
0085fff8 1707 $userselect = "AND fc.userid = :userid";
c70ad9f7 1708 $usergroup = "GROUP BY fc.userid";
0085fff8 1709 $params['userid'] = $USER->id;
c70ad9f7 1710 }
0085fff8 1711
c70ad9f7 1712 $sql = "SELECT MAX(fi.position)
abf1c50f 1713 FROM {feedback_completedtmp} fc, {feedback_valuetmp} fv, {feedback_item} fi
0085fff8 1714 WHERE fc.id = fv.completed
1715 $userselect
1716 AND fc.feedback = :feedbackid
1717 AND $courseselect
c70ad9f7 1718 AND fi.id = fv.item
0085fff8 1719 $usergroup";
1720 $params['feedbackid'] = $feedbackid;
1721
1722 $lastpos = $DB->get_field_sql($sql, $params);
c70ad9f7 1723
1724 //the index of found pagebreak is the searched pagenumber
1725 foreach($allbreaks as $pagenr => $br) {
1726 if($lastpos < $br) return $pagenr;
1727 }
1728 return count($allbreaks);
1729}
1730
1731////////////////////////////////////////////////
1732////////////////////////////////////////////////
1733////////////////////////////////////////////////
1734//functions to handle the values
1735////////////////////////////////////////////////
1736
37b86a39 1737/**
32be99dc 1738 * this saves the values of an completed.
1739 * if the param $tmp is set true so the values are saved temporary in table feedback_valuetmp
1740 * if there is already a completed and the userid is set so the values are updated
1741 * on all other things new value records will be created
d4b1d58c 1742 *
32be99dc 1743 * @global object
32be99dc 1744 * @param int $userid
1745 * @param boolean $tmp
1746 * @return mixed false on error or the completeid
c70ad9f7 1747 */
6a471fe0 1748function feedback_save_values($usrid, $tmp = false) {
0085fff8 1749 global $DB;
6a471fe0
AG
1750
1751 $completedid = optional_param('completedid', false, PARAM_INT);
0085fff8 1752
c70ad9f7 1753 $tmpstr = $tmp ? 'tmp' : '';
6a471fe0
AG
1754 $time = time();
1755 $timemodified = mktime(0, 0, 0, date('m', $time),date('d', $time),date('Y', $time));
1756
c70ad9f7 1757 if($usrid == 0) {
6a471fe0 1758 return feedback_create_values($usrid, $timemodified, $tmp);
c70ad9f7 1759 }
6a471fe0
AG
1760 if(!$completedid or !$completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid))) {
1761 return feedback_create_values($usrid, $timemodified, $tmp);
c70ad9f7 1762 }else{
1763 $completed->timemodified = $timemodified;
6a471fe0 1764 return feedback_update_values($completed, $tmp);
c70ad9f7 1765 }
1766}
1767
37b86a39 1768/**
32be99dc 1769 * this saves the values from anonymous user such as guest on the main-site
1770 *
1771 * @global object
32be99dc 1772 * @param string $guestid the unique guestidentifier
1773 * @return mixed false on error or the completeid
c70ad9f7 1774 */
6a471fe0 1775function feedback_save_guest_values($guestid) {
0085fff8 1776 global $DB;
1777
6a471fe0
AG
1778 $completedid = optional_param('completedid', false, PARAM_INT);
1779
c70ad9f7 1780 $timemodified = time();
6a471fe0
AG
1781 if(!$completed = $DB->get_record('feedback_completedtmp', array('id'=>$completedid))) {
1782 return feedback_create_values(0, $timemodified, true, $guestid);
c70ad9f7 1783 }else {
1784 $completed->timemodified = $timemodified;
6a471fe0 1785 return feedback_update_values($completed, true);
c70ad9f7 1786 }
1787}
1788
37b86a39 1789/**
32be99dc 1790 * get the value from the given item related to the given completed.
1791 * the value can come as temporary or as permanently value. the deciding is done by $tmp
1792 *
1793 * @global object
1794 * @param int $completeid
1795 * @param int $itemid
1796 * @param boolean $tmp
1797 * @return mixed the value, the type depends on plugin-definition
c70ad9f7 1798 */
1799function feedback_get_item_value($completedid, $itemid, $tmp = false) {
0085fff8 1800 global $DB;
1801
c70ad9f7 1802 $tmpstr = $tmp ? 'tmp' : '';
0085fff8 1803 return $DB->get_field('feedback_value'.$tmpstr, 'value', array('completed'=>$completedid, 'item'=>$itemid));
c70ad9f7 1804}
1805
73043833
AG
1806function feedback_compare_item_value($completedid, $itemid, $dependvalue, $tmp = false) {
1807 global $DB, $CFG;
1808
1809 $dbvalue = feedback_get_item_value($completedid, $itemid, $tmp);
1810
1811 //get the class of the given item-typ
1812 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
1813 $itemclass = 'feedback_item_'.$item->typ;
1814 if (!class_exists($itemclass)) {
1815 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1816 }
1817 //get the instance of the item-class
1818 $itemobj = new $itemclass();
1819 return $itemobj->compare_value($item, $dbvalue, $dependvalue); //true or false
1820}
1821
37b86a39 1822/**
32be99dc 1823 * this function checks the correctness of values.
1824 * the rules for this are implemented in the class of each item.
1825 * it can be the required attribute or the value self e.g. numeric.
1826 * the params first/lastitem are given to determine the visible range between pagebreaks.
1827 *
1828 * @global object
32be99dc 1829 * @param int $firstitem the position of firstitem for checking
1830 * @param int $lastitem the position of lastitem for checking
1831 * @return boolean
c70ad9f7 1832 */
6a471fe0 1833function feedback_check_values($firstitem, $lastitem) {
ab5bd34d 1834 global $DB, $CFG;
0085fff8 1835
6a471fe0
AG
1836 $feedbackid = optional_param('feedbackid', 0, PARAM_INT);
1837
c70ad9f7 1838 //get all items between the first- and lastitem
0085fff8 1839 $select = "feedback = ?
1840 AND position >= ?
1841 AND position <= ?
c70ad9f7 1842 AND hasvalue = 1";
6a471fe0 1843 $params = array($feedbackid, $firstitem, $lastitem);
0085fff8 1844 if(!$feedbackitems = $DB->get_records_select('feedback_item', $select, $params)) {
c70ad9f7 1845 //if no values are given so no values can be wrong ;-)
1846 return true;
1847 }
1848
1849 foreach($feedbackitems as $item) {
1850 //the name of the input field of the completeform is given in a special form:
1851 //<item-typ>_<item-id> eg. numeric_234
1852 //this is the key to get the value for the correct item
1853 $formvalname = $item->typ . '_' . $item->id;
6a471fe0
AG
1854
1855 $value = optional_param($formvalname, NULL, PARAM_RAW);
1856
c70ad9f7 1857 //check if the value is set
6a471fe0 1858 if(is_null($value) AND $item->required == 1) {
c70ad9f7 1859 return false;
1860 }
37b86a39 1861
c70ad9f7 1862 //get the class of the item-typ
1863 $itemclass = 'feedback_item_'.$item->typ;
ab5bd34d 1864
1865 if (!class_exists($itemclass)) {
1866 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1867 }
1868
c70ad9f7 1869 //get the instance of the item-class
1870 $itemobj = new $itemclass();
37b86a39 1871
c70ad9f7 1872 //now we let check the value by the item-class
1873 if(!$itemobj->check_value($value, $item)) {
1874 return false;
1875 }
1876 }
1877 //if no wrong values so we can return true
1878 return true;
1879}
1880
37b86a39 1881/**
32be99dc 1882 * this function create a complete-record and the related value-records.
1883 * depending on the $tmp (true/false) the values are saved temporary or permanently
1884 *
1885 * @global object
32be99dc 1886 * @param int $userid
1887 * @param int $timemodified
1888 * @param boolean $tmp
1889 * @param string $guestid a unique identifier to save temporary data
1890 * @return mixed false on error or the completedid
c70ad9f7 1891 */
6a471fe0 1892function feedback_create_values($usrid, $timemodified, $tmp = false, $guestid = false){
0085fff8 1893 global $DB;
1894
6a471fe0
AG
1895 $feedbackid = optional_param('feedbackid', false, PARAM_INT);
1896 $anonymous_response = optional_param('anonymous_response', false, PARAM_INT);
1897 $courseid = optional_param('courseid', false, PARAM_INT);
1898
c70ad9f7 1899 $tmpstr = $tmp ? 'tmp' : '';
1900 //first we create a new completed record
0085fff8 1901 $completed = new object();
6a471fe0 1902 $completed->feedback = $feedbackid;
0085fff8 1903 $completed->userid = $usrid;
1904 $completed->guestid = $guestid;
1905 $completed->timemodified = $timemodified;
6a471fe0 1906 $completed->anonymous_response = $anonymous_response;
37b86a39 1907
a8f3a651 1908 $completedid = $DB->insert_record('feedback_completed'.$tmpstr, $completed);
37b86a39 1909
0085fff8 1910 $completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid));
c70ad9f7 1911
6a471fe0 1912 //the keys are in the form like abc_xxx
c70ad9f7 1913 //with explode we make an array with(abc, xxx) and (abc=typ und xxx=itemnr)
6a471fe0
AG
1914
1915 //get the items of the feedback
1916 if(!$allitems = $DB->get_records('feedback_item', array('feedback'=>$completed->feedback))) {
1917 return false;
1918 }
1919 foreach($allitems as $item) {
1920 $keyname = $item->typ.'_'.$item->id;
1921 $itemvalue = optional_param($keyname, NULL, PARAM_RAW);
1922 if(is_null($itemvalue)) {
1923 continue;
c70ad9f7 1924 }
6a471fe0
AG
1925 $value = new object();
1926 $value->item = $item->id;
1927 $value->completed = $completed->id;
1928 $value->course_id = $courseid;
1929
1930 //get the class of item-typ
1931 $itemclass = 'feedback_item_'.$item->typ;
1932
1933 //get the instance of item-class
1934 if (!class_exists($itemclass)) {
1935 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1936 }
1937 $itemobj = new $itemclass();
1938
1939 //the kind of values can be absolutely different so we run create_value directly by the item-class
1940 $value->value = $itemobj->create_value($itemvalue);
1941 $DB->insert_record('feedback_value'.$tmpstr, $value);
c70ad9f7 1942 }
6a471fe0 1943 return $completed->id;
c70ad9f7 1944}
1945
37b86a39 1946/**
32be99dc 1947 * this function updates a complete-record and the related value-records.
1948 * depending on the $tmp (true/false) the values are saved temporary or permanently
1949 *
1950 * @global object
32be99dc 1951 * @param object $completed
1952 * @param boolean $tmp
1953 * @return int the completedid
c70ad9f7 1954 */
6a471fe0 1955function feedback_update_values($completed, $tmp = false) {
0085fff8 1956 global $DB;
6a471fe0 1957 $courseid = optional_param('courseid', false, PARAM_INT);
0085fff8 1958
c70ad9f7 1959 $tmpstr = $tmp ? 'tmp' : '';
37b86a39 1960
0085fff8 1961 $DB->update_record('feedback_completed'.$tmpstr, $completed);
c70ad9f7 1962 //get the values of this completed
0085fff8 1963 $values = $DB->get_records('feedback_value'.$tmpstr, array('completed'=>$completed->id));
37b86a39 1964
6a471fe0
AG
1965 //get the items of the feedback
1966 if(!$allitems = $DB->get_records('feedback_item', array('feedback'=>$completed->feedback))) {
1967 return false;
1968 }
1969 foreach($allitems as $item) {
1970 $keyname = $item->typ.'_'.$item->id;
1971 $itemvalue = optional_param($keyname, NULL, PARAM_RAW);
1972 //is the itemvalue set (could be a subset of items because pagebreak)?
1973 if(is_null($itemvalue)) {
1974 continue;
1975 }
1976
1977 $newvalue = new object();
1978 $newvalue->item = $item->id;
1979 $newvalue->completed = $completed->id;
1980 $newvalue->course_id = $courseid;
37b86a39 1981
6a471fe0
AG
1982 //get the class of item-typ
1983 $itemclass = 'feedback_item_'.$item->typ;
1984
1985 //get the instance of item-class
1986 if (!class_exists($itemclass)) {
1987 require_once($CFG->dirroot.'/mod/feedback/item/'.$item->typ.'/lib.php');
1988 }
1989 $itemobj = new $itemclass();
1990 //the kind of values can be absolutely different so we run create_value directly by the item-class
1991 $newvalue->value = $itemobj->create_value($itemvalue);
1992
1993 //check, if we have to create or update the value
1994 $exist = false;
1995 foreach($values as $value){
1996 if($value->item == $newvalue->item){
1997 $newvalue->id = $value->id;
1998 $exist = true;
1999 break;
2000 }
2001 }
2002 if($exist){
2003 $DB->update_record('feedback_value'.$tmpstr, $newvalue);
2004 }else {
2005 $DB->insert_record('feedback_value'.$tmpstr, $newvalue);
c70ad9f7 2006 }
2007 }
2008
2009 return $completed->id;
2010}
2011
37b86a39 2012/**
32be99dc 2013 * get the values of an item depending on the given groupid.
2014 * if the feedback is anonymous so the values are shuffled
2015 *
2016 * @global object
2017 * @global object
2018 * @param object $item
2019 * @param int $groupid
2020 * @param int $courseid
2021 * @return array the value-records
c70ad9f7 2022 */
79473294 2023function feedback_get_group_values($item, $groupid = false, $courseid = false, $ignore_empty = false){
0085fff8 2024 global $CFG, $DB;
c70ad9f7 2025
2026 //if the groupid is given?
0085fff8 2027 if (intval($groupid) > 0) {
79473294
AG
2028 if($ignore_empty) {
2029 $ignore_empty_select = "AND fbc.value != ''";
2030 }
2031 else {
2032 $ignore_empty_select = "";
2033 }
2034
c70ad9f7 2035 $query = 'SELECT fbv . *
abf1c50f 2036 FROM {feedback_value} fbv, {feedback_completed} fbc, {groups_members} gm
0085fff8 2037 WHERE fbv.item = ?
37b86a39 2038 AND fbv.completed = fbc.id
2039 AND fbc.userid = gm.userid
79473294 2040 '.$ignore_empty_select.'
0085fff8 2041 AND gm.groupid = ?
2042 ORDER BY fbc.timemodified';
2043 $values = $DB->get_records_sql($query, array($item->id, $groupid));
2044
2045 } else {
79473294
AG
2046 if($ignore_empty) {
2047 $ignore_empty_select = "AND value != ''";
2048 }
2049 else {
2050 $ignore_empty_select = "";
2051 }
2052
c70ad9f7 2053 if ($courseid) {
79473294 2054 $values = $DB->get_records_select('feedback_value', "item = ? AND course_id = ? ".$ignore_empty_select, array($item->id, $courseid));
c70ad9f7 2055 } else {
79473294 2056 $values = $DB->get_records_select('feedback_value', "item = ? ".$ignore_empty_select, array($item->id));
c70ad9f7 2057 }
37b86a39 2058 }
0085fff8 2059 if ($DB->get_field('feedback', 'anonymous', array('id'=>$item->feedback)) == FEEDBACK_ANONYMOUS_YES) {
c70ad9f7 2060 if(is_array($values))
2061 shuffle($values);
2062 }
2063 return $values;
2064}
2065
37b86a39 2066/**
32be99dc 2067 * check for multiple_submit = false.
2068 * if the feedback is global so the courseid must be given
2069 *
2070 * @global object
2071 * @global object
2072 * @param int $feedbackid
2073 * @param int $courseid
2074 * @return boolean true if the feedback already is submitted otherwise false
c70ad9f7 2075 */
2076function feedback_is_already_submitted($feedbackid, $courseid = false) {
534792cd 2077 global $USER, $DB;
37b86a39 2078
534792cd 2079 if (!$trackings = $DB->get_records_menu('feedback_tracking', array('userid'=>$USER->id, 'feedback'=>$feedbackid), '', 'id, completed')) {
c70ad9f7 2080 return false;
2081 }
2082
2083 if($courseid) {
534792cd 2084 $select = 'completed IN ('.implode(',',$trackings).') AND course_id = ?';
2085 if(!$values = $DB->get_records_select('feedback_value', $select, array($courseid))) {
c70ad9f7 2086 return false;
2087 }
2088 }
2089
2090 return true;
2091}
2092
37b86a39 2093/**
32be99dc 2094 * if the completion of a feedback will be continued eg. by pagebreak or by multiple submit so the complete must be found.
2095 * if the param $tmp is set true so all things are related to temporary completeds
2096 *
2097 * @global object
2098 * @global object
2099 * @global object
2100 * @param int $feedbackid
2101 * @param boolean $tmp
2102 * @param int $courseid
2103 * @param string $guestid
2104 * @return int the id of the found completed
c70ad9f7 2105 */
2106function feedback_get_current_completed($feedbackid, $tmp = false, $courseid = false, $guestid = false) {
0085fff8 2107 global $USER, $CFG, $DB;
37b86a39 2108
c70ad9f7 2109 $tmpstr = $tmp ? 'tmp' : '';
37b86a39 2110
c70ad9f7 2111 if(!$courseid) {
2112 if($guestid) {
0085fff8 2113 return $DB->get_record('feedback_completed'.$tmpstr, array('feedback'=>$feedbackid, 'guestid'=>$guestid));
c70ad9f7 2114 }else {
0085fff8 2115 return $DB->get_record('feedback_completed'.$tmpstr, array('feedback'=>$feedbackid, 'userid'=>$USER->id));
c70ad9f7 2116 }
2117 }
37b86a39 2118
0085fff8 2119 $params = array();
37b86a39 2120
0085fff8 2121 if ($guestid) {
2122 $userselect = "AND fc.guestid = :guestid";
2123 $params['guestid'] = $guestid;
c70ad9f7 2124 }else {
0085fff8 2125 $userselect = "AND fc.userid = :userid";
2126 $params['userid'] = $USER->id;
c70ad9f7 2127 }
2128 //if courseid is set the feedback is global. there can be more than one completed on one feedback
31c0fa53 2129 $sql = "SELECT DISTINCT fc.*
abf1c50f 2130 FROM {feedback_value{$tmpstr}} fv, {feedback_completed{$tmpstr}} fc
0085fff8 2131 WHERE fv.course_id = :courseid
c70ad9f7 2132 AND fv.completed = fc.id
0085fff8 2133 $userselect
2134 AND fc.feedback = :feedbackid";
2135 $params['courseid'] = intval($courseid);
2136 $params['feedbackid'] = $feedbackid;
c70ad9f7 2137
d90e0ef3 2138 if (!$sqlresult = $DB->get_records_sql($sql, $params)) {
0085fff8 2139 return false;
2140 }
c70ad9f7 2141 foreach($sqlresult as $r) {
0085fff8 2142 return $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$r->id));
c70ad9f7 2143 }
2144}
2145
37b86a39 2146/**
32be99dc 2147 * get the completeds depending on the given groupid.
2148 *
2149 * @global object
2150 * @global object
2151 * @param object $feedback
2152 * @param int $groupid
2153 * @return mixed array of found completeds otherwise false
c70ad9f7 2154 */
59f89d80 2155function feedback_get_completeds_group($feedback, $groupid = false, $courseid = false) {
0085fff8 2156 global $CFG, $DB;
2157
2158 if (intval($groupid) > 0){
2159 $query = "SELECT fbc.*
abf1c50f 2160 FROM {feedback_completed} fbc, {groups_members} gm
0085fff8 2161 WHERE fbc.feedback = ?
2162 AND gm.groupid = ?
2163 AND fbc.userid = gm.userid";
2164 if ($values = $DB->get_records_sql($query, array($feedback->id, $groupid))) {
c70ad9f7 2165 return $values;
0085fff8 2166 } else {
2167 return false;
2168 }
2169 } else {
59f89d80 2170 if($courseid) {
2171 $query = "SELECT DISTINCT fbc.*
abf1c50f 2172 FROM {feedback_completed} fbc, {feedback_value} fbv
59f89d80 2173 WHERE fbc.id = fbv.completed
2174 AND fbc.feedback = ?
2175 AND fbv.course_id = ?
2176 ORDER BY random_response";
2177 if ($values = $DB->get_records_sql($query, array($feedback->id, $courseid))) {
2178 return $values;
2179 } else {
2180 return false;
2181 }
2182 }else {
2183 if ($values = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id))) {
2184 return $values;
2185 } else {
2186 return false;
2187 }
0085fff8 2188 }
c70ad9f7 2189 }
2190}
2191
37b86a39 2192/**
32be99dc 2193 * get the count of completeds depending on the given groupid.
2194 *
2195 * @global object
2196 * @global object
2197 * @param object $feedback
2198 * @param int $groupid
2199 * @param int $courseid
2200 * @return mixed count of completeds or false
c70ad9f7 2201 */
2202function feedback_get_completeds_group_count($feedback, $groupid = false, $courseid = false) {
0085fff8 2203 global $CFG, $DB;
2204
2205 if ($courseid > 0 AND !$groupid <= 0) {
2206 $sql = "SELECT id, COUNT(item) AS ci
37b86a39 2207 FROM {feedback_value}
0085fff8 2208 WHERE course_id = ?
2209 GROUP BY item ORDER BY ci DESC";
2210 if ($foundrecs = $DB->get_records_sql($sql, array($courseid))) {
c70ad9f7 2211 $foundrecs = array_values($foundrecs);
2212 return $foundrecs[0]->ci;
2213 }
2214 return false;
2215 }
2216 if($values = feedback_get_completeds_group($feedback, $groupid)) {
2217 return sizeof($values);
2218 }else {
2219 return false;
2220 }
2221}
2222
2223/* get the own groupid.
2224@param object $course
2225@param object $cm
2226function feedback_get_groupid($course, $cm) {
2227 $groupmode = groupmode($course, $cm);
37b86a39 2228
c70ad9f7 2229 //get groupid
e3c7f155 2230 if($groupmode > 0 && !has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
c70ad9f7 2231 if($mygroupid = mygroupid($course->id)) {
2232 return $mygroupid[0]; //get the first groupid
2233 }
2234 }else {
2235 return false;
2236 }
2237}
2238 */
2239
37b86a39 2240/**
32be99dc 2241 * deletes all completed-recordsets from a feedback.
2242 * all related data such as values also will be deleted
2243 *
2244 * @global object
2245 * @param int $feedbackid
2246 * @return void
c70ad9f7 2247 */
2248function feedback_delete_all_completeds($feedbackid) {
0085fff8 2249 global $DB;
2250
2251 if (!$completeds = $DB->get_records('feedback_completed', array('feedback'=>$feedbackid))) {
2252 return;
2253 }
c70ad9f7 2254 foreach($completeds as $completed) {
2255 feedback_delete_completed($completed->id);
2256 }
2257}
2258
37b86a39 2259/**
32be99dc 2260 * deletes a completed given by completedid.
2261 * all related data such values or tracking data also will be deleted
d4b1d58c 2262 *
32be99dc 2263 * @global object
2264 * @param int $completedid
2265 * @return boolean
c70ad9f7 2266 */
2267function feedback_delete_completed($completedid) {
0085fff8 2268 global $DB;
2269
2270 if (!$completed = $DB->get_record('feedback_completed', array('id'=>$completedid))) {
c70ad9f7 2271 return false;
2272 }
2273 //first we delete all related values
0085fff8 2274 $DB->delete_records('feedback_value', array('completed'=>$completed->id));
37b86a39 2275
c70ad9f7 2276 //now we delete all tracking data
0085fff8 2277 if($tracking = $DB->get_record('feedback_tracking', array('completed'=>$completed->id, 'feedback'=>$completed->feedback))) {
2278 $DB->delete_records('feedback_tracking', array('completed'=>$completed->id));
c70ad9f7 2279 }
37b86a39 2280
c70ad9f7 2281 //last we delete the completed-record
0085fff8 2282 return $DB->delete_records('feedback_completed', array('id'=>$completed->id));
c70ad9f7 2283}
2284
2285////////////////////////////////////////////////
2286////////////////////////////////////////////////
2287////////////////////////////////////////////////
2288//functions to handle sitecourse mapping
2289////////////////////////////////////////////////
2290
37b86a39 2291/**
32be99dc 2292 * checks if the course and the feedback is in the table feedback_sitecourse_map.
2293 *
2294 * @global object
2295 * @param int $feedbackid
2296 * @param int $courseid
2297 * @return int the count of records
c70ad9f7 2298 */
2299function feedback_is_course_in_sitecourse_map($feedbackid, $courseid) {
0085fff8 2300 global $DB;
2301 return $DB->count_records('feedback_sitecourse_map', array('feedbackid'=>$feedbackid, 'courseid'=>$courseid));
c70ad9f7 2302}
2303
37b86a39 2304/**
32be99dc 2305 * checks if the feedback is in the table feedback_sitecourse_map.
2306 *
2307 * @global object
2308 * @param int $feedbackid
2309 * @return boolean
c70ad9f7 2310 */
2311function feedback_is_feedback_in_sitecourse_map($feedbackid) {
0085fff8 2312 global $Db;
2313 return $DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$feedbackid));
c70ad9f7 2314}
2315
37b86a39 2316/**
32be99dc 2317 * gets the feedbacks from table feedback_sitecourse_map.
2318 * this is used to show the global feedbacks on the feedback block
2319 * all feedbacks with the following criteria will be selected:<br />
2320 *
2321 * 1) all feedbacks which id are listed together with the courseid in sitecoursemap and<br />
2322 * 2) all feedbacks which not are listed in sitecoursemap
2323 *
2324 * @global object
2325 * @param int $courseid
2326 * @return array the feedback-records
c70ad9f7 2327 */
2328function feedback_get_feedbacks_from_sitecourse_map($courseid) {
0085fff8 2329 global $DB;
37b86a39 2330
c70ad9f7 2331 //first get all feedbacks listed in sitecourse_map with named courseid
0085fff8 2332 $sql = "SELECT f.id AS id, cm.id AS cmid, f.name AS name, f.timeopen AS timeopen, f.timeclose AS timeclose
2333 FROM {feedback} f, {course_modules} cm, {feedback_sitecourse_map} sm, {modules} m
2334 WHERE f.id = cm.instance
2335 AND f.course = '".SITEID."'
37b86a39 2336 AND m.id = cm.module
0085fff8 2337 AND m.name = 'feedback'
37b86a39 2338 AND sm.courseid = ?
0085fff8 2339 AND sm.feedbackid = f.id";
37b86a39 2340
0085fff8 2341 if (!$feedbacks1 = $DB->get_records_sql($sql, array($courseid))) {
c70ad9f7 2342 $feedbacks1 = array();
2343 }
37b86a39 2344
c70ad9f7 2345 //second get all feedbacks not listed in sitecourse_map
2346 $feedbacks2 = array();
0085fff8 2347 $sql = "SELECT f.id AS id, cm.id AS cmid, f.name AS name, f.timeopen AS timeopen, f.timeclose AS timeclose
2348 FROM {feedback} f, {course_modules} cm, {modules} m
2349 WHERE f.id = cm.instance
2350 AND f.course = '".SITEID."'
2351 AND m.id = cm.module
2352 AND m.name = 'feedback'";
2353 if (!$allfeedbacks = $DB->get_records_sql($sql)) {
c70ad9f7 2354 $allfeedbacks = array();
2355 }
2356 foreach($allfeedbacks as $a) {
0085fff8 2357 if(!$DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$a->id))) {
c70ad9f7 2358 $feedbacks2[] = $a;
2359 }
2360 }
37b86a39 2361
c70ad9f7 2362 return array_merge($feedbacks1, $feedbacks2);
37b86a39 2363
c70ad9f7 2364}
2365
37b86a39 2366/**
32be99dc 2367 * gets the courses from table feedback_sitecourse_map.
2368 *
2369 * @global object
2370 * @param int $feedbackid
2371 * @return array the course-records
c70ad9f7 2372 */
2373function feedback_get_courses_from_sitecourse_map($feedbackid) {
0085fff8 2374 global $DB;
37b86a39 2375
0085fff8 2376 $sql = "SELECT f.id, f.courseid, c.fullname, c.shortname
2377 FROM {feedback_sitecourse_map} f, {course} c
2378 WHERE c.id = f.courseid
2379 AND f.feedbackid = ?
2380 ORDER BY c.fullname";
37b86a39 2381
0085fff8 2382 return $DB->get_records_sql($sql, array($feedbackid));
37b86a39 2383
c70ad9f7 2384}
2385
37b86a39 2386/**
32be99dc 2387 * removes non existing courses or feedbacks from sitecourse_map.
2388 * it shouldn't be called all too often
2389 * a good place for it could be the mapcourse.php or unmapcourse.php
d4b1d58c 2390 *
32be99dc 2391 * @global object
2392 * @return void
c70ad9f7 2393 */
2394function feedback_clean_up_sitecourse_map() {
0085fff8 2395 global $DB;
2396
2397 $maps = $DB->get_records('feedback_sitecourse_map');
c70ad9f7 2398 foreach($maps as $map) {
0085fff8 2399 if (!$DB->get_record('course', array('id'=>$map->courseid))) {
2400 $DB->delete_records('feedback_sitecourse_map', array('courseid'=>$map->courseid, 'feedbackid'=>$map->feedbackid));
c70ad9f7 2401 continue;
2402 }
0085fff8 2403 if (!$DB->get_record('feedback', array('id'=>$map->feedbackid))) {
2404 $DB->delete_records('feedback_sitecourse_map', array('courseid'=>$map->courseid, 'feedbackid'=>$map->feedbackid));
c70ad9f7 2405 continue;
2406 }
37b86a39 2407
c70ad9f7 2408 }
2409}
2410
2411////////////////////////////////////////////////
2412////////////////////////////////////////////////
2413////////////////////////////////////////////////
2414//not relatable functions
2415////////////////////////////////////////////////
2416
37b86a39 2417/**
32be99dc 2418 * prints the option items of a selection-input item (dropdownlist).
2419 * @param int $startval the first value of the list
2420 * @param int $endval the last value of the list
2421 * @param int $selectval which item should be selected
2422 * @param int $interval the stepsize from the first to the last value
2423 * @return void
c70ad9f7 2424 */
2425function feedback_print_numeric_option_list($startval, $endval, $selectval = '', $interval = 1){
2426 for($i = $startval; $i <= $endval; $i += $interval){
2427 if($selectval == ($i)){
2428 $selected = 'selected="selected"';
2429 }else{
2430 $selected = '';
2431 }
2432 echo '<option '.$selected.'>'.$i.'</option>';
2433 }
2434}
2435
37b86a39 2436/**
32be99dc 2437 * sends an email to the teachers of the course where the given feedback is placed.
2438 *
2439 * @global object
2440 * @global object
2441 * @uses FEEDBACK_ANONYMOUS_NO
2442 * @uses FORMAT_PLAIN
2443 * @param object $cm the coursemodule-record
2444 * @param object $feedback
2445 * @param object $course
2446 * @param int $userid
2447 * @return void
c70ad9f7 2448 */
c184660d 2449function feedback_send_email($cm, $feedback, $course, $userid) {
c6307ef2 2450 global $CFG, $DB;
37b86a39 2451
c70ad9f7 2452 if ($feedback->email_notification == 0) { // No need to do anything
2453 return;
2454 }
37b86a39 2455
0085fff8 2456 $user = $DB->get_record('user', array('id'=>$userid));
37b86a39 2457
84973212 2458 if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
2459 $groupmode = $cm->groupmode;
2460 } else {
2461 $groupmode = $course->groupmode;
2462 }
2463
2464 if ($groupmode == SEPARATEGROUPS) {
c6307ef2 2465 $groups = $DB->get_records_sql_menu("SELECT g.name, g.id
2466 FROM {groups} g, {groups_members} m
2467 WHERE g.courseid = ?
2468 AND g.id = m.groupid
2469 AND m.userid = ?
2470 ORDER BY name ASC", array($course->id, $userid));
c70ad9f7 2471 $groups = array_values($groups);
37b86a39 2472
c184660d 2473 $teachers = feedback_get_receivemail_users($cm->id, $groups);
c70ad9f7 2474 } else {
c184660d 2475 $teachers = feedback_get_receivemail_users($cm->id);
c70ad9f7 2476 }
37b86a39 2477
c70ad9f7 2478 if ($teachers) {
2479
2480 $strfeedbacks = get_string('modulenameplural', 'feedback');
2481 $strfeedback = get_string('modulename', 'feedback');
2482 $strcompleted = get_string('completed', 'feedback');
2483 $printusername = $feedback->anonymous == FEEDBACK_ANONYMOUS_NO ? fullname($user) : get_string('anonymous_user', 'feedback');
37b86a39 2484
c70ad9f7 2485 foreach ($teachers as $teacher) {
0085fff8 2486 $info = new object();
c70ad9f7 2487 $info->username = $printusername;
2488 $info->feedback = format_string($feedback->name,true);
2489 $info->url = $CFG->wwwroot.'/mod/feedback/show_entries.php?id='.$cm->id.'&userid='.$userid.'&do_show=showentries';
2490
2491 $postsubject = $strcompleted.': '.$info->username.' -> '.$feedback->name;
c184660d 2492 $posttext = feedback_send_email_text($info, $course);
2493 $posthtml = ($teacher->mailformat == 1) ? feedback_send_email_html($info, $course, $cm) : '';
37b86a39 2494
c70ad9f7 2495 if($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
3b120e46 2496 $eventdata = new object();
645ba122
AG
2497 $eventdata->name = 'feedback';
2498 $eventdata->component = 'mod';
3b120e46 2499 $eventdata->userfrom = $user;
2500 $eventdata->userto = $teacher;
2501 $eventdata->subject = $postsubject;
2502 $eventdata->fullmessage = $posttext;
2503 $eventdata->fullmessageformat = FORMAT_PLAIN;
2504 $eventdata->fullmessagehtml = $posthtml;
2505 $eventdata->smallmessage = '';
7c7d3afa 2506 message_send($eventdata);
c70ad9f7 2507 }else {
3b120e46 2508 $eventdata = new object();
645ba122
AG
2509 $eventdata->name = 'feedback';
2510 $eventdata->component = 'mod';
3b120e46 2511 $eventdata->userfrom = $teacher;
2512 $eventdata->userto = $teacher;
2513 $eventdata->subject = $postsubject;
2514 $eventdata->fullmessage = $posttext;
2515 $eventdata->fullmessageformat = FORMAT_PLAIN;
2516 $eventdata->fullmessagehtml = $posthtml;
2517 $eventdata->smallmessage = '';
7c7d3afa 2518 message_send($eventdata);
c70ad9f7 2519 }
2520 }
2521 }
2522}
2523
37b86a39 2524/**
32be99dc 2525 * sends an email to the teachers of the course where the given feedback is placed.
2526 *
2527 * @global object
2528 * @uses FORMAT_PLAIN
2529 * @param object $cm the coursemodule-record
2530 * @param object $feedback
2531 * @param object $course
2532 * @return void
c70ad9f7 2533 */
c184660d 2534function feedback_send_email_anonym($cm, $feedback, $course) {
c70ad9f7 2535 global $CFG;
37b86a39 2536
c70ad9f7 2537 if ($feedback->email_notification == 0) { // No need to do anything
2538 return;
2539 }
37b86a39 2540
c184660d 2541 $teachers = feedback_get_receivemail_users($cm->id);
c70ad9f7 2542
2543 if ($teachers) {
2544
2545 $strfeedbacks = get_string('modulenameplural', 'feedback');
2546 $strfeedback = get_string('modulename', 'feedback');
2547 $strcompleted = get_string('completed', 'feedback');
2548 $printusername = get_string('anonymous_user', 'feedback');
37b86a39 2549
c70ad9f7 2550 foreach ($teachers as $teacher) {
0085fff8 2551 $info = new object();
c70ad9f7 2552 $info->username = $printusername;
2553 $info->feedback = format_string($feedback->name,true);
2554 $info->url = $CFG->wwwroot.'/mod/feedback/show_entries_anonym.php?id='.$cm->id;
2555
2556 $postsubject = $strcompleted.': '.$info->username.' -> '.$feedback->name;
c184660d 2557 $posttext = feedback_send_email_text($info, $course);
2558 $posthtml = ($teacher->mailformat == 1) ? feedback_send_email_html($info, $course, $cm) : '';
37b86a39 2559
3b120e46 2560 $eventdata = new object();
645ba122
AG
2561 $eventdata->name = 'feedback';
2562 $eventdata->component = 'mod';
3b120e46 2563 $eventdata->userfrom = $teacher;
2564 $eventdata->userto = $teacher;
2565 $eventdata->subject = $postsubject;
2566 $eventdata->fullmessage = $posttext;
2567 $eventdata->fullmessageformat = FORMAT_PLAIN;
2568 $eventdata->fullmessagehtml = $posthtml;
2569 $eventdata->smallmessage = '';
7c7d3afa 2570 message_send($eventdata);
c70ad9f7 2571 }
2572 }
2573}
2574
37b86a39 2575/**
32be99dc 2576 * send the text-part of the email
2577 *
2578 * @param object $info includes some infos about the feedback you want to send
2579 * @param object $course
2580 * @return string the text you want to post
c70ad9f7 2581 */
c184660d 2582function feedback_send_email_text($info, $course) {
c70ad9f7 2583 $posttext = $course->shortname.' -> '.get_string('modulenameplural', 'feedback').' -> '.
2584 $info->feedback."\n";
2585 $posttext .= '---------------------------------------------------------------------'."\n";
2586 $posttext .= get_string("emailteachermail", "feedback", $info)."\n";
2587 $posttext .= '---------------------------------------------------------------------'."\n";
2588 return $posttext;
2589}
2590
2591
37b86a39 2592/**
32be99dc 2593 * send the html-part of the email
2594 *
2595 * @global object
2596 * @param object $info includes some infos about the feedback you want to send
2597 * @param object $course
2598 * @return string the text you want to post
c70ad9f7 2599 */
c184660d 2600function feedback_send_email_html($info, $course, $cm) {
c70ad9f7 2601 global $CFG;
2602 $posthtml = '<p><font face="sans-serif">'.
5dee2210
AG
2603 '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->shortname.'</a> ->'.
2604 '<a href="'.$CFG->wwwroot.'/mod/feedback/index.php?id='.$course->id.'">'.get_string('modulenameplural', 'feedback').'</a> ->'.
2605 '<a href="'.$CFG->wwwroot.'/mod/feedback/view.php?id='.$cm->id.'">'.$info->feedback.'</a></font></p>';
c70ad9f7 2606 $posthtml .= '<hr /><font face="sans-serif">';
2607 $posthtml .= '<p>'.get_string('emailteachermailhtml', 'feedback', $info).'</p>';
2608 $posthtml .= '</font><hr />';
2609 return $posthtml;
2610}
2611
37b86a39 2612/**
32be99dc 2613 * print some errors to inform users about this.
2614 *
2615 * @global object
2616 * @return void
c70ad9f7 2617 */
2618function feedback_print_errors() {
37b86a39 2619
53b1f5da 2620 global $SESSION, $OUTPUT;
37b86a39 2621
f1b9e2df 2622 feedback_init_feedback_session();
2623
c70ad9f7 2624 if(empty($SESSION->feedback->errors)) {
78946b9b 2625 return;
c70ad9f7 2626 }
2627
d4b1d58c 2628 echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter boxwidthnormal');
53b1f5da 2629 echo $OUTPUT->heading(get_string('handling_error', 'feedback'));
c70ad9f7 2630
2631 echo '<p align="center"><b><font color="black"><pre>';
2632 print_r($SESSION->feedback->errors) . "\n";
2633 echo '</pre></font></b></p>';
37b86a39 2634
d4b1d58c 2635 echo $OUTPUT->box_end();
c70ad9f7 2636 echo '<br /><br />';
2637 $SESSION->feedback->errors = array(); //remove errors
37b86a39 2638}
c70ad9f7 2639
32be99dc 2640/**
2641 * @param string $url
2642 * @return string
2643 */
c184660d 2644function feedback_encode_target_url($url) {
2645 if (strpos($url, '?')) {
2646 list($part1, $part2) = explode('?', $url, 2); //maximal 2 parts
2647 return $part1 . '?' . htmlentities($part2);
2648 } else {
2649 return $url;
2650 }
2651}
ab5bd34d 2652
0b29477b
SH
2653/**
2654 * Adds module specific settings to the settings block
2655 *
2656 * @param settings_navigation $settings The settings navigation object
2657 * @param navigation_node $feedbacknode The node to add module settings to
2658 */
2659function feedback_extend_settings_navigation(settings_navigation $settings, navigation_node $feedbacknode) {
2660 global $PAGE, $DB;
ab5bd34d 2661
c987dbad 2662 if (!$context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id)) {
3406acde 2663 print_error('badcontext');
c987dbad
AG
2664 }
2665
3406acde
SH
2666 if (has_capability('mod/feedback:edititems', $context)) {
2667 $questionnode = $feedbacknode->add(get_string('questions', 'feedback'));
2668 $questionnode->add(get_string('edit_items', 'feedback'), new moodle_url('/mod/feedback/edit.php', array('id'=>$PAGE->cm->id, 'do_show'=>'edit')));
2669 $questionnode->add(get_string('export_questions', 'feedback'), new moodle_url('/mod/feedback/export.php', array('id'=>$PAGE->cm->id, 'action'=>'exportfile')));
2670 $questionnode->add(get_string('import_questions', 'feedback'), new moodle_url('/mod/feedback/import.php', array('id'=>$PAGE->cm->id)));
2671 $questionnode->add(get_string('templates', 'feedback'), new moodle_url('/mod/feedback/edit.php', array('id'=>$PAGE->cm->id, 'do_show'=>'templates')));
ab5bd34d 2672 }
2673
3406acde 2674 if (has_capability('mod/feedback:viewreports', $context)) {
0b29477b 2675 $feedback = $DB->get_record('feedback', array('id'=>$PAGE->cm->instance));
ab5bd34d 2676 if($feedback->course == SITEID){
0b29477b 2677 $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 2678 }else {
0b29477b 2679 $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 2680 }
ab5bd34d 2681
0b29477b 2682 $feedbacknode->add(get_string('show_entries', 'feedback'), new moodle_url('/mod/feedback/show_entries.php', array('id'=>$PAGE->cm->id, 'do_show'=>'showentries')));
ab5bd34d 2683 }
c987dbad 2684}
f1b9e2df 2685
2686function feedback_init_feedback_session() {
2687 //initialize the feedback-Session - not nice at all!!
2688 global $SESSION;
2689 if (!empty($SESSION)) {
2690 if (!isset($SESSION->feedback) OR !is_object($SESSION->feedback)) {
2691 $SESSION->feedback = new object();
2692 }
2693 }
ab5bd34d 2694}