surveylink.class.php 8.7 KB

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