model.lib.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides methods for the notebook management.
  5. * Include/require it in your code to use its features.
  6. * @package chamilo.library
  7. */
  8. class Model {
  9. var $table;
  10. var $columns;
  11. public function __construct() {
  12. }
  13. public function find() {
  14. }
  15. /**
  16. * Delets an item
  17. */
  18. function delete($id) {
  19. if (empty($id) or $id != strval(intval($id))) { return false; }
  20. // Database table definition
  21. $result = Database :: delete($this->table, array('id = ?' => $id));
  22. if ($result != 1){
  23. return false;
  24. }
  25. return true;
  26. }
  27. private function clean_parameters($params){
  28. $clean_params = array();
  29. if (!empty($params)) {
  30. foreach($params as $key=>$value) {
  31. if (in_array($key, $this->columns)) {
  32. $clean_params[$key] = $value;
  33. }
  34. }
  35. }
  36. return $clean_params;
  37. }
  38. /**
  39. * Displays the title + grid
  40. */
  41. function display() {
  42. }
  43. /**
  44. * Gets an element
  45. */
  46. function get($id) {
  47. if (empty($id)) { return array(); }
  48. $result = Database::select('*',$this->table, array('where'=>array('id = ?'=>intval($id))),'first');
  49. return $result;
  50. }
  51. function get_all() {
  52. return $careers = Database::select('*',$this->table);
  53. }
  54. /**
  55. * Get the count of elements
  56. */
  57. function get_count() {
  58. $row = Database::select('count(*) as count', $this->table, array(),'first');
  59. return $row['count'];
  60. }
  61. /**
  62. * a little bit of javascript to display a prettier warning when deleting a note
  63. *
  64. * @return unknown
  65. *
  66. */
  67. function javascript() {
  68. }
  69. /**
  70. * Saves an element into the DB
  71. *
  72. * @param array $values
  73. * @return bool
  74. *
  75. */
  76. function save($values) {
  77. $values = $this->clean_parameters($values);
  78. if (!empty($values)) {
  79. $id = Database::insert($this->table, $values);
  80. if (is_numeric($id)){
  81. return $id;
  82. }
  83. }
  84. return false;
  85. }
  86. /**
  87. * Updates the obj in the database
  88. *
  89. * @param array $values
  90. *
  91. */
  92. function update($values) {
  93. $values = $this->clean_parameters($values);
  94. if (!empty($values)) {
  95. $id = $values['id'];
  96. unset($values['id']);
  97. $result = Database::update($this->table, $values, array('id = ?'=>$id));
  98. if ($result){
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. }