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