chamilo_pens.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * This file is part of chamilo-pens.
  4. *
  5. * chamilo-pens is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * chamilo-pens is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with chamilo-pens. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * ChamiloPens
  20. *
  21. * This file provides the ChamiloPens class
  22. *
  23. * @author Guillaume Viguier-Just <guillaume@viguierjust.com>
  24. * @licence http://www.gnu.org/licenses/gpl.txt
  25. */
  26. require_once __DIR__ . '/../../main/inc/global.inc.php';
  27. require_once __DIR__ . '/lib/pens.php';
  28. /**
  29. * ChamiloPens
  30. *
  31. * Model class that stores a PENS request made to a Chamilo server into the database
  32. *
  33. * @author Guillaume Viguier-Just <guillaume@viguierjust.com>
  34. * @licence http://www.gnu.org/licenses/gpl.txt
  35. */
  36. class ChamiloPens extends Plugin {
  37. /**
  38. * Database table to be used
  39. */
  40. const TABLE_NAME = "plugin_pens";
  41. /**
  42. * Id of the object
  43. * @var int
  44. */
  45. protected $_id = null;
  46. /**
  47. * PENS Version to be used. Currently, the only valid value is 1.0.0. Required.
  48. * @var string
  49. */
  50. protected $_pens_version = null;
  51. /**
  52. * Package type being used. The only valid values are aicc-pkg, scorm-pif, ims-qti. Required
  53. * @var string
  54. */
  55. protected $_package_type = null;
  56. /**
  57. * Package type version. Required
  58. * @var string
  59. */
  60. protected $_package_type_version = null;
  61. /**
  62. * Package format. The only valid values are zip, url, jar, war and xml. Required
  63. * @var string
  64. */
  65. protected $_package_format = null;
  66. /**
  67. * Package id. Requires a valid URI according to RFC 2396. Required
  68. * @var string
  69. */
  70. protected $_package_id = null;
  71. /**
  72. * Name or ID for client submitting the content package to the target system. Required.
  73. * @var string
  74. */
  75. protected $_client = null;
  76. /**
  77. * Unstructured character string that may be used to transfer vendor-specific data such as processing hints or deployment information. Optional.
  78. * @var string
  79. */
  80. protected $_vendor_data = null;
  81. /**
  82. * Package name
  83. * @var string
  84. */
  85. protected $_package_name = null;
  86. /**
  87. * Date of creation
  88. * @var DateTime
  89. */
  90. protected $_created_at = null;
  91. /**
  92. * Update date
  93. * @var DateTime
  94. */
  95. protected $_updated_at = null;
  96. /**
  97. * Constructor. Takes a PENSRequest as an argument
  98. *
  99. * @param PENSRequest Request
  100. */
  101. public function __construct($request) {
  102. if($request instanceof PENSRequest) {
  103. $this->_id = 0;
  104. $this->_pens_version = $request->getPensVersion();
  105. $this->_package_type = $request->getPackageType();
  106. $this->_package_type_version = $request->getPackageTypeVersion();
  107. $this->_package_format = $request->getPackageFormat();
  108. $this->_package_id = $request->getPackageId();
  109. $this->_client = $request->getClient();
  110. $this->_vendor_data = $request->getVendorData();
  111. $this->_package_name = $request->getFilename();
  112. } else if(is_array($request)) {
  113. $this->_id = $request['id'];
  114. $this->_pens_version = $request['pens_version'];
  115. $this->_package_type = $request['package_type'];
  116. $this->_package_type_version = $request['package_type_version'];
  117. $this->_package_format = $request['package_format'];
  118. $this->_package_id = $request['package_id'];
  119. $this->_client = $request['client'];
  120. $this->_vendor_data = $request['vendor_data'];
  121. $this->_package_name = $request['package_name'];
  122. $this->_created_at = new DateTime($request['created_at'], new DateTimeZone('UTC'));
  123. if(!empty($request['updated_at'])) {
  124. $this->_updated_at = new DateTime($request['updated_at'], new DateTimeZone('UTC'));
  125. }
  126. }
  127. }
  128. /**
  129. * Saves the object in the DB
  130. */
  131. public function save() {
  132. $clean_package_type_version = Database::escape_string($this->_package_type_version);
  133. $clean_package_id = Database::escape_string($this->_package_id);
  134. $clean_client = Database::escape_string($this->_client);
  135. $clean_vendor_data = Database::escape_string($this->_vendor_data);
  136. $created_at = api_get_utc_datetime();
  137. $table = Database::get_main_table(ChamiloPens::TABLE_NAME);
  138. $sql_query = "INSERT INTO $table (pens_version, package_type, package_type_version, package_format, package_id, client, vendor_data, package_name, created_at) VALUES (" .
  139. "'". $this->_pens_version. "', " .
  140. "'".$this->_package_type. "', " .
  141. "'".$clean_package_type_version. "', " .
  142. "'".$this->_package_format. "', " .
  143. "'".$clean_package_id. "', " .
  144. "'".$clean_client."', " .
  145. "'".$clean_vendor_data."', " .
  146. "'".$this->_package_name."', " .
  147. "'".$created_at."') ON DUPLICATE KEY UPDATE " .
  148. "pens_version = VALUES(pens_version), ".
  149. "package_type = VALUES(package_type), ".
  150. "package_type_version = VALUES(package_type_version), ".
  151. "package_format = VALUES(package_format), ".
  152. "client = VALUES(client), ".
  153. "vendor_data = VALUES(vendor_data), ".
  154. "package_name = VALUES(package_name), ".
  155. "updated_at = '".$created_at."';";
  156. Database::query($sql_query);
  157. }
  158. /**
  159. * Returns a ChamiloPens object, based on package id
  160. *
  161. * @param string Package id
  162. */
  163. public static function findByPackageId($package_id) {
  164. $table = Database::get_main_table(ChamiloPens::TABLE_NAME);
  165. $sql_query = "SELECT * FROM $table WHERE package_id = '".$package_id."';";
  166. $results = Database::query($sql_query);
  167. $number = Database::num_rows($results);
  168. if($number == 1) {
  169. $obj = Database::fetch_assoc($results);
  170. return new ChamiloPens($obj);
  171. } else {
  172. return null;
  173. }
  174. }
  175. /**
  176. * Returns an array of all the objects of the DB
  177. *
  178. * @return array Array of ChamiloPens objects
  179. */
  180. public static function findAll() {
  181. $table = Database::get_main_table(ChamiloPens::TABLE_NAME);
  182. $sql_query = "SELECT * FROM $table ORDER BY created_at;";
  183. $results = Database::query($sql_query);
  184. $return = array();
  185. while($assoc = Database::fetch_assoc($results)) {
  186. $return[] = new ChamiloPens($assoc);
  187. }
  188. return $return;
  189. }
  190. public function getId() {
  191. return $this->_id;
  192. }
  193. public function getPensVersion() {
  194. return $this->_pens_version;
  195. }
  196. public function getPackageType() {
  197. return $this->_package_type;
  198. }
  199. public function getPackageTypeVersion() {
  200. return $this->_package_type_version;
  201. }
  202. public function getPackageFormat() {
  203. return $this->_package_format;
  204. }
  205. public function getPackageId() {
  206. return $this->_package_id;
  207. }
  208. public function getClient() {
  209. return $this->_client;
  210. }
  211. public function getVendorData() {
  212. return $this->_vendor_data;
  213. }
  214. public function getPackageName() {
  215. return $this->_package_name;
  216. }
  217. public function getCreatedAt() {
  218. return $this->_created_at;
  219. }
  220. }