studentpublicationlink.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Gradebook link to student publication item.
  5. *
  6. * @author Bert Steppé
  7. *
  8. * @package chamilo.gradebook
  9. */
  10. class StudentPublicationLink extends AbstractLink
  11. {
  12. private $studpub_table = null;
  13. private $itemprop_table = null;
  14. /**
  15. * Constructor.
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->set_type(LINK_STUDENTPUBLICATION);
  21. }
  22. /**
  23. * Returns the URL of a document
  24. * This function is loaded when using a gradebook as a tab (gradebook = -1)
  25. * see issue #2705.
  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 all exercises available.
  67. *
  68. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  69. */
  70. public function get_all_links()
  71. {
  72. if (empty($this->course_code)) {
  73. return [];
  74. }
  75. $em = Database::getManager();
  76. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  77. /*
  78. if (empty($session_id)) {
  79. $session_condition = api_get_session_condition(0, true);
  80. } else {
  81. $session_condition = api_get_session_condition($session_id, true, true);
  82. }
  83. $sql = "SELECT id, url, title FROM $tbl_grade_links
  84. WHERE c_id = {$this->course_id} AND filetype='folder' AND active = 1 $session_condition ";*/
  85. //Only show works from the session
  86. //AND has_properties != ''
  87. $links = $em
  88. ->getRepository('ChamiloCourseBundle:CStudentPublication')
  89. ->findBy([
  90. 'cId' => $this->course_id,
  91. 'active' => true,
  92. 'filetype' => 'folder',
  93. 'session' => $session,
  94. ]);
  95. foreach ($links as $data) {
  96. $work_name = $data->getTitle();
  97. if (empty($work_name)) {
  98. $work_name = basename($data->getUrl());
  99. }
  100. $cats[] = [$data->getId(), $work_name];
  101. }
  102. $cats = isset($cats) ? $cats : [];
  103. return $cats;
  104. }
  105. /**
  106. * Has anyone done this exercise yet ?
  107. */
  108. public function has_results()
  109. {
  110. $data = $this->get_exercise_data();
  111. if (empty($data)) {
  112. return '';
  113. }
  114. $id = $data['id'];
  115. $em = Database::getManager();
  116. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  117. $results = $em
  118. ->getRepository('ChamiloCourseBundle:CStudentPublication')
  119. ->findBy([
  120. 'cId' => $this->course_id,
  121. 'parentId' => $id,
  122. 'session' => $session,
  123. ]);
  124. return count($results) != 0;
  125. }
  126. /**
  127. * @param null $stud_id
  128. *
  129. * @return array
  130. */
  131. public function calc_score($stud_id = null, $type = null)
  132. {
  133. $stud_id = (int) $stud_id;
  134. $em = Database::getManager();
  135. $data = $this->get_exercise_data();
  136. if (empty($data)) {
  137. return [];
  138. }
  139. $id = $data['id'];
  140. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  141. $assignment = $em
  142. ->getRepository('ChamiloCourseBundle:CStudentPublication')
  143. ->findOneBy([
  144. 'cId' => $this->course_id,
  145. 'id' => $id,
  146. 'session' => $session,
  147. ])
  148. ;
  149. $parentId = !$assignment ? 0 : $assignment->getId();
  150. if (empty($session)) {
  151. $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a
  152. WHERE
  153. a.cId = :course AND
  154. a.active = :active AND
  155. a.parentId = :parent AND
  156. a.session is null AND
  157. a.qualificatorId <> 0
  158. ';
  159. $params = [
  160. 'course' => $this->course_id,
  161. 'parent' => $parentId,
  162. 'active' => true,
  163. ];
  164. } else {
  165. $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a
  166. WHERE
  167. a.cId = :course AND
  168. a.active = :active AND
  169. a.parentId = :parent AND
  170. a.session = :session AND
  171. a.qualificatorId <> 0
  172. ';
  173. $params = [
  174. 'course' => $this->course_id,
  175. 'parent' => $parentId,
  176. 'session' => $session,
  177. 'active' => true,
  178. ];
  179. }
  180. if (!empty($stud_id)) {
  181. $dql .= ' AND a.userId = :student ';
  182. $params['student'] = $stud_id;
  183. }
  184. $order = api_get_setting('student_publication_to_take_in_gradebook');
  185. switch ($order) {
  186. case 'last':
  187. // latest attempt
  188. $dql .= ' ORDER BY a.sentDate DESC';
  189. break;
  190. case 'first':
  191. default:
  192. // first attempt
  193. $dql .= ' ORDER BY a.id';
  194. break;
  195. }
  196. $scores = $em->createQuery($dql)->execute($params);
  197. // for 1 student
  198. if (!empty($stud_id)) {
  199. if (!count($scores)) {
  200. return [null, null];
  201. }
  202. $data = $scores[0];
  203. return [
  204. $data->getQualification(),
  205. $assignment->getQualification(),
  206. ];
  207. }
  208. $students = []; // user list, needed to make sure we only
  209. // take first attempts into account
  210. $rescount = 0;
  211. $sum = 0;
  212. $bestResult = 0;
  213. $weight = 0;
  214. $sumResult = 0;
  215. foreach ($scores as $data) {
  216. if (!(array_key_exists($data->getUserId(), $students))) {
  217. if ($assignment->getQualification() != 0) {
  218. $students[$data->getUserId()] = $data->getQualification();
  219. $rescount++;
  220. $sum += $data->getQualification() / $assignment->getQualification();
  221. $sumResult += $data->getQualification();
  222. if ($data->getQualification() > $bestResult) {
  223. $bestResult = $data->getQualification();
  224. }
  225. $weight = $assignment->getQualification();
  226. }
  227. }
  228. }
  229. if ($rescount == 0) {
  230. return [null, null];
  231. }
  232. switch ($type) {
  233. case 'best':
  234. return [$bestResult, $weight];
  235. break;
  236. case 'average':
  237. return [$sumResult / $rescount, $weight];
  238. break;
  239. case 'ranking':
  240. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  241. break;
  242. default:
  243. return [$sum, $rescount];
  244. break;
  245. }
  246. }
  247. public function needs_name_and_description()
  248. {
  249. return false;
  250. }
  251. public function get_name()
  252. {
  253. $this->get_exercise_data();
  254. $name = isset($this->exercise_data['title']) && !empty($this->exercise_data['title']) ? $this->exercise_data['title'] : get_lang('Untitled');
  255. return $name;
  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_link()
  263. {
  264. $session_id = api_get_session_id();
  265. $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';
  266. return $url;
  267. }
  268. public function needs_max()
  269. {
  270. return false;
  271. }
  272. public function needs_results()
  273. {
  274. return false;
  275. }
  276. public function is_valid_link()
  277. {
  278. $data = $this->get_exercise_data();
  279. if (empty($data)) {
  280. return '';
  281. }
  282. $id = $data['id'];
  283. $sql = 'SELECT count(id) FROM '.$this->get_studpub_table().'
  284. WHERE
  285. c_id = "'.$this->course_id.'" AND
  286. id = '.$id.'';
  287. $result = Database::query($sql);
  288. $number = Database::fetch_row($result);
  289. return $number[0] != 0;
  290. }
  291. public function get_icon_name()
  292. {
  293. return 'studentpublication';
  294. }
  295. public function save_linked_data()
  296. {
  297. $data = $this->get_exercise_data();
  298. if (empty($data)) {
  299. return '';
  300. }
  301. $id = $data['id'];
  302. $weight = api_float_val($this->get_weight());
  303. if (!empty($id)) {
  304. //Cleans works
  305. $sql = 'UPDATE '.$this->get_studpub_table().'
  306. SET weight= '.$weight.'
  307. WHERE c_id = '.$this->course_id.' AND id ='.$id;
  308. Database::query($sql);
  309. }
  310. }
  311. /**
  312. * @return string
  313. */
  314. public function delete_linked_data()
  315. {
  316. $data = $this->get_exercise_data();
  317. if (empty($data)) {
  318. return '';
  319. }
  320. if (!empty($id)) {
  321. //Cleans works
  322. $sql = 'UPDATE '.$this->get_studpub_table().'
  323. SET weight = 0
  324. WHERE c_id = '.$this->course_id.' AND id ='.$id;
  325. Database::query($sql);
  326. }
  327. }
  328. /**
  329. * Lazy load function to get the database table of the student publications.
  330. */
  331. private function get_studpub_table()
  332. {
  333. return $this->studpub_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  334. }
  335. /**
  336. * Lazy load function to get the database table of the item properties.
  337. */
  338. private function get_itemprop_table()
  339. {
  340. return $this->itemprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  341. }
  342. /**
  343. * @return array
  344. */
  345. private function get_exercise_data()
  346. {
  347. $course_info = api_get_course_info($this->get_course_code());
  348. if (!isset($this->exercise_data)) {
  349. $sql = 'SELECT * FROM '.$this->get_studpub_table()."
  350. WHERE
  351. c_id ='".$course_info['real_id']."' AND
  352. id = '".$this->get_ref_id()."' ";
  353. $query = Database::query($sql);
  354. $this->exercise_data = Database::fetch_array($query);
  355. // Try with iid
  356. if (empty($this->exercise_data)) {
  357. $sql = 'SELECT * FROM '.$this->get_studpub_table()."
  358. WHERE
  359. c_id ='".$course_info['real_id']."' AND
  360. iid = '".$this->get_ref_id()."' ";
  361. $query = Database::query($sql);
  362. $this->exercise_data = Database::fetch_array($query);
  363. }
  364. }
  365. return $this->exercise_data;
  366. }
  367. }