model.lib.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Model.
  5. * This class provides basic methods to implement a CRUD for a new table in the
  6. * database see examples in: career.lib.php and promotion.lib.php
  7. * Include/require it in your code to use its features.
  8. */
  9. class Model
  10. {
  11. public $table;
  12. public $columns;
  13. public $required;
  14. public $is_course_model = false;
  15. /**
  16. * Constructor.
  17. */
  18. public function __construct()
  19. {
  20. }
  21. /**
  22. * Useful finder - experimental akelos like only use in notification.lib.php send function.
  23. *
  24. * @param string $type
  25. * @param array $options
  26. *
  27. * @return array
  28. */
  29. public function find($type, $options = null)
  30. {
  31. switch ($type) {
  32. case 'all':
  33. return self::get_all($options);
  34. break;
  35. default:
  36. if (is_numeric($type)) {
  37. return self::get($type);
  38. }
  39. break;
  40. }
  41. }
  42. /**
  43. * Deletes an item.
  44. *
  45. * @param int $id
  46. *
  47. * @return bool
  48. */
  49. public function delete($id)
  50. {
  51. if (empty($id) || $id != strval(intval($id))) {
  52. return false;
  53. }
  54. $params = ['id = ?' => $id];
  55. if ($this->is_course_model) {
  56. $courseId = api_get_course_int_id();
  57. $params = ['id = ? AND c_id = ?' => [$id, $courseId]];
  58. }
  59. // Database table definition
  60. $result = Database::delete($this->table, $params);
  61. if ($result != 1) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. /**
  67. * Displays the title + grid.
  68. */
  69. public function display()
  70. {
  71. }
  72. /**
  73. * Gets an element.
  74. *
  75. * @param int $id
  76. *
  77. * @return array|mixed
  78. */
  79. public function get($id)
  80. {
  81. if (empty($id)) {
  82. return [];
  83. }
  84. $params = ['id = ?' => intval($id)];
  85. if ($this->is_course_model) {
  86. $course_id = api_get_course_int_id();
  87. $params = ['id = ? AND c_id = ?' => [$id, $course_id]];
  88. }
  89. $result = Database::select(
  90. '*',
  91. $this->table,
  92. ['where' => $params],
  93. 'first'
  94. );
  95. return $result;
  96. }
  97. /**
  98. * @param array $options
  99. *
  100. * @return array
  101. */
  102. public function get_all($options = null)
  103. {
  104. return Database::select('*', $this->table, $options);
  105. }
  106. /**
  107. * @param array $options
  108. *
  109. * @return array
  110. */
  111. public function getDataToExport($options = [])
  112. {
  113. return Database::select('name, description', $this->table, $options);
  114. }
  115. /**
  116. * Get the count of elements.
  117. *
  118. * @return int
  119. */
  120. public function get_count()
  121. {
  122. $row = Database::select(
  123. 'count(*) as count',
  124. $this->table,
  125. ['where' => ['parent_id = ?' => '0']],
  126. 'first'
  127. );
  128. return $row['count'];
  129. }
  130. /**
  131. * a little bit of javascript to display.
  132. */
  133. public function javascript()
  134. {
  135. }
  136. /**
  137. * Saves an element into the DB.
  138. *
  139. * @param array $params
  140. * @param bool $show_query Whether to show the query in logs or not (passed to Database::insert())
  141. *
  142. * @return bool|int
  143. */
  144. public function save($params, $show_query = false)
  145. {
  146. $params = $this->clean_parameters($params);
  147. if ($this->is_course_model) {
  148. if (!isset($params['c_id']) || empty($params['c_id'])) {
  149. $params['c_id'] = api_get_course_int_id();
  150. }
  151. }
  152. if (!empty($this->required)) {
  153. $require_ok = true;
  154. $key_params = array_keys($params);
  155. foreach ($this->required as $field) {
  156. if (!in_array($field, $key_params)) {
  157. $require_ok = false;
  158. }
  159. }
  160. if (!$require_ok) {
  161. return false;
  162. }
  163. }
  164. if (in_array('created_at', $this->columns)) {
  165. $params['created_at'] = api_get_utc_datetime();
  166. }
  167. if (in_array('updated_at', $this->columns)) {
  168. $params['updated_at'] = api_get_utc_datetime();
  169. }
  170. if (!empty($params)) {
  171. $id = Database::insert($this->table, $params, $show_query);
  172. if (is_numeric($id)) {
  173. return $id;
  174. }
  175. }
  176. return false;
  177. }
  178. /**
  179. * Updates the obj in the database. The $params['id'] must exist in order to update a record.
  180. *
  181. * @param array $params
  182. * @param bool $showQuery
  183. *
  184. * @return bool
  185. */
  186. public function update($params, $showQuery = false)
  187. {
  188. $params = $this->clean_parameters($params);
  189. if ($this->is_course_model) {
  190. if (!isset($params['c_id']) || empty($params['c_id'])) {
  191. $params['c_id'] = api_get_course_int_id();
  192. }
  193. }
  194. //If the class has the updated_at field we update the date
  195. if (in_array('updated_at', $this->columns)) {
  196. $params['updated_at'] = api_get_utc_datetime();
  197. }
  198. //If the class has the created_at field then we remove it
  199. if (in_array('created_at', $this->columns)) {
  200. unset($params['created_at']);
  201. }
  202. if (!empty($params) && !empty($params['id'])) {
  203. $id = intval($params['id']);
  204. unset($params['id']); //To not overwrite the id
  205. if (is_numeric($id)) {
  206. $result = Database::update(
  207. $this->table,
  208. $params,
  209. ['id = ?' => $id],
  210. $showQuery
  211. );
  212. if ($result) {
  213. return true;
  214. }
  215. }
  216. }
  217. return false;
  218. }
  219. /**
  220. * @param array $params
  221. *
  222. * @return array
  223. */
  224. private function clean_parameters($params)
  225. {
  226. $clean_params = [];
  227. if (!empty($params)) {
  228. foreach ($params as $key => $value) {
  229. if (in_array($key, $this->columns)) {
  230. $clean_params[$key] = $value;
  231. }
  232. }
  233. }
  234. return $clean_params;
  235. }
  236. }