exerciselink.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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;
  14. private $exercise_table;
  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 = $this->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('Learning path').')',
  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 = $this->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. break;
  185. default:
  186. if (!empty($stud_id)) {
  187. $scoreList = $link->getUserScoreList();
  188. $result = [0, $weight];
  189. if (isset($scoreList[$stud_id])) {
  190. $result = [$scoreList[$stud_id], $weight];
  191. }
  192. return $result;
  193. } else {
  194. $studentCount = count($this->getStudentList());
  195. $sumResult = array_sum($link->getUserScoreList());
  196. $result = [$sumResult, $studentCount];
  197. }
  198. return $result;
  199. break;
  200. }
  201. }
  202. }
  203. $tblStats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  204. $tblHp = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  205. $tblDoc = Database::get_course_table(TABLE_DOCUMENT);
  206. /* the following query should be similar (in conditions) to the one used
  207. in exercise/exercise.php, look for note-query-exe-results marker*/
  208. $sessionId = $this->get_session_id();
  209. $courseId = $this->getCourseId();
  210. $exerciseData = $this->get_exercise_data();
  211. $exerciseId = isset($exerciseData['id']) ? (int) $exerciseData['id'] : 0;
  212. $stud_id = (int) $stud_id;
  213. if (empty($exerciseId)) {
  214. return null;
  215. }
  216. $key = 'exercise_link_id:'.
  217. $this->get_id().
  218. 'exerciseId:'.$exerciseId.'student:'.$stud_id.'session:'.$sessionId.'courseId:'.$courseId.'type:'.$type;
  219. $useCache = api_get_configuration_value('gradebook_use_apcu_cache');
  220. $cacheAvailable = api_get_configuration_value('apc') && $useCache;
  221. $cacheDriver = null;
  222. if ($cacheAvailable) {
  223. $cacheDriver = new \Doctrine\Common\Cache\ApcuCache();
  224. if ($cacheDriver->contains($key)) {
  225. return $cacheDriver->fetch($key);
  226. }
  227. }
  228. $exercise = new Exercise($courseId);
  229. $exercise->read($exerciseId);
  230. if (!$this->is_hp) {
  231. if ($exercise->exercise_was_added_in_lp == false) {
  232. $sql = "SELECT * FROM $tblStats
  233. WHERE
  234. exe_exo_id = $exerciseId AND
  235. orig_lp_id = 0 AND
  236. orig_lp_item_id = 0 AND
  237. status <> 'incomplete' AND
  238. session_id = $sessionId AND
  239. c_id = $courseId
  240. ";
  241. } else {
  242. $lpId = null;
  243. if (!empty($exercise->lpList)) {
  244. // Taking only the first LP
  245. $lpId = current($exercise->lpList);
  246. $lpId = $lpId['lp_id'];
  247. }
  248. $sql = "SELECT *
  249. FROM $tblStats
  250. WHERE
  251. exe_exo_id = $exerciseId AND
  252. orig_lp_id = $lpId AND
  253. status <> 'incomplete' AND
  254. session_id = $sessionId AND
  255. c_id = $courseId ";
  256. }
  257. if (!empty($stud_id) && $type != 'ranking') {
  258. $sql .= " AND exe_user_id = $stud_id ";
  259. }
  260. $sql .= ' ORDER BY exe_id DESC';
  261. } else {
  262. $sql = "SELECT * FROM $tblHp hp
  263. INNER JOIN $tblDoc doc
  264. ON (hp.exe_name = doc.path AND doc.c_id = hp.c_id)
  265. WHERE
  266. hp.c_id = $courseId AND
  267. doc.id = $exerciseId";
  268. if (!empty($stud_id)) {
  269. $sql .= " AND hp.exe_user_id = $stud_id ";
  270. }
  271. }
  272. $scores = Database::query($sql);
  273. if (isset($stud_id) && empty($type)) {
  274. // for 1 student
  275. if ($data = Database::fetch_array($scores)) {
  276. $attempts = Database::query($sql);
  277. $counter = 0;
  278. while ($attempt = Database::fetch_array($attempts)) {
  279. $counter++;
  280. }
  281. $result = [$data['score'], $data['max_score'], $data['exe_date'], $counter];
  282. if ($cacheAvailable) {
  283. $cacheDriver->save($key, $result);
  284. }
  285. return $result;
  286. } else {
  287. if ($cacheAvailable) {
  288. $cacheDriver->save($key, null);
  289. }
  290. return null;
  291. }
  292. } else {
  293. // all students -> get average
  294. // normal way of getting the info
  295. $students = []; // user list, needed to make sure we only
  296. // take first attempts into account
  297. $student_count = 0;
  298. $sum = 0;
  299. $bestResult = 0;
  300. $weight = 0;
  301. $sumResult = 0;
  302. $studentList = $this->getStudentList();
  303. $studentIdList = [];
  304. if (!empty($studentList)) {
  305. $studentIdList = array_column($studentList, 'user_id');
  306. }
  307. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  308. // Only take into account users in the current student list.
  309. if (!empty($studentIdList)) {
  310. if (!in_array($data['exe_user_id'], $studentIdList)) {
  311. continue;
  312. }
  313. }
  314. if (!isset($students[$data['exe_user_id']])) {
  315. if ($data['max_score'] != 0) {
  316. $students[$data['exe_user_id']] = $data['score'];
  317. $student_count++;
  318. if ($data['score'] > $bestResult) {
  319. $bestResult = $data['score'];
  320. }
  321. $sum += $data['score'] / $data['max_score'];
  322. $sumResult += $data['score'];
  323. $weight = $data['max_score'];
  324. }
  325. }
  326. }
  327. if ($student_count == 0) {
  328. if ($cacheAvailable) {
  329. $cacheDriver->save($key, null);
  330. }
  331. return null;
  332. } else {
  333. switch ($type) {
  334. case 'best':
  335. $result = [$bestResult, $weight];
  336. if ($cacheAvailable) {
  337. $cacheDriver->save($key, $result);
  338. }
  339. return $result;
  340. break;
  341. case 'average':
  342. $count = count($this->getStudentList());
  343. if (empty($count)) {
  344. $result = [0, $weight];
  345. if ($cacheAvailable) {
  346. $cacheDriver->save($key, $result);
  347. }
  348. return $result;
  349. }
  350. $result = [$sumResult / $count, $weight];
  351. if ($cacheAvailable) {
  352. $cacheDriver->save($key, $result);
  353. }
  354. return $result;
  355. break;
  356. case 'ranking':
  357. $ranking = AbstractLink::getCurrentUserRanking($stud_id, $students);
  358. if ($cacheAvailable) {
  359. $cacheDriver->save($key, $ranking);
  360. }
  361. return $ranking;
  362. break;
  363. default:
  364. $result = [$sum, $student_count];
  365. if ($cacheAvailable) {
  366. $cacheDriver->save($key, $result);
  367. }
  368. return $result;
  369. break;
  370. }
  371. }
  372. }
  373. }
  374. /**
  375. * Get URL where to go to if the user clicks on the link.
  376. * First we go to exercise_jump.php and then to the result page.
  377. * Check this php file for more info.
  378. */
  379. public function get_link()
  380. {
  381. $sessionId = $this->get_session_id();
  382. $data = $this->get_exercise_data();
  383. $exerciseId = $data['id'];
  384. $path = isset($data['path']) ? $data['path'] : '';
  385. $url = api_get_path(WEB_CODE_PATH).'gradebook/exercise_jump.php?'
  386. .http_build_query(
  387. [
  388. 'path' => $path,
  389. 'session_id' => $sessionId,
  390. 'cidReq' => $this->get_course_code(),
  391. 'gradebook' => 'view',
  392. 'exerciseId' => $exerciseId,
  393. 'type' => $this->get_type(),
  394. ]
  395. );
  396. return $url;
  397. }
  398. /**
  399. * Get name to display: same as exercise title.
  400. */
  401. public function get_name()
  402. {
  403. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code.'/document';
  404. require_once api_get_path(SYS_CODE_PATH).'exercise/hotpotatoes.lib.php';
  405. $data = $this->get_exercise_data();
  406. if ($this->is_hp == 1) {
  407. if (isset($data['path'])) {
  408. $title = GetQuizName($data['path'], $documentPath);
  409. if ($title == '') {
  410. $title = basename($data['path']);
  411. }
  412. return $title;
  413. }
  414. }
  415. return $data['title'];
  416. }
  417. /**
  418. * Get description to display: same as exercise description.
  419. */
  420. public function get_description()
  421. {
  422. $data = $this->get_exercise_data();
  423. return isset($data['description']) ? $data['description'] : null;
  424. }
  425. /**
  426. * Check if this still links to an exercise.
  427. */
  428. public function is_valid_link()
  429. {
  430. $exerciseData = $this->get_exercise_data();
  431. return !empty($exerciseData);
  432. }
  433. /**
  434. * @return string
  435. */
  436. public function get_type_name()
  437. {
  438. if ($this->is_hp == 1) {
  439. return 'HotPotatoes';
  440. }
  441. return get_lang('Tests');
  442. }
  443. public function needs_name_and_description()
  444. {
  445. return false;
  446. }
  447. public function needs_max()
  448. {
  449. return false;
  450. }
  451. public function needs_results()
  452. {
  453. return false;
  454. }
  455. public function is_allowed_to_change_name()
  456. {
  457. return false;
  458. }
  459. /**
  460. * @return string
  461. */
  462. public function get_icon_name()
  463. {
  464. return 'exercise';
  465. }
  466. /**
  467. * @param bool $hp
  468. */
  469. public function setHp($hp)
  470. {
  471. $this->hp = $hp;
  472. }
  473. public function getBestScore()
  474. {
  475. return $this->getStats('best');
  476. }
  477. public function getStats($type)
  478. {
  479. switch ($type) {
  480. case 'best':
  481. break;
  482. }
  483. }
  484. /**
  485. * Lazy load function to get the database contents of this exercise.
  486. */
  487. public function get_exercise_data()
  488. {
  489. $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  490. if ($this->is_hp == 1) {
  491. $table = Database::get_course_table(TABLE_DOCUMENT);
  492. } else {
  493. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  494. }
  495. $exerciseId = $this->get_ref_id();
  496. if (empty($this->exercise_data)) {
  497. if ($this->is_hp == 1) {
  498. $sql = "SELECT * FROM $table ex
  499. INNER JOIN $tableItemProperty ip
  500. ON (ip.ref = ex.id AND ip.c_id = ex.c_id)
  501. WHERE
  502. ip.c_id = $this->course_id AND
  503. ex.c_id = $this->course_id AND
  504. ip.ref = $exerciseId AND
  505. ip.tool = '".TOOL_DOCUMENT."' AND
  506. ex.path LIKE '%htm%' AND
  507. ex.path LIKE '%HotPotatoes_files%' AND
  508. ip.visibility = 1";
  509. $result = Database::query($sql);
  510. $this->exercise_data = Database::fetch_array($result);
  511. } else {
  512. // Try with iid
  513. $sql = 'SELECT * FROM '.$table.'
  514. WHERE
  515. c_id = '.$this->course_id.' AND
  516. iid = '.$exerciseId;
  517. $result = Database::query($sql);
  518. $rows = Database::num_rows($result);
  519. if (!empty($rows)) {
  520. $this->exercise_data = Database::fetch_array($result);
  521. } else {
  522. // Try wit id
  523. $sql = 'SELECT * FROM '.$table.'
  524. WHERE
  525. c_id = '.$this->course_id.' AND
  526. id = '.$exerciseId;
  527. $result = Database::query($sql);
  528. $this->exercise_data = Database::fetch_array($result);
  529. }
  530. }
  531. }
  532. if (empty($this->exercise_data)) {
  533. return false;
  534. }
  535. return $this->exercise_data;
  536. }
  537. /**
  538. * Lazy load function to get the database table of the exercise.
  539. */
  540. private function get_exercise_table()
  541. {
  542. $this->exercise_table = Database::get_course_table(TABLE_QUIZ_TEST);
  543. return $this->exercise_table;
  544. }
  545. }