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