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