model.lib.php 5.4 KB

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