studentpublicationlink.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 course_code = '".Database::escape_string($this->get_course_code())."'"
  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. //no break
  214. default:
  215. // first attempt
  216. $dql .= ' ORDER BY a.id';
  217. break;
  218. }
  219. $scores = $em->createQuery($dql)->execute($params);
  220. // for 1 student
  221. if (!empty($stud_id)) {
  222. if (!count($scores)) {
  223. return '';
  224. }
  225. $data = $scores[0];
  226. return [
  227. $data->getQualification(),
  228. $assignment->getQualification()
  229. ];
  230. }
  231. $students = array(); // user list, needed to make sure we only
  232. // take first attempts into account
  233. $rescount = 0;
  234. $sum = 0;
  235. $bestResult = 0;
  236. $weight = 0;
  237. $sumResult = 0;
  238. foreach ($scores as $data) {
  239. if (!(array_key_exists($data->getUserId(), $students))) {
  240. if ($assignment->getQualification() != 0) {
  241. $students[$data->getUserId()] = $data->getQualification();
  242. $rescount++;
  243. $sum += $data->getQualification() / $assignment->getQualification();
  244. $sumResult += $data->getQualification();
  245. if ($data->getQualification() > $bestResult) {
  246. $bestResult = $data->getQualification();
  247. }
  248. $weight = $assignment->getQualification();
  249. }
  250. }
  251. }
  252. if ($rescount == 0) {
  253. return null;
  254. }
  255. switch ($type) {
  256. case 'best':
  257. return array($bestResult, $weight);
  258. break;
  259. case 'average':
  260. return array($sumResult / $rescount, $weight);
  261. break;
  262. case 'ranking':
  263. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  264. break;
  265. default:
  266. return array($sum, $rescount);
  267. break;
  268. }
  269. }
  270. /**
  271. * Lazy load function to get the database table of the student publications
  272. */
  273. private function get_studpub_table()
  274. {
  275. return $this->studpub_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  276. }
  277. /**
  278. * Lazy load function to get the database table of the item properties
  279. */
  280. private function get_itemprop_table()
  281. {
  282. return $this->itemprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  283. }
  284. public function needs_name_and_description()
  285. {
  286. return false;
  287. }
  288. public function get_name()
  289. {
  290. $this->get_exercise_data();
  291. $name = isset($this->exercise_data['title']) && !empty($this->exercise_data['title']) ? $this->exercise_data['title'] : get_lang('Untitled');
  292. return $name;
  293. }
  294. public function get_description()
  295. {
  296. $this->get_exercise_data();
  297. return isset($this->exercise_data['description']) ? $this->exercise_data['description'] : null;
  298. }
  299. public function get_test_id()
  300. {
  301. return 'DEBUG:ID';
  302. }
  303. public function get_link()
  304. {
  305. $session_id = api_get_session_id();
  306. $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';
  307. return $url;
  308. }
  309. /**
  310. * @return array
  311. */
  312. private function get_exercise_data()
  313. {
  314. $course_info = api_get_course_info($this->get_course_code());
  315. if (!isset($this->exercise_data)) {
  316. $sql = 'SELECT * FROM '.$this->get_studpub_table()."
  317. WHERE
  318. c_id ='".$course_info['real_id']."' AND
  319. id = '".$this->get_ref_id()."' ";
  320. $query = Database::query($sql);
  321. $this->exercise_data = Database::fetch_array($query);
  322. // Try with iid
  323. if (empty($this->exercise_data)) {
  324. $sql = 'SELECT * FROM '.$this->get_studpub_table()."
  325. WHERE
  326. c_id ='".$course_info['real_id']."' AND
  327. iid = '".$this->get_ref_id()."' ";
  328. $query = Database::query($sql);
  329. $this->exercise_data = Database::fetch_array($query);
  330. }
  331. }
  332. return $this->exercise_data;
  333. }
  334. public function needs_max()
  335. {
  336. return false;
  337. }
  338. public function needs_results()
  339. {
  340. return false;
  341. }
  342. public function is_valid_link()
  343. {
  344. $data = $this->get_exercise_data();
  345. if (empty($data)) {
  346. return '';
  347. }
  348. $id = $data['id'];
  349. $sql = 'SELECT count(id) FROM '.$this->get_studpub_table().'
  350. WHERE
  351. c_id = "'.$this->course_id.'" AND
  352. id = '.$id.'';
  353. $result = Database::query($sql);
  354. $number = Database::fetch_row($result);
  355. return ($number[0] != 0);
  356. }
  357. public function get_icon_name()
  358. {
  359. return 'studentpublication';
  360. }
  361. public function save_linked_data()
  362. {
  363. $data = $this->get_exercise_data();
  364. if (empty($data)) {
  365. return '';
  366. }
  367. $id = $data['id'];
  368. $weight = api_float_val($this->get_weight());
  369. if (!empty($id)) {
  370. //Cleans works
  371. $sql = 'UPDATE '.$this->get_studpub_table().'
  372. SET weight= '.$weight.'
  373. WHERE c_id = '.$this->course_id.' AND id ='.$id;
  374. Database::query($sql);
  375. }
  376. }
  377. /**
  378. * @return string
  379. */
  380. public function delete_linked_data()
  381. {
  382. $data = $this->get_exercise_data();
  383. if (empty($data)) {
  384. return '';
  385. }
  386. if (!empty($id)) {
  387. //Cleans works
  388. $sql = 'UPDATE '.$this->get_studpub_table().'
  389. SET weight = 0
  390. WHERE c_id = '.$this->course_id.' AND id ='.$id;
  391. Database::query($sql);
  392. }
  393. }
  394. }