studentpublicationlink.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. $em = Database::getManager();
  91. $session = $em->find('ChamiloCoreBundle:Session', 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. $links = $em
  103. ->getRepository('ChamiloCourseBundle:CStudentPublication')
  104. ->findBy([
  105. 'cId' => $this->course_id,
  106. 'active' => true,
  107. 'filetype' => 'folder',
  108. 'session' => $session
  109. ]);
  110. foreach ($links as $data) {
  111. $work_name = $data->getTitle();
  112. if (empty($work_name)) {
  113. $work_name = basename($data->getUrl());
  114. }
  115. $cats[] = array ($data->getId(), $work_name);
  116. }
  117. $cats=isset($cats) ? $cats : array();
  118. return $cats;
  119. }
  120. /**
  121. * Has anyone done this exercise yet ?
  122. */
  123. public function has_results()
  124. {
  125. $em = Database::getManager();
  126. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  127. $results = $em
  128. ->getRepository('ChamiloCourseBundle:CStudentPublication')
  129. ->findBy([
  130. 'cId' => $this->course_id,
  131. 'parentId' => intval($this->get_ref_id()),
  132. 'session' => $session
  133. ]);
  134. return count($results) != 0;
  135. }
  136. /**
  137. * @param null $stud_id
  138. * @return array|null
  139. */
  140. public function calc_score($stud_id = null, $type = null)
  141. {
  142. $stud_id = intval($stud_id);
  143. $em = Database::getManager();
  144. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  145. $assignment = $em
  146. ->getRepository('ChamiloCourseBundle:CStudentPublication')
  147. ->findOneBy([
  148. 'cId' => $this->course_id,
  149. 'id' => intval($this->get_ref_id()),
  150. 'session' => $session
  151. ]);
  152. $parentId = !$assignment ? 0 : $assignment->getId();
  153. $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a
  154. WHERE
  155. a.cId = :course AND
  156. a.active = :active AND
  157. a.parentId = :parent AND
  158. a.session = :session AND
  159. a.qualificatorId <> 0
  160. ';
  161. $params = [
  162. 'course' => $this->course_id,
  163. 'parent' => $parentId,
  164. 'session' => $session,
  165. 'active' => true
  166. ];
  167. if (!empty($stud_id)) {
  168. $dql .= ' AND a.userId = :student ';
  169. $params['student'] = $stud_id;
  170. }
  171. $order = api_get_setting('student_publication_to_take_in_gradebook');
  172. switch ($order) {
  173. case 'last':
  174. // latest attempt
  175. $dql .= ' ORDER BY a.sentDate DESC';
  176. break;
  177. case 'first':
  178. default:
  179. // first attempt
  180. $dql .= ' ORDER BY a.id';
  181. break;
  182. }
  183. $scores = $em->createQuery($dql)->execute($params);
  184. // for 1 student
  185. if (!empty($stud_id)) {
  186. if (!count($scores)) {
  187. return '';
  188. }
  189. $data = $scores[0];
  190. return [
  191. $data->getQualification(),
  192. $assignment->getQualification()
  193. ];
  194. }
  195. $students = array(); // user list, needed to make sure we only
  196. // take first attempts into account
  197. $rescount = 0;
  198. $sum = 0;
  199. $bestResult = 0;
  200. $weight = 0;
  201. $sumResult = 0;
  202. foreach ($scores as $data) {
  203. if (!(array_key_exists($data->getUserId(), $students))) {
  204. if ($assignment->getQualification() != 0) {
  205. $students[$data->getUserId()] = $data->getQualification();
  206. $rescount++;
  207. $sum += $data->getQualification() / $assignment->getQualification();
  208. $sumResult += $data->getQualification();
  209. if ($data->getQualification() > $bestResult) {
  210. $bestResult = $data->getQualification();
  211. }
  212. $weight = $assignment->getQualification();
  213. }
  214. }
  215. }
  216. if ($rescount == 0) {
  217. return null;
  218. }
  219. switch ($type) {
  220. case 'best':
  221. return array($bestResult, $weight);
  222. break;
  223. case 'average':
  224. return array($sumResult/$rescount, $weight);
  225. break;
  226. case 'ranking':
  227. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  228. break;
  229. default:
  230. return array($sum, $rescount);
  231. break;
  232. }
  233. }
  234. /**
  235. * Lazy load function to get the database table of the student publications
  236. */
  237. private function get_studpub_table()
  238. {
  239. return $this->studpub_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  240. }
  241. /**
  242. * Lazy load function to get the database table of the item properties
  243. */
  244. private function get_itemprop_table()
  245. {
  246. return $this->itemprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  247. }
  248. public function needs_name_and_description()
  249. {
  250. return false;
  251. }
  252. public function get_name()
  253. {
  254. $this->get_exercise_data();
  255. return (isset($this->exercise_data['title']) && !empty($this->exercise_data['title'])) ? $this->exercise_data['title'] : get_lang('Untitled');
  256. }
  257. public function get_description()
  258. {
  259. $this->get_exercise_data();
  260. return isset($this->exercise_data['description']) ? $this->exercise_data['description'] : null;
  261. }
  262. public function get_test_id()
  263. {
  264. return 'DEBUG:ID';
  265. }
  266. public function get_link()
  267. {
  268. $session_id = api_get_session_id();
  269. $url = api_get_path(WEB_PATH).'main/work/work.php?'.api_get_cidreq_params($this->get_course_code(), $session_id).'&id='.$this->exercise_data['id'].'&gradebook=view';
  270. return $url;
  271. }
  272. private function get_exercise_data()
  273. {
  274. $tbl_name = $this->get_studpub_table();
  275. $course_info = api_get_course_info($this->get_course_code());
  276. if ($tbl_name=='') {
  277. return false;
  278. } elseif (!isset($this->exercise_data)) {
  279. $sql = 'SELECT * FROM '.$this->get_studpub_table()."
  280. WHERE
  281. c_id ='".$course_info['real_id']."' AND
  282. id = '".intval($this->get_ref_id())."' ";
  283. $query = Database::query($sql);
  284. $this->exercise_data = Database::fetch_array($query);
  285. }
  286. return $this->exercise_data;
  287. }
  288. public function needs_max()
  289. {
  290. return false;
  291. }
  292. public function needs_results()
  293. {
  294. return false;
  295. }
  296. public function is_valid_link()
  297. {
  298. $sql = 'SELECT count(id) FROM '.$this->get_studpub_table().'
  299. WHERE c_id = "'.$this->course_id.'" AND id = '.intval($this->get_ref_id()).'';
  300. $result = Database::query($sql);
  301. $number = Database::fetch_row($result);
  302. return ($number[0] != 0);
  303. }
  304. public function get_icon_name()
  305. {
  306. return 'studentpublication';
  307. }
  308. public function save_linked_data()
  309. {
  310. $weight = (float)$this->get_weight();
  311. $ref_id = $this->get_ref_id();
  312. if (!empty($ref_id)) {
  313. //Cleans works
  314. $sql = 'UPDATE '.$this->get_studpub_table().' SET weight= '.$weight.'
  315. WHERE c_id = '.$this->course_id.' AND id ='.$ref_id;
  316. Database::query($sql);
  317. }
  318. }
  319. public function delete_linked_data()
  320. {
  321. $ref_id = $this->get_ref_id();
  322. if (!empty($ref_id)) {
  323. //Cleans works
  324. $sql = 'UPDATE '.$this->get_studpub_table().' SET weight=0
  325. WHERE c_id = '.$this->course_id.' AND id ='.$ref_id;
  326. Database::query($sql);
  327. }
  328. }
  329. }