exerciselink.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ExerciseLink
  5. * Defines a gradebook ExerciseLink object.
  6. * @author Bert Steppé
  7. * @package chamilo.gradebook
  8. */
  9. class ExerciseLink extends AbstractLink
  10. {
  11. private $course_info = null;
  12. private $exercise_table = null;
  13. private $exercise_data = null;
  14. private $is_hp;
  15. /**
  16. * @param int $hp
  17. */
  18. public function __construct($hp = 0)
  19. {
  20. parent::__construct();
  21. $this->set_type(LINK_EXERCISE);
  22. $this->is_hp = $hp;
  23. if ($this->is_hp == 1) {
  24. $this->set_type(LINK_HOTPOTATOES);
  25. }
  26. }
  27. /**
  28. * Generate an array of exercises that a teacher hasn't created a link for.
  29. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  30. */
  31. public function get_not_created_links()
  32. {
  33. return false;
  34. if (empty($this->course_code)) {
  35. die('Error in get_not_created_links() : course code not set');
  36. }
  37. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  38. $sql = 'SELECT id, title FROM '.$this->get_exercise_table().' exe
  39. WHERE id NOT IN (
  40. SELECT ref_id FROM '.$tbl_grade_links.'
  41. WHERE
  42. type = '.LINK_EXERCISE." AND
  43. course_code = '".$this->get_course_code()."'
  44. ) AND
  45. exe.c_id = ".$this->course_id;
  46. $result = Database::query($sql);
  47. $cats = array();
  48. while ($data=Database::fetch_array($result)) {
  49. $cats[] = array ($data['id'], $data['title']);
  50. }
  51. return $cats;
  52. }
  53. /**
  54. * Generate an array of all exercises available.
  55. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  56. */
  57. public function get_all_links()
  58. {
  59. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  60. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  61. $exerciseTable = $this->get_exercise_table();
  62. $lpItemTable = Database :: get_course_table(TABLE_LP_ITEM);
  63. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code."/document";
  64. if (empty($this->course_code)) {
  65. die('Error in get_not_created_links() : course code not set');
  66. }
  67. $session_id = api_get_session_id();
  68. if (empty($session_id)) {
  69. $session_condition = api_get_session_condition(0, true);
  70. } else {
  71. $session_condition = api_get_session_condition($session_id, true, true);
  72. }
  73. // @todo
  74. $uploadPath = null;
  75. $sql = 'SELECT id,title FROM '.$exerciseTable.'
  76. WHERE c_id = '.$this->course_id.' AND active=1 '.$session_condition;
  77. $sqlLp = "SELECT e.id, e.title FROM $exerciseTable e INNER JOIN $lpItemTable i
  78. ON (e.c_id = i.c_id AND e.id = i.path)
  79. WHERE e.c_id = $this->course_id AND active = 0 AND item_type = 'quiz'
  80. $session_condition";
  81. $sql2 = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility, d.id
  82. FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  83. WHERE
  84. d.c_id = $this->course_id AND
  85. ip.c_id = $this->course_id AND
  86. d.id = ip.ref AND
  87. ip.tool = '".TOOL_DOCUMENT."' AND
  88. (d.path LIKE '%htm%')AND (d.path LIKE '%HotPotatoes_files%') AND
  89. d.path LIKE '".Database :: escape_string($uploadPath.'/%/%')."' AND
  90. ip.visibility='1'
  91. ";
  92. require_once api_get_path(SYS_CODE_PATH).'exercice/hotpotatoes.lib.php';
  93. $exerciseInLP = array();
  94. if (!$this->is_hp) {
  95. $result = Database::query($sql);
  96. $resultLp = Database::query($sqlLp);
  97. $exerciseInLP = Database::store_result($resultLp);
  98. } else {
  99. $result2 = Database::query($sql2);
  100. }
  101. $cats = array();
  102. if (isset($result)) {
  103. if (Database::num_rows($result) > 0) {
  104. while ($data=Database::fetch_array($result)) {
  105. $cats[] = array ($data['id'], $data['title']);
  106. }
  107. }
  108. }
  109. if (isset($result2)) {
  110. if (Database::num_rows($result2) > 0) {
  111. while ($row=Database::fetch_array($result2)) {
  112. /*$path = $data['path'];
  113. $fname = GetQuizName($path,$documentPath);
  114. $cats[] = array ($data['id'], $fname);*/
  115. $attribute['path'][] = $row['path'];
  116. $attribute['visibility'][] = $row['visibility'];
  117. $attribute['comment'][] = $row['comment'];
  118. $attribute['id'] = $row['id'];
  119. if (isset($attribute['path']) && is_array($attribute['path'])) {
  120. while (list($key, $path) = each($attribute['path'])) {
  121. $title = GetQuizName($path, $documentPath);
  122. if ($title == '') {
  123. $title = basename($path);
  124. }
  125. $cats[] = array($attribute['id'], $title.'(HP)');
  126. }
  127. }
  128. }
  129. }
  130. }
  131. if (!empty($exerciseInLP)) {
  132. foreach ($exerciseInLP as $exercise) {
  133. $cats[] = array(
  134. $exercise['id'],
  135. $exercise['title'].' ('.get_lang('ToolLearnpath').')'
  136. );
  137. }
  138. }
  139. return $cats;
  140. }
  141. /**
  142. * Has anyone done this exercise yet ?
  143. */
  144. public function has_results()
  145. {
  146. $tbl_stats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  147. $session_id = api_get_session_id();
  148. $course_id = api_get_course_int_id($this->get_course_code());
  149. $sql = 'SELECT count(exe_id) AS number FROM '.$tbl_stats."
  150. WHERE
  151. session_id = $session_id AND
  152. c_id = $course_id AND
  153. exe_exo_id = ".(int)$this->get_ref_id();
  154. $result = Database::query($sql);
  155. $number=Database::fetch_row($result);
  156. return $number[0] != 0;
  157. }
  158. /**
  159. * Get the score of this exercise. Only the first attempts are taken into account.
  160. * @param int $stud_id student id (default: all students who have results -
  161. * then the average is returned)
  162. * @return array (score, max) if student is given
  163. * array (sum of scores, number of scores) otherwise
  164. * or null if no scores available
  165. */
  166. public function calc_score($stud_id = null, $type = null)
  167. {
  168. $tblStats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  169. $tblHp = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  170. $tblDoc = Database::get_course_table(TABLE_DOCUMENT);
  171. /* the following query should be similar (in conditions) to the one used
  172. in exercice/exercice.php, look for note-query-exe-results marker*/
  173. $session_id = $this->get_session_id();
  174. $courseId = $this->getCourseId();
  175. $exercise = new Exercise($courseId);
  176. $exercise->read($this->get_ref_id());
  177. if (!$this->is_hp) {
  178. if ($exercise->exercise_was_added_in_lp == false) {
  179. $sql = "SELECT * FROM $tblStats
  180. WHERE
  181. exe_exo_id = ".intval($this->get_ref_id())." AND
  182. orig_lp_id = 0 AND
  183. orig_lp_item_id = 0 AND
  184. status <> 'incomplete' AND
  185. session_id = $session_id AND
  186. c_id = $courseId
  187. ";
  188. } else {
  189. $lpId = null;
  190. if (!empty($exercise->lpList)) {
  191. // Taking only the first LP
  192. $lpId = current($exercise->lpList);
  193. $lpId = $lpId['lp_id'];
  194. }
  195. $sql = "SELECT * FROM $tblStats
  196. WHERE
  197. exe_exo_id = ".intval($this->get_ref_id())." AND
  198. orig_lp_id = $lpId AND
  199. status <> 'incomplete' AND
  200. session_id = $session_id AND
  201. c_id = $courseId
  202. ";
  203. }
  204. if (!empty($stud_id) && $type != 'ranking') {
  205. $sql .= " AND exe_user_id = $stud_id ";
  206. }
  207. $sql .= ' ORDER BY exe_id DESC';
  208. } else {
  209. $sql = "SELECT * FROM $tblHp hp, $tblDoc doc
  210. WHERE
  211. hp.c_id = $courseId AND
  212. hp.exe_user_id = $stud_id AND
  213. hp.exe_name = doc.path AND
  214. doc.c_id = hp.c_id AND
  215. doc.id = ".intval($this->get_ref_id());
  216. }
  217. $scores = Database::query($sql);
  218. if (isset($stud_id) && empty($type)) {
  219. // for 1 student
  220. if ($data = Database::fetch_array($scores)) {
  221. return array($data['exe_result'], $data['exe_weighting']);
  222. } else {
  223. return null;
  224. }
  225. } else {
  226. // all students -> get average
  227. // normal way of getting the info
  228. $students = array(); // user list, needed to make sure we only
  229. // take first attempts into account
  230. $student_count = 0;
  231. $sum = 0;
  232. $bestResult = 0;
  233. $weight = 0;
  234. $sumResult = 0;
  235. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  236. if (!isset($students[$data['exe_user_id']])) {
  237. if ($data['exe_weighting'] != 0) {
  238. $students[$data['exe_user_id']] = $data['exe_result'];
  239. $student_count++;
  240. if ($data['exe_result'] > $bestResult) {
  241. $bestResult = $data['exe_result'];
  242. }
  243. $sum += $data['exe_result'] / $data['exe_weighting'];
  244. $sumResult += $data['exe_result'];
  245. $weight = $data['exe_weighting'];
  246. }
  247. }
  248. }
  249. if ($student_count == 0) {
  250. return null;
  251. } else {
  252. switch ($type) {
  253. case 'best':
  254. return array($bestResult, $weight);
  255. break;
  256. case 'average':
  257. $count = count($this->getStudentList());
  258. if (empty($count)) {
  259. return array(0, $weight);
  260. }
  261. return array($sumResult/$count , $weight);
  262. break;
  263. case 'ranking':
  264. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  265. break;
  266. default:
  267. return array($sum, $student_count);
  268. break;
  269. }
  270. }
  271. }
  272. }
  273. /**
  274. * Get URL where to go to if the user clicks on the link.
  275. * First we go to exercise_jump.php and then to the result page.
  276. * Check this php file for more info.
  277. */
  278. public function get_link()
  279. {
  280. //status student
  281. $user_id = api_get_user_id();
  282. $course_code = $this->get_course_code();
  283. $courseInfo = api_get_course_info($course_code);
  284. $courseId = $courseInfo['real_id'];
  285. $status_user = api_get_status_of_user_in_course($user_id, $courseId);
  286. $session_id = api_get_session_id();
  287. $url = api_get_path(WEB_CODE_PATH).'gradebook/exercise_jump.php?session_id='.$session_id.'&cidReq='.$this->get_course_code().'&gradebook=view&exerciseId='.$this->get_ref_id().'&type='.$this->get_type();
  288. if ((!api_is_allowed_to_edit() && $this->calc_score(api_get_user_id()) == null) || $status_user!=1) {
  289. $url .= '&amp;doexercise='.$this->get_ref_id();
  290. }
  291. return $url;
  292. }
  293. /**
  294. * Get name to display: same as exercise title
  295. */
  296. public function get_name()
  297. {
  298. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code."/document";
  299. require_once api_get_path(SYS_CODE_PATH).'exercice/hotpotatoes.lib.php';
  300. $data = $this->get_exercise_data();
  301. if ($this->is_hp == 1) {
  302. if (isset($data['path'])) {
  303. $title = GetQuizName($data['path'], $documentPath);
  304. if ($title == '') {
  305. $title = basename($data['path']);
  306. }
  307. return $title;
  308. }
  309. }
  310. return $data['title'];
  311. }
  312. /**
  313. * Get description to display: same as exercise description
  314. */
  315. public function get_description()
  316. {
  317. $data = $this->get_exercise_data();
  318. return isset($data['description']) ? $data['description'] : null;
  319. }
  320. /**
  321. * Check if this still links to an exercise
  322. */
  323. public function is_valid_link()
  324. {
  325. $sql = 'SELECT count(id) from '.$this->get_exercise_table().'
  326. WHERE c_id = '.$this->course_id.' AND id = '.(int)$this->get_ref_id().' ';
  327. $result = Database::query($sql);
  328. $number = Database::fetch_row($result);
  329. return ($number[0] != 0);
  330. }
  331. /**
  332. * @return string
  333. */
  334. public function get_type_name()
  335. {
  336. if ($this->is_hp == 1) {
  337. return get_lang('HotPotatoes');
  338. } else {
  339. return get_lang('Quiz');
  340. }
  341. }
  342. public function needs_name_and_description()
  343. {
  344. return false;
  345. }
  346. public function needs_max()
  347. {
  348. return false;
  349. }
  350. public function needs_results()
  351. {
  352. return false;
  353. }
  354. public function is_allowed_to_change_name() {
  355. return false;
  356. }
  357. /**
  358. * Lazy load function to get the database table of the exercise
  359. */
  360. private function get_exercise_table()
  361. {
  362. $this->exercise_table = Database :: get_course_table(TABLE_QUIZ_TEST);
  363. return $this->exercise_table;
  364. }
  365. /**
  366. * Lazy load function to get the database contents of this exercise
  367. */
  368. private function get_exercise_data()
  369. {
  370. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  371. if ($this->is_hp == 1) {
  372. $tbl_exercise = Database :: get_course_table(TABLE_DOCUMENT);
  373. } else {
  374. $tbl_exercise = $this->get_exercise_table();
  375. }
  376. $ref_id = intval($this->get_ref_id());
  377. if ($tbl_exercise == '') {
  378. return false;
  379. } elseif (!isset($this->exercise_data)) {
  380. if ($this->is_hp == 1) {
  381. $sql = "SELECT * FROM $tbl_exercise ex
  382. INNER JOIN $TBL_ITEM_PROPERTY ip
  383. ON (ip.ref = ex.id AND ip.c_id = ex.c_id)
  384. WHERE
  385. ip.c_id = $this->course_id AND
  386. ex.c_id = $this->course_id AND
  387. ip.ref = $ref_id AND
  388. ip.tool = '".TOOL_DOCUMENT."' AND
  389. ex.path LIKE '%htm%' AND
  390. ex.path LIKE '%HotPotatoes_files%' AND
  391. ip.visibility = 1";
  392. } else {
  393. $sql = 'SELECT * FROM '.$tbl_exercise.'
  394. WHERE
  395. c_id = '.$this->course_id.' AND
  396. id = '.$ref_id.' ';
  397. }
  398. $result = Database::query($sql);
  399. $this->exercise_data = Database::fetch_array($result);
  400. }
  401. return $this->exercise_data;
  402. }
  403. /**
  404. * @return string
  405. */
  406. public function get_icon_name()
  407. {
  408. return 'exercise';
  409. }
  410. }