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 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->libdir
.'/formslib.php');
31 define('FEEDBACK_ANONYMOUS_YES', 1);
32 define('FEEDBACK_ANONYMOUS_NO', 2);
33 define('FEEDBACK_MIN_ANONYMOUS_COUNT_IN_GROUP', 2);
34 define('FEEDBACK_DECIMAL', '.');
35 define('FEEDBACK_THOUSAND', ',');
36 define('FEEDBACK_RESETFORM_RESET', 'feedback_reset_data_');
37 define('FEEDBACK_RESETFORM_DROP', 'feedback_drop_feedback_');
38 define('FEEDBACK_MAX_PIX_LENGTH', '400'); //max. Breite des grafischen Balkens in der Auswertung
39 define('FEEDBACK_DEFAULT_PAGE_COUNT', 20);
42 define('FEEDBACK_EVENT_TYPE_OPEN', 'open');
43 define('FEEDBACK_EVENT_TYPE_CLOSE', 'close');
45 require_once(__DIR__
. '/deprecatedlib.php');
48 * @uses FEATURE_GROUPS
49 * @uses FEATURE_GROUPINGS
50 * @uses FEATURE_MOD_INTRO
51 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
52 * @uses FEATURE_GRADE_HAS_GRADE
53 * @uses FEATURE_GRADE_OUTCOMES
54 * @param string $feature FEATURE_xx constant for requested feature
55 * @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose.
57 function feedback_supports($feature) {
59 case FEATURE_GROUPS
: return true;
60 case FEATURE_GROUPINGS
: return true;
61 case FEATURE_MOD_INTRO
: return true;
62 case FEATURE_COMPLETION_TRACKS_VIEWS
: return true;
63 case FEATURE_COMPLETION_HAS_RULES
: return true;
64 case FEATURE_GRADE_HAS_GRADE
: return false;
65 case FEATURE_GRADE_OUTCOMES
: return false;
66 case FEATURE_BACKUP_MOODLE2
: return true;
67 case FEATURE_SHOW_DESCRIPTION
: return true;
68 case FEATURE_MOD_PURPOSE
: return MOD_PURPOSE_COMMUNICATION
;
75 * this will create a new instance and return the id number
76 * of the new instance.
79 * @param object $feedback the object given by mod_feedback_mod_form
82 function feedback_add_instance($feedback) {
85 $feedback->timemodified
= time();
88 if (empty($feedback->site_after_submit
)) {
89 $feedback->site_after_submit
= '';
92 //saving the feedback in db
93 $feedbackid = $DB->insert_record("feedback", $feedback);
95 $feedback->id
= $feedbackid;
97 feedback_set_events($feedback);
99 if (!isset($feedback->coursemodule
)) {
100 $cm = get_coursemodule_from_id('feedback', $feedback->id
);
101 $feedback->coursemodule
= $cm->id
;
103 $context = context_module
::instance($feedback->coursemodule
);
105 if (!empty($feedback->completionexpected
)) {
106 \core_completion\api
::update_completion_date_event($feedback->coursemodule
, 'feedback', $feedback->id
,
107 $feedback->completionexpected
);
110 $editoroptions = feedback_get_editor_options();
112 // process the custom wysiwyg editor in page_after_submit
113 if ($draftitemid = $feedback->page_after_submit_editor
['itemid']) {
114 $feedback->page_after_submit
= file_save_draft_area_files($draftitemid, $context->id
,
115 'mod_feedback', 'page_after_submit',
117 $feedback->page_after_submit_editor
['text']);
119 $feedback->page_after_submitformat
= $feedback->page_after_submit_editor
['format'];
121 $DB->update_record('feedback', $feedback);
127 * this will update a given instance
130 * @param object $feedback the object given by mod_feedback_mod_form
133 function feedback_update_instance($feedback) {
136 $feedback->timemodified
= time();
137 $feedback->id
= $feedback->instance
;
139 if (empty($feedback->site_after_submit
)) {
140 $feedback->site_after_submit
= '';
143 //save the feedback into the db
144 $DB->update_record("feedback", $feedback);
146 //create or update the new events
147 feedback_set_events($feedback);
148 $completionexpected = (!empty($feedback->completionexpected
)) ?
$feedback->completionexpected
: null;
149 \core_completion\api
::update_completion_date_event($feedback->coursemodule
, 'feedback', $feedback->id
, $completionexpected);
151 $context = context_module
::instance($feedback->coursemodule
);
153 $editoroptions = feedback_get_editor_options();
155 // process the custom wysiwyg editor in page_after_submit
156 if ($draftitemid = $feedback->page_after_submit_editor
['itemid']) {
157 $feedback->page_after_submit
= file_save_draft_area_files($draftitemid, $context->id
,
158 'mod_feedback', 'page_after_submit',
160 $feedback->page_after_submit_editor
['text']);
162 $feedback->page_after_submitformat
= $feedback->page_after_submit_editor
['format'];
164 $DB->update_record('feedback', $feedback);
170 * Serves the files included in feedback items like label. Implements needed access control ;-)
172 * There are two situations in general where the files will be sent.
173 * 1) filearea = item, 2) filearea = template
175 * @package mod_feedback
177 * @param stdClass $course course object
178 * @param stdClass $cm course module object
179 * @param stdClass $context context object
180 * @param string $filearea file area
181 * @param array $args extra arguments
182 * @param bool $forcedownload whether or not force download
183 * @param array $options additional options affecting the file serving
184 * @return bool false if file not found, does not return if found - justsend the file
186 function feedback_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
189 if ($filearea === 'item' or $filearea === 'template') {
190 $itemid = (int)array_shift($args);
191 //get the item what includes the file
192 if (!$item = $DB->get_record('feedback_item', array('id'=>$itemid))) {
195 $feedbackid = $item->feedback
;
196 $templateid = $item->template
;
199 if ($filearea === 'page_after_submit' or $filearea === 'item') {
200 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance
))) {
204 $feedbackid = $feedback->id
;
206 //if the filearea is "item" so we check the permissions like view/complete the feedback
208 //first check whether the user has the complete capability
209 if (has_capability('mod/feedback:complete', $context)) {
213 //now we check whether the user has the view capability
214 if (has_capability('mod/feedback:view', $context)) {
218 //if the feedback is on frontpage and anonymous and the fullanonymous is allowed
219 //so the file can be loaded too.
220 if (isset($CFG->feedback_allowfullanonymous
)
221 AND $CFG->feedback_allowfullanonymous
222 AND $course->id
== SITEID
223 AND $feedback->anonymous
== FEEDBACK_ANONYMOUS_YES
) {
230 } else if ($filearea === 'template') { //now we check files in templates
231 if (!$template = $DB->get_record('feedback_template', array('id'=>$templateid))) {
235 //if the file is not public so the capability edititems has to be there
236 if (!$template->ispublic
) {
237 if (!has_capability('mod/feedback:edititems', $context)) {
240 } else { //on public templates, at least the user has to be logged in
249 if ($context->contextlevel
== CONTEXT_MODULE
) {
250 if ($filearea !== 'item' and $filearea !== 'page_after_submit') {
255 if ($context->contextlevel
== CONTEXT_COURSE ||
$context->contextlevel
== CONTEXT_SYSTEM
) {
256 if ($filearea !== 'template') {
261 $relativepath = implode('/', $args);
262 if ($filearea === 'page_after_submit') {
263 $fullpath = "/{$context->id}/mod_feedback/$filearea/$relativepath";
265 $fullpath = "/{$context->id}/mod_feedback/$filearea/{$item->id}/$relativepath";
268 $fs = get_file_storage();
270 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
274 // finally send the file
275 send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
281 * this will delete a given instance.
282 * all referenced data also will be deleted
285 * @param int $id the instanceid of feedback
288 function feedback_delete_instance($id) {
291 //get all referenced items
292 $feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$id));
294 //deleting all referenced items and values
295 if (is_array($feedbackitems)) {
296 foreach ($feedbackitems as $feedbackitem) {
297 $DB->delete_records("feedback_value", array("item"=>$feedbackitem->id
));
298 $DB->delete_records("feedback_valuetmp", array("item"=>$feedbackitem->id
));
300 if ($delitems = $DB->get_records("feedback_item", array("feedback"=>$id))) {
301 foreach ($delitems as $delitem) {
302 feedback_delete_item($delitem->id
, false);
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 * Return a small object with summary information about what a
320 * user has done with a given particular instance of this module
321 * Used for user activity reports.
322 * $return->time = the time they did it
323 * $return->info = a short text description
325 * @param stdClass $course
326 * @param stdClass $user
327 * @param cm_info|stdClass $mod
328 * @param stdClass $feedback
331 function feedback_user_outline($course, $user, $mod, $feedback) {
333 $outline = (object)['info' => '', 'time' => 0];
334 if ($feedback->anonymous
!= FEEDBACK_ANONYMOUS_NO
) {
335 // Do not disclose any user info if feedback is anonymous.
338 $params = array('userid' => $user->id
, 'feedback' => $feedback->id
,
339 'anonymous_response' => FEEDBACK_ANONYMOUS_NO
);
341 $context = context_module
::instance($mod->id
);
342 if ($completed = $DB->get_record('feedback_completed', $params)) {
343 // User has completed feedback.
344 $outline->info
= get_string('completed', 'feedback');
345 $outline->time
= $completed->timemodified
;
346 } else if ($completedtmp = $DB->get_record('feedback_completedtmp', $params)) {
347 // User has started but not completed feedback.
348 $outline->info
= get_string('started', 'feedback');
349 $outline->time
= $completedtmp->timemodified
;
350 } else if (has_capability('mod/feedback:complete', $context, $user)) {
351 // User has not started feedback but has capability to do so.
352 $outline->info
= get_string('not_started', 'feedback');
359 * Returns all users who has completed a specified feedback since a given time
360 * many thanks to Manolescu Dorel, who contributed these two functions
366 * @uses CONTEXT_MODULE
367 * @param array $activities Passed by reference
368 * @param int $index Passed by reference
369 * @param int $timemodified Timestamp
370 * @param int $courseid
373 * @param int $groupid
376 function feedback_get_recent_mod_activity(&$activities, &$index,
377 $timemodified, $courseid,
378 $cmid, $userid="", $groupid="") {
380 global $CFG, $COURSE, $USER, $DB;
382 if ($COURSE->id
== $courseid) {
385 $course = $DB->get_record('course', array('id'=>$courseid));
388 $modinfo = get_fast_modinfo($course);
390 $cm = $modinfo->cms
[$cmid];
394 $userfieldsapi = \core_user\fields
::for_userpic();
395 $userfields = $userfieldsapi->get_sql('u', false, '', 'useridagain', false)->selects
;
396 $sql = " SELECT fk . * , fc . * , $userfields
397 FROM {feedback_completed} fc
398 JOIN {feedback} fk ON fk.id = fc.feedback
399 JOIN {user} u ON u.id = fc.userid ";
402 $sql .= " JOIN {groups_members} gm ON gm.userid=u.id ";
405 $sql .= " WHERE fc.timemodified > ?
407 AND fc.anonymous_response = ?";
408 $sqlargs[] = $timemodified;
409 $sqlargs[] = $cm->instance
;
410 $sqlargs[] = FEEDBACK_ANONYMOUS_NO
;
413 $sql .= " AND u.id = ? ";
414 $sqlargs[] = $userid;
418 $sql .= " AND gm.groupid = ? ";
419 $sqlargs[] = $groupid;
422 if (!$feedbackitems = $DB->get_records_sql($sql, $sqlargs)) {
426 $cm_context = context_module
::instance($cm->id
);
428 if (!has_capability('mod/feedback:view', $cm_context)) {
432 $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
433 $viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context);
434 $groupmode = groups_get_activity_groupmode($cm, $course);
436 $aname = format_string($cm->name
, true);
437 foreach ($feedbackitems as $feedbackitem) {
438 if ($feedbackitem->userid
!= $USER->id
) {
440 if ($groupmode == SEPARATEGROUPS
and !$accessallgroups) {
441 $usersgroups = groups_get_all_groups($course->id
,
442 $feedbackitem->userid
,
444 if (!is_array($usersgroups)) {
447 $usersgroups = array_keys($usersgroups);
448 $intersect = array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid
));
449 if (empty($intersect)) {
455 $tmpactivity = new stdClass();
457 $tmpactivity->type
= 'feedback';
458 $tmpactivity->cmid
= $cm->id
;
459 $tmpactivity->name
= $aname;
460 $tmpactivity->sectionnum
= $cm->sectionnum
;
461 $tmpactivity->timestamp
= $feedbackitem->timemodified
;
463 $tmpactivity->content
= new stdClass();
464 $tmpactivity->content
->feedbackid
= $feedbackitem->id
;
465 $tmpactivity->content
->feedbackuserid
= $feedbackitem->userid
;
467 $tmpactivity->user
= user_picture
::unalias($feedbackitem, null, 'useridagain');
468 $tmpactivity->user
->fullname
= fullname($feedbackitem, $viewfullnames);
470 $activities[$index++
] = $tmpactivity;
477 * Prints all users who has completed a specified feedback since a given time
478 * many thanks to Manolescu Dorel, who contributed these two functions
481 * @param object $activity
482 * @param int $courseid
483 * @param string $detail
484 * @param array $modnames
485 * @return void Output is echo'd
487 function feedback_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
488 global $CFG, $OUTPUT;
490 echo '<table border="0" cellpadding="3" cellspacing="0" class="forum-recent">';
492 echo "<tr><td class=\"userpicture align-top\">";
493 echo $OUTPUT->user_picture($activity->user
, array('courseid'=>$courseid));
497 $modname = $modnames[$activity->type
];
498 echo '<div class="title">';
499 echo $OUTPUT->image_icon('monologo', $modname, $activity->type
);
500 echo "<a href=\"$CFG->wwwroot/mod/feedback/view.php?id={$activity->cmid}\">{$activity->name}</a>";
504 echo '<div class="title">';
507 echo '<div class="user">';
508 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&course=$courseid\">"
509 ."{$activity->user->fullname}</a> - ".userdate($activity->timestamp
);
512 echo "</td></tr></table>";
518 * Print a detailed representation of what a user has done with
519 * a given particular instance of this module, for user activity reports.
521 * @param stdClass $course
522 * @param stdClass $user
523 * @param cm_info|stdClass $mod
524 * @param stdClass $feedback
526 function feedback_user_complete($course, $user, $mod, $feedback) {
528 if ($feedback->anonymous
!= FEEDBACK_ANONYMOUS_NO
) {
529 // Do not disclose any user info if feedback is anonymous.
532 $params = array('userid' => $user->id
, 'feedback' => $feedback->id
,
533 'anonymous_response' => FEEDBACK_ANONYMOUS_NO
);
534 $url = $status = null;
535 $context = context_module
::instance($mod->id
);
536 if ($completed = $DB->get_record('feedback_completed', $params)) {
537 // User has completed feedback.
538 if (has_capability('mod/feedback:viewreports', $context)) {
539 $url = new moodle_url('/mod/feedback/show_entries.php',
540 ['id' => $mod->id
, 'userid' => $user->id
,
541 'showcompleted' => $completed->id
]);
543 $status = get_string('completedon', 'feedback', userdate($completed->timemodified
));
544 } else if ($completedtmp = $DB->get_record('feedback_completedtmp', $params)) {
545 // User has started but not completed feedback.
546 $status = get_string('startedon', 'feedback', userdate($completedtmp->timemodified
));
547 } else if (has_capability('mod/feedback:complete', $context, $user)) {
548 // User has not started feedback but has capability to do so.
549 $status = get_string('not_started', 'feedback');
552 if ($url && $status) {
553 echo html_writer
::link($url, $status);
554 } else if ($status) {
555 echo html_writer
::div($status);
562 function feedback_cron () {
567 * @deprecated since Moodle 3.8
569 function feedback_scale_used() {
570 throw new coding_exception('feedback_scale_used() can not be used anymore. Plugins can implement ' .
571 '<modname>_scale_used_anywhere, all implementations of <modname>_scale_used are now ignored');
575 * Checks if scale is being used by any instance of feedback
577 * This is used to find out if scale used anywhere
578 * @param $scaleid int
579 * @return boolean True if the scale is used by any assignment
581 function feedback_scale_used_anywhere($scaleid) {
586 * List the actions that correspond to a view of this module.
587 * This is used by the participation report.
589 * Note: This is not used by new logging system. Event with
590 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
591 * be considered as view action.
595 function feedback_get_view_actions() {
596 return array('view', 'view all');
600 * List the actions that correspond to a post of this module.
601 * This is used by the participation report.
603 * Note: This is not used by new logging system. Event with
604 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
605 * will be considered as post action.
609 function feedback_get_post_actions() {
610 return array('submit');
614 * This function is used by the reset_course_userdata function in moodlelib.
615 * This function will remove all responses from the specified feedback
616 * and clean up any related data.
620 * @uses FEEDBACK_RESETFORM_RESET
621 * @uses FEEDBACK_RESETFORM_DROP
622 * @param object $data the data submitted from the reset course.
623 * @return array status array
625 function feedback_reset_userdata($data) {
628 $resetfeedbacks = [];
631 $componentstr = get_string('modulenameplural', 'feedback');
633 // Get the relevant entries from $data.
634 foreach ($data as $key => $value) {
636 case substr($key, 0, strlen(FEEDBACK_RESETFORM_RESET
)) == FEEDBACK_RESETFORM_RESET
:
638 $templist = explode('_', $key);
639 if (isset($templist[3])) {
640 $resetfeedbacks[] = intval($templist[3]);
644 case substr($key, 0, strlen(FEEDBACK_RESETFORM_DROP
)) == FEEDBACK_RESETFORM_DROP
:
646 $templist = explode('_', $key);
647 if (isset($templist[3])) {
648 $dropfeedbacks[] = intval($templist[3]);
655 // Reset the selected feedbacks.
656 foreach ($resetfeedbacks as $id) {
657 $feedback = $DB->get_record('feedback', ['id' => $id]);
658 feedback_delete_all_completeds($feedback);
660 'component' => $componentstr.':'.$feedback->name
,
661 'item' => get_string('resetting_data', 'feedback'),
666 // Updating dates - shift may be negative too.
667 if ($data->timeshift
) {
668 // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
670 $shifterror = !shift_course_mod_dates('feedback', ['timeopen', 'timeclose'], $data->timeshift
, $data->courseid
);
672 'component' => $componentstr,
673 'item' => get_string('date'),
674 'error' => $shifterror,
682 * Called by course/reset.php
685 * @uses FEEDBACK_RESETFORM_RESET
686 * @param MoodleQuickForm $mform form passed by reference
688 function feedback_reset_course_form_definition(&$mform) {
691 $mform->addElement('header', 'feedbackheader', get_string('modulenameplural', 'feedback'));
693 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$COURSE->id
), 'name')) {
697 $mform->addElement('static', 'hint', get_string('resetting_delete', 'feedback'));
698 foreach ($feedbacks as $feedback) {
699 $mform->addElement('checkbox', FEEDBACK_RESETFORM_RESET
.$feedback->id
, $feedback->name
);
704 * Course reset form defaults.
707 * @uses FEEDBACK_RESETFORM_RESET
708 * @param object $course
710 function feedback_reset_course_form_defaults($course) {
714 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id
), 'name')) {
717 foreach ($feedbacks as $feedback) {
718 $return[FEEDBACK_RESETFORM_RESET
.$feedback->id
] = true;
724 * Called by course/reset.php and shows the formdata by coursereset.
725 * it prints checkboxes for each feedback available at the given course
726 * there are two checkboxes:
727 * 1) delete userdata and keep the feedback
728 * 2) delete userdata and drop the feedback
731 * @uses FEEDBACK_RESETFORM_RESET
732 * @uses FEEDBACK_RESETFORM_DROP
733 * @param object $course
736 function feedback_reset_course_form($course) {
739 echo get_string('resetting_feedbacks', 'feedback'); echo ':<br />';
740 if (!$feedbacks = $DB->get_records('feedback', ['course' => $course->id
], 'name')) {
744 foreach ($feedbacks as $feedback) {
746 echo get_string('name', 'feedback').': '.$feedback->name
.'<br />';
747 echo html_writer
::checkbox(FEEDBACK_RESETFORM_RESET
.$feedback->id
,
749 get_string('resetting_data', 'feedback'));
751 echo html_writer
::checkbox(FEEDBACK_RESETFORM_DROP
.$feedback->id
,
753 get_string('drop_feedback', 'feedback'));
759 * This gets an array with default options for the editor
761 * @return array the options
763 function feedback_get_editor_options() {
764 return array('maxfiles' => EDITOR_UNLIMITED_FILES
,
769 * This creates new events given as timeopen and closeopen by $feedback.
772 * @param object $feedback
775 function feedback_set_events($feedback) {
778 // Include calendar/lib.php.
779 require_once($CFG->dirroot
.'/calendar/lib.php');
781 // Get CMID if not sent as part of $feedback.
782 if (!isset($feedback->coursemodule
)) {
783 $cm = get_coursemodule_from_instance('feedback', $feedback->id
, $feedback->course
);
784 $feedback->coursemodule
= $cm->id
;
787 // Feedback start calendar events.
788 $eventid = $DB->get_field('event', 'id',
789 array('modulename' => 'feedback', 'instance' => $feedback->id
, 'eventtype' => FEEDBACK_EVENT_TYPE_OPEN
));
791 if (isset($feedback->timeopen
) && $feedback->timeopen
> 0) {
792 $event = new stdClass();
793 $event->eventtype
= FEEDBACK_EVENT_TYPE_OPEN
;
794 $event->type
= empty($feedback->timeclose
) ? CALENDAR_EVENT_TYPE_ACTION
: CALENDAR_EVENT_TYPE_STANDARD
;
795 $event->name
= get_string('calendarstart', 'feedback', $feedback->name
);
796 $event->description
= format_module_intro('feedback', $feedback, $feedback->coursemodule
, false);
797 $event->format
= FORMAT_HTML
;
798 $event->timestart
= $feedback->timeopen
;
799 $event->timesort
= $feedback->timeopen
;
800 $event->visible
= instance_is_visible('feedback', $feedback);
801 $event->timeduration
= 0;
803 // Calendar event exists so update it.
804 $event->id
= $eventid;
805 $calendarevent = calendar_event
::load($event->id
);
806 $calendarevent->update($event, false);
808 // Event doesn't exist so create one.
809 $event->courseid
= $feedback->course
;
812 $event->modulename
= 'feedback';
813 $event->instance
= $feedback->id
;
814 $event->eventtype
= FEEDBACK_EVENT_TYPE_OPEN
;
815 calendar_event
::create($event, false);
817 } else if ($eventid) {
818 // Calendar event is on longer needed.
819 $calendarevent = calendar_event
::load($eventid);
820 $calendarevent->delete();
823 // Feedback close calendar events.
824 $eventid = $DB->get_field('event', 'id',
825 array('modulename' => 'feedback', 'instance' => $feedback->id
, 'eventtype' => FEEDBACK_EVENT_TYPE_CLOSE
));
827 if (isset($feedback->timeclose
) && $feedback->timeclose
> 0) {
828 $event = new stdClass();
829 $event->type
= CALENDAR_EVENT_TYPE_ACTION
;
830 $event->eventtype
= FEEDBACK_EVENT_TYPE_CLOSE
;
831 $event->name
= get_string('calendarend', 'feedback', $feedback->name
);
832 $event->description
= format_module_intro('feedback', $feedback, $feedback->coursemodule
, false);
833 $event->format
= FORMAT_HTML
;
834 $event->timestart
= $feedback->timeclose
;
835 $event->timesort
= $feedback->timeclose
;
836 $event->visible
= instance_is_visible('feedback', $feedback);
837 $event->timeduration
= 0;
839 // Calendar event exists so update it.
840 $event->id
= $eventid;
841 $calendarevent = calendar_event
::load($event->id
);
842 $calendarevent->update($event, false);
844 // Event doesn't exist so create one.
845 $event->courseid
= $feedback->course
;
848 $event->modulename
= 'feedback';
849 $event->instance
= $feedback->id
;
850 calendar_event
::create($event, false);
852 } else if ($eventid) {
853 // Calendar event is on longer needed.
854 $calendarevent = calendar_event
::load($eventid);
855 $calendarevent->delete();
860 * This standard function will check all instances of this module
861 * and make sure there are up-to-date events created for each of them.
862 * If courseid = 0, then every feedback event in the site is checked, else
863 * only feedback events belonging to the course specified are checked.
864 * This function is used, in its new format, by restore_refresh_events()
866 * @param int $courseid
867 * @param int|stdClass $instance Feedback module instance or ID.
868 * @param int|stdClass $cm Course module object or ID (not used in this module).
871 function feedback_refresh_events($courseid = 0, $instance = null, $cm = null) {
874 // If we have instance information then we can just update the one event instead of updating all events.
875 if (isset($instance)) {
876 if (!is_object($instance)) {
877 $instance = $DB->get_record('feedback', array('id' => $instance), '*', MUST_EXIST
);
879 feedback_set_events($instance);
884 if (! $feedbacks = $DB->get_records("feedback", array("course" => $courseid))) {
888 if (! $feedbacks = $DB->get_records("feedback")) {
893 foreach ($feedbacks as $feedback) {
894 feedback_set_events($feedback);
900 * this function is called by {@link feedback_delete_userdata()}
901 * it drops the feedback-instance from the course_module table
904 * @param int $id the id from the coursemodule
907 function feedback_delete_course_module($id) {
910 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
913 return $DB->delete_records('course_modules', array('id'=>$cm->id
));
918 ////////////////////////////////////////////////
919 //functions to handle capabilities
920 ////////////////////////////////////////////////
923 * @deprecated since 3.1
925 function feedback_get_context() {
926 throw new coding_exception('feedback_get_context() can not be used anymore.');
930 * count users which have not completed the feedback
933 * @uses CONTEXT_MODULE
934 * @param cm_info $cm Course-module object
935 * @param int $group single groupid
936 * @param string $sort
937 * @param int $startpage
938 * @param int $pagecount
939 * @param bool $includestatus to return if the user started or not the feedback among the complete user record
940 * @return array array of user ids or user objects when $includestatus set to true
942 function feedback_get_incomplete_users(cm_info
$cm,
947 $includestatus = false) {
951 $context = context_module
::instance($cm->id
);
953 //first get all user who can complete this feedback
954 $cap = 'mod/feedback:complete';
955 $userfieldsapi = \core_user\fields
::for_name();
956 $allnames = $userfieldsapi->get_sql('u', false, '', '', false)->selects
;
957 $fields = 'u.id, ' . $allnames . ', u.picture, u.email, u.imagealt';
958 if (!$allusers = get_users_by_capability($context,
969 // Filter users that are not in the correct group/grouping.
970 $info = new \core_availability\
info_module($cm);
971 $allusersrecords = $info->filter_user_list($allusers);
973 $allusers = array_keys($allusersrecords);
975 //now get all completeds
976 $params = array('feedback'=>$cm->instance
);
977 if ($completedusers = $DB->get_records_menu('feedback_completed', $params, '', 'id, userid')) {
978 // Now strike all completedusers from allusers.
979 $allusers = array_diff($allusers, $completedusers);
982 //for paging I use array_slice()
983 if ($startpage !== false AND $pagecount !== false) {
984 $allusers = array_slice($allusers, $startpage, $pagecount);
987 // Check if we should return the full users objects.
988 if ($includestatus) {
990 $startedusers = $DB->get_records_menu('feedback_completedtmp', ['feedback' => $cm->instance
], '', 'id, userid');
991 $startedusers = array_flip($startedusers);
992 foreach ($allusers as $userid) {
993 $allusersrecords[$userid]->feedbackstarted
= isset($startedusers[$userid]);
994 $userrecords[] = $allusersrecords[$userid];
997 } else { // Return just user ids.
1003 * count users which have not completed the feedback
1007 * @param int $group single groupid
1008 * @return int count of userrecords
1010 function feedback_count_incomplete_users($cm, $group = false) {
1011 if ($allusers = feedback_get_incomplete_users($cm, $group)) {
1012 return count($allusers);
1018 * count users which have completed a feedback
1021 * @uses FEEDBACK_ANONYMOUS_NO
1023 * @param int $group single groupid
1024 * @return int count of userrecords
1026 function feedback_count_complete_users($cm, $group = false) {
1029 $params = array(FEEDBACK_ANONYMOUS_NO
, $cm->instance
);
1034 $fromgroup = ', {groups_members} g';
1035 $wheregroup = ' AND g.groupid = ? AND g.userid = c.userid';
1039 $sql = 'SELECT COUNT(u.id) FROM {user} u, {feedback_completed} c'.$fromgroup.'
1040 WHERE anonymous_response = ? AND u.id = c.userid AND c.feedback = ?
1043 return $DB->count_records_sql($sql, $params);
1048 * get users which have completed a feedback
1051 * @uses CONTEXT_MODULE
1052 * @uses FEEDBACK_ANONYMOUS_NO
1054 * @param int $group single groupid
1055 * @param string $where a sql where condition (must end with " AND ")
1056 * @param array parameters used in $where
1057 * @param string $sort a table field
1058 * @param int $startpage
1059 * @param int $pagecount
1060 * @return object the userrecords
1062 function feedback_get_complete_users($cm,
1065 ?
array $params = null,
1068 $pagecount = false) {
1072 $context = context_module
::instance($cm->id
);
1074 $params = (array)$params;
1076 $params['anon'] = FEEDBACK_ANONYMOUS_NO
;
1077 $params['instance'] = $cm->instance
;
1082 $fromgroup = ', {groups_members} g';
1083 $wheregroup = ' AND g.groupid = :group AND g.userid = c.userid';
1084 $params['group'] = $group;
1088 $sortsql = ' ORDER BY '.$sort;
1093 $userfieldsapi = \core_user\fields
::for_userpic();
1094 $ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects
;
1095 $sql = 'SELECT DISTINCT '.$ufields.', c.timemodified as completed_timemodified
1096 FROM {user} u, {feedback_completed} c '.$fromgroup.'
1097 WHERE '.$where.' anonymous_response = :anon
1099 AND c.feedback = :instance
1100 '.$wheregroup.$sortsql;
1102 if ($startpage === false OR $pagecount === false) {
1106 return $DB->get_records_sql($sql, $params, $startpage, $pagecount);
1110 * get users which have the viewreports-capability
1112 * @uses CONTEXT_MODULE
1114 * @param mixed $groups single groupid or array of groupids - group(s) user is in
1115 * @return object the userrecords
1117 function feedback_get_viewreports_users($cmid, $groups = false) {
1119 $context = context_module
::instance($cmid);
1121 //description of the call below:
1122 //get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='',
1123 // $limitnum='', $groups='', $exceptions='', $doanything=true)
1124 return get_users_by_capability($context,
1125 'mod/feedback:viewreports',
1136 * get users which have the receivemail-capability
1138 * @uses CONTEXT_MODULE
1140 * @param mixed $groups single groupid or array of groupids - group(s) user is in
1141 * @return object the userrecords
1143 function feedback_get_receivemail_users($cmid, $groups = false) {
1145 $context = context_module
::instance($cmid);
1147 //description of the call below:
1148 //get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='',
1149 // $limitnum='', $groups='', $exceptions='', $doanything=true)
1150 return get_users_by_capability($context,
1151 'mod/feedback:receivemail',
1161 ////////////////////////////////////////////////
1162 //functions to handle the templates
1163 ////////////////////////////////////////////////
1164 ////////////////////////////////////////////////
1167 * creates a new template-record.
1170 * @param int $courseid
1171 * @param string $name the name of template shown in the templatelist
1172 * @param int $ispublic 0:privat 1:public
1173 * @return stdClass the new template
1175 function feedback_create_template($courseid, $name, $ispublic = 0) {
1178 $templ = new stdClass();
1179 $templ->course
= ($ispublic ?
0 : $courseid);
1180 $templ->name
= $name;
1181 $templ->ispublic
= $ispublic;
1183 $templid = $DB->insert_record('feedback_template', $templ);
1184 return $DB->get_record('feedback_template', array('id'=>$templid));
1188 * creates new template items.
1189 * all items will be copied and the attribute feedback will be set to 0
1190 * and the attribute template will be set to the new templateid
1193 * @uses CONTEXT_MODULE
1194 * @uses CONTEXT_COURSE
1195 * @param object $feedback
1196 * @param string $name the name of template shown in the templatelist
1197 * @param int $ispublic 0:privat 1:public
1200 function feedback_save_as_template($feedback, $name, $ispublic = 0) {
1202 $fs = get_file_storage();
1204 if (!$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id
))) {
1208 if (!$newtempl = feedback_create_template($feedback->course
, $name, $ispublic)) {
1212 //files in the template_item are in the context of the current course or
1213 //if the template is public the files are in the system context
1214 //files in the feedback_item are in the feedback_context of the feedback
1216 $s_context = context_system
::instance();
1218 $s_context = context_course
::instance($newtempl->course
);
1220 $cm = get_coursemodule_from_instance('feedback', $feedback->id
);
1221 $f_context = context_module
::instance($cm->id
);
1223 //create items of this new template
1224 //depend items we are storing temporary in an mapping list array(new id => dependitem)
1225 //we also store a mapping of all items array(oldid => newid)
1226 $dependitemsmap = array();
1227 $itembackup = array();
1228 foreach ($feedbackitems as $item) {
1230 $t_item = clone($item);
1233 $t_item->feedback
= 0;
1234 $t_item->template
= $newtempl->id
;
1235 $t_item->id
= $DB->insert_record('feedback_item', $t_item);
1236 //copy all included files to the feedback_template filearea
1237 $itemfiles = $fs->get_area_files($f_context->id
,
1244 foreach ($itemfiles as $ifile) {
1245 $file_record = new stdClass();
1246 $file_record->contextid
= $s_context->id
;
1247 $file_record->component
= 'mod_feedback';
1248 $file_record->filearea
= 'template';
1249 $file_record->itemid
= $t_item->id
;
1250 $fs->create_file_from_storedfile($file_record, $ifile);
1254 $itembackup[$item->id
] = $t_item->id
;
1255 if ($t_item->dependitem
) {
1256 $dependitemsmap[$t_item->id
] = $t_item->dependitem
;
1261 //remapping the dependency
1262 foreach ($dependitemsmap as $key => $dependitem) {
1263 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
1264 $newitem->dependitem
= $itembackup[$newitem->dependitem
];
1265 $DB->update_record('feedback_item', $newitem);
1272 * deletes all feedback_items related to the given template id
1275 * @uses CONTEXT_COURSE
1276 * @param object $template the template
1279 function feedback_delete_template($template) {
1282 //deleting the files from the item is done by feedback_delete_item
1283 if ($t_items = $DB->get_records("feedback_item", array("template"=>$template->id
))) {
1284 foreach ($t_items as $t_item) {
1285 feedback_delete_item($t_item->id
, false, $template);
1288 $DB->delete_records("feedback_template", array("id"=>$template->id
));
1292 * creates new feedback_item-records from template.
1293 * if $deleteold is set true so the existing items of the given feedback will be deleted
1294 * if $deleteold is set false so the new items will be appanded to the old items
1297 * @uses CONTEXT_COURSE
1298 * @uses CONTEXT_MODULE
1299 * @param object $feedback
1300 * @param int $templateid
1301 * @param boolean $deleteold
1303 function feedback_items_from_template($feedback, $templateid, $deleteold = false) {
1306 require_once($CFG->libdir
.'/completionlib.php');
1308 $fs = get_file_storage();
1310 if (!$template = $DB->get_record('feedback_template', array('id'=>$templateid))) {
1313 //get all templateitems
1314 if (!$templitems = $DB->get_records('feedback_item', array('template'=>$templateid))) {
1318 //files in the template_item are in the context of the current course
1319 //files in the feedback_item are in the feedback_context of the feedback
1320 if ($template->ispublic
) {
1321 $s_context = context_system
::instance();
1323 $s_context = context_course
::instance($feedback->course
);
1325 $course = $DB->get_record('course', array('id'=>$feedback->course
));
1326 $cm = get_coursemodule_from_instance('feedback', $feedback->id
);
1327 $f_context = context_module
::instance($cm->id
);
1329 //if deleteold then delete all old items before
1332 if ($feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id
))) {
1333 //delete all items of this feedback
1334 foreach ($feedbackitems as $item) {
1335 feedback_delete_item($item->id
, false);
1338 $params = array('feedback'=>$feedback->id
);
1339 if ($completeds = $DB->get_records('feedback_completed', $params)) {
1340 $completion = new completion_info($course);
1341 foreach ($completeds as $completed) {
1342 $DB->delete_records('feedback_completed', array('id' => $completed->id
));
1343 // Update completion state
1344 if ($completion->is_enabled($cm) && $cm->completion
== COMPLETION_TRACKING_AUTOMATIC
&&
1345 $feedback->completionsubmit
) {
1346 $completion->update_state($cm, COMPLETION_INCOMPLETE
, $completed->userid
);
1350 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedback->id
));
1352 $positionoffset = 0;
1354 //if the old items are kept the new items will be appended
1355 //therefor the new position has an offset
1356 $positionoffset = $DB->count_records('feedback_item', array('feedback'=>$feedback->id
));
1359 //create items of this new template
1360 //depend items we are storing temporary in an mapping list array(new id => dependitem)
1361 //we also store a mapping of all items array(oldid => newid)
1362 $dependitemsmap = array();
1363 $itembackup = array();
1364 foreach ($templitems as $t_item) {
1365 $item = clone($t_item);
1367 $item->feedback
= $feedback->id
;
1368 $item->template
= 0;
1369 $item->position
= $item->position +
$positionoffset;
1371 $item->id
= $DB->insert_record('feedback_item', $item);
1373 //moving the files to the new item
1374 $templatefiles = $fs->get_area_files($s_context->id
,
1380 if ($templatefiles) {
1381 foreach ($templatefiles as $tfile) {
1382 $file_record = new stdClass();
1383 $file_record->contextid
= $f_context->id
;
1384 $file_record->component
= 'mod_feedback';
1385 $file_record->filearea
= 'item';
1386 $file_record->itemid
= $item->id
;
1387 $fs->create_file_from_storedfile($file_record, $tfile);
1391 $itembackup[$t_item->id
] = $item->id
;
1392 if ($item->dependitem
) {
1393 $dependitemsmap[$item->id
] = $item->dependitem
;
1397 //remapping the dependency
1398 foreach ($dependitemsmap as $key => $dependitem) {
1399 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
1400 $newitem->dependitem
= $itembackup[$newitem->dependitem
];
1401 $DB->update_record('feedback_item', $newitem);
1406 * get the list of available templates.
1407 * if the $onlyown param is set true so only templates from own course will be served
1408 * this is important for droping templates
1411 * @param object $course
1412 * @param string $onlyownorpublic
1413 * @return array the template recordsets
1415 function feedback_get_template_list($course, $onlyownorpublic = '') {
1418 switch($onlyownorpublic) {
1420 $templates = $DB->get_records_select('feedback_template',
1421 'course = ? OR ispublic = 1',
1426 $templates = $DB->get_records('feedback_template',
1427 array('course'=>$course->id
),
1431 $templates = $DB->get_records('feedback_template', array('ispublic'=>1), 'name');
1437 ////////////////////////////////////////////////
1438 //Handling der Items
1439 ////////////////////////////////////////////////
1440 ////////////////////////////////////////////////
1443 * load the lib.php from item-plugin-dir and returns the instance of the itemclass
1445 * @param string $typ
1446 * @return feedback_item_base the instance of itemclass
1447 * @throws moodle_exception For invalid type
1449 function feedback_get_item_class($typ) {
1452 require_once($CFG->dirroot
.'/mod/feedback/item/feedback_item_class.php');
1454 //get the class of item-typ
1455 $typeclean = clean_param($typ, PARAM_ALPHA
);
1457 $itemclass = "feedback_item_{$typeclean}";
1458 $itemclasspath = "{$CFG->dirroot}/mod/feedback/item/{$typeclean}/lib.php";
1460 //get the instance of item-class
1461 if (!class_exists($itemclass) && file_exists($itemclasspath)) {
1462 require_once($itemclasspath);
1465 if (!class_exists($itemclass)) {
1466 throw new moodle_exception('typemissing', 'feedback');
1469 return new $itemclass();
1473 * load the available item plugins from given subdirectory of $CFG->dirroot
1474 * the default is "mod/feedback/item"
1477 * @param string $dir the subdir
1478 * @return array pluginnames as string
1480 function feedback_load_feedback_items($dir = 'mod/feedback/item') {
1482 $names = get_list_of_plugins($dir);
1483 $ret_names = array();
1485 foreach ($names as $name) {
1486 require_once($CFG->dirroot
.'/'.$dir.'/'.$name.'/lib.php');
1487 if (class_exists('feedback_item_'.$name)) {
1488 $ret_names[] = $name;
1495 * load the available item plugins to use as dropdown-options
1498 * @return array pluginnames as string
1500 function feedback_load_feedback_items_options() {
1503 $feedback_options = array("pagebreak" => get_string('add_pagebreak', 'feedback'));
1505 if (!$feedback_names = feedback_load_feedback_items('mod/feedback/item')) {
1509 foreach ($feedback_names as $fn) {
1510 $feedback_options[$fn] = get_string($fn, 'feedback');
1512 asort($feedback_options);
1513 return $feedback_options;
1517 * load the available items for the depend item dropdown list shown in the edit_item form
1520 * @param object $feedback
1521 * @param object $item the item of the edit_item form
1522 * @return array all items except the item $item, labels and pagebreaks
1524 function feedback_get_depend_candidates_for_item($feedback, $item) {
1526 //all items for dependitem
1527 $where = "feedback = ? AND typ != 'pagebreak' AND hasvalue = 1";
1528 $params = array($feedback->id
);
1529 if (isset($item->id
) AND $item->id
) {
1530 $where .= ' AND id != ?';
1531 $params[] = $item->id
;
1533 $dependitems = array(0 => get_string('choose'));
1534 $feedbackitems = $DB->get_records_select_menu('feedback_item',
1540 if (!$feedbackitems) {
1541 return $dependitems;
1543 //adding the choose-option
1544 foreach ($feedbackitems as $key => $val) {
1545 if (trim(strval($val)) !== '') {
1546 $dependitems[$key] = format_string($val);
1549 return $dependitems;
1553 * @deprecated since 3.1
1555 function feedback_create_item() {
1556 throw new coding_exception('feedback_create_item() can not be used anymore.');
1560 * save the changes of a given item.
1563 * @param object $item
1566 function feedback_update_item($item) {
1568 return $DB->update_record("feedback_item", $item);
1572 * deletes an item and also deletes all related values
1575 * @uses CONTEXT_MODULE
1576 * @param int $itemid
1577 * @param boolean $renumber should the kept items renumbered Yes/No
1578 * @param object $template if the template is given so the items are bound to it
1581 function feedback_delete_item($itemid, $renumber = true, $template = false) {
1584 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
1586 //deleting the files from the item
1587 $fs = get_file_storage();
1590 if ($template->ispublic
) {
1591 $context = context_system
::instance();
1593 $context = context_course
::instance($template->course
);
1595 $templatefiles = $fs->get_area_files($context->id
,
1602 if ($templatefiles) {
1603 $fs->delete_area_files($context->id
, 'mod_feedback', 'template', $item->id
);
1606 if (!$cm = get_coursemodule_from_instance('feedback', $item->feedback
)) {
1609 $context = context_module
::instance($cm->id
);
1611 $itemfiles = $fs->get_area_files($context->id
,
1618 $fs->delete_area_files($context->id
, 'mod_feedback', 'item', $item->id
);
1622 $DB->delete_records("feedback_value", array("item"=>$itemid));
1623 $DB->delete_records("feedback_valuetmp", array("item"=>$itemid));
1625 //remove all depends
1626 $DB->set_field('feedback_item', 'dependvalue', '', array('dependitem'=>$itemid));
1627 $DB->set_field('feedback_item', 'dependitem', 0, array('dependitem'=>$itemid));
1629 $DB->delete_records("feedback_item", array("id"=>$itemid));
1631 feedback_renumber_items($item->feedback
);
1636 * deletes all items of the given feedbackid
1639 * @param int $feedbackid
1642 function feedback_delete_all_items($feedbackid) {
1644 require_once($CFG->libdir
.'/completionlib.php');
1646 if (!$feedback = $DB->get_record('feedback', array('id'=>$feedbackid))) {
1650 if (!$cm = get_coursemodule_from_instance('feedback', $feedback->id
)) {
1654 if (!$course = $DB->get_record('course', array('id'=>$feedback->course
))) {
1658 if (!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid))) {
1661 foreach ($items as $item) {
1662 feedback_delete_item($item->id
, false);
1664 if ($completeds = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id
))) {
1665 $completion = new completion_info($course);
1666 foreach ($completeds as $completed) {
1667 $DB->delete_records('feedback_completed', array('id' => $completed->id
));
1668 // Update completion state
1669 if ($completion->is_enabled($cm) && $cm->completion
== COMPLETION_TRACKING_AUTOMATIC
&&
1670 $feedback->completionsubmit
) {
1671 $completion->update_state($cm, COMPLETION_INCOMPLETE
, $completed->userid
);
1676 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedbackid));
1681 * this function toggled the item-attribute required (yes/no)
1684 * @param object $item
1687 function feedback_switch_item_required($item) {
1690 $itemobj = feedback_get_item_class($item->typ
);
1692 if ($itemobj->can_switch_require()) {
1693 $new_require_val = (int)!(bool)$item->required
;
1694 $params = array('id'=>$item->id
);
1695 $DB->set_field('feedback_item', 'required', $new_require_val, $params);
1701 * renumbers all items of the given feedbackid
1704 * @param int $feedbackid
1707 function feedback_renumber_items($feedbackid) {
1710 $items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position');
1713 foreach ($items as $item) {
1714 $DB->set_field('feedback_item', 'position', $pos, array('id'=>$item->id
));
1721 * this decreases the position of the given item
1724 * @param object $item
1727 function feedback_moveup_item($item) {
1730 if ($item->position
== 1) {
1734 $params = array('feedback'=>$item->feedback
);
1735 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
1740 foreach ($items as $i) {
1741 if ($i->id
== $item->id
) {
1742 if (is_null($itembefore)) {
1745 $itembefore->position
= $item->position
;
1747 feedback_update_item($itembefore);
1748 feedback_update_item($item);
1749 feedback_renumber_items($item->feedback
);
1758 * this increased the position of the given item
1761 * @param object $item
1764 function feedback_movedown_item($item) {
1767 $params = array('feedback'=>$item->feedback
);
1768 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
1772 $movedownitem = null;
1773 foreach ($items as $i) {
1774 if (!is_null($movedownitem) AND $movedownitem->id
== $item->id
) {
1775 $movedownitem->position
= $i->position
;
1777 feedback_update_item($movedownitem);
1778 feedback_update_item($i);
1779 feedback_renumber_items($item->feedback
);
1788 * here the position of the given item will be set to the value in $pos
1791 * @param object $moveitem
1795 function feedback_move_item($moveitem, $pos) {
1798 $params = array('feedback'=>$moveitem->feedback
);
1799 if (!$allitems = $DB->get_records('feedback_item', $params, 'position')) {
1802 if (is_array($allitems)) {
1804 foreach ($allitems as $item) {
1805 if ($index == $pos) {
1808 if ($item->id
== $moveitem->id
) {
1809 $moveitem->position
= $pos;
1810 feedback_update_item($moveitem);
1813 $item->position
= $index;
1814 feedback_update_item($item);
1823 * @deprecated since Moodle 3.1
1825 function feedback_print_item_preview() {
1826 throw new coding_exception('feedback_print_item_preview() can not be used anymore. '
1827 . 'Items must implement complete_form_element().');
1831 * @deprecated since Moodle 3.1
1833 function feedback_print_item_complete() {
1834 throw new coding_exception('feedback_print_item_complete() can not be used anymore. '
1835 . 'Items must implement complete_form_element().');
1839 * @deprecated since Moodle 3.1
1841 function feedback_print_item_show_value() {
1842 throw new coding_exception('feedback_print_item_show_value() can not be used anymore. '
1843 . 'Items must implement complete_form_element().');
1847 * if the user completes a feedback and there is a pagebreak so the values are saved temporary.
1848 * the values are not saved permanently until the user click on save button
1851 * @param object $feedbackcompleted
1852 * @return object temporary saved completed-record
1854 function feedback_set_tmp_values($feedbackcompleted) {
1857 //first we create a completedtmp
1858 $tmpcpl = new stdClass();
1859 foreach ($feedbackcompleted as $key => $value) {
1860 $tmpcpl->{$key} = $value;
1863 $tmpcpl->timemodified
= time();
1864 $tmpcpl->id
= $DB->insert_record('feedback_completedtmp', $tmpcpl);
1865 //get all values of original-completed
1866 if (!$values = $DB->get_records('feedback_value', array('completed'=>$feedbackcompleted->id
))) {
1869 foreach ($values as $value) {
1871 $value->completed
= $tmpcpl->id
;
1872 $DB->insert_record('feedback_valuetmp', $value);
1878 * this saves the temporary saved values permanently
1881 * @param object $feedbackcompletedtmp the temporary completed
1882 * @param stdClass|null $feedbackcompleted the target completed
1883 * @return int the id of the completed
1885 function feedback_save_tmp_values($feedbackcompletedtmp, ?stdClass
$feedbackcompleted = null) {
1888 $tmpcplid = $feedbackcompletedtmp->id
;
1889 if ($feedbackcompleted) {
1890 //first drop all existing values
1891 $DB->delete_records('feedback_value', array('completed'=>$feedbackcompleted->id
));
1892 //update the current completed
1893 $feedbackcompleted->timemodified
= time();
1894 $DB->update_record('feedback_completed', $feedbackcompleted);
1896 $feedbackcompleted = clone($feedbackcompletedtmp);
1897 $feedbackcompleted->id
= '';
1898 $feedbackcompleted->timemodified
= time();
1899 $feedbackcompleted->id
= $DB->insert_record('feedback_completed', $feedbackcompleted);
1902 $allitems = $DB->get_records('feedback_item', array('feedback' => $feedbackcompleted->feedback
));
1904 //save all the new values from feedback_valuetmp
1905 //get all values of tmp-completed
1906 $params = array('completed'=>$feedbackcompletedtmp->id
);
1907 $values = $DB->get_records('feedback_valuetmp', $params);
1908 foreach ($values as $value) {
1909 //check if there are depend items
1910 $item = $DB->get_record('feedback_item', array('id'=>$value->item
));
1911 if ($item->dependitem
> 0 && isset($allitems[$item->dependitem
])) {
1912 $ditem = $allitems[$item->dependitem
];
1913 while ($ditem !== null) {
1914 $check = feedback_compare_item_value($tmpcplid,
1921 if ($ditem->dependitem
> 0 && isset($allitems[$ditem->dependitem
])) {
1923 $ditem = $allitems[$ditem->dependitem
];
1934 $value->completed
= $feedbackcompleted->id
;
1935 $DB->insert_record('feedback_value', $value);
1938 //drop all the tmpvalues
1939 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1940 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
1942 // Trigger event for the delete action we performed.
1943 $cm = get_coursemodule_from_instance('feedback', $feedbackcompleted->feedback
);
1944 $event = \mod_feedback\event\response_submitted
::create_from_record($feedbackcompleted, $cm);
1946 return $feedbackcompleted->id
;
1951 * @deprecated since Moodle 3.1
1953 function feedback_delete_completedtmp() {
1954 throw new coding_exception('feedback_delete_completedtmp() can not be used anymore.');
1958 ////////////////////////////////////////////////
1959 ////////////////////////////////////////////////
1960 ////////////////////////////////////////////////
1961 //functions to handle the pagebreaks
1962 ////////////////////////////////////////////////
1965 * this creates a pagebreak.
1966 * a pagebreak is a special kind of item
1969 * @param int $feedbackid
1970 * @return int|false false if there already is a pagebreak on last position or the id of the pagebreak-item
1972 function feedback_create_pagebreak($feedbackid) {
1975 // Disallow pagebreak if there's already one present in last position, or the feedback has no items.
1976 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
1977 if ($lastposition == feedback_get_last_break_position($feedbackid)) {
1981 $item = new stdClass();
1982 $item->feedback
= $feedbackid;
1988 $item->presentation
= '';
1989 $item->hasvalue
= 0;
1991 $item->typ
= 'pagebreak';
1992 $item->position
= $lastposition +
1;
1996 return $DB->insert_record('feedback_item', $item);
2000 * get all positions of pagebreaks in the given feedback
2003 * @param int $feedbackid
2004 * @return array all ordered pagebreak positions
2006 function feedback_get_all_break_positions($feedbackid) {
2009 $params = array('typ'=>'pagebreak', 'feedback'=>$feedbackid);
2010 $allbreaks = $DB->get_records_menu('feedback_item', $params, 'position', 'id, position');
2014 return array_values($allbreaks);
2018 * get the position of the last pagebreak
2020 * @param int $feedbackid
2021 * @return int the position of the last pagebreak
2023 function feedback_get_last_break_position($feedbackid) {
2024 if (!$allbreaks = feedback_get_all_break_positions($feedbackid)) {
2027 return $allbreaks[count($allbreaks) - 1];
2031 * @deprecated since Moodle 3.1
2033 function feedback_get_page_to_continue() {
2034 throw new coding_exception('feedback_get_page_to_continue() can not be used anymore.');
2037 ////////////////////////////////////////////////
2038 ////////////////////////////////////////////////
2039 ////////////////////////////////////////////////
2040 //functions to handle the values
2041 ////////////////////////////////////////////////
2044 * @deprecated since Moodle 3.1
2046 function feedback_clean_input_value() {
2047 throw new coding_exception('feedback_clean_input_value() can not be used anymore. '
2048 . 'Items must implement complete_form_element().');
2053 * @deprecated since Moodle 3.1
2055 function feedback_save_values() {
2056 throw new coding_exception('feedback_save_values() can not be used anymore.');
2060 * @deprecated since Moodle 3.1
2062 function feedback_save_guest_values() {
2063 throw new coding_exception('feedback_save_guest_values() can not be used anymore.');
2067 * get the value from the given item related to the given completed.
2068 * the value can come as temporary or as permanently value. the deciding is done by $tmp
2071 * @param int $completeid
2072 * @param int $itemid
2073 * @param boolean $tmp
2074 * @return mixed the value, the type depends on plugin-definition
2076 function feedback_get_item_value($completedid, $itemid, $tmp = false) {
2079 $tmpstr = $tmp ?
'tmp' : '';
2080 $params = array('completed'=>$completedid, 'item'=>$itemid);
2081 return $DB->get_field('feedback_value'.$tmpstr, 'value', $params);
2085 * compares the value of the itemid related to the completedid with the dependvalue.
2086 * this is used if a depend item is set.
2087 * the value can come as temporary or as permanently value. the deciding is done by $tmp.
2089 * @param int $completedid
2090 * @param stdClass|int $item
2091 * @param mixed $dependvalue
2095 function feedback_compare_item_value($completedid, $item, $dependvalue, $tmp = false) {
2098 if (is_int($item)) {
2099 $item = $DB->get_record('feedback_item', array('id' => $item));
2102 $dbvalue = feedback_get_item_value($completedid, $item->id
, $tmp);
2104 $itemobj = feedback_get_item_class($item->typ
);
2105 return $itemobj->compare_value($item, $dbvalue, $dependvalue); //true or false
2109 * @deprecated since Moodle 3.1
2111 function feedback_check_values() {
2112 throw new coding_exception('feedback_check_values() can not be used anymore. '
2113 . 'Items must implement complete_form_element().');
2117 * @deprecated since Moodle 3.1
2119 function feedback_create_values() {
2120 throw new coding_exception('feedback_create_values() can not be used anymore.');
2124 * @deprecated since Moodle 3.1
2126 function feedback_update_values() {
2127 throw new coding_exception('feedback_update_values() can not be used anymore.');
2131 * get the values of an item depending on the given groupid.
2132 * if the feedback is anonymous so the values are shuffled
2136 * @param object $item
2137 * @param int $groupid
2138 * @param int $courseid
2139 * @param bool $ignore_empty if this is set true so empty values are not delivered
2140 * @return array the value-records
2142 function feedback_get_group_values($item,
2145 $ignore_empty = false) {
2149 //if the groupid is given?
2150 if (intval($groupid) > 0) {
2152 if ($ignore_empty) {
2153 $value = $DB->sql_compare_text('fbv.value');
2154 $ignore_empty_select = "AND $value != :emptyvalue AND $value != :zerovalue";
2155 $params +
= array('emptyvalue' => '', 'zerovalue' => '0');
2157 $ignore_empty_select = "";
2160 $query = 'SELECT fbv . *
2161 FROM {feedback_value} fbv, {feedback_completed} fbc, {groups_members} gm
2162 WHERE fbv.item = :itemid
2163 AND fbv.completed = fbc.id
2164 AND fbc.userid = gm.userid
2165 '.$ignore_empty_select.'
2166 AND gm.groupid = :groupid
2167 ORDER BY fbc.timemodified';
2168 $params +
= array('itemid' => $item->id
, 'groupid' => $groupid);
2169 $values = $DB->get_records_sql($query, $params);
2173 if ($ignore_empty) {
2174 $value = $DB->sql_compare_text('value');
2175 $ignore_empty_select = "AND $value != :emptyvalue AND $value != :zerovalue";
2176 $params +
= array('emptyvalue' => '', 'zerovalue' => '0');
2178 $ignore_empty_select = "";
2182 $select = "item = :itemid AND course_id = :courseid ".$ignore_empty_select;
2183 $params +
= array('itemid' => $item->id
, 'courseid' => $courseid);
2184 $values = $DB->get_records_select('feedback_value', $select, $params);
2186 $select = "item = :itemid ".$ignore_empty_select;
2187 $params +
= array('itemid' => $item->id
);
2188 $values = $DB->get_records_select('feedback_value', $select, $params);
2191 $params = array('id'=>$item->feedback
);
2192 if ($DB->get_field('feedback', 'anonymous', $params) == FEEDBACK_ANONYMOUS_YES
) {
2193 if (is_array($values)) {
2201 * check for multiple_submit = false.
2202 * if the feedback is global so the courseid must be given
2206 * @param int $feedbackid
2207 * @param int $courseid
2208 * @return boolean true if the feedback already is submitted otherwise false
2210 function feedback_is_already_submitted($feedbackid, $courseid = false) {
2213 if (!isloggedin() ||
isguestuser()) {
2217 $params = array('userid' => $USER->id
, 'feedback' => $feedbackid);
2219 $params['courseid'] = $courseid;
2221 return $DB->record_exists('feedback_completed', $params);
2225 * @deprecated since Moodle 3.1. Use feedback_get_current_completed_tmp() or feedback_get_last_completed.
2227 function feedback_get_current_completed() {
2228 throw new coding_exception('feedback_get_current_completed() can not be used anymore. Please ' .
2229 'use either feedback_get_current_completed_tmp() or feedback_get_last_completed()');
2233 * get the completeds depending on the given groupid.
2237 * @param object $feedback
2238 * @param int $groupid
2239 * @param int $courseid
2240 * @return mixed array of found completeds otherwise false
2242 function feedback_get_completeds_group($feedback, $groupid = false, $courseid = false) {
2245 if (intval($groupid) > 0) {
2246 $query = "SELECT fbc.*
2247 FROM {feedback_completed} fbc, {groups_members} gm
2248 WHERE fbc.feedback = ?
2250 AND fbc.userid = gm.userid";
2251 if ($values = $DB->get_records_sql($query, array($feedback->id
, $groupid))) {
2258 $query = "SELECT DISTINCT fbc.*
2259 FROM {feedback_completed} fbc, {feedback_value} fbv
2260 WHERE fbc.id = fbv.completed
2261 AND fbc.feedback = ?
2262 AND fbv.course_id = ?
2263 ORDER BY random_response";
2264 if ($values = $DB->get_records_sql($query, array($feedback->id
, $courseid))) {
2270 if ($values = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id
))) {
2280 * get the count of completeds depending on the given groupid.
2284 * @param object $feedback
2285 * @param int $groupid
2286 * @param int $courseid
2287 * @return mixed count of completeds or false
2289 function feedback_get_completeds_group_count($feedback, $groupid = false, $courseid = false) {
2292 if ($courseid > 0 AND !$groupid <= 0) {
2293 $sql = "SELECT id, COUNT(item) AS ci
2294 FROM {feedback_value}
2296 GROUP BY item ORDER BY ci DESC";
2297 if ($foundrecs = $DB->get_records_sql($sql, array($courseid))) {
2298 $foundrecs = array_values($foundrecs);
2299 return $foundrecs[0]->ci
;
2303 if ($values = feedback_get_completeds_group($feedback, $groupid)) {
2304 return count($values);
2311 * deletes all completed-recordsets from a feedback.
2312 * all related data such as values also will be deleted
2314 * @param stdClass|int $feedback
2315 * @param stdClass|cm_info $cm
2316 * @param stdClass $course
2319 function feedback_delete_all_completeds($feedback, $cm = null, $course = null) {
2322 if (is_int($feedback)) {
2323 $feedback = $DB->get_record('feedback', array('id' => $feedback));
2326 if (!$completeds = $DB->get_records('feedback_completed', array('feedback' => $feedback->id
))) {
2330 if (!$course && !($course = $DB->get_record('course', array('id' => $feedback->course
)))) {
2334 if (!$cm && !($cm = get_coursemodule_from_instance('feedback', $feedback->id
))) {
2338 foreach ($completeds as $completed) {
2339 feedback_delete_completed($completed, $feedback, $cm, $course);
2344 * deletes a completed given by completedid.
2345 * all related data such values or tracking data also will be deleted
2347 * @param int|stdClass $completed
2348 * @param stdClass $feedback
2349 * @param stdClass|cm_info $cm
2350 * @param stdClass $course
2353 function feedback_delete_completed($completed, $feedback = null, $cm = null, $course = null) {
2355 require_once($CFG->libdir
.'/completionlib.php');
2357 if (!isset($completed->id
)) {
2358 if (!$completed = $DB->get_record('feedback_completed', array('id' => $completed))) {
2363 if (!$feedback && !($feedback = $DB->get_record('feedback', array('id' => $completed->feedback
)))) {
2367 if (!$course && !($course = $DB->get_record('course', array('id' => $feedback->course
)))) {
2371 if (!$cm && !($cm = get_coursemodule_from_instance('feedback', $feedback->id
))) {
2375 //first we delete all related values
2376 $DB->delete_records('feedback_value', array('completed' => $completed->id
));
2378 // Delete the completed record.
2379 $return = $DB->delete_records('feedback_completed', array('id' => $completed->id
));
2381 // Update completion state
2382 $completion = new completion_info($course);
2383 if ($completion->is_enabled($cm) && $cm->completion
== COMPLETION_TRACKING_AUTOMATIC
&& $feedback->completionsubmit
) {
2384 $completion->update_state($cm, COMPLETION_INCOMPLETE
, $completed->userid
);
2386 // Trigger event for the delete action we performed.
2387 $event = \mod_feedback\event\response_deleted
::create_from_record($completed, $cm, $feedback);
2393 ////////////////////////////////////////////////
2394 ////////////////////////////////////////////////
2395 ////////////////////////////////////////////////
2396 //functions to handle sitecourse mapping
2397 ////////////////////////////////////////////////
2400 * @deprecated since 3.1
2402 function feedback_is_course_in_sitecourse_map() {
2403 throw new coding_exception('feedback_is_course_in_sitecourse_map() can not be used anymore.');
2407 * @deprecated since 3.1
2409 function feedback_is_feedback_in_sitecourse_map() {
2410 throw new coding_exception('feedback_is_feedback_in_sitecourse_map() can not be used anymore.');
2414 * gets the feedbacks from table feedback_sitecourse_map.
2415 * this is used to show the global feedbacks on the feedback block
2416 * all feedbacks with the following criteria will be selected:<br />
2418 * 1) all feedbacks which id are listed together with the courseid in sitecoursemap and<br />
2419 * 2) all feedbacks which not are listed in sitecoursemap
2422 * @param int $courseid
2423 * @return array the feedback-records
2425 function feedback_get_feedbacks_from_sitecourse_map($courseid) {
2428 //first get all feedbacks listed in sitecourse_map with named courseid
2429 $sql = "SELECT f.id AS id,
2432 f.timeopen AS timeopen,
2433 f.timeclose AS timeclose
2434 FROM {feedback} f, {course_modules} cm, {feedback_sitecourse_map} sm, {modules} m
2435 WHERE f.id = cm.instance
2436 AND f.course = '".SITEID
."'
2437 AND m.id = cm.module
2438 AND m.name = 'feedback'
2440 AND sm.feedbackid = f.id";
2442 if (!$feedbacks1 = $DB->get_records_sql($sql, array($courseid))) {
2443 $feedbacks1 = array();
2446 //second get all feedbacks not listed in sitecourse_map
2447 $feedbacks2 = array();
2448 $sql = "SELECT f.id AS id,
2451 f.timeopen AS timeopen,
2452 f.timeclose AS timeclose
2453 FROM {feedback} f, {course_modules} cm, {modules} m
2454 WHERE f.id = cm.instance
2455 AND f.course = '".SITEID
."'
2456 AND m.id = cm.module
2457 AND m.name = 'feedback'";
2458 if (!$allfeedbacks = $DB->get_records_sql($sql)) {
2459 $allfeedbacks = array();
2461 foreach ($allfeedbacks as $a) {
2462 if (!$DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$a->id
))) {
2467 $feedbacks = array_merge($feedbacks1, $feedbacks2);
2468 $modinfo = get_fast_modinfo(SITEID
);
2469 return array_filter($feedbacks, function($f) use ($modinfo) {
2470 return ($cm = $modinfo->get_cm($f->cmid
)) && $cm->uservisible
;
2476 * Gets the courses from table feedback_sitecourse_map
2478 * @param int $feedbackid
2479 * @return array the course-records
2481 function feedback_get_courses_from_sitecourse_map($feedbackid) {
2484 $sql = "SELECT c.id, c.fullname, c.shortname
2485 FROM {feedback_sitecourse_map} f, {course} c
2486 WHERE c.id = f.courseid
2487 AND f.feedbackid = ?
2488 ORDER BY c.fullname";
2490 return $DB->get_records_sql($sql, array($feedbackid));
2495 * Updates the course mapping for the feedback
2497 * @param stdClass $feedback
2498 * @param array $courses array of course ids
2500 function feedback_update_sitecourse_map($feedback, $courses) {
2502 if (empty($courses)) {
2505 $currentmapping = $DB->get_fieldset_select('feedback_sitecourse_map', 'courseid', 'feedbackid=?', array($feedback->id
));
2506 foreach (array_diff($courses, $currentmapping) as $courseid) {
2507 $DB->insert_record('feedback_sitecourse_map', array('feedbackid' => $feedback->id
, 'courseid' => $courseid));
2509 foreach (array_diff($currentmapping, $courses) as $courseid) {
2510 $DB->delete_records('feedback_sitecourse_map', array('feedbackid' => $feedback->id
, 'courseid' => $courseid));
2512 // TODO MDL-53574 add events.
2516 * @deprecated since 3.1
2518 function feedback_clean_up_sitecourse_map() {
2519 throw new coding_exception('feedback_clean_up_sitecourse_map() can not be used anymore.');
2522 ////////////////////////////////////////////////
2523 ////////////////////////////////////////////////
2524 ////////////////////////////////////////////////
2525 //not relatable functions
2526 ////////////////////////////////////////////////
2529 * @deprecated since 3.1
2531 function feedback_print_numeric_option_list() {
2532 throw new coding_exception('feedback_print_numeric_option_list() can not be used anymore.');
2536 * sends an email to the teachers of the course where the given feedback is placed.
2540 * @uses FEEDBACK_ANONYMOUS_NO
2541 * @uses FORMAT_PLAIN
2542 * @param object $cm the coursemodule-record
2543 * @param object $feedback
2544 * @param object $course
2545 * @param stdClass|int $user
2546 * @param stdClass $completed record from feedback_completed if known
2549 function feedback_send_email($cm, $feedback, $course, $user, $completed = null) {
2550 global $CFG, $DB, $PAGE;
2552 if ($feedback->email_notification
== 0) { // No need to do anything
2556 if (!is_object($user)) {
2557 $user = $DB->get_record('user', array('id' => $user));
2560 if (isset($cm->groupmode
) && empty($course->groupmodeforce
)) {
2561 $groupmode = $cm->groupmode
;
2563 $groupmode = $course->groupmode
;
2566 if ($groupmode == SEPARATEGROUPS
) {
2567 $groups = $DB->get_records_sql_menu("SELECT g.name, g.id
2568 FROM {groups} g, {groups_members} m
2569 WHERE g.courseid = ?
2570 AND g.id = m.groupid
2572 ORDER BY name ASC", array($course->id
, $user->id
));
2573 $groups = array_values($groups);
2575 $teachers = feedback_get_receivemail_users($cm->id
, $groups);
2577 $teachers = feedback_get_receivemail_users($cm->id
);
2582 $strfeedbacks = get_string('modulenameplural', 'feedback');
2583 $strfeedback = get_string('modulename', 'feedback');
2585 if ($feedback->anonymous
== FEEDBACK_ANONYMOUS_NO
) {
2586 $printusername = fullname($user);
2588 $printusername = get_string('anonymous_user', 'feedback');
2591 foreach ($teachers as $teacher) {
2592 $info = new stdClass();
2593 $info->username
= $printusername;
2594 $info->feedback
= format_string($feedback->name
, true);
2595 $info->url
= $CFG->wwwroot
.'/mod/feedback/show_entries.php?'.
2597 'userid=' . $user->id
;
2599 $info->url
.= '&showcompleted=' . $completed->id
;
2600 if ($feedback->course
== SITEID
) {
2601 // Course where feedback was completed (for site feedbacks only).
2602 $info->url
.= '&courseid=' . $completed->courseid
;
2606 $a = array('username' => $info->username
, 'feedbackname' => $feedback->name
);
2608 $postsubject = get_string('feedbackcompleted', 'feedback', $a);
2609 $posttext = feedback_send_email_text($info, $course);
2611 if ($teacher->mailformat
== 1) {
2612 $posthtml = feedback_send_email_html($info, $course, $cm);
2619 'instance' => $feedback->id
,
2621 if ($feedback->anonymous
== FEEDBACK_ANONYMOUS_NO
) {
2622 $eventdata = new \core\message\
message();
2623 $eventdata->anonymous
= false;
2624 $eventdata->courseid
= $course->id
;
2625 $eventdata->name
= 'submission';
2626 $eventdata->component
= 'mod_feedback';
2627 $eventdata->userfrom
= $user;
2628 $eventdata->userto
= $teacher;
2629 $eventdata->subject
= $postsubject;
2630 $eventdata->fullmessage
= $posttext;
2631 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
2632 $eventdata->fullmessagehtml
= $posthtml;
2633 $eventdata->smallmessage
= '';
2634 $eventdata->courseid
= $course->id
;
2635 $eventdata->contexturl
= $info->url
;
2636 $eventdata->contexturlname
= $info->feedback
;
2638 $userpicture = new user_picture($user);
2639 $userpicture->size
= 1; // Use f1 size.
2640 $userpicture->includetoken
= $teacher->id
; // Generate an out-of-session token for the user receiving the message.
2641 $customdata['notificationiconurl'] = $userpicture->get_url($PAGE)->out(false);
2642 $eventdata->customdata
= $customdata;
2643 message_send($eventdata);
2645 $eventdata = new \core\message\
message();
2646 $eventdata->anonymous
= true;
2647 $eventdata->courseid
= $course->id
;
2648 $eventdata->name
= 'submission';
2649 $eventdata->component
= 'mod_feedback';
2650 $eventdata->userfrom
= $teacher;
2651 $eventdata->userto
= $teacher;
2652 $eventdata->subject
= $postsubject;
2653 $eventdata->fullmessage
= $posttext;
2654 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
2655 $eventdata->fullmessagehtml
= $posthtml;
2656 $eventdata->smallmessage
= '';
2657 $eventdata->courseid
= $course->id
;
2658 $eventdata->contexturl
= $info->url
;
2659 $eventdata->contexturlname
= $info->feedback
;
2660 // Feedback icon if can be easily reachable.
2661 $customdata['notificationiconurl'] = ($cm instanceof cm_info
) ?
$cm->get_icon_url()->out() : '';
2662 $eventdata->customdata
= $customdata;
2663 message_send($eventdata);
2670 * sends an email to the teachers of the course where the given feedback is placed.
2673 * @uses FORMAT_PLAIN
2674 * @param object $cm the coursemodule-record
2675 * @param object $feedback
2676 * @param object $course
2679 function feedback_send_email_anonym($cm, $feedback, $course) {
2682 if ($feedback->email_notification
== 0) { // No need to do anything
2686 $teachers = feedback_get_receivemail_users($cm->id
);
2690 $strfeedbacks = get_string('modulenameplural', 'feedback');
2691 $strfeedback = get_string('modulename', 'feedback');
2692 $printusername = get_string('anonymous_user', 'feedback');
2694 foreach ($teachers as $teacher) {
2695 $info = new stdClass();
2696 $info->username
= $printusername;
2697 $info->feedback
= format_string($feedback->name
, true);
2698 $info->url
= $CFG->wwwroot
.'/mod/feedback/show_entries.php?id=' . $cm->id
;
2700 $a = array('username' => $info->username
, 'feedbackname' => $feedback->name
);
2702 $postsubject = get_string('feedbackcompleted', 'feedback', $a);
2703 $posttext = feedback_send_email_text($info, $course);
2705 if ($teacher->mailformat
== 1) {
2706 $posthtml = feedback_send_email_html($info, $course, $cm);
2711 $eventdata = new \core\message\
message();
2712 $eventdata->anonymous
= true;
2713 $eventdata->courseid
= $course->id
;
2714 $eventdata->name
= 'submission';
2715 $eventdata->component
= 'mod_feedback';
2716 $eventdata->userfrom
= $teacher;
2717 $eventdata->userto
= $teacher;
2718 $eventdata->subject
= $postsubject;
2719 $eventdata->fullmessage
= $posttext;
2720 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
2721 $eventdata->fullmessagehtml
= $posthtml;
2722 $eventdata->smallmessage
= '';
2723 $eventdata->courseid
= $course->id
;
2724 $eventdata->contexturl
= $info->url
;
2725 $eventdata->contexturlname
= $info->feedback
;
2726 $eventdata->customdata
= [
2728 'instance' => $feedback->id
,
2729 'notificationiconurl' => ($cm instanceof cm_info
) ?
$cm->get_icon_url()->out() : '', // Performance wise.
2732 message_send($eventdata);
2738 * send the text-part of the email
2740 * @param object $info includes some infos about the feedback you want to send
2741 * @param object $course
2742 * @return string the text you want to post
2744 function feedback_send_email_text($info, $course) {
2745 $coursecontext = context_course
::instance($course->id
);
2746 $courseshortname = format_string($course->shortname
, true, array('context' => $coursecontext));
2747 $posttext = $courseshortname.' -> '.get_string('modulenameplural', 'feedback').' -> '.
2748 $info->feedback
."\n";
2749 $posttext .= '---------------------------------------------------------------------'."\n";
2750 $posttext .= get_string("emailteachermail", "feedback", $info)."\n";
2751 $posttext .= '---------------------------------------------------------------------'."\n";
2757 * send the html-part of the email
2760 * @param object $info includes some infos about the feedback you want to send
2761 * @param object $course
2762 * @return string the text you want to post
2764 function feedback_send_email_html($info, $course, $cm) {
2766 $coursecontext = context_course
::instance($course->id
);
2767 $courseshortname = format_string($course->shortname
, true, array('context' => $coursecontext));
2768 $course_url = $CFG->wwwroot
.'/course/view.php?id='.$course->id
;
2769 $feedback_all_url = $CFG->wwwroot
.'/mod/feedback/index.php?id='.$course->id
;
2770 $feedback_url = $CFG->wwwroot
.'/mod/feedback/view.php?id='.$cm->id
;
2772 $posthtml = '<p><font face="sans-serif">'.
2773 '<a href="'.$course_url.'">'.$courseshortname.'</a> ->'.
2774 '<a href="'.$feedback_all_url.'">'.get_string('modulenameplural', 'feedback').'</a> ->'.
2775 '<a href="'.$feedback_url.'">'.$info->feedback
.'</a></font></p>';
2776 $posthtml .= '<hr /><font face="sans-serif">';
2777 $posthtml .= '<p>'.get_string('emailteachermailhtml', 'feedback', $info).'</p>';
2778 $posthtml .= '</font><hr />';
2783 * @param string $url
2786 function feedback_encode_target_url($url) {
2787 if (strpos($url, '?')) {
2788 list($part1, $part2) = explode('?', $url, 2); //maximal 2 parts
2789 return $part1 . '?' . htmlentities($part2, ENT_COMPAT
);
2796 * Adds module specific settings to the settings block
2798 * @param settings_navigation $settings The settings navigation object
2799 * @param navigation_node $feedbacknode The node to add module settings to
2801 function feedback_extend_settings_navigation(settings_navigation
$settings, navigation_node
$feedbacknode) {
2802 $hassecondary = $settings->get_page()->has_secondary_navigation();
2803 if (!$context = context_module
::instance($settings->get_page()->cm
->id
, IGNORE_MISSING
)) {
2804 throw new \
moodle_exception('badcontext');
2807 if (has_capability('mod/feedback:edititems', $context)) {
2808 $feedbacknode->add(get_string('questions', 'feedback'),
2809 new moodle_url('/mod/feedback/edit.php', ['id' => $settings->get_page()->cm
->id
]),
2810 navigation_node
::TYPE_CUSTOM
, null, 'questionnode');
2812 $feedbacknode->add(get_string('templates', 'feedback'),
2813 new moodle_url('/mod/feedback/manage_templates.php', ['id' => $settings->get_page()->cm
->id
, 'mode' => 'manage']),
2814 navigation_node
::TYPE_CUSTOM
, null, 'templatenode');
2817 if (has_capability('mod/feedback:mapcourse', $context) && $settings->get_page()->course
->id
== SITEID
) {
2818 $feedbacknode->add(get_string('mappedcourses', 'feedback'),
2819 new moodle_url('/mod/feedback/mapcourse.php', ['id' => $settings->get_page()->cm
->id
]),
2820 navigation_node
::TYPE_CUSTOM
, null, 'mapcourse');
2823 $feedback = $settings->get_page()->activityrecord
;
2824 if ($feedback->course
== SITEID
) {
2825 $analysisnode = navigation_node
::create(get_string('analysis', 'feedback'),
2826 new moodle_url('/mod/feedback/analysis_course.php', ['id' => $settings->get_page()->cm
->id
]),
2827 navigation_node
::TYPE_CUSTOM
, null, 'feedbackanalysis');
2829 $analysisnode = navigation_node
::create(get_string('analysis', 'feedback'),
2830 new moodle_url('/mod/feedback/analysis.php', ['id' => $settings->get_page()->cm
->id
]),
2831 navigation_node
::TYPE_CUSTOM
, null, 'feedbackanalysis');
2834 if (has_capability('mod/feedback:viewreports', $context)) {
2835 $feedbacknode->add_node($analysisnode);
2836 $feedbacknode->add(get_string(($hassecondary ?
'responses' : 'show_entries'), 'feedback'),
2837 new moodle_url('/mod/feedback/show_entries.php', ['id' => $settings->get_page()->cm
->id
]),
2838 navigation_node
::TYPE_CUSTOM
, null, 'responses');
2840 $feedbackcompletion = new mod_feedback_completion($feedback, $context, $settings->get_page()->course
->id
);
2841 if ($feedbackcompletion->can_view_analysis()) {
2842 $feedbacknode->add_node($analysisnode);
2847 function feedback_init_feedback_session() {
2848 //initialize the feedback-Session - not nice at all!!
2850 if (!empty($SESSION)) {
2851 if (!isset($SESSION->feedback
) OR !is_object($SESSION->feedback
)) {
2852 $SESSION->feedback
= new stdClass();
2858 * Return a list of page types
2859 * @param string $pagetype current page type
2860 * @param stdClass $parentcontext Block's parent context
2861 * @param stdClass $currentcontext Current context of block
2863 function feedback_page_type_list($pagetype, $parentcontext, $currentcontext) {
2864 $module_pagetype = array('mod-feedback-*'=>get_string('page-mod-feedback-x', 'feedback'));
2865 return $module_pagetype;
2869 * Move save the items of the given $feedback in the order of $itemlist.
2870 * @param string $itemlist a comma separated list with item ids
2871 * @param stdClass $feedback
2872 * @return bool true if success
2874 function feedback_ajax_saveitemorder($itemlist, $feedback) {
2879 foreach ($itemlist as $itemid) {
2881 $result = $result && $DB->set_field('feedback_item',
2884 array('id'=>$itemid, 'feedback'=>$feedback->id
));
2890 * Checks if current user is able to view feedback on this course.
2892 * @param stdClass $feedback
2893 * @param context_module $context
2894 * @param int $courseid
2897 function feedback_can_view_analysis($feedback, $context, $courseid = false) {
2898 if (has_capability('mod/feedback:viewreports', $context)) {
2902 if (intval($feedback->publish_stats
) != 1 ||
2903 !has_capability('mod/feedback:viewanalysepage', $context)) {
2907 if (!isloggedin() ||
isguestuser()) {
2908 // There is no tracking for the guests, assume that they can view analysis if condition above is satisfied.
2909 return $feedback->course
== SITEID
;
2912 return feedback_is_already_submitted($feedback->id
, $courseid);
2916 * Get icon mapping for font-awesome.
2918 function mod_feedback_get_fontawesome_icon_map() {
2920 'mod_feedback:notrequired' => 'fa-circle-question',
2921 'mod_feedback:required' => 'fa-circle-exclamation',
2926 * Check if the module has any update that affects the current user since a given time.
2928 * @param cm_info $cm course module data
2929 * @param int $from the time to check updates from
2930 * @param array $filter if we need to check only specific updates
2931 * @return stdClass an object with the different type of areas indicating if they were updated or not
2934 function feedback_check_updates_since(cm_info
$cm, $from, $filter = array()) {
2935 global $DB, $USER, $CFG;
2937 $updates = course_check_module_updates_since($cm, $from, array(), $filter);
2939 // Check for new attempts.
2940 $updates->attemptsfinished
= (object) array('updated' => false);
2941 $updates->attemptsunfinished
= (object) array('updated' => false);
2942 $select = 'feedback = ? AND userid = ? AND timemodified > ?';
2943 $params = array($cm->instance
, $USER->id
, $from);
2945 $attemptsfinished = $DB->get_records_select('feedback_completed', $select, $params, '', 'id');
2946 if (!empty($attemptsfinished)) {
2947 $updates->attemptsfinished
->updated
= true;
2948 $updates->attemptsfinished
->itemids
= array_keys($attemptsfinished);
2950 $attemptsunfinished = $DB->get_records_select('feedback_completedtmp', $select, $params, '', 'id');
2951 if (!empty($attemptsunfinished)) {
2952 $updates->attemptsunfinished
->updated
= true;
2953 $updates->attemptsunfinished
->itemids
= array_keys($attemptsunfinished);
2956 // Now, teachers should see other students updates.
2957 if (has_capability('mod/feedback:viewreports', $cm->context
)) {
2958 $select = 'feedback = ? AND timemodified > ?';
2959 $params = array($cm->instance
, $from);
2961 if (groups_get_activity_groupmode($cm) == SEPARATEGROUPS
) {
2962 $groupusers = array_keys(groups_get_activity_shared_group_members($cm));
2963 if (empty($groupusers)) {
2966 list($insql, $inparams) = $DB->get_in_or_equal($groupusers);
2967 $select .= ' AND userid ' . $insql;
2968 $params = array_merge($params, $inparams);
2971 $updates->userattemptsfinished
= (object) array('updated' => false);
2972 $attemptsfinished = $DB->get_records_select('feedback_completed', $select, $params, '', 'id');
2973 if (!empty($attemptsfinished)) {
2974 $updates->userattemptsfinished
->updated
= true;
2975 $updates->userattemptsfinished
->itemids
= array_keys($attemptsfinished);
2978 $updates->userattemptsunfinished
= (object) array('updated' => false);
2979 $attemptsunfinished = $DB->get_records_select('feedback_completedtmp', $select, $params, '', 'id');
2980 if (!empty($attemptsunfinished)) {
2981 $updates->userattemptsunfinished
->updated
= true;
2982 $updates->userattemptsunfinished
->itemids
= array_keys($attemptsunfinished);
2990 * This function receives a calendar event and returns the action associated with it, or null if there is none.
2992 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
2993 * is not displayed on the block.
2995 * @param calendar_event $event
2996 * @param \core_calendar\action_factory $factory
2997 * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
2998 * @return \core_calendar\local\event\entities\action_interface|null
3000 function mod_feedback_core_calendar_provide_event_action(calendar_event
$event,
3001 \core_calendar\action_factory
$factory,
3006 if (empty($userid)) {
3007 $userid = $USER->id
;
3010 $cm = get_fast_modinfo($event->courseid
, $userid)->instances
['feedback'][$event->instance
];
3012 if (!$cm->uservisible
) {
3013 // The module is not visible to the user for any reason.
3017 $completion = new \
completion_info($cm->get_course());
3019 $completiondata = $completion->get_data($cm, false, $userid);
3021 if ($completiondata->completionstate
!= COMPLETION_INCOMPLETE
) {
3025 $feedbackcompletion = new mod_feedback_completion(null, $cm, 0, false, null, null, $userid);
3027 if (!empty($cm->customdata
['timeclose']) && $cm->customdata
['timeclose'] < time()) {
3028 // Feedback is already closed, do not display it even if it was never submitted.
3032 if (!$feedbackcompletion->can_complete()) {
3033 // The user can't complete the feedback so there is no action for them.
3037 // The feedback is actionable if it does not have timeopen or timeopen is in the past.
3038 $actionable = $feedbackcompletion->is_open();
3040 if ($actionable && $feedbackcompletion->is_already_submitted(false)) {
3041 // There is no need to display anything if the user has already submitted the feedback.
3045 return $factory->create_instance(
3046 get_string('answerquestions', 'feedback'),
3047 new \
moodle_url('/mod/feedback/view.php', ['id' => $cm->id
]),
3054 * Add a get_coursemodule_info function in case any feedback type wants to add 'extra' information
3055 * for the course (see resource).
3057 * Given a course_module object, this function returns any "extra" information that may be needed
3058 * when printing this activity in a course listing. See get_array_of_activities() in course/lib.php.
3060 * @param stdClass $coursemodule The coursemodule object (record).
3061 * @return cached_cm_info An object on information that the courses
3062 * will know about (most noticeably, an icon).
3064 function feedback_get_coursemodule_info($coursemodule) {
3067 $dbparams = ['id' => $coursemodule->instance
];
3068 $fields = 'id, name, intro, introformat, completionsubmit, timeopen, timeclose, anonymous';
3069 if (!$feedback = $DB->get_record('feedback', $dbparams, $fields)) {
3073 $result = new cached_cm_info();
3074 $result->name
= $feedback->name
;
3076 if ($coursemodule->showdescription
) {
3077 // Convert intro to html. Do not filter cached version, filters run at display time.
3078 $result->content
= format_module_intro('feedback', $feedback, $coursemodule->id
, false);
3081 // Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'.
3082 if ($coursemodule->completion
== COMPLETION_TRACKING_AUTOMATIC
) {
3083 $result->customdata
['customcompletionrules']['completionsubmit'] = $feedback->completionsubmit
;
3085 // Populate some other values that can be used in calendar or on dashboard.
3086 if ($feedback->timeopen
) {
3087 $result->customdata
['timeopen'] = $feedback->timeopen
;
3089 if ($feedback->timeclose
) {
3090 $result->customdata
['timeclose'] = $feedback->timeclose
;
3092 if ($feedback->anonymous
) {
3093 $result->customdata
['anonymous'] = $feedback->anonymous
;
3100 * Callback which returns human-readable strings describing the active completion custom rules for the module instance.
3102 * @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules']
3103 * @return array $descriptions the array of descriptions for the custom rules.
3105 function mod_feedback_get_completion_active_rule_descriptions($cm) {
3106 // Values will be present in cm_info, and we assume these are up to date.
3107 if (empty($cm->customdata
['customcompletionrules'])
3108 ||
$cm->completion
!= COMPLETION_TRACKING_AUTOMATIC
) {
3113 foreach ($cm->customdata
['customcompletionrules'] as $key => $val) {
3115 case 'completionsubmit':
3117 $descriptions[] = get_string('completionsubmit', 'feedback');
3124 return $descriptions;
3128 * This function calculates the minimum and maximum cutoff values for the timestart of
3131 * It will return an array with two values, the first being the minimum cutoff value and
3132 * the second being the maximum cutoff value. Either or both values can be null, which
3133 * indicates there is no minimum or maximum, respectively.
3135 * If a cutoff is required then the function must return an array containing the cutoff
3136 * timestamp and error string to display to the user if the cutoff value is violated.
3138 * A minimum and maximum cutoff return value will look like:
3140 * [1505704373, 'The due date must be after the sbumission start date'],
3141 * [1506741172, 'The due date must be before the cutoff date']
3144 * @param calendar_event $event The calendar event to get the time range for
3145 * @param stdClass $instance The module instance to get the range from
3148 function mod_feedback_core_calendar_get_valid_event_timestart_range(\calendar_event
$event, \stdClass
$instance) {
3152 if ($event->eventtype
== FEEDBACK_EVENT_TYPE_OPEN
) {
3153 // The start time of the open event can't be equal to or after the
3154 // close time of the choice activity.
3155 if (!empty($instance->timeclose
)) {
3157 $instance->timeclose
,
3158 get_string('openafterclose', 'feedback')
3161 } else if ($event->eventtype
== FEEDBACK_EVENT_TYPE_CLOSE
) {
3162 // The start time of the close event can't be equal to or earlier than the
3163 // open time of the choice activity.
3164 if (!empty($instance->timeopen
)) {
3166 $instance->timeopen
,
3167 get_string('closebeforeopen', 'feedback')
3172 return [$mindate, $maxdate];
3176 * This function will update the feedback module according to the
3177 * event that has been modified.
3179 * It will set the timeopen or timeclose value of the feedback instance
3180 * according to the type of event provided.
3182 * @throws \moodle_exception
3183 * @param \calendar_event $event
3184 * @param stdClass $feedback The module instance to get the range from
3186 function mod_feedback_core_calendar_event_timestart_updated(\calendar_event
$event, \stdClass
$feedback) {
3189 if (empty($event->instance
) ||
$event->modulename
!= 'feedback') {
3193 if ($event->instance
!= $feedback->id
) {
3197 if (!in_array($event->eventtype
, [FEEDBACK_EVENT_TYPE_OPEN
, FEEDBACK_EVENT_TYPE_CLOSE
])) {
3201 $courseid = $event->courseid
;
3202 $modulename = $event->modulename
;
3203 $instanceid = $event->instance
;
3206 $coursemodule = get_fast_modinfo($courseid)->instances
[$modulename][$instanceid];
3207 $context = context_module
::instance($coursemodule->id
);
3209 // The user does not have the capability to modify this activity.
3210 if (!has_capability('moodle/course:manageactivities', $context)) {
3214 if ($event->eventtype
== FEEDBACK_EVENT_TYPE_OPEN
) {
3215 // If the event is for the feedback activity opening then we should
3216 // set the start time of the feedback activity to be the new start
3217 // time of the event.
3218 if ($feedback->timeopen
!= $event->timestart
) {
3219 $feedback->timeopen
= $event->timestart
;
3220 $feedback->timemodified
= time();
3223 } else if ($event->eventtype
== FEEDBACK_EVENT_TYPE_CLOSE
) {
3224 // If the event is for the feedback activity closing then we should
3225 // set the end time of the feedback activity to be the new start
3226 // time of the event.
3227 if ($feedback->timeclose
!= $event->timestart
) {
3228 $feedback->timeclose
= $event->timestart
;
3234 $feedback->timemodified
= time();
3235 $DB->update_record('feedback', $feedback);
3236 $event = \core\event\course_module_updated
::create_from_cm($coursemodule, $context);
3242 * Callback to fetch the activity event type lang string.
3244 * @param string $eventtype The event type.
3245 * @return lang_string The event type lang string.
3247 function mod_feedback_core_calendar_get_event_action_string(string $eventtype): string {
3248 $modulename = get_string('modulename', 'feedback');
3250 switch ($eventtype) {
3251 case FEEDBACK_EVENT_TYPE_OPEN
:
3252 $identifier = 'calendarstart';
3254 case FEEDBACK_EVENT_TYPE_CLOSE
:
3255 $identifier = 'calendarend';
3258 return get_string('requiresaction', 'calendar', $modulename);
3261 return get_string($identifier, 'feedback', $modulename);