studentpublicationlink.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos Latinoamerica SAC
  6. Copyright (c) 2006 Dokeos SPRL
  7. Copyright (c) 2006 Ghent University (UGent)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. * Gradebook link to student publication item
  22. * @author Bert Steppé
  23. * @package dokeos.gradebook
  24. */
  25. class StudentPublicationLink extends AbstractLink
  26. {
  27. // INTERNAL VARIABLES
  28. private $studpub_table = null;
  29. private $itemprop_table = null;
  30. // CONSTRUCTORS
  31. function StudentPublicationLink() {
  32. $this->set_type(LINK_STUDENTPUBLICATION);
  33. }
  34. // FUNCTIONS IMPLEMENTING ABSTRACTLINK
  35. public function get_view_url ($stud_id) {
  36. // find a file uploaded by the given student,
  37. // with the same title as the evaluation name
  38. $eval = $this->get_evaluation();
  39. $sql = 'SELECT pub.url'
  40. .' FROM '.$this->get_itemprop_table().' prop, '
  41. .$this->get_studpub_table().' pub'
  42. ." WHERE prop.tool = 'work'"
  43. .' AND prop.insert_user_id = '.$stud_id
  44. .' AND prop.ref = pub.id'
  45. ." AND pub.title = '".Database::escape_string($eval->get_name())."'";
  46. $result = api_sql_query($sql, __FILE__, __LINE__);
  47. if ($fileurl = Database::fetch_row($result)) {
  48. $course_info = Database :: get_course_info($this->get_course_code());
  49. $url = api_get_path(WEB_PATH)
  50. .'main/gradebook/open_document.php?file='
  51. .$course_info['directory']
  52. .'/'
  53. .$fileurl[0];
  54. return $url;
  55. } else {
  56. return null;
  57. }
  58. }
  59. public function get_type_name() {
  60. return get_lang('DokeosStudentPublications');
  61. }
  62. public function is_allowed_to_change_name() {
  63. return false;
  64. }
  65. // FUNCTIONS IMPLEMENTING ABSTRACTLINK
  66. /**
  67. * Generate an array of exercises that a teacher hasn't created a link for.
  68. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  69. */
  70. public function get_not_created_links() {
  71. if (empty($this->course_code)) {
  72. die('Error in get_not_created_links() : course code not set');
  73. }
  74. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  75. $sql = 'SELECT id,url from '.$this->get_studpub_table()
  76. .' WHERE has_properties != '."''".' AND id NOT IN'
  77. .' (SELECT ref_id FROM '.$tbl_grade_links
  78. .' WHERE type = '.LINK_STUDENTPUBLICATION
  79. ." AND course_code = '".$this->get_course_code()."'"
  80. .')';
  81. $result = api_sql_query($sql, __FILE__, __LINE__);
  82. $cats=array();
  83. while ($data=Database::fetch_array($result)) {
  84. $cats[] = array ($data['id'], $data['url']);
  85. }
  86. return $cats;
  87. }
  88. /**
  89. * Generate an array of all exercises available.
  90. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  91. */
  92. public function get_all_links() {
  93. if (empty($this->course_code)) {
  94. die('Error in get_not_created_links() : course code not set');
  95. }
  96. $course_info = api_get_course_info($this->course_code);
  97. $tbl_grade_links = Database :: get_course_table(TABLE_STUDENT_PUBLICATION,$course_info['dbName']);
  98. $sql = 'SELECT id,url FROM '.$tbl_grade_links.' WHERE has_properties != '."'' AND filetype='folder'";
  99. $result = api_sql_query($sql, __FILE__, __LINE__);
  100. while ($data=Database::fetch_array($result)) {
  101. $cats[] = array ($data['id'], $data['url']);
  102. }
  103. return $cats;
  104. }
  105. /**
  106. * Has anyone done this exercise yet ?
  107. */
  108. public function has_results() {
  109. $course_info = api_get_course_info($this->course_code);
  110. $tbl_grade_links = Database :: get_course_table(TABLE_STUDENT_PUBLICATION,$course_info['dbName']);
  111. $sql = 'SELECT count(*) AS number FROM '.$tbl_grade_links." WHERE parent_id = '".$this->get_ref_id()."'";
  112. $result = api_sql_query($sql, __FILE__, __LINE__);
  113. $number=Database::fetch_row($result);
  114. return ($number[0] != 0);
  115. }
  116. public function calc_score($stud_id = null) {
  117. $course_info = Database :: get_course_info($this->get_course_code());
  118. $database_name = (empty($course_info['db_name']))?$course_info['dbName']:$course_info['db_name'];
  119. $tbl_stats = Database :: get_course_table(TABLE_STUDENT_PUBLICATION, $database_name);
  120. $sql = 'SELECT * FROM '.$tbl_stats." WHERE id = '".$this->get_ref_id()."'";
  121. $query = api_sql_query($sql,__FILE__,__LINE__);
  122. $assignment = Database::fetch_array($query);
  123. if(Database::num_rows($assignment)==0) {
  124. $v_assigment_id ='0';
  125. } else {
  126. $v_assigment_id = $assignment['id'];
  127. }
  128. $sql = 'SELECT * FROM '.$tbl_stats.' WHERE parent_id = '.$v_assigment_id;
  129. if (isset($stud_id)){
  130. $sql1='SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER)." WHERE user_id = '".$stud_id."'";
  131. $query = api_sql_query($sql1,__FILE__,__LINE__);
  132. $student = Database::fetch_array($query);
  133. $sql .= ' AND author = '."'".$student['firstname'].' '.$student['lastname']."'";
  134. }
  135. // order by id, that way the student's first attempt is accessed first
  136. $sql .= ' ORDER BY id';
  137. $scores = api_sql_query($sql, __FILE__, __LINE__);
  138. // for 1 student
  139. if (isset($stud_id)) {
  140. if ($data=Database::fetch_array($scores)) {
  141. return array ($data['qualification'], $assignment['qualification']);
  142. } else {
  143. return null;
  144. }
  145. } else {
  146. $students=array(); // user list, needed to make sure we only
  147. // take first attempts into account
  148. $rescount = 0;
  149. $sum = 0;
  150. while ($data=Database::fetch_array($scores)) {
  151. if (!(array_key_exists($data['author'],$students))) {
  152. if ($assignment['qualification'] != 0) {
  153. $students[$data['author']] = $data['qualification'];
  154. $rescount++;
  155. $sum += ($data['qualification'] / $assignment['qualification']);
  156. }
  157. }
  158. }
  159. if ($rescount == 0) {
  160. return null;
  161. } else {
  162. return array ($sum , $rescount);
  163. }
  164. }
  165. }
  166. // INTERNAL FUNCTIONS
  167. /**
  168. * Lazy load function to get the database table of the student publications
  169. */
  170. private function get_studpub_table () {
  171. if (!isset($this->studpub_table)) {
  172. $course_info = Database :: get_course_info($this->get_course_code());
  173. $database_name = $course_info['db_name'];
  174. $this->studpub_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION, $database_name);
  175. }
  176. return $this->studpub_table;
  177. }
  178. /**
  179. * Lazy load function to get the database table of the item properties
  180. */
  181. private function get_itemprop_table () {
  182. if (!isset($this->itemprop_table)) {
  183. $course_info = Database :: get_course_info($this->get_course_code());
  184. $database_name = $course_info['db_name'];
  185. $this->itemprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY, $database_name);
  186. }
  187. return $this->itemprop_table;
  188. }
  189. public function needs_name_and_description() {
  190. return false;
  191. }
  192. public function get_name() {
  193. $this->get_exercise_data();
  194. return $this->exercise_data['url'];
  195. }
  196. public function get_description() {
  197. $this->get_exercise_data();
  198. return $this->exercise_data['description'];
  199. }
  200. public function get_test_id() {
  201. return 'DEBUG:ID';
  202. }
  203. public function get_link() {
  204. $url = api_get_path(WEB_PATH)
  205. .'main/work/work.php?cidReq='.$this->get_course_code();
  206. if (!api_is_allowed_to_create_course()
  207. && $this->calc_score(api_get_user_id()) == null) {
  208. $url .= '&curdirpath=/'.$this->get_ref_id();
  209. }
  210. return $url;
  211. }
  212. private function get_exercise_data() {
  213. if (!isset($this->exercise_data)) {
  214. $sql = 'SELECT * FROM '.$this->get_studpub_table()." WHERE id = '".$this->get_ref_id()."'";
  215. $query = api_sql_query($sql,__FILE__,__LINE__);
  216. $this->exercise_data = Database::fetch_array($query);
  217. }
  218. return $this->exercise_data;
  219. }
  220. public function needs_max() {
  221. return false;
  222. }
  223. public function needs_results() {
  224. return false;
  225. }
  226. public function is_valid_link() {
  227. $sql = 'SELECT count(id) from '.$this->get_studpub_table()
  228. .' WHERE id = '.$this->get_ref_id();
  229. $result = api_sql_query($sql, __FILE__, __LINE__);
  230. $number=Database::fetch_row($result);
  231. return ($number[0] != 0);
  232. }
  233. }