Combu Server  3.1.1
PHP API Documentation
Achievement.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class Achievement extends DataClass {
11 
12  const TABLE_NAME = "Achievement";
13 
14  public $Id = 0;
15  public $IdApp = 0;
16  public $Title = "";
17  public $Description = "";
18  public $UniqueRecords = 1;
19  public $Position = 0;
20 
21 
25  public function __construct($src = null, $stripSlashes = false) {
26  if ($src == null)
27  return;
28  if (is_array($src)) {
29  // Load by array
30  $this->_loadByRow($src, $stripSlashes);
31  } else if (is_numeric($src) && intval($src) > 0) {
32  // Load by Id
33  $this->_loadFilter(self::GetTableName(__CLASS__), "Id = " . intval($src));
34  }
35  }
36 
47  public static function Load ($idApp = 0, $limit = null, $offset = null, &$count = null, $returnArray = false) {
48  $where = "";
49  if ($idApp > 0) {
50  $where = sprintf("(IdApp = 0 OR IdApp = %d)", $idApp);
51  }
52  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "IdApp, Position", $limit, $offset, $count);
53  }
54 
60  public function Save() {
61  global $Database;
62  if ($this->Id > 0) {
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),
67  $this->Id);
68  } else {
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__),
72  $this->IdApp,
73  $Database->Escape($this->Title),
74  $Database->Escape($this->Description),
75  $this->UniqueRecords,
76  $this->Position);
77  }
78  $saved = $Database->Query($query);
79  if ($saved) {
80  if ($this->Id <= 0) {
81  $this->Id = $Database->InsertedId();
82  }
83  return TRUE;
84  }
85  return FALSE;
86  }
87 
88  private static function GetNextPosition($idApp) {
89  $maxPosition = 0;
90  $count = self::CountRecords(self::GetTableName(__CLASS__), sprintf("IdApp = %d", $idApp));
91  if ($count > 0) {
92  $maxPosition = $count;
93  }
94  return $maxPosition;
95  }
96 
97  public function MoveUp() {
98  global $Database;
99  if ($this->Id > 0 && $this->Position > 0) {
100  // Calculate the desired position
101  $newPosition = $this->Position - 1;
102  // Try to update the early positions
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)) {
106  // Try to update the current position
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;
111  return TRUE;
112  } else {
113  // Update current position failed, revert the previous statement
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);
117  }
118  }
119  }
120  return FALSE;
121  }
122 
123  public function MoveDown() {
124  global $Database;
125  if ($this->Id > 0) {
126  $maxPosition = self::GetNextPosition($this->IdApp) - 1;
127  if ($maxPosition > 0 && $this->Position < $maxPosition) {
128  // Calculate the desired position
129  $newPosition = $this->Position + 1;
130  // Try to update the early positions
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)) {
134  // Try to update the current position
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;
139  return TRUE;
140  } else {
141  // Update current position failed, revert the previous statement
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);
145  }
146  }
147  }
148  }
149  return FALSE;
150  }
151 
157  public function Delete() {
158  global $Database;
159  if ($this->Id < 1)
160  return FALSE;
161  if ($this->_Delete(self::GetTableName(__CLASS__), "Id = " . $this->Id)) {
162  // Reorder the next positions
163  $query = sprintf("UPDATE %s SET Position = Position - 1 WHERE Position > %d",
164  self::GetTableName(__CLASS__), $this->Position);
165  $Database->Query($query);
166  // Delete all the associated resources
167  $this->_Delete(self::GetTableName(Achievement_User::class), "IdAchievement = " . $this->Id);
168  return TRUE;
169  }
170  return FALSE;
171  }
172 
176  public static function Prune() {
177  self::TruncateClass(__CLASS__);
178  self::TruncateClass(Achievement_User::class);
179  }
180 
187  public static function LoadUserAchievements ($idAccount, $idApp = 0) {
188  $result = array();
189  $where = "";
190  if ($idApp > 0) {
191  $where = sprintf("(IdApp = 0 OR IdApp = %d)", $idApp);
192  }
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) {
197  $newAchievement = new Achievement_User();
198  $newAchievement->IdAchievement = $achievement->Id;
199  $newAchievement->IdAccount = $idAccount;
200  $newAchievement->Progress = 0;
201  $records[] = $newAchievement;
202  }
203  $finished = 0;
204  foreach ($records as $progress1) {
205  if ($progress1->Progress >= 100)
206  ++$finished;
207  }
208  foreach ($records as $progress) {
209  $result[] = array(
210  "Id" => $achievement->Id,
211  "Title" => $achievement->Title,
212  "Description" => $achievement->Description,
213  "Progress" => $progress->Progress,
214  "Finished" => $finished
215  );
216  }
217  }
218  return $result;
219  }
220 
228  public function PostProgress ($account, $progressValue, &$newProgress = 0, &$cannotRepeat = NULL) {
229  $newProgress = $progressValue;
230  $cannotRepeat = FALSE;
231  // Load the current saved records for this user
232  $records = Achievement_User::LoadAccount($account->Id, $this->Id);
233  if (count($records) == 0) {
234  // We still haven't records, create new one
235  $newRecord = new Achievement_User();
236  $newRecord->IdAchievement = $this->Id;
237  $newRecord->IdAccount = $account->Id;
238  $newRecord->Progress = $progressValue;
239  return $newRecord->Save();
240  }
241  // Ok! So we have at least one record for this user in the achievement...
242  // Check to see if the achievement requires unique records per user
243  if ($this->UniqueRecords == 1) {
244  // Achievement has already been completed, return TRUE
245  if ($records[0]->Progress >= 100)
246  return TRUE;
247  // It requires unique records, so update the current saved
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();
253  }
254  // It doesn't require unique records, so create new one if needed
255  if ($records[count($records) - 1]->Progress < 100)
256  $newRecord = $records[count($records) - 1];
257  else {
258  $newRecord = new Achievement_User();
259  $newRecord->IdAchievement = $this->Id;
260  $newRecord->IdAccount = $account->Id;
261  }
262  $newRecord->Progress += $progressValue;
263  if ($newRecord->Progress > 100)
264  $newRecord->Progress = 100;
265  $newProgress = $newRecord->Progress;
266  return $newRecord->Save();
267  }
268 }
PostProgress($account, $progressValue, &$newProgress=0, &$cannotRepeat=NULL)
__construct($src=null, $stripSlashes=false)
Definition: Achievement.php:25
static Load($idApp=0, $limit=null, $offset=null, &$count=null, $returnArray=false)
Definition: Achievement.php:47
Definition: Account.php:3
static LoadUserAchievements($idAccount, $idApp=0)