evallink.class.php 4.6 KB

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