/classes/resources/RevisionHistory.php

https://github.com/VivekVish/Veda-Project-API · PHP · 199 lines · 149 code · 21 blank · 29 comment · 6 complexity · 041a081bd4a54877eaf779d197aba465 MD5 · raw file

  1. <?php
  2. require_once("classes/resources/Material.php");
  3. Class RevisionRow
  4. {
  5. ########################################################
  6. #### Member Variables ##################################
  7. ########################################################
  8. private $revisionId = null;
  9. private $resourceId = null;
  10. private $name = null;
  11. private $content = null;
  12. private $userId = null;
  13. private $timestamp = null;
  14. private $notes = null;
  15. private $resourceTable = null;
  16. private $resourceType = null;
  17. ########################################################
  18. #### Constructor and main function #####################
  19. ########################################################
  20. public function __construct($resourceTable, $resourceType)
  21. {
  22. $this->resourceTable = $resourceTable;
  23. $this->resourceType = $resourceType;
  24. }
  25. ########################################################
  26. #### Helper functions for loading object ###############
  27. ########################################################
  28. public function loadFromUri($uri,$compareTo=null)
  29. {
  30. $uri= trim($uri, "/");
  31. $uriArr = explode("/", $uri);
  32. $this->revisionId = $uriArr[count($uriArr)-1];
  33. if(count($uriArr)>(RESOURCE_ID+1))
  34. {
  35. $this->resourceId = Material::URIToId($uri,"discussion");
  36. }
  37. else
  38. {
  39. $this->resourceId = Material::URIToId($uri,"lesson");
  40. }
  41. $query = sprintf("SELECT * FROM %s WHERE revision_id=%s AND %s_id=%s",
  42. pg_escape_string($this->resourceTable),
  43. pg_escape_string($this->revisionId),
  44. pg_escape_string($this->resourceType),
  45. pg_escape_string($this->resourceId));
  46. $result = $GLOBALS['transaction']->query($query,63);
  47. $this->name = $result[0]['name'];
  48. $this->content = stripslashes($result[0]['content']);
  49. $this->userId = $result[0]['user_id'];
  50. $this->timestamp = $result[0]['revision_date'];
  51. $this->notes = $result[0]['user_notes'];
  52. if(!is_null($compareTo))
  53. {
  54. $comparisonRow = new RevisionRow("lesson_history","lesson");
  55. $comparisonUriArr = explode("/",trim($uri, "/"));
  56. array_pop($comparisonUriArr);
  57. array_push($comparisonUriArr,$compareTo);
  58. $comparisonRow->loadFromUri(join("/",$comparisonUriArr));
  59. }
  60. return true;
  61. }
  62. public function loadFromData($revisionId,$resourceId,$name,$content,$userId,$notes)
  63. {
  64. $this->revisionId = $revisionId;
  65. $this->resourceId = $resourceId;
  66. $this->name = $name;
  67. $this->content = $content;
  68. $this->userId = $userId;
  69. $this->notes = $notes;
  70. return true;
  71. }
  72. ########################################################
  73. #### Database interface functions ######################
  74. ########################################################
  75. public function save()
  76. {
  77. if(is_null($this->revisionId))
  78. {
  79. // Get maximum revisionId for a given resourceId
  80. $query = sprintf("SELECT MAX(revision_id) FROM %s WHERE %s_id='%s'",pg_escape_string($this->resourceTable), pg_escape_string($this->resourceType), pg_escape_string($this->resourceId));
  81. $result = $GLOBALS['transaction']->query($query);
  82. if($result!==false)
  83. {
  84. if($result=="none")
  85. {
  86. $this->revisionId=1;
  87. }
  88. else
  89. {
  90. $this->revisionId=((int)$result[0]['max'])+1;
  91. }
  92. }
  93. else
  94. {
  95. Error::generateError(65);
  96. }
  97. }
  98. $query = sprintf("INSERT INTO %s (revision_id,%s_id,name,content,user_id,user_notes,revision_date) VALUES (%s,%s,'%s','%s',%s,'%s',CURRENT_TIMESTAMP)",
  99. pg_escape_string($this->resourceTable),
  100. pg_escape_string($this->resourceType),
  101. pg_escape_string($this->revisionId),
  102. pg_escape_string($this->resourceId),
  103. pg_escape_string($this->name),
  104. pg_escape_string($this->content),
  105. pg_escape_string($this->userId),
  106. pg_escape_string($this->notes));
  107. $GLOBALS['transaction']->query($query,64);
  108. return true;
  109. }
  110. ########################################################
  111. ### Getters and Setters ################################
  112. ########################################################
  113. public function getJSON()
  114. {
  115. $json = json_encode(array("revisionId"=>$this->revisionId,"resourceId"=>$this->resourceId,"name"=>$this->name,"content"=>htmlentities($this->content),"userId"=>$this->userId,"notes"=>$this->notes));
  116. return $json;
  117. }
  118. }
  119. Class RevisionHistory
  120. {
  121. ########################################################
  122. #### Member Variables ##################################
  123. ########################################################
  124. private $resourceRows = null;
  125. private $resourceTable = null;
  126. private $resourceType = null;
  127. private $resourceId = null;
  128. ########################################################
  129. #### Constructor and main function #####################
  130. ########################################################
  131. # Constructor
  132. public function __construct($resourceTable, $resourceType, $resourceId)
  133. {
  134. $this->resourceRows = array();
  135. $this->resourceTable = $resourceTable;
  136. $this->resourceType = $resourceType;
  137. $this->resourceId = $resourceId;
  138. }
  139. ########################################################
  140. #### Database interface functions ######################
  141. ########################################################
  142. public function getHistory($startNum = 1, $endNum = 100, $oldToNew = true)
  143. {
  144. $query = sprintf("SELECT oldtable.%s_id, oldtable.revision_id, oldtable.username, oldtable.user_notes, oldtable.revision_date, oldtable.name
  145. FROM (SELECT * FROM %s LEFT JOIN usernames ON %s.user_id=usernames.id WHERE %s_id=%s ORDER BY revision_id %s) AS oldtable CROSS JOIN
  146. (SELECT ARRAY(SELECT revision_id FROM %s WHERE %s_id=%s ORDER BY revision_id %s) AS id)
  147. AS oldids CROSS JOIN GENERATE_SERIES(1, (SELECT COUNT(*) FROM %s WHERE %s_id=%s))
  148. AS row_number WHERE oldids.id[row_number] = oldtable.revision_id AND row_number>=%s AND row_number<=%s ORDER BY row_number",
  149. pg_escape_string($this->resourceType),
  150. pg_escape_string($this->resourceTable),
  151. pg_escape_string($this->resourceTable),
  152. pg_escape_string($this->resourceType),
  153. pg_escape_string($this->resourceId),
  154. pg_escape_string($oldToNew ? "DESC" : ""),
  155. pg_escape_string($this->resourceTable),
  156. pg_escape_string($this->resourceType),
  157. pg_escape_string($this->resourceId),
  158. pg_escape_string($oldToNew ? "DESC" : ""),
  159. pg_escape_string($this->resourceTable),
  160. pg_escape_string($this->resourceType),
  161. pg_escape_string($this->resourceId),
  162. pg_escape_string($startNum),
  163. pg_escape_string($endNum));
  164. $result = $GLOBALS['transaction']->query($query);
  165. if($result=="none")
  166. {
  167. return array();
  168. }
  169. else
  170. {
  171. return $result;
  172. }
  173. }
  174. ########################################################
  175. ### Getters and Setters ################################
  176. ########################################################
  177. }
  178. ?>