evallink.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. *
  30. * @return array
  31. */
  32. public function calc_score($userId = null)
  33. {
  34. $eval = $this->get_evaluation();
  35. return $eval->calc_score($userId);
  36. }
  37. public function get_link()
  38. {
  39. $eval = $this->get_evaluation();
  40. // course/platform admin can go to the view_results page
  41. if (api_is_allowed_to_edit()) {
  42. return 'gradebook_view_result.php?selecteval=' . $eval->get_id();
  43. } // students can go to the statistics page (if custom display enabled)
  44. elseif (ScoreDisplay :: instance()->is_custom()) {
  45. return 'gradebook_statistics.php?selecteval=' . $eval->get_id();
  46. } else {
  47. return null;
  48. }
  49. }
  50. public function get_name()
  51. {
  52. $eval = $this->get_evaluation();
  53. return $eval->get_name();
  54. }
  55. public function get_description()
  56. {
  57. $eval = $this->get_evaluation();
  58. return $eval->get_description();
  59. }
  60. public function get_max()
  61. {
  62. $eval = $this->get_evaluation();
  63. return $eval->get_max();
  64. }
  65. public function is_valid_link()
  66. {
  67. $eval = $this->get_evaluation();
  68. return (isset($eval));
  69. }
  70. public function needs_name_and_description()
  71. {
  72. return true;
  73. }
  74. public function needs_max()
  75. {
  76. return true;
  77. }
  78. public function needs_results()
  79. {
  80. return true;
  81. }
  82. public function add_linked_data()
  83. {
  84. if ($this->is_valid_link()) {
  85. $this->evaluation->add();
  86. $this->set_ref_id($this->evaluation->get_id());
  87. }
  88. }
  89. public function save_linked_data()
  90. {
  91. if ($this->is_valid_link()) {
  92. $this->evaluation->save();
  93. }
  94. }
  95. public function delete_linked_data()
  96. {
  97. if ($this->is_valid_link()) {
  98. $this->evaluation->delete_with_results();
  99. }
  100. }
  101. public function set_name($name)
  102. {
  103. if ($this->is_valid_link()) {
  104. $this->evaluation->set_name($name);
  105. }
  106. }
  107. public function set_description($description)
  108. {
  109. if ($this->is_valid_link()) {
  110. $this->evaluation->set_description($description);
  111. }
  112. }
  113. public function set_max($max)
  114. {
  115. if ($this->is_valid_link()) {
  116. $this->evaluation->set_max($max);
  117. }
  118. }
  119. // Functions overriding non-trivial implementations from AbstractLink
  120. public function set_date($date)
  121. {
  122. $this->created_at = $date;
  123. if ($this->is_valid_link()) {
  124. $this->evaluation->set_date($date);
  125. }
  126. }
  127. public function set_weight($weight)
  128. {
  129. $this->weight = $weight;
  130. if ($this->is_valid_link()) {
  131. $this->evaluation->set_weight($weight);
  132. }
  133. }
  134. public function set_visible($visible)
  135. {
  136. $this->visible = $visible;
  137. if ($this->is_valid_link()) {
  138. $this->evaluation->set_visible($visible);
  139. }
  140. }
  141. /**
  142. * Lazy load function to get the linked evaluation
  143. */
  144. protected function get_evaluation ()
  145. {
  146. if (!isset($this->evaluation)) {
  147. if (isset($this->ref_id)) {
  148. $evalarray = Evaluation::load($this->get_ref_id());
  149. $this->evaluation = $evalarray[0];
  150. } else {
  151. $eval = new Evaluation();
  152. $eval->set_category_id(-1);
  153. $eval->set_date(api_get_utc_datetime()); // these values will be changed
  154. $eval->set_weight(0); // when the link setter
  155. $eval->set_visible(0); // is called
  156. $eval->set_id(-1); // a 'real' id will be set when eval is added to db
  157. $eval->set_user_id($this->get_user_id());
  158. $eval->set_course_code($this->get_course_code());
  159. $this->evaluation = $eval;
  160. $this->set_ref_id($eval->get_id());
  161. }
  162. }
  163. return $this->evaluation;
  164. }
  165. }