/** * Deletes a booking option and the associated user answers * @param $bookingid the booking instance * @param $optionid the booking option * @return false if not successful, true on success */ function booking_delete_booking_option($booking, $optionid) { global $DB; $event = new stdClass(); if (!($option = $DB->get_record("booking_options", array("id" => $optionid)))) { return false; } $result = true; $params = array('bookingid' => $booking->id, 'optionid' => $optionid); $userids = $DB->get_fieldset_select('booking_answers', 'userid', 'bookingid = :bookingid AND optionid = :optionid', $params); foreach ($userids as $userid) { booking_check_unenrol_user($option, $booking, $userid); // Unenrol any users enroled via this option. } if (!$DB->delete_records("booking_answers", array("bookingid" => $booking->id, "optionid" => $optionid))) { $result = false; } // Delete calendar entry, if any $event->id = $DB->get_field('booking_options', 'calendarid', array('id' => $optionid)); if ($event->id > 0) { // Delete event if exist $event = calendar_event::load($event->id); $event->delete(true); } if (!$DB->delete_records("booking_options", array("id" => $optionid))) { $result = false; } return $result; }
/** * Deletes a single booking of a user if user cancels the booking, sends mail to bookingmanager. If there is a limit * book other user ans send mail to him. * @param $userid * @return true if booking was deleted successfully, otherwise false */ public function user_delete_response($userid) { global $USER, $DB; if (!$DB->delete_records('booking_answers', array('userid' => $userid, 'optionid' => $this->optionid))) { return false; } if ($userid == $USER->id) { $user = $USER; } else { $user = $DB->get_record('user', array('id' => $userid)); } /** log deletion of user * */ $event = \mod_booking\event\booking_cancelled::create(array('objectid' => $this->optionid, 'context' => context_module::instance($this->cm->id), 'relateduserid' => $user->id, 'other' => array('userid' => $user->id))); $event->trigger(); booking_check_unenrol_user($this->option, $this->booking, $user->id); $params = booking_generate_email_params($this->booking, $this->option, $user, $this->cm->id); $messagetext = get_string('deletedbookingmessage', 'booking', $params); if ($userid == $USER->id) { // I canceled the booking $deletedbookingusermessage = booking_get_email_body($this->booking, 'userleave', 'userleavebookedmessage', $params); $subject = get_string('userleavebookedsubject', 'booking', $params); } else { // Booking manager canceled the booking $deletedbookingusermessage = booking_get_email_body($this->booking, 'deletedtext', 'deletedbookingmessage', $params); $subject = get_string('deletedbookingsubject', 'booking', $params); } $bookingmanager = $DB->get_record('user', array('username' => $this->booking->bookingmanager)); $eventdata = new stdClass(); if ($this->booking->sendmail) { // Generate ical attachment to go with the message. $attachname = ''; $ical = new booking_ical($this->booking, $this->option, $user, $bookingmanager); if ($attachment = $ical->get_attachment(true)) { $attachname = $ical->get_name(); } $messagehtml = text_to_html($deletedbookingusermessage, false, false, true); if (isset($this->booking->sendmailtobooker) && $this->booking->sendmailtobooker) { $eventdata->userto = $USER; } else { $eventdata->userto = $user; } $eventdata->userfrom = $bookingmanager; $eventdata->subject = $subject; $eventdata->messagetext = $deletedbookingusermessage; $eventdata->messagehtml = $messagehtml; $eventdata->attachment = $attachment; $eventdata->attachname = $attachname; booking_booking_deleted($eventdata); if ($this->booking->copymail) { $eventdata->userto = $bookingmanager; booking_booking_deleted($eventdata); } } if ($this->option->limitanswers) { $maxplacesavailable = $this->option->maxanswers + $this->option->maxoverbooking; $bookedUsers = $DB->count_records("booking_answers", array('optionid' => $this->optionid, 'waitinglist' => 0)); $waitingUsers = $DB->count_records("booking_answers", array('optionid' => $this->optionid, 'waitinglist' => 1)); $allUsersCount = $bookedUsers + $waitingUsers; if ($waitingUsers > 0 && $this->option->maxanswers > $bookedUsers) { $newUser = $DB->get_record_sql('SELECT * FROM {booking_answers} WHERE optionid = ? AND waitinglist = 1 ORDER BY timemodified ASC', array($this->optionid), IGNORE_MULTIPLE); $newUser->waitinglist = 0; $DB->update_record("booking_answers", $newUser); booking_check_enrol_user($this->option, $this->booking, $newUser->userid); if ($this->booking->sendmail == 1 || $this->booking->copymail) { $newbookeduser = $DB->get_record('user', array('id' => $newUser->userid)); $params = booking_generate_email_params($this->booking, $this->option, $newbookeduser, $this->cm->id); $messagetextnewuser = booking_get_email_body($this->booking, 'statuschangetext', 'statuschangebookedmessage', $params); $messagehtml = text_to_html($messagetextnewuser, false, false, true); // Generate ical attachment to go with the message. $attachname = ''; $ical = new booking_ical($this->booking, $this->option, $newbookeduser, $bookingmanager); if ($attachment = $ical->get_attachment()) { $attachname = $ical->get_name(); } $eventdata->userto = $newbookeduser; $eventdata->userfrom = $bookingmanager; $eventdata->subject = get_string('statuschangebookedsubject', 'booking', $params); $eventdata->messagetext = $messagetextnewuser; $eventdata->messagehtml = $messagehtml; $eventdata->attachment = $attachment; $eventdata->attachname = $attachname; if ($this->booking->sendmail == 1) { booking_booking_deleted($eventdata); } if ($this->booking->copymail) { $eventdata->userto = $bookingmanager; booking_booking_deleted($eventdata); } } } } return true; }