/site/lib/dkp/dkpAward.php

https://github.com/lukas89/WebDKP · PHP · 298 lines · 210 code · 12 blank · 76 comment · 8 complexity · e0eedd019159b5b268706ad44658b0bd MD5 · raw file

  1. <?php
  2. /*===========================================================
  3. CLASS DESCRIPTION
  4. =============================================================
  5. Class Description should be placed here.
  6. */
  7. include_once("dkpUser.php");
  8. class dkpAward {
  9. /*===========================================================
  10. MEMBER VARIABLES
  11. ============================================================*/
  12. var $id;
  13. var $tableid = 1;
  14. var $guild = 0;
  15. var $points = 0;
  16. var $reason = "";
  17. var $location = "WebDKP";
  18. var $awardedby = "Unknown";
  19. var $dateDate;
  20. var $dateTime;
  21. var $foritem = 0;
  22. var $playercount = 0;
  23. var $transfer = 0;
  24. var $zerosumauto = 0;
  25. var $linked = 0;
  26. var $players = array(); //array of players with award. Filled with dkp_users
  27. //after loadPlayers() is called.
  28. const tablename = "dkp_awards";
  29. /*===========================================================
  30. DEFAULT CONSTRUCTOR
  31. ============================================================*/
  32. function dkpAward()
  33. {
  34. $this->tablename = dkpAward::tablename;
  35. }
  36. /*===========================================================
  37. loadFromDatabase($id)
  38. Loads the information for this class from the backend database
  39. using the passed string.
  40. ============================================================*/
  41. function loadFromDatabase($id)
  42. {
  43. global $sql;
  44. $row = $sql->QueryRow("SELECT * FROM $this->tablename WHERE id='$id'");
  45. $this->loadFromRow($row);
  46. }
  47. /*===========================================================
  48. loadFromDetails($id)
  49. Attempts to load details for an award given details about it
  50. ============================================================*/
  51. function loadFromDetails($guildid, $tableid, $reason, $date){
  52. global $sql;
  53. $guildid = sql::Escape($guildid);
  54. $tableid = sql::Escape($tableid);
  55. $reason = sql::Escape($reason);
  56. $date = sql::Escape($date);
  57. $row = $sql->QueryRow("SELECT * FROM $this->tablename
  58. WHERE guild='$guildid' AND tableid='$tableid' AND reason='$reason' AND date='$date'");
  59. $this->loadFromRow($row);
  60. }
  61. /*===========================================================
  62. loadFromRow($row)
  63. Loads the information for this class from the passed database row.
  64. ============================================================*/
  65. function loadFromRow($row)
  66. {
  67. $this->id=$row["id"];
  68. if(isset($row["awardid"]))
  69. $this->id = $row["awardid"];
  70. if(isset($row["historyid"]))
  71. $this->historyid = $row["historyid"];
  72. $this->tableid = $row["tableid"];
  73. $this->guild = $row["guild"];
  74. $this->playercount = $row["playercount"];
  75. $this->points = $row["points"];
  76. $this->points = str_replace(".00", "", $this->points);
  77. $this->reason = $row["reason"];
  78. $this->location = $row["location"];
  79. $this->awardedby = $row["awardedby"];
  80. $this->date = $row["date"];
  81. if($row["date"]!="")
  82. {
  83. $this->dateDate = date("F j, Y", strtotime($row["date"]));
  84. $this->dateTime = date("g:i A", strtotime($row["date"]));
  85. }
  86. $this->foritem = $row["foritem"];
  87. $this->transfer = $row["transfer"];
  88. $this->zerosumauto = $row["zerosumauto"];
  89. $this->linked = $row["linked"];
  90. }
  91. /*===========================================================
  92. save()
  93. Saves data into the backend database using the supplied id
  94. ============================================================*/
  95. function save()
  96. {
  97. global $sql;
  98. $reason = sql::Escape($this->reason);
  99. $location = sql::Escape($this->location);
  100. $awardedby = sql::Escape($this->awardedby);
  101. $date = sql::Escape($this->date);
  102. $transfer = sql::Escape($this->transfer);
  103. $zerosumauto = sql::Escape($this->zerosumauto);
  104. $linked = sql::Escape($this->linked);
  105. $sql->Query("UPDATE $this->tablename SET
  106. tableid = '$this->tableid',
  107. guild = '$this->guild',
  108. playercount = '$this->playercount',
  109. points = '$this->points',
  110. reason = '$reason',
  111. location = '$location',
  112. awardedby = '$awardedby',
  113. foritem = '$this->foritem',
  114. date ='$date',
  115. transfer = '$transfer',
  116. zerosumauto = '$zerosumauto',
  117. linked = '$linked'
  118. WHERE id='$this->id'");
  119. }
  120. /*===========================================================
  121. saveNew()
  122. Saves data into the backend database as a new row entry. After
  123. calling this method $id will be filled with a new value
  124. matching the new row for the data
  125. ============================================================*/
  126. function saveNew()
  127. {
  128. global $sql;
  129. $reason = sql::Escape($this->reason);
  130. $location = sql::Escape($this->location);
  131. $awardedby = sql::Escape($this->awardedby);
  132. $date = sql::Escape($this->date);
  133. $transfer = sql::Escape($this->transfer);
  134. $zerosumauto = sql::Escape($this->zerosumauto);
  135. $linked = sql::Escape($this->linked);
  136. $sql->Query("INSERT INTO $this->tablename SET
  137. tableid = '$this->tableid',
  138. guild = '$this->guild',
  139. playercount = '$this->playercount',
  140. points = '$this->points',
  141. reason = '$reason',
  142. location = '$location',
  143. awardedby = '$awardedby',
  144. foritem = '$this->foritem',
  145. date = '$this->date',
  146. transfer = '$transfer',
  147. zerosumauto = '$zerosumauto',
  148. linked = '$linked'
  149. ");
  150. $this->id=$sql->GetLastId();
  151. }
  152. /*===========================================================
  153. delete()
  154. Deletes the row with the current id of this instance from the
  155. database
  156. ============================================================*/
  157. function delete()
  158. {
  159. global $sql;
  160. $sql->Query("DELETE FROM $this->tablename WHERE id = '$this->id'");
  161. }
  162. /*===========================================================
  163. exists()
  164. STATIC METHOD
  165. Returns true if the given entry exists in the database
  166. database
  167. ============================================================*/
  168. function exists($id)
  169. {
  170. global $sql;
  171. $name = sql::escape($name);
  172. $tablename = dkpAward::tablename;
  173. $exists = $sql->QueryItem("SELECT id FROM $tablename WHERE id='$name'"); //MODIFY THIS LINE
  174. return ($exists != "");
  175. }
  176. /*===========================================================
  177. timestamp()
  178. Applies a timestamp to the award
  179. ============================================================*/
  180. function timestamp() {
  181. global $sql;
  182. $table = $this->tablename;
  183. $sql->Query("UPDATE $table SET date=NOW() WHERE id='$this->id'");
  184. }
  185. /*===========================================================
  186. loadPlayers()
  187. Loads all players with this award and stores them in the
  188. $players member variable
  189. ============================================================*/
  190. function loadPlayers(){
  191. global $sql;
  192. $awardid = sql::Escape($this->id);
  193. $result = $sql->Query("SELECT *, dkp_users.id as userid
  194. FROM dkp_pointhistory, dkp_users
  195. WHERE dkp_pointhistory.award = '$awardid'
  196. AND dkp_pointhistory.user = dkp_users.id
  197. ORDER BY dkp_users.name ASC");
  198. $this->players = array();
  199. while($row = mysql_fetch_array($result)) {
  200. $player = new dkpUser();
  201. $player->loadFromRow($row);
  202. $this->players[] = $player;
  203. }
  204. return $this->players;
  205. }
  206. /*===========================================================
  207. loadPlayer()
  208. Returns a single player that recieved this award. This is only
  209. helpful if the award is for an item (or a transfer), in which case only
  210. one person should have it anyways.
  211. ============================================================*/
  212. function loadPlayer(){
  213. $awardid = sql::Escape($this->id);
  214. global $sql;
  215. $row = $sql->QueryRow("SELECT *, dkp_users.id as userid
  216. FROM dkp_pointhistory, dkp_users
  217. WHERE dkp_pointhistory.award = '$awardid'
  218. AND dkp_pointhistory.user = dkp_users.id
  219. ORDER BY dkp_users.name ASC LIMIT 1");
  220. $player = new dkpUser();
  221. $player->loadFromRow($row);
  222. $this->player = $player;
  223. return $player;
  224. }
  225. /*===========================================================
  226. getPlayerids()
  227. Returns an array of playerids of all players with this award.
  228. ============================================================*/
  229. function getPlayerids(){
  230. $this->loadPlayers();
  231. $playerids = array();
  232. foreach($this->players as $player)
  233. $playerids[] = $player->id;
  234. return $playerids;
  235. }
  236. /*===========================================================
  237. calculatePlayerCount()
  238. Updates this awads current player could field by querying the
  239. database to see who all refers to it. You MUST call SAVE afterwards
  240. to save this new value;
  241. ============================================================*/
  242. function calculatePlayerCount(){
  243. if($this->id == "")
  244. return;
  245. global $sql;
  246. $awardid = sql::Escape($this->id);
  247. $count = $sql->QueryItem("SELECT count(*) as total
  248. FROM dkp_pointhistory
  249. WHERE award='$awardid'
  250. AND user != 0");
  251. $this->playercount = $count;
  252. }
  253. /*===========================================================
  254. setupTable()
  255. Checks to see if the classes database table exists. If it does not
  256. the table is created.
  257. ============================================================*/
  258. function setupTable()
  259. {
  260. if(!sql::TableExists(dkpAward::tablename)) {
  261. $tablename = dkpAward::tablename;
  262. global $sql;
  263. $sql->Query("CREATE TABLE `$tablename` (
  264. `id` INT NOT NULL AUTO_INCREMENT ,
  265. `tableid` INT NOT NULL,
  266. `guild` INT NOT NULL,
  267. `playercount` INT NOT NULL,
  268. `points` DECIMAL(11,2) NOT NULL,
  269. `reason` VARCHAR (128) NOT NULL,
  270. `location` VARCHAR (64) NOT NULL,
  271. `awardedby` VARCHAR (32) NOT NULL,
  272. `date` DATETIME NOT NULL,
  273. `foritem` INT NOT NULL DEFAULT '0',
  274. `transfer` INT (1) NOT NULL DEFAULT '0',
  275. `zerosumauto` INT (1) NOT NULL DEFAULT '0',
  276. `linked` INT NOT NULL DEFAULT '0',
  277. PRIMARY KEY ( `id` ),
  278. KEY `key_date` (`guild`,`tableid`,`date`),
  279. KEY `key_points` (`guild`,`tableid`,`points`),
  280. KEY `key_reason` (`guild`,`tableid`,`reason`)
  281. ) ENGINE = innodb;");
  282. }
  283. }
  284. }
  285. dkpAward::setupTable()
  286. ?>