forumthreadlink.class.php 8.0 KB

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