Select Git revision
RequestNotification.php
RequestNotification.php 6.35 KiB
<?php
class App_RequestNotification
{
static protected $_notifications;
static protected function _loadNotifications()
{
if (Unl_Util::isArray(self::$_notifications)) {
return;
}
self::$_notifications = array();
$users = Auth_UserModel::findAll();
$userRoles = Requests_ApprovalRoleModel::findByUser($users);
$allRoles = Requests_ApprovalRoleModel::findAll();
$ownedRequests = Requests_RequestModel::findByUser($users);
$roleRequests = Requests_RequestModel::findByRole($allRoles);
foreach ($users as $user) {
$myRoles = $userRoles[$user->getId()];
unset($myRoles[1]);
$myRequests = new Unl_Model_Collection('Requests_RequestModel');
foreach ($myRoles as $role) {
if ($roleRequests[$role->getId()]) {
$myRequests->merge($roleRequests[$role->getId()]);
}
}
$myOwnedRequests = $ownedRequests[$user->getId()];
$myOwnedRequestsToUnset = array();
foreach ($myOwnedRequests as $id => $myOwnedRequest) {
if (!$myOwnedRequest->isSubmitterAttentionRequired()) {
$myOwnedRequestsToUnset[] = $id;
}
}
foreach ($myOwnedRequestsToUnset as $id) {
unset ($myOwnedRequests[$id]);
}
$myRequests->merge($myOwnedRequests);
$myVotes = Requests_ApproverVoteModel::findUsersVotesForRequests($user, $myRequests);
$myRequestCount = count($myRequests);
$myVotedRequests = array();
if (count($myRequests) > 0) {
$myRequestStacks = Requests_StackModel::findForRequests($myRequests);
}
foreach ($myVotes as $myVote) {
if ($myVote->getApprovalAction() != $myRequestStacks[$myVote->getRequest()]->getAction()) {
continue;
}
// THIS IS A HACK TO EXCLUDE PRE-VOTES FROM PREVIOUSLY TABLED VOTES OF THE SAME ACTION.
// IF ANY [VOTING PERIOD/TIME BETWEEN VOTING PERIODS] IS EVER [LONGER/SHORTER] THAN 14 DAYS IT WILL NO LONGER WORK!
if ($myVote->getTime()->getTimestamp() < time() - 60*60*24*14) {
continue;
}
$myVotedRequests[] = $myVote->getRequest();
}
if ($myRequestCount <= 0) {
continue;
}
foreach ($myRequests as $key => $myRequest) {
if (in_array($myRequest->getId(), $myVotedRequests)) {
$myRequestCount--;
}
}
if ($myRequestCount <= 0) {
continue;
}
self::$_notifications[$user->getId()] = array(
'user' => $user,
'requestCount' => $myRequestCount
);
}
}
public static function listNotifications()
{
self::_loadNotifications();
foreach (self::$_notifications as $userNotification) {
$user = $userNotification['user'];
$requestCount = $userNotification['requestCount'];
echo $user->getUsername() . ' has ' . $requestCount . ' pending requests.' . "\n";
}
}
public static function sendNotifications()
{
self::_loadNotifications();
foreach (self::$_notifications as $userNotification) {
$user = $userNotification['user'];
$requestCount = $userNotification['requestCount'];
$mail = new Zend_Mail();
$mail->addTo($user->getEmail(), $user->getFirstName() . ' ' . $user->getLastName());
$mail->setFrom('automated@courseapproval.unl.edu', 'Course Approval');
$mail->setSubject('Curriculum Approval: You have ' . $requestCount . ' pending requests.');
$mail->setBodyText(
<<<EOF
Please log in to courseapproval.unl.edu to view and respond to your pending
course requests.
1. Copy and paste or type courseapproval.unl.edu into to address bar of your
Internet browser and log in to the system using your Blackboard username and
password. You may also access the system through the ACE web site: Go to
ace.unl.edu, click on Course Submission under Navigation, then click on the
link on that page.
2. After you have logged in, click on "My Requests".
3. Course requests with a status of "decision" require you attention.
4. Upon determining your action, click on the drop-down box under "status" and
make your selection.
5. Finally, click on the "Submit Decisions" button to process your decisions.
For more information about these automated e-mails go to ace.unl.edu.
EOF
);
$mail->setBodyHtml(
<<<EOF
<p>
Please log in to <a href="courseapproval.unl.edu">courseapproval.unl.edu</a>
to view and respond to your pending course requests.
</p>
<ol>
<li>
Copy and paste or type courseapproval.unl.edu into to address bar of your Internet
browser and log in to the system using your Blackboard username and password. You
may also access the system through the ACE web site: Go to ace.unl.edu, click on
Course Submission under Navigation, then click on the link on that page.
</li>
<li>After you have logged in, click on "My Requests".</li>
<li>Course requests with a status of "decision" require you attention.</li>
<li>Upon determining your action, click on the drop-down box under "status" and make your selection.</li>
<li>Finally, click on the "Submit Decisions" button to process your decisions.</li>
</ol>
<p>For more information about these automated e-mails go to ace.unl.edu.</p>
EOF
);
$mail->send();
}
}
public static function scheduledNotifications()
{
self::sendNotifications();
$cronJob = Cron_JobsModel::fetchNew();
$cronDate = new Zend_Date();
$cronDate->addDay(2);
$cronDate->setHour(1);
$cronDate->setMinute(0);
$cronDate->setSecond(0);
$cronJob->setDate($cronDate);
$cronJob->setClass(__CLASS__);
$cronJob->setMethod(__FUNCTION__);
$collection = new Unl_Model_Collection('Cron_JobsModel');
$collection[] = $cronJob;
Cron_JobsModel::save($collection);
}
}