/** * Common code for sending confirmation and cancellation notices * * @param string $postsubject Subject of the email * @param string $posttext Plain text contents of the email * @param string $posttextmgrheading Header to prepend to $posttext in manager email * @param string $notificationtype The type of notification to send * @see {{MDL_F2F_INVITE}} * @param class $facetoface record from the facetoface table * @param class $session record from the facetoface_sessions table * @param integer $userid ID of the recipient of the email * @returns string Error message (or empty string if successful) */ function facetoface_send_notice($postsubject, $posttext, $posttextmgrheading, $notificationtype, $facetoface, $session, $userid) { global $CFG, $DB; $user = $DB->get_record('user', array('id' => $userid)); if (!$user) { return 'error:invaliduserid'; } if (empty($postsubject) || empty($posttext)) { return ''; } // If no notice type is defined (TEXT or ICAL). if (!($notificationtype & MDL_F2F_BOTH)) { // If none, make sure they at least get a text email. $notificationtype |= MDL_F2F_TEXT; } // If we are cancelling, check if ical cancellations are disabled. if ($notificationtype & MDL_F2F_CANCEL && get_config(null, 'facetoface_disableicalcancel')) { $notificationtype |= MDL_F2F_TEXT; // Add a text notification. $notificationtype &= ~MDL_F2F_ICAL; // Remove the iCalendar notification. } // If we are sending an ical attachment, set file name. if ($notificationtype & MDL_F2F_ICAL) { if ($notificationtype & MDL_F2F_INVITE) { $attachmentfilename = 'invite.ics'; } else { if ($notificationtype & MDL_F2F_CANCEL) { $attachmentfilename = 'cancel.ics'; } } } // Do iCal attachement stuff. $icalattachments = array(); if ($notificationtype & MDL_F2F_ICAL) { if (get_config(null, 'facetoface_oneemailperday')) { // Keep track of all sessiondates. $sessiondates = $session->sessiondates; foreach ($sessiondates as $sessiondate) { $session->sessiondates = array($sessiondate); // One day at a time. $filename = facetoface_get_ical_attachment($notificationtype, $facetoface, $session, $user); $subject = facetoface_email_substitutions($postsubject, $facetoface->name, $facetoface->reminderperiod, $user, $session, $session->id); $body = facetoface_email_substitutions($posttext, $facetoface->name, $facetoface->reminderperiod, $user, $session, $session->id); $htmlbody = ''; // TODO. $icalattachments[] = array('filename' => $filename, 'subject' => $subject, 'body' => $body, 'htmlbody' => $htmlbody); } // Restore session dates. $session->sessiondates = $sessiondates; } else { $filename = facetoface_get_ical_attachment($notificationtype, $facetoface, $session, $user); $subject = facetoface_email_substitutions($postsubject, $facetoface->name, $facetoface->reminderperiod, $user, $session, $session->id); $body = facetoface_email_substitutions($posttext, $facetoface->name, $facetoface->reminderperiod, $user, $session, $session->id); $htmlbody = ''; // FIXME. $icalattachments[] = array('filename' => $filename, 'subject' => $subject, 'body' => $body, 'htmlbody' => $htmlbody); } } // Fill-in the email placeholders. $postsubject = facetoface_email_substitutions($postsubject, $facetoface->name, $facetoface->reminderperiod, $user, $session, $session->id); $posttext = facetoface_email_substitutions($posttext, $facetoface->name, $facetoface->reminderperiod, $user, $session, $session->id); $posttextmgrheading = facetoface_email_substitutions($posttextmgrheading, $facetoface->name, $facetoface->reminderperiod, $user, $session, $session->id); $posthtml = ''; // FIXME. if ($fromaddress = get_config(null, 'facetoface_fromaddress')) { $from = new stdClass(); $from->maildisplay = true; $from->email = $fromaddress; } else { $from = null; } $usercheck = $DB->get_record('user', array('id' => $userid)); // Send email with iCal attachment. if ($notificationtype & MDL_F2F_ICAL) { foreach ($icalattachments as $attachment) { if (!email_to_user($user, $from, $attachment['subject'], $attachment['body'], $attachment['htmlbody'], $attachment['filename'], $attachmentfilename)) { return 'error:cannotsendconfirmationuser'; } unlink($CFG->dataroot . '/' . $attachment['filename']); } } // Send plain text email. if ($notificationtype & MDL_F2F_TEXT) { if (!email_to_user($user, $from, $postsubject, $posttext, $posthtml)) { return 'error:cannotsendconfirmationuser'; } } // Manager notification. $manageremail = facetoface_get_manageremail($userid); if (!empty($posttextmgrheading) and !empty($manageremail) and $session->datetimeknown) { $managertext = $posttextmgrheading . $posttext; $manager = $user; $manager->email = $manageremail; // Leave out the ical attachments in the managers notification. if (!email_to_user($manager, $from, $postsubject, $managertext, $posthtml)) { return 'error:cannotsendconfirmationmanager'; } } // Third-party notification. if (!empty($facetoface->thirdparty) && ($session->datetimeknown || !empty($facetoface->thirdpartywaitlist))) { $thirdparty = $user; $recipients = explode(',', $facetoface->thirdparty); foreach ($recipients as $recipient) { $thirdparty->email = trim($recipient); // Leave out the ical attachments in the 3rd parties notification. if (!email_to_user($thirdparty, $from, $postsubject, $posttext, $posthtml)) { return 'error:cannotsendconfirmationthirdparty'; } } } return ''; }
/** * Send a notice. * * @param class $facetoface record from the facetoface table * @param class $session record from the facetoface_sessions table * @param integer $userid ID of the recipient of the email * @param array $params The parameters for the notification * @param int $icalattachmenttype The ical attachment type, or MDL_F2F_TEXT to disable ical attachments * @param int $icalattachmentmethod The ical method type: MDL_F2F_INVITE or MDL_F2F_CANCEL * @return string Error message (or empty string if successful) */ function facetoface_send_notice($facetoface, $session, $userid, $params, $icalattachmenttype = MDL_F2F_TEXT, $icalattachmentmethod = MDL_F2F_INVITE) { global $DB, $CFG; $user = $DB->get_record('user', array('id' => $userid)); if (!$user) { return 'userdoesnotexist'; } $notice = new facetoface_notification($params); if (isset($facetoface->ccmanager)) { $notice->ccmanager = $facetoface->ccmanager; } $notice->set_facetoface($facetoface); if (!isset($session->notifyuser)) { $session->notifyuser = true; } if (get_config(null, 'facetoface_oneemailperday')) { // Keep track of all sessiondates. $sessiondates = $session->sessiondates; foreach ($sessiondates as $sessiondate) { $session->sessiondates = array($sessiondate); // One day at a time. if ((int)$icalattachmenttype == MDL_F2F_BOTH) { $ical_attach = facetoface_get_ical_attachment($icalattachmentmethod, $facetoface, $session, $userid); $notice->set_ical_attachment($ical_attach); } $newevent = $notice->set_newevent($user, $session->id, $sessiondate); // print_object($newevent); if ($session->notifyuser) { echo "Test this email HEre Only"; print_object($newevent); $notice->send_to_user($newevent, $user, $session->id, $sessiondate); } $notice->send_to_manager($newevent, $user, $session->id); $notice->send_to_thirdparty($newevent, $user, $session->id); //@unlink($CFG->dataroot . DIRECTORY_SEPARATOR . $newevent->attachment); } // Restore session dates. $session->sessiondates = $sessiondates; } else { if ((int)$icalattachmenttype == MDL_F2F_BOTH) { // $ical_attach = facetoface_get_ical_attachment($icalattachmentmethod, $facetoface, $session, $userid); //$notice->set_ical_attachment($ical_attach); } // print_object($session); $newevent = $notice->set_newevent($user, $session->id); // print_object($newevent->userto); if ($session->notifyuser) { $notice->send_to_user($newevent, $user, $session->id); } $notice->send_to_manager($newevent, $user, $session->id); $notice->send_to_thirdparty($newevent, $user, $session->id); @unlink($CFG->dataroot . DIRECTORY_SEPARATOR . $newevent->attachment); } return ''; }