exerciselink.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. private $is_hp;
  19. /**
  20. * @param int $hp
  21. */
  22. public function __construct($hp = 0)
  23. {
  24. parent::__construct();
  25. $this->set_type(LINK_EXERCISE);
  26. $this->is_hp = $hp;
  27. if ($this->is_hp == 1) {
  28. $this->set_type(LINK_HOTPOTATOES);
  29. }
  30. }
  31. /**
  32. * Generate an array of exercises that a teacher hasn't created a link for.
  33. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  34. */
  35. public function get_not_created_links()
  36. {
  37. return false;
  38. if (empty($this->course_code)) {
  39. die('Error in get_not_created_links() : course code not set');
  40. }
  41. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  42. $sql = 'SELECT id, title from '.$this->get_exercise_table().' exe
  43. WHERE id NOT IN (
  44. SELECT ref_id FROM '.$tbl_grade_links.'
  45. WHERE type = '.LINK_EXERCISE." AND
  46. course_code = '".$this->get_course_code()."'
  47. ) AND
  48. exe.c_id = ".$this->course_id;
  49. $result = Database::query($sql);
  50. $cats = array();
  51. while ($data=Database::fetch_array($result)) {
  52. $cats[] = array ($data['id'], $data['title']);
  53. }
  54. return $cats;
  55. }
  56. /**
  57. * Generate an array of all exercises available.
  58. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  59. */
  60. public function get_all_links()
  61. {
  62. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  63. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  64. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code."/document";
  65. if (empty($this->course_code)) {
  66. die('Error in get_not_created_links() : course code not set');
  67. }
  68. $session_id = api_get_session_id();
  69. if (empty($session_id)) {
  70. $session_condition = api_get_session_condition(0, true);
  71. } else {
  72. $session_condition = api_get_session_condition($session_id, true, true);
  73. }
  74. $uploadPath = null;
  75. $sql = 'SELECT id,title from '.$this->get_exercise_table().'
  76. WHERE c_id = '.$this->course_id.' AND active=1 '.$session_condition;
  77. $sql2 = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility, d.id
  78. FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  79. WHERE d.c_id = $this->course_id AND
  80. ip.c_id = $this->course_id AND
  81. d.id = ip.ref AND ip.tool = '".TOOL_DOCUMENT."' AND (d.path LIKE '%htm%')AND (d.path LIKE '%HotPotatoes_files%')
  82. AND d.path LIKE '".Database :: escape_string($uploadPath)."/%/%' AND ip.visibility='1'";
  83. /*
  84. $sql = 'SELECT id,title from '.$this->get_exercise_table().'
  85. WHERE c_id = '.$this->course_id.' AND active=1 AND session_id='.api_get_session_id().'';
  86. */
  87. require_once api_get_path(SYS_CODE_PATH).'exercice/hotpotatoes.lib.php';
  88. if (!$this->is_hp) {
  89. $result = Database::query($sql);
  90. } else {
  91. $result2 = Database::query($sql2);
  92. }
  93. $cats = array();
  94. if (isset($result)) {
  95. if (Database::num_rows($result) > 0) {
  96. while ($data=Database::fetch_array($result)) {
  97. $cats[] = array ($data['id'], $data['title']);
  98. }
  99. }
  100. }
  101. if (isset($result2)) {
  102. if (Database::num_rows($result2) > 0) {
  103. while ($row=Database::fetch_array($result2)) {
  104. /*$path = $data['path'];
  105. $fname = GetQuizName($path,$documentPath);
  106. $cats[] = array ($data['id'], $fname);*/
  107. $attribute['path'][] = $row['path'];
  108. $attribute['visibility'][] = $row['visibility'];
  109. $attribute['comment'][] = $row['comment'];
  110. $attribute['id'] = $row['id'];
  111. if (isset($attribute['path']) && is_array($attribute['path'])) {
  112. $hotpotatoes_exist = true;
  113. while (list($key, $path) = each($attribute['path'])) {
  114. $item = '';
  115. $title = GetQuizName($path, $documentPath);
  116. if ($title == '') {
  117. $title = basename($path);
  118. }
  119. $cats[] = array ($attribute['id'], $title.'(HP)');
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return $cats;
  126. }
  127. /**
  128. * Has anyone done this exercise yet ?
  129. */
  130. public function has_results() {
  131. $tbl_stats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  132. $session_id = api_get_session_id();
  133. $sql = 'SELECT count(exe_id) AS number FROM '.$tbl_stats."
  134. WHERE session_id = $session_id AND
  135. c_id = '".$this->course_id."'".' AND
  136. exe_exo_id = '.(int)$this->get_ref_id();
  137. $result = Database::query($sql);
  138. $number=Database::fetch_row($result);
  139. return ($number[0] != 0);
  140. }
  141. /**
  142. * Get the score of this exercise. Only the first attempts are taken into account.
  143. * @param $stud_id student id (default: all students who have results - then the average is returned)
  144. * @return array (score, max) if student is given
  145. * array (sum of scores, number of scores) otherwise
  146. * or null if no scores available
  147. */
  148. public function calc_score($stud_id = null)
  149. {
  150. $tbl_stats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  151. // The following query should be similar (in conditions)
  152. // to the one used in exercice/exercice.php, look for note-query-exe-results marker
  153. $session_id = api_get_session_id();
  154. $sql = "SELECT * FROM $tbl_stats
  155. WHERE exe_exo_id = ".intval($this->get_ref_id())." AND
  156. orig_lp_id = 0 AND
  157. orig_lp_item_id = 0 AND
  158. status <> 'incomplete' AND
  159. session_id = $session_id";
  160. if (isset($stud_id)) {
  161. $sql .= " AND c_id = '{$this->course_id}' AND
  162. exe_user_id = '$stud_id' ";
  163. }
  164. $sql .= ' ORDER BY exe_id DESC';
  165. $scores = Database::query($sql);
  166. if (isset($stud_id)) {
  167. // for 1 student
  168. if ($data = Database::fetch_array($scores)) {
  169. return array ($data['exe_result'], $data['exe_weighting']);
  170. } else {
  171. return null;
  172. }
  173. } else {
  174. /// all students -> get average
  175. // normal way of getting the info
  176. $students = array(); // user list, needed to make sure we only
  177. // take first attempts into account
  178. $student_count = 0;
  179. $sum = 0;
  180. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  181. if (!in_array($data['exe_user_id'], $students)) {
  182. if ($data['exe_weighting'] != 0) {
  183. $students[] = $data['exe_user_id'];
  184. $student_count++;
  185. $sum += $data['exe_result'] / $data['exe_weighting'];
  186. }
  187. }
  188. }
  189. if ($student_count == 0) {
  190. return null;
  191. } else {
  192. return array ($sum , $student_count);
  193. }
  194. }
  195. }
  196. /**
  197. * Get URL where to go to if the user clicks on the link.
  198. * First we go to exercise_jump.php and then to the result page.
  199. * Check this php file for more info.
  200. */
  201. public function get_link()
  202. {
  203. //status student
  204. $user_id = api_get_user_id();
  205. $status_user = api_get_status_of_user_in_course($user_id, $this->course_id);
  206. $session_id =api_get_session_id();
  207. $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();
  208. if ((!api_is_allowed_to_edit() && $this->calc_score(api_get_user_id()) == null) || $status_user!=1) {
  209. $url .= '&amp;doexercise='.$this->get_ref_id();
  210. }
  211. return $url;
  212. }
  213. /**
  214. * Get name to display: same as exercise title
  215. */
  216. public function get_name()
  217. {
  218. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code."/document";
  219. require_once api_get_path(SYS_CODE_PATH).'exercice/hotpotatoes.lib.php';
  220. $data = $this->get_exercise_data();
  221. if ($this->is_hp == 1) {
  222. if (isset($data['path'])) {
  223. $hotpotatoes_exist = true;
  224. $item = '';
  225. $title = GetQuizName($data['path'], $documentPath);
  226. if ($title == '') {
  227. $title = basename($data['path']);
  228. }
  229. return $title;
  230. }
  231. }
  232. return $data['title'];
  233. }
  234. /**
  235. * Get description to display: same as exercise description
  236. */
  237. public function get_description()
  238. {
  239. $data = $this->get_exercise_data();
  240. return isset($data['description']) ? $data['description'] : null;
  241. }
  242. /**
  243. * Check if this still links to an exercise
  244. */
  245. public function is_valid_link()
  246. {
  247. $sql = 'SELECT count(id) from '.$this->get_exercise_table().'
  248. WHERE c_id = '.$this->course_id.' AND id = '.(int)$this->get_ref_id().' ';
  249. $result = Database::query($sql);
  250. $number=Database::fetch_row($result);
  251. return ($number[0] != 0);
  252. }
  253. /**
  254. * @return array|string
  255. */
  256. public function get_type_name()
  257. {
  258. if ($this->is_hp == 1) {
  259. return get_lang('HotPotatoes');
  260. } else {
  261. return get_lang('Quiz');
  262. }
  263. }
  264. public function needs_name_and_description()
  265. {
  266. return false;
  267. }
  268. public function needs_max()
  269. {
  270. return false;
  271. }
  272. public function needs_results()
  273. {
  274. return false;
  275. }
  276. public function is_allowed_to_change_name() {
  277. return false;
  278. }
  279. /**
  280. * Lazy load function to get the database table of the exercise
  281. */
  282. private function get_exercise_table()
  283. {
  284. $this->exercise_table = Database :: get_course_table(TABLE_QUIZ_TEST);
  285. return $this->exercise_table;
  286. }
  287. /**
  288. * Lazy load function to get the database contents of this exercise
  289. */
  290. private function get_exercise_data()
  291. {
  292. if ($this->is_hp == 1) {
  293. $tbl_exercise = Database :: get_course_table(TABLE_DOCUMENT);
  294. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  295. } else {
  296. $tbl_exercise = $this->get_exercise_table();
  297. }
  298. if ($tbl_exercise=='') {
  299. return false;
  300. } elseif (!isset($this->exercise_data)) {
  301. if ($this->is_hp == 1) {
  302. $ref_id = intval($this->get_ref_id());
  303. $sql = "SELECT * FROM $tbl_exercise ex, $TBL_ITEM_PROPERTY ip
  304. WHERE
  305. ip.ref = ex.id AND
  306. ip.c_id = $this->course_id AND
  307. ex.c_id = $this->course_id AND
  308. ip.ref = $ref_id AND
  309. ip.tool = '".TOOL_DOCUMENT."' AND
  310. ex.path LIKE '%htm%' AND
  311. ex.path LIKE '%HotPotatoes_files%' AND
  312. ip.visibility = 1";
  313. } else {
  314. $sql = 'SELECT * FROM '.$tbl_exercise.'
  315. WHERE
  316. c_id = '.$this->course_id.' AND
  317. id = '.(int)$this->get_ref_id().' ';
  318. }
  319. $result = Database::query($sql);
  320. $this->exercise_data=Database::fetch_array($result);
  321. }
  322. return $this->exercise_data;
  323. }
  324. /**
  325. * @return string
  326. */
  327. public function get_icon_name()
  328. {
  329. return 'exercise';
  330. }
  331. }