studentpublicationlink.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Gradebook link to student publication item
  5. * @author Bert Steppé
  6. * @package chamilo.gradebook
  7. */
  8. class StudentPublicationLink extends AbstractLink
  9. {
  10. private $studpub_table = null;
  11. private $itemprop_table = null;
  12. /**
  13. * Constructor
  14. */
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->set_type(LINK_STUDENTPUBLICATION);
  19. }
  20. /**
  21. *
  22. * Returns the URL of a document
  23. * This function is loaded when using a gradebook as a tab (gradebook = -1)
  24. * see issue #2705
  25. *
  26. */
  27. public function get_view_url($stud_id)
  28. {
  29. // find a file uploaded by the given student,
  30. // with the same title as the evaluation name
  31. $eval = $this->get_evaluation();
  32. $stud_id = intval($stud_id);
  33. $sql = 'SELECT pub.url
  34. FROM '.$this->get_itemprop_table().' prop, '.$this->get_studpub_table().' pub'
  35. ." WHERE
  36. prop.c_id = ".$this->course_id." AND
  37. pub.c_id = ".$this->course_id." AND
  38. prop.tool = 'work'"
  39. .' AND prop.insert_user_id = '.$stud_id
  40. .' AND prop.ref = pub.id'
  41. ." AND pub.title = '".Database::escape_string($eval->get_name())."' AND pub.session_id=".api_get_session_id()."";
  42. $result = Database::query($sql);
  43. if ($fileurl = Database::fetch_row($result)) {
  44. return null;
  45. } else {
  46. return null;
  47. }
  48. }
  49. public function get_type_name()
  50. {
  51. return get_lang('Works');
  52. }
  53. public function is_allowed_to_change_name()
  54. {
  55. return false;
  56. }
  57. /**
  58. * Generate an array of exercises that a teacher hasn't created a link for.
  59. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  60. */
  61. public function get_not_created_links()
  62. {
  63. return false;
  64. if (empty($this->course_code)) {
  65. die('Error in get_not_created_links() : course code not set');
  66. }
  67. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  68. $sql = 'SELECT id, url from '.$this->get_studpub_table()
  69. .' pup WHERE c_id = '.$this->course_id.' AND has_properties != '."''".' AND id NOT IN'
  70. .' (SELECT ref_id FROM '.$tbl_grade_links
  71. .' WHERE type = '.LINK_STUDENTPUBLICATION
  72. ." AND course_code = '".Database::escape_string($this->get_course_code())."'"
  73. .') AND pub.session_id='.api_get_session_id().'';
  74. $result = Database::query($sql);
  75. $cats=array();
  76. while ($data=Database::fetch_array($result)) {
  77. $cats[] = array ($data['id'], $data['url']);
  78. }
  79. return $cats;
  80. }
  81. /**
  82. * Generate an array of all exercises available.
  83. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  84. */
  85. public function get_all_links()
  86. {
  87. if (empty($this->course_code)) {
  88. die('Error in get_not_created_links() : course code not set');
  89. }
  90. $tbl_grade_links = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  91. $session_id = api_get_session_id();
  92. /*
  93. if (empty($session_id)) {
  94. $session_condition = api_get_session_condition(0, true);
  95. } else {
  96. $session_condition = api_get_session_condition($session_id, true, true);
  97. }
  98. $sql = "SELECT id, url, title FROM $tbl_grade_links
  99. WHERE c_id = {$this->course_id} AND filetype='folder' AND active = 1 $session_condition ";*/
  100. //Only show works from the session
  101. //AND has_properties != ''
  102. $sql = "SELECT id, url, title FROM $tbl_grade_links
  103. WHERE
  104. c_id = {$this->course_id} AND
  105. active = 1 AND
  106. filetype='folder' AND
  107. session_id = ".api_get_session_id()."";
  108. $result = Database::query($sql);
  109. while ($data = Database::fetch_array($result)) {
  110. $work_name = $data['title'];
  111. if (empty($work_name)) {
  112. $work_name = basename($data['url']);
  113. }
  114. $cats[] = array ($data['id'], $work_name);
  115. }
  116. $cats=isset($cats) ? $cats : array();
  117. return $cats;
  118. }
  119. /**
  120. * Has anyone done this exercise yet ?
  121. */
  122. public function has_results()
  123. {
  124. $tbl_grade_links = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  125. $sql = 'SELECT count(*) AS number FROM '.$tbl_grade_links."
  126. WHERE c_id = {$this->course_id} AND
  127. parent_id = '".intval($this->get_ref_id())."' AND
  128. session_id =".api_get_session_id()."";
  129. $result = Database::query($sql);
  130. $number = Database::fetch_row($result);
  131. return ($number[0] != 0);
  132. }
  133. /**
  134. * @param null $stud_id
  135. * @return array|null
  136. */
  137. public function calc_score($stud_id = null, $type = null)
  138. {
  139. $stud_id = intval($stud_id);
  140. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  141. $sql = 'SELECT * FROM '.$table."
  142. WHERE
  143. c_id = {$this->course_id} AND
  144. id = '".intval($this->get_ref_id())."' AND
  145. session_id = ".api_get_session_id()."
  146. "
  147. ;
  148. $query = Database::query($sql);
  149. $assignment = Database::fetch_array($query);
  150. if (count($assignment) == 0) {
  151. $parentId = '0';
  152. } else {
  153. $parentId = $assignment['id'];
  154. }
  155. $sql = 'SELECT * FROM '.$table.'
  156. WHERE
  157. c_id = '.$this->course_id.' AND
  158. active = 1 AND
  159. parent_id = "'.$parentId.'" AND
  160. session_id = '.api_get_session_id() .' AND
  161. qualificator_id <> 0
  162. ';
  163. if (!empty($stud_id)) {
  164. $sql .= " AND user_id = $stud_id ";
  165. }
  166. $order = api_get_setting('student_publication_to_take_in_gradebook');
  167. switch ($order) {
  168. case 'last':
  169. // latest attempt
  170. $sql .= ' ORDER BY sent_date DESC';
  171. break;
  172. case 'first':
  173. default:
  174. // first attempt
  175. $sql .= ' ORDER BY id';
  176. break;
  177. }
  178. $scores = Database::query($sql);
  179. // for 1 student
  180. if (!empty($stud_id)) {
  181. if ($data = Database::fetch_array($scores)) {
  182. return array(
  183. $data['qualification'],
  184. $assignment['qualification']
  185. );
  186. } else {
  187. return '';
  188. }
  189. } else {
  190. $students = array(); // user list, needed to make sure we only
  191. // take first attempts into account
  192. $rescount = 0;
  193. $sum = 0;
  194. $bestResult = 0;
  195. $weight = 0;
  196. $sumResult = 0;
  197. $myResult = 0;
  198. while ($data = Database::fetch_array($scores)) {
  199. if (!(array_key_exists($data['user_id'], $students))) {
  200. if ($assignment['qualification'] != 0) {
  201. $students[$data['user_id']] = $data['qualification'];
  202. $rescount++;
  203. $sum += $data['qualification'] / $assignment['qualification'];
  204. $sumResult += $data['qualification'];
  205. if ($data['qualification'] > $bestResult) {
  206. $bestResult = $data['qualification'];
  207. }
  208. $weight = $assignment['qualification'];
  209. }
  210. }
  211. }
  212. if ($rescount == 0) {
  213. return null;
  214. } else {
  215. switch ($type) {
  216. case 'best':
  217. return array($bestResult, $weight);
  218. break;
  219. case 'average':
  220. return array($sumResult/$rescount, $weight);
  221. break;
  222. case 'ranking':
  223. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  224. break;
  225. default:
  226. return array($sum, $rescount);
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. /**
  233. * Lazy load function to get the database table of the student publications
  234. */
  235. private function get_studpub_table()
  236. {
  237. return $this->studpub_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  238. }
  239. /**
  240. * Lazy load function to get the database table of the item properties
  241. */
  242. private function get_itemprop_table()
  243. {
  244. return $this->itemprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  245. }
  246. public function needs_name_and_description()
  247. {
  248. return false;
  249. }
  250. public function get_name()
  251. {
  252. $this->get_exercise_data();
  253. return (isset($this->exercise_data['title']) && !empty($this->exercise_data['title'])) ? $this->exercise_data['title'] : get_lang('Untitled');
  254. }
  255. public function get_description()
  256. {
  257. $this->get_exercise_data();
  258. return isset($this->exercise_data['description']) ? $this->exercise_data['description'] : null;
  259. }
  260. public function get_test_id()
  261. {
  262. return 'DEBUG:ID';
  263. }
  264. public function get_link()
  265. {
  266. $session_id = api_get_session_id();
  267. $url = api_get_path(WEB_PATH).'main/work/work.php?session_id='.$session_id.'&cidReq='.$this->get_course_code().'&id='.$this->exercise_data['id'].'&gradebook=view';
  268. return $url;
  269. }
  270. private function get_exercise_data()
  271. {
  272. $tbl_name = $this->get_studpub_table();
  273. $course_info = api_get_course_info($this->get_course_code());
  274. if ($tbl_name=='') {
  275. return false;
  276. } elseif (!isset($this->exercise_data)) {
  277. $sql = 'SELECT * FROM '.$this->get_studpub_table()."
  278. WHERE
  279. c_id ='".$course_info['real_id']."' AND
  280. id = '".intval($this->get_ref_id())."' ";
  281. $query = Database::query($sql);
  282. $this->exercise_data = Database::fetch_array($query);
  283. }
  284. return $this->exercise_data;
  285. }
  286. public function needs_max()
  287. {
  288. return false;
  289. }
  290. public function needs_results()
  291. {
  292. return false;
  293. }
  294. public function is_valid_link()
  295. {
  296. $sql = 'SELECT count(id) FROM '.$this->get_studpub_table().'
  297. WHERE c_id = "'.$this->course_id.'" AND id = '.intval($this->get_ref_id()).'';
  298. $result = Database::query($sql);
  299. $number = Database::fetch_row($result);
  300. return ($number[0] != 0);
  301. }
  302. public function get_icon_name()
  303. {
  304. return 'studentpublication';
  305. }
  306. public function save_linked_data()
  307. {
  308. $weight = (float)$this->get_weight();
  309. $ref_id = $this->get_ref_id();
  310. if (!empty($ref_id)) {
  311. //Cleans works
  312. $sql = 'UPDATE '.$this->get_studpub_table().' SET weight= '.$weight.'
  313. WHERE c_id = '.$this->course_id.' AND id ='.$ref_id;
  314. Database::query($sql);
  315. }
  316. }
  317. public function delete_linked_data()
  318. {
  319. $ref_id = $this->get_ref_id();
  320. if (!empty($ref_id)) {
  321. //Cleans works
  322. $sql = 'UPDATE '.$this->get_studpub_table().' SET weight=0
  323. WHERE c_id = '.$this->course_id.' AND id ='.$ref_id;
  324. Database::query($sql);
  325. }
  326. }
  327. }