surveylink.class.php 9.6 KB

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