Skip to content
Snippets Groups Projects
Commit d977d746 authored by Tim Steiner's avatar Tim Steiner
Browse files

Scheduled email alerts.

parent 5c45aef0
Branches
Tags
No related merge requests found
......@@ -31,6 +31,21 @@ class Auth_UserModel extends Unl_Model {
}
static public function findAll()
{
$db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db);
$select->from(array('u' => 'creqUsers'));
$records = $select->query()->fetchAll();
$userIds = array();
foreach ($records as $record) {
$userIds[] = $record['userId'];
}
return self::find($userIds);
}
public static function findByUsername($username)
{
$db = Zend_Registry::get('db');
......
......@@ -34,6 +34,22 @@ class Cron_JobsModel extends Unl_Model
}
}
static public function fetchNew()
{
$data = array(
'date' => NULL,
'hasStarted' => 'no',
'hasRun' => 'no',
'class' => NULL,
'method' => NULL,
'arguments' => serialize(array())
);
$new = new self($data);
$new->_setClean();
return $new;
}
static public function runPendingJobs()
{
......@@ -182,6 +198,11 @@ class Cron_JobsModel extends Unl_Model
return $this->_data['cronJobId'];
}
public function setDate(Zend_Date $date)
{
$this->_data['date'] = $date->getTimestamp();
}
public function setHasStarted($running = true)
{
if ($running) {
......@@ -199,4 +220,23 @@ class Cron_JobsModel extends Unl_Model
$this->_data['hasRun'] = 'no';
}
}
public function setClass($class)
{
$this->_data['class'] = $class;
}
public function setMethod($method)
{
$this->_data['method'] = $method;
}
public function setArguments($arguments)
{
if (!is_array($arguments)) {
throw new Zend_Exception('$arguments must be an array.');
}
$this->_data['arguments'] = serialize($arguments);
}
}
\ No newline at end of file
<?php
class Requests_NotificationController extends App_Controller_Action
{
public function indexAction()
{
App_RequestNotification::listNotifications();
$this->_disableLayoutAndView();
}
}
\ No newline at end of file
<?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()];
foreach ($myOwnedRequests as $id => $myOwnedRequest) {
if (!$myOwnedRequest->isSubmitterAttentionRequired()) {
unset($myOwnedRequests[$id]);
}
}
$myRequests->merge($myOwnedRequests);
$myVotes = Requests_ApproverVoteModel::findUsersVotesForRequsets($user, $myRequests);
$myRequestCount = count($myRequests);
$myVotedRequests = array();
foreach ($myVotes as $myVote) {
$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 requsets.');
$mail->setBodyText('Please log in to courseapproval.unl.edu to view and respond to your pending requsets.');
$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);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment