exerciselink.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 type = '.LINK_EXERCISE." AND
  42. course_code = '".$this->get_course_code()."'
  43. ) AND
  44. exe.c_id = ".$this->course_id;
  45. $result = Database::query($sql);
  46. $cats = array();
  47. while ($data=Database::fetch_array($result)) {
  48. $cats[] = array ($data['id'], $data['title']);
  49. }
  50. return $cats;
  51. }
  52. /**
  53. * Generate an array of all exercises available.
  54. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  55. */
  56. public function get_all_links()
  57. {
  58. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  59. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  60. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code."/document";
  61. if (empty($this->course_code)) {
  62. die('Error in get_not_created_links() : course code not set');
  63. }
  64. $session_id = api_get_session_id();
  65. if (empty($session_id)) {
  66. $session_condition = api_get_session_condition(0, true);
  67. } else {
  68. $session_condition = api_get_session_condition($session_id, true, true);
  69. }
  70. // @todo
  71. $uploadPath = null;
  72. $sql = 'SELECT id,title from '.$this->get_exercise_table().'
  73. WHERE c_id = '.$this->course_id.' AND active=1 '.$session_condition;
  74. $sql2 = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility, d.id
  75. FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  76. WHERE
  77. d.c_id = $this->course_id AND
  78. ip.c_id = $this->course_id AND
  79. d.id = ip.ref AND
  80. ip.tool = '".TOOL_DOCUMENT."' AND
  81. (d.path LIKE '%htm%')AND (d.path LIKE '%HotPotatoes_files%') AND
  82. d.path LIKE '".Database :: escape_string($uploadPath.'/%/%')."' AND
  83. ip.visibility='1'
  84. ";
  85. require_once api_get_path(SYS_CODE_PATH).'exercice/hotpotatoes.lib.php';
  86. if (!$this->is_hp) {
  87. $result = Database::query($sql);
  88. } else {
  89. $result2 = Database::query($sql2);
  90. }
  91. $cats = array();
  92. if (isset($result)) {
  93. if (Database::num_rows($result) > 0) {
  94. while ($data=Database::fetch_array($result)) {
  95. $cats[] = array ($data['id'], $data['title']);
  96. }
  97. }
  98. }
  99. if (isset($result2)) {
  100. if (Database::num_rows($result2) > 0) {
  101. while ($row=Database::fetch_array($result2)) {
  102. /*$path = $data['path'];
  103. $fname = GetQuizName($path,$documentPath);
  104. $cats[] = array ($data['id'], $fname);*/
  105. $attribute['path'][] = $row['path'];
  106. $attribute['visibility'][] = $row['visibility'];
  107. $attribute['comment'][] = $row['comment'];
  108. $attribute['id'] = $row['id'];
  109. if (isset($attribute['path']) && is_array($attribute['path'])) {
  110. while (list($key, $path) = each($attribute['path'])) {
  111. $title = GetQuizName($path, $documentPath);
  112. if ($title == '') {
  113. $title = basename($path);
  114. }
  115. $cats[] = array ($attribute['id'], $title.'(HP)');
  116. }
  117. }
  118. }
  119. }
  120. }
  121. return $cats;
  122. }
  123. /**
  124. * Has anyone done this exercise yet ?
  125. */
  126. public function has_results()
  127. {
  128. $tbl_stats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  129. $session_id = api_get_session_id();
  130. $sql = 'SELECT count(exe_id) AS number FROM '.$tbl_stats."
  131. WHERE
  132. session_id = $session_id AND
  133. exe_cours_id = '".Database::escape_string($this->get_course_code())."'".' AND
  134. exe_exo_id = '.(int)$this->get_ref_id();
  135. $result = Database::query($sql);
  136. $number=Database::fetch_row($result);
  137. return ($number[0] != 0);
  138. }
  139. /**
  140. * Get the score of this exercise. Only the first attempts are taken into account.
  141. * @param int $stud_id student id (default: all students who have results - then the average is returned)
  142. * @return array (score, max) if student is given
  143. * array (sum of scores, number of scores) otherwise
  144. * or null if no scores available
  145. */
  146. public function calc_score($stud_id = null)
  147. {
  148. $tblStats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  149. $tblHp = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  150. $tblDoc = Database::get_course_table(TABLE_DOCUMENT);
  151. /* the following query should be similar (in conditions) to the one used
  152. in exercice/exercice.php, look for note-query-exe-results marker*/
  153. $session_id = api_get_session_id();
  154. if (!$this->is_hp) {
  155. $sql = "SELECT * FROM $tblStats
  156. WHERE
  157. exe_exo_id = ".intval($this->get_ref_id())." AND
  158. orig_lp_id = 0 AND
  159. orig_lp_item_id = 0 AND
  160. status <> 'incomplete' AND
  161. session_id = $session_id";
  162. if (isset($stud_id)) {
  163. $course_code_exe = $this->get_course_code();
  164. $sql .= " AND exe_cours_id = '$course_code_exe' AND exe_user_id = '$stud_id' ";
  165. }
  166. $sql .= ' ORDER BY exe_id DESC';
  167. } else {
  168. $course_code_exe = $this->get_course_code();
  169. $courseId = $this->getCourseId();
  170. $sql = "SELECT * FROM $tblHp hp, $tblDoc doc
  171. WHERE
  172. hp.exe_cours_id = '$course_code_exe' AND
  173. hp.exe_user_id = '$stud_id' AND
  174. hp.exe_name = doc.path AND
  175. doc.c_id = $courseId AND
  176. doc.id = ".intval($this->get_ref_id())."";
  177. }
  178. $scores = Database::query($sql);
  179. if (isset($stud_id)) {
  180. // for 1 student
  181. if ($data = Database::fetch_array($scores)) {
  182. return array($data['exe_result'], $data['exe_weighting']);
  183. } else {
  184. return null;
  185. }
  186. } else {
  187. // all students -> get average
  188. // normal way of getting the info
  189. $students = array(); // user list, needed to make sure we only
  190. // take first attempts into account
  191. $student_count = 0;
  192. $sum = 0;
  193. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  194. if (!in_array($data['exe_user_id'], $students)) {
  195. if ($data['exe_weighting'] != 0) {
  196. $students[] = $data['exe_user_id'];
  197. $student_count++;
  198. $sum += $data['exe_result'] / $data['exe_weighting'];
  199. }
  200. }
  201. }
  202. if ($student_count == 0) {
  203. return null;
  204. } else {
  205. return array($sum, $student_count);
  206. }
  207. }
  208. }
  209. /**
  210. * Get URL where to go to if the user clicks on the link.
  211. * First we go to exercise_jump.php and then to the result page.
  212. * Check this php file for more info.
  213. */
  214. public function get_link()
  215. {
  216. //status student
  217. $user_id = api_get_user_id();
  218. $course_code = $this->get_course_code();
  219. $status_user=api_get_status_of_user_in_course ($user_id, $course_code);
  220. $session_id =api_get_session_id();
  221. $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();
  222. if ((!api_is_allowed_to_edit() && $this->calc_score(api_get_user_id()) == null) || $status_user!=1) {
  223. $url .= '&amp;doexercise='.$this->get_ref_id();
  224. }
  225. return $url;
  226. }
  227. /**
  228. * Get name to display: same as exercise title
  229. */
  230. public function get_name()
  231. {
  232. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code."/document";
  233. require_once api_get_path(SYS_CODE_PATH).'exercice/hotpotatoes.lib.php';
  234. $data = $this->get_exercise_data();
  235. if ($this->is_hp == 1) {
  236. if (isset($data['path'])) {
  237. $title = GetQuizName($data['path'], $documentPath);
  238. if ($title == '') {
  239. $title = basename($data['path']);
  240. }
  241. return $title;
  242. }
  243. }
  244. return $data['title'];
  245. }
  246. /**
  247. * Get description to display: same as exercise description
  248. */
  249. public function get_description()
  250. {
  251. $data = $this->get_exercise_data();
  252. return isset($data['description']) ? $data['description'] : null;
  253. }
  254. /**
  255. * Check if this still links to an exercise
  256. */
  257. public function is_valid_link()
  258. {
  259. $sql = 'SELECT count(id) from '.$this->get_exercise_table().'
  260. WHERE c_id = '.$this->course_id.' AND id = '.(int)$this->get_ref_id().' ';
  261. $result = Database::query($sql);
  262. $number=Database::fetch_row($result);
  263. return ($number[0] != 0);
  264. }
  265. /**
  266. * @return array|string
  267. */
  268. public function get_type_name()
  269. {
  270. if ($this->is_hp == 1) {
  271. return get_lang('HotPotatoes');
  272. } else {
  273. return get_lang('Quiz');
  274. }
  275. }
  276. public function needs_name_and_description()
  277. {
  278. return false;
  279. }
  280. public function needs_max()
  281. {
  282. return false;
  283. }
  284. public function needs_results()
  285. {
  286. return false;
  287. }
  288. public function is_allowed_to_change_name() {
  289. return false;
  290. }
  291. /**
  292. * Lazy load function to get the database table of the exercise
  293. */
  294. private function get_exercise_table()
  295. {
  296. $this->exercise_table = Database :: get_course_table(TABLE_QUIZ_TEST);
  297. return $this->exercise_table;
  298. }
  299. /**
  300. * Lazy load function to get the database contents of this exercise
  301. */
  302. private function get_exercise_data()
  303. {
  304. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  305. if ($this->is_hp == 1) {
  306. $tbl_exercise = Database :: get_course_table(TABLE_DOCUMENT);
  307. } else {
  308. $tbl_exercise = $this->get_exercise_table();
  309. }
  310. $ref_id = intval($this->get_ref_id());
  311. if ($tbl_exercise == '') {
  312. return false;
  313. } elseif (!isset($this->exercise_data)) {
  314. if ($this->is_hp == 1) {
  315. $sql = "SELECT * FROM $tbl_exercise ex
  316. INNER JOIN $TBL_ITEM_PROPERTY ip
  317. ON (ip.ref = ex.id AND ip.c_id = ex.c_id)
  318. WHERE
  319. ip.c_id = $this->course_id AND
  320. ex.c_id = $this->course_id AND
  321. ip.ref = $ref_id AND
  322. ip.tool = '".TOOL_DOCUMENT."' AND
  323. ex.path LIKE '%htm%' AND
  324. ex.path LIKE '%HotPotatoes_files%' AND
  325. ip.visibility = 1";
  326. } else {
  327. $sql = 'SELECT * FROM '.$tbl_exercise.'
  328. WHERE
  329. c_id = '.$this->course_id.' AND
  330. id = '.$ref_id.' ';
  331. }
  332. $result = Database::query($sql);
  333. $this->exercise_data = Database::fetch_array($result);
  334. }
  335. return $this->exercise_data;
  336. }
  337. /**
  338. * @return string
  339. */
  340. public function get_icon_name()
  341. {
  342. return 'exercise';
  343. }
  344. }