exerciselink.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ExerciseLink
  5. * Defines a gradebook ExerciseLink object.
  6. *
  7. * @author Bert Steppé
  8. *
  9. * @package chamilo.gradebook
  10. */
  11. class ExerciseLink extends AbstractLink
  12. {
  13. private $course_info = null;
  14. private $exercise_table = null;
  15. private $exercise_data = [];
  16. private $is_hp;
  17. /**
  18. * @param int $hp
  19. */
  20. public function __construct($hp = 0)
  21. {
  22. parent::__construct();
  23. $this->set_type(LINK_EXERCISE);
  24. $this->is_hp = $hp;
  25. if ($this->is_hp == 1) {
  26. $this->set_type(LINK_HOTPOTATOES);
  27. }
  28. }
  29. /**
  30. * Generate an array of all exercises available.
  31. *
  32. * @param bool $getOnlyHotPotatoes
  33. *
  34. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  35. */
  36. public function get_all_links($getOnlyHotPotatoes = false)
  37. {
  38. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  39. $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  40. $exerciseTable = $this->get_exercise_table();
  41. $lpItemTable = Database::get_course_table(TABLE_LP_ITEM);
  42. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code."/document";
  43. if (empty($this->course_code)) {
  44. return [];
  45. }
  46. $sessionId = api_get_session_id();
  47. if (empty($sessionId)) {
  48. $session_condition = api_get_session_condition(0, true);
  49. } else {
  50. $session_condition = api_get_session_condition($sessionId, true, true);
  51. }
  52. // @todo
  53. $uploadPath = null;
  54. $sql = 'SELECT iid, title FROM '.$exerciseTable.'
  55. WHERE c_id = '.$this->course_id.' AND active=1 '.$session_condition;
  56. $sqlLp = "SELECT e.iid, e.title
  57. FROM $exerciseTable e
  58. INNER JOIN $lpItemTable i
  59. ON (e.c_id = i.c_id AND e.id = i.path)
  60. WHERE
  61. e.c_id = $this->course_id AND
  62. active = 0 AND
  63. item_type = 'quiz'
  64. $session_condition";
  65. $sql2 = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility, d.id
  66. FROM $TBL_DOCUMENT d
  67. INNER JOIN $tableItemProperty ip
  68. ON (d.id = ip.ref AND d.c_id = ip.c_id)
  69. WHERE
  70. d.c_id = $this->course_id AND
  71. ip.c_id = $this->course_id AND
  72. ip.tool = '".TOOL_DOCUMENT."' AND
  73. (d.path LIKE '%htm%') AND
  74. (d.path LIKE '%HotPotatoes_files%') AND
  75. d.path LIKE '".Database::escape_string($uploadPath.'/%/%')."' AND
  76. ip.visibility = '1'
  77. ";
  78. require_once api_get_path(SYS_CODE_PATH).'exercise/hotpotatoes.lib.php';
  79. $exerciseInLP = [];
  80. if (!$this->is_hp) {
  81. $result = Database::query($sql);
  82. $resultLp = Database::query($sqlLp);
  83. $exerciseInLP = Database::store_result($resultLp);
  84. } else {
  85. $result2 = Database::query($sql2);
  86. }
  87. $cats = [];
  88. if (isset($result)) {
  89. if (Database::num_rows($result) > 0) {
  90. while ($data = Database::fetch_array($result)) {
  91. $cats[] = [$data['iid'], $data['title']];
  92. }
  93. }
  94. }
  95. $hotPotatoes = [];
  96. if (isset($result2)) {
  97. if (Database::num_rows($result2) > 0) {
  98. while ($row = Database::fetch_array($result2)) {
  99. $attribute['path'][] = $row['path'];
  100. $attribute['visibility'][] = $row['visibility'];
  101. $attribute['comment'][] = $row['comment'];
  102. $attribute['id'] = $row['id'];
  103. if (isset($attribute['path']) && is_array($attribute['path'])) {
  104. foreach ($attribute['path'] as $path) {
  105. $title = GetQuizName($path, $documentPath);
  106. if ($title == '') {
  107. $title = basename($path);
  108. }
  109. $element = [$attribute['id'], $title.'(HP)'];
  110. $cats[] = $element;
  111. $hotPotatoes[] = $element;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. if ($getOnlyHotPotatoes) {
  118. return $hotPotatoes;
  119. }
  120. if (!empty($exerciseInLP)) {
  121. foreach ($exerciseInLP as $exercise) {
  122. $cats[] = [
  123. $exercise['iid'],
  124. $exercise['title'].' ('.get_lang('ToolLearnpath').')',
  125. ];
  126. }
  127. }
  128. return $cats;
  129. }
  130. /**
  131. * Has anyone done this exercise yet ?
  132. */
  133. public function has_results()
  134. {
  135. $tbl_stats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  136. $sessionId = api_get_session_id();
  137. $course_id = api_get_course_int_id($this->get_course_code());
  138. $sql = "SELECT count(exe_id) AS number
  139. FROM $tbl_stats
  140. WHERE
  141. session_id = $sessionId AND
  142. c_id = $course_id AND
  143. exe_exo_id = ".$this->get_ref_id();
  144. $result = Database::query($sql);
  145. $number = Database::fetch_row($result);
  146. return $number[0] != 0;
  147. }
  148. /**
  149. * Get the score of this exercise. Only the first attempts are taken into account.
  150. *
  151. * @param int $stud_id student id (default: all students who have results -
  152. * then the average is returned)
  153. * @param string $type
  154. *
  155. * @return array (score, max) if student is given
  156. * array (sum of scores, number of scores) otherwise
  157. * or null if no scores available
  158. */
  159. public function calc_score($stud_id = null, $type = null)
  160. {
  161. $allowStats = api_get_configuration_value('allow_gradebook_stats');
  162. if ($allowStats) {
  163. $link = $this->entity;
  164. if (!empty($link)) {
  165. $weight = $link->getScoreWeight();
  166. switch ($type) {
  167. case 'best':
  168. $bestResult = $link->getBestScore();
  169. $result = [$bestResult, $weight];
  170. return $result;
  171. break;
  172. case 'average':
  173. $count = count($this->getStudentList());
  174. if (empty($count)) {
  175. $result = [0, $weight];
  176. return $result;
  177. }
  178. $sumResult = array_sum($link->getUserScoreList());
  179. $result = [$sumResult / $count, $weight];
  180. return $result;
  181. break;
  182. case 'ranking':
  183. return [null, null];
  184. /*
  185. $newList = [];
  186. $ranking = AbstractLink::getCurrentUserRanking($stud_id, $link->getUserScoreList());
  187. return $ranking;*/
  188. break;
  189. default:
  190. if (!empty($stud_id)) {
  191. $scoreList = $link->getUserScoreList();
  192. $result = [0, $weight];
  193. if (isset($scoreList[$stud_id])) {
  194. $result = [$scoreList[$stud_id], $weight];
  195. }
  196. return $result;
  197. } else {
  198. $studentCount = count($this->getStudentList());
  199. $sumResult = array_sum($link->getUserScoreList());
  200. $result = [$sumResult, $studentCount];
  201. }
  202. return $result;
  203. break;
  204. }
  205. }
  206. }
  207. $tblStats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  208. $tblHp = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  209. $tblDoc = Database::get_course_table(TABLE_DOCUMENT);
  210. /* the following query should be similar (in conditions) to the one used
  211. in exercise/exercise.php, look for note-query-exe-results marker*/
  212. $sessionId = $this->get_session_id();
  213. $courseId = $this->getCourseId();
  214. $exerciseData = $this->get_exercise_data();
  215. $exerciseId = isset($exerciseData['id']) ? $exerciseData['id'] : 0;
  216. $stud_id = (int) $stud_id;
  217. if (empty($exerciseId)) {
  218. return null;
  219. }
  220. $key = 'exercise_link_id:'.
  221. $this->get_id().
  222. 'exerciseId:'.$exerciseId.'student:'.$stud_id.'session:'.$sessionId.'courseId:'.$courseId.'type:'.$type;
  223. $useCache = api_get_configuration_value('gradebook_use_apcu_cache');
  224. $cacheAvailable = api_get_configuration_value('apc') && $useCache;
  225. $cacheDriver = null;
  226. if ($cacheAvailable) {
  227. $cacheDriver = new \Doctrine\Common\Cache\ApcuCache();
  228. if ($cacheDriver->contains($key)) {
  229. return $cacheDriver->fetch($key);
  230. }
  231. }
  232. $exercise = new Exercise($courseId);
  233. $exercise->read($exerciseData['id']);
  234. if (!$this->is_hp) {
  235. if ($exercise->exercise_was_added_in_lp == false) {
  236. $sql = "SELECT * FROM $tblStats
  237. WHERE
  238. exe_exo_id = $exerciseId AND
  239. orig_lp_id = 0 AND
  240. orig_lp_item_id = 0 AND
  241. status <> 'incomplete' AND
  242. session_id = $sessionId AND
  243. c_id = $courseId
  244. ";
  245. } else {
  246. $lpId = null;
  247. if (!empty($exercise->lpList)) {
  248. // Taking only the first LP
  249. $lpId = current($exercise->lpList);
  250. $lpId = $lpId['lp_id'];
  251. }
  252. $sql = "SELECT *
  253. FROM $tblStats
  254. WHERE
  255. exe_exo_id = $exerciseId AND
  256. orig_lp_id = $lpId AND
  257. status <> 'incomplete' AND
  258. session_id = $sessionId AND
  259. c_id = $courseId ";
  260. }
  261. if (!empty($stud_id) && $type != 'ranking') {
  262. $sql .= " AND exe_user_id = $stud_id ";
  263. }
  264. $sql .= ' ORDER BY exe_id DESC';
  265. } else {
  266. $sql = "SELECT * FROM $tblHp hp
  267. INNER JOIN $tblDoc doc
  268. ON (hp.exe_name = doc.path AND doc.c_id = hp.c_id)
  269. WHERE
  270. hp.c_id = $courseId AND
  271. doc.id = ".$exerciseId;
  272. if (!empty($stud_id)) {
  273. $sql .= " AND hp.exe_user_id = $stud_id ";
  274. }
  275. }
  276. $scores = Database::query($sql);
  277. if (isset($stud_id) && empty($type)) {
  278. // for 1 student
  279. if ($data = Database::fetch_array($scores)) {
  280. $result = [$data['exe_result'], $data['exe_weighting']];
  281. if ($cacheAvailable) {
  282. $cacheDriver->save($key, $result);
  283. }
  284. return $result;
  285. } else {
  286. if ($cacheAvailable) {
  287. $cacheDriver->save($key, null);
  288. }
  289. return null;
  290. }
  291. } else {
  292. // all students -> get average
  293. // normal way of getting the info
  294. $students = []; // user list, needed to make sure we only
  295. // take first attempts into account
  296. $student_count = 0;
  297. $sum = 0;
  298. $bestResult = 0;
  299. $weight = 0;
  300. $sumResult = 0;
  301. $studentList = $this->getStudentList();
  302. $studentIdList = [];
  303. if (!empty($studentList)) {
  304. $studentIdList = array_column($studentList, 'user_id');
  305. }
  306. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  307. // Only take into account users in the current student list.
  308. if (!empty($studentIdList)) {
  309. if (!in_array($data['exe_user_id'], $studentIdList)) {
  310. continue;
  311. }
  312. }
  313. if (!isset($students[$data['exe_user_id']])) {
  314. if ($data['exe_weighting'] != 0) {
  315. $students[$data['exe_user_id']] = $data['exe_result'];
  316. $student_count++;
  317. if ($data['exe_result'] > $bestResult) {
  318. $bestResult = $data['exe_result'];
  319. }
  320. $sum += $data['exe_result'] / $data['exe_weighting'];
  321. $sumResult += $data['exe_result'];
  322. $weight = $data['exe_weighting'];
  323. }
  324. }
  325. }
  326. if ($student_count == 0) {
  327. if ($cacheAvailable) {
  328. $cacheDriver->save($key, null);
  329. }
  330. return null;
  331. } else {
  332. switch ($type) {
  333. case 'best':
  334. $result = [$bestResult, $weight];
  335. if ($cacheAvailable) {
  336. $cacheDriver->save($key, $result);
  337. }
  338. return $result;
  339. break;
  340. case 'average':
  341. $count = count($this->getStudentList());
  342. if (empty($count)) {
  343. $result = [0, $weight];
  344. if ($cacheAvailable) {
  345. $cacheDriver->save($key, $result);
  346. }
  347. return $result;
  348. }
  349. $result = [$sumResult / $count, $weight];
  350. if ($cacheAvailable) {
  351. $cacheDriver->save($key, $result);
  352. }
  353. return $result;
  354. break;
  355. case 'ranking':
  356. $ranking = AbstractLink::getCurrentUserRanking($stud_id, $students);
  357. if ($cacheAvailable) {
  358. $cacheDriver->save($key, $ranking);
  359. }
  360. return $ranking;
  361. break;
  362. default:
  363. $result = [$sum, $student_count];
  364. if ($cacheAvailable) {
  365. $cacheDriver->save($key, $result);
  366. }
  367. return $result;
  368. break;
  369. }
  370. }
  371. }
  372. }
  373. /**
  374. * Get URL where to go to if the user clicks on the link.
  375. * First we go to exercise_jump.php and then to the result page.
  376. * Check this php file for more info.
  377. */
  378. public function get_link()
  379. {
  380. // Status student
  381. $user_id = api_get_user_id();
  382. $sessionId = api_get_session_id();
  383. $course_code = $this->get_course_code();
  384. $courseInfo = api_get_course_info($course_code);
  385. $courseId = $courseInfo['real_id'];
  386. $status_user = api_get_status_of_user_in_course($user_id, $courseId);
  387. $data = $this->get_exercise_data();
  388. $exerciseId = $data['id'];
  389. $path = isset($data['path']) ? $data['path'] : '';
  390. $url = api_get_path(WEB_CODE_PATH).'gradebook/exercise_jump.php?'
  391. .http_build_query(
  392. [
  393. 'path' => $path,
  394. 'session_id' => $sessionId,
  395. 'cidReq' => $this->get_course_code(),
  396. 'gradebook' => 'view',
  397. 'exerciseId' => $exerciseId,
  398. 'type' => $this->get_type(),
  399. ]
  400. );
  401. return $url;
  402. }
  403. /**
  404. * Get name to display: same as exercise title.
  405. */
  406. public function get_name()
  407. {
  408. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code.'/document';
  409. require_once api_get_path(SYS_CODE_PATH).'exercise/hotpotatoes.lib.php';
  410. $data = $this->get_exercise_data();
  411. if ($this->is_hp == 1) {
  412. if (isset($data['path'])) {
  413. $title = GetQuizName($data['path'], $documentPath);
  414. if ($title == '') {
  415. $title = basename($data['path']);
  416. }
  417. return $title;
  418. }
  419. }
  420. return $data['title'];
  421. }
  422. /**
  423. * Get description to display: same as exercise description.
  424. */
  425. public function get_description()
  426. {
  427. $data = $this->get_exercise_data();
  428. return isset($data['description']) ? $data['description'] : null;
  429. }
  430. /**
  431. * Check if this still links to an exercise.
  432. */
  433. public function is_valid_link()
  434. {
  435. $exerciseData = $this->get_exercise_data();
  436. return !empty($exerciseData);
  437. }
  438. /**
  439. * @return string
  440. */
  441. public function get_type_name()
  442. {
  443. if ($this->is_hp == 1) {
  444. return 'HotPotatoes';
  445. } else {
  446. return get_lang('Quiz');
  447. }
  448. }
  449. public function needs_name_and_description()
  450. {
  451. return false;
  452. }
  453. public function needs_max()
  454. {
  455. return false;
  456. }
  457. public function needs_results()
  458. {
  459. return false;
  460. }
  461. public function is_allowed_to_change_name()
  462. {
  463. return false;
  464. }
  465. /**
  466. * @return string
  467. */
  468. public function get_icon_name()
  469. {
  470. return 'exercise';
  471. }
  472. /**
  473. * @param bool $hp
  474. */
  475. public function setHp($hp)
  476. {
  477. $this->hp = $hp;
  478. }
  479. public function getBestScore()
  480. {
  481. return $this->getStats('best');
  482. }
  483. public function getStats($type)
  484. {
  485. switch ($type) {
  486. case 'best':
  487. break;
  488. }
  489. }
  490. /**
  491. * Lazy load function to get the database table of the exercise.
  492. */
  493. private function get_exercise_table()
  494. {
  495. $this->exercise_table = Database::get_course_table(TABLE_QUIZ_TEST);
  496. return $this->exercise_table;
  497. }
  498. /**
  499. * Lazy load function to get the database contents of this exercise.
  500. */
  501. private function get_exercise_data()
  502. {
  503. $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  504. if ($this->is_hp == 1) {
  505. $table = Database::get_course_table(TABLE_DOCUMENT);
  506. } else {
  507. $table = $this->get_exercise_table();
  508. }
  509. $exerciseId = $this->get_ref_id();
  510. if ($table == '') {
  511. return false;
  512. } elseif (empty($this->exercise_data)) {
  513. if ($this->is_hp == 1) {
  514. $sql = "SELECT * FROM $table ex
  515. INNER JOIN $tableItemProperty ip
  516. ON (ip.ref = ex.id AND ip.c_id = ex.c_id)
  517. WHERE
  518. ip.c_id = $this->course_id AND
  519. ex.c_id = $this->course_id AND
  520. ip.ref = $exerciseId AND
  521. ip.tool = '".TOOL_DOCUMENT."' AND
  522. ex.path LIKE '%htm%' AND
  523. ex.path LIKE '%HotPotatoes_files%' AND
  524. ip.visibility = 1";
  525. $result = Database::query($sql);
  526. $this->exercise_data = Database::fetch_array($result);
  527. } else {
  528. // Try with iid
  529. $sql = 'SELECT * FROM '.$table.'
  530. WHERE
  531. c_id = '.$this->course_id.' AND
  532. iid = '.$exerciseId;
  533. $result = Database::query($sql);
  534. $rows = Database::num_rows($result);
  535. if (!empty($rows)) {
  536. $this->exercise_data = Database::fetch_array($result);
  537. } else {
  538. // Try wit id
  539. $sql = 'SELECT * FROM '.$table.'
  540. WHERE
  541. c_id = '.$this->course_id.' AND
  542. id = '.$exerciseId;
  543. $result = Database::query($sql);
  544. $this->exercise_data = Database::fetch_array($result);
  545. }
  546. }
  547. }
  548. return $this->exercise_data;
  549. }
  550. }