forumthreadlink.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ForumThreadLink
  5. *
  6. * @author Bert Steppé
  7. *
  8. * @package chamilo.gradebook
  9. */
  10. class ForumThreadLink extends AbstractLink
  11. {
  12. private $forum_thread_table = null;
  13. private $itemprop_table = null;
  14. /**
  15. * Constructor
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->set_type(LINK_FORUM_THREAD);
  21. }
  22. /**
  23. * @return string
  24. */
  25. public function get_type_name()
  26. {
  27. return get_lang('ForumThreads');
  28. }
  29. /**
  30. * @return bool
  31. */
  32. public function is_allowed_to_change_name()
  33. {
  34. return false;
  35. }
  36. /**
  37. * Generate an array of exercises that a teacher hasn't created a link for.
  38. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  39. */
  40. public function get_not_created_links()
  41. {
  42. if (empty($this->course_code)) {
  43. die('Error in get_not_created_links() : course code not set');
  44. }
  45. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  46. $sql = 'SELECT thread_id,thread_title,thread_title_qualify FROM '.$this->get_forum_thread_table()
  47. .' forum_thread WHERE thread_id NOT IN'
  48. .' (SELECT ref_id FROM '.$tbl_grade_links
  49. .' WHERE type = '.LINK_FORUM_THREAD
  50. ." AND c_id = ".intval($this->course_id)
  51. .') AND forum_thread.session_id='.api_get_session_id().'';
  52. $result = Database::query($sql);
  53. $cats = array();
  54. while ($data = Database::fetch_array($result)) {
  55. if (isset($data['thread_title_qualify']) && $data['thread_title_qualify'] != "") {
  56. $cats[] = array(
  57. $data['thread_id'],
  58. $data['thread_title_qualify'],
  59. );
  60. } else {
  61. $cats[] = array($data['thread_id'], $data['thread_title']);
  62. }
  63. }
  64. return $cats;
  65. }
  66. /**
  67. * Generate an array of all exercises available.
  68. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  69. */
  70. public function get_all_links()
  71. {
  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_course_table(TABLE_FORUM_THREAD);
  76. $tbl_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  77. $session_id = api_get_session_id();
  78. if ($session_id) {
  79. $session_condition = 'tl.session_id='.api_get_session_id();
  80. } else {
  81. $session_condition = '(tl.session_id = 0 OR tl.session_id IS NULL)';
  82. }
  83. $sql = 'SELECT tl.thread_id, tl.thread_title, tl.thread_title_qualify
  84. FROM '.$tbl_grade_links.' tl INNER JOIN '.$tbl_item_property.' ip
  85. ON (tl.thread_id = ip.ref AND tl.c_id = ip.c_id )
  86. WHERE
  87. tl.c_id = '.$this->course_id.' AND
  88. ip.c_id = '.$this->course_id.' AND
  89. ip.tool = "forum_thread" AND
  90. ip.visibility<>2 AND
  91. '.$session_condition.'
  92. ';
  93. $result = Database::query($sql);
  94. while ($data = Database::fetch_array($result)) {
  95. if ( isset($data['thread_title_qualify']) and $data['thread_title_qualify']!=""){
  96. $cats[] = array ($data['thread_id'], $data['thread_title_qualify']);
  97. } else {
  98. $cats[] = array ($data['thread_id'], $data['thread_title']);
  99. }
  100. }
  101. $my_cats = isset($cats) ? $cats : null;
  102. return $my_cats;
  103. }
  104. /**
  105. * Has anyone done this exercise yet ?
  106. * @return boolean
  107. */
  108. public function has_results()
  109. {
  110. $table = Database :: get_course_table(TABLE_FORUM_POST);
  111. $sql = 'SELECT count(*) AS number FROM '.$table."
  112. WHERE
  113. c_id = ".$this->course_id." AND
  114. thread_id = '".$this->get_ref_id()."'";
  115. $result = Database::query($sql);
  116. $number = Database::fetch_row($result);
  117. return $number[0] != 0;
  118. }
  119. /**
  120. * @param int $stud_id
  121. * @param string $type
  122. *
  123. * @return array|null
  124. */
  125. public function calc_score($stud_id = null, $type = null)
  126. {
  127. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  128. $threadInfo = get_thread_information($this->get_ref_id());
  129. $thread_qualify = Database :: get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  130. $sql = 'SELECT thread_qualify_max
  131. FROM '.Database :: get_course_table(TABLE_FORUM_THREAD)."
  132. WHERE c_id = ".$this->course_id." AND thread_id = '".$this->get_ref_id()."'";
  133. $query = Database::query($sql);
  134. $assignment = Database::fetch_array($query);
  135. $sql = "SELECT * FROM $thread_qualify
  136. WHERE c_id = ".$this->course_id." AND thread_id = ".$this->get_ref_id();
  137. if (isset($stud_id)) {
  138. $sql .= ' AND user_id = '.intval($stud_id);
  139. }
  140. // order by id, that way the student's first attempt is accessed first
  141. $sql .= ' ORDER BY qualify_time DESC';
  142. $scores = Database::query($sql);
  143. // for 1 student
  144. if (isset($stud_id)) {
  145. if ($threadInfo['thread_peer_qualify'] == 0) {
  146. // Classic way of calculate score
  147. if ($data = Database::fetch_array($scores)) {
  148. return array(
  149. $data['qualify'],
  150. $assignment['thread_qualify_max']
  151. );
  152. } else {
  153. // We sent the 0/thread_qualify_max instead of null for correct calculations
  154. return array(0, $assignment['thread_qualify_max']);
  155. }
  156. } else {
  157. // Take average
  158. $score = 0;
  159. $counter = 0;
  160. if (Database::num_rows($scores)) {
  161. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  162. $score += $data['qualify'];
  163. $counter++;
  164. }
  165. }
  166. // If no result
  167. if (empty($counter) || $counter <= 2) {
  168. return array(0, $assignment['thread_qualify_max']);
  169. }
  170. return [$score/$counter, $assignment['thread_qualify_max']];
  171. }
  172. } else {
  173. // All students -> get average
  174. $students = array(); // user list, needed to make sure we only
  175. // take first attempts into account
  176. $counter = 0;
  177. $sum = 0;
  178. $bestResult = 0;
  179. $weight = 0;
  180. $sumResult = 0;
  181. while ($data = Database::fetch_array($scores)) {
  182. if (!(array_key_exists($data['user_id'], $students))) {
  183. if ($assignment['thread_qualify_max'] != 0) {
  184. $students[$data['user_id']] = $data['qualify'];
  185. $counter++;
  186. $sum += $data['qualify'] / $assignment['thread_qualify_max'];
  187. $sumResult += $data['qualify'];
  188. if ($data['qualify'] > $bestResult) {
  189. $bestResult = $data['qualify'];
  190. }
  191. $weight = $assignment['thread_qualify_max'];
  192. }
  193. }
  194. }
  195. if ($counter == 0) {
  196. return null;
  197. } else {
  198. switch ($type) {
  199. case 'best':
  200. return array($bestResult, $weight);
  201. break;
  202. case 'average':
  203. return array($sumResult/$counter, $weight);
  204. break;
  205. case 'ranking':
  206. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  207. break;
  208. default:
  209. return array($sum, $counter);
  210. break;
  211. }
  212. }
  213. }
  214. }
  215. /**
  216. * Lazy load function to get the database table of the student publications
  217. */
  218. private function get_forum_thread_table()
  219. {
  220. return $this->forum_thread_table = Database :: get_course_table(TABLE_FORUM_THREAD);
  221. }
  222. public function needs_name_and_description()
  223. {
  224. return false;
  225. }
  226. public function needs_max()
  227. {
  228. return false;
  229. }
  230. public function needs_results()
  231. {
  232. return false;
  233. }
  234. /**
  235. * @return string
  236. */
  237. public function get_name()
  238. {
  239. $this->get_exercise_data();
  240. $thread_title=isset($this->exercise_data['thread_title']) ? $this->exercise_data['thread_title'] : '';
  241. $thread_title_qualify=isset($this->exercise_data['thread_title_qualify']) ? $this->exercise_data['thread_title_qualify'] : '';
  242. if ( isset($thread_title_qualify) && $thread_title_qualify!="") {
  243. return $this->exercise_data['thread_title_qualify'];
  244. } else {
  245. return $thread_title;
  246. }
  247. }
  248. /**
  249. * @return string
  250. */
  251. public function get_description()
  252. {
  253. return '';//$this->exercise_data['description'];
  254. }
  255. /**
  256. * Check if this still links to an exercise
  257. */
  258. public function is_valid_link()
  259. {
  260. $sql = 'SELECT count(id) from '.$this->get_forum_thread_table().'
  261. WHERE c_id = '.$this->course_id.' AND thread_id = '.$this->get_ref_id().' AND session_id='.api_get_session_id().'';
  262. $result = Database::query($sql);
  263. $number = Database::fetch_row($result);
  264. return ($number[0] != 0);
  265. }
  266. public function get_test_id()
  267. {
  268. return 'DEBUG:ID';
  269. }
  270. public function get_link()
  271. {
  272. //it was extracts the forum id
  273. $sql = 'SELECT * FROM '.$this->get_forum_thread_table()."
  274. WHERE c_id = '.$this->course_id.' AND thread_id = '".$this->get_ref_id()."' AND session_id = ".api_get_session_id()."";
  275. $result = Database::query($sql);
  276. $row = Database::fetch_array($result,'ASSOC');
  277. $forum_id=$row['forum_id'];
  278. $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;
  279. return $url;
  280. }
  281. private function get_exercise_data()
  282. {
  283. $session_id = api_get_session_id();
  284. if ($session_id) {
  285. $session_condition = 'session_id='.api_get_session_id();
  286. } else {
  287. $session_condition = '(session_id = 0 OR session_id IS NULL)';
  288. }
  289. if (!isset($this->exercise_data)) {
  290. $sql = 'SELECT * FROM '.$this->get_forum_thread_table().'
  291. WHERE c_id = '.$this->course_id.' AND thread_id = '.$this->get_ref_id().' AND '.$session_condition;
  292. $query = Database::query($sql);
  293. $this->exercise_data = Database::fetch_array($query);
  294. }
  295. return $this->exercise_data;
  296. }
  297. public function get_icon_name()
  298. {
  299. return 'forum';
  300. }
  301. function save_linked_data()
  302. {
  303. $weight = (float)$this->get_weight();
  304. $ref_id = $this->get_ref_id();
  305. if (!empty($ref_id)) {
  306. $sql = 'UPDATE '.$this->get_forum_thread_table().' SET thread_weight='.$weight.'
  307. WHERE c_id = '.$this->course_id.' AND thread_id= '.$ref_id;
  308. Database::query($sql);
  309. }
  310. }
  311. function delete_linked_data()
  312. {
  313. $ref_id = $this->get_ref_id();
  314. if (!empty($ref_id)) {
  315. //Cleans forum
  316. $sql = 'UPDATE '.$this->get_forum_thread_table().' SET
  317. thread_qualify_max = 0,
  318. thread_weight = 0,
  319. thread_title_qualify = ""
  320. WHERE c_id = '.$this->course_id.' AND thread_id= '.$ref_id;
  321. Database::query($sql);
  322. }
  323. }
  324. }