studentpublicationlink.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. return null;
  30. // find a file uploaded by the given student,
  31. // with the same title as the evaluation name
  32. $eval = $this->get_evaluation();
  33. $stud_id = intval($stud_id);
  34. $itemProperty = $this->get_itemprop_table();
  35. $workTable = $this->get_studpub_table();
  36. $courseId = $this->course_id;
  37. $sql = "SELECT pub.url
  38. FROM $itemProperty prop INNER JOIN $workTable pub
  39. ON (prop.c_id = pub.c_id AND prop.ref = pub.id)
  40. WHERE
  41. prop.c_id = ".$courseId." AND
  42. pub.c_id = ".$courseId." AND
  43. prop.tool = 'work' AND
  44. prop.insert_user_id = $stud_id AND
  45. pub.title = '".Database::escape_string($eval->get_name())."' AND
  46. pub.session_id=".api_get_session_id();
  47. $result = Database::query($sql);
  48. if ($fileurl = Database::fetch_row($result)) {
  49. return null;
  50. } else {
  51. return null;
  52. }
  53. }
  54. /**
  55. * @return string
  56. */
  57. public function get_type_name()
  58. {
  59. return get_lang('Works');
  60. }
  61. public function is_allowed_to_change_name()
  62. {
  63. return false;
  64. }
  65. /**
  66. * Generate an array of exercises that a teacher hasn't created a link for.
  67. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  68. */
  69. public function get_not_created_links()
  70. {
  71. return false;
  72. if (empty($this->course_code)) {
  73. die('Error in get_not_created_links() : course code not set');
  74. }
  75. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  76. $sql = 'SELECT id, url from '.$this->get_studpub_table()
  77. .' pup WHERE c_id = '.$this->course_id.' AND has_properties != '."''".' AND id NOT IN'
  78. .' (SELECT ref_id FROM '.$tbl_grade_links
  79. .' WHERE type = '.LINK_STUDENTPUBLICATION
  80. ." AND c_id = '".intval($this->course_id)."'"
  81. .') AND pub.session_id='.api_get_session_id().'';
  82. $result = Database::query($sql);
  83. $cats=array();
  84. while ($data = Database::fetch_array($result)) {
  85. $cats[] = array($data['id'], $data['url']);
  86. }
  87. return $cats;
  88. }
  89. /**
  90. * Generate an array of all exercises available.
  91. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  92. */
  93. public function get_all_links()
  94. {
  95. if (empty($this->course_code)) {
  96. die('Error in get_not_created_links() : course code not set');
  97. }
  98. $em = Database::getManager();
  99. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  100. /*
  101. if (empty($session_id)) {
  102. $session_condition = api_get_session_condition(0, true);
  103. } else {
  104. $session_condition = api_get_session_condition($session_id, true, true);
  105. }
  106. $sql = "SELECT id, url, title FROM $tbl_grade_links
  107. WHERE c_id = {$this->course_id} AND filetype='folder' AND active = 1 $session_condition ";*/
  108. //Only show works from the session
  109. //AND has_properties != ''
  110. $links = $em
  111. ->getRepository('ChamiloCourseBundle:CStudentPublication')
  112. ->findBy([
  113. 'cId' => $this->course_id,
  114. 'active' => true,
  115. 'filetype' => 'folder',
  116. 'session' => $session
  117. ]);
  118. foreach ($links as $data) {
  119. $work_name = $data->getTitle();
  120. if (empty($work_name)) {
  121. $work_name = basename($data->getUrl());
  122. }
  123. $cats[] = array ($data->getId(), $work_name);
  124. }
  125. $cats=isset($cats) ? $cats : array();
  126. return $cats;
  127. }
  128. /**
  129. * Has anyone done this exercise yet ?
  130. */
  131. public function has_results()
  132. {
  133. $data = $this->get_exercise_data();
  134. if (empty($data)) {
  135. return '';
  136. }
  137. $id = $data['id'];
  138. $em = Database::getManager();
  139. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  140. $results = $em
  141. ->getRepository('ChamiloCourseBundle:CStudentPublication')
  142. ->findBy([
  143. 'cId' => $this->course_id,
  144. 'parentId' => $id,
  145. 'session' => $session
  146. ]);
  147. return count($results) != 0;
  148. }
  149. /**
  150. * @param null $stud_id
  151. * @return array|null
  152. */
  153. public function calc_score($stud_id = null, $type = null)
  154. {
  155. $stud_id = (int) $stud_id;
  156. $em = Database::getManager();
  157. $data = $this->get_exercise_data();
  158. if (empty($data)) {
  159. return '';
  160. }
  161. $id = $data['id'];
  162. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  163. $assignment = $em
  164. ->getRepository('ChamiloCourseBundle:CStudentPublication')
  165. ->findOneBy([
  166. 'cId' => $this->course_id,
  167. 'id' => $id,
  168. 'session' => $session
  169. ])
  170. ;
  171. $parentId = !$assignment ? 0 : $assignment->getId();
  172. if (empty($session)) {
  173. $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a
  174. WHERE
  175. a.cId = :course AND
  176. a.active = :active AND
  177. a.parentId = :parent AND
  178. a.session is null AND
  179. a.qualificatorId <> 0
  180. ';
  181. $params = [
  182. 'course' => $this->course_id,
  183. 'parent' => $parentId,
  184. 'active' => true
  185. ];
  186. } else {
  187. $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a
  188. WHERE
  189. a.cId = :course AND
  190. a.active = :active AND
  191. a.parentId = :parent AND
  192. a.session = :session AND
  193. a.qualificatorId <> 0
  194. ';
  195. $params = [
  196. 'course' => $this->course_id,
  197. 'parent' => $parentId,
  198. 'session' => $session,
  199. 'active' => true,
  200. ];
  201. }
  202. if (!empty($stud_id)) {
  203. $dql .= ' AND a.userId = :student ';
  204. $params['student'] = $stud_id;
  205. }
  206. $order = api_get_setting('student_publication_to_take_in_gradebook');
  207. switch ($order) {
  208. case 'last':
  209. // latest attempt
  210. $dql .= ' ORDER BY a.sentDate DESC';
  211. break;
  212. case 'first':
  213. default:
  214. // first attempt
  215. $dql .= ' ORDER BY a.id';
  216. break;
  217. }
  218. $scores = $em->createQuery($dql)->execute($params);
  219. // for 1 student
  220. if (!empty($stud_id)) {
  221. if (!count($scores)) {
  222. return '';
  223. }
  224. $data = $scores[0];
  225. return [
  226. $data->getQualification(),
  227. $assignment->getQualification()
  228. ];
  229. }
  230. $students = array(); // user list, needed to make sure we only
  231. // take first attempts into account
  232. $rescount = 0;
  233. $sum = 0;
  234. $bestResult = 0;
  235. $weight = 0;
  236. $sumResult = 0;
  237. foreach ($scores as $data) {
  238. if (!(array_key_exists($data->getUserId(), $students))) {
  239. if ($assignment->getQualification() != 0) {
  240. $students[$data->getUserId()] = $data->getQualification();
  241. $rescount++;
  242. $sum += $data->getQualification() / $assignment->getQualification();
  243. $sumResult += $data->getQualification();
  244. if ($data->getQualification() > $bestResult) {
  245. $bestResult = $data->getQualification();
  246. }
  247. $weight = $assignment->getQualification();
  248. }
  249. }
  250. }
  251. if ($rescount == 0) {
  252. return null;
  253. }
  254. switch ($type) {
  255. case 'best':
  256. return array($bestResult, $weight);
  257. break;
  258. case 'average':
  259. return array($sumResult/$rescount, $weight);
  260. break;
  261. case 'ranking':
  262. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  263. break;
  264. default:
  265. return array($sum, $rescount);
  266. break;
  267. }
  268. }
  269. /**
  270. * Lazy load function to get the database table of the student publications
  271. */
  272. private function get_studpub_table()
  273. {
  274. return $this->studpub_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  275. }
  276. /**
  277. * Lazy load function to get the database table of the item properties
  278. */
  279. private function get_itemprop_table()
  280. {
  281. return $this->itemprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  282. }
  283. public function needs_name_and_description()
  284. {
  285. return false;
  286. }
  287. public function get_name()
  288. {
  289. $this->get_exercise_data();
  290. $name = isset($this->exercise_data['title']) && !empty($this->exercise_data['title']) ? $this->exercise_data['title'] : get_lang('Untitled');
  291. return $name;
  292. }
  293. public function get_description()
  294. {
  295. $this->get_exercise_data();
  296. return isset($this->exercise_data['description']) ? $this->exercise_data['description'] : null;
  297. }
  298. public function get_test_id()
  299. {
  300. return 'DEBUG:ID';
  301. }
  302. public function get_link()
  303. {
  304. $session_id = api_get_session_id();
  305. $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';
  306. return $url;
  307. }
  308. /**
  309. * @return array
  310. */
  311. private function get_exercise_data()
  312. {
  313. $course_info = api_get_course_info($this->get_course_code());
  314. if (!isset($this->exercise_data)) {
  315. $sql = 'SELECT * FROM '.$this->get_studpub_table()."
  316. WHERE
  317. c_id ='".$course_info['real_id']."' AND
  318. id = '".$this->get_ref_id()."' ";
  319. $query = Database::query($sql);
  320. $this->exercise_data = Database::fetch_array($query);
  321. // Try with iid
  322. if (empty($this->exercise_data)) {
  323. $sql = 'SELECT * FROM '.$this->get_studpub_table()."
  324. WHERE
  325. c_id ='".$course_info['real_id']."' AND
  326. iid = '".$this->get_ref_id()."' ";
  327. $query = Database::query($sql);
  328. $this->exercise_data = Database::fetch_array($query);
  329. }
  330. }
  331. return $this->exercise_data;
  332. }
  333. public function needs_max()
  334. {
  335. return false;
  336. }
  337. public function needs_results()
  338. {
  339. return false;
  340. }
  341. public function is_valid_link()
  342. {
  343. $data = $this->get_exercise_data();
  344. if (empty($data)) {
  345. return '';
  346. }
  347. $id = $data['id'];
  348. $sql = 'SELECT count(id) FROM '.$this->get_studpub_table().'
  349. WHERE
  350. c_id = "'.$this->course_id.'" AND
  351. id = '.$id.'';
  352. $result = Database::query($sql);
  353. $number = Database::fetch_row($result);
  354. return ($number[0] != 0);
  355. }
  356. public function get_icon_name()
  357. {
  358. return 'studentpublication';
  359. }
  360. public function save_linked_data()
  361. {
  362. $data = $this->get_exercise_data();
  363. if (empty($data)) {
  364. return '';
  365. }
  366. $id = $data['id'];
  367. $weight = (float) $this->get_weight();
  368. if (!empty($id)) {
  369. //Cleans works
  370. $sql = 'UPDATE '.$this->get_studpub_table().'
  371. SET weight= '.$weight.'
  372. WHERE c_id = '.$this->course_id.' AND id ='.$id;
  373. Database::query($sql);
  374. }
  375. }
  376. public function delete_linked_data()
  377. {
  378. $data = $this->get_exercise_data();
  379. if (empty($data)) {
  380. return '';
  381. }
  382. if (!empty($id)) {
  383. //Cleans works
  384. $sql = 'UPDATE '.$this->get_studpub_table().'
  385. SET weight=0
  386. WHERE c_id = '.$this->course_id.' AND id ='.$id;
  387. Database::query($sql);
  388. }
  389. }
  390. }