chamilo_pens.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /**
  39. * Database table to be used.
  40. */
  41. const TABLE_NAME = "plugin_pens";
  42. /**
  43. * Id of the object.
  44. *
  45. * @var int
  46. */
  47. protected $_id = null;
  48. /**
  49. * PENS Version to be used. Currently, the only valid value is 1.0.0. Required.
  50. *
  51. * @var string
  52. */
  53. protected $_pens_version = null;
  54. /**
  55. * Package type being used. The only valid values are aicc-pkg, scorm-pif, ims-qti. Required.
  56. *
  57. * @var string
  58. */
  59. protected $_package_type = null;
  60. /**
  61. * Package type version. Required.
  62. *
  63. * @var string
  64. */
  65. protected $_package_type_version = null;
  66. /**
  67. * Package format. The only valid values are zip, url, jar, war and xml. Required.
  68. *
  69. * @var string
  70. */
  71. protected $_package_format = null;
  72. /**
  73. * Package id. Requires a valid URI according to RFC 2396. Required.
  74. *
  75. * @var string
  76. */
  77. protected $_package_id = null;
  78. /**
  79. * Name or ID for client submitting the content package to the target system. Required.
  80. *
  81. * @var string
  82. */
  83. protected $_client = null;
  84. /**
  85. * Unstructured character string that may be used to transfer vendor-specific data such as processing hints or deployment information. Optional.
  86. *
  87. * @var string
  88. */
  89. protected $_vendor_data = null;
  90. /**
  91. * Package name.
  92. *
  93. * @var string
  94. */
  95. protected $_package_name = null;
  96. /**
  97. * Date of creation.
  98. *
  99. * @var DateTime
  100. */
  101. protected $_created_at = null;
  102. /**
  103. * Update date.
  104. *
  105. * @var DateTime
  106. */
  107. protected $_updated_at = null;
  108. /**
  109. * Constructor. Takes a PENSRequest as an argument.
  110. *
  111. * @param object $request Request
  112. */
  113. public function __construct($request)
  114. {
  115. if ($request instanceof PENSRequest) {
  116. $this->_id = 0;
  117. $this->_pens_version = $request->getPensVersion();
  118. $this->_package_type = $request->getPackageType();
  119. $this->_package_type_version = $request->getPackageTypeVersion();
  120. $this->_package_format = $request->getPackageFormat();
  121. $this->_package_id = $request->getPackageId();
  122. $this->_client = $request->getClient();
  123. $this->_vendor_data = $request->getVendorData();
  124. $this->_package_name = $request->getFilename();
  125. } else {
  126. if (is_array($request)) {
  127. $this->_id = $request['id'];
  128. $this->_pens_version = $request['pens_version'];
  129. $this->_package_type = $request['package_type'];
  130. $this->_package_type_version = $request['package_type_version'];
  131. $this->_package_format = $request['package_format'];
  132. $this->_package_id = $request['package_id'];
  133. $this->_client = $request['client'];
  134. $this->_vendor_data = $request['vendor_data'];
  135. $this->_package_name = $request['package_name'];
  136. $this->_created_at = new DateTime($request['created_at'], new DateTimeZone('UTC'));
  137. if (!empty($request['updated_at'])) {
  138. $this->_updated_at = new DateTime($request['updated_at'], new DateTimeZone('UTC'));
  139. }
  140. }
  141. }
  142. }
  143. /**
  144. * Saves the object in the DB.
  145. */
  146. public function save()
  147. {
  148. $clean_package_type_version = Database::escape_string($this->_package_type_version);
  149. $clean_package_id = Database::escape_string($this->_package_id);
  150. $clean_client = Database::escape_string($this->_client);
  151. $clean_vendor_data = Database::escape_string($this->_vendor_data);
  152. $created_at = api_get_utc_datetime();
  153. $table = Database::get_main_table(self::TABLE_NAME);
  154. $sql_query = "INSERT INTO $table (pens_version, package_type, package_type_version, package_format, package_id, client, vendor_data, package_name, created_at) VALUES (".
  155. "'".$this->_pens_version."', ".
  156. "'".$this->_package_type."', ".
  157. "'".$clean_package_type_version."', ".
  158. "'".$this->_package_format."', ".
  159. "'".$clean_package_id."', ".
  160. "'".$clean_client."', ".
  161. "'".$clean_vendor_data."', ".
  162. "'".$this->_package_name."', ".
  163. "'".$created_at."') ON DUPLICATE KEY UPDATE ".
  164. "pens_version = VALUES(pens_version), ".
  165. "package_type = VALUES(package_type), ".
  166. "package_type_version = VALUES(package_type_version), ".
  167. "package_format = VALUES(package_format), ".
  168. "client = VALUES(client), ".
  169. "vendor_data = VALUES(vendor_data), ".
  170. "package_name = VALUES(package_name), ".
  171. "updated_at = '".$created_at."';";
  172. Database::query($sql_query);
  173. }
  174. /**
  175. * Returns a ChamiloPens object, based on package id.
  176. *
  177. * @param string $package_id Package id
  178. *
  179. * @return ChamiloPens|null
  180. */
  181. public static function findByPackageId($package_id)
  182. {
  183. $table = Database::get_main_table(self::TABLE_NAME);
  184. $sql_query = "SELECT * FROM $table WHERE package_id = '".$package_id."';";
  185. $results = Database::query($sql_query);
  186. $number = Database::num_rows($results);
  187. if ($number == 1) {
  188. $obj = Database::fetch_assoc($results);
  189. return new ChamiloPens($obj);
  190. } else {
  191. return null;
  192. }
  193. }
  194. /**
  195. * Returns an array of all the objects of the DB.
  196. *
  197. * @return array Array of ChamiloPens objects
  198. */
  199. public static function findAll()
  200. {
  201. $table = Database::get_main_table(self::TABLE_NAME);
  202. $sql_query = "SELECT * FROM $table ORDER BY created_at;";
  203. $results = Database::query($sql_query);
  204. $return = [];
  205. while ($assoc = Database::fetch_assoc($results)) {
  206. $return[] = new ChamiloPens($assoc);
  207. }
  208. return $return;
  209. }
  210. public function getId()
  211. {
  212. return $this->_id;
  213. }
  214. public function getPensVersion()
  215. {
  216. return $this->_pens_version;
  217. }
  218. public function getPackageType()
  219. {
  220. return $this->_package_type;
  221. }
  222. public function getPackageTypeVersion()
  223. {
  224. return $this->_package_type_version;
  225. }
  226. public function getPackageFormat()
  227. {
  228. return $this->_package_format;
  229. }
  230. public function getPackageId()
  231. {
  232. return $this->_package_id;
  233. }
  234. public function getClient()
  235. {
  236. return $this->_client;
  237. }
  238. public function getVendorData()
  239. {
  240. return $this->_vendor_data;
  241. }
  242. public function getPackageName()
  243. {
  244. return $this->_package_name;
  245. }
  246. public function getCreatedAt()
  247. {
  248. return $this->_created_at;
  249. }
  250. }