forumthreadlink.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 ForumThreadLink extends AbstractLink
  13. {
  14. // INTERNAL VARIABLES
  15. private $forum_thread_table = null;
  16. private $itemprop_table = null;
  17. function __construct() {
  18. parent::__construct();
  19. $this->set_type(LINK_FORUM_THREAD);
  20. }
  21. public function get_type_name() {
  22. return get_lang('ForumThreads');
  23. }
  24. public function is_allowed_to_change_name() {
  25. return false;
  26. }
  27. // FUNCTIONS IMPLEMENTING ABSTRACTLINK
  28. /**
  29. * Generate an array of exercises that a teacher hasn't created a link for.
  30. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  31. */
  32. public function get_not_created_links() {
  33. if (empty($this->course_code)) {
  34. die('Error in get_not_created_links() : course code not set');
  35. }
  36. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  37. $sql = 'SELECT thread_id,thread_title,thread_title_qualify from '.$this->get_forum_thread_table()
  38. .' forum_thread WHERE thread_id NOT IN'
  39. .' (SELECT ref_id FROM '.$tbl_grade_links
  40. .' WHERE type = '.LINK_FORUM_THREAD
  41. ." AND course_code = '".Database::escape_string($this->get_course_code())."'"
  42. .') AND forum_thread.session_id='.api_get_session_id().'';
  43. $result = Database::query($sql);
  44. $cats=array();
  45. while ($data=Database::fetch_array($result)) {
  46. if ( isset($data['thread_title_qualify']) and $data['thread_title_qualify']!=""){
  47. $cats[] = array ($data['thread_id'], $data['thread_title_qualify']);
  48. } else {
  49. $cats[] = array ($data['thread_id'], $data['thread_title']);
  50. }
  51. }
  52. return $cats;
  53. }
  54. /**
  55. * Generate an array of all exercises available.
  56. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  57. */
  58. public function get_all_links() {
  59. if (empty($this->course_code)) {
  60. die('Error in get_not_created_links() : course code not set');
  61. }
  62. $tbl_grade_links = Database :: get_course_table(TABLE_FORUM_THREAD);
  63. $tbl_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  64. $session_id = api_get_session_id();
  65. if ($session_id) {
  66. $session_condition = 'tl.session_id='.api_get_session_id();
  67. } else {
  68. $session_condition = '(tl.session_id = 0 OR tl.session_id IS NULL)';
  69. }
  70. $sql = 'SELECT tl.thread_id, tl.thread_title, tl.thread_title_qualify
  71. FROM '.$tbl_grade_links.' tl ,'.$tbl_item_property.' ip
  72. WHERE tl.c_id = '.$this->course_id.' AND
  73. ip.c_id = '.$this->course_id.' AND
  74. tl.thread_id = ip.ref AND
  75. ip.tool = "forum_thread" AND
  76. ip.visibility<>2 AND '.$session_condition.' GROUP BY ip.ref ';
  77. $result = Database::query($sql);
  78. while ($data=Database::fetch_array($result)) {
  79. if ( isset($data['thread_title_qualify']) and $data['thread_title_qualify']!=""){
  80. $cats[] = array ($data['thread_id'], $data['thread_title_qualify']);
  81. } else {
  82. $cats[] = array ($data['thread_id'], $data['thread_title']);
  83. }
  84. }
  85. $my_cats=isset($cats)?$cats:null;
  86. return $my_cats;
  87. }
  88. /**
  89. * Has anyone done this exercise yet ?
  90. */
  91. public function has_results() {
  92. $tbl_grade_links = Database :: get_course_table(TABLE_FORUM_POST);
  93. $sql = 'SELECT count(*) AS number FROM '.$tbl_grade_links."
  94. WHERE c_id = ".$this->course_id." AND thread_id = '".$this->get_ref_id()."'";
  95. $result = Database::query($sql);
  96. $number=Database::fetch_row($result);
  97. return ($number[0] != 0);
  98. }
  99. public function calc_score($stud_id = null) {
  100. $thread_qualify = Database :: get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  101. $sql = 'SELECT thread_qualify_max FROM '.Database :: get_course_table(TABLE_FORUM_THREAD)."
  102. WHERE c_id = ".$this->course_id." AND thread_id = '".$this->get_ref_id()."'";
  103. $query = Database::query($sql);
  104. $assignment = Database::fetch_array($query);
  105. $sql = "SELECT * FROM $thread_qualify WHERE c_id = ".$this->course_id." AND thread_id = ".$this->get_ref_id();
  106. if (isset($stud_id)) {
  107. $sql .= ' AND user_id = '."'".intval($stud_id)."'";
  108. }
  109. // order by id, that way the student's first attempt is accessed first
  110. $sql .= ' ORDER BY qualify_time DESC';
  111. $scores = Database::query($sql);
  112. // for 1 student
  113. if (isset($stud_id)) {
  114. if ($data = Database::fetch_array($scores)) {
  115. return array ($data['qualify'], $assignment['thread_qualify_max']);
  116. } else {
  117. //We sent the 0/thread_qualify_max instead of null for correct calculations
  118. //return null;
  119. return array (0, $assignment['thread_qualify_max']);
  120. }
  121. } else {
  122. // all students -> get average
  123. $students=array(); // user list, needed to make sure we only
  124. // take first attempts into account
  125. $rescount = 0;
  126. $sum = 0;
  127. while ($data=Database::fetch_array($scores)) {
  128. if (!(array_key_exists($data['user_id'],$students))) {
  129. if ($assignment['thread_qualify_max'] != 0) {
  130. $students[$data['user_id']] = $data['qualify'];
  131. $rescount++;
  132. $sum += ($data['qualify'] / $assignment['thread_qualify_max']);
  133. }
  134. }
  135. }
  136. if ($rescount == 0) {
  137. return null;
  138. } else {
  139. return array ($sum , $rescount);
  140. }
  141. }
  142. }
  143. // INTERNAL FUNCTIONS
  144. /**
  145. * Lazy load function to get the database table of the student publications
  146. */
  147. private function get_forum_thread_table () {
  148. return $this->forum_thread_table = Database :: get_course_table(TABLE_FORUM_THREAD);
  149. }
  150. /**
  151. * Lazy load function to get the database table of the item properties
  152. */
  153. private function get_itemprop_table () {
  154. $this->itemprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  155. return $this->itemprop_table;
  156. }
  157. public function needs_name_and_description() {
  158. return false;
  159. }
  160. public function needs_max() {
  161. return false;
  162. }
  163. public function needs_results() {
  164. return false;
  165. }
  166. public function get_name() {
  167. $this->get_exercise_data();
  168. $thread_title=isset($this->exercise_data['thread_title']) ? $this->exercise_data['thread_title'] : '';
  169. $thread_title_qualify=isset($this->exercise_data['thread_title_qualify']) ? $this->exercise_data['thread_title_qualify'] : '';
  170. if ( isset($thread_title_qualify) && $thread_title_qualify!="") {
  171. return $this->exercise_data['thread_title_qualify'];
  172. } else {
  173. return $thread_title;
  174. }
  175. }
  176. public function get_description() {
  177. return '';//$this->exercise_data['description'];
  178. }
  179. /**
  180. * Check if this still links to an exercise
  181. */
  182. public function is_valid_link() {
  183. $sql = 'SELECT count(id) from '.$this->get_forum_thread_table().'
  184. WHERE c_id = '.$this->course_id.' AND thread_id = '.$this->get_ref_id().' AND session_id='.api_get_session_id().'';
  185. $result = Database::query($sql);
  186. $number = Database::fetch_row($result);
  187. return ($number[0] != 0);
  188. }
  189. public function get_test_id() {
  190. return 'DEBUG:ID';
  191. }
  192. public function get_link() {
  193. //it was extracts the forum id
  194. $sql = 'SELECT * FROM '.$this->get_forum_thread_table()."
  195. WHERE c_id = '.$this->course_id.' AND thread_id = '".$this->get_ref_id()."' AND session_id = ".api_get_session_id()."";
  196. $result = Database::query($sql);
  197. $row = Database::fetch_array($result,'ASSOC');
  198. $forum_id=$row['forum_id'];
  199. $url = api_get_path(WEB_PATH).'main/forum/viewthread.php?cidReq='.$this->get_course_code().'&thread='.$this->get_ref_id().'&gradebook=view&forum='.$forum_id;
  200. return $url;
  201. }
  202. private function get_exercise_data() {
  203. $session_id = api_get_session_id();
  204. if ($session_id) {
  205. $session_condition = 'session_id='.api_get_session_id();
  206. } else {
  207. $session_condition = '(session_id = 0 OR session_id IS NULL)';
  208. }
  209. if (!isset($this->exercise_data)) {
  210. $sql = 'SELECT * FROM '.$this->get_forum_thread_table().'
  211. WHERE c_id = '.$this->course_id.' AND thread_id = '.$this->get_ref_id().' AND '.$session_condition;
  212. $query = Database::query($sql);
  213. $this->exercise_data = Database::fetch_array($query);
  214. }
  215. return $this->exercise_data;
  216. }
  217. public function get_icon_name() {
  218. return 'forum';
  219. }
  220. function save_linked_data() {
  221. $weight = (float)$this->get_weight();
  222. $ref_id = $this->get_ref_id();
  223. if (!empty($ref_id)) {
  224. $sql = 'UPDATE '.$this->get_forum_thread_table().' SET thread_weight='.$weight.'
  225. WHERE c_id = '.$this->course_id.' AND thread_id= '.$ref_id;
  226. Database::query($sql);
  227. }
  228. }
  229. function delete_linked_data() {
  230. $ref_id = $this->get_ref_id();
  231. if (!empty($ref_id)) {
  232. //Cleans forum
  233. $sql = 'UPDATE '.$this->get_forum_thread_table().' SET thread_qualify_max=0,thread_weight=0,thread_title_qualify=""
  234. WHERE c_id = '.$this->course_id.' AND thread_id= '.$ref_id;
  235. Database::query($sql);
  236. }
  237. }
  238. }