exerciselink.class.php 7.4 KB

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