exerciselink.class.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Defines a gradebook ExerciseLink object.
  5. * @author Bert Steppé
  6. * @package chamilo.gradebook
  7. */
  8. /**
  9. * Class
  10. * @package chamilo.gradebook
  11. */
  12. class ExerciseLink extends AbstractLink
  13. {
  14. // INTERNAL VARIABLES
  15. private $course_info = null;
  16. private $exercise_table = null;
  17. private $exercise_data = null;
  18. // CONSTRUCTORS
  19. function __construct() {
  20. parent::__construct();
  21. $this->set_type(LINK_EXERCISE);
  22. }
  23. // FUNCTIONS IMPLEMENTING ABSTRACTLINK
  24. /**
  25. * Generate an array of exercises that a teacher hasn't created a link for.
  26. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  27. */
  28. public function get_not_created_links() {
  29. return false;
  30. if (empty($this->course_code)) {
  31. die('Error in get_not_created_links() : course code not set');
  32. }
  33. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  34. $sql = 'SELECT id, title from '.$this->get_exercise_table().' exe
  35. WHERE id NOT IN (
  36. SELECT ref_id FROM '.$tbl_grade_links.'
  37. WHERE type = '.LINK_EXERCISE." AND
  38. course_code = '".$this->get_course_code()."'
  39. ) AND
  40. exe.c_id = ".$this->course_id;
  41. $result = Database::query($sql);
  42. $cats = array();
  43. while ($data=Database::fetch_array($result)) {
  44. $cats[] = array ($data['id'], $data['title']);
  45. }
  46. return $cats;
  47. }
  48. /**
  49. * Generate an array of all exercises available.
  50. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  51. */
  52. public function get_all_links() {
  53. if (empty($this->course_code)) {
  54. die('Error in get_not_created_links() : course code not set');
  55. }
  56. $session_id = api_get_session_id();
  57. if (empty($session_id)) {
  58. $session_condition = api_get_session_condition(0, true);
  59. } else {
  60. $session_condition = api_get_session_condition($session_id, true, true);
  61. }
  62. $sql = 'SELECT id,title from '.$this->get_exercise_table().'
  63. WHERE c_id = '.$this->course_id.' AND active=1 '.$session_condition;
  64. /* $sql = 'SELECT id,title from '.$this->get_exercise_table().'
  65. WHERE c_id = '.$this->course_id.' AND active=1 AND session_id='.api_get_session_id().'';
  66. */
  67. $result = Database::query($sql);
  68. $cats = array();
  69. while ($data=Database::fetch_array($result)) {
  70. $cats[] = array ($data['id'], $data['title']);
  71. }
  72. return $cats;
  73. }
  74. /**
  75. * Has anyone done this exercise yet ?
  76. */
  77. public function has_results() {
  78. $tbl_stats = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  79. $session_id = api_get_session_id();
  80. $sql = 'SELECT count(exe_id) AS number FROM '.$tbl_stats."
  81. WHERE session_id = $session_id AND
  82. exe_cours_id = '".$this->get_course_code()."'".' AND
  83. exe_exo_id = '.(int)$this->get_ref_id();
  84. $result = Database::query($sql);
  85. $number=Database::fetch_row($result);
  86. return ($number[0] != 0);
  87. }
  88. /**
  89. * Get the score of this exercise. Only the first attempts are taken into account.
  90. * @param $stud_id student id (default: all students who have results - then the average is returned)
  91. * @return array (score, max) if student is given
  92. * array (sum of scores, number of scores) otherwise
  93. * or null if no scores available
  94. */
  95. public function calc_score($stud_id = null) {
  96. $tbl_stats = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  97. //$tbl_stats_e_attempt_recording = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  98. //the following query should be similar (in conditions) to the one used in exercice/exercice.php, look for note-query-exe-results marker
  99. $session_id = api_get_session_id();
  100. $sql = "SELECT * FROM $tbl_stats
  101. WHERE exe_exo_id = ".intval($this->get_ref_id())." AND
  102. orig_lp_id = 0 AND
  103. orig_lp_item_id = 0 AND
  104. status <> 'incomplete' AND
  105. session_id = $session_id
  106. ";
  107. if (isset($stud_id)) {
  108. $course_code_exe = $this->get_course_code();
  109. $sql .= " AND exe_cours_id = '$course_code_exe' AND
  110. exe_user_id = '$stud_id' ";
  111. }
  112. $sql .= ' ORDER BY exe_id DESC';
  113. $scores = Database::query($sql);
  114. if (isset($stud_id)) {
  115. // for 1 student
  116. if ($data = Database::fetch_array($scores)) {
  117. return array ($data['exe_result'], $data['exe_weighting']);
  118. } else {
  119. return null;
  120. }
  121. } else {
  122. /// all students -> get average
  123. // normal way of getting the info
  124. $students = array(); // user list, needed to make sure we only
  125. // take first attempts into account
  126. $student_count = 0;
  127. $sum = 0;
  128. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  129. if (!in_array($data['exe_user_id'], $students)) {
  130. if ($data['exe_weighting'] != 0) {
  131. $students[] = $data['exe_user_id'];
  132. $student_count++;
  133. $sum += $data['exe_result'] / $data['exe_weighting'];
  134. }
  135. }
  136. }
  137. if ($student_count == 0) {
  138. return null;
  139. } else {
  140. return array ($sum , $student_count);
  141. }
  142. }
  143. }
  144. /**
  145. * Get URL where to go to if the user clicks on the link.
  146. * First we go to exercise_jump.php and then to the result page.
  147. * Check this php file for more info.
  148. */
  149. public function get_link() {
  150. //status student
  151. $user_id = api_get_user_id();
  152. $course_code = $this->get_course_code();
  153. $status_user=api_get_status_of_user_in_course ($user_id, $course_code);
  154. $session_id =api_get_session_id();
  155. $url = api_get_path(WEB_PATH).'main/gradebook/exercise_jump.php?session_id='.$session_id.'&cidReq='.$this->get_course_code().'&gradebook=view&exerciseId='.$this->get_ref_id();
  156. if ((!api_is_allowed_to_edit() && $this->calc_score(api_get_user_id()) == null) || $status_user!=1) {
  157. $url .= '&amp;doexercise='.$this->get_ref_id();
  158. }
  159. return $url;
  160. }
  161. /**
  162. * Get name to display: same as exercise title
  163. */
  164. public function get_name() {
  165. $data = $this->get_exercise_data();
  166. return $data['title'];
  167. }
  168. /**
  169. * Get description to display: same as exercise description
  170. */
  171. public function get_description() {
  172. $data = $this->get_exercise_data();
  173. return $data['description'];
  174. }
  175. /**
  176. * Check if this still links to an exercise
  177. */
  178. public function is_valid_link() {
  179. //$sql = 'SELECT count(id) from '.$this->get_exercise_table().' WHERE c_id = '.$this->course_id.' AND id = '.(int)$this->get_ref_id().' AND session_id='.api_get_session_id().'';
  180. $sql = 'SELECT count(id) from '.$this->get_exercise_table().' WHERE c_id = '.$this->course_id.' AND id = '.(int)$this->get_ref_id().' ';
  181. $result = Database::query($sql);
  182. $number=Database::fetch_row($result);
  183. return ($number[0] != 0);
  184. }
  185. public function get_type_name() {
  186. return get_lang('Quiz');
  187. }
  188. public function needs_name_and_description() {
  189. return false;
  190. }
  191. public function needs_max() {
  192. return false;
  193. }
  194. public function needs_results() {
  195. return false;
  196. }
  197. public function is_allowed_to_change_name() {
  198. return false;
  199. }
  200. // INTERNAL FUNCTIONS
  201. /**
  202. * Lazy load function to get the database table of the exercise
  203. */
  204. private function get_exercise_table () {
  205. $this->exercise_table = Database :: get_course_table(TABLE_QUIZ_TEST);
  206. return $this->exercise_table;
  207. }
  208. /**
  209. * Lazy load function to get the database contents of this exercise
  210. */
  211. private function get_exercise_data() {
  212. $tbl_exercise = $this->get_exercise_table();
  213. if ($tbl_exercise=='') {
  214. return false;
  215. } elseif (!isset($this->exercise_data)) {
  216. $sql = 'SELECT * FROM '.$this->get_exercise_table().'
  217. WHERE c_id = '.$this->course_id.' AND id = '.(int)$this->get_ref_id().' ';
  218. $result = Database::query($sql);
  219. $this->exercise_data=Database::fetch_array($result);
  220. }
  221. return $this->exercise_data;
  222. }
  223. public function get_icon_name() {
  224. return 'exercise';
  225. }
  226. }