forumthreadlink.class.php 12 KB

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