course.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Model;
  3. use Database;
  4. use ResultSet;
  5. /**
  6. * Description of course
  7. *
  8. * @license see /license.txt
  9. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  10. */
  11. class Course
  12. {
  13. /**
  14. *
  15. * @param string $where
  16. * @return \ResultSet
  17. */
  18. public static function query($where)
  19. {
  20. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  21. $sql = "SELECT * FROM $table ";
  22. $sql .= $where ? "WHERE $where" : '';
  23. $result = new ResultSet($sql);
  24. return $result->return_type(__CLASS__);
  25. }
  26. /**
  27. *
  28. * @param string $code
  29. * @return \Model\Course|null
  30. */
  31. public static function get_by_code($code)
  32. {
  33. $current = self::current();
  34. if ($current && $current->get_code() == $code) {
  35. return $current;
  36. }
  37. return self::query("code = '$code'")->first();
  38. }
  39. /**
  40. *
  41. * @param int $id
  42. * @return \Model\Course|null
  43. */
  44. public static function get_by_id($id)
  45. {
  46. $id = (int) $id;
  47. $current = self::current();
  48. if ($current && $current->get_id() == $id) {
  49. return $current;
  50. }
  51. return self::query("id = $id")->first();
  52. }
  53. /**
  54. *
  55. * @return \Model\Course|null
  56. */
  57. public static function current()
  58. {
  59. global $_course;
  60. /**
  61. * Note that $_course = -1 when not set.
  62. */
  63. if (empty($_course) || !is_array($_course)) {
  64. return null;
  65. }
  66. static $result = null;
  67. if (empty($result)) {
  68. $id = $_course['real_id'];
  69. $result = self::query("id = $id")->first();
  70. }
  71. return $result;
  72. }
  73. protected $id = 0;
  74. protected $code = 0;
  75. protected $directory = '';
  76. protected $show_score = '';
  77. public function __construct($data)
  78. {
  79. $data = (object) $data;
  80. $this->id = $data->id;
  81. $this->code = $data->code;
  82. $this->directory = $data->directory;
  83. $this->show_score = $data->show_score;
  84. }
  85. public function get_id()
  86. {
  87. return $this->id;
  88. }
  89. public function set_id($value)
  90. {
  91. $this->id = (int) $value;
  92. }
  93. public function get_code()
  94. {
  95. return $this->code;
  96. }
  97. public function set_code($value)
  98. {
  99. $this->code = (int) $value;
  100. }
  101. public function get_directory()
  102. {
  103. return $this->directory;
  104. }
  105. public function set_directory($value)
  106. {
  107. $this->directory = (int) $value;
  108. }
  109. public function get_show_score()
  110. {
  111. return $this->show_score;
  112. }
  113. public function set_show_score($value)
  114. {
  115. $this->show_score = (int) $value;
  116. }
  117. public function get_path()
  118. {
  119. $dir = $this->directory;
  120. if (empty($dir)) {
  121. return '';
  122. }
  123. return api_get_path(SYS_COURSE_PATH) . $dir . '/';
  124. }
  125. }