exerciselink.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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('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 = $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. /*
  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']) ? (int) $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($exerciseId);
  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. $attempts = Database::query($sql);
  281. $counter = 0;
  282. while ($attempt = Database::fetch_array($attempts)) {
  283. $counter++;
  284. }
  285. $result = [$data['exe_result'], $data['exe_weighting'], $data['exe_date'], $counter];
  286. if ($cacheAvailable) {
  287. $cacheDriver->save($key, $result);
  288. }
  289. return $result;
  290. } else {
  291. if ($cacheAvailable) {
  292. $cacheDriver->save($key, null);
  293. }
  294. return null;
  295. }
  296. } else {
  297. // all students -> get average
  298. // normal way of getting the info
  299. $students = []; // user list, needed to make sure we only
  300. // take first attempts into account
  301. $student_count = 0;
  302. $sum = 0;
  303. $bestResult = 0;
  304. $weight = 0;
  305. $sumResult = 0;
  306. $studentList = $this->getStudentList();
  307. $studentIdList = [];
  308. if (!empty($studentList)) {
  309. $studentIdList = array_column($studentList, 'user_id');
  310. }
  311. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  312. // Only take into account users in the current student list.
  313. if (!empty($studentIdList)) {
  314. if (!in_array($data['exe_user_id'], $studentIdList)) {
  315. continue;
  316. }
  317. }
  318. if (!isset($students[$data['exe_user_id']])) {
  319. if ($data['exe_weighting'] != 0) {
  320. $students[$data['exe_user_id']] = $data['exe_result'];
  321. $student_count++;
  322. if ($data['exe_result'] > $bestResult) {
  323. $bestResult = $data['exe_result'];
  324. }
  325. $sum += $data['exe_result'] / $data['exe_weighting'];
  326. $sumResult += $data['exe_result'];
  327. $weight = $data['exe_weighting'];
  328. }
  329. }
  330. }
  331. if ($student_count == 0) {
  332. if ($cacheAvailable) {
  333. $cacheDriver->save($key, null);
  334. }
  335. return null;
  336. } else {
  337. switch ($type) {
  338. case 'best':
  339. $result = [$bestResult, $weight];
  340. if ($cacheAvailable) {
  341. $cacheDriver->save($key, $result);
  342. }
  343. return $result;
  344. break;
  345. case 'average':
  346. $count = count($this->getStudentList());
  347. if (empty($count)) {
  348. $result = [0, $weight];
  349. if ($cacheAvailable) {
  350. $cacheDriver->save($key, $result);
  351. }
  352. return $result;
  353. }
  354. $result = [$sumResult / $count, $weight];
  355. if ($cacheAvailable) {
  356. $cacheDriver->save($key, $result);
  357. }
  358. return $result;
  359. break;
  360. case 'ranking':
  361. $ranking = AbstractLink::getCurrentUserRanking($stud_id, $students);
  362. if ($cacheAvailable) {
  363. $cacheDriver->save($key, $ranking);
  364. }
  365. return $ranking;
  366. break;
  367. default:
  368. $result = [$sum, $student_count];
  369. if ($cacheAvailable) {
  370. $cacheDriver->save($key, $result);
  371. }
  372. return $result;
  373. break;
  374. }
  375. }
  376. }
  377. }
  378. /**
  379. * Get URL where to go to if the user clicks on the link.
  380. * First we go to exercise_jump.php and then to the result page.
  381. * Check this php file for more info.
  382. */
  383. public function get_link()
  384. {
  385. $sessionId = $this->get_session_id();
  386. $data = $this->get_exercise_data();
  387. $exerciseId = $data['id'];
  388. $path = isset($data['path']) ? $data['path'] : '';
  389. $url = api_get_path(WEB_CODE_PATH).'gradebook/exercise_jump.php?'
  390. .http_build_query(
  391. [
  392. 'path' => $path,
  393. 'session_id' => $sessionId,
  394. 'cidReq' => $this->get_course_code(),
  395. 'gradebook' => 'view',
  396. 'exerciseId' => $exerciseId,
  397. 'type' => $this->get_type(),
  398. ]
  399. );
  400. return $url;
  401. }
  402. /**
  403. * Get name to display: same as exercise title.
  404. */
  405. public function get_name()
  406. {
  407. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code.'/document';
  408. require_once api_get_path(SYS_CODE_PATH).'exercise/hotpotatoes.lib.php';
  409. $data = $this->get_exercise_data();
  410. if ($this->is_hp == 1) {
  411. if (isset($data['path'])) {
  412. $title = GetQuizName($data['path'], $documentPath);
  413. if ($title == '') {
  414. $title = basename($data['path']);
  415. }
  416. return $title;
  417. }
  418. }
  419. return $data['title'];
  420. }
  421. /**
  422. * Get description to display: same as exercise description.
  423. */
  424. public function get_description()
  425. {
  426. $data = $this->get_exercise_data();
  427. return isset($data['description']) ? $data['description'] : null;
  428. }
  429. /**
  430. * Check if this still links to an exercise.
  431. */
  432. public function is_valid_link()
  433. {
  434. $exerciseData = $this->get_exercise_data();
  435. return !empty($exerciseData);
  436. }
  437. /**
  438. * @return string
  439. */
  440. public function get_type_name()
  441. {
  442. if ($this->is_hp == 1) {
  443. return 'HotPotatoes';
  444. }
  445. return get_lang('Quiz');
  446. }
  447. public function needs_name_and_description()
  448. {
  449. return false;
  450. }
  451. public function needs_max()
  452. {
  453. return false;
  454. }
  455. public function needs_results()
  456. {
  457. return false;
  458. }
  459. public function is_allowed_to_change_name()
  460. {
  461. return false;
  462. }
  463. /**
  464. * @return string
  465. */
  466. public function get_icon_name()
  467. {
  468. return 'exercise';
  469. }
  470. /**
  471. * @param bool $hp
  472. */
  473. public function setHp($hp)
  474. {
  475. $this->hp = $hp;
  476. }
  477. public function getBestScore()
  478. {
  479. return $this->getStats('best');
  480. }
  481. public function getStats($type)
  482. {
  483. switch ($type) {
  484. case 'best':
  485. break;
  486. }
  487. }
  488. /**
  489. * Lazy load function to get the database contents of this exercise.
  490. */
  491. public function get_exercise_data()
  492. {
  493. $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  494. if ($this->is_hp == 1) {
  495. $table = Database::get_course_table(TABLE_DOCUMENT);
  496. } else {
  497. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  498. }
  499. $exerciseId = $this->get_ref_id();
  500. if (empty($this->exercise_data)) {
  501. if ($this->is_hp == 1) {
  502. $sql = "SELECT * FROM $table ex
  503. INNER JOIN $tableItemProperty ip
  504. ON (ip.ref = ex.id AND ip.c_id = ex.c_id)
  505. WHERE
  506. ip.c_id = $this->course_id AND
  507. ex.c_id = $this->course_id AND
  508. ip.ref = $exerciseId AND
  509. ip.tool = '".TOOL_DOCUMENT."' AND
  510. ex.path LIKE '%htm%' AND
  511. ex.path LIKE '%HotPotatoes_files%' AND
  512. ip.visibility = 1";
  513. $result = Database::query($sql);
  514. $this->exercise_data = Database::fetch_array($result);
  515. } else {
  516. // Try with iid
  517. $sql = 'SELECT * FROM '.$table.'
  518. WHERE
  519. c_id = '.$this->course_id.' AND
  520. iid = '.$exerciseId;
  521. $result = Database::query($sql);
  522. $rows = Database::num_rows($result);
  523. if (!empty($rows)) {
  524. $this->exercise_data = Database::fetch_array($result);
  525. } else {
  526. // Try wit id
  527. $sql = 'SELECT * FROM '.$table.'
  528. WHERE
  529. c_id = '.$this->course_id.' AND
  530. id = '.$exerciseId;
  531. $result = Database::query($sql);
  532. $this->exercise_data = Database::fetch_array($result);
  533. }
  534. }
  535. }
  536. if (empty($this->exercise_data)) {
  537. return false;
  538. }
  539. return $this->exercise_data;
  540. }
  541. /**
  542. * Lazy load function to get the database table of the exercise.
  543. */
  544. private function get_exercise_table()
  545. {
  546. $this->exercise_table = Database::get_course_table(TABLE_QUIZ_TEST);
  547. return $this->exercise_table;
  548. }
  549. }