MDL-40047 events: Add unit tests for feedback events
[moodle.git] / mod / feedback / lib.php
CommitLineData
32be99dc 1<?php
32be99dc 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
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/>.
16
17/**
18 * Library of functions and constants for module feedback
19 * includes the main-part of feedback-functions
20 *
21 * @package mod-feedback
22 * @copyright Andreas Grabs
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26/** Include eventslib.php */
3b120e46 27require_once($CFG->libdir.'/eventslib.php');
9f42276c
SH
28/** Include calendar/lib.php */
29require_once($CFG->dirroot.'/calendar/lib.php');
9c515cdc
AA
30// Include forms lib.
31require_once($CFG->libdir.'/formslib.php');
c70ad9f7 32
c70ad9f7 33define('FEEDBACK_ANONYMOUS_YES', 1);
34define('FEEDBACK_ANONYMOUS_NO', 2);
35define('FEEDBACK_MIN_ANONYMOUS_COUNT_IN_GROUP', 2);
36define('FEEDBACK_DECIMAL', '.');
37define('FEEDBACK_THOUSAND', ',');
38define('FEEDBACK_RESETFORM_RESET', 'feedback_reset_data_');
39define('FEEDBACK_RESETFORM_DROP', 'feedback_drop_feedback_');
40define('FEEDBACK_MAX_PIX_LENGTH', '400'); //max. Breite des grafischen Balkens in der Auswertung
8a23b38c 41define('FEEDBACK_DEFAULT_PAGE_COUNT', 20);
c70ad9f7 42
06ca6cc9
TH
43/**
44 * Returns all other caps used in module.
45 *
46 * @return array
47 */
48function feedback_get_extra_capabilities() {
49 return array('moodle/site:accessallgroups');
50}
51
29081d02 52/**
32be99dc 53 * @uses FEATURE_GROUPS
54 * @uses FEATURE_GROUPINGS
55 * @uses FEATURE_GROUPMEMBERSONLY
56 * @uses FEATURE_MOD_INTRO
57 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
58 * @uses FEATURE_GRADE_HAS_GRADE
59 * @uses FEATURE_GRADE_OUTCOMES
29081d02 60 * @param string $feature FEATURE_xx constant for requested feature
61 * @return mixed True if module supports feature, null if doesn't know
62 */
63function feedback_supports($feature) {
64 switch($feature) {
42f103be 65 case FEATURE_GROUPS: return true;
66 case FEATURE_GROUPINGS: return true;
67 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 68 case FEATURE_MOD_INTRO: return true;
29081d02 69 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
8a41eb4f 70 case FEATURE_COMPLETION_HAS_RULES: return true;
42f103be 71 case FEATURE_GRADE_HAS_GRADE: return false;
72 case FEATURE_GRADE_OUTCOMES: return false;
cb3dcd78 73 case FEATURE_BACKUP_MOODLE2: return true;
3e4c2435 74 case FEATURE_SHOW_DESCRIPTION: return true;
42f103be 75
29081d02 76 default: return null;
77 }
78}
3b120e46 79
c70ad9f7 80/**
32be99dc 81 * this will create a new instance and return the id number
82 * of the new instance.
83 *
84 * @global object
85 * @param object $feedback the object given by mod_feedback_mod_form
86 * @return int
87 */
c70ad9f7 88function feedback_add_instance($feedback) {
c18269c7 89 global $DB;
c70ad9f7 90
91 $feedback->timemodified = time();
92 $feedback->id = '';
93
9c5bc7a5 94 if (empty($feedback->site_after_submit)) {
c184660d 95 $feedback->site_after_submit = '';
96 }
c70ad9f7 97
98 //saving the feedback in db
a8f3a651 99 $feedbackid = $DB->insert_record("feedback", $feedback);
37b86a39 100
101 $feedback->id = $feedbackid;
102
c70ad9f7 103 feedback_set_events($feedback);
37b86a39 104
abd2899c
AG
105 if (!isset($feedback->coursemodule)) {
106 $cm = get_coursemodule_from_id('feedback', $feedback->id);
107 $feedback->coursemodule = $cm->id;
108 }
2c979976 109 $context = context_module::instance($feedback->coursemodule);
abd2899c
AG
110
111 $editoroptions = feedback_get_editor_options();
112
113 // process the custom wysiwyg editor in page_after_submit
114 if ($draftitemid = $feedback->page_after_submit_editor['itemid']) {
115 $feedback->page_after_submit = file_save_draft_area_files($draftitemid, $context->id,
116 'mod_feedback', 'page_after_submit',
117 0, $editoroptions,
118 $feedback->page_after_submit_editor['text']);
119
120 $feedback->page_after_submitformat = $feedback->page_after_submit_editor['format'];
121 }
122 $DB->update_record('feedback', $feedback);
123
c70ad9f7 124 return $feedbackid;
125}
126
127/**
32be99dc 128 * this will update a given instance
129 *
130 * @global object
131 * @param object $feedback the object given by mod_feedback_mod_form
132 * @return boolean
133 */
c70ad9f7 134function feedback_update_instance($feedback) {
c18269c7 135 global $DB;
c70ad9f7 136
137 $feedback->timemodified = time();
138 $feedback->id = $feedback->instance;
139
9c5bc7a5 140 if (empty($feedback->site_after_submit)) {
c184660d 141 $feedback->site_after_submit = '';
142 }
c70ad9f7 143
144 //save the feedback into the db
9d749339 145 $DB->update_record("feedback", $feedback);
c70ad9f7 146
147 //create or update the new events
148 feedback_set_events($feedback);
37b86a39 149
2c979976 150 $context = context_module::instance($feedback->coursemodule);
abd2899c
AG
151
152 $editoroptions = feedback_get_editor_options();
153
154 // process the custom wysiwyg editor in page_after_submit
155 if ($draftitemid = $feedback->page_after_submit_editor['itemid']) {
156 $feedback->page_after_submit = file_save_draft_area_files($draftitemid, $context->id,
157 'mod_feedback', 'page_after_submit',
158 0, $editoroptions,
159 $feedback->page_after_submit_editor['text']);
160
161 $feedback->page_after_submitformat = $feedback->page_after_submit_editor['format'];
162 }
163 $DB->update_record('feedback', $feedback);
164
c70ad9f7 165 return true;
166}
167
a59ff6b0
AG
168/**
169 * Serves the files included in feedback items like label. Implements needed access control ;-)
170 *
485ec594
AG
171 * There are two situations in general where the files will be sent.
172 * 1) filearea = item, 2) filearea = template
173 *
d2b7803e
DC
174 * @package mod_feedback
175 * @category files
176 * @param stdClass $course course object
177 * @param stdClass $cm course module object
178 * @param stdClass $context context object
179 * @param string $filearea file area
180 * @param array $args extra arguments
181 * @param bool $forcedownload whether or not force download
261cbbac 182 * @param array $options additional options affecting the file serving
a59ff6b0
AG
183 * @return bool false if file not found, does not return if found - justsend the file
184 */
261cbbac 185function feedback_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
a59ff6b0
AG
186 global $CFG, $DB;
187
abd2899c
AG
188 if ($filearea === 'item' or $filearea === 'template') {
189 $itemid = (int)array_shift($args);
190 //get the item what includes the file
191 if (!$item = $DB->get_record('feedback_item', array('id'=>$itemid))) {
192 return false;
193 }
194 $feedbackid = $item->feedback;
195 $templateid = $item->template;
32d2ddab 196 }
22ca3e49 197
abd2899c
AG
198 if ($filearea === 'page_after_submit' or $filearea === 'item') {
199 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
485ec594
AG
200 return false;
201 }
a59ff6b0 202
abd2899c
AG
203 $feedbackid = $feedback->id;
204
205 //if the filearea is "item" so we check the permissions like view/complete the feedback
485ec594
AG
206 $canload = false;
207 //first check whether the user has the complete capability
9c5bc7a5 208 if (has_capability('mod/feedback:complete', $context)) {
485ec594
AG
209 $canload = true;
210 }
22ca3e49 211
485ec594 212 //now we check whether the user has the view capability
9c5bc7a5 213 if (has_capability('mod/feedback:view', $context)) {
485ec594
AG
214 $canload = true;
215 }
22ca3e49 216
485ec594
AG
217 //if the feedback is on frontpage and anonymous and the fullanonymous is allowed
218 //so the file can be loaded too.
9c5bc7a5 219 if (isset($CFG->feedback_allowfullanonymous)
485ec594
AG
220 AND $CFG->feedback_allowfullanonymous
221 AND $course->id == SITEID
222 AND $feedback->anonymous == FEEDBACK_ANONYMOUS_YES ) {
223 $canload = true;
224 }
225
9c5bc7a5 226 if (!$canload) {
485ec594
AG
227 return false;
228 }
9c5bc7a5 229 } else if ($filearea === 'template') { //now we check files in templates
abd2899c 230 if (!$template = $DB->get_record('feedback_template', array('id'=>$templateid))) {
485ec594
AG
231 return false;
232 }
22ca3e49 233
485ec594 234 //if the file is not public so the capability edititems has to be there
9c5bc7a5
AG
235 if (!$template->ispublic) {
236 if (!has_capability('mod/feedback:edititems', $context)) {
485ec594
AG
237 return false;
238 }
9c5bc7a5
AG
239 } else { //on public templates, at least the user has to be logged in
240 if (!isloggedin()) {
485ec594
AG
241 return false;
242 }
243 }
9c5bc7a5 244 } else {
32d2ddab
AG
245 return false;
246 }
a59ff6b0 247
32d2ddab 248 if ($context->contextlevel == CONTEXT_MODULE) {
abd2899c 249 if ($filearea !== 'item' and $filearea !== 'page_after_submit') {
a59ff6b0
AG
250 return false;
251 }
32d2ddab 252 }
a59ff6b0 253
f35c4500 254 if ($context->contextlevel == CONTEXT_COURSE || $context->contextlevel == CONTEXT_SYSTEM) {
32d2ddab 255 if ($filearea !== 'template') {
a59ff6b0
AG
256 return false;
257 }
32d2ddab 258 }
0f21a964 259
32d2ddab 260 $relativepath = implode('/', $args);
abd2899c
AG
261 if ($filearea === 'page_after_submit') {
262 $fullpath = "/{$context->id}/mod_feedback/$filearea/$relativepath";
263 } else {
264 $fullpath = "/{$context->id}/mod_feedback/$filearea/{$item->id}/$relativepath";
265 }
a59ff6b0 266
32d2ddab 267 $fs = get_file_storage();
abd2899c 268
32d2ddab
AG
269 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
270 return false;
a59ff6b0
AG
271 }
272
32d2ddab 273 // finally send the file
261cbbac 274 send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
32d2ddab 275
a59ff6b0
AG
276 return false;
277}
278
c70ad9f7 279/**
32be99dc 280 * this will delete a given instance.
281 * all referenced data also will be deleted
282 *
283 * @global object
284 * @param int $id the instanceid of feedback
285 * @return boolean
286 */
c70ad9f7 287function feedback_delete_instance($id) {
c18269c7 288 global $DB;
289
c70ad9f7 290 //get all referenced items
c18269c7 291 $feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$id));
37b86a39 292
c70ad9f7 293 //deleting all referenced items and values
9c5bc7a5
AG
294 if (is_array($feedbackitems)) {
295 foreach ($feedbackitems as $feedbackitem) {
c18269c7 296 $DB->delete_records("feedback_value", array("item"=>$feedbackitem->id));
297 $DB->delete_records("feedback_valuetmp", array("item"=>$feedbackitem->id));
c70ad9f7 298 }
9c5bc7a5
AG
299 if ($delitems = $DB->get_records("feedback_item", array("feedback"=>$id))) {
300 foreach ($delitems as $delitem) {
14832313
AG
301 feedback_delete_item($delitem->id, false);
302 }
303 }
c70ad9f7 304 }
37b86a39 305
c70ad9f7 306 //deleting the referenced tracking data
c18269c7 307 $DB->delete_records('feedback_tracking', array('feedback'=>$id));
37b86a39 308
c70ad9f7 309 //deleting the completeds
c18269c7 310 $DB->delete_records("feedback_completed", array("feedback"=>$id));
37b86a39 311
c70ad9f7 312 //deleting the unfinished completeds
c18269c7 313 $DB->delete_records("feedback_completedtmp", array("feedback"=>$id));
37b86a39 314
c70ad9f7 315 //deleting old events
c18269c7 316 $DB->delete_records('event', array('modulename'=>'feedback', 'instance'=>$id));
317 return $DB->delete_records("feedback", array("id"=>$id));
c70ad9f7 318}
319
320/**
32be99dc 321 * this is called after deleting all instances if the course will be deleted.
322 * only templates have to be deleted
323 *
324 * @global object
325 * @param object $course
326 * @return boolean
327 */
c70ad9f7 328function feedback_delete_course($course) {
0085fff8 329 global $DB;
330
c70ad9f7 331 //delete all templates of given course
0085fff8 332 return $DB->delete_records('feedback_template', array('course'=>$course->id));
c70ad9f7 333}
334
335/**
32be99dc 336 * Return a small object with summary information about what a
337 * user has done with a given particular instance of this module
338 * Used for user activity reports.
339 * $return->time = the time they did it
340 * $return->info = a short text description
341 *
342 * @param object $course
343 * @param object $user
344 * @param object $mod
345 * @param object $feedback
346 * @return object
347 */
c70ad9f7 348function feedback_user_outline($course, $user, $mod, $feedback) {
0085fff8 349 return null;
c70ad9f7 350}
351
6133bd16 352/**
353 * Returns all users who has completed a specified feedback since a given time
354 * many thanks to Manolescu Dorel, who contributed these two functions
32be99dc 355 *
356 * @global object
357 * @global object
358 * @global object
359 * @global object
360 * @uses CONTEXT_MODULE
361 * @param array $activities Passed by reference
362 * @param int $index Passed by reference
363 * @param int $timemodified Timestamp
364 * @param int $courseid
365 * @param int $cmid
366 * @param int $userid
367 * @param int $groupid
368 * @return void
6133bd16 369 */
9c5bc7a5
AG
370function feedback_get_recent_mod_activity(&$activities, &$index,
371 $timemodified, $courseid,
372 $cmid, $userid="", $groupid="") {
373
6133bd16 374 global $CFG, $COURSE, $USER, $DB;
375
376 if ($COURSE->id == $courseid) {
377 $course = $COURSE;
378 } else {
76b7b592 379 $course = $DB->get_record('course', array('id'=>$courseid));
6133bd16 380 }
381
f20edd52 382 $modinfo = get_fast_modinfo($course);
6133bd16 383
384 $cm = $modinfo->cms[$cmid];
6938191b 385
69f6d0b4 386 $sqlargs = array();
6133bd16 387
3a11c09f
PS
388 //TODO: user user_picture::fields;
389 $sql = " SELECT fk . * , fc . * , u.firstname, u.lastname, u.email, u.picture, u.email
69f6d0b4
AG
390 FROM {feedback_completed} fc
391 JOIN {feedback} fk ON fk.id = fc.feedback
392 JOIN {user} u ON u.id = fc.userid ";
6133bd16 393
394 if ($groupid) {
69f6d0b4 395 $sql .= " JOIN {groups_members} gm ON gm.userid=u.id ";
6133bd16 396 }
6938191b 397
69f6d0b4
AG
398 $sql .= " WHERE fc.timemodified > ? AND fk.id = ? ";
399 $sqlargs[] = $timemodified;
fdd375e1 400 $sqlargs[] = $cm->instance;
d4b1d58c 401
69f6d0b4
AG
402 if ($userid) {
403 $sql .= " AND u.id = ? ";
404 $sqlargs[] = $userid;
6133bd16 405 }
406
69f6d0b4
AG
407 if ($groupid) {
408 $sql .= " AND gm.groupid = ? ";
409 $sqlargs[] = $groupid;
410 }
6938191b 411
69f6d0b4
AG
412 if (!$feedbackitems = $DB->get_records_sql($sql, $sqlargs)) {
413 return;
414 }
6133bd16 415
09c2ec43
AG
416 $cm_context = context_module::instance($cm->id);
417
418 if (!has_capability('mod/feedback:view', $cm_context)) {
419 return;
420 }
421
6133bd16 422 $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
423 $viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context);
424 $groupmode = groups_get_activity_groupmode($cm, $course);
425
426 if (is_null($modinfo->groups)) {
9c5bc7a5
AG
427 // load all my groups and cache it in modinfo
428 $modinfo->groups = groups_get_user_groups($course->id);
6133bd16 429 }
430
9c5bc7a5 431 $aname = format_string($cm->name, true);
6133bd16 432 foreach ($feedbackitems as $feedbackitem) {
433 if ($feedbackitem->userid != $USER->id) {
d4b1d58c 434
435 if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
9c5bc7a5
AG
436 $usersgroups = groups_get_all_groups($course->id,
437 $feedbackitem->userid,
438 $cm->groupingid);
6133bd16 439 if (!is_array($usersgroups)) {
440 continue;
441 }
442 $usersgroups = array_keys($usersgroups);
a6807ceb 443 $intersect = array_intersect($usersgroups, $modinfo->groups[$cm->id]);
6133bd16 444 if (empty($intersect)) {
445 continue;
446 }
447 }
9c5bc7a5 448 }
6133bd16 449
39790bd8 450 $tmpactivity = new stdClass();
6133bd16 451
452 $tmpactivity->type = 'feedback';
453 $tmpactivity->cmid = $cm->id;
454 $tmpactivity->name = $aname;
455 $tmpactivity->sectionnum= $cm->sectionnum;
456 $tmpactivity->timestamp = $feedbackitem->timemodified;
d4b1d58c 457
82e4dc33 458 $tmpactivity->content = new stdClass();
6133bd16 459 $tmpactivity->content->feedbackid = $feedbackitem->id;
460 $tmpactivity->content->feedbackuserid = $feedbackitem->userid;
d4b1d58c 461
82e4dc33
AG
462 $userfields = explode(',', user_picture::fields());
463 $tmpactivity->user = new stdClass();
464 foreach ($userfields as $userfield) {
465 if ($userfield == 'id') {
466 $tmpactivity->user->{$userfield} = $feedbackitem->userid; // aliased in SQL above
467 } else {
468 if (!empty($feedbackitem->{$userfield})) {
469 $tmpactivity->user->{$userfield} = $feedbackitem->{$userfield};
470 } else {
471 $tmpactivity->user->{$userfield} = null;
472 }
473 }
474 }
6133bd16 475 $tmpactivity->user->fullname = fullname($feedbackitem, $viewfullnames);
d4b1d58c 476
6133bd16 477 $activities[$index++] = $tmpactivity;
478 }
479
9c5bc7a5 480 return;
6133bd16 481}
482
483/**
484 * Prints all users who has completed a specified feedback since a given time
485 * many thanks to Manolescu Dorel, who contributed these two functions
32be99dc 486 *
487 * @global object
488 * @param object $activity
489 * @param int $courseid
490 * @param string $detail
491 * @param array $modnames
492 * @return void Output is echo'd
6133bd16 493 */
494function feedback_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
e63f88c9 495 global $CFG, $OUTPUT;
6133bd16 496
497 echo '<table border="0" cellpadding="3" cellspacing="0" class="forum-recent">';
498
499 echo "<tr><td class=\"userpicture\" valign=\"top\">";
812dbaf7 500 echo $OUTPUT->user_picture($activity->user, array('courseid'=>$courseid));
6133bd16 501 echo "</td><td>";
502
503 if ($detail) {
504 $modname = $modnames[$activity->type];
505 echo '<div class="title">';
b5d0cafc 506 echo "<img src=\"" . $OUTPUT->pix_url('icon', $activity->type) . "\" ".
6133bd16 507 "class=\"icon\" alt=\"$modname\" />";
508 echo "<a href=\"$CFG->wwwroot/mod/feedback/view.php?id={$activity->cmid}\">{$activity->name}</a>";
509 echo '</div>';
510 }
d4b1d58c 511
78946b9b 512 echo '<div class="title">';
6133bd16 513 echo '</div>';
d4b1d58c 514
6133bd16 515 echo '<div class="user">';
82e4dc33 516 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&amp;course=$courseid\">"
6133bd16 517 ."{$activity->user->fullname}</a> - ".userdate($activity->timestamp);
518 echo '</div>';
519
520 echo "</td></tr></table>";
521
522 return;
523}
524
8a41eb4f
AG
525/**
526 * Obtains the automatic completion state for this feedback based on the condition
527 * in feedback settings.
528 *
529 * @param object $course Course
530 * @param object $cm Course-module
531 * @param int $userid User ID
532 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
533 * @return bool True if completed, false if not, $type if conditions not set.
534 */
535function feedback_get_completion_state($course, $cm, $userid, $type) {
536 global $CFG, $DB;
537
538 // Get feedback details
539 $feedback = $DB->get_record('feedback', array('id'=>$cm->instance), '*', MUST_EXIST);
540
516c5eca 541 // If completion option is enabled, evaluate it and return true/false
9c5bc7a5
AG
542 if ($feedback->completionsubmit) {
543 $params = array('userid'=>$userid, 'feedback'=>$feedback->id);
544 return $DB->record_exists('feedback_tracking', $params);
8a41eb4f
AG
545 } else {
546 // Completion option is not enabled so just return $type
547 return $type;
548 }
549}
550
6133bd16 551
c70ad9f7 552/**
32be99dc 553 * Print a detailed representation of what a user has done with
554 * a given particular instance of this module, for user activity reports.
d4b1d58c 555 *
32be99dc 556 * @param object $course
557 * @param object $user
558 * @param object $mod
559 * @param object $feedback
560 * @return bool
561 */
c70ad9f7 562function feedback_user_complete($course, $user, $mod, $feedback) {
c70ad9f7 563 return true;
564}
565
32be99dc 566/**
567 * @return bool true
568 */
c70ad9f7 569function feedback_cron () {
570 return true;
571}
572
32be99dc 573/**
574 * @return bool false
575 */
9c5bc7a5 576function feedback_scale_used ($feedbackid, $scaleid) {
0085fff8 577 return false;
c70ad9f7 578}
579
c27bb7ca
AG
580/**
581 * Checks if scale is being used by any instance of feedback
582 *
583 * This is used to find out if scale used anywhere
584 * @param $scaleid int
585 * @return boolean True if the scale is used by any assignment
586 */
587function feedback_scale_used_anywhere($scaleid) {
588 return false;
589}
590
da457a94
AG
591/**
592 * @return array
593 */
594function feedback_get_view_actions() {
9c5bc7a5 595 return array('view', 'view all');
da457a94
AG
596}
597
598/**
599 * @return array
600 */
601function feedback_get_post_actions() {
602 return array('submit');
603}
604
1c57ce25 605/**
606 * This function is used by the reset_course_userdata function in moodlelib.
607 * This function will remove all responses from the specified feedback
608 * and clean up any related data.
32be99dc 609 *
610 * @global object
611 * @global object
612 * @uses FEEDBACK_RESETFORM_RESET
613 * @uses FEEDBACK_RESETFORM_DROP
614 * @param object $data the data submitted from the reset course.
1c57ce25 615 * @return array status array
c70ad9f7 616 */
1c57ce25 617function feedback_reset_userdata($data) {
0085fff8 618 global $CFG, $DB;
37b86a39 619
c70ad9f7 620 $resetfeedbacks = array();
621 $dropfeedbacks = array();
1c57ce25 622 $status = array();
623 $componentstr = get_string('modulenameplural', 'feedback');
37b86a39 624
c70ad9f7 625 //get the relevant entries from $data
9c5bc7a5 626 foreach ($data as $key => $value) {
c70ad9f7 627 switch(true) {
628 case substr($key, 0, strlen(FEEDBACK_RESETFORM_RESET)) == FEEDBACK_RESETFORM_RESET:
9c5bc7a5 629 if ($value == 1) {
c70ad9f7 630 $templist = explode('_', $key);
9c5bc7a5
AG
631 if (isset($templist[3])) {
632 $resetfeedbacks[] = intval($templist[3]);
633 }
c70ad9f7 634 }
635 break;
636 case substr($key, 0, strlen(FEEDBACK_RESETFORM_DROP)) == FEEDBACK_RESETFORM_DROP:
9c5bc7a5 637 if ($value == 1) {
c70ad9f7 638 $templist = explode('_', $key);
9c5bc7a5
AG
639 if (isset($templist[3])) {
640 $dropfeedbacks[] = intval($templist[3]);
641 }
c70ad9f7 642 }
643 break;
644 }
645 }
37b86a39 646
c70ad9f7 647 //reset the selected feedbacks
9c5bc7a5 648 foreach ($resetfeedbacks as $id) {
0085fff8 649 $feedback = $DB->get_record('feedback', array('id'=>$id));
c70ad9f7 650 feedback_delete_all_completeds($id);
9c5bc7a5
AG
651 $status[] = array('component'=>$componentstr.':'.$feedback->name,
652 'item'=>get_string('resetting_data', 'feedback'),
653 'error'=>false);
c70ad9f7 654 }
37b86a39 655
1c57ce25 656 return $status;
657}
658
659/**
660 * Called by course/reset.php
32be99dc 661 *
662 * @global object
663 * @uses FEEDBACK_RESETFORM_RESET
664 * @param object $mform form passed by reference
1c57ce25 665 */
666function feedback_reset_course_form_definition(&$mform) {
0085fff8 667 global $COURSE, $DB;
668
1c57ce25 669 $mform->addElement('header', 'feedbackheader', get_string('modulenameplural', 'feedback'));
37b86a39 670
9c5bc7a5 671 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$COURSE->id), 'name')) {
1c57ce25 672 return;
c70ad9f7 673 }
1c57ce25 674
9c5bc7a5
AG
675 $mform->addElement('static', 'hint', get_string('resetting_data', 'feedback'));
676 foreach ($feedbacks as $feedback) {
1c57ce25 677 $mform->addElement('checkbox', FEEDBACK_RESETFORM_RESET.$feedback->id, $feedback->name);
1c57ce25 678 }
679}
680
681/**
682 * Course reset form defaults.
32be99dc 683 *
684 * @global object
685 * @uses FEEDBACK_RESETFORM_RESET
686 * @param object $course
1c57ce25 687 */
688function feedback_reset_course_form_defaults($course) {
0085fff8 689 global $DB;
690
1c57ce25 691 $return = array();
9c5bc7a5 692 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id), 'name')) {
1c57ce25 693 return;
694 }
9c5bc7a5 695 foreach ($feedbacks as $feedback) {
1c57ce25 696 $return[FEEDBACK_RESETFORM_RESET.$feedback->id] = true;
1c57ce25 697 }
698 return $return;
c70ad9f7 699}
700
37b86a39 701/**
c70ad9f7 702 * Called by course/reset.php and shows the formdata by coursereset.
703 * it prints checkboxes for each feedback available at the given course
9c5bc7a5
AG
704 * there are two checkboxes:
705 * 1) delete userdata and keep the feedback
706 * 2) delete userdata and drop the feedback
32be99dc 707 *
708 * @global object
709 * @uses FEEDBACK_RESETFORM_RESET
710 * @uses FEEDBACK_RESETFORM_DROP
c70ad9f7 711 * @param object $course
712 * @return void
713 */
714function feedback_reset_course_form($course) {
be96a472 715 global $DB, $OUTPUT;
0085fff8 716
c70ad9f7 717 echo get_string('resetting_feedbacks', 'feedback'); echo ':<br />';
0085fff8 718 if (!$feedbacks = $DB->get_records('feedback', array('course'=>$course->id), 'name')) {
719 return;
720 }
37b86a39 721
9c5bc7a5 722 foreach ($feedbacks as $feedback) {
c70ad9f7 723 echo '<p>';
9c5bc7a5
AG
724 echo get_string('name', 'feedback').': '.$feedback->name.'<br />';
725 echo html_writer::checkbox(FEEDBACK_RESETFORM_RESET.$feedback->id,
726 1, true,
727 get_string('resetting_data', 'feedback'));
be96a472 728 echo '<br />';
9c5bc7a5
AG
729 echo html_writer::checkbox(FEEDBACK_RESETFORM_DROP.$feedback->id,
730 1, false,
731 get_string('drop_feedback', 'feedback'));
c70ad9f7 732 echo '</p>';
733 }
734}
735
abd2899c
AG
736/**
737 * This gets an array with default options for the editor
738 *
739 * @return array the options
740 */
741function feedback_get_editor_options() {
742 return array('maxfiles' => EDITOR_UNLIMITED_FILES,
743 'trusttext'=>true);
744}
745
37b86a39 746/**
32be99dc 747 * This creates new events given as timeopen and closeopen by $feedback.
748 *
749 * @global object
750 * @param object $feedback
751 * @return void
c70ad9f7 752 */
753function feedback_set_events($feedback) {
c18269c7 754 global $DB;
755
c70ad9f7 756 // adding the feedback to the eventtable (I have seen this at quiz-module)
c18269c7 757 $DB->delete_records('event', array('modulename'=>'feedback', 'instance'=>$feedback->id));
c70ad9f7 758
50aa3fcc 759 if (!isset($feedback->coursemodule)) {
760 $cm = get_coursemodule_from_id('feedback', $feedback->id);
761 $feedback->coursemodule = $cm->id;
762 }
763
c70ad9f7 764 // the open-event
9c5bc7a5 765 if ($feedback->timeopen > 0) {
b85b25eb 766 $event = new stdClass();
50aa3fcc 767 $event->name = get_string('start', 'feedback').' '.$feedback->name;
768 $event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
c70ad9f7 769 $event->courseid = $feedback->course;
770 $event->groupid = 0;
50aa3fcc 771 $event->userid = 0;
772 $event->modulename = 'feedback';
c18269c7 773 $event->instance = $feedback->id;
c70ad9f7 774 $event->eventtype = 'open';
775 $event->timestart = $feedback->timeopen;
776 $event->visible = instance_is_visible('feedback', $feedback);
9c5bc7a5 777 if ($feedback->timeclose > 0) {
c70ad9f7 778 $event->timeduration = ($feedback->timeclose - $feedback->timeopen);
779 } else {
780 $event->timeduration = 0;
781 }
37b86a39 782
9f42276c 783 calendar_event::create($event);
c70ad9f7 784 }
785
786 // the close-event
9c5bc7a5 787 if ($feedback->timeclose > 0) {
b85b25eb 788 $event = new stdClass();
50aa3fcc 789 $event->name = get_string('stop', 'feedback').' '.$feedback->name;
790 $event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
c70ad9f7 791 $event->courseid = $feedback->course;
792 $event->groupid = 0;
50aa3fcc 793 $event->userid = 0;
794 $event->modulename = 'feedback';
c18269c7 795 $event->instance = $feedback->id;
c70ad9f7 796 $event->eventtype = 'close';
797 $event->timestart = $feedback->timeclose;
798 $event->visible = instance_is_visible('feedback', $feedback);
799 $event->timeduration = 0;
800
9f42276c 801 calendar_event::create($event);
c70ad9f7 802 }
803}
804
37b86a39 805/**
9c5bc7a5
AG
806 * this function is called by {@link feedback_delete_userdata()}
807 * it drops the feedback-instance from the course_module table
32be99dc 808 *
809 * @global object
9c5bc7a5
AG
810 * @param int $id the id from the coursemodule
811 * @return boolean
c70ad9f7 812 */
813function feedback_delete_course_module($id) {
0085fff8 814 global $DB;
815
816 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
c70ad9f7 817 return true;
818 }
0085fff8 819 return $DB->delete_records('course_modules', array('id'=>$cm->id));
c70ad9f7 820}
821
822
823
824////////////////////////////////////////////////
825//functions to handle capabilities
826////////////////////////////////////////////////
827
37b86a39 828/**
9c5bc7a5 829 * returns the context-id related to the given coursemodule-id
32be99dc 830 *
831 * @staticvar object $context
9c5bc7a5
AG
832 * @param int $cmid the coursemodule-id
833 * @return object $context
c70ad9f7 834 */
835function feedback_get_context($cmid) {
836 static $context;
37b86a39 837
9c5bc7a5
AG
838 if (isset($context)) {
839 return $context;
840 }
37b86a39 841
2c979976 842 $context = context_module::instance($cmid);
c70ad9f7 843 return $context;
844}
845
37b86a39 846/**
c70ad9f7 847 * returns true if the current role is faked by switching role feature
32be99dc 848 *
849 * @global object
9c5bc7a5 850 * @return boolean
c70ad9f7 851 */
9c5bc7a5 852function feedback_check_is_switchrole() {
c70ad9f7 853 global $USER;
9c5bc7a5
AG
854 if (isset($USER->switchrole) AND
855 is_array($USER->switchrole) AND
856 count($USER->switchrole) > 0) {
857
c70ad9f7 858 return true;
859 }
860 return false;
861}
862
8d7b8286
AG
863/**
864 * count users which have not completed the feedback
865 *
866 * @global object
867 * @uses CONTEXT_MODULE
868 * @param object $cm
869 * @param int $group single groupid
3a787259
AG
870 * @param string $sort
871 * @param int $startpage
872 * @param int $pagecount
8d7b8286
AG
873 * @return object the userrecords
874 */
9c5bc7a5
AG
875function feedback_get_incomplete_users($cm,
876 $group = false,
877 $sort = '',
878 $startpage = false,
879 $pagecount = false) {
880
8d7b8286 881 global $DB;
6938191b 882
2c979976 883 $context = context_module::instance($cm->id);
6938191b 884
8d7b8286
AG
885 //first get all user who can complete this feedback
886 $cap = 'mod/feedback:complete';
887 $fields = 'u.id, u.username';
9c5bc7a5
AG
888 if (!$allusers = get_users_by_capability($context,
889 $cap,
890 $fields,
891 $sort,
892 '',
893 '',
894 $group,
895 '',
896 true)) {
8d7b8286
AG
897 return false;
898 }
899 $allusers = array_keys($allusers);
900
901 //now get all completeds
9c5bc7a5
AG
902 $params = array('feedback'=>$cm->instance);
903 if (!$completedusers = $DB->get_records_menu('feedback_completed', $params, '', 'userid,id')) {
3fac763e 904 return $allusers;
8d7b8286
AG
905 }
906 $completedusers = array_keys($completedusers);
6938191b 907
8d7b8286
AG
908 //now strike all completedusers from allusers
909 $allusers = array_diff($allusers, $completedusers);
910
911 //for paging I use array_slice()
9c5bc7a5 912 if ($startpage !== false AND $pagecount !== false) {
8d7b8286
AG
913 $allusers = array_slice($allusers, $startpage, $pagecount);
914 }
915
916 return $allusers;
917}
918
919/**
920 * count users which have not completed the feedback
921 *
922 * @global object
8d7b8286
AG
923 * @param object $cm
924 * @param int $group single groupid
3a787259 925 * @return int count of userrecords
8d7b8286
AG
926 */
927function feedback_count_incomplete_users($cm, $group = false) {
9c5bc7a5 928 if ($allusers = feedback_get_incomplete_users($cm, $group)) {
8d7b8286
AG
929 return count($allusers);
930 }
931 return 0;
932}
933
d4b1d58c 934/**
3a787259 935 * count users which have completed a feedback
32be99dc 936 *
937 * @global object
3a787259 938 * @uses FEEDBACK_ANONYMOUS_NO
32be99dc 939 * @param object $cm
940 * @param int $group single groupid
3a787259 941 * @return int count of userrecords
c70ad9f7 942 */
248bafd6
AG
943function feedback_count_complete_users($cm, $group = false) {
944 global $DB;
945
946 $params = array(FEEDBACK_ANONYMOUS_NO, $cm->instance);
947
948 $fromgroup = '';
949 $wheregroup = '';
9c5bc7a5 950 if ($group) {
248bafd6
AG
951 $fromgroup = ', {groups_members} g';
952 $wheregroup = ' AND g.groupid = ? AND g.userid = c.userid';
953 $params[] = $group;
954 }
6938191b 955
248bafd6
AG
956 $sql = 'SELECT COUNT(u.id) FROM {user} u, {feedback_completed} c'.$fromgroup.'
957 WHERE anonymous_response = ? AND u.id = c.userid AND c.feedback = ?
958 '.$wheregroup;
6938191b 959
248bafd6
AG
960 return $DB->count_records_sql($sql, $params);
961
962}
963
964/**
3a787259 965 * get users which have completed a feedback
248bafd6
AG
966 *
967 * @global object
968 * @uses CONTEXT_MODULE
3a787259 969 * @uses FEEDBACK_ANONYMOUS_NO
248bafd6
AG
970 * @param object $cm
971 * @param int $group single groupid
0f21a964
PS
972 * @param string $where a sql where condition (must end with " AND ")
973 * @param array parameters used in $where
248bafd6 974 * @param string $sort a table field
3a787259
AG
975 * @param int $startpage
976 * @param int $pagecount
248bafd6
AG
977 * @return object the userrecords
978 */
9c5bc7a5
AG
979function feedback_get_complete_users($cm,
980 $group = false,
981 $where = '',
982 array $params = null,
983 $sort = '',
984 $startpage = false,
985 $pagecount = false) {
986
a600ad35 987 global $DB;
d4b1d58c 988
2c979976 989 $context = context_module::instance($cm->id);
37b86a39 990
0f21a964
PS
991 $params = (array)$params;
992
993 $params['anon'] = FEEDBACK_ANONYMOUS_NO;
994 $params['instance'] = $cm->instance;
a600ad35 995
996 $fromgroup = '';
997 $wheregroup = '';
0f21a964 998 if ($group) {
a600ad35 999 $fromgroup = ', {groups_members} g';
0f21a964
PS
1000 $wheregroup = ' AND g.groupid = :group AND g.userid = c.userid';
1001 $params['group'] = $group;
a600ad35 1002 }
6938191b 1003
0f21a964 1004 if ($sort) {
248bafd6 1005 $sortsql = ' ORDER BY '.$sort;
0f21a964 1006 } else {
248bafd6
AG
1007 $sortsql = '';
1008 }
6938191b 1009
eae1948c 1010 $ufields = user_picture::fields('u');
a5448b3f 1011 $sql = 'SELECT DISTINCT '.$ufields.', c.timemodified as completed_timemodified
9c5bc7a5
AG
1012 FROM {user} u, {feedback_completed} c '.$fromgroup.'
1013 WHERE '.$where.' anonymous_response = :anon
1014 AND u.id = c.userid
1015 AND c.feedback = :instance
248bafd6 1016 '.$wheregroup.$sortsql;
6938191b 1017
0f21a964 1018 if ($startpage === false OR $pagecount === false) {
248bafd6
AG
1019 $startpage = false;
1020 $pagecount = false;
1021 }
1022 return $DB->get_records_sql($sql, $params, $startpage, $pagecount);
c70ad9f7 1023}
1024
37b86a39 1025/**
32be99dc 1026 * get users which have the viewreports-capability
1027 *
1028 * @uses CONTEXT_MODULE
1029 * @param int $cmid
1030 * @param mixed $groups single groupid or array of groupids - group(s) user is in
1031 * @return object the userrecords
c70ad9f7 1032 */
1033function feedback_get_viewreports_users($cmid, $groups = false) {
1034
2c979976 1035 $context = context_module::instance($cmid);
37b86a39 1036
9c5bc7a5
AG
1037 //description of the call below:
1038 //get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='',
1039 // $limitnum='', $groups='', $exceptions='', $doanything=true)
1040 return get_users_by_capability($context,
1041 'mod/feedback:viewreports',
1042 '',
1043 'lastname',
1044 '',
1045 '',
1046 $groups,
1047 '',
1048 false);
c70ad9f7 1049}
1050
37b86a39 1051/**
32be99dc 1052 * get users which have the receivemail-capability
1053 *
1054 * @uses CONTEXT_MODULE
1055 * @param int $cmid
1056 * @param mixed $groups single groupid or array of groupids - group(s) user is in
1057 * @return object the userrecords
c184660d 1058 */
1059function feedback_get_receivemail_users($cmid, $groups = false) {
1060
2c979976 1061 $context = context_module::instance($cmid);
37b86a39 1062
9c5bc7a5
AG
1063 //description of the call below:
1064 //get_users_by_capability($context, $capability, $fields='', $sort='', $limitfrom='',
1065 // $limitnum='', $groups='', $exceptions='', $doanything=true)
1066 return get_users_by_capability($context,
1067 'mod/feedback:receivemail',
1068 '',
1069 'lastname',
1070 '',
1071 '',
1072 $groups,
1073 '',
1074 false);
c184660d 1075}
1076
c70ad9f7 1077////////////////////////////////////////////////
1078//functions to handle the templates
1079////////////////////////////////////////////////
1080////////////////////////////////////////////////
1081
37b86a39 1082/**
32be99dc 1083 * creates a new template-record.
1084 *
1085 * @global object
1086 * @param int $courseid
1087 * @param string $name the name of template shown in the templatelist
1088 * @param int $ispublic 0:privat 1:public
1089 * @return int the new templateid
c70ad9f7 1090 */
1091function feedback_create_template($courseid, $name, $ispublic = 0) {
0085fff8 1092 global $DB;
1093
39790bd8 1094 $templ = new stdClass();
7b1126fd 1095 $templ->course = ($ispublic ? 0 : $courseid);
0085fff8 1096 $templ->name = $name;
c70ad9f7 1097 $templ->ispublic = $ispublic;
0085fff8 1098
14832313
AG
1099 $templid = $DB->insert_record('feedback_template', $templ);
1100 return $DB->get_record('feedback_template', array('id'=>$templid));
c70ad9f7 1101}
1102
37b86a39 1103/**
32be99dc 1104 * creates new template items.
1105 * all items will be copied and the attribute feedback will be set to 0
1106 * and the attribute template will be set to the new templateid
1107 *
1108 * @global object
3a787259
AG
1109 * @uses CONTEXT_MODULE
1110 * @uses CONTEXT_COURSE
32be99dc 1111 * @param object $feedback
1112 * @param string $name the name of template shown in the templatelist
1113 * @param int $ispublic 0:privat 1:public
1114 * @return boolean
c70ad9f7 1115 */
1116function feedback_save_as_template($feedback, $name, $ispublic = 0) {
0085fff8 1117 global $DB;
14832313 1118 $fs = get_file_storage();
0085fff8 1119
1120 if (!$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
c70ad9f7 1121 return false;
1122 }
37b86a39 1123
0085fff8 1124 if (!$newtempl = feedback_create_template($feedback->course, $name, $ispublic)) {
c70ad9f7 1125 return false;
1126 }
6938191b 1127
7b1126fd
AG
1128 //files in the template_item are in the context of the current course or
1129 //if the template is public the files are in the system context
14832313 1130 //files in the feedback_item are in the feedback_context of the feedback
9c5bc7a5 1131 if ($ispublic) {
0601e0ee 1132 $s_context = context_system::instance();
9c5bc7a5 1133 } else {
2c979976 1134 $s_context = context_course::instance($newtempl->course);
7b1126fd 1135 }
14832313 1136 $cm = get_coursemodule_from_instance('feedback', $feedback->id);
2c979976 1137 $f_context = context_module::instance($cm->id);
6938191b 1138
c70ad9f7 1139 //create items of this new template
73043833
AG
1140 //depend items we are storing temporary in an mapping list array(new id => dependitem)
1141 //we also store a mapping of all items array(oldid => newid)
1142 $dependitemsmap = array();
1143 $itembackup = array();
9c5bc7a5 1144 foreach ($feedbackitems as $item) {
6938191b 1145
14832313 1146 $t_item = clone($item);
6938191b 1147
14832313
AG
1148 unset($t_item->id);
1149 $t_item->feedback = 0;
1150 $t_item->template = $newtempl->id;
1151 $t_item->id = $DB->insert_record('feedback_item', $t_item);
1152 //copy all included files to the feedback_template filearea
9c5bc7a5
AG
1153 $itemfiles = $fs->get_area_files($f_context->id,
1154 'mod_feedback',
1155 'item',
1156 $item->id,
1157 "id",
1158 false);
1159 if ($itemfiles) {
1160 foreach ($itemfiles as $ifile) {
39790bd8 1161 $file_record = new stdClass();
7b1126fd 1162 $file_record->contextid = $s_context->id;
64f93798
PS
1163 $file_record->component = 'mod_feedback';
1164 $file_record->filearea = 'template';
14832313
AG
1165 $file_record->itemid = $t_item->id;
1166 $fs->create_file_from_storedfile($file_record, $ifile);
1167 }
1168 }
6938191b 1169
73043833 1170 $itembackup[$item->id] = $t_item->id;
9c5bc7a5 1171 if ($t_item->dependitem) {
73043833
AG
1172 $dependitemsmap[$t_item->id] = $t_item->dependitem;
1173 }
6938191b 1174
c70ad9f7 1175 }
6938191b 1176
73043833 1177 //remapping the dependency
9c5bc7a5 1178 foreach ($dependitemsmap as $key => $dependitem) {
73043833
AG
1179 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
1180 $newitem->dependitem = $itembackup[$newitem->dependitem];
1181 $DB->update_record('feedback_item', $newitem);
1182 }
6938191b 1183
c70ad9f7 1184 return true;
1185}
1186
37b86a39 1187/**
32be99dc 1188 * deletes all feedback_items related to the given template id
1189 *
1190 * @global object
3a787259 1191 * @uses CONTEXT_COURSE
7b1126fd 1192 * @param object $template the template
32be99dc 1193 * @return void
c70ad9f7 1194 */
7b1126fd 1195function feedback_delete_template($template) {
0085fff8 1196 global $DB;
1197
7b1126fd 1198 //deleting the files from the item is done by feedback_delete_item
9c5bc7a5
AG
1199 if ($t_items = $DB->get_records("feedback_item", array("template"=>$template->id))) {
1200 foreach ($t_items as $t_item) {
7b1126fd 1201 feedback_delete_item($t_item->id, false, $template);
14832313
AG
1202 }
1203 }
7b1126fd 1204 $DB->delete_records("feedback_template", array("id"=>$template->id));
c70ad9f7 1205}
1206
37b86a39 1207/**
32be99dc 1208 * creates new feedback_item-records from template.
1209 * if $deleteold is set true so the existing items of the given feedback will be deleted
1210 * if $deleteold is set false so the new items will be appanded to the old items
1211 *
1212 * @global object
3a787259
AG
1213 * @uses CONTEXT_COURSE
1214 * @uses CONTEXT_MODULE
32be99dc 1215 * @param object $feedback
1216 * @param int $templateid
1217 * @param boolean $deleteold
c70ad9f7 1218 */
1219function feedback_items_from_template($feedback, $templateid, $deleteold = false) {
516c5eca
PS
1220 global $DB, $CFG;
1221
1222 require_once($CFG->libdir.'/completionlib.php');
1223
14832313 1224 $fs = get_file_storage();
0085fff8 1225
9c5bc7a5 1226 if (!$template = $DB->get_record('feedback_template', array('id'=>$templateid))) {
485ec594
AG
1227 return false;
1228 }
c70ad9f7 1229 //get all templateitems
9c5bc7a5 1230 if (!$templitems = $DB->get_records('feedback_item', array('template'=>$templateid))) {
c70ad9f7 1231 return false;
1232 }
37b86a39 1233
14832313
AG
1234 //files in the template_item are in the context of the current course
1235 //files in the feedback_item are in the feedback_context of the feedback
9c5bc7a5 1236 if ($template->ispublic) {
0601e0ee 1237 $s_context = context_system::instance();
9c5bc7a5 1238 } else {
2c979976 1239 $s_context = context_course::instance($feedback->course);
485ec594 1240 }
8a41eb4f 1241 $course = $DB->get_record('course', array('id'=>$feedback->course));
14832313 1242 $cm = get_coursemodule_from_instance('feedback', $feedback->id);
2c979976 1243 $f_context = context_module::instance($cm->id);
14832313 1244
c70ad9f7 1245 //if deleteold then delete all old items before
1246 //get all items
9c5bc7a5
AG
1247 if ($deleteold) {
1248 if ($feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
c70ad9f7 1249 //delete all items of this feedback
9c5bc7a5 1250 foreach ($feedbackitems as $item) {
c70ad9f7 1251 feedback_delete_item($item->id, false);
1252 }
1253 //delete tracking-data
0085fff8 1254 $DB->delete_records('feedback_tracking', array('feedback'=>$feedback->id));
516c5eca 1255
9c5bc7a5
AG
1256 $params = array('feedback'=>$feedback->id);
1257 if ($completeds = $DB->get_records('feedback_completed', $params)) {
8a41eb4f 1258 $completion = new completion_info($course);
9c5bc7a5 1259 foreach ($completeds as $completed) {
8a41eb4f
AG
1260 // Update completion state
1261 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
1262 $completion->update_state($cm, COMPLETION_INCOMPLETE, $completed->userid);
1263 }
1264 $DB->delete_records('feedback_completed', array('id'=>$completed->id));
1265 }
1266 }
0085fff8 1267 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedback->id));
c70ad9f7 1268 }
194dd279 1269 $positionoffset = 0;
0085fff8 1270 } else {
c70ad9f7 1271 //if the old items are kept the new items will be appended
1272 //therefor the new position has an offset
0085fff8 1273 $positionoffset = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
c70ad9f7 1274 }
37b86a39 1275
73043833
AG
1276 //create items of this new template
1277 //depend items we are storing temporary in an mapping list array(new id => dependitem)
1278 //we also store a mapping of all items array(oldid => newid)
1279 $dependitemsmap = array();
1280 $itembackup = array();
9c5bc7a5 1281 foreach ($templitems as $t_item) {
14832313
AG
1282 $item = clone($t_item);
1283 unset($item->id);
1284 $item->feedback = $feedback->id;
1285 $item->template = 0;
1286 $item->position = $item->position + $positionoffset;
1287
1288 $item->id = $DB->insert_record('feedback_item', $item);
22ca3e49 1289
9c5bc7a5
AG
1290 //moving the files to the new item
1291 $templatefiles = $fs->get_area_files($s_context->id,
1292 'mod_feedback',
1293 'template',
1294 $t_item->id,
1295 "id",
1296 false);
1297 if ($templatefiles) {
1298 foreach ($templatefiles as $tfile) {
39790bd8 1299 $file_record = new stdClass();
14832313 1300 $file_record->contextid = $f_context->id;
64f93798
PS
1301 $file_record->component = 'mod_feedback';
1302 $file_record->filearea = 'item';
14832313
AG
1303 $file_record->itemid = $item->id;
1304 $fs->create_file_from_storedfile($file_record, $tfile);
1305 }
1306 }
6938191b 1307
73043833 1308 $itembackup[$t_item->id] = $item->id;
9c5bc7a5 1309 if ($item->dependitem) {
73043833
AG
1310 $dependitemsmap[$item->id] = $item->dependitem;
1311 }
c70ad9f7 1312 }
6938191b 1313
73043833 1314 //remapping the dependency
9c5bc7a5 1315 foreach ($dependitemsmap as $key => $dependitem) {
73043833
AG
1316 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
1317 $newitem->dependitem = $itembackup[$newitem->dependitem];
1318 $DB->update_record('feedback_item', $newitem);
6938191b 1319 }
c70ad9f7 1320}
1321
37b86a39 1322/**
32be99dc 1323 * get the list of available templates.
1324 * if the $onlyown param is set true so only templates from own course will be served
1325 * this is important for droping templates
1326 *
1327 * @global object
1328 * @param object $course
485ec594 1329 * @param string $onlyownorpublic
32be99dc 1330 * @return array the template recordsets
c70ad9f7 1331 */
485ec594
AG
1332function feedback_get_template_list($course, $onlyownorpublic = '') {
1333 global $DB, $CFG;
0085fff8 1334
485ec594
AG
1335 switch($onlyownorpublic) {
1336 case '':
9c5bc7a5
AG
1337 $templates = $DB->get_records_select('feedback_template',
1338 'course = ? OR ispublic = 1',
1339 array($course->id),
1340 'name');
485ec594
AG
1341 break;
1342 case 'own':
9c5bc7a5
AG
1343 $templates = $DB->get_records('feedback_template',
1344 array('course'=>$course->id),
1345 'name');
485ec594
AG
1346 break;
1347 case 'public':
7b1126fd 1348 $templates = $DB->get_records('feedback_template', array('ispublic'=>1), 'name');
485ec594 1349 break;
c70ad9f7 1350 }
1351 return $templates;
1352}
1353
1354////////////////////////////////////////////////
1355//Handling der Items
1356////////////////////////////////////////////////
1357////////////////////////////////////////////////
1358
6cc1599e
AG
1359/**
1360 * load the lib.php from item-plugin-dir and returns the instance of the itemclass
1361 *
1362 * @global object
1363 * @param object $item
1364 * @return object the instanz of itemclass
1365 */
1366function feedback_get_item_class($typ) {
1367 global $CFG;
6938191b 1368
6cc1599e
AG
1369 //get the class of item-typ
1370 $itemclass = 'feedback_item_'.$typ;
1371 //get the instance of item-class
1372 if (!class_exists($itemclass)) {
1373 require_once($CFG->dirroot.'/mod/feedback/item/'.$typ.'/lib.php');
1374 }
1375 return new $itemclass();
1376}
1377
37b86a39 1378/**
32be99dc 1379 * load the available item plugins from given subdirectory of $CFG->dirroot
1380 * the default is "mod/feedback/item"
1381 *
1382 * @global object
1383 * @param string $dir the subdir
1384 * @return array pluginnames as string
c70ad9f7 1385 */
1386function feedback_load_feedback_items($dir = 'mod/feedback/item') {
1387 global $CFG;
3a787259 1388 $names = get_list_of_plugins($dir);
c70ad9f7 1389 $ret_names = array();
1390
9c5bc7a5 1391 foreach ($names as $name) {
c70ad9f7 1392 require_once($CFG->dirroot.'/'.$dir.'/'.$name.'/lib.php');
9c5bc7a5
AG
1393 if (class_exists('feedback_item_'.$name)) {
1394 $ret_names[] = $name;
c70ad9f7 1395 }
1396 }
1397 return $ret_names;
1398}
1399
37b86a39 1400/**
32be99dc 1401 * load the available item plugins to use as dropdown-options
1402 *
1403 * @global object
1404 * @return array pluginnames as string
c70ad9f7 1405 */
1406function feedback_load_feedback_items_options() {
1407 global $CFG;
37b86a39 1408
c70ad9f7 1409 $feedback_options = array("pagebreak" => get_string('add_pagebreak', 'feedback'));
37b86a39 1410
c70ad9f7 1411 if (!$feedback_names = feedback_load_feedback_items('mod/feedback/item')) {
1412 return array();
1413 }
37b86a39 1414
9c5bc7a5
AG
1415 foreach ($feedback_names as $fn) {
1416 $feedback_options[$fn] = get_string($fn, 'feedback');
c70ad9f7 1417 }
1418 asort($feedback_options);
37b86a39 1419 $feedback_options = array_merge( array(' ' => get_string('select')), $feedback_options );
c70ad9f7 1420 return $feedback_options;
1421}
1422
3a787259
AG
1423/**
1424 * load the available items for the depend item dropdown list shown in the edit_item form
1425 *
1426 * @global object
1427 * @param object $feedback
1428 * @param object $item the item of the edit_item form
1429 * @return array all items except the item $item, labels and pagebreaks
1430 */
73043833
AG
1431function feedback_get_depend_candidates_for_item($feedback, $item) {
1432 global $DB;
1433 //all items for dependitem
1434 $where = "feedback = ? AND typ != 'pagebreak' AND hasvalue = 1";
1435 $params = array($feedback->id);
9c5bc7a5 1436 if (isset($item->id) AND $item->id) {
73043833
AG
1437 $where .= ' AND id != ?';
1438 $params[] = $item->id;
1439 }
1440 $dependitems = array(0 => get_string('choose'));
9c5bc7a5
AG
1441 $feedbackitems = $DB->get_records_select_menu('feedback_item',
1442 $where,
1443 $params,
1444 'position',
1445 'id, label');
1446
1447 if (!$feedbackitems) {
73043833
AG
1448 return $dependitems;
1449 }
1450 //adding the choose-option
9c5bc7a5 1451 foreach ($feedbackitems as $key => $val) {
73043833
AG
1452 $dependitems[$key] = $val;
1453 }
1454 return $dependitems;
1455}
1456
37b86a39 1457/**
32be99dc 1458 * creates a new item-record
1459 *
1460 * @global object
1461 * @param object $data the data from edit_item_form
1462 * @return int the new itemid
c70ad9f7 1463 */
1464function feedback_create_item($data) {
0085fff8 1465 global $DB;
1466
39790bd8 1467 $item = new stdClass();
0085fff8 1468 $item->feedback = $data->feedbackid;
c70ad9f7 1469
1470 $item->template=0;
1471 if (isset($data->templateid)) {
78946b9b 1472 $item->template = intval($data->templateid);
37b86a39 1473 }
c70ad9f7 1474
1475 $itemname = trim($data->itemname);
0085fff8 1476 $item->name = ($itemname ? $data->itemname : get_string('no_itemname', 'feedback'));
37b86a39 1477
d185ff44
SH
1478 if (!empty($data->itemlabel)) {
1479 $item->label = trim($data->itemlabel);
1480 } else {
1481 $item->label = get_string('no_itemlabel', 'feedback');
1482 }
b7a47958 1483
6cc1599e 1484 $itemobj = feedback_get_item_class($data->typ);
a59ff6b0 1485 $item->presentation = ''; //the date comes from postupdate() of the itemobj
37b86a39 1486
c70ad9f7 1487 $item->hasvalue = $itemobj->get_hasvalue();
37b86a39 1488
c70ad9f7 1489 $item->typ = $data->typ;
1490 $item->position = $data->position;
1491
1492 $item->required=0;
ab5bd34d 1493 if (!empty($data->required)) {
1494 $item->required = $data->required;
37b86a39 1495 }
c70ad9f7 1496
a59ff6b0
AG
1497 $item->id = $DB->insert_record('feedback_item', $item);
1498
1499 //move all itemdata to the data
1500 $data->id = $item->id;
1501 $data->feedback = $item->feedback;
1502 $data->name = $item->name;
1503 $data->label = $item->label;
1504 $data->required = $item->required;
1505 return $itemobj->postupdate($data);
c70ad9f7 1506}
1507
37b86a39 1508/**
32be99dc 1509 * save the changes of a given item.
1510 *
1511 * @global object
1512 * @param object $item
32be99dc 1513 * @return boolean
c70ad9f7 1514 */
9c5bc7a5 1515function feedback_update_item($item) {
0085fff8 1516 global $DB;
0085fff8 1517 return $DB->update_record("feedback_item", $item);
c70ad9f7 1518}
1519
37b86a39 1520/**
3a787259 1521 * deletes an item and also deletes all related values
32be99dc 1522 *
1523 * @global object
3a787259 1524 * @uses CONTEXT_MODULE
32be99dc 1525 * @param int $itemid
1526 * @param boolean $renumber should the kept items renumbered Yes/No
7b1126fd 1527 * @param object $template if the template is given so the items are bound to it
32be99dc 1528 * @return void
c70ad9f7 1529 */
9c5bc7a5 1530function feedback_delete_item($itemid, $renumber = true, $template = false) {
0085fff8 1531 global $DB;
6938191b 1532
0085fff8 1533 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
6938191b 1534
14832313
AG
1535 //deleting the files from the item
1536 $fs = get_file_storage();
22ca3e49 1537
9c5bc7a5
AG
1538 if ($template) {
1539 if ($template->ispublic) {
0601e0ee 1540 $context = context_system::instance();
9c5bc7a5 1541 } else {
2c979976 1542 $context = context_course::instance($template->course);
7b1126fd 1543 }
9c5bc7a5
AG
1544 $templatefiles = $fs->get_area_files($context->id,
1545 'mod_feedback',
1546 'template',
1547 $item->id,
1548 "id",
1549 false);
1550
1551 if ($templatefiles) {
7b1126fd
AG
1552 $fs->delete_area_files($context->id, 'mod_feedback', 'template', $item->id);
1553 }
9c5bc7a5 1554 } else {
7b1126fd
AG
1555 if (!$cm = get_coursemodule_from_instance('feedback', $item->feedback)) {
1556 return false;
1557 }
2c979976 1558 $context = context_module::instance($cm->id);
6938191b 1559
9c5bc7a5
AG
1560 $itemfiles = $fs->get_area_files($context->id,
1561 'mod_feedback',
1562 'item',
1563 $item->id,
1564 "id", false);
1565
1566 if ($itemfiles) {
7b1126fd
AG
1567 $fs->delete_area_files($context->id, 'mod_feedback', 'item', $item->id);
1568 }
14832313 1569 }
6938191b 1570
0085fff8 1571 $DB->delete_records("feedback_value", array("item"=>$itemid));
1572 $DB->delete_records("feedback_valuetmp", array("item"=>$itemid));
6938191b 1573
73043833
AG
1574 //remove all depends
1575 $DB->set_field('feedback_item', 'dependvalue', '', array('dependitem'=>$itemid));
1576 $DB->set_field('feedback_item', 'dependitem', 0, array('dependitem'=>$itemid));
6938191b 1577
0085fff8 1578 $DB->delete_records("feedback_item", array("id"=>$itemid));
9c5bc7a5 1579 if ($renumber) {
c70ad9f7 1580 feedback_renumber_items($item->feedback);
1581 }
1582}
1583
37b86a39 1584/**
32be99dc 1585 * deletes all items of the given feedbackid
1586 *
1587 * @global object
1588 * @param int $feedbackid
1589 * @return void
c70ad9f7 1590 */
9c5bc7a5 1591function feedback_delete_all_items($feedbackid) {
516c5eca
PS
1592 global $DB, $CFG;
1593 require_once($CFG->libdir.'/completionlib.php');
0085fff8 1594
9c5bc7a5 1595 if (!$feedback = $DB->get_record('feedback', array('id'=>$feedbackid))) {
8a41eb4f
AG
1596 return false;
1597 }
516c5eca 1598
8a41eb4f
AG
1599 if (!$cm = get_coursemodule_from_instance('feedback', $feedback->id)) {
1600 return false;
1601 }
516c5eca 1602
9c5bc7a5 1603 if (!$course = $DB->get_record('course', array('id'=>$feedback->course))) {
8a41eb4f
AG
1604 return false;
1605 }
516c5eca 1606
9c5bc7a5 1607 if (!$items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid))) {
c70ad9f7 1608 return;
1609 }
9c5bc7a5 1610 foreach ($items as $item) {
c70ad9f7 1611 feedback_delete_item($item->id, false);
1612 }
9c5bc7a5 1613 if ($completeds = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id))) {
8a41eb4f 1614 $completion = new completion_info($course);
9c5bc7a5 1615 foreach ($completeds as $completed) {
8a41eb4f
AG
1616 // Update completion state
1617 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
1618 $completion->update_state($cm, COMPLETION_INCOMPLETE, $completed->userid);
1619 }
1620 $DB->delete_records('feedback_completed', array('id'=>$completed->id));
1621 }
1622 }
516c5eca 1623
0085fff8 1624 $DB->delete_records('feedback_completedtmp', array('feedback'=>$feedbackid));
516c5eca 1625
c70ad9f7 1626}
1627
37b86a39 1628/**
32be99dc 1629 * this function toggled the item-attribute required (yes/no)
1630 *
1631 * @global object
1632 * @param object $item
1633 * @return boolean
c70ad9f7 1634 */
1635function feedback_switch_item_required($item) {
6cc1599e
AG
1636 global $DB, $CFG;
1637
1638 $itemobj = feedback_get_item_class($item->typ);
0085fff8 1639
9c5bc7a5
AG
1640 if ($itemobj->can_switch_require()) {
1641 $new_require_val = (int)!(bool)$item->required;
1642 $params = array('id'=>$item->id);
1643 $DB->set_field('feedback_item', 'required', $new_require_val, $params);
6cc1599e
AG
1644 }
1645 return true;
c70ad9f7 1646}
1647
37b86a39 1648/**
32be99dc 1649 * renumbers all items of the given feedbackid
1650 *
1651 * @global object
1652 * @param int $feedbackid
1653 * @return void
c70ad9f7 1654 */
9c5bc7a5 1655function feedback_renumber_items($feedbackid) {
0085fff8 1656 global $DB;
1657
1658 $items = $DB->get_records('feedback_item', array('feedback'=>$feedbackid), 'position');
c70ad9f7 1659 $pos = 1;
9c5bc7a5
AG
1660 if ($items) {
1661 foreach ($items as $item) {
3a787259 1662 $DB->set_field('feedback_item', 'position', $pos, array('id'=>$item->id));
c70ad9f7 1663 $pos++;
c70ad9f7 1664 }
1665 }
1666}
1667
37b86a39 1668/**
32be99dc 1669 * this decreases the position of the given item
1670 *
1671 * @global object
1672 * @param object $item
3a787259 1673 * @return bool
c70ad9f7 1674 */
9c5bc7a5 1675function feedback_moveup_item($item) {
0085fff8 1676 global $DB;
1677
9c5bc7a5 1678 if ($item->position == 1) {
a59ff6b0
AG
1679 return true;
1680 }
6938191b 1681
9c5bc7a5
AG
1682 $params = array('feedback'=>$item->feedback);
1683 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
a59ff6b0
AG
1684 return false;
1685 }
6938191b 1686
a59ff6b0 1687 $itembefore = null;
9c5bc7a5
AG
1688 foreach ($items as $i) {
1689 if ($i->id == $item->id) {
1690 if (is_null($itembefore)) {
a59ff6b0
AG
1691 return true;
1692 }
1693 $itembefore->position = $item->position;
1694 $item->position--;
1695 feedback_update_item($itembefore);
1696 feedback_update_item($item);
1697 feedback_renumber_items($item->feedback);
1698 return true;
1699 }
1700 $itembefore = $i;
1701 }
1702 return false;
c70ad9f7 1703}
1704
37b86a39 1705/**
32be99dc 1706 * this increased the position of the given item
1707 *
1708 * @global object
1709 * @param object $item
3a787259 1710 * @return bool
c70ad9f7 1711 */
9c5bc7a5 1712function feedback_movedown_item($item) {
0085fff8 1713 global $DB;
1714
9c5bc7a5
AG
1715 $params = array('feedback'=>$item->feedback);
1716 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
a59ff6b0 1717 return false;
c70ad9f7 1718 }
6938191b 1719
a59ff6b0 1720 $movedownitem = null;
9c5bc7a5
AG
1721 foreach ($items as $i) {
1722 if (!is_null($movedownitem) AND $movedownitem->id == $item->id) {
a59ff6b0
AG
1723 $movedownitem->position = $i->position;
1724 $i->position--;
1725 feedback_update_item($movedownitem);
1726 feedback_update_item($i);
1727 feedback_renumber_items($item->feedback);
1728 return true;
1729 }
1730 $movedownitem = $i;
1731 }
1732 return false;
c70ad9f7 1733}
1734
37b86a39 1735/**
32be99dc 1736 * here the position of the given item will be set to the value in $pos
1737 *
1738 * @global object
1739 * @param object $moveitem
1740 * @param int $pos
1741 * @return boolean
c70ad9f7 1742 */
9c5bc7a5 1743function feedback_move_item($moveitem, $pos) {
0085fff8 1744 global $DB;
1745
9c5bc7a5
AG
1746 $params = array('feedback'=>$moveitem->feedback);
1747 if (!$allitems = $DB->get_records('feedback_item', $params, 'position')) {
c70ad9f7 1748 return false;
1749 }
0085fff8 1750 if (is_array($allitems)) {
c70ad9f7 1751 $index = 1;
9c5bc7a5
AG
1752 foreach ($allitems as $item) {
1753 if ($index == $pos) {
c70ad9f7 1754 $index++;
1755 }
9c5bc7a5 1756 if ($item->id == $moveitem->id) {
a59ff6b0
AG
1757 $moveitem->position = $pos;
1758 feedback_update_item($moveitem);
1759 continue;
1760 }
c70ad9f7 1761 $item->position = $index;
1762 feedback_update_item($item);
1763 $index++;
1764 }
c70ad9f7 1765 return true;
1766 }
1767 return false;
1768}
1769
9d5fbd65 1770/**
3a787259
AG
1771 * prints the given item as a preview.
1772 * each item-class has an own print_item_preview function implemented.
9d5fbd65 1773 *
3a787259 1774 * @global object
9d5fbd65
AG
1775 * @param object $item the item what we want to print out
1776 * @return void
1777 */
9c5bc7a5 1778function feedback_print_item_preview($item) {
9d5fbd65 1779 global $CFG;
9c5bc7a5 1780 if ($item->typ == 'pagebreak') {
9d5fbd65
AG
1781 return;
1782 }
9d5fbd65 1783 //get the instance of the item-class
6cc1599e 1784 $itemobj = feedback_get_item_class($item->typ);
9d5fbd65
AG
1785 $itemobj->print_item_preview($item);
1786}
1787
37b86a39 1788/**
3a787259
AG
1789 * prints the given item in the completion form.
1790 * each item-class has an own print_item_complete function implemented.
32be99dc 1791 *
1792 * @param object $item the item what we want to print out
3a787259 1793 * @param mixed $value the value
32be99dc 1794 * @param boolean $highlightrequire if this set true and the value are false on completing so the item will be highlighted
1795 * @return void
c70ad9f7 1796 */
9c5bc7a5 1797function feedback_print_item_complete($item, $value = false, $highlightrequire = false) {
ab5bd34d 1798 global $CFG;
9c5bc7a5 1799 if ($item->typ == 'pagebreak') {
9d5fbd65
AG
1800 return;
1801 }
37b86a39 1802
c70ad9f7 1803 //get the instance of the item-class
6cc1599e 1804 $itemobj = feedback_get_item_class($item->typ);
9d5fbd65
AG
1805 $itemobj->print_item_complete($item, $value, $highlightrequire);
1806}
1807
1808/**
3a787259
AG
1809 * prints the given item in the show entries page.
1810 * each item-class has an own print_item_show_value function implemented.
9d5fbd65
AG
1811 *
1812 * @param object $item the item what we want to print out
3a787259 1813 * @param mixed $value
9d5fbd65
AG
1814 * @return void
1815 */
9c5bc7a5 1816function feedback_print_item_show_value($item, $value = false) {
9d5fbd65 1817 global $CFG;
9c5bc7a5 1818 if ($item->typ == 'pagebreak') {
9d5fbd65
AG
1819 return;
1820 }
6938191b 1821
9d5fbd65 1822 //get the instance of the item-class
6cc1599e 1823 $itemobj = feedback_get_item_class($item->typ);
9d5fbd65 1824 $itemobj->print_item_show_value($item, $value);
c70ad9f7 1825}
1826
37b86a39 1827/**
32be99dc 1828 * if the user completes a feedback and there is a pagebreak so the values are saved temporary.
3a787259 1829 * the values are not saved permanently until the user click on save button
32be99dc 1830 *
1831 * @global object
1832 * @param object $feedbackcompleted
1833 * @return object temporary saved completed-record
c70ad9f7 1834 */
0085fff8 1835function feedback_set_tmp_values($feedbackcompleted) {
1836 global $DB;
1837
c70ad9f7 1838 //first we create a completedtmp
39790bd8 1839 $tmpcpl = new stdClass();
9c5bc7a5 1840 foreach ($feedbackcompleted as $key => $value) {
c70ad9f7 1841 $tmpcpl->{$key} = $value;
1842 }
0085fff8 1843 unset($tmpcpl->id);
c70ad9f7 1844 $tmpcpl->timemodified = time();
7826abc7 1845 $tmpcpl->id = $DB->insert_record('feedback_completedtmp', $tmpcpl);
c70ad9f7 1846 //get all values of original-completed
9c5bc7a5 1847 if (!$values = $DB->get_records('feedback_value', array('completed'=>$feedbackcompleted->id))) {
c70ad9f7 1848 return;
1849 }
9c5bc7a5 1850 foreach ($values as $value) {
0085fff8 1851 unset($value->id);
c70ad9f7 1852 $value->completed = $tmpcpl->id;
0085fff8 1853 $DB->insert_record('feedback_valuetmp', $value);
c70ad9f7 1854 }
1855 return $tmpcpl;
1856}
1857
37b86a39 1858/**
32be99dc 1859 * this saves the temporary saved values permanently
1860 *
1861 * @global object
1862 * @param object $feedbackcompletedtmp the temporary completed
1863 * @param object $feedbackcompleted the target completed
1864 * @param int $userid
1865 * @return int the id of the completed
c70ad9f7 1866 */
1867function feedback_save_tmp_values($feedbackcompletedtmp, $feedbackcompleted, $userid) {
0085fff8 1868 global $DB;
1869
c70ad9f7 1870 $tmpcplid = $feedbackcompletedtmp->id;
9c5bc7a5 1871 if ($feedbackcompleted) {
c70ad9f7 1872 //first drop all existing values
0085fff8 1873 $DB->delete_records('feedback_value', array('completed'=>$feedbackcompleted->id));
c70ad9f7 1874 //update the current completed
1875 $feedbackcompleted->timemodified = time();
0085fff8 1876 $DB->update_record('feedback_completed', $feedbackcompleted);
9c5bc7a5 1877 } else {
73043833
AG
1878 $feedbackcompleted = clone($feedbackcompletedtmp);
1879 $feedbackcompleted->id = '';
1880 $feedbackcompleted->userid = $userid;
1881 $feedbackcompleted->timemodified = time();
1882 $feedbackcompleted->id = $DB->insert_record('feedback_completed', $feedbackcompleted);
1883 }
1884
1885 //save all the new values from feedback_valuetmp
1886 //get all values of tmp-completed
9c5bc7a5
AG
1887 $params = array('completed'=>$feedbackcompletedtmp->id);
1888 if (!$values = $DB->get_records('feedback_valuetmp', $params)) {
73043833
AG
1889 return false;
1890 }
9c5bc7a5 1891 foreach ($values as $value) {
73043833
AG
1892 //check if there are depend items
1893 $item = $DB->get_record('feedback_item', array('id'=>$value->item));
9c5bc7a5
AG
1894 if ($item->dependitem > 0) {
1895 $check = feedback_compare_item_value($tmpcplid,
1896 $item->dependitem,
1897 $item->dependvalue,
1898 true);
1899 } else {
73043833 1900 $check = true;
c70ad9f7 1901 }
9c5bc7a5 1902 if ($check) {
0085fff8 1903 unset($value->id);
c70ad9f7 1904 $value->completed = $feedbackcompleted->id;
0085fff8 1905 $DB->insert_record('feedback_value', $value);
c70ad9f7 1906 }
c70ad9f7 1907 }
73043833
AG
1908 //drop all the tmpvalues
1909 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1910 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
1911 return $feedbackcompleted->id;
1912
c70ad9f7 1913}
1914
37b86a39 1915/**
32be99dc 1916 * deletes the given temporary completed and all related temporary values
1917 *
1918 * @global object
1919 * @param int $tmpcplid
1920 * @return void
c70ad9f7 1921 */
1922function feedback_delete_completedtmp($tmpcplid) {
0085fff8 1923 global $DB;
1924
1925 $DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
1926 $DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
c70ad9f7 1927}
1928
1929////////////////////////////////////////////////
1930////////////////////////////////////////////////
1931////////////////////////////////////////////////
1932//functions to handle the pagebreaks
1933////////////////////////////////////////////////
1934
37b86a39 1935/**
32be99dc 1936 * this creates a pagebreak.
1937 * a pagebreak is a special kind of item
1938 *
1939 * @global object
1940 * @param int $feedbackid
1941 * @return mixed false if there already is a pagebreak on last position or the id of the pagebreak-item
c70ad9f7 1942 */
1943function feedback_create_pagebreak($feedbackid) {
0085fff8 1944 global $DB;
1945
c70ad9f7 1946 //check if there already is a pagebreak on the last position
0085fff8 1947 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
9c5bc7a5 1948 if ($lastposition == feedback_get_last_break_position($feedbackid)) {
c70ad9f7 1949 return false;
1950 }
37b86a39 1951
39790bd8 1952 $item = new stdClass();
c70ad9f7 1953 $item->feedback = $feedbackid;
1954
1955 $item->template=0;
1956
1957 $item->name = '';
37b86a39 1958
c70ad9f7 1959 $item->presentation = '';
1960 $item->hasvalue = 0;
37b86a39 1961
c70ad9f7 1962 $item->typ = 'pagebreak';
1963 $item->position = $lastposition + 1;
1964
1965 $item->required=0;
1966
0085fff8 1967 return $DB->insert_record('feedback_item', $item);
c70ad9f7 1968}
1969
37b86a39 1970/**
32be99dc 1971 * get all positions of pagebreaks in the given feedback
1972 *
1973 * @global object
1974 * @param int $feedbackid
1975 * @return array all ordered pagebreak positions
c70ad9f7 1976 */
1977function feedback_get_all_break_positions($feedbackid) {
534792cd 1978 global $DB;
1979
9c5bc7a5
AG
1980 $params = array('typ'=>'pagebreak', 'feedback'=>$feedbackid);
1981 $allbreaks = $DB->get_records_menu('feedback_item', $params, 'position', 'id, position');
1982 if (!$allbreaks) {
1983 return false;
1984 }
c70ad9f7 1985 return array_values($allbreaks);
1986}
1987
37b86a39 1988/**
32be99dc 1989 * get the position of the last pagebreak
d4b1d58c 1990 *
32be99dc 1991 * @param int $feedbackid
1992 * @return int the position of the last pagebreak
c70ad9f7 1993 */
1994function feedback_get_last_break_position($feedbackid) {
9c5bc7a5 1995 if (!$allbreaks = feedback_get_all_break_positions($feedbackid)) {
73043833
AG
1996 return false;
1997 }
c70ad9f7 1998 return $allbreaks[count($allbreaks) - 1];
1999}
2000
37b86a39 2001/**
32be99dc 2002 * this returns the position where the user can continue the completing.
2003 *
2004 * @global object
2005 * @global object
2006 * @global object
2007 * @param int $feedbackid
2008 * @param int $courseid
2009 * @param string $guestid this id will be saved temporary and is unique
2010 * @return int the position to continue
c70ad9f7 2011 */
3a787259 2012function feedback_get_page_to_continue($feedbackid, $courseid = false, $guestid = false) {
0085fff8 2013 global $CFG, $USER, $DB;
37b86a39 2014
c70ad9f7 2015 //is there any break?
37b86a39 2016
9c5bc7a5
AG
2017 if (!$allbreaks = feedback_get_all_break_positions($feedbackid)) {
2018 return false;
2019 }
0085fff8 2020
2021 $params = array();
9c5bc7a5 2022 if ($courseid) {
05edae9f 2023 $courseselect = "AND fv.course_id = :courseid";
0085fff8 2024 $params['courseid'] = $courseid;
9c5bc7a5 2025 } else {
05edae9f 2026 $courseselect = '';
c70ad9f7 2027 }
37b86a39 2028
9c5bc7a5 2029 if ($guestid) {
0085fff8 2030 $userselect = "AND fc.guestid = :guestid";
c70ad9f7 2031 $usergroup = "GROUP BY fc.guestid";
0085fff8 2032 $params['guestid'] = $guestid;
9c5bc7a5 2033 } else {
0085fff8 2034 $userselect = "AND fc.userid = :userid";
c70ad9f7 2035 $usergroup = "GROUP BY fc.userid";
0085fff8 2036 $params['userid'] = $USER->id;
c70ad9f7 2037 }
0085fff8 2038
c70ad9f7 2039 $sql = "SELECT MAX(fi.position)
abf1c50f 2040 FROM {feedback_completedtmp} fc, {feedback_valuetmp} fv, {feedback_item} fi
0085fff8 2041 WHERE fc.id = fv.completed
2042 $userselect
2043 AND fc.feedback = :feedbackid
05edae9f 2044 $courseselect
c70ad9f7 2045 AND fi.id = fv.item
0085fff8 2046 $usergroup";
2047 $params['feedbackid'] = $feedbackid;
2048
2049 $lastpos = $DB->get_field_sql($sql, $params);
c70ad9f7 2050
2051 //the index of found pagebreak is the searched pagenumber
9c5bc7a5
AG
2052 foreach ($allbreaks as $pagenr => $br) {
2053 if ($lastpos < $br) {
2054 return $pagenr;
2055 }
c70ad9f7 2056 }
2057 return count($allbreaks);
2058}
2059
2060////////////////////////////////////////////////
2061////////////////////////////////////////////////
2062////////////////////////////////////////////////
2063//functions to handle the values
2064////////////////////////////////////////////////
2065
5f17311f 2066/**
e7fc2ff4
AG
2067 * cleans the userinput while submitting the form.
2068 *
2069 * @param mixed $value
2070 * @return mixed
2071 */
2072function feedback_clean_input_value($item, $value) {
2073 $itemobj = feedback_get_item_class($item->typ);
2074 return $itemobj->clean_input_value($value);
2075}
2076
37b86a39 2077/**
32be99dc 2078 * this saves the values of an completed.
3a787259
AG
2079 * if the param $tmp is set true so the values are saved temporary in table feedback_valuetmp.
2080 * if there is already a completed and the userid is set so the values are updated.
2081 * on all other things new value records will be created.
d4b1d58c 2082 *
32be99dc 2083 * @global object
32be99dc 2084 * @param int $userid
2085 * @param boolean $tmp
2086 * @return mixed false on error or the completeid
c70ad9f7 2087 */
6a471fe0 2088function feedback_save_values($usrid, $tmp = false) {
0085fff8 2089 global $DB;
6938191b 2090
9c5bc7a5 2091 $completedid = optional_param('completedid', 0, PARAM_INT);
0085fff8 2092
c70ad9f7 2093 $tmpstr = $tmp ? 'tmp' : '';
6a471fe0 2094 $time = time();
9c5bc7a5 2095 $timemodified = mktime(0, 0, 0, date('m', $time), date('d', $time), date('Y', $time));
6a471fe0 2096
9c5bc7a5 2097 if ($usrid == 0) {
6a471fe0 2098 return feedback_create_values($usrid, $timemodified, $tmp);
c70ad9f7 2099 }
9c5bc7a5
AG
2100 $completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid));
2101 if (!$completed) {
6a471fe0 2102 return feedback_create_values($usrid, $timemodified, $tmp);
9c5bc7a5 2103 } else {
c70ad9f7 2104 $completed->timemodified = $timemodified;
6a471fe0 2105 return feedback_update_values($completed, $tmp);
c70ad9f7 2106 }
2107}
2108
37b86a39 2109/**
32be99dc 2110 * this saves the values from anonymous user such as guest on the main-site
2111 *
2112 * @global object
32be99dc 2113 * @param string $guestid the unique guestidentifier
2114 * @return mixed false on error or the completeid
c70ad9f7 2115 */
6a471fe0 2116function feedback_save_guest_values($guestid) {
0085fff8 2117 global $DB;
2118
6a471fe0 2119 $completedid = optional_param('completedid', false, PARAM_INT);
6938191b 2120
c70ad9f7 2121 $timemodified = time();
9c5bc7a5 2122 if (!$completed = $DB->get_record('feedback_completedtmp', array('id'=>$completedid))) {
6a471fe0 2123 return feedback_create_values(0, $timemodified, true, $guestid);
9c5bc7a5 2124 } else {
c70ad9f7 2125 $completed->timemodified = $timemodified;
6a471fe0 2126 return feedback_update_values($completed, true);
c70ad9f7 2127 }
2128}
2129
37b86a39 2130/**
32be99dc 2131 * get the value from the given item related to the given completed.
2132 * the value can come as temporary or as permanently value. the deciding is done by $tmp
2133 *
2134 * @global object
2135 * @param int $completeid
2136 * @param int $itemid
2137 * @param boolean $tmp
2138 * @return mixed the value, the type depends on plugin-definition
c70ad9f7 2139 */
2140function feedback_get_item_value($completedid, $itemid, $tmp = false) {
0085fff8 2141 global $DB;
2142
c70ad9f7 2143 $tmpstr = $tmp ? 'tmp' : '';
9c5bc7a5
AG
2144 $params = array('completed'=>$completedid, 'item'=>$itemid);
2145 return $DB->get_field('feedback_value'.$tmpstr, 'value', $params);
c70ad9f7 2146}
2147
3a787259
AG
2148/**
2149 * compares the value of the itemid related to the completedid with the dependvalue.
2150 * this is used if a depend item is set.
2151 * the value can come as temporary or as permanently value. the deciding is done by $tmp.
2152 *
2153 * @global object
2154 * @global object
2155 * @param int $completeid
2156 * @param int $itemid
2157 * @param mixed $dependvalue
2158 * @param boolean $tmp
2159 * @return bool
2160 */
73043833
AG
2161function feedback_compare_item_value($completedid, $itemid, $dependvalue, $tmp = false) {
2162 global $DB, $CFG;
6938191b 2163
73043833 2164 $dbvalue = feedback_get_item_value($completedid, $itemid, $tmp);
6938191b 2165
73043833
AG
2166 //get the class of the given item-typ
2167 $item = $DB->get_record('feedback_item', array('id'=>$itemid));
6cc1599e 2168
73043833 2169 //get the instance of the item-class
6cc1599e 2170 $itemobj = feedback_get_item_class($item->typ);
73043833
AG
2171 return $itemobj->compare_value($item, $dbvalue, $dependvalue); //true or false
2172}
2173
37b86a39 2174/**
32be99dc 2175 * this function checks the correctness of values.
2176 * the rules for this are implemented in the class of each item.
2177 * it can be the required attribute or the value self e.g. numeric.
2178 * the params first/lastitem are given to determine the visible range between pagebreaks.
2179 *
2180 * @global object
32be99dc 2181 * @param int $firstitem the position of firstitem for checking
2182 * @param int $lastitem the position of lastitem for checking
2183 * @return boolean
c70ad9f7 2184 */
6a471fe0 2185function feedback_check_values($firstitem, $lastitem) {
ab5bd34d 2186 global $DB, $CFG;
0085fff8 2187
6a471fe0
AG
2188 $feedbackid = optional_param('feedbackid', 0, PARAM_INT);
2189
c70ad9f7 2190 //get all items between the first- and lastitem
0085fff8 2191 $select = "feedback = ?
2192 AND position >= ?
2193 AND position <= ?
c70ad9f7 2194 AND hasvalue = 1";
6a471fe0 2195 $params = array($feedbackid, $firstitem, $lastitem);
9c5bc7a5 2196 if (!$feedbackitems = $DB->get_records_select('feedback_item', $select, $params)) {
c70ad9f7 2197 //if no values are given so no values can be wrong ;-)
2198 return true;
2199 }
2200
9c5bc7a5
AG
2201 foreach ($feedbackitems as $item) {
2202 //get the instance of the item-class
2203 $itemobj = feedback_get_item_class($item->typ);
2204
c70ad9f7 2205 //the name of the input field of the completeform is given in a special form:
2206 //<item-typ>_<item-id> eg. numeric_234
2207 //this is the key to get the value for the correct item
2208 $formvalname = $item->typ . '_' . $item->id;
6938191b 2209
9c5bc7a5 2210 if ($itemobj->value_is_array()) {
94848b07
AG
2211 //get the raw value here. It is cleaned after that by the object itself
2212 $value = optional_param_array($formvalname, null, PARAM_RAW);
9c5bc7a5 2213 } else {
94848b07
AG
2214 //get the raw value here. It is cleaned after that by the object itself
2215 $value = optional_param($formvalname, null, PARAM_RAW);
9c5bc7a5 2216 }
94848b07 2217 $value = $itemobj->clean_input_value($value);
6938191b 2218
c70ad9f7 2219 //check if the value is set
9c5bc7a5 2220 if (is_null($value) AND $item->required == 1) {
c70ad9f7 2221 return false;
2222 }
37b86a39 2223
c70ad9f7 2224 //now we let check the value by the item-class
9c5bc7a5 2225 if (!$itemobj->check_value($value, $item)) {
c70ad9f7 2226 return false;
2227 }
2228 }
2229 //if no wrong values so we can return true
2230 return true;
2231}
2232
37b86a39 2233/**
32be99dc 2234 * this function create a complete-record and the related value-records.
2235 * depending on the $tmp (true/false) the values are saved temporary or permanently
2236 *
2237 * @global object
32be99dc 2238 * @param int $userid
2239 * @param int $timemodified
2240 * @param boolean $tmp
2241 * @param string $guestid a unique identifier to save temporary data
2242 * @return mixed false on error or the completedid
c70ad9f7 2243 */
9c5bc7a5 2244function feedback_create_values($usrid, $timemodified, $tmp = false, $guestid = false) {
0085fff8 2245 global $DB;
2246
6a471fe0
AG
2247 $feedbackid = optional_param('feedbackid', false, PARAM_INT);
2248 $anonymous_response = optional_param('anonymous_response', false, PARAM_INT);
2249 $courseid = optional_param('courseid', false, PARAM_INT);
6938191b 2250
c70ad9f7 2251 $tmpstr = $tmp ? 'tmp' : '';
2252 //first we create a new completed record
39790bd8 2253 $completed = new stdClass();
6a471fe0 2254 $completed->feedback = $feedbackid;
0085fff8 2255 $completed->userid = $usrid;
2256 $completed->guestid = $guestid;
2257 $completed->timemodified = $timemodified;
6a471fe0 2258 $completed->anonymous_response = $anonymous_response;
37b86a39 2259
a8f3a651 2260 $completedid = $DB->insert_record('feedback_completed'.$tmpstr, $completed);
37b86a39 2261
0085fff8 2262 $completed = $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$completedid));
c70ad9f7 2263
6a471fe0 2264 //the keys are in the form like abc_xxx
c70ad9f7 2265 //with explode we make an array with(abc, xxx) and (abc=typ und xxx=itemnr)
6938191b 2266
6a471fe0 2267 //get the items of the feedback
9c5bc7a5 2268 if (!$allitems = $DB->get_records('feedback_item', array('feedback'=>$completed->feedback))) {
6a471fe0
AG
2269 return false;
2270 }
9c5bc7a5
AG
2271 foreach ($allitems as $item) {
2272 if (!$item->hasvalue) {
2273 continue;
2274 }
2275 //get the class of item-typ
2276 $itemobj = feedback_get_item_class($item->typ);
2277
6a471fe0 2278 $keyname = $item->typ.'_'.$item->id;
9c5bc7a5
AG
2279
2280 if ($itemobj->value_is_array()) {
2281 $itemvalue = optional_param_array($keyname, null, $itemobj->value_type());
2282 } else {
2283 $itemvalue = optional_param($keyname, null, $itemobj->value_type());
2284 }
2285
2286 if (is_null($itemvalue)) {
6a471fe0 2287 continue;
c70ad9f7 2288 }
9c5bc7a5 2289
39790bd8 2290 $value = new stdClass();
6a471fe0
AG
2291 $value->item = $item->id;
2292 $value->completed = $completed->id;
2293 $value->course_id = $courseid;
6938191b 2294
9c5bc7a5
AG
2295 //the kind of values can be absolutely different
2296 //so we run create_value directly by the item-class
6a471fe0
AG
2297 $value->value = $itemobj->create_value($itemvalue);
2298 $DB->insert_record('feedback_value'.$tmpstr, $value);
c70ad9f7 2299 }
6a471fe0 2300 return $completed->id;
c70ad9f7 2301}
2302
37b86a39 2303/**
32be99dc 2304 * this function updates a complete-record and the related value-records.
2305 * depending on the $tmp (true/false) the values are saved temporary or permanently
2306 *
2307 * @global object
32be99dc 2308 * @param object $completed
2309 * @param boolean $tmp
2310 * @return int the completedid
c70ad9f7 2311 */
6a471fe0 2312function feedback_update_values($completed, $tmp = false) {
0085fff8 2313 global $DB;
2314
9c5bc7a5 2315 $courseid = optional_param('courseid', false, PARAM_INT);
c70ad9f7 2316 $tmpstr = $tmp ? 'tmp' : '';
37b86a39 2317
0085fff8 2318 $DB->update_record('feedback_completed'.$tmpstr, $completed);
c70ad9f7 2319 //get the values of this completed
0085fff8 2320 $values = $DB->get_records('feedback_value'.$tmpstr, array('completed'=>$completed->id));
37b86a39 2321
6a471fe0 2322 //get the items of the feedback
9c5bc7a5 2323 if (!$allitems = $DB->get_records('feedback_item', array('feedback'=>$completed->feedback))) {
6a471fe0
AG
2324 return false;
2325 }
9c5bc7a5
AG
2326 foreach ($allitems as $item) {
2327 if (!$item->hasvalue) {
2328 continue;
2329 }
2330 //get the class of item-typ
2331 $itemobj = feedback_get_item_class($item->typ);
2332
6a471fe0 2333 $keyname = $item->typ.'_'.$item->id;
9c5bc7a5
AG
2334
2335 if ($itemobj->value_is_array()) {
2336 $itemvalue = optional_param_array($keyname, null, $itemobj->value_type());
2337 } else {
2338 $itemvalue = optional_param($keyname, null, $itemobj->value_type());
2339 }
2340
6a471fe0 2341 //is the itemvalue set (could be a subset of items because pagebreak)?
9c5bc7a5 2342 if (is_null($itemvalue)) {
6a471fe0
AG
2343 continue;
2344 }
6938191b 2345
39790bd8 2346 $newvalue = new stdClass();
6a471fe0
AG
2347 $newvalue->item = $item->id;
2348 $newvalue->completed = $completed->id;
2349 $newvalue->course_id = $courseid;
37b86a39 2350
9c5bc7a5
AG
2351 //the kind of values can be absolutely different
2352 //so we run create_value directly by the item-class
6a471fe0 2353 $newvalue->value = $itemobj->create_value($itemvalue);
6938191b 2354
6a471fe0
AG
2355 //check, if we have to create or update the value
2356 $exist = false;
9c5bc7a5
AG
2357 foreach ($values as $value) {
2358 if ($value->item == $newvalue->item) {
6a471fe0
AG
2359 $newvalue->id = $value->id;
2360 $exist = true;
2361 break;
2362 }
2363 }
9c5bc7a5 2364 if ($exist) {
6a471fe0 2365 $DB->update_record('feedback_value'.$tmpstr, $newvalue);
9c5bc7a5 2366 } else {
6a471fe0 2367 $DB->insert_record('feedback_value'.$tmpstr, $newvalue);
c70ad9f7 2368 }
2369 }
2370
2371 return $completed->id;
2372}
2373
37b86a39 2374/**
32be99dc 2375 * get the values of an item depending on the given groupid.
2376 * if the feedback is anonymous so the values are shuffled
2377 *
2378 * @global object
2379 * @global object
2380 * @param object $item
2381 * @param int $groupid
2382 * @param int $courseid
3a787259 2383 * @param bool $ignore_empty if this is set true so empty values are not delivered
32be99dc 2384 * @return array the value-records
c70ad9f7 2385 */
9c5bc7a5
AG
2386function feedback_get_group_values($item,
2387 $groupid = false,
2388 $courseid = false,
2389 $ignore_empty = false) {
2390
0085fff8 2391 global $CFG, $DB;
c70ad9f7 2392
2393 //if the groupid is given?
0085fff8 2394 if (intval($groupid) > 0) {
9c5bc7a5
AG
2395 if ($ignore_empty) {
2396 $ignore_empty_select = "AND fbv.value != '' AND fbv.value != '0'";
2397 } else {
79473294
AG
2398 $ignore_empty_select = "";
2399 }
6938191b 2400
c70ad9f7 2401 $query = 'SELECT fbv . *
abf1c50f 2402 FROM {feedback_value} fbv, {feedback_completed} fbc, {groups_members} gm
0085fff8 2403 WHERE fbv.item = ?
37b86a39 2404 AND fbv.completed = fbc.id
2405 AND fbc.userid = gm.userid
79473294 2406 '.$ignore_empty_select.'
0085fff8 2407 AND gm.groupid = ?
2408 ORDER BY fbc.timemodified';
2409 $values = $DB->get_records_sql($query, array($item->id, $groupid));
2410
2411 } else {
9c5bc7a5
AG
2412 if ($ignore_empty) {
2413 $ignore_empty_select = "AND value != '' AND value != '0'";
2414 } else {
79473294
AG
2415 $ignore_empty_select = "";
2416 }
6938191b 2417
c70ad9f7 2418 if ($courseid) {
9c5bc7a5
AG
2419 $select = "item = ? AND course_id = ? ".$ignore_empty_select;
2420 $params = array($item->id, $courseid);
2421 $values = $DB->get_records_select('feedback_value', $select, $params);
c70ad9f7 2422 } else {
9c5bc7a5
AG
2423 $select = "item = ? ".$ignore_empty_select;
2424 $params = array($item->id);
2425 $values = $DB->get_records_select('feedback_value', $select, $params);
c70ad9f7 2426 }
37b86a39 2427 }
9c5bc7a5
AG
2428 $params = array('id'=>$item->feedback);
2429 if ($DB->get_field('feedback', 'anonymous', $params) == FEEDBACK_ANONYMOUS_YES) {
2430 if (is_array($values)) {
c70ad9f7 2431 shuffle($values);
9c5bc7a5 2432 }
c70ad9f7 2433 }
2434 return $values;
2435}
2436
37b86a39 2437/**
32be99dc 2438 * check for multiple_submit = false.
2439 * if the feedback is global so the courseid must be given
2440 *
2441 * @global object
2442 * @global object
2443 * @param int $feedbackid
2444 * @param int $courseid
2445 * @return boolean true if the feedback already is submitted otherwise false
c70ad9f7 2446 */
2447function feedback_is_already_submitted($feedbackid, $courseid = false) {
534792cd 2448 global $USER, $DB;
37b86a39 2449
9c5bc7a5
AG
2450 $params = array('userid'=>$USER->id, 'feedback'=>$feedbackid);
2451 if (!$trackings = $DB->get_records_menu('feedback_tracking', $params, '', 'id, completed')) {
c70ad9f7 2452 return false;
2453 }
2454
9c5bc7a5
AG
2455 if ($courseid) {
2456 $select = 'completed IN ('.implode(',', $trackings).') AND course_id = ?';
2457 if (!$values = $DB->get_records_select('feedback_value', $select, array($courseid))) {
c70ad9f7 2458 return false;
2459 }
2460 }
2461
2462 return true;
2463}
2464
37b86a39 2465/**
9c5bc7a5
AG
2466 * if the completion of a feedback will be continued eg.
2467 * by pagebreak or by multiple submit so the complete must be found.
32be99dc 2468 * if the param $tmp is set true so all things are related to temporary completeds
2469 *
2470 * @global object
2471 * @global object
2472 * @global object
2473 * @param int $feedbackid
2474 * @param boolean $tmp
2475 * @param int $courseid
2476 * @param string $guestid
2477 * @return int the id of the found completed
c70ad9f7 2478 */
9c5bc7a5
AG
2479function feedback_get_current_completed($feedbackid,
2480 $tmp = false,
2481 $courseid = false,
2482 $guestid = false) {
2483
0085fff8 2484 global $USER, $CFG, $DB;
37b86a39 2485
c70ad9f7 2486 $tmpstr = $tmp ? 'tmp' : '';
37b86a39 2487
9c5bc7a5
AG
2488 if (!$courseid) {
2489 if ($guestid) {
2490 $params = array('feedback'=>$feedbackid, 'guestid'=>$guestid);
2491 return $DB->get_record('feedback_completed'.$tmpstr, $params);
2492 } else {
2493 $params = array('feedback'=>$feedbackid, 'userid'=>$USER->id);
2494 return $DB->get_record('feedback_completed'.$tmpstr, $params);
c70ad9f7 2495 }
2496 }
37b86a39 2497
0085fff8 2498 $params = array();
37b86a39 2499
0085fff8 2500 if ($guestid) {
2501 $userselect = "AND fc.guestid = :guestid";
2502 $params['guestid'] = $guestid;
9c5bc7a5 2503 } else {
0085fff8 2504 $userselect = "AND fc.userid = :userid";
2505 $params['userid'] = $USER->id;
c70ad9f7 2506 }
9c5bc7a5
AG
2507 //if courseid is set the feedback is global.
2508 //there can be more than one completed on one feedback
31c0fa53 2509 $sql = "SELECT DISTINCT fc.*
abf1c50f 2510 FROM {feedback_value{$tmpstr}} fv, {feedback_completed{$tmpstr}} fc
0085fff8 2511 WHERE fv.course_id = :courseid
c70ad9f7 2512 AND fv.completed = fc.id
0085fff8 2513 $userselect
2514 AND fc.feedback = :feedbackid";
2515 $params['courseid'] = intval($courseid);
2516 $params['feedbackid'] = $feedbackid;
c70ad9f7 2517
d90e0ef3 2518 if (!$sqlresult = $DB->get_records_sql($sql, $params)) {
0085fff8 2519 return false;
2520 }
9c5bc7a5 2521 foreach ($sqlresult as $r) {
0085fff8 2522 return $DB->get_record('feedback_completed'.$tmpstr, array('id'=>$r->id));
c70ad9f7 2523 }
2524}
2525
37b86a39 2526/**
32be99dc 2527 * get the completeds depending on the given groupid.
2528 *
2529 * @global object
2530 * @global object
2531 * @param object $feedback
2532 * @param int $groupid
3a787259 2533 * @param int $courseid
32be99dc 2534 * @return mixed array of found completeds otherwise false
c70ad9f7 2535 */
59f89d80 2536function feedback_get_completeds_group($feedback, $groupid = false, $courseid = false) {
0085fff8 2537 global $CFG, $DB;
2538
9c5bc7a5 2539 if (intval($groupid) > 0) {
0085fff8 2540 $query = "SELECT fbc.*
abf1c50f 2541 FROM {feedback_completed} fbc, {groups_members} gm
0085fff8 2542 WHERE fbc.feedback = ?
2543 AND gm.groupid = ?
2544 AND fbc.userid = gm.userid";
2545 if ($values = $DB->get_records_sql($query, array($feedback->id, $groupid))) {
c70ad9f7 2546 return $values;
0085fff8 2547 } else {
2548 return false;
2549 }
2550 } else {
9c5bc7a5 2551 if ($courseid) {
59f89d80 2552 $query = "SELECT DISTINCT fbc.*
abf1c50f 2553 FROM {feedback_completed} fbc, {feedback_value} fbv
59f89d80 2554 WHERE fbc.id = fbv.completed
2555 AND fbc.feedback = ?
2556 AND fbv.course_id = ?
2557 ORDER BY random_response";
2558 if ($values = $DB->get_records_sql($query, array($feedback->id, $courseid))) {
2559 return $values;
2560 } else {
2561 return false;
2562 }
9c5bc7a5 2563 } else {
59f89d80 2564 if ($values = $DB->get_records('feedback_completed', array('feedback'=>$feedback->id))) {
2565 return $values;
2566 } else {
2567 return false;
2568 }
0085fff8 2569 }
c70ad9f7 2570 }
2571}
2572
37b86a39 2573/**
32be99dc 2574 * get the count of completeds depending on the given groupid.
2575 *
2576 * @global object
2577 * @global object
2578 * @param object $feedback
2579 * @param int $groupid
2580 * @param int $courseid
2581 * @return mixed count of completeds or false
c70ad9f7 2582 */
2583function feedback_get_completeds_group_count($feedback, $groupid = false, $courseid = false) {
0085fff8 2584 global $CFG, $DB;
2585
2586 if ($courseid > 0 AND !$groupid <= 0) {
2587 $sql = "SELECT id, COUNT(item) AS ci
37b86a39 2588 FROM {feedback_value}
0085fff8 2589 WHERE course_id = ?
2590 GROUP BY item ORDER BY ci DESC";
2591 if ($foundrecs = $DB->get_records_sql($sql, array($courseid))) {
c70ad9f7 2592 $foundrecs = array_values($foundrecs);
2593 return $foundrecs[0]->ci;
2594 }
2595 return false;
2596 }
9c5bc7a5
AG
2597 if ($values = feedback_get_completeds_group($feedback, $groupid)) {
2598 return count($values);
2599 } else {
c70ad9f7 2600 return false;
2601 }
2602}
2603
37b86a39 2604/**
32be99dc 2605 * deletes all completed-recordsets from a feedback.
2606 * all related data such as values also will be deleted
2607 *
2608 * @global object
2609 * @param int $feedbackid
2610 * @return void
c70ad9f7 2611 */
2612function feedback_delete_all_completeds($feedbackid) {
0085fff8 2613 global $DB;
2614
2615 if (!$completeds = $DB->get_records('feedback_completed', array('feedback'=>$feedbackid))) {
2616 return;
2617 }
9c5bc7a5 2618 foreach ($completeds as $completed) {
c70ad9f7 2619 feedback_delete_completed($completed->id);
2620 }
2621}
2622
37b86a39 2623/**
32be99dc 2624 * deletes a completed given by completedid.
2625 * all related data such values or tracking data also will be deleted
d4b1d58c 2626 *
32be99dc 2627 * @global object
2628 * @param int $completedid
2629 * @return boolean
c70ad9f7 2630 */
2631function feedback_delete_completed($completedid) {
516c5eca
PS
2632 global $DB, $CFG;
2633 require_once($CFG->libdir.'/completionlib.php');
0085fff8 2634
2635 if (!$completed = $DB->get_record('feedback_completed', array('id'=>$completedid))) {
c70ad9f7 2636 return false;
2637 }
516c5eca 2638
8a41eb4f
AG
2639 if (!$feedback = $DB->get_record('feedback', array('id'=>$completed->feedback))) {
2640 return false;
2641 }
516c5eca 2642
8a41eb4f
AG
2643 if (!$course = $DB->get_record('course', array('id'=>$feedback->course))) {
2644 return false;
2645 }
516c5eca 2646
8a41eb4f
AG
2647 if (!$cm = get_coursemodule_from_instance('feedback', $feedback->id)) {
2648 return false;
2649 }
2650
c70ad9f7 2651 //first we delete all related values
0085fff8 2652 $DB->delete_records('feedback_value', array('completed'=>$completed->id));
37b86a39 2653
c70ad9f7 2654 //now we delete all tracking data
9c5bc7a5
AG
2655 $params = array('completed'=>$completed->id, 'feedback'=>$completed->feedback);
2656 if ($tracking = $DB->get_record('feedback_tracking', $params)) {
0085fff8 2657 $DB->delete_records('feedback_tracking', array('completed'=>$completed->id));
c70ad9f7 2658 }
37b86a39 2659
8a41eb4f
AG
2660 // Update completion state
2661 $completion = new completion_info($course);
2662 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
2663 $completion->update_state($cm, COMPLETION_INCOMPLETE, $completed->userid);
2664 }
c70ad9f7 2665 //last we delete the completed-record
0085fff8 2666 return $DB->delete_records('feedback_completed', array('id'=>$completed->id));
c70ad9f7 2667}
2668
2669////////////////////////////////////////////////
2670////////////////////////////////////////////////
2671////////////////////////////////////////////////
2672//functions to handle sitecourse mapping
2673////////////////////////////////////////////////
2674
37b86a39 2675/**
32be99dc 2676 * checks if the course and the feedback is in the table feedback_sitecourse_map.
2677 *
2678 * @global object
2679 * @param int $feedbackid
2680 * @param int $courseid
2681 * @return int the count of records
c70ad9f7 2682 */
2683function feedback_is_course_in_sitecourse_map($feedbackid, $courseid) {
0085fff8 2684 global $DB;
9c5bc7a5
AG
2685 $params = array('feedbackid'=>$feedbackid, 'courseid'=>$courseid);
2686 return $DB->count_records('feedback_sitecourse_map', $params);
c70ad9f7 2687}
2688
37b86a39 2689/**
32be99dc 2690 * checks if the feedback is in the table feedback_sitecourse_map.
2691 *
2692 * @global object
2693 * @param int $feedbackid
2694 * @return boolean
c70ad9f7 2695 */
2696function feedback_is_feedback_in_sitecourse_map($feedbackid) {
436bbff3 2697 global $DB;
0085fff8 2698 return $DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$feedbackid));
c70ad9f7 2699}
2700
37b86a39 2701/**
32be99dc 2702 * gets the feedbacks from table feedback_sitecourse_map.
2703 * this is used to show the global feedbacks on the feedback block
2704 * all feedbacks with the following criteria will be selected:<br />
2705 *
2706 * 1) all feedbacks which id are listed together with the courseid in sitecoursemap and<br />
2707 * 2) all feedbacks which not are listed in sitecoursemap
2708 *
2709 * @global object
2710 * @param int $courseid
2711 * @return array the feedback-records
c70ad9f7 2712 */
2713function feedback_get_feedbacks_from_sitecourse_map($courseid) {
0085fff8 2714 global $DB;
37b86a39 2715
c70ad9f7 2716 //first get all feedbacks listed in sitecourse_map with named courseid
9c5bc7a5
AG
2717 $sql = "SELECT f.id AS id,
2718 cm.id AS cmid,
2719 f.name AS name,
2720 f.timeopen AS timeopen,
2721 f.timeclose AS timeclose
2722 FROM {feedback} f, {course_modules} cm, {feedback_sitecourse_map} sm, {modules} m
2723 WHERE f.id = cm.instance
0085fff8 2724 AND f.course = '".SITEID."'
37b86a39 2725 AND m.id = cm.module
0085fff8 2726 AND m.name = 'feedback'
37b86a39 2727 AND sm.courseid = ?
0085fff8 2728 AND sm.feedbackid = f.id";
37b86a39 2729
0085fff8 2730 if (!$feedbacks1 = $DB->get_records_sql($sql, array($courseid))) {
c70ad9f7 2731 $feedbacks1 = array();
2732 }
37b86a39 2733
c70ad9f7 2734 //second get all feedbacks not listed in sitecourse_map
2735 $feedbacks2 = array();
9c5bc7a5
AG
2736 $sql = "SELECT f.id AS id,
2737 cm.id AS cmid,
2738 f.name AS name,
2739 f.timeopen AS timeopen,
2740 f.timeclose AS timeclose
2741 FROM {feedback} f, {course_modules} cm, {modules} m
2742 WHERE f.id = cm.instance
0085fff8 2743 AND f.course = '".SITEID."'
2744 AND m.id = cm.module
2745 AND m.name = 'feedback'";
2746 if (!$allfeedbacks = $DB->get_records_sql($sql)) {
c70ad9f7 2747 $allfeedbacks = array();
2748 }
9c5bc7a5
AG
2749 foreach ($allfeedbacks as $a) {
2750 if (!$DB->record_exists('feedback_sitecourse_map', array('feedbackid'=>$a->id))) {
c70ad9f7 2751 $feedbacks2[] = $a;
2752 }
2753 }
37b86a39 2754
c70ad9f7 2755 return array_merge($feedbacks1, $feedbacks2);
37b86a39 2756
c70ad9f7 2757}
2758
37b86a39 2759/**
32be99dc 2760 * gets the courses from table feedback_sitecourse_map.
2761 *
2762 * @global object
2763 * @param int $feedbackid
2764 * @return array the course-records
c70ad9f7 2765 */
2766function feedback_get_courses_from_sitecourse_map($feedbackid) {
0085fff8 2767 global $DB;
37b86a39 2768
0085fff8 2769 $sql = "SELECT f.id, f.courseid, c.fullname, c.shortname
2770 FROM {feedback_sitecourse_map} f, {course} c
2771 WHERE c.id = f.courseid
2772 AND f.feedbackid = ?
2773 ORDER BY c.fullname";
37b86a39 2774
0085fff8 2775 return $DB->get_records_sql($sql, array($feedbackid));
37b86a39 2776
c70ad9f7 2777}
2778
37b86a39 2779/**
32be99dc 2780 * removes non existing courses or feedbacks from sitecourse_map.
2781 * it shouldn't be called all too often
2782 * a good place for it could be the mapcourse.php or unmapcourse.php
d4b1d58c 2783 *
32be99dc 2784 * @global object
2785 * @return void
c70ad9f7 2786 */
2787function feedback_clean_up_sitecourse_map() {
0085fff8 2788 global $DB;
2789
2790 $maps = $DB->get_records('feedback_sitecourse_map');
9c5bc7a5 2791 foreach ($maps as $map) {
0085fff8 2792 if (!$DB->get_record('course', array('id'=>$map->courseid))) {
9c5bc7a5
AG
2793 $params = array('courseid'=>$map->courseid, 'feedbackid'=>$map->feedbackid);
2794 $DB->delete_records('feedback_sitecourse_map', $params);
c70ad9f7 2795 continue;
2796 }
0085fff8 2797 if (!$DB->get_record('feedback', array('id'=>$map->feedbackid))) {
9c5bc7a5
AG
2798 $params = array('courseid'=>$map->courseid, 'feedbackid'=>$map->feedbackid);
2799 $DB->delete_records('feedback_sitecourse_map', $params);
c70ad9f7 2800 continue;
2801 }
37b86a39 2802
c70ad9f7 2803 }
2804}
2805
2806////////////////////////////////////////////////
2807////////////////////////////////////////////////
2808////////////////////////////////////////////////
2809//not relatable functions
2810////////////////////////////////////////////////
2811
37b86a39 2812/**
32be99dc 2813 * prints the option items of a selection-input item (dropdownlist).
2814 * @param int $startval the first value of the list
2815 * @param int $endval the last value of the list
2816 * @param int $selectval which item should be selected
2817 * @param int $interval the stepsize from the first to the last value
2818 * @return void
c70ad9f7 2819 */
9c5bc7a5
AG
2820function feedback_print_numeric_option_list($startval, $endval, $selectval = '', $interval = 1) {
2821 for ($i = $startval; $i <= $endval; $i += $interval) {
2822 if ($selectval == ($i)) {
c70ad9f7 2823 $selected = 'selected="selected"';
9c5bc7a5 2824 } else {
c70ad9f7 2825 $selected = '';
2826 }
2827 echo '<option '.$selected.'>'.$i.'</option>';
2828 }
2829}
2830
37b86a39 2831/**
32be99dc 2832 * sends an email to the teachers of the course where the given feedback is placed.
2833 *
2834 * @global object
2835 * @global object
2836 * @uses FEEDBACK_ANONYMOUS_NO
2837 * @uses FORMAT_PLAIN
2838 * @param object $cm the coursemodule-record
2839 * @param object $feedback
2840 * @param object $course
2841 * @param int $userid
2842 * @return void
c70ad9f7 2843 */
c184660d 2844function feedback_send_email($cm, $feedback, $course, $userid) {
c6307ef2 2845 global $CFG, $DB;
37b86a39 2846
c70ad9f7 2847 if ($feedback->email_notification == 0) { // No need to do anything
2848 return;
2849 }
37b86a39 2850
0085fff8 2851 $user = $DB->get_record('user', array('id'=>$userid));
37b86a39 2852
84973212 2853 if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
2854 $groupmode = $cm->groupmode;
2855 } else {
2856 $groupmode = $course->groupmode;
2857 }
2858
2859 if ($groupmode == SEPARATEGROUPS) {
c6307ef2 2860 $groups = $DB->get_records_sql_menu("SELECT g.name, g.id
2861 FROM {groups} g, {groups_members} m
2862 WHERE g.courseid = ?
2863 AND g.id = m.groupid
2864 AND m.userid = ?
2865 ORDER BY name ASC", array($course->id, $userid));
c70ad9f7 2866 $groups = array_values($groups);
37b86a39 2867
c184660d 2868 $teachers = feedback_get_receivemail_users($cm->id, $groups);
c70ad9f7 2869 } else {
c184660d 2870 $teachers = feedback_get_receivemail_users($cm->id);
c70ad9f7 2871 }
37b86a39 2872
c70ad9f7 2873 if ($teachers) {
2874
2875 $strfeedbacks = get_string('modulenameplural', 'feedback');
2876 $strfeedback = get_string('modulename', 'feedback');
2877 $strcompleted = get_string('completed', 'feedback');
9c5bc7a5
AG
2878
2879 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
2880 $printusername = fullname($user);
2881 } else {
2882 $printusername = get_string('anonymous_user', 'feedback');
2883 }
37b86a39 2884
c70ad9f7 2885 foreach ($teachers as $teacher) {
39790bd8 2886 $info = new stdClass();
c70ad9f7 2887 $info->username = $printusername;
9c5bc7a5
AG
2888 $info->feedback = format_string($feedback->name, true);
2889 $info->url = $CFG->wwwroot.'/mod/feedback/show_entries.php?'.
2890 'id='.$cm->id.'&'.
2891 'userid='.$userid.'&'.
2892 'do_show=showentries';
c70ad9f7 2893
2894 $postsubject = $strcompleted.': '.$info->username.' -> '.$feedback->name;
c184660d 2895 $posttext = feedback_send_email_text($info, $course);
37b86a39 2896
9c5bc7a5
AG
2897 if ($teacher->mailformat == 1) {
2898 $posthtml = feedback_send_email_html($info, $course, $cm);
2899 } else {
2900 $posthtml = '';
2901 }
2902
2903 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
39790bd8 2904 $eventdata = new stdClass();
51c49cc0
AG
2905 $eventdata->name = 'submission';
2906 $eventdata->component = 'mod_feedback';
3b120e46 2907 $eventdata->userfrom = $user;
2908 $eventdata->userto = $teacher;
2909 $eventdata->subject = $postsubject;
2910 $eventdata->fullmessage = $posttext;
2911 $eventdata->fullmessageformat = FORMAT_PLAIN;
2912 $eventdata->fullmessagehtml = $posthtml;
2913 $eventdata->smallmessage = '';
7c7d3afa 2914 message_send($eventdata);
9c5bc7a5 2915 } else {
39790bd8 2916 $eventdata = new stdClass();
51c49cc0
AG
2917 $eventdata->name = 'submission';
2918 $eventdata->component = 'mod_feedback';
3b120e46 2919 $eventdata->userfrom = $teacher;
2920 $eventdata->userto = $teacher;
2921 $eventdata->subject = $postsubject;
2922 $eventdata->fullmessage = $posttext;
2923 $eventdata->fullmessageformat = FORMAT_PLAIN;
2924 $eventdata->fullmessagehtml = $posthtml;
2925 $eventdata->smallmessage = '';
7c7d3afa 2926 message_send($eventdata);
c70ad9f7 2927 }
2928 }
2929 }
2930}
2931
37b86a39 2932/**
32be99dc 2933 * sends an email to the teachers of the course where the given feedback is placed.
2934 *
2935 * @global object
2936 * @uses FORMAT_PLAIN
2937 * @param object $cm the coursemodule-record
2938 * @param object $feedback
2939 * @param object $course
2940 * @return void
c70ad9f7 2941 */
c184660d 2942function feedback_send_email_anonym($cm, $feedback, $course) {
c70ad9f7 2943 global $CFG;
37b86a39 2944
9c5bc7a5 2945 if ($feedback->email_notification == 0) { // No need to do anything
c70ad9f7 2946 return;
2947 }
37b86a39 2948
c184660d 2949 $teachers = feedback_get_receivemail_users($cm->id);
c70ad9f7 2950
2951 if ($teachers) {
2952
2953 $strfeedbacks = get_string('modulenameplural', 'feedback');
2954 $strfeedback = get_string('modulename', 'feedback');
2955 $strcompleted = get_string('completed', 'feedback');
2956 $printusername = get_string('anonymous_user', 'feedback');
37b86a39 2957
c70ad9f7 2958 foreach ($teachers as $teacher) {
39790bd8 2959 $info = new stdClass();
c70ad9f7 2960 $info->username = $printusername;
9c5bc7a5 2961 $info->feedback = format_string($feedback->name, true);
c70ad9f7 2962 $info->url = $CFG->wwwroot.'/mod/feedback/show_entries_anonym.php?id='.$cm->id;
2963
2964 $postsubject = $strcompleted.': '.$info->username.' -> '.$feedback->name;
c184660d 2965 $posttext = feedback_send_email_text($info, $course);
9c5bc7a5
AG
2966
2967 if ($teacher->mailformat == 1) {
2968 $posthtml = feedback_send_email_html($info, $course, $cm);
2969 } else {
2970 $posthtml = '';
2971 }
37b86a39 2972
39790bd8 2973 $eventdata = new stdClass();
51c49cc0
AG
2974 $eventdata->name = 'submission';
2975 $eventdata->component = 'mod_feedback';
3b120e46 2976 $eventdata->userfrom = $teacher;
2977 $eventdata->userto = $teacher;
2978 $eventdata->subject = $postsubject;
2979 $eventdata->fullmessage = $posttext;
2980 $eventdata->fullmessageformat = FORMAT_PLAIN;
2981 $eventdata->fullmessagehtml = $posthtml;
2982 $eventdata->smallmessage = '';
7c7d3afa 2983 message_send($eventdata);
c70ad9f7 2984 }
2985 }
2986}
2987
37b86a39 2988/**
32be99dc 2989 * send the text-part of the email
2990 *
2991 * @param object $info includes some infos about the feedback you want to send
2992 * @param object $course
2993 * @return string the text you want to post
c70ad9f7 2994 */
c184660d 2995function feedback_send_email_text($info, $course) {
2c979976 2996 $coursecontext = context_course::instance($course->id);
8ebbb06a
SH
2997 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
2998 $posttext = $courseshortname.' -> '.get_string('modulenameplural', 'feedback').' -> '.
c70ad9f7 2999 $info->feedback."\n";
3000 $posttext .= '---------------------------------------------------------------------'."\n";
3001 $posttext .= get_string("emailteachermail", "feedback", $info)."\n";
3002 $posttext .= '---------------------------------------------------------------------'."\n";
3003 return $posttext;
3004}
3005
3006
37b86a39 3007/**
32be99dc 3008 * send the html-part of the email
3009 *
3010 * @global object
3011 * @param object $info includes some infos about the feedback you want to send
3012 * @param object $course
3013 * @return string the text you want to post
c70ad9f7 3014 */
c184660d 3015function feedback_send_email_html($info, $course, $cm) {
c70ad9f7 3016 global $CFG;
2c979976 3017 $coursecontext = context_course::instance($course->id);
8ebbb06a 3018 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
9c5bc7a5
AG
3019 $course_url = $CFG->wwwroot.'/course/view.php?id='.$course->id;
3020 $feedback_all_url = $CFG->wwwroot.'/mod/feedback/index.php?id='.$course->id;
3021 $feedback_url = $CFG->wwwroot.'/mod/feedback/view.php?id='.$cm->id;
3022
3023 $posthtml = '<p><font face="sans-serif">'.
3024 '<a href="'.$course_url.'">'.$courseshortname.'</a> ->'.
3025 '<a href="'.$feedback_all_url.'">'.get_string('modulenameplural', 'feedback').'</a> ->'.
3026 '<a href="'.$feedback_url.'">'.$info->feedback.'</a></font></p>';
c70ad9f7 3027 $posthtml .= '<hr /><font face="sans-serif">';
3028 $posthtml .= '<p>'.get_string('emailteachermailhtml', 'feedback', $info).'</p>';
3029 $posthtml .= '</font><hr />';
3030 return $posthtml;
3031}
3032
32be99dc 3033/**
3034 * @param string $url
3035 * @return string
3036 */
c184660d 3037function feedback_encode_target_url($url) {
3038 if (strpos($url, '?')) {
3039 list($part1, $part2) = explode('?', $url, 2); //maximal 2 parts
3040 return $part1 . '?' . htmlentities($part2);
3041 } else {
3042 return $url;
3043 }
3044}
ab5bd34d 3045
0b29477b
SH
3046/**
3047 * Adds module specific settings to the settings block
3048 *
3049 * @param settings_navigation $settings The settings navigation object
3050 * @param navigation_node $feedbacknode The node to add module settings to
3051 */
9c5bc7a5
AG
3052function feedback_extend_settings_navigation(settings_navigation $settings,
3053 navigation_node $feedbacknode) {
3054
0b29477b 3055 global $PAGE, $DB;
ab5bd34d 3056
2c979976 3057 if (!$context = context_module::instance($PAGE->cm->id, IGNORE_MISSING)) {
3406acde 3058 print_error('badcontext');
c987dbad
AG
3059 }
3060
3406acde
SH
3061 if (has_capability('mod/feedback:edititems', $context)) {
3062 $questionnode = $feedbacknode->add(get_string('questions', 'feedback'));
9c5bc7a5
AG
3063
3064 $questionnode->add(get_string('edit_items', 'feedback'),
3065 new moodle_url('/mod/feedback/edit.php',
3066 array('id' => $PAGE->cm->id,
3067 'do_show' => 'edit')));
3068
3069 $questionnode->add(get_string('export_questions', 'feedback'),
3070 new moodle_url('/mod/feedback/export.php',
3071 array('id' => $PAGE->cm->id,
3072 'action' => 'exportfile')));
3073
3074 $questionnode->add(get_string('import_questions', 'feedback'),
3075 new moodle_url('/mod/feedback/import.php',
3076 array('id' => $PAGE->cm->id)));
3077
3078 $questionnode->add(get_string('templates', 'feedback'),
3079 new moodle_url('/mod/feedback/edit.php',
3080 array('id' => $PAGE->cm->id,
3081 'do_show' => 'templates')));
ab5bd34d 3082 }
3083
3406acde 3084 if (has_capability('mod/feedback:viewreports', $context)) {
0b29477b 3085 $feedback = $DB->get_record('feedback', array('id'=>$PAGE->cm->instance));
9c5bc7a5
AG
3086 if ($feedback->course == SITEID) {
3087 $feedbacknode->add(get_string('analysis', 'feedback'),
3088 new moodle_url('/mod/feedback/analysis_course.php',
3089 array('id' => $PAGE->cm->id,
3090 'course' => $PAGE->course->id,
3091 'do_show' => 'analysis')));
3092 } else {
3093 $feedbacknode->add(get_string('analysis', 'feedback'),
3094 new moodle_url('/mod/feedback/analysis.php',
3095 array('id' => $PAGE->cm->id,
3096 'course' => $PAGE->course->id,
3097 'do_show' => 'analysis')));
ab5bd34d 3098 }
ab5bd34d 3099
9c5bc7a5
AG
3100 $feedbacknode->add(get_string('show_entries', 'feedback'),
3101 new moodle_url('/mod/feedback/show_entries.php',
3102 array('id' => $PAGE->cm->id,
3103 'do_show' => 'showentries')));
ab5bd34d 3104 }
c987dbad 3105}
f1b9e2df 3106
3107function feedback_init_feedback_session() {
3108 //initialize the feedback-Session - not nice at all!!
3109 global $SESSION;
3110 if (!empty($SESSION)) {
3111 if (!isset($SESSION->feedback) OR !is_object($SESSION->feedback)) {
39790bd8