forumthreadlink.class.php 11 KB

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