model.lib.php 5.7 KB

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