forumthreadlink.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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
  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. die('Error in get_not_created_links() : course code not set');
  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($sessionId, true, false, 'session_id');
  137. $sql = 'SELECT thread_qualify_max
  138. FROM '.Database :: get_course_table(TABLE_FORUM_THREAD)."
  139. WHERE
  140. c_id = ".$this->course_id." AND
  141. thread_id = '".$this->get_ref_id()."'
  142. $sessionCondition
  143. ";
  144. $query = Database::query($sql);
  145. $assignment = Database::fetch_array($query);
  146. $sql = "SELECT * FROM $thread_qualify
  147. WHERE
  148. c_id = ".$this->course_id." AND
  149. thread_id = ".$this->get_ref_id()."
  150. $sessionCondition
  151. ";
  152. if (isset($stud_id)) {
  153. $sql .= ' AND user_id = '.intval($stud_id);
  154. }
  155. // order by id, that way the student's first attempt is accessed first
  156. $sql .= ' ORDER BY qualify_time DESC';
  157. $scores = Database::query($sql);
  158. // for 1 student
  159. if (isset($stud_id)) {
  160. if ($threadInfo['thread_peer_qualify'] == 0) {
  161. // Classic way of calculate score
  162. if ($data = Database::fetch_array($scores)) {
  163. return array(
  164. $data['qualify'],
  165. $assignment['thread_qualify_max']
  166. );
  167. } else {
  168. // We sent the 0/thread_qualify_max instead of null for correct calculations
  169. return array(0, $assignment['thread_qualify_max']);
  170. }
  171. } else {
  172. // Take average
  173. $score = 0;
  174. $counter = 0;
  175. if (Database::num_rows($scores)) {
  176. while ($data = Database::fetch_array($scores, 'ASSOC')) {
  177. $score += $data['qualify'];
  178. $counter++;
  179. }
  180. }
  181. // If no result
  182. if (empty($counter) || $counter <= 2) {
  183. return array(0, $assignment['thread_qualify_max']);
  184. }
  185. return [$score/$counter, $assignment['thread_qualify_max']];
  186. }
  187. } else {
  188. // All students -> get average
  189. $students = array(); // user list, needed to make sure we only
  190. // take first attempts into account
  191. $counter = 0;
  192. $sum = 0;
  193. $bestResult = 0;
  194. $weight = 0;
  195. $sumResult = 0;
  196. while ($data = Database::fetch_array($scores)) {
  197. if (!(array_key_exists($data['user_id'], $students))) {
  198. if ($assignment['thread_qualify_max'] != 0) {
  199. $students[$data['user_id']] = $data['qualify'];
  200. $counter++;
  201. $sum += $data['qualify'] / $assignment['thread_qualify_max'];
  202. $sumResult += $data['qualify'];
  203. if ($data['qualify'] > $bestResult) {
  204. $bestResult = $data['qualify'];
  205. }
  206. $weight = $assignment['thread_qualify_max'];
  207. }
  208. }
  209. }
  210. if ($counter == 0) {
  211. return null;
  212. } else {
  213. switch ($type) {
  214. case 'best':
  215. return array($bestResult, $weight);
  216. break;
  217. case 'average':
  218. return array($sumResult/$counter, $weight);
  219. break;
  220. case 'ranking':
  221. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  222. break;
  223. default:
  224. return array($sum, $counter);
  225. break;
  226. }
  227. }
  228. }
  229. }
  230. /**
  231. * Lazy load function to get the database table of the student publications
  232. */
  233. private function get_forum_thread_table()
  234. {
  235. return $this->forum_thread_table = Database :: get_course_table(TABLE_FORUM_THREAD);
  236. }
  237. public function needs_name_and_description()
  238. {
  239. return false;
  240. }
  241. public function needs_max()
  242. {
  243. return false;
  244. }
  245. public function needs_results()
  246. {
  247. return false;
  248. }
  249. /**
  250. * @return string
  251. */
  252. public function get_name()
  253. {
  254. $this->get_exercise_data();
  255. $thread_title=isset($this->exercise_data['thread_title']) ? $this->exercise_data['thread_title'] : '';
  256. $thread_title_qualify=isset($this->exercise_data['thread_title_qualify']) ? $this->exercise_data['thread_title_qualify'] : '';
  257. if ( isset($thread_title_qualify) && $thread_title_qualify!="") {
  258. return $this->exercise_data['thread_title_qualify'];
  259. } else {
  260. return $thread_title;
  261. }
  262. }
  263. /**
  264. * @return string
  265. */
  266. public function get_description()
  267. {
  268. return '';//$this->exercise_data['description'];
  269. }
  270. /**
  271. * Check if this still links to an exercise
  272. */
  273. public function is_valid_link()
  274. {
  275. $sql = 'SELECT count(id) from '.$this->get_forum_thread_table().'
  276. WHERE c_id = '.$this->course_id.' AND thread_id = '.$this->get_ref_id().' AND session_id='.api_get_session_id().'';
  277. $result = Database::query($sql);
  278. $number = Database::fetch_row($result);
  279. return ($number[0] != 0);
  280. }
  281. public function get_test_id()
  282. {
  283. return 'DEBUG:ID';
  284. }
  285. public function get_link()
  286. {
  287. $sessionId = api_get_session_id();
  288. //it was extracts the forum id
  289. $sql = 'SELECT * FROM '.$this->get_forum_thread_table()."
  290. WHERE c_id = '.$this->course_id.' AND thread_id = '".$this->get_ref_id()."' AND session_id = ".$sessionId."";
  291. $result = Database::query($sql);
  292. $row = Database::fetch_array($result,'ASSOC');
  293. $forum_id=$row['forum_id'];
  294. $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;
  295. return $url;
  296. }
  297. private function get_exercise_data()
  298. {
  299. $session_id = api_get_session_id();
  300. if ($session_id) {
  301. $session_condition = 'session_id='.api_get_session_id();
  302. } else {
  303. $session_condition = '(session_id = 0 OR session_id IS NULL)';
  304. }
  305. if (!isset($this->exercise_data)) {
  306. $sql = 'SELECT * FROM '.$this->get_forum_thread_table().'
  307. WHERE c_id = '.$this->course_id.' AND thread_id = '.$this->get_ref_id().' AND '.$session_condition;
  308. $query = Database::query($sql);
  309. $this->exercise_data = Database::fetch_array($query);
  310. }
  311. return $this->exercise_data;
  312. }
  313. public function get_icon_name()
  314. {
  315. return 'forum';
  316. }
  317. function save_linked_data()
  318. {
  319. $weight = (float)$this->get_weight();
  320. $ref_id = $this->get_ref_id();
  321. if (!empty($ref_id)) {
  322. $sql = 'UPDATE '.$this->get_forum_thread_table().' SET thread_weight='.$weight.'
  323. WHERE c_id = '.$this->course_id.' AND thread_id= '.$ref_id;
  324. Database::query($sql);
  325. }
  326. }
  327. function delete_linked_data()
  328. {
  329. $ref_id = $this->get_ref_id();
  330. if (!empty($ref_id)) {
  331. //Cleans forum
  332. $sql = 'UPDATE '.$this->get_forum_thread_table().' SET
  333. thread_qualify_max = 0,
  334. thread_weight = 0,
  335. thread_title_qualify = ""
  336. WHERE c_id = '.$this->course_id.' AND thread_id= '.$ref_id;
  337. Database::query($sql);
  338. }
  339. }
  340. }