surveylink.class.php 7.5 KB

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