Move assurer challenge action logic into model

Signed-off-by: Michael Tänzer <neo@nhng.de>
bug-1390
Michael Tänzer 13 years ago
parent c01c319a99
commit bb3e4e892f

@ -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);

@ -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();
}
}
Loading…
Cancel
Save