surveylink.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /**
  9. * Class
  10. * @package chamilo.gradebook
  11. */
  12. class SurveyLink extends AbstractLink
  13. {
  14. private $survey_table = null;
  15. public function __construct() {
  16. parent::__construct();
  17. $this->set_type(LINK_SURVEY);
  18. }
  19. public function get_name() {
  20. $this->get_survey_data();
  21. return $this->survey_data['code'].': '.self::html_to_text($this->survey_data['title']);
  22. }
  23. public function get_description() {
  24. $this->get_survey_data();
  25. return $this->survey_data['subtitle'];
  26. }
  27. public function get_type_name() {
  28. return get_lang('Survey');
  29. }
  30. public function is_allowed_to_change_name() {
  31. return false;
  32. }
  33. public function needs_name_and_description() {
  34. return false;
  35. }
  36. public function needs_max() {
  37. return false;
  38. }
  39. public function needs_results() {
  40. return false;
  41. }
  42. /**
  43. * Generates an array of all surveys available.
  44. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  45. */
  46. public function get_all_links() {
  47. if (empty($this->course_code)) {
  48. die('Error in get_all_links() : course code not set');
  49. }
  50. $tbl_survey = $this->get_survey_table();
  51. $session_id = api_get_session_id();
  52. $course_id = api_get_course_int_id();
  53. $sql = 'SELECT survey_id, title, code FROM '.$tbl_survey.' WHERE c_id = '.$course_id.' AND session_id = '.intval($session_id).'';
  54. $result = Database::query($sql);
  55. while ($data = Database::fetch_array($result)) {
  56. $links[] = array($data['survey_id'], Text::api_trunc_str($data['code'].': '.self::html_to_text($data['title']), 80));
  57. };
  58. return isset($links) ? $links : null;
  59. }
  60. /**
  61. * Generates an array of surveys that a teacher hasn't created a link for.
  62. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  63. */
  64. public function get_not_created_links() {
  65. if (empty($this->course_code)) {
  66. die('Error in get_not_created_links() : course code not set');
  67. }
  68. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  69. $sql = 'SELECT survey_id, title, code
  70. FROM '.$this->get_survey_table().' AS srv
  71. WHERE survey_id NOT IN
  72. (SELECT ref_id FROM '.$tbl_grade_links.'
  73. WHERE type = '.LINK_SURVEY." AND course_code = '".$this->get_course_code()."'"
  74. .') AND srv.session_id='.api_get_session_id().'';
  75. $result = Database::query($sql);
  76. $links = array();
  77. while ($data = Database::fetch_array($result)) {
  78. $links[] = array($data['survey_id'], Text::api_trunc_str($data['code'].': '.self::html_to_text($data['title']), 80));
  79. }
  80. return $links;
  81. }
  82. /**
  83. * Has anyone done this survey yet?
  84. */
  85. public function has_results($stud_id=null) {
  86. $ref_id = intval($this->get_ref_id());
  87. $session_id = api_get_session_id();
  88. $tbl_survey = Database::get_course_table(TABLE_SURVEY);
  89. $tbl_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
  90. $get_individual_score = !is_null($stud_id);
  91. $sql = "SELECT COUNT(i.answered) FROM $tbl_survey AS s JOIN $tbl_survey_invitation AS i ON s.code = i.survey_code
  92. WHERE s.c_id = {$this->course_id} AND
  93. i.c_id = {$this->course_id} AND
  94. s.survey_id = $ref_id AND
  95. i.session_id = $session_id";
  96. $sql_result = Database::query($sql);
  97. $data = Database::fetch_array($sql_result);
  98. return ($data[0] != 0);
  99. }
  100. public function calc_score($stud_id = null) {
  101. // Note: Max score is assumed to be always 1 for surveys,
  102. // only student's participation is to be taken into account.
  103. $max_score = 1;
  104. $ref_id = intval($this->get_ref_id());
  105. $session_id = api_get_session_id();
  106. $tbl_survey = Database::get_course_table(TABLE_SURVEY, $database_name);
  107. $tbl_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION, $database_name);
  108. $get_individual_score = !is_null($stud_id);
  109. $sql = "SELECT i.answered FROM $tbl_survey AS s JOIN $tbl_survey_invitation AS i
  110. ON s.code = i.survey_code
  111. WHERE s.c_id = {$this->course_id} AND
  112. i.c_id = {$this->course_id} AND
  113. s.survey_id = $ref_id AND i.session_id = $session_id";
  114. if ($get_individual_score) {
  115. $sql .= ' AND i.user = '.intval($stud_id);
  116. }
  117. $sql_result = Database::query($sql);
  118. if ($get_individual_score) {
  119. // for 1 student
  120. if ($data = Database::fetch_array($sql_result)) {
  121. return array ($data['answered'] ? $max_score : 0, $max_score);
  122. }
  123. return array(0, $max_score);
  124. } else {
  125. // for all the students -> get average
  126. $students = array();
  127. $rescount = 0;
  128. $sum = 0;
  129. while ($data = Database::fetch_array($sql_result)) {
  130. $sum += $data['answered'] ? $max_score : 0;
  131. $rescount++;
  132. }
  133. $sum = $sum / $max_score;
  134. if ($rescount == 0) {
  135. return null;
  136. }
  137. return array($sum, $rescount);
  138. }
  139. }
  140. /**
  141. * Lazy load function to get the database table of the surveys
  142. */
  143. private function get_survey_table() {
  144. $this->survey_table = Database :: get_course_table(TABLE_SURVEY);
  145. return $this->survey_table;
  146. }
  147. /**
  148. * Check if this still links to a survey
  149. */
  150. public function is_valid_link() {
  151. $session_id = api_get_session_id();
  152. $sql = 'SELECT count(survey_id) FROM '.$this->get_survey_table().'
  153. WHERE c_id = '.$this->course_id.' AND survey_id = '.intval($this->get_ref_id()).' AND session_id='.intval($session_id).'';
  154. $result = Database::query($sql);
  155. $number = Database::fetch_row($result);
  156. return ($number[0] != 0);
  157. }
  158. public function get_test_id() {
  159. return 'DEBUG:ID';
  160. }
  161. public function get_link() {
  162. if (api_is_allowed_to_edit()) { // Let students make access only through "Surveys" tool.
  163. $tbl_name = $this->get_survey_table();
  164. $session_id = api_get_session_id();
  165. if ($tbl_name != '') {
  166. $sql = 'SELECT survey_id FROM '.$this->get_survey_table().'
  167. WHERE c_id = '.$this->course_id.' AND survey_id = '.intval($this->get_ref_id()).' AND session_id = '.intval($session_id).' ';
  168. $result = Database::query($sql);
  169. $row = Database::fetch_array($result, 'ASSOC');
  170. $survey_id = $row['survey_id'];
  171. return api_get_path(WEB_PATH).'main/survey/reporting.php?cidReq='.$this->get_course_code().'&survey_id='.$survey_id;
  172. }
  173. }
  174. return null;
  175. }
  176. private function get_survey_data() {
  177. $tbl_name = $this->get_survey_table();
  178. $session_id = api_get_session_id();
  179. if ($tbl_name == '') {
  180. return false;
  181. } elseif (!isset($this->survey_data)) {
  182. $sql = 'SELECT * FROM '.$tbl_name.' WHERE c_id = '.$this->course_id.' AND survey_id = '.intval($this->get_ref_id()).' AND session_id='.intval($session_id).'';
  183. $query = Database::query($sql);
  184. $this->survey_data = Database::fetch_array($query);
  185. }
  186. return $this->survey_data;
  187. }
  188. public function get_icon_name() {
  189. return 'survey';
  190. }
  191. private static function html_to_text($string) {
  192. return trim(api_html_entity_decode(strip_tags(str_ireplace(array('<p>', '</p>', '<br />', '<br/>', '<br>'), array('', ' ', ' ', ' ', ' '), $string)), ENT_QUOTES));
  193. }
  194. }