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