studentpublicationlink.class.php 12 KB

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