Merge branch 'MDL-46239-28-2' of git://github.com/xow/moodle into MOODLE_28_STABLE
[moodle.git] / mod / feedback / lib.php
blob589d3d85a92e8b3780511380cc01728799af113d
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_MOD_INTRO
56 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
57 * @uses FEATURE_GRADE_HAS_GRADE
58 * @uses FEATURE_GRADE_OUTCOMES
59 * @param string $feature FEATURE_xx constant for requested feature
60 * @return mixed True if module supports feature, null if doesn't know
62 function feedback_supports($feature) {
63 switch($feature) {
64 case FEATURE_GROUPS: return true;
65 case FEATURE_GROUPINGS: return true;
66 case FEATURE_MOD_INTRO: return true;
67 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
68 case FEATURE_COMPLETION_HAS_RULES: return true;
69 case FEATURE_GRADE_HAS_GRADE: return false;
70 case FEATURE_GRADE_OUTCOMES: return false;
71 case FEATURE_BACKUP_MOODLE2: return true;
72 case FEATURE_SHOW_DESCRIPTION: return true;
74 default: return null;
78 /**
79 * this will create a new instance and return the id number
80 * of the new instance.
82 * @global object
83 * @param object $feedback the object given by mod_feedback_mod_form
84 * @return int
86 function feedback_add_instance($feedback) {
87 global $DB;
89 $feedback->timemodified = time();
90 $feedback->id = '';
92 if (empty($feedback->site_after_submit)) {
93 $feedback->site_after_submit = '';
96 //saving the feedback in db
97 $feedbackid = $DB->insert_record("feedback", $feedback);
99 $feedback->id = $feedbackid;
101 feedback_set_events($feedback);
103 if (!isset($feedback->coursemodule)) {
104 $cm = get_coursemodule_from_id('feedback', $feedback->id);
105 $feedback->coursemodule = $cm->id;
107 $context = context_module::instance($feedback->coursemodule);
109 $editoroptions = feedback_get_editor_options();
111 // process the custom wysiwyg editor in page_after_submit
112 if ($draftitemid = $feedback->page_after_submit_editor['itemid']) {
113 $feedback->page_after_submit = file_save_draft_area_files($draftitemid, $context->id,
114 'mod_feedback', 'page_after_submit',
115 0, $editoroptions,
116 $feedback->page_after_submit_editor['text']);
118 $feedback->page_after_submitformat = $feedback->page_after_submit_editor['format'];
120 $DB->update_record('feedback', $feedback);
122 return $feedbackid;
126 * this will update a given instance
128 * @global object
129 * @param object $feedback the object given by mod_feedback_mod_form
130 * @return boolean
132 function feedback_update_instance($feedback) {
133 global $DB;
135 $feedback->timemodified = time();
136 $feedback->id = $feedback->instance;
138 if (empty($feedback->site_after_submit)) {
139 $feedback->site_after_submit = '';
142 //save the feedback into the db
143 $DB->update_record("feedback", $feedback);
145 //create or update the new events
146 feedback_set_events($feedback);
148 $context = context_module::instance($feedback->coursemodule);
150 $editoroptions = feedback_get_editor_options();
152 // process the custom wysiwyg editor in page_after_submit
153 if ($draftitemid = $feedback->page_after_submit_editor['itemid']) {
154 $feedback->page_after_submit = file_save_draft_area_files($draftitemid, $context->id,
155 'mod_feedback', 'page_after_submit',
156 0, $editoroptions,
157 $feedback->page_after_submit_editor['text']);
159 $feedback->page_after_submitformat = $feedback->page_after_submit_editor['format'];
161 $DB->update_record('feedback', $feedback);
163 return true;
167 * Serves the files included in feedback items like label. Implements needed access control ;-)
169 * There are two situations in general where the files will be sent.
170 * 1) filearea = item, 2) filearea = template
172 * @package mod_feedback
173 * @category files
174 * @param stdClass $course course object
175 * @param stdClass $cm course module object
176 * @param stdClass $context context object
177 * @param string $filearea file area
178 * @param array $args extra arguments
179 * @param bool $forcedownload whether or not force download
180 * @param array $options additional options affecting the file serving
181 * @return bool false if file not found, does not return if found - justsend the file
183 function feedback_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
184 global $CFG, $DB;
186 if ($filearea === 'item' or $filearea === 'template') {
187 $itemid = (int)array_shift($args);
188 //get the item what includes the file
189 if (!$item = $DB->get_record('feedback_item', array('id'=>$itemid))) {
190 return false;
192 $feedbackid = $item->feedback;
193 $templateid = $item->template;
196 if ($filearea === 'page_after_submit' or $filearea === 'item') {
197 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
198 return false;
201 $feedbackid = $feedback->id;
203 //if the filearea is "item" so we check the permissions like view/complete the feedback
204 $canload = false;
205 //first check whether the user has the complete capability
206 if (has_capability('mod/feedback:complete', $context)) {
207 $canload = true;
210 //now we check whether the user has the view capability
211 if (has_capability('mod/feedback:view', $context)) {
212 $canload = true;
215 //if the feedback is on frontpage and anonymous and the fullanonymous is allowed
216 //so the file can be loaded too.
217 if (isset($CFG->feedback_allowfullanonymous)
218 AND $CFG->feedback_allowfullanonymous
219 AND $course->id == SITEID
220 AND $feedback->anonymous == FEEDBACK_ANONYMOUS_YES ) {
221 $canload = true;
224 if (!$canload) {
225 return false;
227 } else if ($filearea === 'template') { //now we check files in templates
228 if (!$template = $DB->get_record('feedback_template', array('id'=>$templateid))) {
229 return false;
232 //if the file is not public so the capability edititems has to be there
233 if (!$template->ispublic) {
234 if (!has_capability('mod/feedback:edititems', $context)) {
235 return false;
237 } else { //on public templates, at least the user has to be logged in
238 if (!isloggedin()) {
239 return false;
242 } else {
243 return false;
246 if ($context->contextlevel == CONTEXT_MODULE) {
247 if ($filearea !== 'item' and $filearea !== 'page_after_submit') {
248 return false;
252 if ($context->contextlevel == CONTEXT_COURSE || $context->contextlevel == CONTEXT_SYSTEM) {
253 if ($filearea !== 'template') {
254 return false;
258 $relativepath = implode('/', $args);
259 if ($filearea === 'page_after_submit') {
260 $fullpath = "/{$context->id}/mod_feedback/$filearea/$relativepath";
261 } else {
262 $fullpath = "/{$context->id}/mod_feedback/$filearea/{$item->id}/$relativepath";
265 $fs = get_file_storage();
267 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
268 return false;
271 // finally send the file
272 send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
274 return false;
278 * this will delete a given instance.
279 * all referenced data also will be deleted
281 * @global object
282 * @param int $id the instanceid of feedback
283 * @return boolean
285 function feedback_delete_instance($id) {
286 global $DB;
288 //get all referenced items
289 $feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$id));
291 //deleting all referenced items and values
292 if (is_array($feedbackitems)) {
293 foreach ($feedbackitems as $feedbackitem) {
294 $DB->delete_records("feedback_value", array("item"=>$feedbackitem->id));
295 $DB->delete_records("feedback_valuetmp", array("item"=>$feedbackitem->id));
297 if ($delitems = $DB->get_records("feedback_item", array("feedback"=>$id))) {
298 foreach ($delitems as $delitem) {
299 feedback_delete_item($delitem->id, false);
304 //deleting the referenced tracking data
305 $DB->delete_records('feedback_tracking', array('feedback'=>$id));
307 //deleting the completeds
308 $DB->delete_records("feedback_completed", array("feedback"=>$id));
310 //deleting the unfinished completeds
311 $DB->delete_records("feedback_completedtmp", array("feedback"=>$id));
313 //deleting old events
314 $DB->delete_records('event', array('modulename'=>'feedback', 'instance'=>$id));
315 return $DB->delete_records("feedback", array("id"=>$id));
319 * this is called after deleting all instances if the course will be deleted.
320 * only templates have to be deleted
322 * @global object
323 * @param object $course
324 * @return boolean
326 function feedback_delete_course($course) {
327 global $DB;
329 //delete all templates of given course
330 return $DB->delete_records('feedback_template', array('course'=>$course->id));
334 * Return a small object with summary information about what a
335 * user has done with a given particular instance of this module
336 * Used for user activity reports.
337 * $return->time = the time they did it
338 * $return->info = a short text description
340 * @param object $course
341 * @param object $user
342 * @param object $mod
343 * @param object $feedback
344 * @return object
346 function feedback_user_outline($course, $user, $mod, $feedback) {
347 return null;
351 * Returns all users who has completed a specified feedback since a given time
352 * many thanks to Manolescu Dorel, who contributed these two functions
354 * @global object
355 * @global object
356 * @global object
357 * @global object
358 * @uses CONTEXT_MODULE
359 * @param array $activities Passed by reference
360 * @param int $index Passed by reference
361 * @param int $timemodified Timestamp
362 * @param int $courseid
363 * @param int $cmid
364 * @param int $userid
365 * @param int $groupid
366 * @return void
368 function feedback_get_recent_mod_activity(&$activities, &$index,
369 $timemodified, $courseid,
370 $cmid, $userid="", $groupid="") {
372 global $CFG, $COURSE, $USER, $DB;
374 if ($COURSE->id == $courseid) {
375 $course = $COURSE;
376 } else {
377 $course = $DB->get_record('course', array('id'=>$courseid));
380 $modinfo = get_fast_modinfo($course);
382 $cm = $modinfo->cms[$cmid];
384 $sqlargs = array();
386 $userfields = user_picture::fields('u', null, 'useridagain');
387 $sql = " SELECT fk . * , fc . * , $userfields
388 FROM {feedback_completed} fc
389 JOIN {feedback} fk ON fk.id = fc.feedback
390 JOIN {user} u ON u.id = fc.userid ";
392 if ($groupid) {
393 $sql .= " JOIN {groups_members} gm ON gm.userid=u.id ";
396 $sql .= " WHERE fc.timemodified > ?
397 AND fk.id = ?
398 AND fc.anonymous_response = ?";
399 $sqlargs[] = $timemodified;
400 $sqlargs[] = $cm->instance;
401 $sqlargs[] = FEEDBACK_ANONYMOUS_NO;
403 if ($userid) {
404 $sql .= " AND u.id = ? ";
405 $sqlargs[] = $userid;
408 if ($groupid) {
409 $sql .= " AND gm.groupid = ? ";
410 $sqlargs[] = $groupid;
413 if (!$feedbackitems = $DB->get_records_sql($sql, $sqlargs)) {
414 return;
417 $cm_context = context_module::instance($cm->id);
419 if (!has_capability('mod/feedback:view', $cm_context)) {
420 return;
423 $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
424 $viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context);
425 $groupmode = groups_get_activity_groupmode($cm, $course);
427 $aname = format_string($cm->name, true);
428 foreach ($feedbackitems as $feedbackitem) {
429 if ($feedbackitem->userid != $USER->id) {
431 if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
432 $usersgroups = groups_get_all_groups($course->id,
433 $feedbackitem->userid,
434 $cm->groupingid);
435 if (!is_array($usersgroups)) {
436 continue;
438 $usersgroups = array_keys($usersgroups);
439 $intersect = array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid));
440 if (empty($intersect)) {
441 continue;
446 $tmpactivity = new stdClass();
448 $tmpactivity->type = 'feedback';
449 $tmpactivity->cmid = $cm->id;
450 $tmpactivity->name = $aname;
451 $tmpactivity->sectionnum= $cm->sectionnum;
452 $tmpactivity->timestamp = $feedbackitem->timemodified;
454 $tmpactivity->content = new stdClass();
455 $tmpactivity->content->feedbackid = $feedbackitem->id;
456 $tmpactivity->content->feedbackuserid = $feedbackitem->userid;
458 $tmpactivity->user = user_picture::unalias($feedbackitem, null, 'useridagain');
459 $tmpactivity->user->fullname = fullname($feedbackitem, $viewfullnames);
461 $activities[$index++] = $tmpactivity;
464 return;
468 * Prints all users who has completed a specified feedback since a given time
469 * many thanks to Manolescu Dorel, who contributed these two functions
471 * @global object
472 * @param object $activity
473 * @param int $courseid
474 * @param string $detail
475 * @param array $modnames
476 * @return void Output is echo'd
478 function feedback_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
479 global $CFG, $OUTPUT;
481 echo '<table border="0" cellpadding="3" cellspacing="0" class="forum-recent">';
483 echo "<tr><td class=\"userpicture\" valign=\"top\">";
484 echo $OUTPUT->user_picture($activity->user, array('courseid'=>$courseid));
485 echo "</td><td>";
487 if ($detail) {
488 $modname = $modnames[$activity->type];
489 echo '<div class="title">';
490 echo "<img src=\"" . $OUTPUT->pix_url('icon', $activity->type) . "\" ".
491 "class=\"icon\" alt=\"$modname\" />";
492 echo "<a href=\"$CFG->wwwroot/mod/feedback/view.php?id={$activity->cmid}\">{$activity->name}</a>";
493 echo '</div>';
496 echo '<div class="title">';
497 echo '</div>';
499 echo '<div class="user">';
500 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&amp;course=$courseid\">"
501 ."{$activity->user->fullname}</a> - ".userdate($activity->timestamp);
502 echo '</div>';
504 echo "</td></tr></table>";
506 return;
510 * Obtains the automatic completion state for this feedback based on the condition
511 * in feedback settings.
513 * @param object $course Course
514 * @param object $cm Course-module
515 * @param int $userid User ID
516 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
517 * @return bool True if completed, false if not, $type if conditions not set.
519 function feedback_get_completion_state($course, $cm, $userid, $type) {
520 global $CFG, $DB;
522 // Get feedback details
523 $feedback = $DB->get_record('feedback', array('id'=>$cm->instance), '*', MUST_EXIST);
525 // If completion option is enabled, evaluate it and return true/false
526 if ($feedback->completionsubmit) {
527 $params = array('userid'=>$userid, 'feedback'=>$feedback->id);
528 return $DB->record_exists('feedback_tracking', $params);
529 } else {
530 // Completion option is not enabled so just return $type
531 return $type;
537 * Print a detailed representation of what a user has done with
538 * a given particular instance of this module, for user activity reports.
540 * @param object $course
541 * @param object $user
542 * @param object $mod
543 * @param object $feedback
544 * @return bool
546 function feedback_user_complete($course, $user, $mod, $feedback) {
547 return true;
551 * @return bool true
553 function feedback_cron () {
554 return true;
558 * @return bool false
560 function feedback_scale_used ($feedbackid, $scaleid) {
561 return false;
565 * Checks if scale is being used by any instance of feedback
567 * This is used to find out if scale used anywhere
568 * @param $scaleid int
569 * @return boolean True if the scale is used by any assignment
571 function feedback_scale_used_anywhere($scaleid) {
572 return false;
576 * List the actions that correspond to a view of this module.
577 * This is used by the participation report.
579 * Note: This is not used by new logging system. Event with
580 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
581 * be considered as view action.
583 * @return array
585 function feedback_get_view_actions() {
586 return array('view', 'view all');
590 * List the actions that correspond to a post of this module.
591 * This is used by the participation report.
593 * Note: This is not used by new logging system. Event with
594 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
595 * will be considered as post action.
597 * @return array
599 function feedback_get_post_actions() {
600 return array('submit');
604 * This function is used by the reset_course_userdata function in moodlelib.
605 * This function will remove all responses from the specified feedback
606 * and clean up any related data.
608 * @global object
609 * @global object
610 * @uses FEEDBACK_RESETFORM_RESET
611 * @uses FEEDBACK_RESETFORM_DROP
612 * @param object $data the data submitted from the reset course.
613 * @return array status array
615 function feedback_reset_userdata($data) {
616 global $CFG, $DB;
618 $resetfeedbacks = array();
619 $dropfeedbacks = array();
620 $status = array();
621 $componentstr = get_string('modulenameplural', 'feedback');
623 //get the relevant entries from $data
624 foreach ($data as $key => $value) {
625 switch(true) {
626 case substr($key, 0, strlen(FEEDBACK_RESETFORM_RESET)) == FEEDBACK_RESETFORM_RESET:
627 if ($value == 1) {
628 $templist = explode('_', $key);
629 if (isset($templist[3])) {
630 $resetfeedbacks[] = intval($templist[3]);
633 break;
634 case substr($key, 0, strlen(FEEDBACK_RESETFORM_DROP)) == FEEDBACK_RESETFORM_DROP:
635 if ($value == 1) {
636 $templist = explode('_', $key);
637 if (isset($templist[3])) {
638 $dropfeedbacks[] = intval($templist[3]);
641 break;
645 //reset the selected feedbacks
646 foreach ($resetfeedbacks as $id) {
647 $feedback = $DB->get_record('feedback', array('id'=>$id));
648 feedback_delete_all_completeds($id);
649 $status[] = array('component'=>$componentstr.':'.$feedback->name,
650 'item'=>get_string('resetting_data', 'feedback'),
651 'error'=>false);
654 return $status;
658 * Called by course/reset.php
660 * @global object
661 * @uses FEEDBACK_RESETFORM_RESET
662 * @param object $mform form passed by reference
664 function feedback_reset_course_form_definition(&$mform) {
665 global $COURSE, $DB;
667 $mform->addElement('header', 'feedbackheader', get_string('modulenameplural', 'feedback'));
669 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$COURSE->id), 'name')) {
670 return;
673 $mform->addElement('static', 'hint', get_string('resetting_data', 'feedback'));
674 foreach ($feedbacks as $feedback) {
675 $mform->addElement('checkbox', FEEDBACK_RESETFORM_RESET.$feedback->id, $feedback->name);
680 * Course reset form defaults.
682 * @global object
683 * @uses FEEDBACK_RESETFORM_RESET
684 * @param object $course
686 function feedback_reset_course_form_defaults($course) {
687 global $DB;
689 $return = array();
690 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id), 'name')) {
691 return;
693 foreach ($feedbacks as $feedback) {
694 $return[FEEDBACK_RESETFORM_RESET.$feedback->id] = true;
696 return $return;
700 * Called by course/reset.php and shows the formdata by coursereset.
701 * it prints checkboxes for each feedback available at the given course
702 * there are two checkboxes:
703 * 1) delete userdata and keep the feedback
704 * 2) delete userdata and drop the feedback
706 * @global object
707 * @uses FEEDBACK_RESETFORM_RESET
708 * @uses FEEDBACK_RESETFORM_DROP
709 * @param object $course
710 * @return void
712 function feedback_reset_course_form($course) {
713 global $DB, $OUTPUT;
715 echo get_string('resetting_feedbacks', 'feedback'); echo ':<br />';
716 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id), 'name')) {
717 return;
720 foreach ($feedbacks as $feedback) {
721 echo '<p>';
722 echo get_string('name', 'feedback').': '.$feedback->name.'<br />';
723 echo html_writer::checkbox(FEEDBACK_RESETFORM_RESET.$feedback->id,
724 1, true,
725 get_string('resetting_data', 'feedback'));
726 echo '<br />';
727 echo html_writer::checkbox(FEEDBACK_RESETFORM_DROP.$feedback->id,
728 1, false,
729 get_string('drop_feedback', 'feedback'));
730 echo '</p>';
735 * This gets an array with default options for the editor
737 * @return array the options
739 function feedback_get_editor_options() {
740 return array('maxfiles' => EDITOR_UNLIMITED_FILES,
741 'trusttext'=>true);
745 * This creates new events given as timeopen and closeopen by $feedback.
747 * @global object
748 * @param object $feedback
749 * @return void
751 function feedback_set_events($feedback) {
752 global $DB;
754 // adding the feedback to the eventtable (I have seen this at quiz-module)
755 $DB->delete_records('event', array('modulename'=>'feedback', 'instance'=>$feedback->id));
757 if (!isset($feedback->coursemodule)) {
758 $cm = get_coursemodule_from_id('feedback', $feedback->id);
759 $feedback->coursemodule = $cm->id;
762 // the open-event
763 if ($feedback->timeopen > 0) {
764 $event = new stdClass();
765 $event->name = get_string('start', 'feedback').' '.$feedback->name;
766 $event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
767 $event->courseid = $feedback->course;
768 $event->groupid = 0;
769 $event->userid = 0;
770 $event->modulename = 'feedback';
771 $event->instance = $feedback->id;
772 $event->eventtype = 'open';
773 $event->timestart = $feedback->timeopen;
774 $event->visible = instance_is_visible('feedback', $feedback);
775 if ($feedback->timeclose > 0) {
776 $event->timeduration = ($feedback->timeclose - $feedback->timeopen);
777 } else {
778 $event->timeduration = 0;
781 calendar_event::create($event);
784 // the close-event
785 if ($feedback->timeclose > 0) {
786 $event = new stdClass();
787 $event->name = get_string('stop', 'feedback').' '.$feedback->name;
788 $event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
789 $event->courseid = $feedback->course;
790 $event->groupid = 0;
791 $event->userid = 0;
792 $event->modulename = 'feedback';
793 $event->instance = $feedback->id;
794 $event->eventtype = 'close';
795 $event->timestart = $feedback->timeclose;
796 $event->visible = instance_is_visible('feedback', $feedback);
797 $event->timeduration = 0;
799 calendar_event::create($event);
804 * this function is called by {@link feedback_delete_userdata()}
805 * it drops the feedback-instance from the course_module table
807 * @global object
808 * @param int $id the id from the coursemodule
809 * @return boolean
811 function feedback_delete_course_module($id) {
812 global $DB;
814 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
815 return true;
817 return $DB->delete_records('course_modules', array('id'=>$cm->id));
822 ////////////////////////////////////////////////
823 //functions to handle capabilities
824 ////////////////////////////////////////////////
827 * returns the context-id related to the given coursemodule-id
829 * @staticvar object $context
830 * @param int $cmid the coursemodule-id
831 * @return object $context
833 function feedback_get_context($cmid) {
834 static $context;
836 if (isset($context)) {
837 return $context;
840 $context = context_module::instance($cmid);
841 return $context;
845 * returns true if the current role is faked by switching role feature
847 * @global object
848 * @return boolean
850 function feedback_check_is_switchrole() {
851 global $USER;
852 if (isset($USER->switchrole) AND
853 is_array($USER->switchrole) AND
854 count($USER->switchrole) > 0) {
856 return true;
858 return false;
862 * count users which have not completed the feedback
864 * @global object
865 * @uses CONTEXT_MODULE
866 * @param cm_info $cm Course-module object
867 * @param int $group single groupid
868 * @param string $sort
869 * @param int $startpage
870 * @param int $pagecount
871 * @return object the userrecords
873 function feedback_get_incomplete_users(cm_info $cm,
874 $group = false,
875 $sort = '',
876 $startpage = false,
877 $pagecount = false) {
879 global $DB;
881 $context = context_module::instance($cm->id);
883 //first get all user who can complete this feedback
884 $cap = 'mod/feedback:complete';
885 $fields = 'u.id, u.username';
886 if (!$allusers = get_users_by_capability($context,
887 $cap,
888 $fields,
889 $sort,
892 $group,
894 true)) {
895 return false;
897 // Filter users that are not in the correct group/grouping.
898 $info = new \core_availability\info_module($cm);
899 $allusers = $info->filter_user_list($allusers);
901 $allusers = array_keys($allusers);
903 //now get all completeds
904 $params = array('feedback'=>$cm->instance);
905 if (!$completedusers = $DB->get_records_menu('feedback_completed', $params, '', 'userid,id')) {
906 return $allusers;
908 $completedusers = array_keys($completedusers);
910 //now strike all completedusers from allusers
911 $allusers = array_diff($allusers, $completedusers);
913 //for paging I use array_slice()
914 if ($startpage !== false AND $pagecount !== false) {
915 $allusers = array_slice($allusers, $startpage, $pagecount);
918 return $allusers;
922 * count users which have not completed the feedback
924 * @global object
925 * @param object $cm
926 * @param int $group single groupid
927 * @return int count of userrecords
929 function feedback_count_incomplete_users($cm, $group = false) {
930 if ($allusers = feedback_get_incomplete_users($cm, $group)) {
931 return count($allusers);
933 return 0;
937 * count users which have completed a feedback
939 * @global object
940 * @uses FEEDBACK_ANONYMOUS_NO
941 * @param object $cm
942 * @param int $group single groupid
943 * @return int count of userrecords
945 function feedback_count_complete_users($cm, $group = false) {
946 global $DB;
948 $params = array(FEEDBACK_ANONYMOUS_NO, $cm->instance);
950 $fromgroup = '';
951 $wheregroup = '';
952 if ($group) {
953 $fromgroup = ', {groups_members} g';
954 $wheregroup = ' AND g.groupid = ? AND g.userid = c.userid';
955 $params[] = $group;
958 $sql = 'SELECT COUNT(u.id) FROM {user} u, {feedback_completed} c'.$fromgroup.'
959 WHERE anonymous_response = ? AND u.id = c.userid AND c.feedback = ?
960 '.$wheregroup;
962 return $DB->count_records_sql($sql, $params);
967 * get users which have completed a feedback
969 * @global object
970 * @uses CONTEXT_MODULE
971 * @uses FEEDBACK_ANONYMOUS_NO
972 * @param object $cm
973 * @param int $group single groupid
974 * @param string $where a sql where condition (must end with " AND ")
975 * @param array parameters used in $where
976 * @param string $sort a table field
977 * @param int $startpage
978 * @param int $pagecount
979 * @return object the userrecords
981 function feedback_get_complete_users($cm,
982 $group = false,
983 $where = '',
984 array $params = null,
985 $sort = '',
986 $startpage = false,
987 $pagecount = false) {
989 global $DB;
991 $context = context_module::instance($cm->id);
993 $params = (array)$params;
995 $params['anon'] = FEEDBACK_ANONYMOUS_NO;
996 $params['instance'] = $cm->instance;
998 $fromgroup = '';
999 $wheregroup = '';
1000 if ($group) {
1001 $fromgroup = ', {groups_members} g';
1002 $wheregroup = ' AND g.groupid = :group AND g.userid = c.userid';
1003 $params['group'] = $group;
1006 if ($sort) {
1007 $sortsql = ' ORDER BY '.$sort;
1008 } else {
1009 $sortsql = '';
1012 $ufields = user_picture::fields('u');
1013 $sql = 'SELECT DISTINCT '.$ufields.', c.timemodified as completed_timemodified
1014 FROM {user} u, {feedback_completed} c '.$fromgroup.'
1015 WHERE '.$where.' anonymous_response = :anon
1016 AND u.id = c.userid
1017 AND c.feedback = :instance
1018 '.$wheregroup.$sortsql;
1020 if ($startpage === false OR $pagecount === false) {
1021 $startpage = false;
1022 $pagecount = false;
1024 return $DB->get_records_sql($sql, $params, $startpage, $pagecount);
1028 * get users which have the viewreports-capability
1030 * @uses CONTEXT_MODULE
1031 * @param int $cmid
1032 * @param mixed $groups single groupid or array of groupids - group(s) user is in
1033 * @return object the userrecords
1035 function feedback_get_viewreports_users($cmid, $groups = false) {
1037 $context = context_module::instance($cmid);
1039 //description of the call below:
1040 //get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='',
1041 // $limitnum='', $groups='', $exceptions='', $doanything=true)
1042 return get_users_by_capability($context,
1043 'mod/feedback:viewreports',
1045 'lastname',
1048 $groups,
1050 false);
1054 * get users which have the receivemail-capability
1056 * @uses CONTEXT_MODULE
1057 * @param int $cmid
1058 * @param mixed $groups single groupid or array of groupids - group(s) user is in
1059 * @return object the userrecords
1061 function feedback_get_receivemail_users($cmid, $groups = false) {
1063 $context = context_module::instance($cmid);
1065 //description of the call below:
1066 //get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='',
1067 // $limitnum='', $groups='', $exceptions='', $doanything=true)
1068 return get_users_by_capability($context,
1069 'mod/feedback:receivemail',
1071 'lastname',
1074 $groups,
1076 false);
1079 ////////////////////////////////////////////////
1080 //functions to handle the templates
1081 ////////////////////////////////////////////////
1082 ////////////////////////////////////////////////
1085 * creates a new template-record.
1087 * @global object
1088 * @param int $courseid
1089 * @param string $name the name of template shown in the templatelist
1090 * @param int $ispublic 0:privat 1:public
1091 * @return int the new templateid
1093 function feedback_create_template($courseid, $name, $ispublic = 0) {
1094 global $DB;
1096 $templ = new stdClass();
1097 $templ->course = ($ispublic ? 0 : $courseid);
1098 $templ->name = $name;
1099 $templ->ispublic = $ispublic;
1101 $templid = $DB->insert_record('feedback_template', $templ);
1102 return $DB->get_record('feedback_template', array('id'=>$templid));
1106 * creates new template items.
1107 * all items will be copied and the attribute feedback will be set to 0
1108 * and the attribute template will be set to the new templateid
1110 * @global object
1111 * @uses CONTEXT_MODULE
1112 * @uses CONTEXT_COURSE
1113 * @param object $feedback
1114 * @param string $name the name of template shown in the templatelist
1115 * @param int $ispublic 0:privat 1:public
1116 * @return boolean
1118 function feedback_save_as_template($feedback, $name, $ispublic = 0) {
1119 global $DB;
1120 $fs = get_file_storage();
1122 if (!$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
1123 return false;
1126 if (!$newtempl = feedback_create_template($feedback->course, $name, $ispublic)) {
1127 return false;
1130 //files in the template_item are in the context of the current course or
1131 //if the template is public the files are in the system context
1132 //files in the feedback_item are in the feedback_context of the feedback
1133 if ($ispublic) {
1134 $s_context = context_system::instance();
1135 } else {
1136 $s_context = context_course::instance($newtempl->course);
1138 $cm = get_coursemodule_from_instance('feedback', $feedback->id);
1139 $f_context = context_module::instance($cm->id);
1141 //create items of this new template
1142 //depend items we are storing temporary in an mapping list array(new id => dependitem)
1143 //we also store a mapping of all items array(oldid => newid)
1144 $dependitemsmap = array();
1145 $itembackup = array();
1146 foreach ($feedbackitems as $item) {
1148 $t_item = clone($item);
1150 unset($t_item->id);
1151 $t_item->feedback = 0;
1152 $t_item->template = $newtempl->id;
1153 $t_item->id = $DB->insert_record('feedback_item', $t_item);
1154 //copy all included files to the feedback_template filearea
1155 $itemfiles = $fs->get_area_files($f_context->id,
1156 'mod_feedback',
1157 'item',
1158 $item->id,
1159 "id",
1160 false);
1161 if ($itemfiles) {
1162 foreach ($itemfiles as $ifile) {
1163 $file_record = new stdClass();
1164 $file_record->contextid = $s_context->id;
1165 $file_record->component = 'mod_feedback';
1166 $file_record->filearea = 'template';
1167 $file_record->itemid = $t_item->id;
1168 $fs->create_file_from_storedfile($file_record, $ifile);
1172 $itembackup[$item->id] = $t_item->id;
1173 if ($t_item->dependitem) {
1174 $dependitemsmap[$t_item->id] = $t_item->dependitem;
1179 //remapping the dependency
1180 foreach ($dependitemsmap as $key => $dependitem) {
1181 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
1182 $newitem->dependitem = $itembackup[$newitem->dependitem];
1183 $DB->update_record('feedback_item', $newitem);
1186 return true;
1190 * deletes all feedback_items related to the given template id
1192 * @global object
1193 * @uses CONTEXT_COURSE
1194 * @param object $template the template
1195 * @return void
1197 function feedback_delete_template($template) {
1198 global $DB;
1200 //deleting the files from the item is done by feedback_delete_item
1201 if ($t_items = $DB->get_records("feedback_item", array("template"=>$template->id))) {
1202 foreach ($t_items as $t_item) {
1203 feedback_delete_item($t_item->id, false, $template);
1206 $DB->delete_records("feedback_template", array("id"=>$template->id));
1210 * creates new feedback_item-records from template.
1211 * if $deleteold is set true so the existing items of the given feedback will be deleted
1212 * if $deleteold is set false so the new items will be appanded to the old items
1214 * @global object
1215 * @uses CONTEXT_COURSE
1216 * @uses CONTEXT_MODULE
1217 * @param object $feedback
1218 * @param int $templateid
1219 * @param boolean $deleteold
1221 function feedback_items_from_template($feedback, $templateid, $deleteold = false) {
1222 global $DB, $CFG;
1224 require_once($CFG->libdir.'/completionlib.php');
1226 $fs = get_file_storage();
1228 if (!$template = $DB->get_record('feedback_template', array('id'=>$templateid))) {
1229 return false;
1231 //get all templateitems
1232 if (!$templitems = $DB->get_records('feedback_item', array('template'=>$templateid))) {
1233 return false;
1236 //files in the template_item are in the context of the current course
1237 //files in the feedback_item are in the feedback_context of the feedback
1238 if ($template->ispublic) {
1239 $s_context = context_system::instance();
1240 } else {
1241 $s_context = context_course::instance($feedback->course);
1243 $course = $DB->get_record('course', array('id'=>$feedback->course));
1244 $cm = get_coursemodule_from_instance('feedback', $feedback->id);
1245 $f_context = context_module::instance($cm->id);
1247 //if deleteold then delete all old items before
1248 //get all items
1249 if ($deleteold) {
1250 if ($feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
1251 //delete all items of this feedback
1252 foreach ($feedbackitems as $item) {
1253 feedback_delete_item($item->id, false);
1255 //delete tracking-data
1256 $DB->delete_records('feedback_tracking', array('feedback'=>$feedback->id));
1258 $params = array('feedback'=>$feedback->id);
1259 if ($completeds = $DB->get_records('feedback_completed', $params)) {
1260 $completion = new completion_info($course);
1261 foreach ($completeds as $completed) {
1262 // Update completion state
1263 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
1264 $completion->update_state($cm, COMPLETION_INCOMPLETE, $completed->userid);
1266 $DB->delete_records('feedback_completed', array('id'=>$completed->id));
1269 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedback->id));
1271 $positionoffset = 0;
1272 } else {
1273 //if the old items are kept the new items will be appended
1274 //therefor the new position has an offset
1275 $positionoffset = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
1278 //create items of this new template
1279 //depend items we are storing temporary in an mapping list array(new id => dependitem)
1280 //we also store a mapping of all items array(oldid => newid)
1281 $dependitemsmap = array();
1282 $itembackup = array();
1283 foreach ($templitems as $t_item) {
1284 $item = clone($t_item);
1285 unset($item->id);
1286 $item->feedback = $feedback->id;
1287 $item->template = 0;
1288 $item->position = $item->position + $positionoffset;
1290 $item->id = $DB->insert_record('feedback_item', $item);
1292 //moving the files to the new item
1293 $templatefiles = $fs->get_area_files($s_context->id,
1294 'mod_feedback',
1295 'template',
1296 $t_item->id,
1297 "id",
1298 false);
1299 if ($templatefiles) {
1300 foreach ($templatefiles as $tfile) {
1301 $file_record = new stdClass();
1302 $file_record->contextid = $f_context->id;
1303 $file_record->component = 'mod_feedback';
1304 $file_record->filearea = 'item';
1305 $file_record->itemid = $item->id;
1306 $fs->create_file_from_storedfile($file_record, $tfile);
1310 $itembackup[$t_item->id] = $item->id;
1311 if ($item->dependitem) {
1312 $dependitemsmap[$item->id] = $item->dependitem;
1316 //remapping the dependency
1317 foreach ($dependitemsmap as $key => $dependitem) {
1318 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
1319 $newitem->dependitem = $itembackup[$newitem->dependitem];
1320 $DB->update_record('feedback_item', $newitem);
1325 * get the list of available templates.
1326 * if the $onlyown param is set true so only templates from own course will be served
1327 * this is important for droping templates
1329 * @global object
1330 * @param object $course
1331 * @param string $onlyownorpublic
1332 * @return array the template recordsets
1334 function feedback_get_template_list($course, $onlyownorpublic = '') {
1335 global $DB, $CFG;
1337 switch($onlyownorpublic) {
1338 case '':
1339 $templates = $DB->get_records_select('feedback_template',
1340 'course = ? OR ispublic = 1',
1341 array($course->id),
1342 'name');
1343 break;
1344 case 'own':
1345 $templates = $DB->get_records('feedback_template',
1346 array('course'=>$course->id),
1347 'name');
1348 break;
1349 case 'public':
1350 $templates = $DB->get_records('feedback_template', array('ispublic'=>1), 'name');
1351 break;
1353 return $templates;
1356 ////////////////////////////////////////////////
1357 //Handling der Items
1358 ////////////////////////////////////////////////
1359 ////////////////////////////////////////////////
1362 * load the lib.php from item-plugin-dir and returns the instance of the itemclass
1364 * @global object
1365 * @param object $item
1366 * @return object the instanz of itemclass
1368 function feedback_get_item_class($typ) {
1369 global $CFG;
1371 //get the class of item-typ
1372 $itemclass = 'feedback_item_'.$typ;
1373 //get the instance of item-class
1374 if (!class_exists($itemclass)) {
1375 require_once($CFG->dirroot.'/mod/feedback/item/'.$typ.'/lib.php');
1377 return new $itemclass();
1381 * load the available item plugins from given subdirectory of $CFG->dirroot
1382 * the default is "mod/feedback/item"
1384 * @global object
1385 * @param string $dir the subdir
1386 * @return array pluginnames as string
1388 function feedback_load_feedback_items($dir = 'mod/feedback/item') {
1389 global $CFG;
1390 $names = get_list_of_plugins($dir);
1391 $ret_names = array();
1393 foreach ($names as $name) {
1394 require_once($CFG->dirroot.'/'.$dir.'/'.$name.'/lib.php');
1395 if (class_exists('feedback_item_'.$name)) {
1396 $ret_names[] = $name;
1399 return $ret_names;
1403 * load the available item plugins to use as dropdown-options
1405 * @global object
1406 * @return array pluginnames as string
1408 function feedback_load_feedback_items_options() {
1409 global $CFG;
1411 $feedback_options = array("pagebreak" => get_string('add_pagebreak', 'feedback'));
1413 if (!$feedback_names = feedback_load_feedback_items('mod/feedback/item')) {
1414 return array();
1417 foreach ($feedback_names as $fn) {
1418 $feedback_options[$fn] = get_string($fn, 'feedback');
1420 asort($feedback_options);
1421 $feedback_options = array_merge( array(' ' => get_string('select')), $feedback_options );
1422 return $feedback_options;
1426 * load the available items for the depend item dropdown list shown in the edit_item form
1428 * @global object
1429 * @param object $feedback
1430 * @param object $item the item of the edit_item form
1431 * @return array all items except the item $item, labels and pagebreaks
1433 function feedback_get_depend_candidates_for_item($feedback, $item) {
1434 global $DB;
1435 //all items for dependitem
1436 $where = "feedback = ? AND typ != 'pagebreak' AND hasvalue = 1";
1437 $params = array($feedback->id);
1438 if (isset($item->id) AND $item->id) {
1439 $where .= ' AND id != ?';
1440 $params[] = $item->id;
1442 $dependitems = array(0 => get_string('choose'));
1443 $feedbackitems = $DB->get_records_select_menu('feedback_item',
1444 $where,
1445 $params,
1446 'position',
1447 'id, label');
1449 if (!$feedbackitems) {
1450 return $dependitems;
1452 //adding the choose-option
1453 foreach ($feedbackitems as $key => $val) {
1454 $dependitems[$key] = $val;
1456 return $dependitems;
1460 * creates a new item-record
1462 * @global object
1463 * @param object $data the data from edit_item_form
1464 * @return int the new itemid
1466 function feedback_create_item($data) {
1467 global $DB;
1469 $item = new stdClass();
1470 $item->feedback = $data->feedbackid;
1472 $item->template=0;
1473 if (isset($data->templateid)) {
1474 $item->template = intval($data->templateid);
1477 $itemname = trim($data->itemname);
1478 $item->name = ($itemname ? $data->itemname : get_string('no_itemname', 'feedback'));
1480 if (!empty($data->itemlabel)) {
1481 $item->label = trim($data->itemlabel);
1482 } else {
1483 $item->label = get_string('no_itemlabel', 'feedback');
1486 $itemobj = feedback_get_item_class($data->typ);
1487 $item->presentation = ''; //the date comes from postupdate() of the itemobj
1489 $item->hasvalue = $itemobj->get_hasvalue();
1491 $item->typ = $data->typ;
1492 $item->position = $data->position;
1494 $item->required=0;
1495 if (!empty($data->required)) {
1496 $item->required = $data->required;
1499 $item->id = $DB->insert_record('feedback_item', $item);
1501 //move all itemdata to the data
1502 $data->id = $item->id;
1503 $data->feedback = $item->feedback;
1504 $data->name = $item->name;
1505 $data->label = $item->label;
1506 $data->required = $item->required;
1507 return $itemobj->postupdate($data);
1511 * save the changes of a given item.
1513 * @global object
1514 * @param object $item
1515 * @return boolean
1517 function feedback_update_item($item) {
1518 global $DB;
1519 return $DB->update_record("feedback_item", $item);
1523 * deletes an item and also deletes all related values
1525 * @global object
1526 * @uses CONTEXT_MODULE
1527 * @param int $itemid
1528 * @param boolean $renumber should the kept items renumbered Yes/No
1529 * @param object $template if the template is given so the items are bound to it
1530 * @return void
1532 function feedback_delete_item($itemid, $renumber = true, $template = false) {
1533 global $DB;
1535 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
1537 //deleting the files from the item
1538 $fs = get_file_storage();
1540 if ($template) {
1541 if ($template->ispublic) {
1542 $context = context_system::instance();
1543 } else {
1544 $context = context_course::instance($template->course);
1546 $templatefiles = $fs->get_area_files($context->id,
1547 'mod_feedback',
1548 'template',
1549 $item->id,
1550 "id",
1551 false);
1553 if ($templatefiles) {
1554 $fs->delete_area_files($context->id, 'mod_feedback', 'template', $item->id);
1556 } else {
1557 if (!$cm = get_coursemodule_from_instance('feedback', $item->feedback)) {
1558 return false;
1560 $context = context_module::instance($cm->id);
1562 $itemfiles = $fs->get_area_files($context->id,
1563 'mod_feedback',
1564 'item',
1565 $item->id,
1566 "id", false);
1568 if ($itemfiles) {
1569 $fs->delete_area_files($context->id, 'mod_feedback', 'item', $item->id);
1573 $DB->delete_records("feedback_value", array("item"=>$itemid));
1574 $DB->delete_records("feedback_valuetmp", array("item"=>$itemid));
1576 //remove all depends
1577 $DB->set_field('feedback_item', 'dependvalue', '', array('dependitem'=>$itemid));
1578 $DB->set_field('feedback_item', 'dependitem', 0, array('dependitem'=>$itemid));
1580 $DB->delete_records("feedback_item", array("id"=>$itemid));
1581 if ($renumber) {
1582 feedback_renumber_items($item->feedback);
1587 * deletes all items of the given feedbackid
1589 * @global object
1590 * @param int $feedbackid
1591 * @return void
1593 function feedback_delete_all_items($feedbackid) {
1594 global $DB, $CFG;
1595 require_once($CFG->libdir.'/completionlib.php');
1597 if (!$feedback = $DB->get_record('feedback', array('id'=>$feedbackid))) {
1598 return false;
1601 if (!$cm = get_coursemodule_from_instance('feedback', $feedback->id)) {
1602 return false;
1605 if (!$course = $DB->get_record('course', array('id'=>$feedback->course))) {
1606 return false;
1609 if (!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid))) {
1610 return;
1612 foreach ($items as $item) {
1613 feedback_delete_item($item->id, false);
1615 if ($completeds = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id))) {
1616 $completion = new completion_info($course);
1617 foreach ($completeds as $completed) {
1618 // Update completion state
1619 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
1620 $completion->update_state($cm, COMPLETION_INCOMPLETE, $completed->userid);
1622 $DB->delete_records('feedback_completed', array('id'=>$completed->id));
1626 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedbackid));
1631 * this function toggled the item-attribute required (yes/no)
1633 * @global object
1634 * @param object $item
1635 * @return boolean
1637 function feedback_switch_item_required($item) {
1638 global $DB, $CFG;
1640 $itemobj = feedback_get_item_class($item->typ);
1642 if ($itemobj->can_switch_require()) {
1643 $new_require_val = (int)!(bool)$item->required;
1644 $params = array('id'=>$item->id);
1645 $DB->set_field('feedback_item', 'required', $new_require_val, $params);
1647 return true;
1651 * renumbers all items of the given feedbackid
1653 * @global object
1654 * @param int $feedbackid
1655 * @return void
1657 function feedback_renumber_items($feedbackid) {
1658 global $DB;
1660 $items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position');
1661 $pos = 1;
1662 if ($items) {
1663 foreach ($items as $item) {
1664 $DB->set_field('feedback_item', 'position', $pos, array('id'=>$item->id));
1665 $pos++;
1671 * this decreases the position of the given item
1673 * @global object
1674 * @param object $item
1675 * @return bool
1677 function feedback_moveup_item($item) {
1678 global $DB;
1680 if ($item->position == 1) {
1681 return true;
1684 $params = array('feedback'=>$item->feedback);
1685 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
1686 return false;
1689 $itembefore = null;
1690 foreach ($items as $i) {
1691 if ($i->id == $item->id) {
1692 if (is_null($itembefore)) {
1693 return true;
1695 $itembefore->position = $item->position;
1696 $item->position--;
1697 feedback_update_item($itembefore);
1698 feedback_update_item($item);
1699 feedback_renumber_items($item->feedback);
1700 return true;
1702 $itembefore = $i;
1704 return false;
1708 * this increased the position of the given item
1710 * @global object
1711 * @param object $item
1712 * @return bool
1714 function feedback_movedown_item($item) {
1715 global $DB;
1717 $params = array('feedback'=>$item->feedback);
1718 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
1719 return false;
1722 $movedownitem = null;
1723 foreach ($items as $i) {
1724 if (!is_null($movedownitem) AND $movedownitem->id == $item->id) {
1725 $movedownitem->position = $i->position;
1726 $i->position--;
1727 feedback_update_item($movedownitem);
1728 feedback_update_item($i);
1729 feedback_renumber_items($item->feedback);
1730 return true;
1732 $movedownitem = $i;
1734 return false;
1738 * here the position of the given item will be set to the value in $pos
1740 * @global object
1741 * @param object $moveitem
1742 * @param int $pos
1743 * @return boolean
1745 function feedback_move_item($moveitem, $pos) {
1746 global $DB;
1748 $params = array('feedback'=>$moveitem->feedback);
1749 if (!$allitems = $DB->get_records('feedback_item', $params, 'position')) {
1750 return false;
1752 if (is_array($allitems)) {
1753 $index = 1;
1754 foreach ($allitems as $item) {
1755 if ($index == $pos) {
1756 $index++;
1758 if ($item->id == $moveitem->id) {
1759 $moveitem->position = $pos;
1760 feedback_update_item($moveitem);
1761 continue;
1763 $item->position = $index;
1764 feedback_update_item($item);
1765 $index++;
1767 return true;
1769 return false;
1773 * prints the given item as a preview.
1774 * each item-class has an own print_item_preview function implemented.
1776 * @global object
1777 * @param object $item the item what we want to print out
1778 * @return void
1780 function feedback_print_item_preview($item) {
1781 global $CFG;
1782 if ($item->typ == 'pagebreak') {
1783 return;
1785 //get the instance of the item-class
1786 $itemobj = feedback_get_item_class($item->typ);
1787 $itemobj->print_item_preview($item);
1791 * prints the given item in the completion form.
1792 * each item-class has an own print_item_complete function implemented.
1794 * @param object $item the item what we want to print out
1795 * @param mixed $value the value
1796 * @param boolean $highlightrequire if this set true and the value are false on completing so the item will be highlighted
1797 * @return void
1799 function feedback_print_item_complete($item, $value = false, $highlightrequire = false) {
1800 global $CFG;
1801 if ($item->typ == 'pagebreak') {
1802 return;
1805 //get the instance of the item-class
1806 $itemobj = feedback_get_item_class($item->typ);
1807 $itemobj->print_item_complete($item, $value, $highlightrequire);
1811 * prints the given item in the show entries page.
1812 * each item-class has an own print_item_show_value function implemented.
1814 * @param object $item the item what we want to print out
1815 * @param mixed $value
1816 * @return void
1818 function feedback_print_item_show_value($item, $value = false) {
1819 global $CFG;
1820 if ($item->typ == 'pagebreak') {
1821 return;
1824 //get the instance of the item-class
1825 $itemobj = feedback_get_item_class($item->typ);
1826 $itemobj->print_item_show_value($item, $value);
1830 * if the user completes a feedback and there is a pagebreak so the values are saved temporary.
1831 * the values are not saved permanently until the user click on save button
1833 * @global object
1834 * @param object $feedbackcompleted
1835 * @return object temporary saved completed-record
1837 function feedback_set_tmp_values($feedbackcompleted) {
1838 global $DB;
1840 //first we create a completedtmp
1841 $tmpcpl = new stdClass();
1842 foreach ($feedbackcompleted as $key => $value) {
1843 $tmpcpl->{$key} = $value;
1845 unset($tmpcpl->id);
1846 $tmpcpl->timemodified = time();
1847 $tmpcpl->id = $DB->insert_record('feedback_completedtmp', $tmpcpl);
1848 //get all values of original-completed
1849 if (!$values = $DB->get_records('feedback_value', array('completed'=>$feedbackcompleted->id))) {
1850 return;
1852 foreach ($values as $value) {
1853 unset($value->id);
1854 $value->completed = $tmpcpl->id;
1855 $DB->insert_record('feedback_valuetmp', $value);
1857 return $tmpcpl;
1861 * this saves the temporary saved values permanently
1863 * @global object
1864 * @param object $feedbackcompletedtmp the temporary completed
1865 * @param object $feedbackcompleted the target completed
1866 * @param int $userid
1867 * @return int the id of the completed
1869 function feedback_save_tmp_values($feedbackcompletedtmp, $feedbackcompleted, $userid) {
1870 global $DB;
1872 $tmpcplid = $feedbackcompletedtmp->id;
1873 if ($feedbackcompleted) {
1874 //first drop all existing values
1875 $DB->delete_records('feedback_value', array('completed'=>$feedbackcompleted->id));
1876 //update the current completed
1877 $feedbackcompleted->timemodified = time();
1878 $DB->update_record('feedback_completed', $feedbackcompleted);
1879 } else {
1880 $feedbackcompleted = clone($feedbackcompletedtmp);
1881 $feedbackcompleted->id = '';
1882 $feedbackcompleted->userid = $userid;
1883 $feedbackcompleted->timemodified = time();
1884 $feedbackcompleted->id = $DB->insert_record('feedback_completed', $feedbackcompleted);
1887 //save all the new values from feedback_valuetmp
1888 //get all values of tmp-completed
1889 $params = array('completed'=>$feedbackcompletedtmp->id);
1890 if (!$values = $DB->get_records('feedback_valuetmp', $params)) {
1891 return false;
1893 foreach ($values as $value) {
1894 //check if there are depend items
1895 $item = $DB->get_record('feedback_item', array('id'=>$value->item));
1896 if ($item->dependitem > 0) {
1897 $check = feedback_compare_item_value($tmpcplid,
1898 $item->dependitem,
1899 $item->dependvalue,
1900 true);
1901 } else {
1902 $check = true;
1904 if ($check) {
1905 unset($value->id);
1906 $value->completed = $feedbackcompleted->id;
1907 $DB->insert_record('feedback_value', $value);
1910 //drop all the tmpvalues
1911 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1912 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
1914 // Trigger event for the delete action we performed.
1915 $cm = get_coursemodule_from_instance('feedback', $feedbackcompleted->feedback);
1916 $event = \mod_feedback\event\response_submitted::create(array(
1917 'relateduserid' => $userid,
1918 'objectid' => $feedbackcompleted->id,
1919 'context' => context_module::instance($cm->id),
1920 'anonymous' => ($feedbackcompleted->anonymous_response == FEEDBACK_ANONYMOUS_YES),
1921 'other' => array(
1922 'cmid' => $cm->id,
1923 'instanceid' => $feedbackcompleted->feedback,
1924 'anonymous' => $feedbackcompleted->anonymous_response // Deprecated.
1928 $event->add_record_snapshot('feedback_completed', $feedbackcompleted);
1930 $event->trigger();
1931 return $feedbackcompleted->id;
1936 * deletes the given temporary completed and all related temporary values
1938 * @global object
1939 * @param int $tmpcplid
1940 * @return void
1942 function feedback_delete_completedtmp($tmpcplid) {
1943 global $DB;
1945 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1946 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
1949 ////////////////////////////////////////////////
1950 ////////////////////////////////////////////////
1951 ////////////////////////////////////////////////
1952 //functions to handle the pagebreaks
1953 ////////////////////////////////////////////////
1956 * this creates a pagebreak.
1957 * a pagebreak is a special kind of item
1959 * @global object
1960 * @param int $feedbackid
1961 * @return mixed false if there already is a pagebreak on last position or the id of the pagebreak-item
1963 function feedback_create_pagebreak($feedbackid) {
1964 global $DB;
1966 //check if there already is a pagebreak on the last position
1967 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
1968 if ($lastposition == feedback_get_last_break_position($feedbackid)) {
1969 return false;
1972 $item = new stdClass();
1973 $item->feedback = $feedbackid;
1975 $item->template=0;
1977 $item->name = '';
1979 $item->presentation = '';
1980 $item->hasvalue = 0;
1982 $item->typ = 'pagebreak';
1983 $item->position = $lastposition + 1;
1985 $item->required=0;
1987 return $DB->insert_record('feedback_item', $item);
1991 * get all positions of pagebreaks in the given feedback
1993 * @global object
1994 * @param int $feedbackid
1995 * @return array all ordered pagebreak positions
1997 function feedback_get_all_break_positions($feedbackid) {
1998 global $DB;
2000 $params = array('typ'=>'pagebreak', 'feedback'=>$feedbackid);
2001 $allbreaks = $DB->get_records_menu('feedback_item', $params, 'position', 'id, position');
2002 if (!$allbreaks) {
2003 return false;
2005 return array_values($allbreaks);
2009 * get the position of the last pagebreak
2011 * @param int $feedbackid
2012 * @return int the position of the last pagebreak
2014 function feedback_get_last_break_position($feedbackid) {
2015 if (!$allbreaks = feedback_get_all_break_positions($feedbackid)) {
2016 return false;
2018 return $allbreaks[count($allbreaks) - 1];
2022 * this returns the position where the user can continue the completing.
2024 * @global object
2025 * @global object
2026 * @global object
2027 * @param int $feedbackid
2028 * @param int $courseid
2029 * @param string $guestid this id will be saved temporary and is unique
2030 * @return int the position to continue
2032 function feedback_get_page_to_continue($feedbackid, $courseid = false, $guestid = false) {
2033 global $CFG, $USER, $DB;
2035 //is there any break?
2037 if (!$allbreaks = feedback_get_all_break_positions($feedbackid)) {
2038 return false;
2041 $params = array();
2042 if ($courseid) {
2043 $courseselect = "AND fv.course_id = :courseid";
2044 $params['courseid'] = $courseid;
2045 } else {
2046 $courseselect = '';
2049 if ($guestid) {
2050 $userselect = "AND fc.guestid = :guestid";
2051 $usergroup = "GROUP BY fc.guestid";
2052 $params['guestid'] = $guestid;
2053 } else {
2054 $userselect = "AND fc.userid = :userid";
2055 $usergroup = "GROUP BY fc.userid";
2056 $params['userid'] = $USER->id;
2059 $sql = "SELECT MAX(fi.position)
2060 FROM {feedback_completedtmp} fc, {feedback_valuetmp} fv, {feedback_item} fi
2061 WHERE fc.id = fv.completed
2062 $userselect
2063 AND fc.feedback = :feedbackid
2064 $courseselect
2065 AND fi.id = fv.item
2066 $usergroup";
2067 $params['feedbackid'] = $feedbackid;
2069 $lastpos = $DB->get_field_sql($sql, $params);
2071 //the index of found pagebreak is the searched pagenumber
2072 foreach ($allbreaks as $pagenr => $br) {
2073 if ($lastpos < $br) {
2074 return $pagenr;
2077 return count($allbreaks);
2080 ////////////////////////////////////////////////
2081 ////////////////////////////////////////////////
2082 ////////////////////////////////////////////////
2083 //functions to handle the values
2084 ////////////////////////////////////////////////
2087 * cleans the userinput while submitting the form.
2089 * @param mixed $value
2090 * @return mixed
2092 function feedback_clean_input_value($item, $value) {
2093 $itemobj = feedback_get_item_class($item->typ);
2094 return $itemobj->clean_input_value($value);
2098 * this saves the values of an completed.
2099 * if the param $tmp is set true so the values are saved temporary in table feedback_valuetmp.
2100 * if there is already a completed and the userid is set so the values are updated.
2101 * on all other things new value records will be created.
2103 * @global object
2104 * @param int $userid
2105 * @param boolean $tmp
2106 * @return mixed false on error or the completeid
2108 function feedback_save_values($usrid, $tmp = false) {
2109 global $DB;
2111 $completedid = optional_param('completedid', 0, PARAM_INT);
2113 $tmpstr = $tmp ? 'tmp' : '';
2114 $time = time();
2115 $timemodified = mktime(0, 0, 0, date('m', $time), date('d', $time), date('Y', $time));
2117 if ($usrid == 0) {
2118 return feedback_create_values($usrid, $timemodified, $tmp);
2120 $completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid));
2121 if (!$completed) {
2122 return feedback_create_values($usrid, $timemodified, $tmp);
2123 } else {
2124 $completed->timemodified = $timemodified;
2125 return feedback_update_values($completed, $tmp);
2130 * this saves the values from anonymous user such as guest on the main-site
2132 * @global object
2133 * @param string $guestid the unique guestidentifier
2134 * @return mixed false on error or the completeid
2136 function feedback_save_guest_values($guestid) {
2137 global $DB;
2139 $completedid = optional_param('completedid', false, PARAM_INT);
2141 $timemodified = time();
2142 if (!$completed = $DB->get_record('feedback_completedtmp', array('id'=>$completedid))) {
2143 return feedback_create_values(0, $timemodified, true, $guestid);
2144 } else {
2145 $completed->timemodified = $timemodified;
2146 return feedback_update_values($completed, true);
2151 * get the value from the given item related to the given completed.
2152 * the value can come as temporary or as permanently value. the deciding is done by $tmp
2154 * @global object
2155 * @param int $completeid
2156 * @param int $itemid
2157 * @param boolean $tmp
2158 * @return mixed the value, the type depends on plugin-definition
2160 function feedback_get_item_value($completedid, $itemid, $tmp = false) {
2161 global $DB;
2163 $tmpstr = $tmp ? 'tmp' : '';
2164 $params = array('completed'=>$completedid, 'item'=>$itemid);
2165 return $DB->get_field('feedback_value'.$tmpstr, 'value', $params);
2169 * compares the value of the itemid related to the completedid with the dependvalue.
2170 * this is used if a depend item is set.
2171 * the value can come as temporary or as permanently value. the deciding is done by $tmp.
2173 * @global object
2174 * @global object
2175 * @param int $completeid
2176 * @param int $itemid
2177 * @param mixed $dependvalue
2178 * @param boolean $tmp
2179 * @return bool
2181 function feedback_compare_item_value($completedid, $itemid, $dependvalue, $tmp = false) {
2182 global $DB, $CFG;
2184 $dbvalue = feedback_get_item_value($completedid, $itemid, $tmp);
2186 //get the class of the given item-typ
2187 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
2189 //get the instance of the item-class
2190 $itemobj = feedback_get_item_class($item->typ);
2191 return $itemobj->compare_value($item, $dbvalue, $dependvalue); //true or false
2195 * this function checks the correctness of values.
2196 * the rules for this are implemented in the class of each item.
2197 * it can be the required attribute or the value self e.g. numeric.
2198 * the params first/lastitem are given to determine the visible range between pagebreaks.
2200 * @global object
2201 * @param int $firstitem the position of firstitem for checking
2202 * @param int $lastitem the position of lastitem for checking
2203 * @return boolean
2205 function feedback_check_values($firstitem, $lastitem) {
2206 global $DB, $CFG;
2208 $feedbackid = optional_param('feedbackid', 0, PARAM_INT);
2210 //get all items between the first- and lastitem
2211 $select = "feedback = ?
2212 AND position >= ?
2213 AND position <= ?
2214 AND hasvalue = 1";
2215 $params = array($feedbackid, $firstitem, $lastitem);
2216 if (!$feedbackitems = $DB->get_records_select('feedback_item', $select, $params)) {
2217 //if no values are given so no values can be wrong ;-)
2218 return true;
2221 foreach ($feedbackitems as $item) {
2222 //get the instance of the item-class
2223 $itemobj = feedback_get_item_class($item->typ);
2225 //the name of the input field of the completeform is given in a special form:
2226 //<item-typ>_<item-id> eg. numeric_234
2227 //this is the key to get the value for the correct item
2228 $formvalname = $item->typ . '_' . $item->id;
2230 if ($itemobj->value_is_array()) {
2231 //get the raw value here. It is cleaned after that by the object itself
2232 $value = optional_param_array($formvalname, null, PARAM_RAW);
2233 } else {
2234 //get the raw value here. It is cleaned after that by the object itself
2235 $value = optional_param($formvalname, null, PARAM_RAW);
2237 $value = $itemobj->clean_input_value($value);
2239 // If the item is not visible due to its dependency so it shouldn't be required.
2240 // Many thanks to Pau Ferrer Ocaña.
2241 if ($item->dependitem > 0 AND $item->required == 1) {
2242 $comparevalue = false;
2243 if ($feedbackcompletedtmp = feedback_get_current_completed($item->feedback, true)) {
2244 $comparevalue = feedback_compare_item_value($feedbackcompletedtmp->id,
2245 $item->dependitem,
2246 $item->dependvalue,
2247 true);
2250 if (!$comparevalue) {
2251 $item->required = 0; // Override the required property.
2255 //check if the value is set
2256 if (is_null($value) AND $item->required == 1) {
2257 return false;
2260 //now we let check the value by the item-class
2261 if (!$itemobj->check_value($value, $item)) {
2262 return false;
2265 //if no wrong values so we can return true
2266 return true;
2270 * this function create a complete-record and the related value-records.
2271 * depending on the $tmp (true/false) the values are saved temporary or permanently
2273 * @global object
2274 * @param int $userid
2275 * @param int $timemodified
2276 * @param boolean $tmp
2277 * @param string $guestid a unique identifier to save temporary data
2278 * @return mixed false on error or the completedid
2280 function feedback_create_values($usrid, $timemodified, $tmp = false, $guestid = false) {
2281 global $DB;
2283 $feedbackid = optional_param('feedbackid', false, PARAM_INT);
2284 $anonymous_response = optional_param('anonymous_response', false, PARAM_INT);
2285 $courseid = optional_param('courseid', false, PARAM_INT);
2287 $tmpstr = $tmp ? 'tmp' : '';
2288 //first we create a new completed record
2289 $completed = new stdClass();
2290 $completed->feedback = $feedbackid;
2291 $completed->userid = $usrid;
2292 $completed->guestid = $guestid;
2293 $completed->timemodified = $timemodified;
2294 $completed->anonymous_response = $anonymous_response;
2296 $completedid = $DB->insert_record('feedback_completed'.$tmpstr, $completed);
2298 $completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid));
2300 //the keys are in the form like abc_xxx
2301 //with explode we make an array with(abc, xxx) and (abc=typ und xxx=itemnr)
2303 //get the items of the feedback
2304 if (!$allitems = $DB->get_records('feedback_item', array('feedback'=>$completed->feedback))) {
2305 return false;
2307 foreach ($allitems as $item) {
2308 if (!$item->hasvalue) {
2309 continue;
2311 //get the class of item-typ
2312 $itemobj = feedback_get_item_class($item->typ);
2314 $keyname = $item->typ.'_'.$item->id;
2316 if ($itemobj->value_is_array()) {
2317 $itemvalue = optional_param_array($keyname, null, $itemobj->value_type());
2318 } else {
2319 $itemvalue = optional_param($keyname, null, $itemobj->value_type());
2322 if (is_null($itemvalue)) {
2323 continue;
2326 $value = new stdClass();
2327 $value->item = $item->id;
2328 $value->completed = $completed->id;
2329 $value->course_id = $courseid;
2331 //the kind of values can be absolutely different
2332 //so we run create_value directly by the item-class
2333 $value->value = $itemobj->create_value($itemvalue);
2334 $DB->insert_record('feedback_value'.$tmpstr, $value);
2336 return $completed->id;
2340 * this function updates a complete-record and the related value-records.
2341 * depending on the $tmp (true/false) the values are saved temporary or permanently
2343 * @global object
2344 * @param object $completed
2345 * @param boolean $tmp
2346 * @return int the completedid
2348 function feedback_update_values($completed, $tmp = false) {
2349 global $DB;
2351 $courseid = optional_param('courseid', false, PARAM_INT);
2352 $tmpstr = $tmp ? 'tmp' : '';
2354 $DB->update_record('feedback_completed'.$tmpstr, $completed);
2355 //get the values of this completed
2356 $values = $DB->get_records('feedback_value'.$tmpstr, array('completed'=>$completed->id));
2358 //get the items of the feedback
2359 if (!$allitems = $DB->get_records('feedback_item', array('feedback'=>$completed->feedback))) {
2360 return false;
2362 foreach ($allitems as $item) {
2363 if (!$item->hasvalue) {
2364 continue;
2366 //get the class of item-typ
2367 $itemobj = feedback_get_item_class($item->typ);
2369 $keyname = $item->typ.'_'.$item->id;
2371 if ($itemobj->value_is_array()) {
2372 $itemvalue = optional_param_array($keyname, null, $itemobj->value_type());
2373 } else {
2374 $itemvalue = optional_param($keyname, null, $itemobj->value_type());
2377 //is the itemvalue set (could be a subset of items because pagebreak)?
2378 if (is_null($itemvalue)) {
2379 continue;
2382 $newvalue = new stdClass();
2383 $newvalue->item = $item->id;
2384 $newvalue->completed = $completed->id;
2385 $newvalue->course_id = $courseid;
2387 //the kind of values can be absolutely different
2388 //so we run create_value directly by the item-class
2389 $newvalue->value = $itemobj->create_value($itemvalue);
2391 //check, if we have to create or update the value
2392 $exist = false;
2393 foreach ($values as $value) {
2394 if ($value->item == $newvalue->item) {
2395 $newvalue->id = $value->id;
2396 $exist = true;
2397 break;
2400 if ($exist) {
2401 $DB->update_record('feedback_value'.$tmpstr, $newvalue);
2402 } else {
2403 $DB->insert_record('feedback_value'.$tmpstr, $newvalue);
2407 return $completed->id;
2411 * get the values of an item depending on the given groupid.
2412 * if the feedback is anonymous so the values are shuffled
2414 * @global object
2415 * @global object
2416 * @param object $item
2417 * @param int $groupid
2418 * @param int $courseid
2419 * @param bool $ignore_empty if this is set true so empty values are not delivered
2420 * @return array the value-records
2422 function feedback_get_group_values($item,
2423 $groupid = false,
2424 $courseid = false,
2425 $ignore_empty = false) {
2427 global $CFG, $DB;
2429 //if the groupid is given?
2430 if (intval($groupid) > 0) {
2431 $params = array();
2432 if ($ignore_empty) {
2433 $value = $DB->sql_compare_text('fbv.value');
2434 $ignore_empty_select = "AND $value != :emptyvalue AND $value != :zerovalue";
2435 $params += array('emptyvalue' => '', 'zerovalue' => '0');
2436 } else {
2437 $ignore_empty_select = "";
2440 $query = 'SELECT fbv . *
2441 FROM {feedback_value} fbv, {feedback_completed} fbc, {groups_members} gm
2442 WHERE fbv.item = :itemid
2443 AND fbv.completed = fbc.id
2444 AND fbc.userid = gm.userid
2445 '.$ignore_empty_select.'
2446 AND gm.groupid = :groupid
2447 ORDER BY fbc.timemodified';
2448 $params += array('itemid' => $item->id, 'groupid' => $groupid);
2449 $values = $DB->get_records_sql($query, $params);
2451 } else {
2452 $params = array();
2453 if ($ignore_empty) {
2454 $value = $DB->sql_compare_text('value');
2455 $ignore_empty_select = "AND $value != :emptyvalue AND $value != :zerovalue";
2456 $params += array('emptyvalue' => '', 'zerovalue' => '0');
2457 } else {
2458 $ignore_empty_select = "";
2461 if ($courseid) {
2462 $select = "item = :itemid AND course_id = :courseid ".$ignore_empty_select;
2463 $params += array('itemid' => $item->id, 'courseid' => $courseid);
2464 $values = $DB->get_records_select('feedback_value', $select, $params);
2465 } else {
2466 $select = "item = :itemid ".$ignore_empty_select;
2467 $params += array('itemid' => $item->id);
2468 $values = $DB->get_records_select('feedback_value', $select, $params);
2471 $params = array('id'=>$item->feedback);
2472 if ($DB->get_field('feedback', 'anonymous', $params) == FEEDBACK_ANONYMOUS_YES) {
2473 if (is_array($values)) {
2474 shuffle($values);
2477 return $values;
2481 * check for multiple_submit = false.
2482 * if the feedback is global so the courseid must be given
2484 * @global object
2485 * @global object
2486 * @param int $feedbackid
2487 * @param int $courseid
2488 * @return boolean true if the feedback already is submitted otherwise false
2490 function feedback_is_already_submitted($feedbackid, $courseid = false) {
2491 global $USER, $DB;
2493 $params = array('userid'=>$USER->id, 'feedback'=>$feedbackid);
2494 if (!$trackings = $DB->get_records_menu('feedback_tracking', $params, '', 'id, completed')) {
2495 return false;
2498 if ($courseid) {
2499 $select = 'completed IN ('.implode(',', $trackings).') AND course_id = ?';
2500 if (!$values = $DB->get_records_select('feedback_value', $select, array($courseid))) {
2501 return false;
2505 return true;
2509 * if the completion of a feedback will be continued eg.
2510 * by pagebreak or by multiple submit so the complete must be found.
2511 * if the param $tmp is set true so all things are related to temporary completeds
2513 * @global object
2514 * @global object
2515 * @global object
2516 * @param int $feedbackid
2517 * @param boolean $tmp
2518 * @param int $courseid
2519 * @param string $guestid
2520 * @return int the id of the found completed
2522 function feedback_get_current_completed($feedbackid,
2523 $tmp = false,
2524 $courseid = false,
2525 $guestid = false) {
2527 global $USER, $CFG, $DB;
2529 $tmpstr = $tmp ? 'tmp' : '';
2531 if (!$courseid) {
2532 if ($guestid) {
2533 $params = array('feedback'=>$feedbackid, 'guestid'=>$guestid);
2534 return $DB->get_record('feedback_completed'.$tmpstr, $params);
2535 } else {
2536 $params = array('feedback'=>$feedbackid, 'userid'=>$USER->id);
2537 return $DB->get_record('feedback_completed'.$tmpstr, $params);
2541 $params = array();
2543 if ($guestid) {
2544 $userselect = "AND fc.guestid = :guestid";
2545 $params['guestid'] = $guestid;
2546 } else {
2547 $userselect = "AND fc.userid = :userid";
2548 $params['userid'] = $USER->id;
2550 //if courseid is set the feedback is global.
2551 //there can be more than one completed on one feedback
2552 $sql = "SELECT DISTINCT fc.*
2553 FROM {feedback_value{$tmpstr}} fv, {feedback_completed{$tmpstr}} fc
2554 WHERE fv.course_id = :courseid
2555 AND fv.completed = fc.id
2556 $userselect
2557 AND fc.feedback = :feedbackid";
2558 $params['courseid'] = intval($courseid);
2559 $params['feedbackid'] = $feedbackid;
2561 if (!$sqlresult = $DB->get_records_sql($sql, $params)) {
2562 return false;
2564 foreach ($sqlresult as $r) {
2565 return $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$r->id));
2570 * get the completeds depending on the given groupid.
2572 * @global object
2573 * @global object
2574 * @param object $feedback
2575 * @param int $groupid
2576 * @param int $courseid
2577 * @return mixed array of found completeds otherwise false
2579 function feedback_get_completeds_group($feedback, $groupid = false, $courseid = false) {
2580 global $CFG, $DB;
2582 if (intval($groupid) > 0) {
2583 $query = "SELECT fbc.*
2584 FROM {feedback_completed} fbc, {groups_members} gm
2585 WHERE fbc.feedback = ?
2586 AND gm.groupid = ?
2587 AND fbc.userid = gm.userid";
2588 if ($values = $DB->get_records_sql($query, array($feedback->id, $groupid))) {
2589 return $values;
2590 } else {
2591 return false;
2593 } else {
2594 if ($courseid) {
2595 $query = "SELECT DISTINCT fbc.*
2596 FROM {feedback_completed} fbc, {feedback_value} fbv
2597 WHERE fbc.id = fbv.completed
2598 AND fbc.feedback = ?
2599 AND fbv.course_id = ?
2600 ORDER BY random_response";
2601 if ($values = $DB->get_records_sql($query, array($feedback->id, $courseid))) {
2602 return $values;
2603 } else {
2604 return false;
2606 } else {
2607 if ($values = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id))) {
2608 return $values;
2609 } else {
2610 return false;
2617 * get the count of completeds depending on the given groupid.
2619 * @global object
2620 * @global object
2621 * @param object $feedback
2622 * @param int $groupid
2623 * @param int $courseid
2624 * @return mixed count of completeds or false
2626 function feedback_get_completeds_group_count($feedback, $groupid = false, $courseid = false) {
2627 global $CFG, $DB;
2629 if ($courseid > 0 AND !$groupid <= 0) {
2630 $sql = "SELECT id, COUNT(item) AS ci
2631 FROM {feedback_value}
2632 WHERE course_id = ?
2633 GROUP BY item ORDER BY ci DESC";
2634 if ($foundrecs = $DB->get_records_sql($sql, array($courseid))) {
2635 $foundrecs = array_values($foundrecs);
2636 return $foundrecs[0]->ci;
2638 return false;
2640 if ($values = feedback_get_completeds_group($feedback, $groupid)) {
2641 return count($values);
2642 } else {
2643 return false;
2648 * deletes all completed-recordsets from a feedback.
2649 * all related data such as values also will be deleted
2651 * @global object
2652 * @param int $feedbackid
2653 * @return void
2655 function feedback_delete_all_completeds($feedbackid) {
2656 global $DB;
2658 if (!$completeds = $DB->get_records('feedback_completed', array('feedback'=>$feedbackid))) {
2659 return;
2661 foreach ($completeds as $completed) {
2662 feedback_delete_completed($completed->id);
2667 * deletes a completed given by completedid.
2668 * all related data such values or tracking data also will be deleted
2670 * @global object
2671 * @param int $completedid
2672 * @return boolean
2674 function feedback_delete_completed($completedid) {
2675 global $DB, $CFG;
2676 require_once($CFG->libdir.'/completionlib.php');
2678 if (!$completed = $DB->get_record('feedback_completed', array('id'=>$completedid))) {
2679 return false;
2682 if (!$feedback = $DB->get_record('feedback', array('id'=>$completed->feedback))) {
2683 return false;
2686 if (!$course = $DB->get_record('course', array('id'=>$feedback->course))) {
2687 return false;
2690 if (!$cm = get_coursemodule_from_instance('feedback', $feedback->id)) {
2691 return false;
2694 //first we delete all related values
2695 $DB->delete_records('feedback_value', array('completed'=>$completed->id));
2697 //now we delete all tracking data
2698 $params = array('completed'=>$completed->id, 'feedback'=>$completed->feedback);
2699 if ($tracking = $DB->get_record('feedback_tracking', $params)) {
2700 $DB->delete_records('feedback_tracking', array('completed'=>$completed->id));
2703 // Update completion state
2704 $completion = new completion_info($course);
2705 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
2706 $completion->update_state($cm, COMPLETION_INCOMPLETE, $completed->userid);
2708 // Last we delete the completed-record.
2709 $return = $DB->delete_records('feedback_completed', array('id'=>$completed->id));
2711 // Trigger event for the delete action we performed.
2712 $event = \mod_feedback\event\response_deleted::create(array(
2713 'relateduserid' => $completed->userid,
2714 'objectid' => $completedid,
2715 'courseid' => $course->id,
2716 'context' => context_module::instance($cm->id),
2717 'anonymous' => ($completed->anonymous_response == FEEDBACK_ANONYMOUS_YES),
2718 'other' => array(
2719 'cmid' => $cm->id,
2720 'instanceid' => $feedback->id,
2721 'anonymous' => $completed->anonymous_response) // Deprecated.
2724 $event->add_record_snapshot('feedback_completed', $completed);
2725 $event->add_record_snapshot('course', $course);
2726 $event->add_record_snapshot('feedback', $feedback);
2728 $event->trigger();
2730 return $return;
2733 ////////////////////////////////////////////////
2734 ////////////////////////////////////////////////
2735 ////////////////////////////////////////////////
2736 //functions to handle sitecourse mapping
2737 ////////////////////////////////////////////////
2740 * checks if the course and the feedback is in the table feedback_sitecourse_map.
2742 * @global object
2743 * @param int $feedbackid
2744 * @param int $courseid
2745 * @return int the count of records
2747 function feedback_is_course_in_sitecourse_map($feedbackid, $courseid) {
2748 global $DB;
2749 $params = array('feedbackid'=>$feedbackid, 'courseid'=>$courseid);
2750 return $DB->count_records('feedback_sitecourse_map', $params);
2754 * checks if the feedback is in the table feedback_sitecourse_map.
2756 * @global object
2757 * @param int $feedbackid
2758 * @return boolean
2760 function feedback_is_feedback_in_sitecourse_map($feedbackid) {
2761 global $DB;
2762 return $DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$feedbackid));
2766 * gets the feedbacks from table feedback_sitecourse_map.
2767 * this is used to show the global feedbacks on the feedback block
2768 * all feedbacks with the following criteria will be selected:<br />
2770 * 1) all feedbacks which id are listed together with the courseid in sitecoursemap and<br />
2771 * 2) all feedbacks which not are listed in sitecoursemap
2773 * @global object
2774 * @param int $courseid
2775 * @return array the feedback-records
2777 function feedback_get_feedbacks_from_sitecourse_map($courseid) {
2778 global $DB;
2780 //first get all feedbacks listed in sitecourse_map with named courseid
2781 $sql = "SELECT f.id AS id,
2782 cm.id AS cmid,
2783 f.name AS name,
2784 f.timeopen AS timeopen,
2785 f.timeclose AS timeclose
2786 FROM {feedback} f, {course_modules} cm, {feedback_sitecourse_map} sm, {modules} m
2787 WHERE f.id = cm.instance
2788 AND f.course = '".SITEID."'
2789 AND m.id = cm.module
2790 AND m.name = 'feedback'
2791 AND sm.courseid = ?
2792 AND sm.feedbackid = f.id";
2794 if (!$feedbacks1 = $DB->get_records_sql($sql, array($courseid))) {
2795 $feedbacks1 = array();
2798 //second get all feedbacks not listed in sitecourse_map
2799 $feedbacks2 = array();
2800 $sql = "SELECT f.id AS id,
2801 cm.id AS cmid,
2802 f.name AS name,
2803 f.timeopen AS timeopen,
2804 f.timeclose AS timeclose
2805 FROM {feedback} f, {course_modules} cm, {modules} m
2806 WHERE f.id = cm.instance
2807 AND f.course = '".SITEID."'
2808 AND m.id = cm.module
2809 AND m.name = 'feedback'";
2810 if (!$allfeedbacks = $DB->get_records_sql($sql)) {
2811 $allfeedbacks = array();
2813 foreach ($allfeedbacks as $a) {
2814 if (!$DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$a->id))) {
2815 $feedbacks2[] = $a;
2819 return array_merge($feedbacks1, $feedbacks2);
2824 * gets the courses from table feedback_sitecourse_map.
2826 * @global object
2827 * @param int $feedbackid
2828 * @return array the course-records
2830 function feedback_get_courses_from_sitecourse_map($feedbackid) {
2831 global $DB;
2833 $sql = "SELECT f.id, f.courseid, c.fullname, c.shortname
2834 FROM {feedback_sitecourse_map} f, {course} c
2835 WHERE c.id = f.courseid
2836 AND f.feedbackid = ?
2837 ORDER BY c.fullname";
2839 return $DB->get_records_sql($sql, array($feedbackid));
2844 * removes non existing courses or feedbacks from sitecourse_map.
2845 * it shouldn't be called all too often
2846 * a good place for it could be the mapcourse.php or unmapcourse.php
2848 * @global object
2849 * @return void
2851 function feedback_clean_up_sitecourse_map() {
2852 global $DB;
2854 $maps = $DB->get_records('feedback_sitecourse_map');
2855 foreach ($maps as $map) {
2856 if (!$DB->get_record('course', array('id'=>$map->courseid))) {
2857 $params = array('courseid'=>$map->courseid, 'feedbackid'=>$map->feedbackid);
2858 $DB->delete_records('feedback_sitecourse_map', $params);
2859 continue;
2861 if (!$DB->get_record('feedback', array('id'=>$map->feedbackid))) {
2862 $params = array('courseid'=>$map->courseid, 'feedbackid'=>$map->feedbackid);
2863 $DB->delete_records('feedback_sitecourse_map', $params);
2864 continue;
2870 ////////////////////////////////////////////////
2871 ////////////////////////////////////////////////
2872 ////////////////////////////////////////////////
2873 //not relatable functions
2874 ////////////////////////////////////////////////
2877 * prints the option items of a selection-input item (dropdownlist).
2878 * @param int $startval the first value of the list
2879 * @param int $endval the last value of the list
2880 * @param int $selectval which item should be selected
2881 * @param int $interval the stepsize from the first to the last value
2882 * @return void
2884 function feedback_print_numeric_option_list($startval, $endval, $selectval = '', $interval = 1) {
2885 for ($i = $startval; $i <= $endval; $i += $interval) {
2886 if ($selectval == ($i)) {
2887 $selected = 'selected="selected"';
2888 } else {
2889 $selected = '';
2891 echo '<option '.$selected.'>'.$i.'</option>';
2896 * sends an email to the teachers of the course where the given feedback is placed.
2898 * @global object
2899 * @global object
2900 * @uses FEEDBACK_ANONYMOUS_NO
2901 * @uses FORMAT_PLAIN
2902 * @param object $cm the coursemodule-record
2903 * @param object $feedback
2904 * @param object $course
2905 * @param int $userid
2906 * @return void
2908 function feedback_send_email($cm, $feedback, $course, $userid) {
2909 global $CFG, $DB;
2911 if ($feedback->email_notification == 0) { // No need to do anything
2912 return;
2915 $user = $DB->get_record('user', array('id'=>$userid));
2917 if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
2918 $groupmode = $cm->groupmode;
2919 } else {
2920 $groupmode = $course->groupmode;
2923 if ($groupmode == SEPARATEGROUPS) {
2924 $groups = $DB->get_records_sql_menu("SELECT g.name, g.id
2925 FROM {groups} g, {groups_members} m
2926 WHERE g.courseid = ?
2927 AND g.id = m.groupid
2928 AND m.userid = ?
2929 ORDER BY name ASC", array($course->id, $userid));
2930 $groups = array_values($groups);
2932 $teachers = feedback_get_receivemail_users($cm->id, $groups);
2933 } else {
2934 $teachers = feedback_get_receivemail_users($cm->id);
2937 if ($teachers) {
2939 $strfeedbacks = get_string('modulenameplural', 'feedback');
2940 $strfeedback = get_string('modulename', 'feedback');
2941 $strcompleted = get_string('completed', 'feedback');
2943 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
2944 $printusername = fullname($user);
2945 } else {
2946 $printusername = get_string('anonymous_user', 'feedback');
2949 foreach ($teachers as $teacher) {
2950 $info = new stdClass();
2951 $info->username = $printusername;
2952 $info->feedback = format_string($feedback->name, true);
2953 $info->url = $CFG->wwwroot.'/mod/feedback/show_entries.php?'.
2954 'id='.$cm->id.'&'.
2955 'userid='.$userid.'&'.
2956 'do_show=showentries';
2958 $postsubject = $strcompleted.': '.$info->username.' -> '.$feedback->name;
2959 $posttext = feedback_send_email_text($info, $course);
2961 if ($teacher->mailformat == 1) {
2962 $posthtml = feedback_send_email_html($info, $course, $cm);
2963 } else {
2964 $posthtml = '';
2967 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
2968 $eventdata = new stdClass();
2969 $eventdata->name = 'submission';
2970 $eventdata->component = 'mod_feedback';
2971 $eventdata->userfrom = $user;
2972 $eventdata->userto = $teacher;
2973 $eventdata->subject = $postsubject;
2974 $eventdata->fullmessage = $posttext;
2975 $eventdata->fullmessageformat = FORMAT_PLAIN;
2976 $eventdata->fullmessagehtml = $posthtml;
2977 $eventdata->smallmessage = '';
2978 message_send($eventdata);
2979 } else {
2980 $eventdata = new stdClass();
2981 $eventdata->name = 'submission';
2982 $eventdata->component = 'mod_feedback';
2983 $eventdata->userfrom = $teacher;
2984 $eventdata->userto = $teacher;
2985 $eventdata->subject = $postsubject;
2986 $eventdata->fullmessage = $posttext;
2987 $eventdata->fullmessageformat = FORMAT_PLAIN;
2988 $eventdata->fullmessagehtml = $posthtml;
2989 $eventdata->smallmessage = '';
2990 message_send($eventdata);
2997 * sends an email to the teachers of the course where the given feedback is placed.
2999 * @global object
3000 * @uses FORMAT_PLAIN
3001 * @param object $cm the coursemodule-record
3002 * @param object $feedback
3003 * @param object $course
3004 * @return void
3006 function feedback_send_email_anonym($cm, $feedback, $course) {
3007 global $CFG;
3009 if ($feedback->email_notification == 0) { // No need to do anything
3010 return;
3013 $teachers = feedback_get_receivemail_users($cm->id);
3015 if ($teachers) {
3017 $strfeedbacks = get_string('modulenameplural', 'feedback');
3018 $strfeedback = get_string('modulename', 'feedback');
3019 $strcompleted = get_string('completed', 'feedback');
3020 $printusername = get_string('anonymous_user', 'feedback');
3022 foreach ($teachers as $teacher) {
3023 $info = new stdClass();
3024 $info->username = $printusername;
3025 $info->feedback = format_string($feedback->name, true);
3026 $info->url = $CFG->wwwroot.'/mod/feedback/show_entries_anonym.php?id='.$cm->id;
3028 $postsubject = $strcompleted.': '.$info->username.' -> '.$feedback->name;
3029 $posttext = feedback_send_email_text($info, $course);
3031 if ($teacher->mailformat == 1) {
3032 $posthtml = feedback_send_email_html($info, $course, $cm);
3033 } else {
3034 $posthtml = '';
3037 $eventdata = new stdClass();
3038 $eventdata->name = 'submission';
3039 $eventdata->component = 'mod_feedback';
3040 $eventdata->userfrom = $teacher;
3041 $eventdata->userto = $teacher;
3042 $eventdata->subject = $postsubject;
3043 $eventdata->fullmessage = $posttext;
3044 $eventdata->fullmessageformat = FORMAT_PLAIN;
3045 $eventdata->fullmessagehtml = $posthtml;
3046 $eventdata->smallmessage = '';
3047 message_send($eventdata);
3053 * send the text-part of the email
3055 * @param object $info includes some infos about the feedback you want to send
3056 * @param object $course
3057 * @return string the text you want to post
3059 function feedback_send_email_text($info, $course) {
3060 $coursecontext = context_course::instance($course->id);
3061 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
3062 $posttext = $courseshortname.' -> '.get_string('modulenameplural', 'feedback').' -> '.
3063 $info->feedback."\n";
3064 $posttext .= '---------------------------------------------------------------------'."\n";
3065 $posttext .= get_string("emailteachermail", "feedback", $info)."\n";
3066 $posttext .= '---------------------------------------------------------------------'."\n";
3067 return $posttext;
3072 * send the html-part of the email
3074 * @global object
3075 * @param object $info includes some infos about the feedback you want to send
3076 * @param object $course
3077 * @return string the text you want to post
3079 function feedback_send_email_html($info, $course, $cm) {
3080 global $CFG;
3081 $coursecontext = context_course::instance($course->id);
3082 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
3083 $course_url = $CFG->wwwroot.'/course/view.php?id='.$course->id;
3084 $feedback_all_url = $CFG->wwwroot.'/mod/feedback/index.php?id='.$course->id;
3085 $feedback_url = $CFG->wwwroot.'/mod/feedback/view.php?id='.$cm->id;
3087 $posthtml = '<p><font face="sans-serif">'.
3088 '<a href="'.$course_url.'">'.$courseshortname.'</a> ->'.
3089 '<a href="'.$feedback_all_url.'">'.get_string('modulenameplural', 'feedback').'</a> ->'.
3090 '<a href="'.$feedback_url.'">'.$info->feedback.'</a></font></p>';
3091 $posthtml .= '<hr /><font face="sans-serif">';
3092 $posthtml .= '<p>'.get_string('emailteachermailhtml', 'feedback', $info).'</p>';
3093 $posthtml .= '</font><hr />';
3094 return $posthtml;
3098 * @param string $url
3099 * @return string
3101 function feedback_encode_target_url($url) {
3102 if (strpos($url, '?')) {
3103 list($part1, $part2) = explode('?', $url, 2); //maximal 2 parts
3104 return $part1 . '?' . htmlentities($part2);
3105 } else {
3106 return $url;
3111 * Adds module specific settings to the settings block
3113 * @param settings_navigation $settings The settings navigation object
3114 * @param navigation_node $feedbacknode The node to add module settings to
3116 function feedback_extend_settings_navigation(settings_navigation $settings,
3117 navigation_node $feedbacknode) {
3119 global $PAGE, $DB;
3121 if (!$context = context_module::instance($PAGE->cm->id, IGNORE_MISSING)) {
3122 print_error('badcontext');
3125 if (has_capability('mod/feedback:edititems', $context)) {
3126 $questionnode = $feedbacknode->add(get_string('questions', 'feedback'));
3128 $questionnode->add(get_string('edit_items', 'feedback'),
3129 new moodle_url('/mod/feedback/edit.php',
3130 array('id' => $PAGE->cm->id,
3131 'do_show' => 'edit')));
3133 $questionnode->add(get_string('export_questions', 'feedback'),
3134 new moodle_url('/mod/feedback/export.php',
3135 array('id' => $PAGE->cm->id,
3136 'action' => 'exportfile')));
3138 $questionnode->add(get_string('import_questions', 'feedback'),
3139 new moodle_url('/mod/feedback/import.php',
3140 array('id' => $PAGE->cm->id)));
3142 $questionnode->add(get_string('templates', 'feedback'),
3143 new moodle_url('/mod/feedback/edit.php',
3144 array('id' => $PAGE->cm->id,
3145 'do_show' => 'templates')));
3148 if (has_capability('mod/feedback:viewreports', $context)) {
3149 $feedback = $DB->get_record('feedback', array('id'=>$PAGE->cm->instance));
3150 if ($feedback->course == SITEID) {
3151 $feedbacknode->add(get_string('analysis', 'feedback'),
3152 new moodle_url('/mod/feedback/analysis_course.php',
3153 array('id' => $PAGE->cm->id,
3154 'course' => $PAGE->course->id,
3155 'do_show' => 'analysis')));
3156 } else {
3157 $feedbacknode->add(get_string('analysis', 'feedback'),
3158 new moodle_url('/mod/feedback/analysis.php',
3159 array('id' => $PAGE->cm->id,
3160 'course' => $PAGE->course->id,
3161 'do_show' => 'analysis')));
3164 $feedbacknode->add(get_string('show_entries', 'feedback'),
3165 new moodle_url('/mod/feedback/show_entries.php',
3166 array('id' => $PAGE->cm->id,
3167 'do_show' => 'showentries')));
3171 function feedback_init_feedback_session() {
3172 //initialize the feedback-Session - not nice at all!!
3173 global $SESSION;
3174 if (!empty($SESSION)) {
3175 if (!isset($SESSION->feedback) OR !is_object($SESSION->feedback)) {
3176 $SESSION->feedback = new stdClass();
3182 * Return a list of page types
3183 * @param string $pagetype current page type
3184 * @param stdClass $parentcontext Block's parent context
3185 * @param stdClass $currentcontext Current context of block
3187 function feedback_page_type_list($pagetype, $parentcontext, $currentcontext) {
3188 $module_pagetype = array('mod-feedback-*'=>get_string('page-mod-feedback-x', 'feedback'));
3189 return $module_pagetype;
3193 * Move save the items of the given $feedback in the order of $itemlist.
3194 * @param string $itemlist a comma separated list with item ids
3195 * @param stdClass $feedback
3196 * @return bool true if success
3198 function feedback_ajax_saveitemorder($itemlist, $feedback) {
3199 global $DB;
3201 $result = true;
3202 $position = 0;
3203 foreach ($itemlist as $itemid) {
3204 $position++;
3205 $result = $result && $DB->set_field('feedback_item',
3206 'position',
3207 $position,
3208 array('id'=>$itemid, 'feedback'=>$feedback->id));
3210 return $result;