summaryrefslogtreecommitdiff
path: root/manager/application/models
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 /manager/application/models
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>
Diffstat (limited to 'manager/application/models')
-rw-r--r--manager/application/models/User.php73
1 files changed, 73 insertions, 0 deletions
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