surveylink.class.php 7.5 KB

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