chamilo_pens.php 7.9 KB

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