/**
  * Given a collection type and Item (Either User or Person),  try to find an existing model. If the model does
  * not exist, create it and populate the Item and type.
  * @param string $type
  * @param Item $person
  * @return The found or created model.
  * @throws NotSupportedException
  */
 public static function resolveByTypeAndPerson($type, Item $person)
 {
     assert('is_string($type)');
     assert('$person->id > 0');
     assert('$person instanceof Contact || $person instanceof User');
     $searchAttributeData = array();
     $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'type', 'operatorType' => 'equals', 'value' => $type), 2 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $person->getClassId('Item')));
     $searchAttributeData['structure'] = '1 and 2';
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('GameCollection');
     $where = RedBeanModelDataProvider::makeWhere('GameCollection', $searchAttributeData, $joinTablesAdapter);
     $models = self::getSubset($joinTablesAdapter, null, 2, $where, null);
     if (count($models) > 1) {
         $logContent = 'Duplicate Game Collection for Person: ' . $person->id . ' with type ' . $type;
         GamificationUtil::logAndNotifyOnDuplicateGameModel($logContent);
         return $models[0];
     }
     if (count($models) == 0) {
         $gameCollectionRules = GameCollectionRulesFactory::createByType($type);
         $gameCollection = new GameCollection();
         $gameCollection->type = $type;
         $gameCollection->person = $person;
         $gameCollection->serializedData = serialize($gameCollectionRules::makeDefaultData());
         return $gameCollection;
     }
     return $models[0];
 }
Example #2
0
 /**
  * Given an Item (Either User or Person),  try to find an existing model. If the model does
  * not exist, create it and populate the Item.
  * @param Item $person
  * @return The found or created model.
  * @throws NotSupportedException
  */
 public static function resolveByPerson(Item $person)
 {
     assert('$person->id > 0');
     assert('$person instanceof Contact || $person instanceof User');
     $searchAttributeData = array();
     $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $person->getClassId('Item')));
     $searchAttributeData['structure'] = '1';
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('GameCoin');
     $where = RedBeanModelDataProvider::makeWhere('GameCoin', $searchAttributeData, $joinTablesAdapter);
     $models = self::getSubset($joinTablesAdapter, null, 2, $where, null);
     if (count($models) > 1) {
         $logContent = 'Duplicate Game Coin for Person: ' . $person->id;
         GamificationUtil::logAndNotifyOnDuplicateGameModel($logContent);
         return $models[0];
     }
     if (count($models) == 0) {
         $gameCoin = new GameCoin();
         $gameCoin->person = $person;
         $gameCoin->value = 0;
         return $gameCoin;
     }
     return $models[0];
 }
 /**
  * @see GamificationUtil::logAndNotifyOnDuplicateGameModel($logContent for an explanation of why you would
  * use this method.
  */
 public function actionRepairGamification()
 {
     $duplicateModelsData = array();
     //Check GameCoin for duplication person models
     $gameCoinDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByPersonKey(GameCoin::getTableName());
     $duplicateModelsData['GameCoin'] = $gameCoinDuplicateData;
     //Check GameCollection, GameLevel, GamePoint, and GameScore for duplicate type/person models
     $gameCollectionDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GameCollection::getTableName());
     $duplicateModelsData['GameCollection'] = $gameCollectionDuplicateData;
     $gameLevelDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GameLevel::getTableName());
     $duplicateModelsData['GameLevel'] = $gameLevelDuplicateData;
     $gamePointDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GamePoint::getTableName());
     $duplicateModelsData['GamePoint'] = $gamePointDuplicateData;
     $gameScoreDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GameScore::getTableName());
     $duplicateModelsData['GameScore'] = $gameScoreDuplicateData;
     foreach ($duplicateModelsData as $modelClassName => $duplicatesData) {
         if (empty($duplicatesData)) {
             echo 'No duplicates found for ' . $modelClassName . "<BR>";
         } else {
             echo 'Duplicates discovered for ' . $modelClassName . "<BR>";
             foreach ($duplicatesData as $typePersonKeyDuplicateData) {
                 $searchAttributeData = array();
                 if ($modelClassName == 'GameCoin') {
                     $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $typePersonKeyDuplicateData['person_item_id']));
                     $searchAttributeData['structure'] = '1';
                 } else {
                     $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'type', 'operatorType' => 'equals', 'value' => $typePersonKeyDuplicateData['type']), 2 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $typePersonKeyDuplicateData['person_item_id']));
                     $searchAttributeData['structure'] = '1 and 2';
                 }
                 $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($modelClassName);
                 $where = RedBeanModelDataProvider::makeWhere($modelClassName, $searchAttributeData, $joinTablesAdapter);
                 $models = $modelClassName::getSubset($joinTablesAdapter, null, null, $where, null);
                 if ($modelClassName == 'GameCoin') {
                     echo $modelClassName . ' --- Quantity of duplicates: ' . count($models) . ' --- for person_item_id: ' . $typePersonKeyDuplicateData['person_item_id'] . "<BR>";
                 } else {
                     echo $modelClassName . ' --- Quantity of duplicates: ' . count($models) . ' --- for person_item_id: ' . $typePersonKeyDuplicateData['person_item_id'] . ' with type: ' . $typePersonKeyDuplicateData['type'] . "<BR>";
                 }
                 $messageContent = null;
                 GamificationUtil::removeDuplicatesByModels($models, $messageContent);
                 echo $messageContent;
             }
         }
     }
     echo "<BR>" . 'Repair complete.' . "<BR>";
 }
 public function testRemoveDuplicatesByModelsGameCollection()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $models = array();
     $gameCollection = new GameCollection();
     $gameCollection->person = $super;
     $gameCollection->type = 'Basketball';
     $gameCollection->serializedData = serialize(array('something'));
     $gameCollection->save();
     $models[] = $gameCollection;
     $gameCollection2 = new GameCollection();
     $gameCollection2->person = $super;
     $gameCollection2->type = 'Basketball';
     $gameCollection2->serializedData = serialize(array('something2'));
     $gameCollection2->save();
     $models[] = $gameCollection2;
     $messageContent = null;
     $this->assertEquals(2, count(GameCollection::getAll()));
     GamificationUtil::removeDuplicatesByModels($models, $messageContent);
     $gameCollections = GameCollection::getAll();
     $this->assertEquals(1, count($gameCollections));
     $this->assertNotNull($messageContent);
 }