2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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');
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);
44 * Returns all other caps used in module.
48 function feedback_get_extra_capabilities() {
49 return array('moodle/site:accessallgroups');
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) {
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;
79 * this will create a new instance and return the id number
80 * of the new instance.
83 * @param object $feedback the object given by mod_feedback_mod_form
86 function feedback_add_instance($feedback) {
89 $feedback->timemodified
= time();
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',
116 $feedback->page_after_submit_editor
['text']);
118 $feedback->page_after_submitformat
= $feedback->page_after_submit_editor
['format'];
120 $DB->update_record('feedback', $feedback);
126 * this will update a given instance
129 * @param object $feedback the object given by mod_feedback_mod_form
132 function feedback_update_instance($feedback) {
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',
157 $feedback->page_after_submit_editor
['text']);
159 $feedback->page_after_submitformat
= $feedback->page_after_submit_editor
['format'];
161 $DB->update_record('feedback', $feedback);
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
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()) {
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))) {
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
))) {
201 $feedbackid = $feedback->id
;
203 //if the filearea is "item" so we check the permissions like view/complete the feedback
205 //first check whether the user has the complete capability
206 if (has_capability('mod/feedback:complete', $context)) {
210 //now we check whether the user has the view capability
211 if (has_capability('mod/feedback:view', $context)) {
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
) {
227 } else if ($filearea === 'template') { //now we check files in templates
228 if (!$template = $DB->get_record('feedback_template', array('id'=>$templateid))) {
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)) {
237 } else { //on public templates, at least the user has to be logged in
246 if ($context->contextlevel
== CONTEXT_MODULE
) {
247 if ($filearea !== 'item' and $filearea !== 'page_after_submit') {
252 if ($context->contextlevel
== CONTEXT_COURSE ||
$context->contextlevel
== CONTEXT_SYSTEM
) {
253 if ($filearea !== 'template') {
258 $relativepath = implode('/', $args);
259 if ($filearea === 'page_after_submit') {
260 $fullpath = "/{$context->id}/mod_feedback/$filearea/$relativepath";
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()) {
271 // finally send the file
272 send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
278 * this will delete a given instance.
279 * all referenced data also will be deleted
282 * @param int $id the instanceid of feedback
285 function feedback_delete_instance($id) {
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
323 * @param object $course
326 function feedback_delete_course($course) {
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
343 * @param object $feedback
346 function feedback_user_outline($course, $user, $mod, $feedback) {
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
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
365 * @param int $groupid
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) {
377 $course = $DB->get_record('course', array('id'=>$courseid));
380 $modinfo = get_fast_modinfo($course);
382 $cm = $modinfo->cms
[$cmid];
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 ";
393 $sql .= " JOIN {groups_members} gm ON gm.userid=u.id ";
396 $sql .= " WHERE fc.timemodified > ?
398 AND fc.anonymous_response = ?";
399 $sqlargs[] = $timemodified;
400 $sqlargs[] = $cm->instance
;
401 $sqlargs[] = FEEDBACK_ANONYMOUS_NO
;
404 $sql .= " AND u.id = ? ";
405 $sqlargs[] = $userid;
409 $sql .= " AND gm.groupid = ? ";
410 $sqlargs[] = $groupid;
413 if (!$feedbackitems = $DB->get_records_sql($sql, $sqlargs)) {
417 $cm_context = context_module
::instance($cm->id
);
419 if (!has_capability('mod/feedback:view', $cm_context)) {
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
,
435 if (!is_array($usersgroups)) {
438 $usersgroups = array_keys($usersgroups);
439 $intersect = array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid
));
440 if (empty($intersect)) {
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;
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
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));
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>";
496 echo '<div class="title">';
499 echo '<div class="user">';
500 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&course=$courseid\">"
501 ."{$activity->user->fullname}</a> - ".userdate($activity->timestamp
);
504 echo "</td></tr></table>";
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) {
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);
530 // Completion option is not enabled so just 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
543 * @param object $feedback
546 function feedback_user_complete($course, $user, $mod, $feedback) {
553 function feedback_cron () {
560 function feedback_scale_used ($feedbackid, $scaleid) {
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) {
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.
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.
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.
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) {
618 $resetfeedbacks = array();
619 $dropfeedbacks = array();
621 $componentstr = get_string('modulenameplural', 'feedback');
623 //get the relevant entries from $data
624 foreach ($data as $key => $value) {
626 case substr($key, 0, strlen(FEEDBACK_RESETFORM_RESET
)) == FEEDBACK_RESETFORM_RESET
:
628 $templist = explode('_', $key);
629 if (isset($templist[3])) {
630 $resetfeedbacks[] = intval($templist[3]);
634 case substr($key, 0, strlen(FEEDBACK_RESETFORM_DROP
)) == FEEDBACK_RESETFORM_DROP
:
636 $templist = explode('_', $key);
637 if (isset($templist[3])) {
638 $dropfeedbacks[] = intval($templist[3]);
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'),
658 * Called by course/reset.php
661 * @uses FEEDBACK_RESETFORM_RESET
662 * @param object $mform form passed by reference
664 function feedback_reset_course_form_definition(&$mform) {
667 $mform->addElement('header', 'feedbackheader', get_string('modulenameplural', 'feedback'));
669 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$COURSE->id
), 'name')) {
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.
683 * @uses FEEDBACK_RESETFORM_RESET
684 * @param object $course
686 function feedback_reset_course_form_defaults($course) {
690 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id
), 'name')) {
693 foreach ($feedbacks as $feedback) {
694 $return[FEEDBACK_RESETFORM_RESET
.$feedback->id
] = true;
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
707 * @uses FEEDBACK_RESETFORM_RESET
708 * @uses FEEDBACK_RESETFORM_DROP
709 * @param object $course
712 function feedback_reset_course_form($course) {
715 echo get_string('resetting_feedbacks', 'feedback'); echo ':<br />';
716 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id
), 'name')) {
720 foreach ($feedbacks as $feedback) {
722 echo get_string('name', 'feedback').': '.$feedback->name
.'<br />';
723 echo html_writer
::checkbox(FEEDBACK_RESETFORM_RESET
.$feedback->id
,
725 get_string('resetting_data', 'feedback'));
727 echo html_writer
::checkbox(FEEDBACK_RESETFORM_DROP
.$feedback->id
,
729 get_string('drop_feedback', 'feedback'));
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
,
745 * This creates new events given as timeopen and closeopen by $feedback.
748 * @param object $feedback
751 function feedback_set_events($feedback) {
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
;
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
;
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
);
778 $event->timeduration
= 0;
781 calendar_event
::create($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
;
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
808 * @param int $id the id from the coursemodule
811 function feedback_delete_course_module($id) {
814 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
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) {
836 if (isset($context)) {
840 $context = context_module
::instance($cmid);
845 * returns true if the current role is faked by switching role feature
850 function feedback_check_is_switchrole() {
852 if (isset($USER->switchrole
) AND
853 is_array($USER->switchrole
) AND
854 count($USER->switchrole
) > 0) {
862 * count users which have not completed the feedback
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,
877 $pagecount = false) {
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,
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')) {
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);
922 * count users which have not completed the feedback
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);
937 * count users which have completed a feedback
940 * @uses FEEDBACK_ANONYMOUS_NO
942 * @param int $group single groupid
943 * @return int count of userrecords
945 function feedback_count_complete_users($cm, $group = false) {
948 $params = array(FEEDBACK_ANONYMOUS_NO
, $cm->instance
);
953 $fromgroup = ', {groups_members} g';
954 $wheregroup = ' AND g.groupid = ? AND g.userid = c.userid';
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 = ?
962 return $DB->count_records_sql($sql, $params);
967 * get users which have completed a feedback
970 * @uses CONTEXT_MODULE
971 * @uses FEEDBACK_ANONYMOUS_NO
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,
984 array $params = null,
987 $pagecount = false) {
991 $context = context_module
::instance($cm->id
);
993 $params = (array)$params;
995 $params['anon'] = FEEDBACK_ANONYMOUS_NO
;
996 $params['instance'] = $cm->instance
;
1001 $fromgroup = ', {groups_members} g';
1002 $wheregroup = ' AND g.groupid = :group AND g.userid = c.userid';
1003 $params['group'] = $group;
1007 $sortsql = ' ORDER BY '.$sort;
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
1017 AND c.feedback = :instance
1018 '.$wheregroup.$sortsql;
1020 if ($startpage === false OR $pagecount === false) {
1024 return $DB->get_records_sql($sql, $params, $startpage, $pagecount);
1028 * get users which have the viewreports-capability
1030 * @uses CONTEXT_MODULE
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',
1054 * get users which have the receivemail-capability
1056 * @uses CONTEXT_MODULE
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',
1079 ////////////////////////////////////////////////
1080 //functions to handle the templates
1081 ////////////////////////////////////////////////
1082 ////////////////////////////////////////////////
1085 * creates a new template-record.
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) {
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
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
1118 function feedback_save_as_template($feedback, $name, $ispublic = 0) {
1120 $fs = get_file_storage();
1122 if (!$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id
))) {
1126 if (!$newtempl = feedback_create_template($feedback->course
, $name, $ispublic)) {
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
1134 $s_context = context_system
::instance();
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);
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
,
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);
1190 * deletes all feedback_items related to the given template id
1193 * @uses CONTEXT_COURSE
1194 * @param object $template the template
1197 function feedback_delete_template($template) {
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
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) {
1224 require_once($CFG->libdir
.'/completionlib.php');
1226 $fs = get_file_storage();
1228 if (!$template = $DB->get_record('feedback_template', array('id'=>$templateid))) {
1231 //get all templateitems
1232 if (!$templitems = $DB->get_records('feedback_item', array('template'=>$templateid))) {
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();
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
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;
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);
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
,
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
1330 * @param object $course
1331 * @param string $onlyownorpublic
1332 * @return array the template recordsets
1334 function feedback_get_template_list($course, $onlyownorpublic = '') {
1337 switch($onlyownorpublic) {
1339 $templates = $DB->get_records_select('feedback_template',
1340 'course = ? OR ispublic = 1',
1345 $templates = $DB->get_records('feedback_template',
1346 array('course'=>$course->id
),
1350 $templates = $DB->get_records('feedback_template', array('ispublic'=>1), 'name');
1356 ////////////////////////////////////////////////
1357 //Handling der Items
1358 ////////////////////////////////////////////////
1359 ////////////////////////////////////////////////
1362 * load the lib.php from item-plugin-dir and returns the instance of the itemclass
1365 * @param object $item
1366 * @return object the instanz of itemclass
1368 function feedback_get_item_class($typ) {
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"
1385 * @param string $dir the subdir
1386 * @return array pluginnames as string
1388 function feedback_load_feedback_items($dir = 'mod/feedback/item') {
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;
1403 * load the available item plugins to use as dropdown-options
1406 * @return array pluginnames as string
1408 function feedback_load_feedback_items_options() {
1411 $feedback_options = array("pagebreak" => get_string('add_pagebreak', 'feedback'));
1413 if (!$feedback_names = feedback_load_feedback_items('mod/feedback/item')) {
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
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) {
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',
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
1463 * @param object $data the data from edit_item_form
1464 * @return int the new itemid
1466 function feedback_create_item($data) {
1469 $item = new stdClass();
1470 $item->feedback
= $data->feedbackid
;
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
);
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
;
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.
1514 * @param object $item
1517 function feedback_update_item($item) {
1519 return $DB->update_record("feedback_item", $item);
1523 * deletes an item and also deletes all related values
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
1532 function feedback_delete_item($itemid, $renumber = true, $template = false) {
1535 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
1537 //deleting the files from the item
1538 $fs = get_file_storage();
1541 if ($template->ispublic
) {
1542 $context = context_system
::instance();
1544 $context = context_course
::instance($template->course
);
1546 $templatefiles = $fs->get_area_files($context->id
,
1553 if ($templatefiles) {
1554 $fs->delete_area_files($context->id
, 'mod_feedback', 'template', $item->id
);
1557 if (!$cm = get_coursemodule_from_instance('feedback', $item->feedback
)) {
1560 $context = context_module
::instance($cm->id
);
1562 $itemfiles = $fs->get_area_files($context->id
,
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));
1582 feedback_renumber_items($item->feedback
);
1587 * deletes all items of the given feedbackid
1590 * @param int $feedbackid
1593 function feedback_delete_all_items($feedbackid) {
1595 require_once($CFG->libdir
.'/completionlib.php');
1597 if (!$feedback = $DB->get_record('feedback', array('id'=>$feedbackid))) {
1601 if (!$cm = get_coursemodule_from_instance('feedback', $feedback->id
)) {
1605 if (!$course = $DB->get_record('course', array('id'=>$feedback->course
))) {
1609 if (!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid))) {
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)
1634 * @param object $item
1637 function feedback_switch_item_required($item) {
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);
1651 * renumbers all items of the given feedbackid
1654 * @param int $feedbackid
1657 function feedback_renumber_items($feedbackid) {
1660 $items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position');
1663 foreach ($items as $item) {
1664 $DB->set_field('feedback_item', 'position', $pos, array('id'=>$item->id
));
1671 * this decreases the position of the given item
1674 * @param object $item
1677 function feedback_moveup_item($item) {
1680 if ($item->position
== 1) {
1684 $params = array('feedback'=>$item->feedback
);
1685 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
1690 foreach ($items as $i) {
1691 if ($i->id
== $item->id
) {
1692 if (is_null($itembefore)) {
1695 $itembefore->position
= $item->position
;
1697 feedback_update_item($itembefore);
1698 feedback_update_item($item);
1699 feedback_renumber_items($item->feedback
);
1708 * this increased the position of the given item
1711 * @param object $item
1714 function feedback_movedown_item($item) {
1717 $params = array('feedback'=>$item->feedback
);
1718 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
1722 $movedownitem = null;
1723 foreach ($items as $i) {
1724 if (!is_null($movedownitem) AND $movedownitem->id
== $item->id
) {
1725 $movedownitem->position
= $i->position
;
1727 feedback_update_item($movedownitem);
1728 feedback_update_item($i);
1729 feedback_renumber_items($item->feedback
);
1738 * here the position of the given item will be set to the value in $pos
1741 * @param object $moveitem
1745 function feedback_move_item($moveitem, $pos) {
1748 $params = array('feedback'=>$moveitem->feedback
);
1749 if (!$allitems = $DB->get_records('feedback_item', $params, 'position')) {
1752 if (is_array($allitems)) {
1754 foreach ($allitems as $item) {
1755 if ($index == $pos) {
1758 if ($item->id
== $moveitem->id
) {
1759 $moveitem->position
= $pos;
1760 feedback_update_item($moveitem);
1763 $item->position
= $index;
1764 feedback_update_item($item);
1773 * prints the given item as a preview.
1774 * each item-class has an own print_item_preview function implemented.
1777 * @param object $item the item what we want to print out
1780 function feedback_print_item_preview($item) {
1782 if ($item->typ
== 'pagebreak') {
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
1799 function feedback_print_item_complete($item, $value = false, $highlightrequire = false) {
1801 if ($item->typ
== 'pagebreak') {
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
1818 function feedback_print_item_show_value($item, $value = false) {
1820 if ($item->typ
== 'pagebreak') {
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
1834 * @param object $feedbackcompleted
1835 * @return object temporary saved completed-record
1837 function feedback_set_tmp_values($feedbackcompleted) {
1840 //first we create a completedtmp
1841 $tmpcpl = new stdClass();
1842 foreach ($feedbackcompleted as $key => $value) {
1843 $tmpcpl->{$key} = $value;
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
))) {
1852 foreach ($values as $value) {
1854 $value->completed
= $tmpcpl->id
;
1855 $DB->insert_record('feedback_valuetmp', $value);
1861 * this saves the temporary saved values permanently
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) {
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);
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)) {
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,
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
),
1923 'instanceid' => $feedbackcompleted->feedback
,
1924 'anonymous' => $feedbackcompleted->anonymous_response
// Deprecated.
1928 $event->add_record_snapshot('feedback_completed', $feedbackcompleted);
1931 return $feedbackcompleted->id
;
1936 * deletes the given temporary completed and all related temporary values
1939 * @param int $tmpcplid
1942 function feedback_delete_completedtmp($tmpcplid) {
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
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) {
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)) {
1972 $item = new stdClass();
1973 $item->feedback
= $feedbackid;
1979 $item->presentation
= '';
1980 $item->hasvalue
= 0;
1982 $item->typ
= 'pagebreak';
1983 $item->position
= $lastposition +
1;
1987 return $DB->insert_record('feedback_item', $item);
1991 * get all positions of pagebreaks in the given feedback
1994 * @param int $feedbackid
1995 * @return array all ordered pagebreak positions
1997 function feedback_get_all_break_positions($feedbackid) {
2000 $params = array('typ'=>'pagebreak', 'feedback'=>$feedbackid);
2001 $allbreaks = $DB->get_records_menu('feedback_item', $params, 'position', 'id, position');
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)) {
2018 return $allbreaks[count($allbreaks) - 1];
2022 * this returns the position where the user can continue the completing.
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)) {
2043 $courseselect = "AND fv.course_id = :courseid";
2044 $params['courseid'] = $courseid;
2050 $userselect = "AND fc.guestid = :guestid";
2051 $usergroup = "GROUP BY fc.guestid";
2052 $params['guestid'] = $guestid;
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
2063 AND fc.feedback = :feedbackid
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) {
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
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.
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) {
2111 $completedid = optional_param('completedid', 0, PARAM_INT
);
2113 $tmpstr = $tmp ?
'tmp' : '';
2115 $timemodified = mktime(0, 0, 0, date('m', $time), date('d', $time), date('Y', $time));
2118 return feedback_create_values($usrid, $timemodified, $tmp);
2120 $completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid));
2122 return feedback_create_values($usrid, $timemodified, $tmp);
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
2133 * @param string $guestid the unique guestidentifier
2134 * @return mixed false on error or the completeid
2136 function feedback_save_guest_values($guestid) {
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);
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
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) {
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.
2175 * @param int $completeid
2176 * @param int $itemid
2177 * @param mixed $dependvalue
2178 * @param boolean $tmp
2181 function feedback_compare_item_value($completedid, $itemid, $dependvalue, $tmp = false) {
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.
2201 * @param int $firstitem the position of firstitem for checking
2202 * @param int $lastitem the position of lastitem for checking
2205 function feedback_check_values($firstitem, $lastitem) {
2208 $feedbackid = optional_param('feedbackid', 0, PARAM_INT
);
2210 //get all items between the first- and lastitem
2211 $select = "feedback = ?
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 ;-)
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
);
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
,
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) {
2260 //now we let check the value by the item-class
2261 if (!$itemobj->check_value($value, $item)) {
2265 //if no wrong values so we can 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
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) {
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
))) {
2307 foreach ($allitems as $item) {
2308 if (!$item->hasvalue
) {
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());
2319 $itemvalue = optional_param($keyname, null, $itemobj->value_type());
2322 if (is_null($itemvalue)) {
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
2344 * @param object $completed
2345 * @param boolean $tmp
2346 * @return int the completedid
2348 function feedback_update_values($completed, $tmp = false) {
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
))) {
2362 foreach ($allitems as $item) {
2363 if (!$item->hasvalue
) {
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());
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)) {
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
2393 foreach ($values as $value) {
2394 if ($value->item
== $newvalue->item
) {
2395 $newvalue->id
= $value->id
;
2401 $DB->update_record('feedback_value'.$tmpstr, $newvalue);
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
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,
2425 $ignore_empty = false) {
2429 //if the groupid is given?
2430 if (intval($groupid) > 0) {
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');
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);
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');
2458 $ignore_empty_select = "";
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);
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)) {
2481 * check for multiple_submit = false.
2482 * if the feedback is global so the courseid must be given
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) {
2493 $params = array('userid'=>$USER->id
, 'feedback'=>$feedbackid);
2494 if (!$trackings = $DB->get_records_menu('feedback_tracking', $params, '', 'id, completed')) {
2499 $select = 'completed IN ('.implode(',', $trackings).') AND course_id = ?';
2500 if (!$values = $DB->get_records_select('feedback_value', $select, array($courseid))) {
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
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,
2527 global $USER, $CFG, $DB;
2529 $tmpstr = $tmp ?
'tmp' : '';
2533 $params = array('feedback'=>$feedbackid, 'guestid'=>$guestid);
2534 return $DB->get_record('feedback_completed'.$tmpstr, $params);
2536 $params = array('feedback'=>$feedbackid, 'userid'=>$USER->id
);
2537 return $DB->get_record('feedback_completed'.$tmpstr, $params);
2544 $userselect = "AND fc.guestid = :guestid";
2545 $params['guestid'] = $guestid;
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
2557 AND fc.feedback = :feedbackid";
2558 $params['courseid'] = intval($courseid);
2559 $params['feedbackid'] = $feedbackid;
2561 if (!$sqlresult = $DB->get_records_sql($sql, $params)) {
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.
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) {
2582 if (intval($groupid) > 0) {
2583 $query = "SELECT fbc.*
2584 FROM {feedback_completed} fbc, {groups_members} gm
2585 WHERE fbc.feedback = ?
2587 AND fbc.userid = gm.userid";
2588 if ($values = $DB->get_records_sql($query, array($feedback->id
, $groupid))) {
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))) {
2607 if ($values = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id
))) {
2617 * get the count of completeds depending on the given groupid.
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) {
2629 if ($courseid > 0 AND !$groupid <= 0) {
2630 $sql = "SELECT id, COUNT(item) AS ci
2631 FROM {feedback_value}
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
;
2640 if ($values = feedback_get_completeds_group($feedback, $groupid)) {
2641 return count($values);
2648 * deletes all completed-recordsets from a feedback.
2649 * all related data such as values also will be deleted
2652 * @param int $feedbackid
2655 function feedback_delete_all_completeds($feedbackid) {
2658 if (!$completeds = $DB->get_records('feedback_completed', array('feedback'=>$feedbackid))) {
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
2671 * @param int $completedid
2674 function feedback_delete_completed($completedid) {
2676 require_once($CFG->libdir
.'/completionlib.php');
2678 if (!$completed = $DB->get_record('feedback_completed', array('id'=>$completedid))) {
2682 if (!$feedback = $DB->get_record('feedback', array('id'=>$completed->feedback
))) {
2686 if (!$course = $DB->get_record('course', array('id'=>$feedback->course
))) {
2690 if (!$cm = get_coursemodule_from_instance('feedback', $feedback->id
)) {
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
),
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);
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.
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) {
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.
2757 * @param int $feedbackid
2760 function feedback_is_feedback_in_sitecourse_map($feedbackid) {
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
2774 * @param int $courseid
2775 * @return array the feedback-records
2777 function feedback_get_feedbacks_from_sitecourse_map($courseid) {
2780 //first get all feedbacks listed in sitecourse_map with named courseid
2781 $sql = "SELECT f.id AS id,
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'
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,
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
))) {
2819 return array_merge($feedbacks1, $feedbacks2);
2824 * gets the courses from table feedback_sitecourse_map.
2827 * @param int $feedbackid
2828 * @return array the course-records
2830 function feedback_get_courses_from_sitecourse_map($feedbackid) {
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
2851 function feedback_clean_up_sitecourse_map() {
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);
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);
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
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"';
2891 echo '<option '.$selected.'>'.$i.'</option>';
2896 * sends an email to the teachers of the course where the given feedback is placed.
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
2908 function feedback_send_email($cm, $feedback, $course, $userid) {
2911 if ($feedback->email_notification
== 0) { // No need to do anything
2915 $user = $DB->get_record('user', array('id'=>$userid));
2917 if (isset($cm->groupmode
) && empty($course->groupmodeforce
)) {
2918 $groupmode = $cm->groupmode
;
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
2929 ORDER BY name ASC", array($course->id
, $userid));
2930 $groups = array_values($groups);
2932 $teachers = feedback_get_receivemail_users($cm->id
, $groups);
2934 $teachers = feedback_get_receivemail_users($cm->id
);
2939 $strfeedbacks = get_string('modulenameplural', 'feedback');
2940 $strfeedback = get_string('modulename', 'feedback');
2942 if ($feedback->anonymous
== FEEDBACK_ANONYMOUS_NO
) {
2943 $printusername = fullname($user);
2945 $printusername = get_string('anonymous_user', 'feedback');
2948 foreach ($teachers as $teacher) {
2949 $info = new stdClass();
2950 $info->username
= $printusername;
2951 $info->feedback
= format_string($feedback->name
, true);
2952 $info->url
= $CFG->wwwroot
.'/mod/feedback/show_entries.php?'.
2954 'userid='.$userid.'&'.
2955 'do_show=showentries';
2957 $a = array('username' => $info->username
, 'feedbackname' => $feedback->name
);
2959 $postsubject = get_string('feedbackcompleted', 'feedback', $a);
2960 $posttext = feedback_send_email_text($info, $course);
2962 if ($teacher->mailformat
== 1) {
2963 $posthtml = feedback_send_email_html($info, $course, $cm);
2968 if ($feedback->anonymous
== FEEDBACK_ANONYMOUS_NO
) {
2969 $eventdata = new stdClass();
2970 $eventdata->name
= 'submission';
2971 $eventdata->component
= 'mod_feedback';
2972 $eventdata->userfrom
= $user;
2973 $eventdata->userto
= $teacher;
2974 $eventdata->subject
= $postsubject;
2975 $eventdata->fullmessage
= $posttext;
2976 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
2977 $eventdata->fullmessagehtml
= $posthtml;
2978 $eventdata->smallmessage
= '';
2979 message_send($eventdata);
2981 $eventdata = new stdClass();
2982 $eventdata->name
= 'submission';
2983 $eventdata->component
= 'mod_feedback';
2984 $eventdata->userfrom
= $teacher;
2985 $eventdata->userto
= $teacher;
2986 $eventdata->subject
= $postsubject;
2987 $eventdata->fullmessage
= $posttext;
2988 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
2989 $eventdata->fullmessagehtml
= $posthtml;
2990 $eventdata->smallmessage
= '';
2991 message_send($eventdata);
2998 * sends an email to the teachers of the course where the given feedback is placed.
3001 * @uses FORMAT_PLAIN
3002 * @param object $cm the coursemodule-record
3003 * @param object $feedback
3004 * @param object $course
3007 function feedback_send_email_anonym($cm, $feedback, $course) {
3010 if ($feedback->email_notification
== 0) { // No need to do anything
3014 $teachers = feedback_get_receivemail_users($cm->id
);
3018 $strfeedbacks = get_string('modulenameplural', 'feedback');
3019 $strfeedback = get_string('modulename', '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 $a = array('username' => $info->username
, 'feedbackname' => $feedback->name
);
3030 $postsubject = get_string('feedbackcompleted', 'feedback', $a);
3031 $posttext = feedback_send_email_text($info, $course);
3033 if ($teacher->mailformat
== 1) {
3034 $posthtml = feedback_send_email_html($info, $course, $cm);
3039 $eventdata = new stdClass();
3040 $eventdata->name
= 'submission';
3041 $eventdata->component
= 'mod_feedback';
3042 $eventdata->userfrom
= $teacher;
3043 $eventdata->userto
= $teacher;
3044 $eventdata->subject
= $postsubject;
3045 $eventdata->fullmessage
= $posttext;
3046 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
3047 $eventdata->fullmessagehtml
= $posthtml;
3048 $eventdata->smallmessage
= '';
3049 message_send($eventdata);
3055 * send the text-part of the email
3057 * @param object $info includes some infos about the feedback you want to send
3058 * @param object $course
3059 * @return string the text you want to post
3061 function feedback_send_email_text($info, $course) {
3062 $coursecontext = context_course
::instance($course->id
);
3063 $courseshortname = format_string($course->shortname
, true, array('context' => $coursecontext));
3064 $posttext = $courseshortname.' -> '.get_string('modulenameplural', 'feedback').' -> '.
3065 $info->feedback
."\n";
3066 $posttext .= '---------------------------------------------------------------------'."\n";
3067 $posttext .= get_string("emailteachermail", "feedback", $info)."\n";
3068 $posttext .= '---------------------------------------------------------------------'."\n";
3074 * send the html-part of the email
3077 * @param object $info includes some infos about the feedback you want to send
3078 * @param object $course
3079 * @return string the text you want to post
3081 function feedback_send_email_html($info, $course, $cm) {
3083 $coursecontext = context_course
::instance($course->id
);
3084 $courseshortname = format_string($course->shortname
, true, array('context' => $coursecontext));
3085 $course_url = $CFG->wwwroot
.'/course/view.php?id='.$course->id
;
3086 $feedback_all_url = $CFG->wwwroot
.'/mod/feedback/index.php?id='.$course->id
;
3087 $feedback_url = $CFG->wwwroot
.'/mod/feedback/view.php?id='.$cm->id
;
3089 $posthtml = '<p><font face="sans-serif">'.
3090 '<a href="'.$course_url.'">'.$courseshortname.'</a> ->'.
3091 '<a href="'.$feedback_all_url.'">'.get_string('modulenameplural', 'feedback').'</a> ->'.
3092 '<a href="'.$feedback_url.'">'.$info->feedback
.'</a></font></p>';
3093 $posthtml .= '<hr /><font face="sans-serif">';
3094 $posthtml .= '<p>'.get_string('emailteachermailhtml', 'feedback', $info).'</p>';
3095 $posthtml .= '</font><hr />';
3100 * @param string $url
3103 function feedback_encode_target_url($url) {
3104 if (strpos($url, '?')) {
3105 list($part1, $part2) = explode('?', $url, 2); //maximal 2 parts
3106 return $part1 . '?' . htmlentities($part2);
3113 * Adds module specific settings to the settings block
3115 * @param settings_navigation $settings The settings navigation object
3116 * @param navigation_node $feedbacknode The node to add module settings to
3118 function feedback_extend_settings_navigation(settings_navigation
$settings,
3119 navigation_node
$feedbacknode) {
3123 if (!$context = context_module
::instance($PAGE->cm
->id
, IGNORE_MISSING
)) {
3124 print_error('badcontext');
3127 if (has_capability('mod/feedback:edititems', $context)) {
3128 $questionnode = $feedbacknode->add(get_string('questions', 'feedback'));
3130 $questionnode->add(get_string('edit_items', 'feedback'),
3131 new moodle_url('/mod/feedback/edit.php',
3132 array('id' => $PAGE->cm
->id
,
3133 'do_show' => 'edit')));
3135 $questionnode->add(get_string('export_questions', 'feedback'),
3136 new moodle_url('/mod/feedback/export.php',
3137 array('id' => $PAGE->cm
->id
,
3138 'action' => 'exportfile')));
3140 $questionnode->add(get_string('import_questions', 'feedback'),
3141 new moodle_url('/mod/feedback/import.php',
3142 array('id' => $PAGE->cm
->id
)));
3144 $questionnode->add(get_string('templates', 'feedback'),
3145 new moodle_url('/mod/feedback/edit.php',
3146 array('id' => $PAGE->cm
->id
,
3147 'do_show' => 'templates')));
3150 if (has_capability('mod/feedback:viewreports', $context)) {
3151 $feedback = $DB->get_record('feedback', array('id'=>$PAGE->cm
->instance
));
3152 if ($feedback->course
== SITEID
) {
3153 $feedbacknode->add(get_string('analysis', 'feedback'),
3154 new moodle_url('/mod/feedback/analysis_course.php',
3155 array('id' => $PAGE->cm
->id
,
3156 'course' => $PAGE->course
->id
,
3157 'do_show' => 'analysis')));
3159 $feedbacknode->add(get_string('analysis', 'feedback'),
3160 new moodle_url('/mod/feedback/analysis.php',
3161 array('id' => $PAGE->cm
->id
,
3162 'course' => $PAGE->course
->id
,
3163 'do_show' => 'analysis')));
3166 $feedbacknode->add(get_string('show_entries', 'feedback'),
3167 new moodle_url('/mod/feedback/show_entries.php',
3168 array('id' => $PAGE->cm
->id
,
3169 'do_show' => 'showentries')));
3173 function feedback_init_feedback_session() {
3174 //initialize the feedback-Session - not nice at all!!
3176 if (!empty($SESSION)) {
3177 if (!isset($SESSION->feedback
) OR !is_object($SESSION->feedback
)) {
3178 $SESSION->feedback
= new stdClass();
3184 * Return a list of page types
3185 * @param string $pagetype current page type
3186 * @param stdClass $parentcontext Block's parent context
3187 * @param stdClass $currentcontext Current context of block
3189 function feedback_page_type_list($pagetype, $parentcontext, $currentcontext) {
3190 $module_pagetype = array('mod-feedback-*'=>get_string('page-mod-feedback-x', 'feedback'));
3191 return $module_pagetype;
3195 * Move save the items of the given $feedback in the order of $itemlist.
3196 * @param string $itemlist a comma separated list with item ids
3197 * @param stdClass $feedback
3198 * @return bool true if success
3200 function feedback_ajax_saveitemorder($itemlist, $feedback) {
3205 foreach ($itemlist as $itemid) {
3207 $result = $result && $DB->set_field('feedback_item',
3210 array('id'=>$itemid, 'feedback'=>$feedback->id
));