MDL-48430 custom_menu: Malformed url in custom menu cannot break the platform
[moodle.git] / mod / feedback / lib.php
blob9752180946f17972797ecda6ca1b34127a466d81
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Library of functions and constants for module feedback
19 * includes the main-part of feedback-functions
21 * @package mod_feedback
22 * @copyright Andreas Grabs
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 /** Include eventslib.php */
27 require_once($CFG->libdir.'/eventslib.php');
28 /** Include calendar/lib.php */
29 require_once($CFG->dirroot.'/calendar/lib.php');
30 // Include forms lib.
31 require_once($CFG->libdir.'/formslib.php');
33 define('FEEDBACK_ANONYMOUS_YES', 1);
34 define('FEEDBACK_ANONYMOUS_NO', 2);
35 define('FEEDBACK_MIN_ANONYMOUS_COUNT_IN_GROUP', 2);
36 define('FEEDBACK_DECIMAL', '.');
37 define('FEEDBACK_THOUSAND', ',');
38 define('FEEDBACK_RESETFORM_RESET', 'feedback_reset_data_');
39 define('FEEDBACK_RESETFORM_DROP', 'feedback_drop_feedback_');
40 define('FEEDBACK_MAX_PIX_LENGTH', '400'); //max. Breite des grafischen Balkens in der Auswertung
41 define('FEEDBACK_DEFAULT_PAGE_COUNT', 20);
43 /**
44 * Returns all other caps used in module.
46 * @return array
48 function feedback_get_extra_capabilities() {
49 return array('moodle/site:accessallgroups');
52 /**
53 * @uses FEATURE_GROUPS
54 * @uses FEATURE_GROUPINGS
55 * @uses FEATURE_GROUPMEMBERSONLY
56 * @uses FEATURE_MOD_INTRO
57 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
58 * @uses FEATURE_GRADE_HAS_GRADE
59 * @uses FEATURE_GRADE_OUTCOMES
60 * @param string $feature FEATURE_xx constant for requested feature
61 * @return mixed True if module supports feature, null if doesn't know
63 function feedback_supports($feature) {
64 switch($feature) {
65 case FEATURE_GROUPS: return true;
66 case FEATURE_GROUPINGS: return true;
67 case FEATURE_GROUPMEMBERSONLY: return true;
68 case FEATURE_MOD_INTRO: return true;
69 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
70 case FEATURE_COMPLETION_HAS_RULES: return true;
71 case FEATURE_GRADE_HAS_GRADE: return false;
72 case FEATURE_GRADE_OUTCOMES: return false;
73 case FEATURE_BACKUP_MOODLE2: return true;
74 case FEATURE_SHOW_DESCRIPTION: return true;
76 default: return null;
80 /**
81 * this will create a new instance and return the id number
82 * of the new instance.
84 * @global object
85 * @param object $feedback the object given by mod_feedback_mod_form
86 * @return int
88 function feedback_add_instance($feedback) {
89 global $DB;
91 $feedback->timemodified = time();
92 $feedback->id = '';
94 if (empty($feedback->site_after_submit)) {
95 $feedback->site_after_submit = '';
98 //saving the feedback in db
99 $feedbackid = $DB->insert_record("feedback", $feedback);
101 $feedback->id = $feedbackid;
103 feedback_set_events($feedback);
105 if (!isset($feedback->coursemodule)) {
106 $cm = get_coursemodule_from_id('feedback', $feedback->id);
107 $feedback->coursemodule = $cm->id;
109 $context = context_module::instance($feedback->coursemodule);
111 $editoroptions = feedback_get_editor_options();
113 // process the custom wysiwyg editor in page_after_submit
114 if ($draftitemid = $feedback->page_after_submit_editor['itemid']) {
115 $feedback->page_after_submit = file_save_draft_area_files($draftitemid, $context->id,
116 'mod_feedback', 'page_after_submit',
117 0, $editoroptions,
118 $feedback->page_after_submit_editor['text']);
120 $feedback->page_after_submitformat = $feedback->page_after_submit_editor['format'];
122 $DB->update_record('feedback', $feedback);
124 return $feedbackid;
128 * this will update a given instance
130 * @global object
131 * @param object $feedback the object given by mod_feedback_mod_form
132 * @return boolean
134 function feedback_update_instance($feedback) {
135 global $DB;
137 $feedback->timemodified = time();
138 $feedback->id = $feedback->instance;
140 if (empty($feedback->site_after_submit)) {
141 $feedback->site_after_submit = '';
144 //save the feedback into the db
145 $DB->update_record("feedback", $feedback);
147 //create or update the new events
148 feedback_set_events($feedback);
150 $context = context_module::instance($feedback->coursemodule);
152 $editoroptions = feedback_get_editor_options();
154 // process the custom wysiwyg editor in page_after_submit
155 if ($draftitemid = $feedback->page_after_submit_editor['itemid']) {
156 $feedback->page_after_submit = file_save_draft_area_files($draftitemid, $context->id,
157 'mod_feedback', 'page_after_submit',
158 0, $editoroptions,
159 $feedback->page_after_submit_editor['text']);
161 $feedback->page_after_submitformat = $feedback->page_after_submit_editor['format'];
163 $DB->update_record('feedback', $feedback);
165 return true;
169 * Serves the files included in feedback items like label. Implements needed access control ;-)
171 * There are two situations in general where the files will be sent.
172 * 1) filearea = item, 2) filearea = template
174 * @package mod_feedback
175 * @category files
176 * @param stdClass $course course object
177 * @param stdClass $cm course module object
178 * @param stdClass $context context object
179 * @param string $filearea file area
180 * @param array $args extra arguments
181 * @param bool $forcedownload whether or not force download
182 * @param array $options additional options affecting the file serving
183 * @return bool false if file not found, does not return if found - justsend the file
185 function feedback_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
186 global $CFG, $DB;
188 if ($filearea === 'item' or $filearea === 'template') {
189 $itemid = (int)array_shift($args);
190 //get the item what includes the file
191 if (!$item = $DB->get_record('feedback_item', array('id'=>$itemid))) {
192 return false;
194 $feedbackid = $item->feedback;
195 $templateid = $item->template;
198 if ($filearea === 'page_after_submit' or $filearea === 'item') {
199 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
200 return false;
203 $feedbackid = $feedback->id;
205 //if the filearea is "item" so we check the permissions like view/complete the feedback
206 $canload = false;
207 //first check whether the user has the complete capability
208 if (has_capability('mod/feedback:complete', $context)) {
209 $canload = true;
212 //now we check whether the user has the view capability
213 if (has_capability('mod/feedback:view', $context)) {
214 $canload = true;
217 //if the feedback is on frontpage and anonymous and the fullanonymous is allowed
218 //so the file can be loaded too.
219 if (isset($CFG->feedback_allowfullanonymous)
220 AND $CFG->feedback_allowfullanonymous
221 AND $course->id == SITEID
222 AND $feedback->anonymous == FEEDBACK_ANONYMOUS_YES ) {
223 $canload = true;
226 if (!$canload) {
227 return false;
229 } else if ($filearea === 'template') { //now we check files in templates
230 if (!$template = $DB->get_record('feedback_template', array('id'=>$templateid))) {
231 return false;
234 //if the file is not public so the capability edititems has to be there
235 if (!$template->ispublic) {
236 if (!has_capability('mod/feedback:edititems', $context)) {
237 return false;
239 } else { //on public templates, at least the user has to be logged in
240 if (!isloggedin()) {
241 return false;
244 } else {
245 return false;
248 if ($context->contextlevel == CONTEXT_MODULE) {
249 if ($filearea !== 'item' and $filearea !== 'page_after_submit') {
250 return false;
254 if ($context->contextlevel == CONTEXT_COURSE || $context->contextlevel == CONTEXT_SYSTEM) {
255 if ($filearea !== 'template') {
256 return false;
260 $relativepath = implode('/', $args);
261 if ($filearea === 'page_after_submit') {
262 $fullpath = "/{$context->id}/mod_feedback/$filearea/$relativepath";
263 } else {
264 $fullpath = "/{$context->id}/mod_feedback/$filearea/{$item->id}/$relativepath";
267 $fs = get_file_storage();
269 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
270 return false;
273 // finally send the file
274 send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
276 return false;
280 * this will delete a given instance.
281 * all referenced data also will be deleted
283 * @global object
284 * @param int $id the instanceid of feedback
285 * @return boolean
287 function feedback_delete_instance($id) {
288 global $DB;
290 //get all referenced items
291 $feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$id));
293 //deleting all referenced items and values
294 if (is_array($feedbackitems)) {
295 foreach ($feedbackitems as $feedbackitem) {
296 $DB->delete_records("feedback_value", array("item"=>$feedbackitem->id));
297 $DB->delete_records("feedback_valuetmp", array("item"=>$feedbackitem->id));
299 if ($delitems = $DB->get_records("feedback_item", array("feedback"=>$id))) {
300 foreach ($delitems as $delitem) {
301 feedback_delete_item($delitem->id, false);
306 //deleting the referenced tracking data
307 $DB->delete_records('feedback_tracking', array('feedback'=>$id));
309 //deleting the completeds
310 $DB->delete_records("feedback_completed", array("feedback"=>$id));
312 //deleting the unfinished completeds
313 $DB->delete_records("feedback_completedtmp", array("feedback"=>$id));
315 //deleting old events
316 $DB->delete_records('event', array('modulename'=>'feedback', 'instance'=>$id));
317 return $DB->delete_records("feedback", array("id"=>$id));
321 * this is called after deleting all instances if the course will be deleted.
322 * only templates have to be deleted
324 * @global object
325 * @param object $course
326 * @return boolean
328 function feedback_delete_course($course) {
329 global $DB;
331 //delete all templates of given course
332 return $DB->delete_records('feedback_template', array('course'=>$course->id));
336 * Return a small object with summary information about what a
337 * user has done with a given particular instance of this module
338 * Used for user activity reports.
339 * $return->time = the time they did it
340 * $return->info = a short text description
342 * @param object $course
343 * @param object $user
344 * @param object $mod
345 * @param object $feedback
346 * @return object
348 function feedback_user_outline($course, $user, $mod, $feedback) {
349 return null;
353 * Returns all users who has completed a specified feedback since a given time
354 * many thanks to Manolescu Dorel, who contributed these two functions
356 * @global object
357 * @global object
358 * @global object
359 * @global object
360 * @uses CONTEXT_MODULE
361 * @param array $activities Passed by reference
362 * @param int $index Passed by reference
363 * @param int $timemodified Timestamp
364 * @param int $courseid
365 * @param int $cmid
366 * @param int $userid
367 * @param int $groupid
368 * @return void
370 function feedback_get_recent_mod_activity(&$activities, &$index,
371 $timemodified, $courseid,
372 $cmid, $userid="", $groupid="") {
374 global $CFG, $COURSE, $USER, $DB;
376 if ($COURSE->id == $courseid) {
377 $course = $COURSE;
378 } else {
379 $course = $DB->get_record('course', array('id'=>$courseid));
382 $modinfo = get_fast_modinfo($course);
384 $cm = $modinfo->cms[$cmid];
386 $sqlargs = array();
388 $userfields = user_picture::fields('u', null, 'useridagain');
389 $sql = " SELECT fk . * , fc . * , $userfields
390 FROM {feedback_completed} fc
391 JOIN {feedback} fk ON fk.id = fc.feedback
392 JOIN {user} u ON u.id = fc.userid ";
394 if ($groupid) {
395 $sql .= " JOIN {groups_members} gm ON gm.userid=u.id ";
398 $sql .= " WHERE fc.timemodified > ? AND fk.id = ? ";
399 $sqlargs[] = $timemodified;
400 $sqlargs[] = $cm->instance;
402 if ($userid) {
403 $sql .= " AND u.id = ? ";
404 $sqlargs[] = $userid;
407 if ($groupid) {
408 $sql .= " AND gm.groupid = ? ";
409 $sqlargs[] = $groupid;
412 if (!$feedbackitems = $DB->get_records_sql($sql, $sqlargs)) {
413 return;
416 $cm_context = context_module::instance($cm->id);
418 if (!has_capability('mod/feedback:view', $cm_context)) {
419 return;
422 $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
423 $viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context);
424 $groupmode = groups_get_activity_groupmode($cm, $course);
426 $aname = format_string($cm->name, true);
427 foreach ($feedbackitems as $feedbackitem) {
428 if ($feedbackitem->userid != $USER->id) {
430 if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
431 $usersgroups = groups_get_all_groups($course->id,
432 $feedbackitem->userid,
433 $cm->groupingid);
434 if (!is_array($usersgroups)) {
435 continue;
437 $usersgroups = array_keys($usersgroups);
438 $intersect = array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid));
439 if (empty($intersect)) {
440 continue;
445 $tmpactivity = new stdClass();
447 $tmpactivity->type = 'feedback';
448 $tmpactivity->cmid = $cm->id;
449 $tmpactivity->name = $aname;
450 $tmpactivity->sectionnum= $cm->sectionnum;
451 $tmpactivity->timestamp = $feedbackitem->timemodified;
453 $tmpactivity->content = new stdClass();
454 $tmpactivity->content->feedbackid = $feedbackitem->id;
455 $tmpactivity->content->feedbackuserid = $feedbackitem->userid;
457 $tmpactivity->user = user_picture::unalias($feedbackitem, null, 'useridagain');
458 $tmpactivity->user->fullname = fullname($feedbackitem, $viewfullnames);
460 $activities[$index++] = $tmpactivity;
463 return;
467 * Prints all users who has completed a specified feedback since a given time
468 * many thanks to Manolescu Dorel, who contributed these two functions
470 * @global object
471 * @param object $activity
472 * @param int $courseid
473 * @param string $detail
474 * @param array $modnames
475 * @return void Output is echo'd
477 function feedback_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
478 global $CFG, $OUTPUT;
480 echo '<table border="0" cellpadding="3" cellspacing="0" class="forum-recent">';
482 echo "<tr><td class=\"userpicture\" valign=\"top\">";
483 echo $OUTPUT->user_picture($activity->user, array('courseid'=>$courseid));
484 echo "</td><td>";
486 if ($detail) {
487 $modname = $modnames[$activity->type];
488 echo '<div class="title">';
489 echo "<img src=\"" . $OUTPUT->pix_url('icon', $activity->type) . "\" ".
490 "class=\"icon\" alt=\"$modname\" />";
491 echo "<a href=\"$CFG->wwwroot/mod/feedback/view.php?id={$activity->cmid}\">{$activity->name}</a>";
492 echo '</div>';
495 echo '<div class="title">';
496 echo '</div>';
498 echo '<div class="user">';
499 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&amp;course=$courseid\">"
500 ."{$activity->user->fullname}</a> - ".userdate($activity->timestamp);
501 echo '</div>';
503 echo "</td></tr></table>";
505 return;
509 * Obtains the automatic completion state for this feedback based on the condition
510 * in feedback settings.
512 * @param object $course Course
513 * @param object $cm Course-module
514 * @param int $userid User ID
515 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
516 * @return bool True if completed, false if not, $type if conditions not set.
518 function feedback_get_completion_state($course, $cm, $userid, $type) {
519 global $CFG, $DB;
521 // Get feedback details
522 $feedback = $DB->get_record('feedback', array('id'=>$cm->instance), '*', MUST_EXIST);
524 // If completion option is enabled, evaluate it and return true/false
525 if ($feedback->completionsubmit) {
526 $params = array('userid'=>$userid, 'feedback'=>$feedback->id);
527 return $DB->record_exists('feedback_tracking', $params);
528 } else {
529 // Completion option is not enabled so just return $type
530 return $type;
536 * Print a detailed representation of what a user has done with
537 * a given particular instance of this module, for user activity reports.
539 * @param object $course
540 * @param object $user
541 * @param object $mod
542 * @param object $feedback
543 * @return bool
545 function feedback_user_complete($course, $user, $mod, $feedback) {
546 return true;
550 * @return bool true
552 function feedback_cron () {
553 return true;
557 * @return bool false
559 function feedback_scale_used ($feedbackid, $scaleid) {
560 return false;
564 * Checks if scale is being used by any instance of feedback
566 * This is used to find out if scale used anywhere
567 * @param $scaleid int
568 * @return boolean True if the scale is used by any assignment
570 function feedback_scale_used_anywhere($scaleid) {
571 return false;
575 * List the actions that correspond to a view of this module.
576 * This is used by the participation report.
578 * Note: This is not used by new logging system. Event with
579 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
580 * be considered as view action.
582 * @return array
584 function feedback_get_view_actions() {
585 return array('view', 'view all');
589 * List the actions that correspond to a post of this module.
590 * This is used by the participation report.
592 * Note: This is not used by new logging system. Event with
593 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
594 * will be considered as post action.
596 * @return array
598 function feedback_get_post_actions() {
599 return array('submit');
603 * This function is used by the reset_course_userdata function in moodlelib.
604 * This function will remove all responses from the specified feedback
605 * and clean up any related data.
607 * @global object
608 * @global object
609 * @uses FEEDBACK_RESETFORM_RESET
610 * @uses FEEDBACK_RESETFORM_DROP
611 * @param object $data the data submitted from the reset course.
612 * @return array status array
614 function feedback_reset_userdata($data) {
615 global $CFG, $DB;
617 $resetfeedbacks = array();
618 $dropfeedbacks = array();
619 $status = array();
620 $componentstr = get_string('modulenameplural', 'feedback');
622 //get the relevant entries from $data
623 foreach ($data as $key => $value) {
624 switch(true) {
625 case substr($key, 0, strlen(FEEDBACK_RESETFORM_RESET)) == FEEDBACK_RESETFORM_RESET:
626 if ($value == 1) {
627 $templist = explode('_', $key);
628 if (isset($templist[3])) {
629 $resetfeedbacks[] = intval($templist[3]);
632 break;
633 case substr($key, 0, strlen(FEEDBACK_RESETFORM_DROP)) == FEEDBACK_RESETFORM_DROP:
634 if ($value == 1) {
635 $templist = explode('_', $key);
636 if (isset($templist[3])) {
637 $dropfeedbacks[] = intval($templist[3]);
640 break;
644 //reset the selected feedbacks
645 foreach ($resetfeedbacks as $id) {
646 $feedback = $DB->get_record('feedback', array('id'=>$id));
647 feedback_delete_all_completeds($id);
648 $status[] = array('component'=>$componentstr.':'.$feedback->name,
649 'item'=>get_string('resetting_data', 'feedback'),
650 'error'=>false);
653 return $status;
657 * Called by course/reset.php
659 * @global object
660 * @uses FEEDBACK_RESETFORM_RESET
661 * @param object $mform form passed by reference
663 function feedback_reset_course_form_definition(&$mform) {
664 global $COURSE, $DB;
666 $mform->addElement('header', 'feedbackheader', get_string('modulenameplural', 'feedback'));
668 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$COURSE->id), 'name')) {
669 return;
672 $mform->addElement('static', 'hint', get_string('resetting_data', 'feedback'));
673 foreach ($feedbacks as $feedback) {
674 $mform->addElement('checkbox', FEEDBACK_RESETFORM_RESET.$feedback->id, $feedback->name);
679 * Course reset form defaults.
681 * @global object
682 * @uses FEEDBACK_RESETFORM_RESET
683 * @param object $course
685 function feedback_reset_course_form_defaults($course) {
686 global $DB;
688 $return = array();
689 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id), 'name')) {
690 return;
692 foreach ($feedbacks as $feedback) {
693 $return[FEEDBACK_RESETFORM_RESET.$feedback->id] = true;
695 return $return;
699 * Called by course/reset.php and shows the formdata by coursereset.
700 * it prints checkboxes for each feedback available at the given course
701 * there are two checkboxes:
702 * 1) delete userdata and keep the feedback
703 * 2) delete userdata and drop the feedback
705 * @global object
706 * @uses FEEDBACK_RESETFORM_RESET
707 * @uses FEEDBACK_RESETFORM_DROP
708 * @param object $course
709 * @return void
711 function feedback_reset_course_form($course) {
712 global $DB, $OUTPUT;
714 echo get_string('resetting_feedbacks', 'feedback'); echo ':<br />';
715 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id), 'name')) {
716 return;
719 foreach ($feedbacks as $feedback) {
720 echo '<p>';
721 echo get_string('name', 'feedback').': '.$feedback->name.'<br />';
722 echo html_writer::checkbox(FEEDBACK_RESETFORM_RESET.$feedback->id,
723 1, true,
724 get_string('resetting_data', 'feedback'));
725 echo '<br />';
726 echo html_writer::checkbox(FEEDBACK_RESETFORM_DROP.$feedback->id,
727 1, false,
728 get_string('drop_feedback', 'feedback'));
729 echo '</p>';
734 * This gets an array with default options for the editor
736 * @return array the options
738 function feedback_get_editor_options() {
739 return array('maxfiles' => EDITOR_UNLIMITED_FILES,
740 'trusttext'=>true);
744 * This creates new events given as timeopen and closeopen by $feedback.
746 * @global object
747 * @param object $feedback
748 * @return void
750 function feedback_set_events($feedback) {
751 global $DB;
753 // adding the feedback to the eventtable (I have seen this at quiz-module)
754 $DB->delete_records('event', array('modulename'=>'feedback', 'instance'=>$feedback->id));
756 if (!isset($feedback->coursemodule)) {
757 $cm = get_coursemodule_from_id('feedback', $feedback->id);
758 $feedback->coursemodule = $cm->id;
761 // the open-event
762 if ($feedback->timeopen > 0) {
763 $event = new stdClass();
764 $event->name = get_string('start', 'feedback').' '.$feedback->name;
765 $event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
766 $event->courseid = $feedback->course;
767 $event->groupid = 0;
768 $event->userid = 0;
769 $event->modulename = 'feedback';
770 $event->instance = $feedback->id;
771 $event->eventtype = 'open';
772 $event->timestart = $feedback->timeopen;
773 $event->visible = instance_is_visible('feedback', $feedback);
774 if ($feedback->timeclose > 0) {
775 $event->timeduration = ($feedback->timeclose - $feedback->timeopen);
776 } else {
777 $event->timeduration = 0;
780 calendar_event::create($event);
783 // the close-event
784 if ($feedback->timeclose > 0) {
785 $event = new stdClass();
786 $event->name = get_string('stop', 'feedback').' '.$feedback->name;
787 $event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
788 $event->courseid = $feedback->course;
789 $event->groupid = 0;
790 $event->userid = 0;
791 $event->modulename = 'feedback';
792 $event->instance = $feedback->id;
793 $event->eventtype = 'close';
794 $event->timestart = $feedback->timeclose;
795 $event->visible = instance_is_visible('feedback', $feedback);
796 $event->timeduration = 0;
798 calendar_event::create($event);
803 * this function is called by {@link feedback_delete_userdata()}
804 * it drops the feedback-instance from the course_module table
806 * @global object
807 * @param int $id the id from the coursemodule
808 * @return boolean
810 function feedback_delete_course_module($id) {
811 global $DB;
813 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
814 return true;
816 return $DB->delete_records('course_modules', array('id'=>$cm->id));
821 ////////////////////////////////////////////////
822 //functions to handle capabilities
823 ////////////////////////////////////////////////
826 * returns the context-id related to the given coursemodule-id
828 * @staticvar object $context
829 * @param int $cmid the coursemodule-id
830 * @return object $context
832 function feedback_get_context($cmid) {
833 static $context;
835 if (isset($context)) {
836 return $context;
839 $context = context_module::instance($cmid);
840 return $context;
844 * returns true if the current role is faked by switching role feature
846 * @global object
847 * @return boolean
849 function feedback_check_is_switchrole() {
850 global $USER;
851 if (isset($USER->switchrole) AND
852 is_array($USER->switchrole) AND
853 count($USER->switchrole) > 0) {
855 return true;
857 return false;
861 * count users which have not completed the feedback
863 * @global object
864 * @uses CONTEXT_MODULE
865 * @param object $cm
866 * @param int $group single groupid
867 * @param string $sort
868 * @param int $startpage
869 * @param int $pagecount
870 * @return object the userrecords
872 function feedback_get_incomplete_users($cm,
873 $group = false,
874 $sort = '',
875 $startpage = false,
876 $pagecount = false) {
878 global $DB;
880 $context = context_module::instance($cm->id);
882 //first get all user who can complete this feedback
883 $cap = 'mod/feedback:complete';
884 $fields = 'u.id, u.username';
885 if (!$allusers = get_users_by_capability($context,
886 $cap,
887 $fields,
888 $sort,
891 $group,
893 true)) {
894 return false;
896 // Filter users that are not in the correct group/grouping.
897 $allusers = groups_filter_users_by_course_module_visible($cm, $allusers);
899 $allusers = array_keys($allusers);
901 //now get all completeds
902 $params = array('feedback'=>$cm->instance);
903 if (!$completedusers = $DB->get_records_menu('feedback_completed', $params, '', 'userid,id')) {
904 return $allusers;
906 $completedusers = array_keys($completedusers);
908 //now strike all completedusers from allusers
909 $allusers = array_diff($allusers, $completedusers);
911 //for paging I use array_slice()
912 if ($startpage !== false AND $pagecount !== false) {
913 $allusers = array_slice($allusers, $startpage, $pagecount);
916 return $allusers;
920 * count users which have not completed the feedback
922 * @global object
923 * @param object $cm
924 * @param int $group single groupid
925 * @return int count of userrecords
927 function feedback_count_incomplete_users($cm, $group = false) {
928 if ($allusers = feedback_get_incomplete_users($cm, $group)) {
929 return count($allusers);
931 return 0;
935 * count users which have completed a feedback
937 * @global object
938 * @uses FEEDBACK_ANONYMOUS_NO
939 * @param object $cm
940 * @param int $group single groupid
941 * @return int count of userrecords
943 function feedback_count_complete_users($cm, $group = false) {
944 global $DB;
946 $params = array(FEEDBACK_ANONYMOUS_NO, $cm->instance);
948 $fromgroup = '';
949 $wheregroup = '';
950 if ($group) {
951 $fromgroup = ', {groups_members} g';
952 $wheregroup = ' AND g.groupid = ? AND g.userid = c.userid';
953 $params[] = $group;
956 $sql = 'SELECT COUNT(u.id) FROM {user} u, {feedback_completed} c'.$fromgroup.'
957 WHERE anonymous_response = ? AND u.id = c.userid AND c.feedback = ?
958 '.$wheregroup;
960 return $DB->count_records_sql($sql, $params);
965 * get users which have completed a feedback
967 * @global object
968 * @uses CONTEXT_MODULE
969 * @uses FEEDBACK_ANONYMOUS_NO
970 * @param object $cm
971 * @param int $group single groupid
972 * @param string $where a sql where condition (must end with " AND ")
973 * @param array parameters used in $where
974 * @param string $sort a table field
975 * @param int $startpage
976 * @param int $pagecount
977 * @return object the userrecords
979 function feedback_get_complete_users($cm,
980 $group = false,
981 $where = '',
982 array $params = null,
983 $sort = '',
984 $startpage = false,
985 $pagecount = false) {
987 global $DB;
989 $context = context_module::instance($cm->id);
991 $params = (array)$params;
993 $params['anon'] = FEEDBACK_ANONYMOUS_NO;
994 $params['instance'] = $cm->instance;
996 $fromgroup = '';
997 $wheregroup = '';
998 if ($group) {
999 $fromgroup = ', {groups_members} g';
1000 $wheregroup = ' AND g.groupid = :group AND g.userid = c.userid';
1001 $params['group'] = $group;
1004 if ($sort) {
1005 $sortsql = ' ORDER BY '.$sort;
1006 } else {
1007 $sortsql = '';
1010 $ufields = user_picture::fields('u');
1011 $sql = 'SELECT DISTINCT '.$ufields.', c.timemodified as completed_timemodified
1012 FROM {user} u, {feedback_completed} c '.$fromgroup.'
1013 WHERE '.$where.' anonymous_response = :anon
1014 AND u.id = c.userid
1015 AND c.feedback = :instance
1016 '.$wheregroup.$sortsql;
1018 if ($startpage === false OR $pagecount === false) {
1019 $startpage = false;
1020 $pagecount = false;
1022 return $DB->get_records_sql($sql, $params, $startpage, $pagecount);
1026 * get users which have the viewreports-capability
1028 * @uses CONTEXT_MODULE
1029 * @param int $cmid
1030 * @param mixed $groups single groupid or array of groupids - group(s) user is in
1031 * @return object the userrecords
1033 function feedback_get_viewreports_users($cmid, $groups = false) {
1035 $context = context_module::instance($cmid);
1037 //description of the call below:
1038 //get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='',
1039 // $limitnum='', $groups='', $exceptions='', $doanything=true)
1040 return get_users_by_capability($context,
1041 'mod/feedback:viewreports',
1043 'lastname',
1046 $groups,
1048 false);
1052 * get users which have the receivemail-capability
1054 * @uses CONTEXT_MODULE
1055 * @param int $cmid
1056 * @param mixed $groups single groupid or array of groupids - group(s) user is in
1057 * @return object the userrecords
1059 function feedback_get_receivemail_users($cmid, $groups = false) {
1061 $context = context_module::instance($cmid);
1063 //description of the call below:
1064 //get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='',
1065 // $limitnum='', $groups='', $exceptions='', $doanything=true)
1066 return get_users_by_capability($context,
1067 'mod/feedback:receivemail',
1069 'lastname',
1072 $groups,
1074 false);
1077 ////////////////////////////////////////////////
1078 //functions to handle the templates
1079 ////////////////////////////////////////////////
1080 ////////////////////////////////////////////////
1083 * creates a new template-record.
1085 * @global object
1086 * @param int $courseid
1087 * @param string $name the name of template shown in the templatelist
1088 * @param int $ispublic 0:privat 1:public
1089 * @return int the new templateid
1091 function feedback_create_template($courseid, $name, $ispublic = 0) {
1092 global $DB;
1094 $templ = new stdClass();
1095 $templ->course = ($ispublic ? 0 : $courseid);
1096 $templ->name = $name;
1097 $templ->ispublic = $ispublic;
1099 $templid = $DB->insert_record('feedback_template', $templ);
1100 return $DB->get_record('feedback_template', array('id'=>$templid));
1104 * creates new template items.
1105 * all items will be copied and the attribute feedback will be set to 0
1106 * and the attribute template will be set to the new templateid
1108 * @global object
1109 * @uses CONTEXT_MODULE
1110 * @uses CONTEXT_COURSE
1111 * @param object $feedback
1112 * @param string $name the name of template shown in the templatelist
1113 * @param int $ispublic 0:privat 1:public
1114 * @return boolean
1116 function feedback_save_as_template($feedback, $name, $ispublic = 0) {
1117 global $DB;
1118 $fs = get_file_storage();
1120 if (!$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
1121 return false;
1124 if (!$newtempl = feedback_create_template($feedback->course, $name, $ispublic)) {
1125 return false;
1128 //files in the template_item are in the context of the current course or
1129 //if the template is public the files are in the system context
1130 //files in the feedback_item are in the feedback_context of the feedback
1131 if ($ispublic) {
1132 $s_context = context_system::instance();
1133 } else {
1134 $s_context = context_course::instance($newtempl->course);
1136 $cm = get_coursemodule_from_instance('feedback', $feedback->id);
1137 $f_context = context_module::instance($cm->id);
1139 //create items of this new template
1140 //depend items we are storing temporary in an mapping list array(new id => dependitem)
1141 //we also store a mapping of all items array(oldid => newid)
1142 $dependitemsmap = array();
1143 $itembackup = array();
1144 foreach ($feedbackitems as $item) {
1146 $t_item = clone($item);
1148 unset($t_item->id);
1149 $t_item->feedback = 0;
1150 $t_item->template = $newtempl->id;
1151 $t_item->id = $DB->insert_record('feedback_item', $t_item);
1152 //copy all included files to the feedback_template filearea
1153 $itemfiles = $fs->get_area_files($f_context->id,
1154 'mod_feedback',
1155 'item',
1156 $item->id,
1157 "id",
1158 false);
1159 if ($itemfiles) {
1160 foreach ($itemfiles as $ifile) {
1161 $file_record = new stdClass();
1162 $file_record->contextid = $s_context->id;
1163 $file_record->component = 'mod_feedback';
1164 $file_record->filearea = 'template';
1165 $file_record->itemid = $t_item->id;
1166 $fs->create_file_from_storedfile($file_record, $ifile);
1170 $itembackup[$item->id] = $t_item->id;
1171 if ($t_item->dependitem) {
1172 $dependitemsmap[$t_item->id] = $t_item->dependitem;
1177 //remapping the dependency
1178 foreach ($dependitemsmap as $key => $dependitem) {
1179 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
1180 $newitem->dependitem = $itembackup[$newitem->dependitem];
1181 $DB->update_record('feedback_item', $newitem);
1184 return true;
1188 * deletes all feedback_items related to the given template id
1190 * @global object
1191 * @uses CONTEXT_COURSE
1192 * @param object $template the template
1193 * @return void
1195 function feedback_delete_template($template) {
1196 global $DB;
1198 //deleting the files from the item is done by feedback_delete_item
1199 if ($t_items = $DB->get_records("feedback_item", array("template"=>$template->id))) {
1200 foreach ($t_items as $t_item) {
1201 feedback_delete_item($t_item->id, false, $template);
1204 $DB->delete_records("feedback_template", array("id"=>$template->id));
1208 * creates new feedback_item-records from template.
1209 * if $deleteold is set true so the existing items of the given feedback will be deleted
1210 * if $deleteold is set false so the new items will be appanded to the old items
1212 * @global object
1213 * @uses CONTEXT_COURSE
1214 * @uses CONTEXT_MODULE
1215 * @param object $feedback
1216 * @param int $templateid
1217 * @param boolean $deleteold
1219 function feedback_items_from_template($feedback, $templateid, $deleteold = false) {
1220 global $DB, $CFG;
1222 require_once($CFG->libdir.'/completionlib.php');
1224 $fs = get_file_storage();
1226 if (!$template = $DB->get_record('feedback_template', array('id'=>$templateid))) {
1227 return false;
1229 //get all templateitems
1230 if (!$templitems = $DB->get_records('feedback_item', array('template'=>$templateid))) {
1231 return false;
1234 //files in the template_item are in the context of the current course
1235 //files in the feedback_item are in the feedback_context of the feedback
1236 if ($template->ispublic) {
1237 $s_context = context_system::instance();
1238 } else {
1239 $s_context = context_course::instance($feedback->course);
1241 $course = $DB->get_record('course', array('id'=>$feedback->course));
1242 $cm = get_coursemodule_from_instance('feedback', $feedback->id);
1243 $f_context = context_module::instance($cm->id);
1245 //if deleteold then delete all old items before
1246 //get all items
1247 if ($deleteold) {
1248 if ($feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
1249 //delete all items of this feedback
1250 foreach ($feedbackitems as $item) {
1251 feedback_delete_item($item->id, false);
1253 //delete tracking-data
1254 $DB->delete_records('feedback_tracking', array('feedback'=>$feedback->id));
1256 $params = array('feedback'=>$feedback->id);
1257 if ($completeds = $DB->get_records('feedback_completed', $params)) {
1258 $completion = new completion_info($course);
1259 foreach ($completeds as $completed) {
1260 // Update completion state
1261 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
1262 $completion->update_state($cm, COMPLETION_INCOMPLETE, $completed->userid);
1264 $DB->delete_records('feedback_completed', array('id'=>$completed->id));
1267 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedback->id));
1269 $positionoffset = 0;
1270 } else {
1271 //if the old items are kept the new items will be appended
1272 //therefor the new position has an offset
1273 $positionoffset = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
1276 //create items of this new template
1277 //depend items we are storing temporary in an mapping list array(new id => dependitem)
1278 //we also store a mapping of all items array(oldid => newid)
1279 $dependitemsmap = array();
1280 $itembackup = array();
1281 foreach ($templitems as $t_item) {
1282 $item = clone($t_item);
1283 unset($item->id);
1284 $item->feedback = $feedback->id;
1285 $item->template = 0;
1286 $item->position = $item->position + $positionoffset;
1288 $item->id = $DB->insert_record('feedback_item', $item);
1290 //moving the files to the new item
1291 $templatefiles = $fs->get_area_files($s_context->id,
1292 'mod_feedback',
1293 'template',
1294 $t_item->id,
1295 "id",
1296 false);
1297 if ($templatefiles) {
1298 foreach ($templatefiles as $tfile) {
1299 $file_record = new stdClass();
1300 $file_record->contextid = $f_context->id;
1301 $file_record->component = 'mod_feedback';
1302 $file_record->filearea = 'item';
1303 $file_record->itemid = $item->id;
1304 $fs->create_file_from_storedfile($file_record, $tfile);
1308 $itembackup[$t_item->id] = $item->id;
1309 if ($item->dependitem) {
1310 $dependitemsmap[$item->id] = $item->dependitem;
1314 //remapping the dependency
1315 foreach ($dependitemsmap as $key => $dependitem) {
1316 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
1317 $newitem->dependitem = $itembackup[$newitem->dependitem];
1318 $DB->update_record('feedback_item', $newitem);
1323 * get the list of available templates.
1324 * if the $onlyown param is set true so only templates from own course will be served
1325 * this is important for droping templates
1327 * @global object
1328 * @param object $course
1329 * @param string $onlyownorpublic
1330 * @return array the template recordsets
1332 function feedback_get_template_list($course, $onlyownorpublic = '') {
1333 global $DB, $CFG;
1335 switch($onlyownorpublic) {
1336 case '':
1337 $templates = $DB->get_records_select('feedback_template',
1338 'course = ? OR ispublic = 1',
1339 array($course->id),
1340 'name');
1341 break;
1342 case 'own':
1343 $templates = $DB->get_records('feedback_template',
1344 array('course'=>$course->id),
1345 'name');
1346 break;
1347 case 'public':
1348 $templates = $DB->get_records('feedback_template', array('ispublic'=>1), 'name');
1349 break;
1351 return $templates;
1354 ////////////////////////////////////////////////
1355 //Handling der Items
1356 ////////////////////////////////////////////////
1357 ////////////////////////////////////////////////
1360 * load the lib.php from item-plugin-dir and returns the instance of the itemclass
1362 * @global object
1363 * @param object $item
1364 * @return object the instanz of itemclass
1366 function feedback_get_item_class($typ) {
1367 global $CFG;
1369 //get the class of item-typ
1370 $itemclass = 'feedback_item_'.$typ;
1371 //get the instance of item-class
1372 if (!class_exists($itemclass)) {
1373 require_once($CFG->dirroot.'/mod/feedback/item/'.$typ.'/lib.php');
1375 return new $itemclass();
1379 * load the available item plugins from given subdirectory of $CFG->dirroot
1380 * the default is "mod/feedback/item"
1382 * @global object
1383 * @param string $dir the subdir
1384 * @return array pluginnames as string
1386 function feedback_load_feedback_items($dir = 'mod/feedback/item') {
1387 global $CFG;
1388 $names = get_list_of_plugins($dir);
1389 $ret_names = array();
1391 foreach ($names as $name) {
1392 require_once($CFG->dirroot.'/'.$dir.'/'.$name.'/lib.php');
1393 if (class_exists('feedback_item_'.$name)) {
1394 $ret_names[] = $name;
1397 return $ret_names;
1401 * load the available item plugins to use as dropdown-options
1403 * @global object
1404 * @return array pluginnames as string
1406 function feedback_load_feedback_items_options() {
1407 global $CFG;
1409 $feedback_options = array("pagebreak" => get_string('add_pagebreak', 'feedback'));
1411 if (!$feedback_names = feedback_load_feedback_items('mod/feedback/item')) {
1412 return array();
1415 foreach ($feedback_names as $fn) {
1416 $feedback_options[$fn] = get_string($fn, 'feedback');
1418 asort($feedback_options);
1419 $feedback_options = array_merge( array(' ' => get_string('select')), $feedback_options );
1420 return $feedback_options;
1424 * load the available items for the depend item dropdown list shown in the edit_item form
1426 * @global object
1427 * @param object $feedback
1428 * @param object $item the item of the edit_item form
1429 * @return array all items except the item $item, labels and pagebreaks
1431 function feedback_get_depend_candidates_for_item($feedback, $item) {
1432 global $DB;
1433 //all items for dependitem
1434 $where = "feedback = ? AND typ != 'pagebreak' AND hasvalue = 1";
1435 $params = array($feedback->id);
1436 if (isset($item->id) AND $item->id) {
1437 $where .= ' AND id != ?';
1438 $params[] = $item->id;
1440 $dependitems = array(0 => get_string('choose'));
1441 $feedbackitems = $DB->get_records_select_menu('feedback_item',
1442 $where,
1443 $params,
1444 'position',
1445 'id, label');
1447 if (!$feedbackitems) {
1448 return $dependitems;
1450 //adding the choose-option
1451 foreach ($feedbackitems as $key => $val) {
1452 $dependitems[$key] = $val;
1454 return $dependitems;
1458 * creates a new item-record
1460 * @global object
1461 * @param object $data the data from edit_item_form
1462 * @return int the new itemid
1464 function feedback_create_item($data) {
1465 global $DB;
1467 $item = new stdClass();
1468 $item->feedback = $data->feedbackid;
1470 $item->template=0;
1471 if (isset($data->templateid)) {
1472 $item->template = intval($data->templateid);
1475 $itemname = trim($data->itemname);
1476 $item->name = ($itemname ? $data->itemname : get_string('no_itemname', 'feedback'));
1478 if (!empty($data->itemlabel)) {
1479 $item->label = trim($data->itemlabel);
1480 } else {
1481 $item->label = get_string('no_itemlabel', 'feedback');
1484 $itemobj = feedback_get_item_class($data->typ);
1485 $item->presentation = ''; //the date comes from postupdate() of the itemobj
1487 $item->hasvalue = $itemobj->get_hasvalue();
1489 $item->typ = $data->typ;
1490 $item->position = $data->position;
1492 $item->required=0;
1493 if (!empty($data->required)) {
1494 $item->required = $data->required;
1497 $item->id = $DB->insert_record('feedback_item', $item);
1499 //move all itemdata to the data
1500 $data->id = $item->id;
1501 $data->feedback = $item->feedback;
1502 $data->name = $item->name;
1503 $data->label = $item->label;
1504 $data->required = $item->required;
1505 return $itemobj->postupdate($data);
1509 * save the changes of a given item.
1511 * @global object
1512 * @param object $item
1513 * @return boolean
1515 function feedback_update_item($item) {
1516 global $DB;
1517 return $DB->update_record("feedback_item", $item);
1521 * deletes an item and also deletes all related values
1523 * @global object
1524 * @uses CONTEXT_MODULE
1525 * @param int $itemid
1526 * @param boolean $renumber should the kept items renumbered Yes/No
1527 * @param object $template if the template is given so the items are bound to it
1528 * @return void
1530 function feedback_delete_item($itemid, $renumber = true, $template = false) {
1531 global $DB;
1533 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
1535 //deleting the files from the item
1536 $fs = get_file_storage();
1538 if ($template) {
1539 if ($template->ispublic) {
1540 $context = context_system::instance();
1541 } else {
1542 $context = context_course::instance($template->course);
1544 $templatefiles = $fs->get_area_files($context->id,
1545 'mod_feedback',
1546 'template',
1547 $item->id,
1548 "id",
1549 false);
1551 if ($templatefiles) {
1552 $fs->delete_area_files($context->id, 'mod_feedback', 'template', $item->id);
1554 } else {
1555 if (!$cm = get_coursemodule_from_instance('feedback', $item->feedback)) {
1556 return false;
1558 $context = context_module::instance($cm->id);
1560 $itemfiles = $fs->get_area_files($context->id,
1561 'mod_feedback',
1562 'item',
1563 $item->id,
1564 "id", false);
1566 if ($itemfiles) {
1567 $fs->delete_area_files($context->id, 'mod_feedback', 'item', $item->id);
1571 $DB->delete_records("feedback_value", array("item"=>$itemid));
1572 $DB->delete_records("feedback_valuetmp", array("item"=>$itemid));
1574 //remove all depends
1575 $DB->set_field('feedback_item', 'dependvalue', '', array('dependitem'=>$itemid));
1576 $DB->set_field('feedback_item', 'dependitem', 0, array('dependitem'=>$itemid));
1578 $DB->delete_records("feedback_item", array("id"=>$itemid));
1579 if ($renumber) {
1580 feedback_renumber_items($item->feedback);
1585 * deletes all items of the given feedbackid
1587 * @global object
1588 * @param int $feedbackid
1589 * @return void
1591 function feedback_delete_all_items($feedbackid) {
1592 global $DB, $CFG;
1593 require_once($CFG->libdir.'/completionlib.php');
1595 if (!$feedback = $DB->get_record('feedback', array('id'=>$feedbackid))) {
1596 return false;
1599 if (!$cm = get_coursemodule_from_instance('feedback', $feedback->id)) {
1600 return false;
1603 if (!$course = $DB->get_record('course', array('id'=>$feedback->course))) {
1604 return false;
1607 if (!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid))) {
1608 return;
1610 foreach ($items as $item) {
1611 feedback_delete_item($item->id, false);
1613 if ($completeds = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id))) {
1614 $completion = new completion_info($course);
1615 foreach ($completeds as $completed) {
1616 // Update completion state
1617 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
1618 $completion->update_state($cm, COMPLETION_INCOMPLETE, $completed->userid);
1620 $DB->delete_records('feedback_completed', array('id'=>$completed->id));
1624 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedbackid));
1629 * this function toggled the item-attribute required (yes/no)
1631 * @global object
1632 * @param object $item
1633 * @return boolean
1635 function feedback_switch_item_required($item) {
1636 global $DB, $CFG;
1638 $itemobj = feedback_get_item_class($item->typ);
1640 if ($itemobj->can_switch_require()) {
1641 $new_require_val = (int)!(bool)$item->required;
1642 $params = array('id'=>$item->id);
1643 $DB->set_field('feedback_item', 'required', $new_require_val, $params);
1645 return true;
1649 * renumbers all items of the given feedbackid
1651 * @global object
1652 * @param int $feedbackid
1653 * @return void
1655 function feedback_renumber_items($feedbackid) {
1656 global $DB;
1658 $items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position');
1659 $pos = 1;
1660 if ($items) {
1661 foreach ($items as $item) {
1662 $DB->set_field('feedback_item', 'position', $pos, array('id'=>$item->id));
1663 $pos++;
1669 * this decreases the position of the given item
1671 * @global object
1672 * @param object $item
1673 * @return bool
1675 function feedback_moveup_item($item) {
1676 global $DB;
1678 if ($item->position == 1) {
1679 return true;
1682 $params = array('feedback'=>$item->feedback);
1683 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
1684 return false;
1687 $itembefore = null;
1688 foreach ($items as $i) {
1689 if ($i->id == $item->id) {
1690 if (is_null($itembefore)) {
1691 return true;
1693 $itembefore->position = $item->position;
1694 $item->position--;
1695 feedback_update_item($itembefore);
1696 feedback_update_item($item);
1697 feedback_renumber_items($item->feedback);
1698 return true;
1700 $itembefore = $i;
1702 return false;
1706 * this increased the position of the given item
1708 * @global object
1709 * @param object $item
1710 * @return bool
1712 function feedback_movedown_item($item) {
1713 global $DB;
1715 $params = array('feedback'=>$item->feedback);
1716 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
1717 return false;
1720 $movedownitem = null;
1721 foreach ($items as $i) {
1722 if (!is_null($movedownitem) AND $movedownitem->id == $item->id) {
1723 $movedownitem->position = $i->position;
1724 $i->position--;
1725 feedback_update_item($movedownitem);
1726 feedback_update_item($i);
1727 feedback_renumber_items($item->feedback);
1728 return true;
1730 $movedownitem = $i;
1732 return false;
1736 * here the position of the given item will be set to the value in $pos
1738 * @global object
1739 * @param object $moveitem
1740 * @param int $pos
1741 * @return boolean
1743 function feedback_move_item($moveitem, $pos) {
1744 global $DB;
1746 $params = array('feedback'=>$moveitem->feedback);
1747 if (!$allitems = $DB->get_records('feedback_item', $params, 'position')) {
1748 return false;
1750 if (is_array($allitems)) {
1751 $index = 1;
1752 foreach ($allitems as $item) {
1753 if ($index == $pos) {
1754 $index++;
1756 if ($item->id == $moveitem->id) {
1757 $moveitem->position = $pos;
1758 feedback_update_item($moveitem);
1759 continue;
1761 $item->position = $index;
1762 feedback_update_item($item);
1763 $index++;
1765 return true;
1767 return false;
1771 * prints the given item as a preview.
1772 * each item-class has an own print_item_preview function implemented.
1774 * @global object
1775 * @param object $item the item what we want to print out
1776 * @return void
1778 function feedback_print_item_preview($item) {
1779 global $CFG;
1780 if ($item->typ == 'pagebreak') {
1781 return;
1783 //get the instance of the item-class
1784 $itemobj = feedback_get_item_class($item->typ);
1785 $itemobj->print_item_preview($item);
1789 * prints the given item in the completion form.
1790 * each item-class has an own print_item_complete function implemented.
1792 * @param object $item the item what we want to print out
1793 * @param mixed $value the value
1794 * @param boolean $highlightrequire if this set true and the value are false on completing so the item will be highlighted
1795 * @return void
1797 function feedback_print_item_complete($item, $value = false, $highlightrequire = false) {
1798 global $CFG;
1799 if ($item->typ == 'pagebreak') {
1800 return;
1803 //get the instance of the item-class
1804 $itemobj = feedback_get_item_class($item->typ);
1805 $itemobj->print_item_complete($item, $value, $highlightrequire);
1809 * prints the given item in the show entries page.
1810 * each item-class has an own print_item_show_value function implemented.
1812 * @param object $item the item what we want to print out
1813 * @param mixed $value
1814 * @return void
1816 function feedback_print_item_show_value($item, $value = false) {
1817 global $CFG;
1818 if ($item->typ == 'pagebreak') {
1819 return;
1822 //get the instance of the item-class
1823 $itemobj = feedback_get_item_class($item->typ);
1824 $itemobj->print_item_show_value($item, $value);
1828 * if the user completes a feedback and there is a pagebreak so the values are saved temporary.
1829 * the values are not saved permanently until the user click on save button
1831 * @global object
1832 * @param object $feedbackcompleted
1833 * @return object temporary saved completed-record
1835 function feedback_set_tmp_values($feedbackcompleted) {
1836 global $DB;
1838 //first we create a completedtmp
1839 $tmpcpl = new stdClass();
1840 foreach ($feedbackcompleted as $key => $value) {
1841 $tmpcpl->{$key} = $value;
1843 unset($tmpcpl->id);
1844 $tmpcpl->timemodified = time();
1845 $tmpcpl->id = $DB->insert_record('feedback_completedtmp', $tmpcpl);
1846 //get all values of original-completed
1847 if (!$values = $DB->get_records('feedback_value', array('completed'=>$feedbackcompleted->id))) {
1848 return;
1850 foreach ($values as $value) {
1851 unset($value->id);
1852 $value->completed = $tmpcpl->id;
1853 $DB->insert_record('feedback_valuetmp', $value);
1855 return $tmpcpl;
1859 * this saves the temporary saved values permanently
1861 * @global object
1862 * @param object $feedbackcompletedtmp the temporary completed
1863 * @param object $feedbackcompleted the target completed
1864 * @param int $userid
1865 * @return int the id of the completed
1867 function feedback_save_tmp_values($feedbackcompletedtmp, $feedbackcompleted, $userid) {
1868 global $DB;
1870 $tmpcplid = $feedbackcompletedtmp->id;
1871 if ($feedbackcompleted) {
1872 //first drop all existing values
1873 $DB->delete_records('feedback_value', array('completed'=>$feedbackcompleted->id));
1874 //update the current completed
1875 $feedbackcompleted->timemodified = time();
1876 $DB->update_record('feedback_completed', $feedbackcompleted);
1877 } else {
1878 $feedbackcompleted = clone($feedbackcompletedtmp);
1879 $feedbackcompleted->id = '';
1880 $feedbackcompleted->userid = $userid;
1881 $feedbackcompleted->timemodified = time();
1882 $feedbackcompleted->id = $DB->insert_record('feedback_completed', $feedbackcompleted);
1885 //save all the new values from feedback_valuetmp
1886 //get all values of tmp-completed
1887 $params = array('completed'=>$feedbackcompletedtmp->id);
1888 if (!$values = $DB->get_records('feedback_valuetmp', $params)) {
1889 return false;
1891 foreach ($values as $value) {
1892 //check if there are depend items
1893 $item = $DB->get_record('feedback_item', array('id'=>$value->item));
1894 if ($item->dependitem > 0) {
1895 $check = feedback_compare_item_value($tmpcplid,
1896 $item->dependitem,
1897 $item->dependvalue,
1898 true);
1899 } else {
1900 $check = true;
1902 if ($check) {
1903 unset($value->id);
1904 $value->completed = $feedbackcompleted->id;
1905 $DB->insert_record('feedback_value', $value);
1908 //drop all the tmpvalues
1909 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1910 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
1912 // Trigger event for the delete action we performed.
1913 $cm = get_coursemodule_from_instance('feedback', $feedbackcompleted->feedback);
1914 $event = \mod_feedback\event\response_submitted::create(array(
1915 'relateduserid' => $userid,
1916 'objectid' => $feedbackcompleted->id,
1917 'context' => context_module::instance($cm->id),
1918 'anonymous' => ($feedbackcompleted->anonymous_response == FEEDBACK_ANONYMOUS_YES),
1919 'other' => array(
1920 'cmid' => $cm->id,
1921 'instanceid' => $feedbackcompleted->feedback,
1922 'anonymous' => $feedbackcompleted->anonymous_response // Deprecated.
1926 $event->add_record_snapshot('feedback_completed', $feedbackcompleted);
1928 $event->trigger();
1929 return $feedbackcompleted->id;
1934 * deletes the given temporary completed and all related temporary values
1936 * @global object
1937 * @param int $tmpcplid
1938 * @return void
1940 function feedback_delete_completedtmp($tmpcplid) {
1941 global $DB;
1943 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1944 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
1947 ////////////////////////////////////////////////
1948 ////////////////////////////////////////////////
1949 ////////////////////////////////////////////////
1950 //functions to handle the pagebreaks
1951 ////////////////////////////////////////////////
1954 * this creates a pagebreak.
1955 * a pagebreak is a special kind of item
1957 * @global object
1958 * @param int $feedbackid
1959 * @return mixed false if there already is a pagebreak on last position or the id of the pagebreak-item
1961 function feedback_create_pagebreak($feedbackid) {
1962 global $DB;
1964 //check if there already is a pagebreak on the last position
1965 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
1966 if ($lastposition == feedback_get_last_break_position($feedbackid)) {
1967 return false;
1970 $item = new stdClass();
1971 $item->feedback = $feedbackid;
1973 $item->template=0;
1975 $item->name = '';
1977 $item->presentation = '';
1978 $item->hasvalue = 0;
1980 $item->typ = 'pagebreak';
1981 $item->position = $lastposition + 1;
1983 $item->required=0;
1985 return $DB->insert_record('feedback_item', $item);
1989 * get all positions of pagebreaks in the given feedback
1991 * @global object
1992 * @param int $feedbackid
1993 * @return array all ordered pagebreak positions
1995 function feedback_get_all_break_positions($feedbackid) {
1996 global $DB;
1998 $params = array('typ'=>'pagebreak', 'feedback'=>$feedbackid);
1999 $allbreaks = $DB->get_records_menu('feedback_item', $params, 'position', 'id, position');
2000 if (!$allbreaks) {
2001 return false;
2003 return array_values($allbreaks);
2007 * get the position of the last pagebreak
2009 * @param int $feedbackid
2010 * @return int the position of the last pagebreak
2012 function feedback_get_last_break_position($feedbackid) {
2013 if (!$allbreaks = feedback_get_all_break_positions($feedbackid)) {
2014 return false;
2016 return $allbreaks[count($allbreaks) - 1];
2020 * this returns the position where the user can continue the completing.
2022 * @global object
2023 * @global object
2024 * @global object
2025 * @param int $feedbackid
2026 * @param int $courseid
2027 * @param string $guestid this id will be saved temporary and is unique
2028 * @return int the position to continue
2030 function feedback_get_page_to_continue($feedbackid, $courseid = false, $guestid = false) {
2031 global $CFG, $USER, $DB;
2033 //is there any break?
2035 if (!$allbreaks = feedback_get_all_break_positions($feedbackid)) {
2036 return false;
2039 $params = array();
2040 if ($courseid) {
2041 $courseselect = "AND fv.course_id = :courseid";
2042 $params['courseid'] = $courseid;
2043 } else {
2044 $courseselect = '';
2047 if ($guestid) {
2048 $userselect = "AND fc.guestid = :guestid";
2049 $usergroup = "GROUP BY fc.guestid";
2050 $params['guestid'] = $guestid;
2051 } else {
2052 $userselect = "AND fc.userid = :userid";
2053 $usergroup = "GROUP BY fc.userid";
2054 $params['userid'] = $USER->id;
2057 $sql = "SELECT MAX(fi.position)
2058 FROM {feedback_completedtmp} fc, {feedback_valuetmp} fv, {feedback_item} fi
2059 WHERE fc.id = fv.completed
2060 $userselect
2061 AND fc.feedback = :feedbackid
2062 $courseselect
2063 AND fi.id = fv.item
2064 $usergroup";
2065 $params['feedbackid'] = $feedbackid;
2067 $lastpos = $DB->get_field_sql($sql, $params);
2069 //the index of found pagebreak is the searched pagenumber
2070 foreach ($allbreaks as $pagenr => $br) {
2071 if ($lastpos < $br) {
2072 return $pagenr;
2075 return count($allbreaks);
2078 ////////////////////////////////////////////////
2079 ////////////////////////////////////////////////
2080 ////////////////////////////////////////////////
2081 //functions to handle the values
2082 ////////////////////////////////////////////////
2085 * cleans the userinput while submitting the form.
2087 * @param mixed $value
2088 * @return mixed
2090 function feedback_clean_input_value($item, $value) {
2091 $itemobj = feedback_get_item_class($item->typ);
2092 return $itemobj->clean_input_value($value);
2096 * this saves the values of an completed.
2097 * if the param $tmp is set true so the values are saved temporary in table feedback_valuetmp.
2098 * if there is already a completed and the userid is set so the values are updated.
2099 * on all other things new value records will be created.
2101 * @global object
2102 * @param int $userid
2103 * @param boolean $tmp
2104 * @return mixed false on error or the completeid
2106 function feedback_save_values($usrid, $tmp = false) {
2107 global $DB;
2109 $completedid = optional_param('completedid', 0, PARAM_INT);
2111 $tmpstr = $tmp ? 'tmp' : '';
2112 $time = time();
2113 $timemodified = mktime(0, 0, 0, date('m', $time), date('d', $time), date('Y', $time));
2115 if ($usrid == 0) {
2116 return feedback_create_values($usrid, $timemodified, $tmp);
2118 $completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid));
2119 if (!$completed) {
2120 return feedback_create_values($usrid, $timemodified, $tmp);
2121 } else {
2122 $completed->timemodified = $timemodified;
2123 return feedback_update_values($completed, $tmp);
2128 * this saves the values from anonymous user such as guest on the main-site
2130 * @global object
2131 * @param string $guestid the unique guestidentifier
2132 * @return mixed false on error or the completeid
2134 function feedback_save_guest_values($guestid) {
2135 global $DB;
2137 $completedid = optional_param('completedid', false, PARAM_INT);
2139 $timemodified = time();
2140 if (!$completed = $DB->get_record('feedback_completedtmp', array('id'=>$completedid))) {
2141 return feedback_create_values(0, $timemodified, true, $guestid);
2142 } else {
2143 $completed->timemodified = $timemodified;
2144 return feedback_update_values($completed, true);
2149 * get the value from the given item related to the given completed.
2150 * the value can come as temporary or as permanently value. the deciding is done by $tmp
2152 * @global object
2153 * @param int $completeid
2154 * @param int $itemid
2155 * @param boolean $tmp
2156 * @return mixed the value, the type depends on plugin-definition
2158 function feedback_get_item_value($completedid, $itemid, $tmp = false) {
2159 global $DB;
2161 $tmpstr = $tmp ? 'tmp' : '';
2162 $params = array('completed'=>$completedid, 'item'=>$itemid);
2163 return $DB->get_field('feedback_value'.$tmpstr, 'value', $params);
2167 * compares the value of the itemid related to the completedid with the dependvalue.
2168 * this is used if a depend item is set.
2169 * the value can come as temporary or as permanently value. the deciding is done by $tmp.
2171 * @global object
2172 * @global object
2173 * @param int $completeid
2174 * @param int $itemid
2175 * @param mixed $dependvalue
2176 * @param boolean $tmp
2177 * @return bool
2179 function feedback_compare_item_value($completedid, $itemid, $dependvalue, $tmp = false) {
2180 global $DB, $CFG;
2182 $dbvalue = feedback_get_item_value($completedid, $itemid, $tmp);
2184 //get the class of the given item-typ
2185 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
2187 //get the instance of the item-class
2188 $itemobj = feedback_get_item_class($item->typ);
2189 return $itemobj->compare_value($item, $dbvalue, $dependvalue); //true or false
2193 * this function checks the correctness of values.
2194 * the rules for this are implemented in the class of each item.
2195 * it can be the required attribute or the value self e.g. numeric.
2196 * the params first/lastitem are given to determine the visible range between pagebreaks.
2198 * @global object
2199 * @param int $firstitem the position of firstitem for checking
2200 * @param int $lastitem the position of lastitem for checking
2201 * @return boolean
2203 function feedback_check_values($firstitem, $lastitem) {
2204 global $DB, $CFG;
2206 $feedbackid = optional_param('feedbackid', 0, PARAM_INT);
2208 //get all items between the first- and lastitem
2209 $select = "feedback = ?
2210 AND position >= ?
2211 AND position <= ?
2212 AND hasvalue = 1";
2213 $params = array($feedbackid, $firstitem, $lastitem);
2214 if (!$feedbackitems = $DB->get_records_select('feedback_item', $select, $params)) {
2215 //if no values are given so no values can be wrong ;-)
2216 return true;
2219 foreach ($feedbackitems as $item) {
2220 //get the instance of the item-class
2221 $itemobj = feedback_get_item_class($item->typ);
2223 //the name of the input field of the completeform is given in a special form:
2224 //<item-typ>_<item-id> eg. numeric_234
2225 //this is the key to get the value for the correct item
2226 $formvalname = $item->typ . '_' . $item->id;
2228 if ($itemobj->value_is_array()) {
2229 //get the raw value here. It is cleaned after that by the object itself
2230 $value = optional_param_array($formvalname, null, PARAM_RAW);
2231 } else {
2232 //get the raw value here. It is cleaned after that by the object itself
2233 $value = optional_param($formvalname, null, PARAM_RAW);
2235 $value = $itemobj->clean_input_value($value);
2237 // If the item is not visible due to its dependency so it shouldn't be required.
2238 // Many thanks to Pau Ferrer Ocaña.
2239 if ($item->dependitem > 0 AND $item->required == 1) {
2240 $comparevalue = false;
2241 if ($feedbackcompletedtmp = feedback_get_current_completed($item->feedback, true)) {
2242 $comparevalue = feedback_compare_item_value($feedbackcompletedtmp->id,
2243 $item->dependitem,
2244 $item->dependvalue,
2245 true);
2248 if (!$comparevalue) {
2249 $item->required = 0; // Override the required property.
2253 //check if the value is set
2254 if (is_null($value) AND $item->required == 1) {
2255 return false;
2258 //now we let check the value by the item-class
2259 if (!$itemobj->check_value($value, $item)) {
2260 return false;
2263 //if no wrong values so we can return true
2264 return true;
2268 * this function create a complete-record and the related value-records.
2269 * depending on the $tmp (true/false) the values are saved temporary or permanently
2271 * @global object
2272 * @param int $userid
2273 * @param int $timemodified
2274 * @param boolean $tmp
2275 * @param string $guestid a unique identifier to save temporary data
2276 * @return mixed false on error or the completedid
2278 function feedback_create_values($usrid, $timemodified, $tmp = false, $guestid = false) {
2279 global $DB;
2281 $feedbackid = optional_param('feedbackid', false, PARAM_INT);
2282 $anonymous_response = optional_param('anonymous_response', false, PARAM_INT);
2283 $courseid = optional_param('courseid', false, PARAM_INT);
2285 $tmpstr = $tmp ? 'tmp' : '';
2286 //first we create a new completed record
2287 $completed = new stdClass();
2288 $completed->feedback = $feedbackid;
2289 $completed->userid = $usrid;
2290 $completed->guestid = $guestid;
2291 $completed->timemodified = $timemodified;
2292 $completed->anonymous_response = $anonymous_response;
2294 $completedid = $DB->insert_record('feedback_completed'.$tmpstr, $completed);
2296 $completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid));
2298 //the keys are in the form like abc_xxx
2299 //with explode we make an array with(abc, xxx) and (abc=typ und xxx=itemnr)
2301 //get the items of the feedback
2302 if (!$allitems = $DB->get_records('feedback_item', array('feedback'=>$completed->feedback))) {
2303 return false;
2305 foreach ($allitems as $item) {
2306 if (!$item->hasvalue) {
2307 continue;
2309 //get the class of item-typ
2310 $itemobj = feedback_get_item_class($item->typ);
2312 $keyname = $item->typ.'_'.$item->id;
2314 if ($itemobj->value_is_array()) {
2315 $itemvalue = optional_param_array($keyname, null, $itemobj->value_type());
2316 } else {
2317 $itemvalue = optional_param($keyname, null, $itemobj->value_type());
2320 if (is_null($itemvalue)) {
2321 continue;
2324 $value = new stdClass();
2325 $value->item = $item->id;
2326 $value->completed = $completed->id;
2327 $value->course_id = $courseid;
2329 //the kind of values can be absolutely different
2330 //so we run create_value directly by the item-class
2331 $value->value = $itemobj->create_value($itemvalue);
2332 $DB->insert_record('feedback_value'.$tmpstr, $value);
2334 return $completed->id;
2338 * this function updates a complete-record and the related value-records.
2339 * depending on the $tmp (true/false) the values are saved temporary or permanently
2341 * @global object
2342 * @param object $completed
2343 * @param boolean $tmp
2344 * @return int the completedid
2346 function feedback_update_values($completed, $tmp = false) {
2347 global $DB;
2349 $courseid = optional_param('courseid', false, PARAM_INT);
2350 $tmpstr = $tmp ? 'tmp' : '';
2352 $DB->update_record('feedback_completed'.$tmpstr, $completed);
2353 //get the values of this completed
2354 $values = $DB->get_records('feedback_value'.$tmpstr, array('completed'=>$completed->id));
2356 //get the items of the feedback
2357 if (!$allitems = $DB->get_records('feedback_item', array('feedback'=>$completed->feedback))) {
2358 return false;
2360 foreach ($allitems as $item) {
2361 if (!$item->hasvalue) {
2362 continue;
2364 //get the class of item-typ
2365 $itemobj = feedback_get_item_class($item->typ);
2367 $keyname = $item->typ.'_'.$item->id;
2369 if ($itemobj->value_is_array()) {
2370 $itemvalue = optional_param_array($keyname, null, $itemobj->value_type());
2371 } else {
2372 $itemvalue = optional_param($keyname, null, $itemobj->value_type());
2375 //is the itemvalue set (could be a subset of items because pagebreak)?
2376 if (is_null($itemvalue)) {
2377 continue;
2380 $newvalue = new stdClass();
2381 $newvalue->item = $item->id;
2382 $newvalue->completed = $completed->id;
2383 $newvalue->course_id = $courseid;
2385 //the kind of values can be absolutely different
2386 //so we run create_value directly by the item-class
2387 $newvalue->value = $itemobj->create_value($itemvalue);
2389 //check, if we have to create or update the value
2390 $exist = false;
2391 foreach ($values as $value) {
2392 if ($value->item == $newvalue->item) {
2393 $newvalue->id = $value->id;
2394 $exist = true;
2395 break;
2398 if ($exist) {
2399 $DB->update_record('feedback_value'.$tmpstr, $newvalue);
2400 } else {
2401 $DB->insert_record('feedback_value'.$tmpstr, $newvalue);
2405 return $completed->id;
2409 * get the values of an item depending on the given groupid.
2410 * if the feedback is anonymous so the values are shuffled
2412 * @global object
2413 * @global object
2414 * @param object $item
2415 * @param int $groupid
2416 * @param int $courseid
2417 * @param bool $ignore_empty if this is set true so empty values are not delivered
2418 * @return array the value-records
2420 function feedback_get_group_values($item,
2421 $groupid = false,
2422 $courseid = false,
2423 $ignore_empty = false) {
2425 global $CFG, $DB;
2427 //if the groupid is given?
2428 if (intval($groupid) > 0) {
2429 $params = array();
2430 if ($ignore_empty) {
2431 $value = $DB->sql_compare_text('fbv.value');
2432 $ignore_empty_select = "AND $value != :emptyvalue AND $value != :zerovalue";
2433 $params += array('emptyvalue' => '', 'zerovalue' => '0');
2434 } else {
2435 $ignore_empty_select = "";
2438 $query = 'SELECT fbv . *
2439 FROM {feedback_value} fbv, {feedback_completed} fbc, {groups_members} gm
2440 WHERE fbv.item = :itemid
2441 AND fbv.completed = fbc.id
2442 AND fbc.userid = gm.userid
2443 '.$ignore_empty_select.'
2444 AND gm.groupid = :groupid
2445 ORDER BY fbc.timemodified';
2446 $params += array('itemid' => $item->id, 'groupid' => $groupid);
2447 $values = $DB->get_records_sql($query, $params);
2449 } else {
2450 $params = array();
2451 if ($ignore_empty) {
2452 $value = $DB->sql_compare_text('value');
2453 $ignore_empty_select = "AND $value != :emptyvalue AND $value != :zerovalue";
2454 $params += array('emptyvalue' => '', 'zerovalue' => '0');
2455 } else {
2456 $ignore_empty_select = "";
2459 if ($courseid) {
2460 $select = "item = :itemid AND course_id = :courseid ".$ignore_empty_select;
2461 $params += array('itemid' => $item->id, 'courseid' => $courseid);
2462 $values = $DB->get_records_select('feedback_value', $select, $params);
2463 } else {
2464 $select = "item = :itemid ".$ignore_empty_select;
2465 $params += array('itemid' => $item->id);
2466 $values = $DB->get_records_select('feedback_value', $select, $params);
2469 $params = array('id'=>$item->feedback);
2470 if ($DB->get_field('feedback', 'anonymous', $params) == FEEDBACK_ANONYMOUS_YES) {
2471 if (is_array($values)) {
2472 shuffle($values);
2475 return $values;
2479 * check for multiple_submit = false.
2480 * if the feedback is global so the courseid must be given
2482 * @global object
2483 * @global object
2484 * @param int $feedbackid
2485 * @param int $courseid
2486 * @return boolean true if the feedback already is submitted otherwise false
2488 function feedback_is_already_submitted($feedbackid, $courseid = false) {
2489 global $USER, $DB;
2491 $params = array('userid'=>$USER->id, 'feedback'=>$feedbackid);
2492 if (!$trackings = $DB->get_records_menu('feedback_tracking', $params, '', 'id, completed')) {
2493 return false;
2496 if ($courseid) {
2497 $select = 'completed IN ('.implode(',', $trackings).') AND course_id = ?';
2498 if (!$values = $DB->get_records_select('feedback_value', $select, array($courseid))) {
2499 return false;
2503 return true;
2507 * if the completion of a feedback will be continued eg.
2508 * by pagebreak or by multiple submit so the complete must be found.
2509 * if the param $tmp is set true so all things are related to temporary completeds
2511 * @global object
2512 * @global object
2513 * @global object
2514 * @param int $feedbackid
2515 * @param boolean $tmp
2516 * @param int $courseid
2517 * @param string $guestid
2518 * @return int the id of the found completed
2520 function feedback_get_current_completed($feedbackid,
2521 $tmp = false,
2522 $courseid = false,
2523 $guestid = false) {
2525 global $USER, $CFG, $DB;
2527 $tmpstr = $tmp ? 'tmp' : '';
2529 if (!$courseid) {
2530 if ($guestid) {
2531 $params = array('feedback'=>$feedbackid, 'guestid'=>$guestid);
2532 return $DB->get_record('feedback_completed'.$tmpstr, $params);
2533 } else {
2534 $params = array('feedback'=>$feedbackid, 'userid'=>$USER->id);
2535 return $DB->get_record('feedback_completed'.$tmpstr, $params);
2539 $params = array();
2541 if ($guestid) {
2542 $userselect = "AND fc.guestid = :guestid";
2543 $params['guestid'] = $guestid;
2544 } else {
2545 $userselect = "AND fc.userid = :userid";
2546 $params['userid'] = $USER->id;
2548 //if courseid is set the feedback is global.
2549 //there can be more than one completed on one feedback
2550 $sql = "SELECT DISTINCT fc.*
2551 FROM {feedback_value{$tmpstr}} fv, {feedback_completed{$tmpstr}} fc
2552 WHERE fv.course_id = :courseid
2553 AND fv.completed = fc.id
2554 $userselect
2555 AND fc.feedback = :feedbackid";
2556 $params['courseid'] = intval($courseid);
2557 $params['feedbackid'] = $feedbackid;
2559 if (!$sqlresult = $DB->get_records_sql($sql, $params)) {
2560 return false;
2562 foreach ($sqlresult as $r) {
2563 return $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$r->id));
2568 * get the completeds depending on the given groupid.
2570 * @global object
2571 * @global object
2572 * @param object $feedback
2573 * @param int $groupid
2574 * @param int $courseid
2575 * @return mixed array of found completeds otherwise false
2577 function feedback_get_completeds_group($feedback, $groupid = false, $courseid = false) {
2578 global $CFG, $DB;
2580 if (intval($groupid) > 0) {
2581 $query = "SELECT fbc.*
2582 FROM {feedback_completed} fbc, {groups_members} gm
2583 WHERE fbc.feedback = ?
2584 AND gm.groupid = ?
2585 AND fbc.userid = gm.userid";
2586 if ($values = $DB->get_records_sql($query, array($feedback->id, $groupid))) {
2587 return $values;
2588 } else {
2589 return false;
2591 } else {
2592 if ($courseid) {
2593 $query = "SELECT DISTINCT fbc.*
2594 FROM {feedback_completed} fbc, {feedback_value} fbv
2595 WHERE fbc.id = fbv.completed
2596 AND fbc.feedback = ?
2597 AND fbv.course_id = ?
2598 ORDER BY random_response";
2599 if ($values = $DB->get_records_sql($query, array($feedback->id, $courseid))) {
2600 return $values;
2601 } else {
2602 return false;
2604 } else {
2605 if ($values = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id))) {
2606 return $values;
2607 } else {
2608 return false;
2615 * get the count of completeds depending on the given groupid.
2617 * @global object
2618 * @global object
2619 * @param object $feedback
2620 * @param int $groupid
2621 * @param int $courseid
2622 * @return mixed count of completeds or false
2624 function feedback_get_completeds_group_count($feedback, $groupid = false, $courseid = false) {
2625 global $CFG, $DB;
2627 if ($courseid > 0 AND !$groupid <= 0) {
2628 $sql = "SELECT id, COUNT(item) AS ci
2629 FROM {feedback_value}
2630 WHERE course_id = ?
2631 GROUP BY item ORDER BY ci DESC";
2632 if ($foundrecs = $DB->get_records_sql($sql, array($courseid))) {
2633 $foundrecs = array_values($foundrecs);
2634 return $foundrecs[0]->ci;
2636 return false;
2638 if ($values = feedback_get_completeds_group($feedback, $groupid)) {
2639 return count($values);
2640 } else {
2641 return false;
2646 * deletes all completed-recordsets from a feedback.
2647 * all related data such as values also will be deleted
2649 * @global object
2650 * @param int $feedbackid
2651 * @return void
2653 function feedback_delete_all_completeds($feedbackid) {
2654 global $DB;
2656 if (!$completeds = $DB->get_records('feedback_completed', array('feedback'=>$feedbackid))) {
2657 return;
2659 foreach ($completeds as $completed) {
2660 feedback_delete_completed($completed->id);
2665 * deletes a completed given by completedid.
2666 * all related data such values or tracking data also will be deleted
2668 * @global object
2669 * @param int $completedid
2670 * @return boolean
2672 function feedback_delete_completed($completedid) {
2673 global $DB, $CFG;
2674 require_once($CFG->libdir.'/completionlib.php');
2676 if (!$completed = $DB->get_record('feedback_completed', array('id'=>$completedid))) {
2677 return false;
2680 if (!$feedback = $DB->get_record('feedback', array('id'=>$completed->feedback))) {
2681 return false;
2684 if (!$course = $DB->get_record('course', array('id'=>$feedback->course))) {
2685 return false;
2688 if (!$cm = get_coursemodule_from_instance('feedback', $feedback->id)) {
2689 return false;
2692 //first we delete all related values
2693 $DB->delete_records('feedback_value', array('completed'=>$completed->id));
2695 //now we delete all tracking data
2696 $params = array('completed'=>$completed->id, 'feedback'=>$completed->feedback);
2697 if ($tracking = $DB->get_record('feedback_tracking', $params)) {
2698 $DB->delete_records('feedback_tracking', array('completed'=>$completed->id));
2701 // Update completion state
2702 $completion = new completion_info($course);
2703 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
2704 $completion->update_state($cm, COMPLETION_INCOMPLETE, $completed->userid);
2706 // Last we delete the completed-record.
2707 $return = $DB->delete_records('feedback_completed', array('id'=>$completed->id));
2709 // Trigger event for the delete action we performed.
2710 $event = \mod_feedback\event\response_deleted::create(array(
2711 'relateduserid' => $completed->userid,
2712 'objectid' => $completedid,
2713 'courseid' => $course->id,
2714 'context' => context_module::instance($cm->id),
2715 'anonymous' => ($completed->anonymous_response == FEEDBACK_ANONYMOUS_YES),
2716 'other' => array(
2717 'cmid' => $cm->id,
2718 'instanceid' => $feedback->id,
2719 'anonymous' => $completed->anonymous_response) // Deprecated.
2722 $event->add_record_snapshot('feedback_completed', $completed);
2723 $event->add_record_snapshot('course', $course);
2724 $event->add_record_snapshot('feedback', $feedback);
2726 $event->trigger();
2728 return $return;
2731 ////////////////////////////////////////////////
2732 ////////////////////////////////////////////////
2733 ////////////////////////////////////////////////
2734 //functions to handle sitecourse mapping
2735 ////////////////////////////////////////////////
2738 * checks if the course and the feedback is in the table feedback_sitecourse_map.
2740 * @global object
2741 * @param int $feedbackid
2742 * @param int $courseid
2743 * @return int the count of records
2745 function feedback_is_course_in_sitecourse_map($feedbackid, $courseid) {
2746 global $DB;
2747 $params = array('feedbackid'=>$feedbackid, 'courseid'=>$courseid);
2748 return $DB->count_records('feedback_sitecourse_map', $params);
2752 * checks if the feedback is in the table feedback_sitecourse_map.
2754 * @global object
2755 * @param int $feedbackid
2756 * @return boolean
2758 function feedback_is_feedback_in_sitecourse_map($feedbackid) {
2759 global $DB;
2760 return $DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$feedbackid));
2764 * gets the feedbacks from table feedback_sitecourse_map.
2765 * this is used to show the global feedbacks on the feedback block
2766 * all feedbacks with the following criteria will be selected:<br />
2768 * 1) all feedbacks which id are listed together with the courseid in sitecoursemap and<br />
2769 * 2) all feedbacks which not are listed in sitecoursemap
2771 * @global object
2772 * @param int $courseid
2773 * @return array the feedback-records
2775 function feedback_get_feedbacks_from_sitecourse_map($courseid) {
2776 global $DB;
2778 //first get all feedbacks listed in sitecourse_map with named courseid
2779 $sql = "SELECT f.id AS id,
2780 cm.id AS cmid,
2781 f.name AS name,
2782 f.timeopen AS timeopen,
2783 f.timeclose AS timeclose
2784 FROM {feedback} f, {course_modules} cm, {feedback_sitecourse_map} sm, {modules} m
2785 WHERE f.id = cm.instance
2786 AND f.course = '".SITEID."'
2787 AND m.id = cm.module
2788 AND m.name = 'feedback'
2789 AND sm.courseid = ?
2790 AND sm.feedbackid = f.id";
2792 if (!$feedbacks1 = $DB->get_records_sql($sql, array($courseid))) {
2793 $feedbacks1 = array();
2796 //second get all feedbacks not listed in sitecourse_map
2797 $feedbacks2 = array();
2798 $sql = "SELECT f.id AS id,
2799 cm.id AS cmid,
2800 f.name AS name,
2801 f.timeopen AS timeopen,
2802 f.timeclose AS timeclose
2803 FROM {feedback} f, {course_modules} cm, {modules} m
2804 WHERE f.id = cm.instance
2805 AND f.course = '".SITEID."'
2806 AND m.id = cm.module
2807 AND m.name = 'feedback'";
2808 if (!$allfeedbacks = $DB->get_records_sql($sql)) {
2809 $allfeedbacks = array();
2811 foreach ($allfeedbacks as $a) {
2812 if (!$DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$a->id))) {
2813 $feedbacks2[] = $a;
2817 return array_merge($feedbacks1, $feedbacks2);
2822 * gets the courses from table feedback_sitecourse_map.
2824 * @global object
2825 * @param int $feedbackid
2826 * @return array the course-records
2828 function feedback_get_courses_from_sitecourse_map($feedbackid) {
2829 global $DB;
2831 $sql = "SELECT f.id, f.courseid, c.fullname, c.shortname
2832 FROM {feedback_sitecourse_map} f, {course} c
2833 WHERE c.id = f.courseid
2834 AND f.feedbackid = ?
2835 ORDER BY c.fullname";
2837 return $DB->get_records_sql($sql, array($feedbackid));
2842 * removes non existing courses or feedbacks from sitecourse_map.
2843 * it shouldn't be called all too often
2844 * a good place for it could be the mapcourse.php or unmapcourse.php
2846 * @global object
2847 * @return void
2849 function feedback_clean_up_sitecourse_map() {
2850 global $DB;
2852 $maps = $DB->get_records('feedback_sitecourse_map');
2853 foreach ($maps as $map) {
2854 if (!$DB->get_record('course', array('id'=>$map->courseid))) {
2855 $params = array('courseid'=>$map->courseid, 'feedbackid'=>$map->feedbackid);
2856 $DB->delete_records('feedback_sitecourse_map', $params);
2857 continue;
2859 if (!$DB->get_record('feedback', array('id'=>$map->feedbackid))) {
2860 $params = array('courseid'=>$map->courseid, 'feedbackid'=>$map->feedbackid);
2861 $DB->delete_records('feedback_sitecourse_map', $params);
2862 continue;
2868 ////////////////////////////////////////////////
2869 ////////////////////////////////////////////////
2870 ////////////////////////////////////////////////
2871 //not relatable functions
2872 ////////////////////////////////////////////////
2875 * prints the option items of a selection-input item (dropdownlist).
2876 * @param int $startval the first value of the list
2877 * @param int $endval the last value of the list
2878 * @param int $selectval which item should be selected
2879 * @param int $interval the stepsize from the first to the last value
2880 * @return void
2882 function feedback_print_numeric_option_list($startval, $endval, $selectval = '', $interval = 1) {
2883 for ($i = $startval; $i <= $endval; $i += $interval) {
2884 if ($selectval == ($i)) {
2885 $selected = 'selected="selected"';
2886 } else {
2887 $selected = '';
2889 echo '<option '.$selected.'>'.$i.'</option>';
2894 * sends an email to the teachers of the course where the given feedback is placed.
2896 * @global object
2897 * @global object
2898 * @uses FEEDBACK_ANONYMOUS_NO
2899 * @uses FORMAT_PLAIN
2900 * @param object $cm the coursemodule-record
2901 * @param object $feedback
2902 * @param object $course
2903 * @param int $userid
2904 * @return void
2906 function feedback_send_email($cm, $feedback, $course, $userid) {
2907 global $CFG, $DB;
2909 if ($feedback->email_notification == 0) { // No need to do anything
2910 return;
2913 $user = $DB->get_record('user', array('id'=>$userid));
2915 if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
2916 $groupmode = $cm->groupmode;
2917 } else {
2918 $groupmode = $course->groupmode;
2921 if ($groupmode == SEPARATEGROUPS) {
2922 $groups = $DB->get_records_sql_menu("SELECT g.name, g.id
2923 FROM {groups} g, {groups_members} m
2924 WHERE g.courseid = ?
2925 AND g.id = m.groupid
2926 AND m.userid = ?
2927 ORDER BY name ASC", array($course->id, $userid));
2928 $groups = array_values($groups);
2930 $teachers = feedback_get_receivemail_users($cm->id, $groups);
2931 } else {
2932 $teachers = feedback_get_receivemail_users($cm->id);
2935 if ($teachers) {
2937 $strfeedbacks = get_string('modulenameplural', 'feedback');
2938 $strfeedback = get_string('modulename', 'feedback');
2939 $strcompleted = get_string('completed', 'feedback');
2941 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
2942 $printusername = fullname($user);
2943 } else {
2944 $printusername = get_string('anonymous_user', 'feedback');
2947 foreach ($teachers as $teacher) {
2948 $info = new stdClass();
2949 $info->username = $printusername;
2950 $info->feedback = format_string($feedback->name, true);
2951 $info->url = $CFG->wwwroot.'/mod/feedback/show_entries.php?'.
2952 'id='.$cm->id.'&'.
2953 'userid='.$userid.'&'.
2954 'do_show=showentries';
2956 $postsubject = $strcompleted.': '.$info->username.' -> '.$feedback->name;
2957 $posttext = feedback_send_email_text($info, $course);
2959 if ($teacher->mailformat == 1) {
2960 $posthtml = feedback_send_email_html($info, $course, $cm);
2961 } else {
2962 $posthtml = '';
2965 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
2966 $eventdata = new stdClass();
2967 $eventdata->name = 'submission';
2968 $eventdata->component = 'mod_feedback';
2969 $eventdata->userfrom = $user;
2970 $eventdata->userto = $teacher;
2971 $eventdata->subject = $postsubject;
2972 $eventdata->fullmessage = $posttext;
2973 $eventdata->fullmessageformat = FORMAT_PLAIN;
2974 $eventdata->fullmessagehtml = $posthtml;
2975 $eventdata->smallmessage = '';
2976 message_send($eventdata);
2977 } else {
2978 $eventdata = new stdClass();
2979 $eventdata->name = 'submission';
2980 $eventdata->component = 'mod_feedback';
2981 $eventdata->userfrom = $teacher;
2982 $eventdata->userto = $teacher;
2983 $eventdata->subject = $postsubject;
2984 $eventdata->fullmessage = $posttext;
2985 $eventdata->fullmessageformat = FORMAT_PLAIN;
2986 $eventdata->fullmessagehtml = $posthtml;
2987 $eventdata->smallmessage = '';
2988 message_send($eventdata);
2995 * sends an email to the teachers of the course where the given feedback is placed.
2997 * @global object
2998 * @uses FORMAT_PLAIN
2999 * @param object $cm the coursemodule-record
3000 * @param object $feedback
3001 * @param object $course
3002 * @return void
3004 function feedback_send_email_anonym($cm, $feedback, $course) {
3005 global $CFG;
3007 if ($feedback->email_notification == 0) { // No need to do anything
3008 return;
3011 $teachers = feedback_get_receivemail_users($cm->id);
3013 if ($teachers) {
3015 $strfeedbacks = get_string('modulenameplural', 'feedback');
3016 $strfeedback = get_string('modulename', 'feedback');
3017 $strcompleted = get_string('completed', 'feedback');
3018 $printusername = get_string('anonymous_user', 'feedback');
3020 foreach ($teachers as $teacher) {
3021 $info = new stdClass();
3022 $info->username = $printusername;
3023 $info->feedback = format_string($feedback->name, true);
3024 $info->url = $CFG->wwwroot.'/mod/feedback/show_entries_anonym.php?id='.$cm->id;
3026 $postsubject = $strcompleted.': '.$info->username.' -> '.$feedback->name;
3027 $posttext = feedback_send_email_text($info, $course);
3029 if ($teacher->mailformat == 1) {
3030 $posthtml = feedback_send_email_html($info, $course, $cm);
3031 } else {
3032 $posthtml = '';
3035 $eventdata = new stdClass();
3036 $eventdata->name = 'submission';
3037 $eventdata->component = 'mod_feedback';
3038 $eventdata->userfrom = $teacher;
3039 $eventdata->userto = $teacher;
3040 $eventdata->subject = $postsubject;
3041 $eventdata->fullmessage = $posttext;
3042 $eventdata->fullmessageformat = FORMAT_PLAIN;
3043 $eventdata->fullmessagehtml = $posthtml;
3044 $eventdata->smallmessage = '';
3045 message_send($eventdata);
3051 * send the text-part of the email
3053 * @param object $info includes some infos about the feedback you want to send
3054 * @param object $course
3055 * @return string the text you want to post
3057 function feedback_send_email_text($info, $course) {
3058 $coursecontext = context_course::instance($course->id);
3059 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
3060 $posttext = $courseshortname.' -> '.get_string('modulenameplural', 'feedback').' -> '.
3061 $info->feedback."\n";
3062 $posttext .= '---------------------------------------------------------------------'."\n";
3063 $posttext .= get_string("emailteachermail", "feedback", $info)."\n";
3064 $posttext .= '---------------------------------------------------------------------'."\n";
3065 return $posttext;
3070 * send the html-part of the email
3072 * @global object
3073 * @param object $info includes some infos about the feedback you want to send
3074 * @param object $course
3075 * @return string the text you want to post
3077 function feedback_send_email_html($info, $course, $cm) {
3078 global $CFG;
3079 $coursecontext = context_course::instance($course->id);
3080 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
3081 $course_url = $CFG->wwwroot.'/course/view.php?id='.$course->id;
3082 $feedback_all_url = $CFG->wwwroot.'/mod/feedback/index.php?id='.$course->id;
3083 $feedback_url = $CFG->wwwroot.'/mod/feedback/view.php?id='.$cm->id;
3085 $posthtml = '<p><font face="sans-serif">'.
3086 '<a href="'.$course_url.'">'.$courseshortname.'</a> ->'.
3087 '<a href="'.$feedback_all_url.'">'.get_string('modulenameplural', 'feedback').'</a> ->'.
3088 '<a href="'.$feedback_url.'">'.$info->feedback.'</a></font></p>';
3089 $posthtml .= '<hr /><font face="sans-serif">';
3090 $posthtml .= '<p>'.get_string('emailteachermailhtml', 'feedback', $info).'</p>';
3091 $posthtml .= '</font><hr />';
3092 return $posthtml;
3096 * @param string $url
3097 * @return string
3099 function feedback_encode_target_url($url) {
3100 if (strpos($url, '?')) {
3101 list($part1, $part2) = explode('?', $url, 2); //maximal 2 parts
3102 return $part1 . '?' . htmlentities($part2);
3103 } else {
3104 return $url;
3109 * Adds module specific settings to the settings block
3111 * @param settings_navigation $settings The settings navigation object
3112 * @param navigation_node $feedbacknode The node to add module settings to
3114 function feedback_extend_settings_navigation(settings_navigation $settings,
3115 navigation_node $feedbacknode) {
3117 global $PAGE, $DB;
3119 if (!$context = context_module::instance($PAGE->cm->id, IGNORE_MISSING)) {
3120 print_error('badcontext');
3123 if (has_capability('mod/feedback:edititems', $context)) {
3124 $questionnode = $feedbacknode->add(get_string('questions', 'feedback'));
3126 $questionnode->add(get_string('edit_items', 'feedback'),
3127 new moodle_url('/mod/feedback/edit.php',
3128 array('id' => $PAGE->cm->id,
3129 'do_show' => 'edit')));
3131 $questionnode->add(get_string('export_questions', 'feedback'),
3132 new moodle_url('/mod/feedback/export.php',
3133 array('id' => $PAGE->cm->id,
3134 'action' => 'exportfile')));
3136 $questionnode->add(get_string('import_questions', 'feedback'),
3137 new moodle_url('/mod/feedback/import.php',
3138 array('id' => $PAGE->cm->id)));
3140 $questionnode->add(get_string('templates', 'feedback'),
3141 new moodle_url('/mod/feedback/edit.php',
3142 array('id' => $PAGE->cm->id,
3143 'do_show' => 'templates')));
3146 if (has_capability('mod/feedback:viewreports', $context)) {
3147 $feedback = $DB->get_record('feedback', array('id'=>$PAGE->cm->instance));
3148 if ($feedback->course == SITEID) {
3149 $feedbacknode->add(get_string('analysis', 'feedback'),
3150 new moodle_url('/mod/feedback/analysis_course.php',
3151 array('id' => $PAGE->cm->id,
3152 'course' => $PAGE->course->id,
3153 'do_show' => 'analysis')));
3154 } else {
3155 $feedbacknode->add(get_string('analysis', 'feedback'),
3156 new moodle_url('/mod/feedback/analysis.php',
3157 array('id' => $PAGE->cm->id,
3158 'course' => $PAGE->course->id,
3159 'do_show' => 'analysis')));
3162 $feedbacknode->add(get_string('show_entries', 'feedback'),
3163 new moodle_url('/mod/feedback/show_entries.php',
3164 array('id' => $PAGE->cm->id,
3165 'do_show' => 'showentries')));
3169 function feedback_init_feedback_session() {
3170 //initialize the feedback-Session - not nice at all!!
3171 global $SESSION;
3172 if (!empty($SESSION)) {
3173 if (!isset($SESSION->feedback) OR !is_object($SESSION->feedback)) {
3174 $SESSION->feedback = new stdClass();
3180 * Return a list of page types
3181 * @param string $pagetype current page type
3182 * @param stdClass $parentcontext Block's parent context
3183 * @param stdClass $currentcontext Current context of block
3185 function feedback_page_type_list($pagetype, $parentcontext, $currentcontext) {
3186 $module_pagetype = array('mod-feedback-*'=>get_string('page-mod-feedback-x', 'feedback'));
3187 return $module_pagetype;
3191 * Move save the items of the given $feedback in the order of $itemlist.
3192 * @param string $itemlist a comma separated list with item ids
3193 * @param stdClass $feedback
3194 * @return bool true if success
3196 function feedback_ajax_saveitemorder($itemlist, $feedback) {
3197 global $DB;
3199 $result = true;
3200 $position = 0;
3201 foreach ($itemlist as $itemid) {
3202 $position++;
3203 $result = $result && $DB->set_field('feedback_item',
3204 'position',
3205 $position,
3206 array('id'=>$itemid, 'feedback'=>$feedback->id));
3208 return $result;