forumthreadlink.class.php 13 KB

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