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