studentpublicationlink.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. /**
  9. * Class
  10. * @package chamilo.gradebook
  11. */
  12. class StudentPublicationLink extends AbstractLink
  13. {
  14. // INTERNAL VARIABLES
  15. private $studpub_table = null;
  16. private $itemprop_table = null;
  17. // CONSTRUCTORS
  18. public function __construct() {
  19. parent::__construct();
  20. $this->set_type(LINK_STUDENTPUBLICATION);
  21. }
  22. /**
  23. *
  24. * Returns the URL of a document
  25. * This funcion is loaded when using a gradebook as a tab (gradebook = -1), see issue #2705
  26. *
  27. */
  28. public function get_view_url ($stud_id) {
  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 FROM '.$this->get_itemprop_table().' prop, '.$this->get_studpub_table().' pub'
  34. ." WHERE
  35. prop.c_id = ".$this->course_id." AND
  36. pub.c_id = ".$this->course_id." AND
  37. prop.tool = 'work'"
  38. .' AND prop.insert_user_id = '.$stud_id
  39. .' AND prop.ref = pub.id'
  40. ." AND pub.title = '".Database::escape_string($eval->get_name())."' AND pub.session_id=".api_get_session_id()."";
  41. $result = Database::query($sql);
  42. if ($fileurl = Database::fetch_row($result)) {
  43. $course_info = Database :: get_course_info($this->get_course_code());
  44. //$url = api_get_path(WEB_PATH).'main/gradebook/open_document.php?file='.$course_info['directory'].'/'.$fileurl[0];
  45. //return $url;
  46. return null;
  47. } else {
  48. return null;
  49. }
  50. }
  51. public function get_type_name() {
  52. return get_lang('Works');
  53. }
  54. public function is_allowed_to_change_name() {
  55. return false;
  56. }
  57. // FUNCTIONS IMPLEMENTING ABSTRACTLINK
  58. /**
  59. * Generate an array of exercises that a teacher hasn't created a link for.
  60. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  61. */
  62. public function get_not_created_links() {
  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. if (empty($this->course_code)) {
  87. die('Error in get_not_created_links() : course code not set');
  88. }
  89. $tbl_grade_links = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  90. $session_id = api_get_session_id();
  91. /*
  92. if (empty($session_id)) {
  93. $session_condition = api_get_session_condition(0, true);
  94. } else {
  95. $session_condition = api_get_session_condition($session_id, true, true);
  96. }
  97. $sql = "SELECT id, url, title FROM $tbl_grade_links
  98. WHERE c_id = {$this->course_id} AND filetype='folder' AND active = 1 $session_condition ";*/
  99. //Only show works from the session
  100. //AND has_properties != ''
  101. $sql = "SELECT id, url, title FROM $tbl_grade_links
  102. WHERE c_id = {$this->course_id} AND active = 1 AND filetype='folder' AND session_id = ".api_get_session_id()."";
  103. $result = Database::query($sql);
  104. while ($data = Database::fetch_array($result)) {
  105. $work_name = $data['title'];
  106. if (empty($work_name)) {
  107. $work_name = basename($data['url']);
  108. }
  109. $cats[] = array ($data['id'], $work_name);
  110. }
  111. $cats=isset($cats) ? $cats : array();
  112. return $cats;
  113. }
  114. /**
  115. * Has anyone done this exercise yet ?
  116. */
  117. public function has_results() {
  118. $tbl_grade_links = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  119. $sql = 'SELECT count(*) AS number FROM '.$tbl_grade_links."
  120. WHERE c_id = {$this->course_id} AND
  121. parent_id = '".intval($this->get_ref_id())."' AND
  122. session_id =".api_get_session_id()."";
  123. $result = Database::query($sql);
  124. $number = Database::fetch_row($result);
  125. return ($number[0] != 0);
  126. }
  127. public function calc_score($stud_id = null) {
  128. $stud_id = intval($stud_id);
  129. $tbl_stats = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  130. $sql = 'SELECT * FROM '.$tbl_stats."
  131. WHERE c_id = {$this->course_id} AND
  132. id = '".intval($this->get_ref_id())."' AND
  133. session_id = ".api_get_session_id()."";
  134. $query = Database::query($sql);
  135. $assignment = Database::fetch_array($query);
  136. if (count($assignment)==0) {
  137. $v_assigment_id ='0';
  138. } else {
  139. $v_assigment_id = $assignment['id'];
  140. }
  141. $sql = 'SELECT * FROM '.$tbl_stats.'
  142. WHERE c_id = '.$this->course_id.' AND active = 1 AND parent_id ="'.$v_assigment_id.'" AND session_id='.api_get_session_id().'';
  143. if (!empty($stud_id)) {
  144. $sql .= " AND user_id = $stud_id ";
  145. }
  146. // order by id, that way the student's first attempt is accessed first
  147. $sql .= ' ORDER BY id';
  148. $scores = Database::query($sql);
  149. // for 1 student
  150. if (!empty($stud_id)) {
  151. if ($data = Database::fetch_array($scores)) {
  152. return array($data['qualification'], $assignment['qualification']);
  153. } else {
  154. return '';
  155. }
  156. } else {
  157. $students = array(); // user list, needed to make sure we only
  158. // take first attempts into account
  159. $rescount = 0;
  160. $sum = 0;
  161. while ($data = Database::fetch_array($scores)) {
  162. if (!(array_key_exists($data['user_id'], $students))) {
  163. if ($assignment['qualification'] != 0) {
  164. $students[$data['user_id']] = $data['qualification'];
  165. $rescount++;
  166. $sum += $data['qualification'] / $assignment['qualification'];
  167. }
  168. }
  169. }
  170. if ($rescount == 0) {
  171. return null;
  172. } else {
  173. return array ($sum , $rescount);
  174. }
  175. }
  176. }
  177. // INTERNAL FUNCTIONS
  178. /**
  179. * Lazy load function to get the database table of the student publications
  180. */
  181. private function get_studpub_table() {
  182. return $this->studpub_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  183. }
  184. /**
  185. * Lazy load function to get the database table of the item properties
  186. */
  187. private function get_itemprop_table () {
  188. return $this->itemprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  189. }
  190. public function needs_name_and_description() {
  191. return false;
  192. }
  193. public function get_name() {
  194. $this->get_exercise_data();
  195. return (isset($this->exercise_data['title']) && !empty($this->exercise_data['title'])) ? $this->exercise_data['title'] : get_lang('Untitled');
  196. }
  197. public function get_description() {
  198. $this->get_exercise_data();
  199. return isset($this->exercise_data['description']) ? $this->exercise_data['description'] : null;
  200. }
  201. public function get_test_id() {
  202. return 'DEBUG:ID';
  203. }
  204. public function get_link() {
  205. $session_id = api_get_session_id();
  206. $url = api_get_path(WEB_PATH).'main/work/work.php?session_id='.$session_id.'&cidReq='.$this->get_course_code().'&id='.$this->exercise_data['id'].'&gradebook=view';
  207. return $url;
  208. }
  209. private function get_exercise_data() {
  210. $tbl_name = $this->get_studpub_table();
  211. $course_info = Database :: get_course_info($this->get_course_code());
  212. if ($tbl_name=='') {
  213. return false;
  214. } elseif (!isset($this->exercise_data)) {
  215. $sql = 'SELECT * FROM '.$this->get_studpub_table()." WHERE c_id ='".$course_info['real_id']."' AND id = '".intval($this->get_ref_id())."' ";
  216. $query = Database::query($sql);
  217. $this->exercise_data = Database::fetch_array($query);
  218. }
  219. return $this->exercise_data;
  220. }
  221. public function needs_max() {
  222. return false;
  223. }
  224. public function needs_results() {
  225. return false;
  226. }
  227. public function is_valid_link() {
  228. $sql = 'SELECT count(id) FROM '.$this->get_studpub_table().'
  229. WHERE c_id = "'.$this->course_id.'" AND id = '.intval($this->get_ref_id()).'';
  230. $result = Database::query($sql);
  231. $number = Database::fetch_row($result);
  232. return ($number[0] != 0);
  233. }
  234. public function get_icon_name() {
  235. return 'studentpublication';
  236. }
  237. function save_linked_data() {
  238. $weight = (float)$this->get_weight();
  239. $ref_id = $this->get_ref_id();
  240. if (!empty($ref_id)) {
  241. //Cleans works
  242. $sql = 'UPDATE '.$this->get_studpub_table().' SET weight= '.$weight.'
  243. WHERE c_id = '.$this->course_id.' AND id ='.$ref_id;
  244. Database::query($sql);
  245. }
  246. }
  247. function delete_linked_data() {
  248. $ref_id = $this->get_ref_id();
  249. if (!empty($ref_id)) {
  250. //Cleans works
  251. $sql = 'UPDATE '.$this->get_studpub_table().' SET weight=0
  252. WHERE c_id = '.$this->course_id.' AND id ='.$ref_id;
  253. Database::query($sql);
  254. }
  255. }
  256. }