entity.class.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Description of Entity
  4. *
  5. * @license see /license.txt
  6. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  7. */
  8. class Entity
  9. {
  10. /**
  11. *
  12. * @return \Entity\Course
  13. */
  14. public static function current_course()
  15. {
  16. static $result = false;
  17. if ($result === false) {
  18. $repo = \Entity\Course::repository();
  19. $course_id = api_get_course_int_id();
  20. if ($course_id) {
  21. $result = $repo->find($course_id);
  22. }
  23. }
  24. return $result;
  25. }
  26. /**
  27. *
  28. * @return \Entity\Session
  29. */
  30. public static function current_session()
  31. {
  32. static $result = false;
  33. if ($result === false) {
  34. $repo = \Entity\Session::repository();
  35. $session_id = api_get_session_id();
  36. $result = $repo->find($session_id);
  37. }
  38. return $result;
  39. }
  40. function __construct($data = null)
  41. {
  42. if ($data) {
  43. foreach ($this as $key => $value) {
  44. if (isset($data->{$key})) {
  45. $this->{$key} = $data->{$key};
  46. }
  47. }
  48. }
  49. $this->defaults('session_id', api_get_session_id());
  50. }
  51. function __get($name)
  52. {
  53. $f = array($this, "get_$name");
  54. return call_user_func($f);
  55. }
  56. function __isset($name)
  57. {
  58. $f = array($this, "get_$name");
  59. return is_callable($f);
  60. }
  61. function __set($name, $value)
  62. {
  63. $f = array($this, "set_$name");
  64. if (!is_callable($f)) {
  65. return;
  66. }
  67. call_user_func($f, $value);
  68. }
  69. function before_save()
  70. {
  71. $repo = $this->repository();
  72. $field = $repo->get_id_field();
  73. if (empty($field)) {
  74. return;
  75. }
  76. $value = isset($this->{$field}) ? $this->{$field} : null;
  77. if ($value) {
  78. return;
  79. }
  80. $next_id = $repo->next_id($this);
  81. $this->{$field} = $next_id;
  82. }
  83. function defaults($name, $value)
  84. {
  85. if (property_exists($this, $name) && empty($this->{$name})) {
  86. $this->{$name} = $value;
  87. }
  88. }
  89. }