12 const TABLE_NAME =
"Achievement";
17 public $Description =
"";
18 public $UniqueRecords = 1;
25 public function __construct($src = null, $stripSlashes =
false) {
30 $this->_loadByRow($src, $stripSlashes);
31 }
else if (is_numeric($src) && intval($src) > 0) {
33 $this->_loadFilter(self::GetTableName(__CLASS__),
"Id = " . intval($src));
47 public static function Load ($idApp = 0, $limit = null, $offset = null, &$count = null, $returnArray =
false) {
50 $where = sprintf(
"(IdApp = 0 OR IdApp = %d)", $idApp);
52 return self::_load(self::GetTableName(__CLASS__), ($returnArray ?
"" : __CLASS__), $where,
"IdApp, Position", $limit, $offset, $count);
63 $query = sprintf(
"UPDATE %s SET Title = '%s', Description = '%s' WHERE Id = %d",
64 self::GetTableName(__CLASS__),
65 $Database->Escape($this->Title),
66 $Database->Escape($this->Description),
69 $this->Position = self::GetNextPosition($this->IdApp);
70 $query = sprintf(
"INSERT INTO %s (IdApp, Title, Description, UniqueRecords, Position) VALUES (%d, '%s', '%s', %d, %d)",
71 self::GetTableName(__CLASS__),
73 $Database->Escape($this->Title),
74 $Database->Escape($this->Description),
78 $saved = $Database->Query($query);
81 $this->Id = $Database->InsertedId();
88 private static function GetNextPosition($idApp) {
90 $count = self::CountRecords(self::GetTableName(__CLASS__), sprintf(
"IdApp = %d", $idApp));
92 $maxPosition = $count;
99 if ($this->Id > 0 && $this->Position > 0) {
101 $newPosition = $this->Position - 1;
103 $query = sprintf(
"UPDATE %s SET Position = Position + 1 WHERE Position = %d AND IdApp = %d",
104 self::GetTableName(__CLASS__), $newPosition, $this->IdApp);
105 if ($Database->Query($query)) {
107 $query = sprintf(
"UPDATE %s SET Position = %d WHERE Id = %d",
108 self::GetTableName(__CLASS__), $newPosition, $this->Id);
109 if ($Database->Query($query)) {
110 $this->Position = $newPosition;
114 $query = sprintf(
"UPDATE %s SET Position = Position - 1 WHERE Position <= %d AND IdApp = %d",
115 self::GetTableName(__CLASS__), $newPosition, $this->IdApp);
116 $Database->Query($query);
126 $maxPosition = self::GetNextPosition($this->IdApp) - 1;
127 if ($maxPosition > 0 && $this->Position < $maxPosition) {
129 $newPosition = $this->Position + 1;
131 $query = sprintf(
"UPDATE %s SET Position = Position - 1 WHERE Position = %d AND IdApp = %d",
132 self::GetTableName(__CLASS__), $newPosition, $this->IdApp);
133 if ($Database->Query($query)) {
135 $query = sprintf(
"UPDATE %s SET Position = %d WHERE Id = %d",
136 self::GetTableName(__CLASS__), $newPosition, $this->Id);
137 if ($Database->Query($query)) {
138 $this->Position = $newPosition;
142 $query = sprintf(
"UPDATE %s SET Position = Position + 1 WHERE Position <= %d AND IdApp = %d",
143 self::GetTableName(__CLASS__), $newPosition, $this->IdApp);
144 $Database->Query($query);
161 if ($this->_Delete(self::GetTableName(__CLASS__),
"Id = " . $this->Id)) {
163 $query = sprintf(
"UPDATE %s SET Position = Position - 1 WHERE Position > %d",
164 self::GetTableName(__CLASS__), $this->Position);
165 $Database->Query($query);
167 $this->_Delete(self::GetTableName(Achievement_User::class),
"IdAchievement = " . $this->Id);
177 self::TruncateClass(__CLASS__);
178 self::TruncateClass(Achievement_User::class);
191 $where = sprintf(
"(IdApp = 0 OR IdApp = %d)", $idApp);
193 $achievements = self::_load(self::GetTableName(__CLASS__), __CLASS__, $where,
"IdApp, Position");
194 foreach ($achievements as $achievement) {
195 $records = Achievement_User::LoadAccount($idAccount, $achievement->Id);
196 if (count($records) == 0 && $idAccount > 0) {
198 $newAchievement->IdAchievement = $achievement->Id;
199 $newAchievement->IdAccount = $idAccount;
200 $newAchievement->Progress = 0;
201 $records[] = $newAchievement;
204 foreach ($records as $progress1) {
205 if ($progress1->Progress >= 100)
208 foreach ($records as $progress) {
210 "Id" => $achievement->Id,
211 "Title" => $achievement->Title,
212 "Description" => $achievement->Description,
213 "Progress" => $progress->Progress,
214 "Finished" => $finished
228 public function PostProgress ($account, $progressValue, &$newProgress = 0, &$cannotRepeat = NULL) {
229 $newProgress = $progressValue;
230 $cannotRepeat = FALSE;
232 $records = Achievement_User::LoadAccount($account->Id, $this->Id);
233 if (count($records) == 0) {
236 $newRecord->IdAchievement = $this->Id;
237 $newRecord->IdAccount = $account->Id;
238 $newRecord->Progress = $progressValue;
239 return $newRecord->Save();
243 if ($this->UniqueRecords == 1) {
245 if ($records[0]->Progress >= 100)
248 $records[0]->Progress += $progressValue;
249 if ($records[0]->Progress > 100)
250 $records[0]->Progress = 100;
251 $newProgress = $records[0]->Progress;
252 return $records[0]->Save();
255 if ($records[count($records) - 1]->Progress < 100)
256 $newRecord = $records[count($records) - 1];
259 $newRecord->IdAchievement = $this->Id;
260 $newRecord->IdAccount = $account->Id;
262 $newRecord->Progress += $progressValue;
263 if ($newRecord->Progress > 100)
264 $newRecord->Progress = 100;
265 $newProgress = $newRecord->Progress;
266 return $newRecord->Save();
PostProgress($account, $progressValue, &$newProgress=0, &$cannotRepeat=NULL)
__construct($src=null, $stripSlashes=false)
static Load($idApp=0, $limit=null, $offset=null, &$count=null, $returnArray=false)
static LoadUserAchievements($idAccount, $idApp=0)