surveylink.class.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Gradebook link to a survey item.
  5. *
  6. * @author Ivan Tcholakov <ivantcholakov@gmail.com>, 2010
  7. *
  8. * @package chamilo.gradebook
  9. */
  10. class SurveyLink extends AbstractLink
  11. {
  12. private $survey_table = null;
  13. private $survey_data = [];
  14. /**
  15. * Constructor.
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->set_type(LINK_SURVEY);
  21. }
  22. /**
  23. * @return string
  24. */
  25. public function get_name()
  26. {
  27. $this->get_survey_data();
  28. return $this->survey_data['code'].': '.self::html_to_text($this->survey_data['title']);
  29. }
  30. public function get_description()
  31. {
  32. $this->get_survey_data();
  33. return $this->survey_data['subtitle'];
  34. }
  35. public function get_type_name()
  36. {
  37. return get_lang('Survey');
  38. }
  39. public function is_allowed_to_change_name()
  40. {
  41. return false;
  42. }
  43. public function needs_name_and_description()
  44. {
  45. return false;
  46. }
  47. public function needs_max()
  48. {
  49. return false;
  50. }
  51. public function needs_results()
  52. {
  53. return false;
  54. }
  55. /**
  56. * Generates an array of all surveys available.
  57. *
  58. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  59. */
  60. public function get_all_links()
  61. {
  62. if (empty($this->course_code)) {
  63. die('Error in get_all_links() : course code not set');
  64. }
  65. $tbl_survey = $this->get_survey_table();
  66. $session_id = api_get_session_id();
  67. $course_id = api_get_course_int_id();
  68. $sql = 'SELECT survey_id, title, code FROM '.$tbl_survey.'
  69. WHERE c_id = '.$course_id.' AND session_id = '.intval($session_id);
  70. $result = Database::query($sql);
  71. while ($data = Database::fetch_array($result)) {
  72. $links[] = [
  73. $data['survey_id'],
  74. api_trunc_str(
  75. $data['code'].': '.self::html_to_text($data['title']),
  76. 80
  77. ),
  78. ];
  79. }
  80. return isset($links) ? $links : [];
  81. }
  82. /**
  83. * Has anyone done this survey yet?
  84. * Implementation of the AbstractLink class, mainly used dynamically in gradebook/lib/fe.
  85. */
  86. public function has_results()
  87. {
  88. $ref_id = intval($this->get_ref_id());
  89. $session_id = api_get_session_id();
  90. $tbl_survey = Database::get_course_table(TABLE_SURVEY);
  91. $tbl_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
  92. $sql = "SELECT
  93. COUNT(i.answered)
  94. FROM $tbl_survey AS s
  95. JOIN $tbl_survey_invitation AS i ON s.code = i.survey_code
  96. WHERE
  97. s.c_id = {$this->course_id} AND
  98. i.c_id = {$this->course_id} AND
  99. s.survey_id = $ref_id AND
  100. i.session_id = $session_id";
  101. $sql_result = Database::query($sql);
  102. $data = Database::fetch_array($sql_result);
  103. return $data[0] != 0;
  104. }
  105. /**
  106. * Calculate score for a student (to show in the gradebook).
  107. *
  108. * @param int $stud_id
  109. * @param string $type Type of result we want (best|average|ranking)
  110. *
  111. * @return array|null
  112. */
  113. public function calc_score($stud_id = null, $type = null)
  114. {
  115. // Note: Max score is assumed to be always 1 for surveys,
  116. // only student's participation is to be taken into account.
  117. $max_score = 1;
  118. $ref_id = intval($this->get_ref_id());
  119. $session_id = api_get_session_id();
  120. $tbl_survey = Database::get_course_table(TABLE_SURVEY);
  121. $tbl_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
  122. $get_individual_score = !is_null($stud_id);
  123. $sql = "SELECT i.answered
  124. FROM $tbl_survey AS s
  125. JOIN $tbl_survey_invitation AS i
  126. ON s.code = i.survey_code
  127. WHERE
  128. s.c_id = {$this->course_id} AND
  129. i.c_id = {$this->course_id} AND
  130. s.survey_id = $ref_id AND
  131. i.session_id = $session_id
  132. ";
  133. if ($get_individual_score) {
  134. $sql .= ' AND i.user = '.intval($stud_id);
  135. }
  136. $sql_result = Database::query($sql);
  137. if ($get_individual_score) {
  138. // for 1 student
  139. if ($data = Database::fetch_array($sql_result)) {
  140. return [$data['answered'] ? $max_score : 0, $max_score];
  141. }
  142. return [0, $max_score];
  143. } else {
  144. // for all the students -> get average
  145. $rescount = 0;
  146. $sum = 0;
  147. $bestResult = 0;
  148. while ($data = Database::fetch_array($sql_result)) {
  149. $sum += $data['answered'] ? $max_score : 0;
  150. $rescount++;
  151. if ($data['answered'] > $bestResult) {
  152. $bestResult = $data['answered'];
  153. }
  154. }
  155. $sum = $sum / $max_score;
  156. if ($rescount == 0) {
  157. return [null, null];
  158. }
  159. switch ($type) {
  160. case 'best':
  161. return [$bestResult, $rescount];
  162. break;
  163. case 'average':
  164. return [$sum, $rescount];
  165. break;
  166. case 'ranking':
  167. return null;
  168. break;
  169. default:
  170. return [$sum, $rescount];
  171. break;
  172. }
  173. }
  174. }
  175. /**
  176. * Check if this still links to a survey.
  177. */
  178. public function is_valid_link()
  179. {
  180. $session_id = api_get_session_id();
  181. $sql = 'SELECT count(survey_id) FROM '.$this->get_survey_table().'
  182. WHERE
  183. c_id = '.$this->course_id.' AND
  184. survey_id = '.intval($this->get_ref_id()).' AND
  185. session_id = '.intval($session_id);
  186. $result = Database::query($sql);
  187. $number = Database::fetch_row($result);
  188. return $number[0] != 0;
  189. }
  190. public function get_link()
  191. {
  192. if (api_get_configuration_value('hide_survey_reporting_button')) {
  193. return null;
  194. }
  195. if (api_is_allowed_to_edit()) { // Let students make access only through "Surveys" tool.
  196. $tbl_name = $this->get_survey_table();
  197. $session_id = api_get_session_id();
  198. if ($tbl_name != '') {
  199. $sql = 'SELECT survey_id FROM '.$this->get_survey_table().'
  200. WHERE
  201. c_id = '.$this->course_id.' AND
  202. survey_id = '.intval($this->get_ref_id()).' AND
  203. session_id = '.intval($session_id).' ';
  204. $result = Database::query($sql);
  205. $row = Database::fetch_array($result, 'ASSOC');
  206. $survey_id = $row['survey_id'];
  207. return api_get_path(WEB_PATH).'main/survey/reporting.php?'.api_get_cidreq_params($this->get_course_code(), $session_id).'&survey_id='.$survey_id;
  208. }
  209. }
  210. return null;
  211. }
  212. /**
  213. * Get the name of the icon for this tool.
  214. *
  215. * @return string
  216. */
  217. public function get_icon_name()
  218. {
  219. return 'survey';
  220. }
  221. /**
  222. * Lazy load function to get the database table of the surveys.
  223. */
  224. private function get_survey_table()
  225. {
  226. $this->survey_table = Database::get_course_table(TABLE_SURVEY);
  227. return $this->survey_table;
  228. }
  229. /**
  230. * Get the survey data from the c_survey table with the current object id.
  231. *
  232. * @return mixed
  233. */
  234. private function get_survey_data()
  235. {
  236. $tbl_name = $this->get_survey_table();
  237. $session_id = api_get_session_id();
  238. if ($tbl_name == '') {
  239. return false;
  240. } elseif (empty($this->survey_data)) {
  241. $sql = 'SELECT * FROM '.$tbl_name.'
  242. WHERE
  243. c_id = '.$this->course_id.' AND
  244. survey_id = '.intval($this->get_ref_id()).' AND
  245. session_id = '.intval($session_id);
  246. $query = Database::query($sql);
  247. $this->survey_data = Database::fetch_array($query);
  248. }
  249. return $this->survey_data;
  250. }
  251. private static function html_to_text($string)
  252. {
  253. return strip_tags($string);
  254. }
  255. }