From f23c58bd2b7b0f73466ea5db4e2b74e91d87c5b6 Mon Sep 17 00:00:00 2001
From: Tim Steiner <tsteiner2@unl.edu>
Date: Wed, 19 Sep 2007 22:16:27 +0000
Subject: [PATCH] Updates to Voting ApprovalAction

---
 .../models/rows/ApprovalActionQueue.php       |  2 +-
 .../models/rows/ApprovalActionVote.php        | 45 ++++++++++++++++++-
 application/models/tables/ApproverVotes.php   | 11 +++++
 application/views/home.xhtml                  |  6 +--
 4 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/application/models/rows/ApprovalActionQueue.php b/application/models/rows/ApprovalActionQueue.php
index 5e040aa1..75814e2b 100644
--- a/application/models/rows/ApprovalActionQueue.php
+++ b/application/models/rows/ApprovalActionQueue.php
@@ -110,7 +110,7 @@ class ApprovalActionQueue extends ApprovalAction
      */
     public function userMadeDecision(Request $request, User $user, $decision)
     {
-        $vote = $this->getUserDecision($request, $user);
+        $vote = ApproverVotes::getInstance()->fetchByRequestUserAndAction($request, $user, $this);
         if ($vote) {
             $vote->vote = $decision;
         } else {
diff --git a/application/models/rows/ApprovalActionVote.php b/application/models/rows/ApprovalActionVote.php
index 26e61a6d..001c73bc 100644
--- a/application/models/rows/ApprovalActionVote.php
+++ b/application/models/rows/ApprovalActionVote.php
@@ -9,6 +9,8 @@ require_once 'ApprovalActionRow/Interface.php';
  */
 class ApprovalActionVote extends ApprovalActionQueue
 {
+    protected static $_preVotePrefix = '*PREVOTE*';
+
     public function _init()
     {
         parent::_init();
@@ -33,6 +35,29 @@ class ApprovalActionVote extends ApprovalActionQueue
         $this->_registerRelation($votesRelation);
     }
 
+    /**
+     * Reutrns an array of strings, corresponding to the possible
+     * results of performing this action.  If a customized action can only
+     * result a subset of the possible results, return that subset.
+     *
+     * @return array
+     */
+    public function getResultStatusStrings(Request $request = null, User $user = null)
+    {
+        $resultStatusStrings = parent::getResultStatusStrings();
+        if ($request && $user) {
+            $prevote = ApproverVotes::getInstance()->fetchByRequestAndUser($request, $user);
+            if ($prevote) {
+                $resultStatusStrings = array_merge(
+                    array(self::$_preVotePrefix . ' ' . $prevote->vote
+                          => self::$_preVotePrefix . ' ' . $prevote->vote),
+                    $resultStatusStrings
+                );
+            }
+        }
+        return $resultStatusStrings;
+    }
+
     /**
      * Initiates the action, causing it to begin processing the given request.
      * When consideration finishes, the parent chain will be called again.
@@ -69,7 +94,12 @@ class ApprovalActionVote extends ApprovalActionQueue
      */
     public function userMadeDecision(Request $request, User $user, $decision)
     {
-        $vote = $this->getUserDecision($request, $user);
+        if (substr($decision, 0, strlen(self::$_preVotePrefix)) == self::$_preVotePrefix) {
+            // Don't submit prevotes as final votes.
+            return;
+        }
+
+        $vote = parent::getUserDecision($request, $user);
         if ($vote) {
             // TODO: don't fail silently when someone tries to vote again.
             return;
@@ -78,6 +108,19 @@ class ApprovalActionVote extends ApprovalActionQueue
         parent::userMadeDecision($request, $user, $decision);
     }
 
+    public function getUserDecision(Request $request, User $user)
+    {
+        $finalVote = parent::getUserDecision($request, $user);
+        if ($finalVote) {
+            return $finalVote;
+        }
+
+        $preVote = ApproverVotes::getInstance()->fetchByRequestAndUser($request, $user);
+        if ($preVote) {
+            return self::$_preVotePrefix . ' ' . $preVote->vote;
+        }
+    }
+
     public function advanceQueue()
     {
 
diff --git a/application/models/tables/ApproverVotes.php b/application/models/tables/ApproverVotes.php
index 32134e71..58b74d6c 100644
--- a/application/models/tables/ApproverVotes.php
+++ b/application/models/tables/ApproverVotes.php
@@ -32,6 +32,17 @@ class ApproverVotes extends Nmc_Db_Table
         return $new;
     }
 
+    public function fetchByRequestAndUser(Request $request, User $user)
+    {
+        $db = $this->getAdapter();
+        $where = array();
+        $where[] = $db->quoteInto('request=?', $request->getPrimaryKey());
+        $where[] = $db->quoteInto('user=?', $user->getPrimaryKey());
+        $where = implode(' AND ', $where);
+        $order = 'time';
+        return $this->fetchRow($where, $order);
+    }
+
     public function fetchByRequestUserAndAction(Request $request, User $user, ApprovalAction $action)
     {
         $db = $this->getAdapter();
diff --git a/application/views/home.xhtml b/application/views/home.xhtml
index 3a523cd7..8898b78a 100755
--- a/application/views/home.xhtml
+++ b/application/views/home.xhtml
@@ -6,7 +6,7 @@
                 Announcements
                 <img src="/images/arrow1.png" alt="arrow 1" class="toggle_arrow" />
             </h2>
-          <h3><em>User ID:</em> <?php echo Nmc_User::getInstance()->getUser()->getUserName(); ?></h3>
+          <h3><em>User ID:</em> <?php echo $this->user->getUserName(); ?></h3>
         </div>
       <div class="bl"></div>
     </div>
@@ -189,10 +189,10 @@
                         <?php
                         if ($request->getCurrentAction()) {
                             echo $this->formSelect('decisions[' . $request->getPrimaryKey() . ']',
-                                                   $request->getCurrentAction()->getUserDecision($request, Nmc_User::getInstance()->getUser()),
+                                                   $request->getCurrentAction()->getUserDecision($request, $this->user),
                                                    array('class' => 'decision_selection'),
                                                    array('_null' => "--Make a decision--")
-                                                   + $request->getCurrentAction()->getResultStatusStrings());
+                                                   + $request->getCurrentAction()->getResultStatusStrings($request, $this->user));
                             //
                         }
                         ?>                    </td>
-- 
GitLab