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