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