exerciselink.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 = api_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";
  186. } else {
  187. $lpId = null;
  188. if (!empty($exercise->lpList)) {
  189. // Taking only the first LP
  190. $lpId = current($exercise->lpList);
  191. $lpId = $lpId['lp_id'];
  192. }
  193. $sql = "SELECT * FROM $tblStats
  194. WHERE
  195. exe_exo_id = ".intval($this->get_ref_id())." AND
  196. orig_lp_id = $lpId AND
  197. status <> 'incomplete' AND
  198. session_id = $session_id";
  199. }
  200. if (isset($stud_id)) {
  201. $sql .= " AND c_id = $courseId AND exe_user_id = $stud_id ";
  202. }
  203. $sql .= ' ORDER BY exe_id DESC';
  204. } else {
  205. $sql = "SELECT * FROM $tblHp hp, $tblDoc doc
  206. WHERE
  207. hp.c_id = $courseId AND
  208. hp.exe_user_id = $stud_id AND
  209. hp.exe_name = doc.path AND
  210. doc.c_id = hp.c_id AND
  211. doc.id = ".intval($this->get_ref_id());
  212. }
  213. $scores = Database::query($sql);
  214. if (isset($stud_id)) {
  215. // for 1 student
  216. if ($data = Database::fetch_array($scores)) {
  217. return array($data['exe_result'], $data['exe_weighting']);
  218. } else {
  219. return null;
  220. }
  221. } else {
  222. // all students -> get average
  223. // normal way of getting the info
  224. $students = array(); // user list, needed to make sure we only
  225. // take first attempts into account
  226. $student_count = 0;
  227. $sum = 0;
  228. $bestResult = 0;
  229. $weight = 0;
  230. $sumResult = 0;
  231. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  232. if (!isset($students[$data['exe_user_id']])) {
  233. if ($data['exe_weighting'] != 0) {
  234. $students[$data['exe_user_id']] = $data['exe_result'];
  235. $student_count++;
  236. if ($data['exe_result'] > $bestResult) {
  237. $bestResult = $data['exe_result'];
  238. }
  239. $sum += $data['exe_result'] / $data['exe_weighting'];
  240. $sumResult += $data['exe_result'];
  241. $weight = $data['exe_weighting'];
  242. }
  243. }
  244. }
  245. if ($student_count == 0) {
  246. return null;
  247. } else {
  248. switch ($type) {
  249. case 'best':
  250. return array($bestResult, $weight);
  251. break;
  252. case 'average':
  253. return array($sumResult/$student_count, $weight);
  254. break;
  255. case 'ranking':
  256. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  257. break;
  258. default:
  259. return array($sum, $student_count);
  260. break;
  261. }
  262. }
  263. }
  264. }
  265. /**
  266. * Get URL where to go to if the user clicks on the link.
  267. * First we go to exercise_jump.php and then to the result page.
  268. * Check this php file for more info.
  269. */
  270. public function get_link()
  271. {
  272. //status student
  273. $user_id = api_get_user_id();
  274. $course_code = $this->get_course_code();
  275. $courseInfo = api_get_course_info($course_code);
  276. $courseId = $courseInfo['real_id'];
  277. $status_user = api_get_status_of_user_in_course($user_id, $courseId);
  278. $session_id = api_get_session_id();
  279. $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();
  280. if ((!api_is_allowed_to_edit() && $this->calc_score(api_get_user_id()) == null) || $status_user!=1) {
  281. $url .= '&amp;doexercise='.$this->get_ref_id();
  282. }
  283. return $url;
  284. }
  285. /**
  286. * Get name to display: same as exercise title
  287. */
  288. public function get_name()
  289. {
  290. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code."/document";
  291. require_once api_get_path(SYS_CODE_PATH).'exercice/hotpotatoes.lib.php';
  292. $data = $this->get_exercise_data();
  293. if ($this->is_hp == 1) {
  294. if (isset($data['path'])) {
  295. $title = GetQuizName($data['path'], $documentPath);
  296. if ($title == '') {
  297. $title = basename($data['path']);
  298. }
  299. return $title;
  300. }
  301. }
  302. return $data['title'];
  303. }
  304. /**
  305. * Get description to display: same as exercise description
  306. */
  307. public function get_description()
  308. {
  309. $data = $this->get_exercise_data();
  310. return isset($data['description']) ? $data['description'] : null;
  311. }
  312. /**
  313. * Check if this still links to an exercise
  314. */
  315. public function is_valid_link()
  316. {
  317. $sql = 'SELECT count(id) from '.$this->get_exercise_table().'
  318. WHERE c_id = '.$this->course_id.' AND id = '.(int)$this->get_ref_id().' ';
  319. $result = Database::query($sql);
  320. $number = Database::fetch_row($result);
  321. return ($number[0] != 0);
  322. }
  323. /**
  324. * @return array|string
  325. */
  326. public function get_type_name()
  327. {
  328. if ($this->is_hp == 1) {
  329. return get_lang('HotPotatoes');
  330. } else {
  331. return get_lang('Quiz');
  332. }
  333. }
  334. public function needs_name_and_description()
  335. {
  336. return false;
  337. }
  338. public function needs_max()
  339. {
  340. return false;
  341. }
  342. public function needs_results()
  343. {
  344. return false;
  345. }
  346. public function is_allowed_to_change_name() {
  347. return false;
  348. }
  349. /**
  350. * Lazy load function to get the database table of the exercise
  351. */
  352. private function get_exercise_table()
  353. {
  354. $this->exercise_table = Database :: get_course_table(TABLE_QUIZ_TEST);
  355. return $this->exercise_table;
  356. }
  357. /**
  358. * Lazy load function to get the database contents of this exercise
  359. */
  360. private function get_exercise_data()
  361. {
  362. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  363. if ($this->is_hp == 1) {
  364. $tbl_exercise = Database :: get_course_table(TABLE_DOCUMENT);
  365. } else {
  366. $tbl_exercise = $this->get_exercise_table();
  367. }
  368. $ref_id = intval($this->get_ref_id());
  369. if ($tbl_exercise == '') {
  370. return false;
  371. } elseif (!isset($this->exercise_data)) {
  372. if ($this->is_hp == 1) {
  373. $sql = "SELECT * FROM $tbl_exercise ex
  374. INNER JOIN $TBL_ITEM_PROPERTY ip
  375. ON (ip.ref = ex.id AND ip.c_id = ex.c_id)
  376. WHERE
  377. ip.c_id = $this->course_id AND
  378. ex.c_id = $this->course_id AND
  379. ip.ref = $ref_id AND
  380. ip.tool = '".TOOL_DOCUMENT."' AND
  381. ex.path LIKE '%htm%' AND
  382. ex.path LIKE '%HotPotatoes_files%' AND
  383. ip.visibility = 1";
  384. } else {
  385. $sql = 'SELECT * FROM '.$tbl_exercise.'
  386. WHERE
  387. c_id = '.$this->course_id.' AND
  388. id = '.$ref_id.' ';
  389. }
  390. $result = Database::query($sql);
  391. $this->exercise_data = Database::fetch_array($result);
  392. }
  393. return $this->exercise_data;
  394. }
  395. /**
  396. * @return string
  397. */
  398. public function get_icon_name()
  399. {
  400. return 'exercise';
  401. }
  402. }