function testSetChangedFlagsFromNothing2()
 {
     $groupHistory = new GroupHistoryModel();
     $groupHistory->setFromDataBaseRow(array('group_id' => 1, 'title' => '', 'description' => 'This group has no name', 'user_account_id' => 1, 'created_at' => '2014-02-01 10:00:00', 'url' => 'http://www.groupwithnoname.com', 'twitter_username' => 'noname', 'is_deleted' => 0, 'title_changed' => 0, 'description_changed' => 0, 'url_changed' => 0, 'twitter_username_changed' => 0, 'is_deleted_changed' => 0));
     $groupHistory->setChangedFlagsFromNothing();
     $this->assertEquals(false, $groupHistory->getTitleChanged());
     $this->assertEquals(true, $groupHistory->getDescriptionChanged());
     $this->assertEquals(true, $groupHistory->getUrlChanged());
     $this->assertEquals(true, $groupHistory->getTwitterUsernameChanged());
     $this->assertEquals(false, $groupHistory->getIsDeletedChanged());
     $this->assertEquals(true, $groupHistory->getIsNew());
 }
 public function ensureChangedFlagsAreSet(GroupHistoryModel $groupHistory)
 {
     global $DB;
     // do we already have them?
     if (!$groupHistory->isAnyChangeFlagsUnknown()) {
         return;
     }
     // load last.
     $stat = $DB->prepare("SELECT * FROM group_history WHERE group_id = :id AND created_at < :at " . "ORDER BY created_at DESC");
     $stat->execute(array('id' => $groupHistory->getId(), 'at' => $groupHistory->getCreatedAt()->format("Y-m-d H:i:s")));
     if ($stat->rowCount() == 0) {
         $groupHistory->setChangedFlagsFromNothing();
     } else {
         while ($groupHistory->isAnyChangeFlagsUnknown() && ($lastHistoryData = $stat->fetch())) {
             $lastHistory = new GroupHistoryModel();
             $lastHistory->setFromDataBaseRow($lastHistoryData);
             $groupHistory->setChangedFlagsFromLast($lastHistory);
         }
     }
     // Save back to DB
     $sqlFields = array();
     $sqlParams = array('id' => $groupHistory->getId(), 'created_at' => $groupHistory->getCreatedAt()->format("Y-m-d H:i:s"), 'is_new' => $groupHistory->getIsNew() ? 1 : 0);
     if ($groupHistory->getTitleChangedKnown()) {
         $sqlFields[] = " title_changed = :title_changed ";
         $sqlParams['title_changed'] = $groupHistory->getTitleChanged() ? 1 : -1;
     }
     if ($groupHistory->getDescriptionChangedKnown()) {
         $sqlFields[] = " description_changed = :description_changed ";
         $sqlParams['description_changed'] = $groupHistory->getDescriptionChanged() ? 1 : -1;
     }
     if ($groupHistory->getUrlChangedKnown()) {
         $sqlFields[] = " url_changed = :url_changed ";
         $sqlParams['url_changed'] = $groupHistory->getUrlChanged() ? 1 : -1;
     }
     if ($groupHistory->getTwitterUsernameChangedKnown()) {
         $sqlFields[] = " twitter_username_changed = :twitter_username_changed ";
         $sqlParams['twitter_username_changed'] = $groupHistory->getTwitterUsernameChanged() ? 1 : -1;
     }
     if ($groupHistory->getIsDuplicateOfIdChangedKnown()) {
         $sqlFields[] = " is_duplicate_of_id_changed  = :is_duplicate_of_id_changed ";
         $sqlParams['is_duplicate_of_id_changed'] = $groupHistory->getIsDuplicateOfIdChangedKnown() ? 1 : -1;
     }
     if ($groupHistory->getIsDeletedChangedKnown()) {
         $sqlFields[] = " is_deleted_changed = :is_deleted_changed ";
         $sqlParams['is_deleted_changed'] = $groupHistory->getIsDeletedChanged() ? 1 : -1;
     }
     $statUpdate = $DB->prepare("UPDATE group_history SET " . " is_new = :is_new, " . implode(" , ", $sqlFields) . " WHERE group_id = :id AND created_at = :created_at");
     $statUpdate->execute($sqlParams);
 }