summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Tänzer <neo@nhng.de>2011-06-28 20:11:03 +0200
committerMichael Tänzer <neo@nhng.de>2011-06-28 20:11:03 +0200
commitbb3e4e892f8bd90f59c3000480776d70449d1eaf (patch)
tree58122de0562edf5cd39a04c99dccfa7291838259
parentc01c319a9963a06eb9ca7670caea196f6859efaa (diff)
downloadcacert-mgr-bb3e4e892f8bd90f59c3000480776d70449d1eaf.tar.gz
cacert-mgr-bb3e4e892f8bd90f59c3000480776d70449d1eaf.tar.xz
cacert-mgr-bb3e4e892f8bd90f59c3000480776d70449d1eaf.zip
Move assurer challenge action logic into model
Signed-off-by: Michael Tänzer <neo@nhng.de>
-rw-r--r--manager/application/controllers/ManageAccountController.php22
-rw-r--r--manager/application/models/User.php73
2 files changed, 78 insertions, 17 deletions
diff --git a/manager/application/controllers/ManageAccountController.php b/manager/application/controllers/ManageAccountController.php
index a9be220..f89b177 100644
--- a/manager/application/controllers/ManageAccountController.php
+++ b/manager/application/controllers/ManageAccountController.php
@@ -93,7 +93,7 @@ class ManageAccountController extends Zend_Controller_Action
// Form is valid -> get values for processing
$values = $form->getValues();
- // Get user data
+ // Get current user
$user = Default_Model_User::findCurrentUser();
$this->view->adminIncreasesDone = array();
@@ -129,19 +129,9 @@ class ManageAccountController extends Zend_Controller_Action
$values = $form->getValues();
// Get user data
- $user['id'] = $this->getUserId();
-
- // Assign the assurer challenge
- $challenge = array(); // Make sure the array is empty
- $challenge['user_id'] = $user['id'];
- $challenge['variant_id'] = $values['variant'];
- $challenge['pass_date'] = date('Y-m-d H:i:s');
- $this->db->insert('cats_passed', $challenge);
-
- // Maybe user is now assurer
- $this->fixAssurerFlag($user['id']);
+ $user = Default_Model_User::findCurrentUser();
- return;
+ $user->assignChallenge(1, $values['variant']);
}
public function flagsAction()
@@ -251,10 +241,8 @@ class ManageAccountController extends Zend_Controller_Action
$variant = new Zend_Form_Element_Select('variant');
$variant->setLabel(I18n::_('Variant'));
- // Get the available variants from the database
- $query = 'select `id`, `test_text` from `cats_variant`
- where `type_id` = 1';
- $options = $this->db->fetchPairs($query);
+ $options =
+ Default_Model_User::getAvailableChallengeVariants($this->db, 1);
$variant->setMultiOptions($options)
->setRequired(true);
$form->addElement($variant);
diff --git a/manager/application/models/User.php b/manager/application/models/User.php
index 86556de..7bf4030 100644
--- a/manager/application/models/User.php
+++ b/manager/application/models/User.php
@@ -333,4 +333,77 @@ class Default_Model_User {
throw new Exception(
__METHOD__ . ': '.$this->id.' We have reached unreachable code');
}
+
+ /**
+ * Get the challenge types that are available in the database
+ *
+ * @param $db Zend_Db_Adapter_Abstract
+ * The database connection to use
+ *
+ * @return array(int => string)
+ */
+ public static function getAvailableChallengeTypes(
+ Zend_Db_Adapter_Abstract $db) {
+ $query = 'select `id`, `type_text` from `cats_type`';
+ return $db->fetchPairs($query);
+ }
+
+ /**
+ * Get the challenge variants for this type that are available in the
+ * database
+ *
+ * @param $db Zend_Db_Adapter_Abstract
+ * The database connection to use
+ * @param $type int
+ * The type of challenge you want to get the variants of
+ *
+ * @return array(int => string)
+ */
+ public static function getAvailableChallengeVariants(
+ Zend_Db_Adapter_Abstract $db, $type) {
+ $query = 'select `id`, `test_text` from `cats_variant`
+ where `type_id` = :type';
+ $query_params['type'] = $type;
+ return $db->fetchPairs($query, $query_params);
+ }
+
+ /**
+ * Assign the challenge to the user
+ *
+ * @param $type int
+ * The type of the challenge, has to be one of the keys returned by
+ * getAvailableChallengeTypes()
+ * @param $variant int
+ * The variant of the challenge, has to be one of the keys returned by
+ * getAvailableChallengeVariants()
+ * @param $date Zend_Date
+ * The date the challenge was passed, defaults to current time
+ */
+ public function assignChallenge($type, $variant, Zend_Date $date = null) {
+ $types = self::getAvailableChallengeTypes($this->db);
+ if (!isset($types[$type])) {
+ throw new Exception(
+ __METHOD__ . ': got wrong challenge type '.$type.' when '.
+ 'assigning challenge to user '.$this->id);
+ }
+
+ $variants = self::getAvailableChallengeVariants($this->db, $type);
+ if (!isset($variants[$variant])) {
+ throw new Exception(
+ __METHOD__ . ': got wrong challenge variant '.$variant.' when '.
+ 'assigning challenge to user '.$this->id);
+ }
+
+ if ($date === null) {
+ $date = new Zend_Date();
+ }
+
+ $challenge = array();
+ $challenge['user_id'] = $this->id;
+ $challenge['variant_id'] = $variant;
+ $challenge['pass_date'] = $date->toString('Y-m-d H:i:s');
+ $this->db->insert('cats_passed', $challenge);
+
+ $this->fixAssurerFlag();
+ }
} \ No newline at end of file