exerciselink.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Defines a gradebook ExerciseLink object.
  5. * @author Bert Steppé
  6. * @package chamilo.gradebook
  7. */
  8. /**
  9. * Class
  10. * @package chamilo.gradebook
  11. */
  12. class ExerciseLink extends AbstractLink
  13. {
  14. // INTERNAL VARIABLES
  15. private $course_info = null;
  16. private $exercise_table = null;
  17. private $exercise_data = null;
  18. private $is_hp;
  19. // CONSTRUCTORS
  20. 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. // FUNCTIONS IMPLEMENTING ABSTRACTLINK
  30. /**
  31. * Generate an array of exercises that a teacher hasn't created a link for.
  32. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  33. */
  34. public function get_not_created_links() {
  35. return false;
  36. if (empty($this->course_code)) {
  37. die('Error in get_not_created_links() : course code not set');
  38. }
  39. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  40. $sql = 'SELECT id, title from '.$this->get_exercise_table().' exe
  41. WHERE id NOT IN (
  42. SELECT ref_id FROM '.$tbl_grade_links.'
  43. WHERE type = '.LINK_EXERCISE." AND
  44. course_code = '".$this->get_course_code()."'
  45. ) AND
  46. exe.c_id = ".$this->course_id;
  47. $result = Database::query($sql);
  48. $cats = array();
  49. while ($data=Database::fetch_array($result)) {
  50. $cats[] = array ($data['id'], $data['title']);
  51. }
  52. return $cats;
  53. }
  54. /**
  55. * Generate an array of all exercises available.
  56. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  57. */
  58. public function get_all_links() {
  59. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  60. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  61. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code."/document";
  62. if (empty($this->course_code)) {
  63. die('Error in get_not_created_links() : course code not set');
  64. }
  65. $session_id = api_get_session_id();
  66. if (empty($session_id)) {
  67. $session_condition = api_get_session_condition(0, true);
  68. } else {
  69. $session_condition = api_get_session_condition($session_id, true, true);
  70. }
  71. $sql = 'SELECT id,title from '.$this->get_exercise_table().'
  72. WHERE c_id = '.$this->course_id.' AND active=1 '.$session_condition;
  73. $sql2 = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility, d.id
  74. FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  75. WHERE d.c_id = $this->course_id AND
  76. ip.c_id = $this->course_id AND
  77. d.id = ip.ref AND ip.tool = '".TOOL_DOCUMENT."' AND (d.path LIKE '%htm%')AND (d.path LIKE '%HotPotatoes_files%')
  78. AND d.path LIKE '".Database :: escape_string($uploadPath)."/%/%' AND ip.visibility='1'";
  79. /*
  80. $sql = 'SELECT id,title from '.$this->get_exercise_table().'
  81. WHERE c_id = '.$this->course_id.' AND active=1 AND session_id='.api_get_session_id().'';
  82. */
  83. require_once api_get_path(SYS_CODE_PATH).'exercice/hotpotatoes.lib.php';
  84. if (!$this->is_hp) {
  85. $result = Database::query($sql);
  86. } else {
  87. $result2 = Database::query($sql2);
  88. }
  89. $cats = array();
  90. if (isset($result)) {
  91. if (Database::num_rows($result) > 0) {
  92. while ($data=Database::fetch_array($result)) {
  93. $cats[] = array ($data['id'], $data['title']);
  94. }
  95. }
  96. }
  97. if (isset($result2)) {
  98. if (mysql_numrows($result2) > 0) {
  99. while ($row=Database::fetch_array($result2)) {
  100. /*$path = $data['path'];
  101. $fname = GetQuizName($path,$documentPath);
  102. $cats[] = array ($data['id'], $fname);*/
  103. $attribute['path'][] = $row['path'];
  104. $attribute['visibility'][] = $row['visibility'];
  105. $attribute['comment'][] = $row['comment'];
  106. $attribute['id'] = $row['id'];
  107. }
  108. if (isset($attribute['path']) && is_array($attribute['path'])) {
  109. $hotpotatoes_exist = true;
  110. while (list($key, $path) = each($attribute['path'])) {
  111. $item = '';
  112. $title = GetQuizName($path, $documentPath);
  113. if ($title == '') {
  114. $title = basename($path);
  115. }
  116. $cats[] = array ($attribute['id'], $title.'(HP)');
  117. }
  118. }
  119. }
  120. }
  121. return $cats;
  122. }
  123. /**
  124. * Has anyone done this exercise yet ?
  125. */
  126. public function has_results() {
  127. $tbl_stats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  128. $session_id = api_get_session_id();
  129. $sql = 'SELECT count(exe_id) AS number FROM '.$tbl_stats."
  130. WHERE session_id = $session_id AND
  131. c_id = '".$this->course_id."'".' AND
  132. exe_exo_id = '.(int)$this->get_ref_id();
  133. $result = Database::query($sql);
  134. $number=Database::fetch_row($result);
  135. return ($number[0] != 0);
  136. }
  137. /**
  138. * Get the score of this exercise. Only the first attempts are taken into account.
  139. * @param $stud_id student id (default: all students who have results - then the average is returned)
  140. * @return array (score, max) if student is given
  141. * array (sum of scores, number of scores) otherwise
  142. * or null if no scores available
  143. */
  144. public function calc_score($stud_id = null)
  145. {
  146. $tbl_stats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  147. // The following query should be similar (in conditions)
  148. // to the one used in exercice/exercice.php, look for note-query-exe-results marker
  149. $session_id = api_get_session_id();
  150. $sql = "SELECT * FROM $tbl_stats
  151. WHERE exe_exo_id = ".intval($this->get_ref_id())." AND
  152. orig_lp_id = 0 AND
  153. orig_lp_item_id = 0 AND
  154. status <> 'incomplete' AND
  155. session_id = $session_id";
  156. if (isset($stud_id)) {
  157. $sql .= " AND c_id = '{$this->course_id}' AND
  158. exe_user_id = '$stud_id' ";
  159. }
  160. $sql .= ' ORDER BY exe_id DESC';
  161. $scores = Database::query($sql);
  162. if (isset($stud_id)) {
  163. // for 1 student
  164. if ($data = Database::fetch_array($scores)) {
  165. return array ($data['exe_result'], $data['exe_weighting']);
  166. } else {
  167. return null;
  168. }
  169. } else {
  170. /// all students -> get average
  171. // normal way of getting the info
  172. $students = array(); // user list, needed to make sure we only
  173. // take first attempts into account
  174. $student_count = 0;
  175. $sum = 0;
  176. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  177. if (!in_array($data['exe_user_id'], $students)) {
  178. if ($data['exe_weighting'] != 0) {
  179. $students[] = $data['exe_user_id'];
  180. $student_count++;
  181. $sum += $data['exe_result'] / $data['exe_weighting'];
  182. }
  183. }
  184. }
  185. if ($student_count == 0) {
  186. return null;
  187. } else {
  188. return array ($sum , $student_count);
  189. }
  190. }
  191. }
  192. /**
  193. * Get URL where to go to if the user clicks on the link.
  194. * First we go to exercise_jump.php and then to the result page.
  195. * Check this php file for more info.
  196. */
  197. public function get_link() {
  198. //status student
  199. $user_id = api_get_user_id();
  200. //$course_code = $this->get_course_code();
  201. $status_user = api_get_status_of_user_in_course($user_id, $this->course_id);
  202. $session_id =api_get_session_id();
  203. $url = api_get_path(WEB_PATH).'main/gradebook/exercise_jump.php?session_id='.$session_id.'&cidReq='.$this->get_course_code().'&gradebook=view&exerciseId='.$this->get_ref_id();
  204. if ((!api_is_allowed_to_edit() && $this->calc_score(api_get_user_id()) == null) || $status_user!=1) {
  205. $url .= '&amp;doexercise='.$this->get_ref_id();
  206. }
  207. return $url;
  208. }
  209. /**
  210. * Get name to display: same as exercise title
  211. */
  212. public function get_name() {
  213. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code."/document";
  214. require_once api_get_path(SYS_CODE_PATH).'exercice/hotpotatoes.lib.php';
  215. $data = $this->get_exercise_data();
  216. if ($this->is_hp == 1) {
  217. if (isset($data['path'])) {
  218. $hotpotatoes_exist = true;
  219. $item = '';
  220. $title = GetQuizName($data['path'], $documentPath);
  221. if ($title == '') {
  222. $title = basename($data['path']);
  223. }
  224. return $title;
  225. }
  226. }
  227. return $data['title'];
  228. }
  229. /**
  230. * Get description to display: same as exercise description
  231. */
  232. public function get_description() {
  233. $data = $this->get_exercise_data();
  234. return $data['description'];
  235. }
  236. /**
  237. * Check if this still links to an exercise
  238. */
  239. public function is_valid_link() {
  240. //$sql = 'SELECT count(id) from '.$this->get_exercise_table().' WHERE c_id = '.$this->course_id.' AND id = '.(int)$this->get_ref_id().' AND session_id='.api_get_session_id().'';
  241. $sql = 'SELECT count(id) from '.$this->get_exercise_table().' WHERE c_id = '.$this->course_id.' AND id = '.(int)$this->get_ref_id().' ';
  242. $result = Database::query($sql);
  243. $number=Database::fetch_row($result);
  244. return ($number[0] != 0);
  245. }
  246. public function get_type_name() {
  247. if ($this->is_hp == 1) {
  248. return get_lang('Hopotatoe');
  249. } else {
  250. return get_lang('Quiz');
  251. }
  252. }
  253. public function needs_name_and_description() {
  254. return false;
  255. }
  256. public function needs_max() {
  257. return false;
  258. }
  259. public function needs_results() {
  260. return false;
  261. }
  262. public function is_allowed_to_change_name() {
  263. return false;
  264. }
  265. // INTERNAL FUNCTIONS
  266. /**
  267. * Lazy load function to get the database table of the exercise
  268. */
  269. private function get_exercise_table() {
  270. $this->exercise_table = Database :: get_course_table(TABLE_QUIZ_TEST);
  271. return $this->exercise_table;
  272. }
  273. /**
  274. * Lazy load function to get the database contents of this exercise
  275. */
  276. private function get_exercise_data() {
  277. if ($this->is_hp == 1) {
  278. $tbl_exercise = Database :: get_course_table(TABLE_DOCUMENT);
  279. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  280. } else {
  281. $tbl_exercise = $this->get_exercise_table();
  282. }
  283. if ($tbl_exercise=='') {
  284. return false;
  285. } elseif (!isset($this->exercise_data)) {
  286. if ($this->is_hp == 1) {
  287. $ref_id = intval($this->get_ref_id());
  288. $sql = "SELECT * FROM $tbl_exercise ex, $TBL_ITEM_PROPERTY ip
  289. WHERE ip.ref = ex.id AND ip.c_id = $this->course_id AND ex.c_id = $this->course_id AND ip.tool = '".TOOL_DOCUMENT."' AND (ex.path LIKE '%htm%')AND (ex.path LIKE '%HotPotatoes_files%') AND ip.visibility = 1";
  290. } else {
  291. $sql = 'SELECT * FROM '.$tbl_exercise.'
  292. WHERE c_id = '.$this->course_id.' AND id = '.(int)$this->get_ref_id().' ';
  293. }
  294. $result = Database::query($sql);
  295. $this->exercise_data=Database::fetch_array($result);
  296. }
  297. return $this->exercise_data;
  298. }
  299. public function get_icon_name() {
  300. return 'exercise';
  301. }
  302. }