evallink.class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class to be used as basis for links referring to Evaluation objects.
  5. * @author Bert Steppé
  6. * @package chamilo.gradebook
  7. * @package chamilo.gradebook
  8. */
  9. abstract class EvalLink extends AbstractLink
  10. {
  11. protected $evaluation = null;
  12. /**
  13. * Constructor
  14. */
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. }
  19. /**
  20. * @return bool
  21. */
  22. public function has_results()
  23. {
  24. $eval = $this->get_evaluation();
  25. return $eval->has_results();
  26. }
  27. /**
  28. * @param int $userId
  29. * @param string $type
  30. *
  31. * @return array
  32. */
  33. public function calc_score($userId = null, $type = null)
  34. {
  35. $eval = $this->get_evaluation();
  36. return $eval->calc_score($userId, $type);
  37. }
  38. public function get_link()
  39. {
  40. $eval = $this->get_evaluation();
  41. // course/platform admin can go to the view_results page
  42. if (api_is_allowed_to_edit()) {
  43. return 'gradebook_view_result.php?selecteval=' . $eval->get_id();
  44. } // students can go to the statistics page (if custom display enabled)
  45. elseif (ScoreDisplay :: instance()->is_custom()) {
  46. return 'gradebook_statistics.php?selecteval=' . $eval->get_id();
  47. } else {
  48. return null;
  49. }
  50. }
  51. public function get_name()
  52. {
  53. $eval = $this->get_evaluation();
  54. return $eval->get_name();
  55. }
  56. public function get_description()
  57. {
  58. $eval = $this->get_evaluation();
  59. return $eval->get_description();
  60. }
  61. public function get_max()
  62. {
  63. $eval = $this->get_evaluation();
  64. return $eval->get_max();
  65. }
  66. public function is_valid_link()
  67. {
  68. $eval = $this->get_evaluation();
  69. return (isset($eval));
  70. }
  71. public function needs_name_and_description()
  72. {
  73. return true;
  74. }
  75. public function needs_max()
  76. {
  77. return true;
  78. }
  79. public function needs_results()
  80. {
  81. return true;
  82. }
  83. public function add_linked_data()
  84. {
  85. if ($this->is_valid_link()) {
  86. $this->evaluation->add();
  87. $this->set_ref_id($this->evaluation->get_id());
  88. }
  89. }
  90. public function save_linked_data()
  91. {
  92. if ($this->is_valid_link()) {
  93. $this->evaluation->save();
  94. }
  95. }
  96. public function delete_linked_data()
  97. {
  98. if ($this->is_valid_link()) {
  99. $this->evaluation->delete_with_results();
  100. }
  101. }
  102. public function set_name($name)
  103. {
  104. if ($this->is_valid_link()) {
  105. $this->evaluation->set_name($name);
  106. }
  107. }
  108. public function set_description($description)
  109. {
  110. if ($this->is_valid_link()) {
  111. $this->evaluation->set_description($description);
  112. }
  113. }
  114. public function set_max($max)
  115. {
  116. if ($this->is_valid_link()) {
  117. $this->evaluation->set_max($max);
  118. }
  119. }
  120. // Functions overriding non-trivial implementations from AbstractLink
  121. public function set_date($date)
  122. {
  123. $this->created_at = $date;
  124. if ($this->is_valid_link()) {
  125. $this->evaluation->set_date($date);
  126. }
  127. }
  128. public function set_weight($weight)
  129. {
  130. $this->weight = $weight;
  131. if ($this->is_valid_link()) {
  132. $this->evaluation->set_weight($weight);
  133. }
  134. }
  135. public function set_visible($visible)
  136. {
  137. $this->visible = $visible;
  138. if ($this->is_valid_link()) {
  139. $this->evaluation->set_visible($visible);
  140. }
  141. }
  142. /**
  143. * Lazy load function to get the linked evaluation
  144. */
  145. protected function get_evaluation()
  146. {
  147. if (!isset($this->evaluation)) {
  148. if (isset($this->ref_id)) {
  149. $evalarray = Evaluation::load($this->get_ref_id());
  150. $this->evaluation = $evalarray[0];
  151. } else {
  152. $eval = new Evaluation();
  153. $eval->set_category_id(-1);
  154. $eval->set_date(api_get_utc_datetime()); // these values will be changed
  155. $eval->set_weight(0); // when the link setter
  156. $eval->set_visible(0); // is called
  157. $eval->set_id(-1); // a 'real' id will be set when eval is added to db
  158. $eval->set_user_id($this->get_user_id());
  159. $eval->set_course_code($this->get_course_code());
  160. $this->evaluation = $eval;
  161. $this->set_ref_id($eval->get_id());
  162. }
  163. }
  164. return $this->evaluation;
  165. }
  166. }