diff --git a/application/modules/requests/models/ApprovalActionVoteModel.php b/application/modules/requests/models/ApprovalActionVoteModel.php index d5d1ccefe9748264a731bf1f55ecf93fa695b7aa..1f6f25914ff0499c1abba3a53b12c219bfd3b639 100644 --- a/application/modules/requests/models/ApprovalActionVoteModel.php +++ b/application/modules/requests/models/ApprovalActionVoteModel.php @@ -24,6 +24,18 @@ class Requests_ApprovalActionVoteModel extends Requests_ApprovalActionQueueModel $objects[$objectId] = $object; } + $select = new Zend_Db_Select($db); + $select->from(array('v' => 'creqApproverVotes')); + if (Unl_Util::isArray($id)) { + $select->where('v.approvalAction IN(?)', $id); + } else { + $select->where('v.approvalAction = ?', $id); + } + $records = $select->query()->fetchAll(); + foreach ($records as $record) { + $objects[$record['approvalAction']]->_data['votes'][$record['request']][$record['approverVoteId']] = $record; + } + if (Unl_Util::isArray($objects)) { return $objects; } else { @@ -37,13 +49,58 @@ class Requests_ApprovalActionVoteModel extends Requests_ApprovalActionQueueModel return new Unl_Model_Collection('Requests_RequestModel'); } - public function userMadeDecisions($requests, $requestDecisions) + public function finalize($requests) { - // Don't do anything here, this is an automated action. + // TODO: Tally votes and update the state of each request. + foreach ($requests as $request) { + $votes = $this->_data['votes'][$request->getId()]; + if (!Unl_Util::isArray($votes) || count($votes) == 0) { + $request->setState(null); + continue; + } + + $voteCount = count($votes); + if ($voteCount < $this->getQuorum()) { + $request->setState(null); + continue; + } + + + $voteTally = array(); + foreach ($votes as $vote) { + $voteTally[$vote['vote']]++; + } + if ($voteTally['Table']) { + $request->setState('Table'); + continue; + } + + // TODO: Handle Ties + $mostVotes = 0; + $winner = ''; + foreach ($voteTally as $name => $count) { + if ($count > $mostVotes) { + $winner = $name; + $mostVotes = $count; + } + } + + if ($this->_data['requiredVotes'] == 'plurality') { + $request->setState($winner); + } else if ($this->_data['requiredVotes'] == 'majority' && ($mostVotes / $voteCount) >= 0.5) { + $request->setState($winner); + } else if ($this->_data['requiredVotes'] == 'unanimity' && ($mostVotes / $voteCount) == 1.0) { + $request->setState($winner); + } else { + $request->setState(null); + } + } + + Requests_RequestModel::save($requests); } - public function finalize($requests) + public function getQuorum() { - // TODO: Tally votes and update the state of each request. + return $this->_data['quorum']; } } diff --git a/application/modules/requests/models/ApproverVoteModel.php b/application/modules/requests/models/ApproverVoteModel.php index 21f3caf5d41b54201edfba97ed8dd42f2454fc9b..b5c03013421aef2844d69b7247a85cc463c65e23 100644 --- a/application/modules/requests/models/ApproverVoteModel.php +++ b/application/modules/requests/models/ApproverVoteModel.php @@ -46,6 +46,7 @@ class Requests_ApproverVoteModel extends Unl_Model $select->where('a.request = ?', $requests->getId()); } $select->where('user = ?', $user->getId()); + $select->order('time'); $records = $db->query($select)->fetchAll(); $requestVoteIds = array();