/**
 * Event that sends confirmation notification after user successfully booked
 * TODO this should be rewritten for moodle 2.6 onwards
 *
 * @param object $eventdata data of user and users booking details
 * @return bool
 */
function booking_send_confirm_message($eventdata)
{
    global $DB, $CFG, $USER;
    $cmid = $eventdata->cmid;
    $optionid = $eventdata->optionid;
    $user = $eventdata->user;
    // Used to store the ical attachment (if required)
    $attachname = '';
    $attachment = '';
    $user = $DB->get_record('user', array('id' => $user->id));
    $bookingmanager = $DB->get_record('user', array('username' => $eventdata->booking->bookingmanager));
    $data = booking_generate_email_params($eventdata->booking, $eventdata->booking->option[$optionid], $user, $cmid);
    if ($data->status == get_string('booked', 'booking')) {
        $subject = get_string('confirmationsubject', 'booking', $data);
        $subjectmanager = get_string('confirmationsubjectbookingmanager', 'booking', $data);
        $message = booking_get_email_body($eventdata->booking, 'bookedtext', 'confirmationmessage', $data);
        // Generate ical attachment to go with the message.
        $ical = new booking_ical($eventdata->booking, $eventdata->booking->option[$optionid], $user, $bookingmanager);
        if ($attachment = $ical->get_attachment()) {
            $attachname = $ical->get_name();
        }
    } elseif ($data->status == get_string('onwaitinglist', 'booking')) {
        $subject = get_string('confirmationsubjectwaitinglist', 'booking', $data);
        $subjectmanager = get_string('confirmationsubjectwaitinglistmanager', 'booking', $data);
        $message = booking_get_email_body($eventdata->booking, 'waitingtext', 'confirmationmessagewaitinglist', $data);
    } else {
        $subject = "test";
        $subjectmanager = "tester";
        $message = "message";
    }
    $messagehtml = text_to_html($message, false, false, true);
    $errormessage = get_string('error:failedtosendconfirmation', 'booking', $data);
    $errormessagehtml = text_to_html($errormessage, false, false, true);
    $user->mailformat = 1;
    // Always send HTML version as well
    //implementing message send, but prior to moodle 2.6, that function does not
    //accept attachments, so have to call email_to_user in that case
    $messagedata = new stdClass();
    $messagedata->userfrom = $bookingmanager;
    if ($eventdata->booking->sendmailtobooker) {
        $messagedata->userto = $DB->get_record('user', array('id' => $USER->id));
    } else {
        $messagedata->userto = $DB->get_record('user', array('id' => $user->id));
    }
    $messagedata->subject = $subject;
    $messagedata->messagetext = $message;
    $messagedata->messagehtml = $messagehtml;
    $messagedata->attachment = $attachment;
    $messagedata->attachname = $attachname;
    booking_booking_confirmed($messagedata);
    if ($eventdata->booking->copymail) {
        $messagedata->userto = $bookingmanager;
        $messagedata->subject = $subjectmanager;
    }
    return true;
}
 /**
  * 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;
 }