Total Complexity | 629 |
Total Lines | 4607 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 0 |
Complex classes like SurveyUtil often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SurveyUtil, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class SurveyUtil |
||
13 | { |
||
14 | /** |
||
15 | * Checks whether the given survey has a pagebreak question as the first |
||
16 | * or the last question. |
||
17 | * If so, break the current process, displaying an error message. |
||
18 | * |
||
19 | * @param int $survey_id Survey ID (database ID) |
||
20 | * @param bool $continue Optional. Whether to continue the current |
||
21 | * process or exit when breaking condition found. Defaults to true (do not break). |
||
22 | */ |
||
23 | public static function check_first_last_question($survey_id, $continue = true) |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * This function removes an (or multiple) answer(s) of a user on a question of a survey. |
||
58 | * |
||
59 | * @param mixed The user id or email of the person who fills the survey |
||
60 | * @param int The survey id |
||
61 | * @param int The question id |
||
62 | * @param int The option id |
||
63 | * |
||
64 | * @author Patrick Cool <[email protected]>, Ghent University |
||
65 | * |
||
66 | * @version January 2007 |
||
67 | */ |
||
68 | public static function remove_answer($user, $survey_id, $question_id, $course_id, $sessionId = 0, $lpItemId = 0) |
||
69 | { |
||
70 | $course_id = intval($course_id); |
||
71 | // table definition |
||
72 | $table = Database::get_course_table(TABLE_SURVEY_ANSWER); |
||
73 | |||
74 | // To select the answers by lp Item |
||
75 | $lpItemCondition = ''; |
||
76 | if (true === api_get_configuration_value('allow_survey_tool_in_lp')) { |
||
77 | $lpItemCondition = " AND c_lp_item_id = $lpItemId"; |
||
78 | } |
||
79 | // To select the answers by session |
||
80 | $sessionCondition = ''; |
||
81 | if (true === api_get_configuration_value('show_surveys_base_in_sessions')) { |
||
82 | $sessionCondition = api_get_session_condition($sessionId); |
||
83 | } |
||
84 | |||
85 | $sql = "DELETE FROM $table |
||
86 | WHERE |
||
87 | c_id = $course_id AND |
||
88 | user = '".Database::escape_string($user)."' AND |
||
89 | survey_id = '".intval($survey_id)."' AND |
||
90 | question_id = '".intval($question_id)."' |
||
91 | $sessionCondition |
||
92 | $lpItemCondition"; |
||
93 | Database::query($sql); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * This function stores an answer of a user on a question of a survey. |
||
98 | * |
||
99 | * @param mixed The user id or email of the person who fills the survey |
||
100 | * @param int Survey id |
||
101 | * @param int Question id |
||
102 | * @param int Option id |
||
103 | * @param string Option value |
||
104 | * @param array $survey_data Survey data settings |
||
105 | * |
||
106 | * @return bool False if insufficient data, true otherwise |
||
107 | * |
||
108 | * @author Patrick Cool <[email protected]>, Ghent University |
||
109 | * |
||
110 | * @version January 2007 |
||
111 | */ |
||
112 | public static function store_answer( |
||
113 | $user, |
||
114 | $survey_id, |
||
115 | $question_id, |
||
116 | $option_id, |
||
117 | $option_value, |
||
118 | $survey_data, |
||
119 | $otherOption = '', |
||
120 | $sessionId = 0, |
||
121 | $lpItemId = 0 |
||
122 | ) { |
||
123 | // If the question_id is empty, don't store an answer |
||
124 | if (empty($question_id)) { |
||
125 | return false; |
||
126 | } |
||
127 | |||
128 | // Table definition |
||
129 | $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); |
||
130 | |||
131 | // Make the survey anonymous |
||
132 | if (1 == $survey_data['anonymous']) { |
||
133 | $surveyUser = Session::read('surveyuser'); |
||
134 | if (empty($surveyUser)) { |
||
135 | $user = md5($user.time()); |
||
136 | Session::write('surveyuser', $user); |
||
137 | } else { |
||
138 | $user = Session::read('surveyuser'); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | $course_id = $survey_data['c_id']; |
||
143 | |||
144 | if (!empty($otherOption)) { |
||
145 | $option_id = $option_id.'@:@'.$otherOption; |
||
146 | } |
||
147 | // To select the answers by Lp Item |
||
148 | $lpItemCondition = ''; |
||
149 | if (true === api_get_configuration_value('allow_survey_tool_in_lp')) { |
||
150 | $lpItemCondition = " , c_lp_item_id = $lpItemId"; |
||
151 | } |
||
152 | // To select the answers by session |
||
153 | $sessionCondition = ''; |
||
154 | if (api_get_configuration_value('show_surveys_base_in_sessions')) { |
||
155 | $sessionCondition = " , session_id = $sessionId"; |
||
156 | } |
||
157 | |||
158 | $sql = "INSERT INTO $table_survey_answer SET |
||
159 | c_id = $course_id, |
||
160 | user = '".Database::escape_string($user)."', |
||
161 | survey_id = '".Database::escape_string($survey_id)."', |
||
162 | question_id = '".Database::escape_string($question_id)."', |
||
163 | option_id = '".Database::escape_string($option_id)."', |
||
164 | value = '".Database::escape_string($option_value)."' |
||
165 | $sessionCondition |
||
166 | $lpItemCondition |
||
167 | "; |
||
168 | Database::query($sql); |
||
169 | $insertId = Database::insert_id(); |
||
170 | if ($insertId) { |
||
171 | $sql = "UPDATE $table_survey_answer SET answer_id = $insertId |
||
172 | WHERE iid = $insertId"; |
||
173 | Database::query($sql); |
||
174 | |||
175 | return true; |
||
176 | } |
||
177 | |||
178 | return false; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * This function checks the parameters that are used in this page. |
||
183 | * |
||
184 | * @return string $people_filled The header, an error and the footer if any parameter fails, else it returns true |
||
185 | * |
||
186 | * @author Patrick Cool <[email protected]>, Ghent University |
||
187 | * |
||
188 | * @version February 2007 |
||
189 | */ |
||
190 | public static function check_parameters($people_filled) |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * This function deals with the action handling. |
||
256 | * |
||
257 | * @param array $survey_data |
||
258 | * @param array $people_filled |
||
259 | * |
||
260 | * @author Patrick Cool <[email protected]>, Ghent University |
||
261 | * |
||
262 | * @version February 2007 |
||
263 | */ |
||
264 | public static function handle_reporting_actions($survey_data, $people_filled) |
||
265 | { |
||
266 | $action = isset($_GET['action']) ? $_GET['action'] : ''; |
||
267 | $lpItemId = isset($_GET['lp_item_id']) ? (int) $_GET['lp_item_id'] : 0; |
||
268 | // Getting the number of question |
||
269 | $questions = SurveyManager::get_questions($survey_data['survey_id']); |
||
270 | |||
271 | $counter = 0; |
||
272 | foreach ($questions as $key => $value) { |
||
273 | if ($value['type'] !== 'pagebreak') { |
||
274 | $counter++; |
||
275 | } |
||
276 | } |
||
277 | // Counting the number of questions that are relevant for the reporting |
||
278 | $survey_data['number_of_questions'] = $counter; |
||
279 | |||
280 | switch ($action) { |
||
281 | case 'questionreport': |
||
282 | self::display_question_report($survey_data); |
||
283 | break; |
||
284 | case 'userreport': |
||
285 | self::displayUserReport($survey_data, $people_filled); |
||
286 | break; |
||
287 | case 'comparativereport': |
||
288 | self::display_comparative_report(); |
||
289 | break; |
||
290 | case 'completereport': |
||
291 | if (api_get_configuration_value('allow_survey_tool_in_lp')) { |
||
292 | $surveysAnswered = SurveyManager::getInvitationsAnswered($survey_data['code'], api_get_course_int_id(), api_get_session_id()); |
||
293 | if (count($surveysAnswered) > 0) { |
||
294 | $actionBar = '<div class="actions">'; |
||
295 | $actionBar .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.$survey_data['survey_id'].'&'.api_get_cidreq().'">' |
||
296 | .Display::return_icon( |
||
297 | 'back.png', |
||
298 | get_lang('BackTo').' '.get_lang('ReportingOverview'), |
||
299 | [], |
||
300 | ICON_SIZE_MEDIUM |
||
301 | ) |
||
302 | .'</a>'; |
||
303 | $actionBar .= '</div>'; |
||
304 | echo $actionBar; |
||
305 | foreach ($surveysAnswered as $survey) { |
||
306 | echo self::displayCompleteReport($survey_data, 0, true, true, !$survey_data['anonymous'], $survey->getLpItemId()); |
||
307 | } |
||
308 | } |
||
309 | } else { |
||
310 | echo self::displayCompleteReport($survey_data, 0, true, true, !$survey_data['anonymous']); |
||
311 | } |
||
312 | break; |
||
313 | case 'deleteuserreport': |
||
314 | self::delete_user_report($_GET['survey_id'], $_GET['user'], $lpItemId); |
||
315 | break; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * This function deletes the report of an user who wants to retake the survey. |
||
321 | * |
||
322 | * @param int $survey_id |
||
323 | * @param int $user_id |
||
324 | * |
||
325 | * @author Christian Fasanando Flores <[email protected]> |
||
326 | * |
||
327 | * @version November 2008 |
||
328 | */ |
||
329 | public static function delete_user_report($survey_id, $user_id, $lpItemId = 0) |
||
330 | { |
||
331 | $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); |
||
332 | $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION); |
||
333 | $table_survey = Database::get_course_table(TABLE_SURVEY); |
||
334 | |||
335 | $course_id = api_get_course_int_id(); |
||
336 | $survey_id = (int) $survey_id; |
||
337 | $user_id = Database::escape_string($user_id); |
||
338 | |||
339 | if (!empty($survey_id) && !empty($user_id)) { |
||
340 | |||
341 | // To delete or update the answer by session |
||
342 | $sessionCondition = ''; |
||
343 | if (api_get_configuration_value('show_surveys_base_in_sessions')) { |
||
344 | $sessionId = api_get_session_id(); |
||
345 | $sessionCondition = api_get_session_condition($sessionId); |
||
346 | } |
||
347 | |||
348 | $lpItemCondition = ''; |
||
349 | if (api_get_configuration_value('allow_survey_tool_in_lp')) { |
||
350 | $lpItemCondition = " AND c_lp_item_id = $lpItemId"; |
||
351 | } |
||
352 | |||
353 | // delete data from survey_answer by user_id and survey_id |
||
354 | $sql = "DELETE FROM $table_survey_answer |
||
355 | WHERE c_id = $course_id AND survey_id = '".$survey_id."' AND user = '".$user_id."' $sessionCondition $lpItemCondition"; |
||
356 | Database::query($sql); |
||
357 | // update field answered from survey_invitation by user_id and survey_id |
||
358 | $sql = "UPDATE $table_survey_invitation SET answered = '0' |
||
359 | WHERE |
||
360 | c_id = $course_id AND |
||
361 | survey_code = ( |
||
362 | SELECT code FROM $table_survey |
||
363 | WHERE |
||
364 | c_id = $course_id AND |
||
365 | survey_id = '".$survey_id."' |
||
366 | ) AND |
||
367 | user = '".$user_id."' $sessionCondition"; |
||
368 | $result = Database::query($sql); |
||
369 | } |
||
370 | |||
371 | if ($result !== false) { |
||
372 | $message = get_lang('SurveyUserAnswersHaveBeenRemovedSuccessfully').'<br /> |
||
373 | <a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=userreport&survey_id=' |
||
374 | .$survey_id.'">'. |
||
375 | get_lang('GoBack').'</a>'; |
||
376 | echo Display::return_message($message, 'confirmation', false); |
||
377 | } |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * @param array $survey_data |
||
382 | * @param array $people_filled |
||
383 | * |
||
384 | * @return string |
||
385 | */ |
||
386 | public static function displayUserReportForm($survey_data, $people_filled) |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * @param int $userId |
||
432 | * @param array $survey_data |
||
433 | * @param bool $addMessage |
||
434 | */ |
||
435 | public static function displayUserReportAnswers($userId, $survey_data, $addMessage = true, $lpItemId = 0) |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * This function displays the user report which is basically nothing more |
||
557 | * than a one-page display of all the questions |
||
558 | * of the survey that is filled with the answers of the person who filled the survey. |
||
559 | * |
||
560 | * @return string html code of the one-page survey with the answers of the selected user |
||
561 | * |
||
562 | * @author Patrick Cool <[email protected]>, Ghent University |
||
563 | * |
||
564 | * @version February 2007 - Updated March 2008 |
||
565 | */ |
||
566 | public static function displayUserReport($survey_data, $people_filled, $addActionBar = true) |
||
626 | } |
||
627 | } |
||
628 | |||
629 | public static function addSelectLpItemsForm($surveyId, $courseId, $sessionId, $actionReport = '', &$form = null) |
||
698 | } |
||
699 | } |
||
700 | |||
701 | /** |
||
702 | * This function displays the report by question. |
||
703 | * |
||
704 | * It displays a table with all the options of the question and the number of users who have answered positively on |
||
705 | * the option. The number of users who answered positive on a given option is expressed in an absolute number, in a |
||
706 | * percentage of the total and graphically using bars By clicking on the absolute number you get a list with the |
||
707 | * persons who have answered this. You can then click on the name of the person and you will then go to the report |
||
708 | * by user where you see all the answers of that user. |
||
709 | * |
||
710 | * @param array All the survey data |
||
711 | * |
||
712 | * @return string html code that displays the report by question |
||
713 | * |
||
714 | * @todo allow switching between horizontal and vertical. |
||
715 | * @todo multiple response: percentage are probably not OK |
||
716 | * @todo the question and option text have to be shortened and should expand when the user clicks on it. |
||
717 | * @todo the pagebreak and comment question types should not be shown => removed from $survey_data before |
||
718 | * |
||
719 | * @author Patrick Cool <[email protected]>, Ghent University |
||
720 | * |
||
721 | * @version February 2007 - Updated March 2008 |
||
722 | */ |
||
723 | public static function display_question_report($survey_data) |
||
1042 | } |
||
1043 | } |
||
1044 | |||
1045 | /** |
||
1046 | * Display score data about a survey question. |
||
1047 | * |
||
1048 | * @param array Question info |
||
1049 | * @param int The offset of results shown |
||
1050 | */ |
||
1051 | public static function display_question_report_score($survey_data, $question, $offset, $lpItemId = 0) |
||
1173 | } |
||
1174 | |||
1175 | /** |
||
1176 | * This functions displays the complete reporting. |
||
1177 | * |
||
1178 | * @param array $survey_data |
||
1179 | * @param int $userId |
||
1180 | * @param bool $addActionBar |
||
1181 | * @param bool $addFilters |
||
1182 | * @param bool $addExtraFields |
||
1183 | * |
||
1184 | * @return string |
||
1185 | */ |
||
1186 | public static function displayCompleteReport( |
||
1528 | } |
||
1529 | |||
1530 | /** |
||
1531 | * Return user answers in a row. |
||
1532 | * |
||
1533 | * @param $survey_data |
||
1534 | * @param $possible_options |
||
1535 | * @param $answers_of_user |
||
1536 | * @param $user |
||
1537 | * @param $questions |
||
1538 | * @param bool $display_extra_user_fields |
||
1539 | * |
||
1540 | * @return string |
||
1541 | */ |
||
1542 | public static function display_complete_report_row( |
||
1641 | } |
||
1642 | |||
1643 | /** |
||
1644 | * Quite similar to display_complete_report(), returns an HTML string |
||
1645 | * that can be used in a csv file. |
||
1646 | * |
||
1647 | * @param array $survey_data The basic survey data as initially obtained by SurveyManager::get_survey() |
||
1648 | * @param int $user_id The ID of the user asking for the report |
||
1649 | * @param bool $compact Whether to present the long (v marks with multiple columns per question) or compact (one column per question) answers format |
||
1650 | * |
||
1651 | * @todo consider merging this function with display_complete_report |
||
1652 | * |
||
1653 | * @throws Exception |
||
1654 | * |
||
1655 | * @return string The contents of a csv file |
||
1656 | * |
||
1657 | * @author Patrick Cool <[email protected]>, Ghent University |
||
1658 | * |
||
1659 | * @version February 2007 |
||
1660 | */ |
||
1661 | public static function export_complete_report($survey_data, $user_id = 0, $compact = false, $lpItemId = 0) |
||
1662 | { |
||
1663 | $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : 0; |
||
1664 | |||
1665 | if (empty($surveyId)) { |
||
1666 | return false; |
||
1667 | } |
||
1668 | |||
1669 | $course = api_get_course_info(); |
||
1670 | $course_id = $course['real_id']; |
||
1671 | $sessionId = api_get_session_id(); |
||
1672 | |||
1673 | // To select the answers by session |
||
1674 | $sessionCondition = ''; |
||
1675 | if (api_get_configuration_value('show_surveys_base_in_sessions')) { |
||
1676 | if (!empty($sessionId)) { |
||
1677 | $sessionCondition = api_get_session_condition($sessionId); |
||
1678 | } |
||
1679 | } |
||
1680 | |||
1681 | $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); |
||
1682 | $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); |
||
1683 | $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); |
||
1684 | |||
1685 | $translate = false; |
||
1686 | if (api_get_configuration_value('translate_html') == true) { |
||
1687 | $translate = true; |
||
1688 | } |
||
1689 | |||
1690 | // The first column |
||
1691 | $return = ';'; |
||
1692 | |||
1693 | // Show extra fields blank space (enough for extra fields on next line) |
||
1694 | $extra_user_fields = UserManager::get_extra_fields( |
||
1695 | 0, |
||
1696 | 0, |
||
1697 | 5, |
||
1698 | 'ASC', |
||
1699 | false, |
||
1700 | true |
||
1701 | ); |
||
1702 | |||
1703 | $num = count($extra_user_fields); |
||
1704 | if (!$survey_data['anonymous']) { |
||
1705 | $return .= str_repeat(';', $num); |
||
1706 | } |
||
1707 | $suffixLpItem = !empty($lpItemId) ? '-item'.$lpItemId : ''; |
||
1708 | $sql = "SELECT |
||
1709 | questions.question_id, |
||
1710 | questions.type, |
||
1711 | questions.survey_question, |
||
1712 | count(options.question_option_id) as number_of_options |
||
1713 | FROM $table_survey_question questions |
||
1714 | LEFT JOIN $table_survey_question_option options |
||
1715 | ON |
||
1716 | questions.question_id = options.question_id AND |
||
1717 | options.c_id = questions.c_id |
||
1718 | WHERE |
||
1719 | survey_question NOT LIKE '%{{%' AND |
||
1720 | questions.type <> 'pagebreak' AND |
||
1721 | questions.survey_id = $surveyId AND |
||
1722 | questions.c_id = $course_id |
||
1723 | GROUP BY questions.question_id |
||
1724 | ORDER BY questions.sort ASC"; |
||
1725 | |||
1726 | $result = Database::query($sql); |
||
1727 | while ($row = Database::fetch_array($result)) { |
||
1728 | if ($translate) { |
||
1729 | $row['survey_question'] = api_get_filtered_multilingual_HTML_string($row['survey_question'], $course['language']); |
||
1730 | } |
||
1731 | // We show the questions if |
||
1732 | // 1. there is no question filter and the export button has not been clicked |
||
1733 | // 2. there is a question filter but the question is selected for display |
||
1734 | if (!(isset($_POST['submit_question_filter'.$suffixLpItem])) || |
||
1735 | (isset($_POST['submit_question_filter'.$suffixLpItem]) && |
||
1736 | is_array($_POST['questions_filter'.$suffixLpItem]) && |
||
1737 | in_array($row['question_id'], $_POST['questions_filter'.$suffixLpItem])) |
||
1738 | ) { |
||
1739 | if ($row['number_of_options'] == 0 || $compact) { |
||
1740 | $return .= str_replace( |
||
1741 | "\r\n", |
||
1742 | ' ', |
||
1743 | api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES) |
||
1744 | ) |
||
1745 | .';'; |
||
1746 | } else { |
||
1747 | $numberOfOptions = $row['number_of_options']; |
||
1748 | if ($row['type'] == 'multiplechoiceother') { |
||
1749 | $numberOfOptions++; |
||
1750 | } |
||
1751 | for ($ii = 0; $ii < $numberOfOptions; $ii++) { |
||
1752 | $return .= str_replace( |
||
1753 | "\r\n", |
||
1754 | ' ', |
||
1755 | api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES) |
||
1756 | ) |
||
1757 | .';'; |
||
1758 | } |
||
1759 | } |
||
1760 | } |
||
1761 | } |
||
1762 | |||
1763 | $return .= "\n"; |
||
1764 | // Getting all the questions and options |
||
1765 | $return .= ';'; |
||
1766 | // Show the fields names for user fields |
||
1767 | if (!empty($extra_user_fields) && !$survey_data['anonymous']) { |
||
1768 | foreach ($extra_user_fields as &$field) { |
||
1769 | if ($translate) { |
||
1770 | $field[3] = api_get_filtered_multilingual_HTML_string($field[3], $course['language']); |
||
1771 | } |
||
1772 | $return .= '"' |
||
1773 | .str_replace( |
||
1774 | "\r\n", |
||
1775 | ' ', |
||
1776 | api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES) |
||
1777 | ) |
||
1778 | .'";'; |
||
1779 | } |
||
1780 | } |
||
1781 | |||
1782 | $sql = "SELECT DISTINCT |
||
1783 | survey_question.question_id, |
||
1784 | survey_question.survey_id, |
||
1785 | survey_question.survey_question, |
||
1786 | survey_question.display, |
||
1787 | survey_question.sort, |
||
1788 | survey_question.type, |
||
1789 | survey_question_option.question_option_id, |
||
1790 | survey_question_option.option_text, |
||
1791 | survey_question_option.sort as option_sort |
||
1792 | FROM $table_survey_question survey_question |
||
1793 | LEFT JOIN $table_survey_question_option survey_question_option |
||
1794 | ON |
||
1795 | survey_question.question_id = survey_question_option.question_id AND |
||
1796 | survey_question_option.c_id = survey_question.c_id |
||
1797 | WHERE |
||
1798 | survey_question NOT LIKE '%{{%' AND |
||
1799 | survey_question.type <> 'pagebreak' AND |
||
1800 | survey_question.survey_id = $surveyId AND |
||
1801 | survey_question.c_id = $course_id |
||
1802 | ORDER BY survey_question.sort ASC, survey_question_option.sort ASC"; |
||
1803 | $result = Database::query($sql); |
||
1804 | $possible_answers = []; |
||
1805 | $possible_answers_type = []; |
||
1806 | $current_question_type = ''; |
||
1807 | $current_question_id = null; |
||
1808 | while ($row = Database::fetch_array($result)) { |
||
1809 | // We show the options if |
||
1810 | // 1. there is no question filter and the export button has not been clicked |
||
1811 | // 2. there is a question filter but the question is selected for display |
||
1812 | if ($translate) { |
||
1813 | $row['option_text'] = api_get_filtered_multilingual_HTML_string($row['option_text'], $course['language']); |
||
1814 | } |
||
1815 | if (!(isset($_POST['submit_question_filter'.$suffixLpItem])) || ( |
||
1816 | is_array($_POST['questions_filter'.$suffixLpItem]) && |
||
1817 | in_array($row['question_id'], $_POST['questions_filter'.$suffixLpItem]) |
||
1818 | ) |
||
1819 | ) { |
||
1820 | if ($current_question_id != $row['question_id']) { |
||
1821 | if ($current_question_type == 'multiplechoiceother') { |
||
1822 | $return .= api_html_entity_decode(strip_tags(get_lang('Comment')), ENT_QUOTES).';'; |
||
1823 | } |
||
1824 | } |
||
1825 | |||
1826 | $current_question_type = $row['type']; |
||
1827 | $current_question_id = $row['question_id']; |
||
1828 | |||
1829 | $row['option_text'] = str_replace(["\r", "\n"], ['', ''], $row['option_text']); |
||
1830 | if (!$compact) { |
||
1831 | $return .= api_html_entity_decode(strip_tags($row['option_text']), ENT_QUOTES).';'; |
||
1832 | |||
1833 | $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; |
||
1834 | } else { |
||
1835 | $possible_answers[$row['question_id']][$row['question_option_id']] = $row['option_text']; |
||
1836 | } |
||
1837 | $possible_answers_type[$row['question_id']] = $row['type']; |
||
1838 | } |
||
1839 | } |
||
1840 | |||
1841 | if ($current_question_type == 'multiplechoiceother') { |
||
1842 | $return .= api_html_entity_decode(strip_tags(get_lang('Comment')), ENT_QUOTES).';'; |
||
1843 | } |
||
1844 | |||
1845 | $return .= "\n"; |
||
1846 | |||
1847 | // To select the answers by Lp item |
||
1848 | $lpItemCondition = ''; |
||
1849 | if (api_get_configuration_value('allow_survey_tool_in_lp')) { |
||
1850 | $lpItemCondition = " AND c_lp_item_id = $lpItemId"; |
||
1851 | } |
||
1852 | |||
1853 | // Getting all the answers of the users |
||
1854 | $old_user = ''; |
||
1855 | $answers_of_user = []; |
||
1856 | $sql = "SELECT * FROM $table_survey_answer |
||
1857 | WHERE |
||
1858 | c_id = $course_id AND |
||
1859 | survey_id = $surveyId |
||
1860 | $sessionCondition |
||
1861 | $lpItemCondition |
||
1862 | "; |
||
1863 | if ($user_id != 0) { |
||
1864 | $user_id = (int) $user_id; |
||
1865 | $sql .= " AND user = $user_id "; |
||
1866 | } |
||
1867 | $sql .= ' ORDER BY user ASC '; |
||
1868 | |||
1869 | $questionIdList = array_keys($possible_answers_type); |
||
1870 | $open_question_iterator = 1; |
||
1871 | $result = Database::query($sql); |
||
1872 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
1873 | if (!in_array($row['question_id'], $questionIdList)) { |
||
1874 | continue; |
||
1875 | } |
||
1876 | if ($old_user != $row['user'] && $old_user != '') { |
||
1877 | $return .= self::export_complete_report_row( |
||
1878 | $survey_data, |
||
1879 | $possible_answers, |
||
1880 | $answers_of_user, |
||
1881 | $old_user, |
||
1882 | !$survey_data['anonymous'], |
||
1883 | $compact, |
||
1884 | $possible_answers_type |
||
1885 | ); |
||
1886 | $answers_of_user = []; |
||
1887 | } |
||
1888 | |||
1889 | if ('open' === $possible_answers_type[$row['question_id']] || |
||
1890 | 'comment' === $possible_answers_type[$row['question_id']] |
||
1891 | ) { |
||
1892 | $temp_id = 'open'.$open_question_iterator; |
||
1893 | $answers_of_user[$row['question_id']][$temp_id] = $row; |
||
1894 | $open_question_iterator++; |
||
1895 | } else { |
||
1896 | $answers_of_user[$row['question_id']][$row['option_id']] = $row; |
||
1897 | } |
||
1898 | $old_user = $row['user']; |
||
1899 | } |
||
1900 | |||
1901 | // This is to display the last user |
||
1902 | $return .= self::export_complete_report_row( |
||
1903 | $survey_data, |
||
1904 | $possible_answers, |
||
1905 | $answers_of_user, |
||
1906 | $old_user, |
||
1907 | true, |
||
1908 | $compact, |
||
1909 | $possible_answers_type |
||
1910 | ); |
||
1911 | |||
1912 | return $return; |
||
1913 | } |
||
1914 | |||
1915 | /** |
||
1916 | * Add a line to the csv file. |
||
1917 | * |
||
1918 | * @param array $survey_data Basic survey data (we're mostly interested in the 'anonymous' index) |
||
1919 | * @param array $possible_options Possible answers |
||
1920 | * @param array $answers_of_user User's answers |
||
1921 | * @param mixed $user User ID or user details as string - Used as a string in the result string |
||
1922 | * @param bool $display_extra_user_fields Whether to display user fields or not |
||
1923 | * @param bool $compact Whether to show answers as different column values (true) or one column per option (false, default) |
||
1924 | * |
||
1925 | * @return string One line of the csv file |
||
1926 | * |
||
1927 | * @author Patrick Cool <[email protected]>, Ghent University |
||
1928 | * |
||
1929 | * @version February 2007 |
||
1930 | */ |
||
1931 | public static function export_complete_report_row( |
||
2055 | } |
||
2056 | |||
2057 | /** |
||
2058 | * Quite similar to display_complete_report(), returns an HTML string |
||
2059 | * that can be used in a csv file. |
||
2060 | * |
||
2061 | * @todo consider merging this function with display_complete_report |
||
2062 | * |
||
2063 | * @return string The contents of a csv file |
||
2064 | * |
||
2065 | * @author Patrick Cool <[email protected]>, Ghent University |
||
2066 | * |
||
2067 | * @version February 2007 |
||
2068 | */ |
||
2069 | public static function export_complete_report_xls($survey_data, $filename, $user_id = 0, $returnFile = false, $lpItemId = 0) |
||
2070 | { |
||
2071 | $course_id = api_get_course_int_id(); |
||
2072 | $user_id = (int) $user_id; |
||
2073 | $surveyId = $survey_data['survey_id']; |
||
2074 | |||
2075 | if (empty($course_id) || empty($surveyId)) { |
||
2076 | return false; |
||
2077 | } |
||
2078 | |||
2079 | $spreadsheet = new PHPExcel(); |
||
2080 | $spreadsheet->setActiveSheetIndex(0); |
||
2081 | $worksheet = $spreadsheet->getActiveSheet(); |
||
2082 | $line = 1; |
||
2083 | $column = 1; // Skip the first column (row titles) |
||
2084 | |||
2085 | // Show extra fields blank space (enough for extra fields on next line) |
||
2086 | // Show user fields section with a big th colspan that spans over all fields |
||
2087 | $extra_user_fields = UserManager::get_extra_fields( |
||
2088 | 0, |
||
2089 | 0, |
||
2090 | 5, |
||
2091 | 'ASC', |
||
2092 | false, |
||
2093 | true |
||
2094 | ); |
||
2095 | $num = count($extra_user_fields); |
||
2096 | if (!$survey_data['anonymous']) { |
||
2097 | for ($i = 0; $i < $num; $i++) { |
||
2098 | $worksheet->setCellValueByColumnAndRow($column, $line, ''); |
||
2099 | $column++; |
||
2100 | } |
||
2101 | } |
||
2102 | $display_extra_user_fields = true; |
||
2103 | |||
2104 | // Database table definitions |
||
2105 | $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); |
||
2106 | $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); |
||
2107 | $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); |
||
2108 | |||
2109 | // To select the answers by session |
||
2110 | $sessionId = api_get_session_id(); |
||
2111 | $sessionCondition = ''; |
||
2112 | if (api_get_configuration_value('show_surveys_base_in_sessions')) { |
||
2113 | if (!empty($sessionId)) { |
||
2114 | $sessionCondition = api_get_session_condition($sessionId); |
||
2115 | } |
||
2116 | } |
||
2117 | $suffixLpItem = !empty($lpItemId) ? '-item'.$lpItemId : ''; |
||
2118 | // First line (questions) |
||
2119 | $sql = "SELECT |
||
2120 | questions.question_id, |
||
2121 | questions.type, |
||
2122 | questions.survey_question, |
||
2123 | count(options.question_option_id) as number_of_options |
||
2124 | FROM $table_survey_question questions |
||
2125 | LEFT JOIN $table_survey_question_option options |
||
2126 | ON |
||
2127 | questions.question_id = options.question_id AND |
||
2128 | options.c_id = questions.c_id |
||
2129 | WHERE |
||
2130 | survey_question NOT LIKE '%{{%' AND |
||
2131 | questions.type <> 'pagebreak' AND |
||
2132 | questions.survey_id = $surveyId AND |
||
2133 | questions.c_id = $course_id |
||
2134 | GROUP BY questions.question_id |
||
2135 | ORDER BY questions.sort ASC"; |
||
2136 | $result = Database::query($sql); |
||
2137 | while ($row = Database::fetch_array($result)) { |
||
2138 | // We show the questions if |
||
2139 | // 1. there is no question filter and the export button has not been clicked |
||
2140 | // 2. there is a question filter but the question is selected for display |
||
2141 | if (!(isset($_POST['submit_question_filter'.$suffixLpItem])) || |
||
2142 | (isset($_POST['submit_question_filter'.$suffixLpItem]) && is_array($_POST['questions_filter'.$suffixLpItem]) && |
||
2143 | in_array($row['question_id'], $_POST['questions_filter'.$suffixLpItem])) |
||
2144 | ) { |
||
2145 | if ($row['number_of_options'] == 0 && ($row['type'] === 'open' || $row['type'] === 'comment')) { |
||
2146 | $worksheet->setCellValueByColumnAndRow( |
||
2147 | $column, |
||
2148 | $line, |
||
2149 | api_html_entity_decode( |
||
2150 | strip_tags($row['survey_question']), |
||
2151 | ENT_QUOTES |
||
2152 | ) |
||
2153 | ); |
||
2154 | $column++; |
||
2155 | } else { |
||
2156 | $numberOfOptions = $row['number_of_options']; |
||
2157 | if ($row['type'] == 'multiplechoiceother') { |
||
2158 | $numberOfOptions++; |
||
2159 | } |
||
2160 | for ($ii = 0; $ii < $numberOfOptions; $ii++) { |
||
2161 | $worksheet->setCellValueByColumnAndRow( |
||
2162 | $column, |
||
2163 | $line, |
||
2164 | api_html_entity_decode( |
||
2165 | strip_tags($row['survey_question']), |
||
2166 | ENT_QUOTES |
||
2167 | ) |
||
2168 | ); |
||
2169 | $column++; |
||
2170 | } |
||
2171 | } |
||
2172 | } |
||
2173 | } |
||
2174 | |||
2175 | $line++; |
||
2176 | $column = 1; |
||
2177 | // Show extra field values |
||
2178 | if ($display_extra_user_fields && !$survey_data['anonymous']) { |
||
2179 | // Show the fields names for user fields |
||
2180 | foreach ($extra_user_fields as &$field) { |
||
2181 | $worksheet->setCellValueByColumnAndRow( |
||
2182 | $column, |
||
2183 | $line, |
||
2184 | api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES) |
||
2185 | ); |
||
2186 | $column++; |
||
2187 | } |
||
2188 | } |
||
2189 | |||
2190 | // Getting all the questions and options (second line) |
||
2191 | $sql = "SELECT |
||
2192 | survey_question.question_id, |
||
2193 | survey_question.survey_id, |
||
2194 | survey_question.survey_question, |
||
2195 | survey_question.display, |
||
2196 | survey_question.sort, |
||
2197 | survey_question.type, |
||
2198 | survey_question_option.question_option_id, |
||
2199 | survey_question_option.option_text, |
||
2200 | survey_question_option.sort as option_sort |
||
2201 | FROM $table_survey_question survey_question |
||
2202 | LEFT JOIN $table_survey_question_option survey_question_option |
||
2203 | ON |
||
2204 | survey_question.question_id = survey_question_option.question_id AND |
||
2205 | survey_question_option.c_id = survey_question.c_id |
||
2206 | WHERE |
||
2207 | survey_question NOT LIKE '%{{%' AND |
||
2208 | survey_question.type <> 'pagebreak' AND |
||
2209 | survey_question.survey_id = $surveyId AND |
||
2210 | survey_question.c_id = $course_id |
||
2211 | ORDER BY survey_question.sort ASC, survey_question_option.sort ASC"; |
||
2212 | $result = Database::query($sql); |
||
2213 | $possible_answers = []; |
||
2214 | $possible_answers_type = []; |
||
2215 | $current_question_type = ''; |
||
2216 | $current_question_id = null; |
||
2217 | while ($row = Database::fetch_array($result)) { |
||
2218 | // We show the options if |
||
2219 | // 1. there is no question filter and the export button has not been clicked |
||
2220 | // 2. there is a quesiton filter but the question is selected for display |
||
2221 | if (!isset($_POST['submit_question_filter'.$suffixLpItem]) || |
||
2222 | (isset($_POST['questions_filter'.$suffixLpItem]) && is_array($_POST['questions_filter'.$suffixLpItem]) && |
||
2223 | in_array($row['question_id'], $_POST['questions_filter'.$suffixLpItem])) |
||
2224 | ) { |
||
2225 | if ($current_question_id != $row['question_id']) { |
||
2226 | if ($current_question_type == 'multiplechoiceother') { |
||
2227 | $worksheet->setCellValueByColumnAndRow( |
||
2228 | $column, |
||
2229 | $line, |
||
2230 | api_html_entity_decode( |
||
2231 | strip_tags(get_lang('Comment')), |
||
2232 | ENT_QUOTES |
||
2233 | ) |
||
2234 | ); |
||
2235 | $column++; |
||
2236 | } |
||
2237 | } |
||
2238 | |||
2239 | $current_question_type = $row['type']; |
||
2240 | $current_question_id = $row['question_id']; |
||
2241 | |||
2242 | $worksheet->setCellValueByColumnAndRow( |
||
2243 | $column, |
||
2244 | $line, |
||
2245 | api_html_entity_decode( |
||
2246 | strip_tags($row['option_text']), |
||
2247 | ENT_QUOTES |
||
2248 | ) |
||
2249 | ); |
||
2250 | $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; |
||
2251 | $possible_answers_type[$row['question_id']] = $row['type']; |
||
2252 | $column++; |
||
2253 | } |
||
2254 | } |
||
2255 | |||
2256 | if ($current_question_type == 'multiplechoiceother') { |
||
2257 | $worksheet->setCellValueByColumnAndRow( |
||
2258 | $column, |
||
2259 | $line, |
||
2260 | api_html_entity_decode( |
||
2261 | strip_tags(get_lang('Comment')), |
||
2262 | ENT_QUOTES |
||
2263 | ) |
||
2264 | ); |
||
2265 | } |
||
2266 | |||
2267 | // To select the answers by Lp item |
||
2268 | $lpItemCondition = ''; |
||
2269 | if (api_get_configuration_value('allow_survey_tool_in_lp')) { |
||
2270 | $lpItemCondition = " AND c_lp_item_id = $lpItemId"; |
||
2271 | } |
||
2272 | |||
2273 | // Getting all the answers of the users |
||
2274 | $line++; |
||
2275 | $column = 0; |
||
2276 | $old_user = ''; |
||
2277 | $answers_of_user = []; |
||
2278 | $sql = "SELECT * FROM $table_survey_answer |
||
2279 | WHERE c_id = $course_id AND survey_id = $surveyId $sessionCondition $lpItemCondition"; |
||
2280 | if ($user_id != 0) { |
||
2281 | $sql .= " AND user='".$user_id."' "; |
||
2282 | } |
||
2283 | $sql .= ' ORDER BY user ASC'; |
||
2284 | |||
2285 | $open_question_iterator = 1; |
||
2286 | $result = Database::query($sql); |
||
2287 | while ($row = Database::fetch_array($result)) { |
||
2288 | if ($old_user != $row['user'] && $old_user != '') { |
||
2289 | $return = self::export_complete_report_row_xls( |
||
2290 | $survey_data, |
||
2291 | $possible_answers, |
||
2292 | $answers_of_user, |
||
2293 | $old_user, |
||
2294 | !$survey_data['anonymous'], |
||
2295 | $possible_answers_type |
||
2296 | ); |
||
2297 | foreach ($return as $elem) { |
||
2298 | $worksheet->setCellValueByColumnAndRow($column, $line, $elem); |
||
2299 | $column++; |
||
2300 | } |
||
2301 | $answers_of_user = []; |
||
2302 | $line++; |
||
2303 | $column = 0; |
||
2304 | } |
||
2305 | if ($possible_answers_type[$row['question_id']] === 'open' || |
||
2306 | $possible_answers_type[$row['question_id']] === 'comment' |
||
2307 | ) { |
||
2308 | $temp_id = 'open'.$open_question_iterator; |
||
2309 | $answers_of_user[$row['question_id']][$temp_id] = $row; |
||
2310 | $open_question_iterator++; |
||
2311 | } else { |
||
2312 | $answers_of_user[$row['question_id']][$row['option_id']] = $row; |
||
2313 | } |
||
2314 | $old_user = $row['user']; |
||
2315 | } |
||
2316 | |||
2317 | $return = self::export_complete_report_row_xls( |
||
2318 | $survey_data, |
||
2319 | $possible_answers, |
||
2320 | $answers_of_user, |
||
2321 | $old_user, |
||
2322 | true, |
||
2323 | $possible_answers_type |
||
2324 | ); |
||
2325 | |||
2326 | // this is to display the last user |
||
2327 | foreach ($return as $elem) { |
||
2328 | $worksheet->setCellValueByColumnAndRow($column, $line, $elem); |
||
2329 | $column++; |
||
2330 | } |
||
2331 | |||
2332 | $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); |
||
2333 | $writer = new PHPExcel_Writer_Excel2007($spreadsheet); |
||
2334 | $writer->save($file); |
||
2335 | |||
2336 | if ($returnFile) { |
||
2337 | return $file; |
||
2338 | } |
||
2339 | |||
2340 | DocumentManager::file_send_for_download($file, true, $filename); |
||
2341 | |||
2342 | return null; |
||
2343 | } |
||
2344 | |||
2345 | /** |
||
2346 | * Add a line to the csv file. |
||
2347 | * |
||
2348 | * @param array Possible answers |
||
2349 | * @param array User's answers |
||
2350 | * @param mixed User ID or user details as string - Used as a string in the result string |
||
2351 | * @param bool Whether to display user fields or not |
||
2352 | * |
||
2353 | * @return string One line of the csv file |
||
2354 | */ |
||
2355 | public static function export_complete_report_row_xls( |
||
2356 | $survey_data, |
||
2357 | $possible_options, |
||
2358 | $answers_of_user, |
||
2359 | $user, |
||
2360 | $display_extra_user_fields = false, |
||
2361 | $questionTypes = true |
||
2362 | ) { |
||
2363 | $return = []; |
||
2364 | if ($survey_data['anonymous'] == 0) { |
||
2365 | if (intval($user) !== 0) { |
||
2366 | $userInfo = api_get_user_info($user); |
||
2367 | if ($userInfo) { |
||
2368 | $user_displayed = $userInfo['complete_name_with_username']; |
||
2369 | } else { |
||
2370 | $user_displayed = '-'; |
||
2371 | } |
||
2372 | $return[] = $user_displayed; |
||
2373 | } else { |
||
2374 | $return[] = $user; |
||
2375 | } |
||
2376 | } else { |
||
2377 | $return[] = '-'; // The user column |
||
2378 | } |
||
2379 | |||
2380 | if ($display_extra_user_fields) { |
||
2381 | //show user fields data, if any, for this user |
||
2382 | $user_fields_values = UserManager::get_extra_user_data( |
||
2383 | $user, |
||
2384 | false, |
||
2385 | false, |
||
2386 | false, |
||
2387 | true |
||
2388 | ); |
||
2389 | foreach ($user_fields_values as $fieldName => $value) { |
||
2390 | $fieldValue = is_array($value) ? $value[$fieldName] : $value; |
||
2391 | $return[] = api_html_entity_decode(strip_tags($fieldValue), ENT_QUOTES); |
||
2392 | } |
||
2393 | } |
||
2394 | |||
2395 | if (is_array($possible_options)) { |
||
2396 | foreach ($possible_options as $question_id => &$possible_option) { |
||
2397 | $otherPaddingNeeded = ($questionTypes[$question_id] == 'multiplechoiceother' ? true : false); |
||
2398 | if (is_array($possible_option) && count($possible_option) > 0) { |
||
2399 | foreach ($possible_option as $option_id => &$value) { |
||
2400 | $my_answers_of_user = isset($answers_of_user[$question_id]) |
||
2401 | ? $answers_of_user[$question_id] |
||
2402 | : []; |
||
2403 | $key = array_keys($my_answers_of_user); |
||
2404 | if (isset($key[0]) && substr($key[0], 0, 4) == 'open') { |
||
2405 | $return[] = api_html_entity_decode( |
||
2406 | strip_tags($answers_of_user[$question_id][$key[0]]['option_id']), |
||
2407 | ENT_QUOTES |
||
2408 | ); |
||
2409 | } elseif (!empty($answers_of_user[$question_id][$option_id])) { |
||
2410 | if ($answers_of_user[$question_id][$option_id]['value'] != 0) { |
||
2411 | $return[] = $answers_of_user[$question_id][$option_id]['value']; |
||
2412 | } else { |
||
2413 | $return[] = 'v'; |
||
2414 | } |
||
2415 | } elseif (isset($key[0]) && strpos($key[0], '@:@') !== false) { |
||
2416 | list($idAnswer, $other) = explode('@:@', $key[0]); |
||
2417 | if ($idAnswer == $option_id) { |
||
2418 | if (strlen($other) > 0) { |
||
2419 | $return[] = 'v'; |
||
2420 | $return[] = api_html_entity_decode(strip_tags($other), ENT_QUOTES); |
||
2421 | $otherPaddingNeeded = false; |
||
2422 | } else { |
||
2423 | $return[] = 'v'; |
||
2424 | } |
||
2425 | } else { |
||
2426 | $return[] = ''; |
||
2427 | } |
||
2428 | } else { |
||
2429 | $return[] = ''; |
||
2430 | } |
||
2431 | } |
||
2432 | if ($otherPaddingNeeded == true) { |
||
2433 | $return[] = ''; |
||
2434 | } |
||
2435 | } |
||
2436 | } |
||
2437 | } |
||
2438 | |||
2439 | return $return; |
||
2440 | } |
||
2441 | |||
2442 | /** |
||
2443 | * This function displays the comparative report which |
||
2444 | * allows you to compare two questions |
||
2445 | * A comparative report creates a table where one question |
||
2446 | * is on the x axis and a second question is on the y axis. |
||
2447 | * In the intersection is the number of people who have |
||
2448 | * answered positive on both options. |
||
2449 | * |
||
2450 | * @return string HTML code |
||
2451 | * |
||
2452 | * @author Patrick Cool <[email protected]>, Ghent University |
||
2453 | * |
||
2454 | * @version February 2007 |
||
2455 | */ |
||
2456 | public static function display_comparative_report() |
||
2457 | { |
||
2458 | // Allowed question types for comparative report |
||
2459 | $allowed_question_types = [ |
||
2460 | 'yesno', |
||
2461 | 'multiplechoice', |
||
2462 | 'multipleresponse', |
||
2463 | 'dropdown', |
||
2464 | 'percentage', |
||
2465 | 'score', |
||
2466 | ]; |
||
2467 | |||
2468 | $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : 0; |
||
2469 | |||
2470 | // Getting all the questions |
||
2471 | $questions = SurveyManager::get_questions($surveyId); |
||
2472 | |||
2473 | // Actions bar |
||
2474 | echo '<div class="actions">'; |
||
2475 | echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.$surveyId.'&'.api_get_cidreq() |
||
2476 | .'">' |
||
2477 | .Display::return_icon( |
||
2478 | 'back.png', |
||
2479 | get_lang('BackTo').' '.get_lang('ReportingOverview'), |
||
2480 | [], |
||
2481 | ICON_SIZE_MEDIUM |
||
2482 | ) |
||
2483 | .'</a>'; |
||
2484 | echo '</div>'; |
||
2485 | |||
2486 | // Displaying an information message that only the questions with predefined answers can be used in a comparative report |
||
2487 | echo Display::return_message(get_lang('OnlyQuestionsWithPredefinedAnswers'), 'normal', false); |
||
2488 | |||
2489 | $xAxis = isset($_GET['xaxis']) ? Security::remove_XSS($_GET['xaxis']) : ''; |
||
2490 | $yAxis = isset($_GET['yaxis']) ? Security::remove_XSS($_GET['yaxis']) : ''; |
||
2491 | $course_id = api_get_course_int_id(); |
||
2492 | $lpItemId = isset($_REQUEST['lp_item']) ? (int) $_REQUEST['lp_item'] : ''; |
||
2493 | $sessionId = api_get_session_id(); |
||
2494 | |||
2495 | $url = api_get_self().'?'.api_get_cidreq().'&action='.Security::remove_XSS($_GET['action']) |
||
2496 | .'&survey_id='.$surveyId.'&xaxis='.$xAxis.'&y='.$yAxis; |
||
2497 | |||
2498 | $form = new FormValidator('compare', 'get', $url); |
||
2499 | $form->addHidden('action', Security::remove_XSS($_GET['action'])); |
||
2500 | $form->addHidden('survey_id', $surveyId); |
||
2501 | $optionsX = ['----']; |
||
2502 | $optionsY = ['----']; |
||
2503 | $defaults = []; |
||
2504 | foreach ($questions as &$question) { |
||
2505 | // Ignored tagged questions |
||
2506 | if ($question) { |
||
2507 | if (strpos($question['question'], '{{') !== false) { |
||
2508 | $question = null; |
||
2509 | continue; |
||
2510 | } |
||
2511 | } |
||
2512 | if (is_array($allowed_question_types)) { |
||
2513 | if (in_array($question['type'], $allowed_question_types)) { |
||
2514 | if (isset($_GET['xaxis']) && $_GET['xaxis'] == $question['question_id']) { |
||
2515 | $defaults['xaxis'] = $question['question_id']; |
||
2516 | } |
||
2517 | |||
2518 | if (isset($_GET['yaxis']) && $_GET['yaxis'] == $question['question_id']) { |
||
2519 | $defaults['yaxis'] = $question['question_id']; |
||
2520 | } |
||
2521 | $question['question'] = Security::remove_XSS($question['question']); |
||
2522 | |||
2523 | $optionsX[$question['question_id']] = api_substr(strip_tags($question['question']), 0, 90); |
||
2524 | $optionsY[$question['question_id']] = api_substr(strip_tags($question['question']), 0, 90); |
||
2525 | } |
||
2526 | } |
||
2527 | } |
||
2528 | |||
2529 | if (api_get_configuration_value('allow_survey_tool_in_lp')) { |
||
2530 | self::addSelectLpItemsForm($surveyId, $course_id, $sessionId, 'comparativereport', $form); |
||
2531 | } |
||
2532 | |||
2533 | $form->addSelect('xaxis', get_lang('SelectXAxis'), $optionsX); |
||
2534 | $form->addSelect('yaxis', get_lang('SelectYAxis'), $optionsY); |
||
2535 | |||
2536 | $form->addButtonSearch(get_lang('CompareQuestions')); |
||
2537 | $form->setDefaults($defaults); |
||
2538 | $form->display(); |
||
2539 | |||
2540 | // Getting all the information of the x axis |
||
2541 | if (!empty($xAxis)) { |
||
2542 | $question_x = SurveyManager::get_question($xAxis); |
||
2543 | } |
||
2544 | |||
2545 | // Getting all the information of the y axis |
||
2546 | if (!empty($yAxis)) { |
||
2547 | $question_y = SurveyManager::get_question($yAxis); |
||
2548 | } |
||
2549 | |||
2550 | if (!empty($xAxis) && !empty($yAxis)) { |
||
2551 | // Getting the answers of the two questions |
||
2552 | $answers_x = self::get_answers_of_question_by_user($surveyId, $xAxis, $lpItemId); |
||
2553 | $answers_y = self::get_answers_of_question_by_user($surveyId, $yAxis, $lpItemId); |
||
2554 | |||
2555 | // Displaying the table |
||
2556 | $tableHtml = '<table border="1" class="table table-hover table-striped data_table">'; |
||
2557 | $xOptions = []; |
||
2558 | // The header |
||
2559 | $tableHtml .= '<tr>'; |
||
2560 | for ($ii = 0; $ii <= count($question_x['answers']); $ii++) { |
||
2561 | if ($ii == 0) { |
||
2562 | $tableHtml .= '<th> </th>'; |
||
2563 | } else { |
||
2564 | if ($question_x['type'] === 'score') { |
||
2565 | for ($x = 1; $x <= $question_x['maximum_score']; $x++) { |
||
2566 | $tableHtml .= '<th>'.Security::remove_XSS($question_x['answers'][($ii - 1)]).'<br />'.$x.'</th>'; |
||
2567 | } |
||
2568 | $x = ''; |
||
2569 | } else { |
||
2570 | $tableHtml .= '<th>'.Security::remove_XSS($question_x['answers'][($ii - 1)]).'</th>'; |
||
2571 | } |
||
2572 | $optionText = strip_tags($question_x['answers'][$ii - 1]); |
||
2573 | $optionText = html_entity_decode($optionText); |
||
2574 | $optionText = Security::remove_XSS($optionText); |
||
2575 | array_push($xOptions, trim($optionText)); |
||
2576 | } |
||
2577 | } |
||
2578 | $tableHtml .= '</tr>'; |
||
2579 | $chartData = []; |
||
2580 | // The main part |
||
2581 | for ($ij = 0; $ij < count($question_y['answers']); $ij++) { |
||
2582 | $currentYQuestion = strip_tags($question_y['answers'][$ij]); |
||
2583 | $currentYQuestion = html_entity_decode($currentYQuestion); |
||
2584 | // The Y axis is a scoring question type so we have more rows than the options (actually options * maximum score) |
||
2585 | if ($question_y['type'] == 'score') { |
||
2586 | for ($y = 1; $y <= $question_y['maximum_score']; $y++) { |
||
2587 | $tableHtml .= '<tr>'; |
||
2588 | for ($ii = 0; $ii <= count($question_x['answers']); $ii++) { |
||
2589 | if ($question_x['type'] == 'score') { |
||
2590 | for ($x = 1; $x <= $question_x['maximum_score']; $x++) { |
||
2591 | if ($ii == 0) { |
||
2592 | $tableHtml .= '<th>'.Security::remove_XSS($question_y['answers'][($ij)]).' '.$y.'</th>'; |
||
2593 | break; |
||
2594 | } else { |
||
2595 | $tableHtml .= '<td align="center">'; |
||
2596 | $votes = self::comparative_check( |
||
2597 | $answers_x, |
||
2598 | $answers_y, |
||
2599 | $question_x['answersid'][($ii - 1)], |
||
2600 | $question_y['answersid'][($ij)], |
||
2601 | $x, |
||
2602 | $y |
||
2603 | ); |
||
2604 | $tableHtml .= $votes; |
||
2605 | array_push( |
||
2606 | $chartData, |
||
2607 | [ |
||
2608 | 'serie' => [$currentYQuestion, $xOptions[$ii - 1]], |
||
2609 | 'option' => $x, |
||
2610 | 'votes' => $votes, |
||
2611 | ] |
||
2612 | ); |
||
2613 | $tableHtml .= '</td>'; |
||
2614 | } |
||
2615 | } |
||
2616 | } else { |
||
2617 | if ($ii == 0) { |
||
2618 | $tableHtml .= '<th>'.Security::remove_XSS($question_y['answers'][$ij]).' '.$y.'</th>'; |
||
2619 | } else { |
||
2620 | $tableHtml .= '<td align="center">'; |
||
2621 | $votes = self::comparative_check( |
||
2622 | $answers_x, |
||
2623 | $answers_y, |
||
2624 | $question_x['answersid'][($ii - 1)], |
||
2625 | $question_y['answersid'][($ij)], |
||
2626 | 0, |
||
2627 | $y |
||
2628 | ); |
||
2629 | $tableHtml .= $votes; |
||
2630 | array_push( |
||
2631 | $chartData, |
||
2632 | [ |
||
2633 | 'serie' => [$currentYQuestion, $xOptions[$ii - 1]], |
||
2634 | 'option' => $y, |
||
2635 | 'votes' => $votes, |
||
2636 | ] |
||
2637 | ); |
||
2638 | $tableHtml .= '</td>'; |
||
2639 | } |
||
2640 | } |
||
2641 | } |
||
2642 | $tableHtml .= '</tr>'; |
||
2643 | } |
||
2644 | } else { |
||
2645 | // The Y axis is NOT a score question type so the number of rows = the number of options |
||
2646 | $tableHtml .= '<tr>'; |
||
2647 | for ($ii = 0; $ii <= count($question_x['answers']); $ii++) { |
||
2648 | if ($question_x['type'] === 'score') { |
||
2649 | for ($x = 1; $x <= $question_x['maximum_score']; $x++) { |
||
2650 | if ($ii == 0) { |
||
2651 | $tableHtml .= '<th>'.Security::remove_XSS($question_y['answers'][$ij]).'</th>'; |
||
2652 | break; |
||
2653 | } else { |
||
2654 | $tableHtml .= '<td align="center">'; |
||
2655 | $votes = self::comparative_check( |
||
2656 | $answers_x, |
||
2657 | $answers_y, |
||
2658 | $question_x['answersid'][($ii - 1)], |
||
2659 | $question_y['answersid'][($ij)], |
||
2660 | $x, |
||
2661 | 0 |
||
2662 | ); |
||
2663 | $tableHtml .= $votes; |
||
2664 | array_push( |
||
2665 | $chartData, |
||
2666 | [ |
||
2667 | 'serie' => [$currentYQuestion, $xOptions[$ii - 1]], |
||
2668 | 'option' => $x, |
||
2669 | 'votes' => $votes, |
||
2670 | ] |
||
2671 | ); |
||
2672 | $tableHtml .= '</td>'; |
||
2673 | } |
||
2674 | } |
||
2675 | } else { |
||
2676 | if ($ii == 0) { |
||
2677 | $tableHtml .= '<th>'.Security::remove_XSS($question_y['answers'][($ij)]).'</th>'; |
||
2678 | } else { |
||
2679 | $tableHtml .= '<td align="center">'; |
||
2680 | $votes = self::comparative_check( |
||
2681 | $answers_x, |
||
2682 | $answers_y, |
||
2683 | $question_x['answersid'][($ii - 1)], |
||
2684 | $question_y['answersid'][($ij)] |
||
2685 | ); |
||
2686 | $tableHtml .= $votes; |
||
2687 | array_push( |
||
2688 | $chartData, |
||
2689 | [ |
||
2690 | 'serie' => $xOptions[$ii - 1], |
||
2691 | 'option' => $currentYQuestion, |
||
2692 | 'votes' => $votes, |
||
2693 | ] |
||
2694 | ); |
||
2695 | $tableHtml .= '</td>'; |
||
2696 | } |
||
2697 | } |
||
2698 | } |
||
2699 | $tableHtml .= '</tr>'; |
||
2700 | } |
||
2701 | } |
||
2702 | $tableHtml .= '</table>'; |
||
2703 | echo '<div id="chartContainer" class="col-md-12">'; |
||
2704 | echo self::drawChart($chartData, true); |
||
2705 | echo '</div>'; |
||
2706 | echo $tableHtml; |
||
2707 | } |
||
2708 | } |
||
2709 | |||
2710 | /** |
||
2711 | * Get all the answers of a question grouped by user. |
||
2712 | * |
||
2713 | * @param int $survey_id Survey ID |
||
2714 | * @param int $question_id Question ID |
||
2715 | * |
||
2716 | * @return array Array containing all answers of all users, grouped by user |
||
2717 | * |
||
2718 | * @author Patrick Cool <[email protected]>, Ghent University |
||
2719 | * |
||
2720 | * @version February 2007 - Updated March 2008 |
||
2721 | */ |
||
2722 | public static function get_answers_of_question_by_user($survey_id, $question_id, $lpItemId = 0) |
||
2723 | { |
||
2724 | $course_id = api_get_course_int_id(); |
||
2725 | $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); |
||
2726 | $sessionId = api_get_session_id(); |
||
2727 | |||
2728 | // To select the answers by session |
||
2729 | $sessionCondition = ''; |
||
2730 | if (api_get_configuration_value('show_surveys_base_in_sessions')) { |
||
2731 | if (!empty($sessionId)) { |
||
2732 | $sessionCondition = api_get_session_condition($sessionId); |
||
2733 | } |
||
2734 | } |
||
2735 | // To select the answers by Lp item |
||
2736 | $lpItemCondition = ''; |
||
2737 | if (api_get_configuration_value('allow_survey_tool_in_lp') && !empty($lpItemId)) { |
||
2738 | $lpItemCondition = " AND c_lp_item_id = $lpItemId"; |
||
2739 | } |
||
2740 | |||
2741 | $sql = "SELECT * FROM $table_survey_answer |
||
2742 | WHERE |
||
2743 | c_id = $course_id AND |
||
2744 | survey_id='".intval($survey_id)."' AND |
||
2745 | question_id='".intval($question_id)."' |
||
2746 | $sessionCondition |
||
2747 | $lpItemCondition |
||
2748 | ORDER BY USER ASC"; |
||
2749 | $result = Database::query($sql); |
||
2750 | $return = []; |
||
2751 | while ($row = Database::fetch_array($result)) { |
||
2752 | if ($row['value'] == 0) { |
||
2753 | $return[$row['user']][] = $row['option_id']; |
||
2754 | } else { |
||
2755 | $return[$row['user']][] = $row['option_id'].'*'.$row['value']; |
||
2756 | } |
||
2757 | } |
||
2758 | |||
2759 | return $return; |
||
2760 | } |
||
2761 | |||
2762 | /** |
||
2763 | * Count the number of users who answer positively on both options. |
||
2764 | * |
||
2765 | * @param array All answers of the x axis |
||
2766 | * @param array All answers of the y axis |
||
2767 | * @param int x axis value (= the option_id of the first question) |
||
2768 | * @param int y axis value (= the option_id of the second question) |
||
2769 | * |
||
2770 | * @return int Number of users who have answered positively to both options |
||
2771 | * |
||
2772 | * @author Patrick Cool <[email protected]>, Ghent University |
||
2773 | * |
||
2774 | * @version February 2007 |
||
2775 | */ |
||
2776 | public static function comparative_check( |
||
2777 | $answers_x, |
||
2778 | $answers_y, |
||
2779 | $option_x, |
||
2780 | $option_y, |
||
2781 | $value_x = 0, |
||
2782 | $value_y = 0 |
||
2783 | ) { |
||
2784 | if ($value_x == 0) { |
||
2785 | $check_x = $option_x; |
||
2786 | } else { |
||
2787 | $check_x = $option_x.'*'.$value_x; |
||
2788 | } |
||
2789 | if ($value_y == 0) { |
||
2790 | $check_y = $option_y; |
||
2791 | } else { |
||
2792 | $check_y = $option_y.'*'.$value_y; |
||
2793 | } |
||
2794 | |||
2795 | $counter = 0; |
||
2796 | if (is_array($answers_x)) { |
||
2797 | foreach ($answers_x as $user => &$answers) { |
||
2798 | // Check if the user has given $option_x as answer |
||
2799 | if (in_array($check_x, $answers)) { |
||
2800 | // Check if the user has given $option_y as an answer |
||
2801 | if (!is_null($answers_y[$user]) && |
||
2802 | in_array($check_y, $answers_y[$user]) |
||
2803 | ) { |
||
2804 | $counter++; |
||
2805 | } |
||
2806 | } |
||
2807 | } |
||
2808 | } |
||
2809 | |||
2810 | return $counter; |
||
2811 | } |
||
2812 | |||
2813 | public static function saveInviteMail(CSurvey $survey, $content, $subject, $remind) |
||
2814 | { |
||
2815 | // Reminder or not |
||
2816 | if ($remind) { |
||
2817 | $survey->setReminderMail($content); |
||
2818 | } else { |
||
2819 | $survey->setInviteMail($content); |
||
2820 | } |
||
2821 | |||
2822 | $survey->setMailSubject($subject); |
||
2823 | $em = Database::getManager(); |
||
2824 | $em->persist($survey); |
||
2825 | $em->flush(); |
||
2826 | } |
||
2827 | |||
2828 | /** |
||
2829 | * This function saves all the invitations of course users |
||
2830 | * and additional users in the database |
||
2831 | * and sends the invitations by email. |
||
2832 | * |
||
2833 | * @param int $surveyId |
||
2834 | * @param array $users_array Users array can be both a list of course uids AND a list of additional email addresses |
||
2835 | * @param string $invitation_title title of the mail |
||
2836 | * @param string $invitation_text text of the mail has to contain a **link** string or |
||
2837 | * this will automatically be added to the end |
||
2838 | * @param int $reminder |
||
2839 | * @param bool $sendmail |
||
2840 | * @param int $remindUnAnswered |
||
2841 | * @param bool $isAdditionalEmail |
||
2842 | * @param bool $hideLink |
||
2843 | * |
||
2844 | * @author Patrick Cool <[email protected]>, Ghent University |
||
2845 | * @author Julio Montoya - Adding auto-generated link support |
||
2846 | * |
||
2847 | * @version January 2007 |
||
2848 | */ |
||
2849 | public static function saveInvitations( |
||
2850 | $surveyId, |
||
2851 | $users_array, |
||
2852 | $invitation_title, |
||
2853 | $invitation_text, |
||
2854 | $reminder = 0, |
||
2855 | $sendmail = false, |
||
2856 | $remindUnAnswered = 0, |
||
2857 | $isAdditionalEmail = false, |
||
2858 | $hideLink = false |
||
2859 | ) { |
||
2860 | $surveyId = (int) $surveyId; |
||
2861 | if (!is_array($users_array)) { |
||
2862 | return 0; |
||
2863 | } |
||
2864 | |||
2865 | // Getting the survey information |
||
2866 | $survey_data = SurveyManager::get_survey($surveyId); |
||
2867 | $survey_invitations = self::get_invitations($survey_data['survey_code']); |
||
2868 | $already_invited = self::get_invited_users($survey_data['code']); |
||
2869 | |||
2870 | // Remind unanswered is a special version of remind all reminder |
||
2871 | $exclude_users = []; |
||
2872 | if (1 == $remindUnAnswered) { |
||
2873 | // Remind only unanswered users |
||
2874 | $reminder = 1; |
||
2875 | $exclude_users = SurveyManager::get_people_who_filled_survey($surveyId); |
||
2876 | } |
||
2877 | |||
2878 | $counter = 0; // Nr of invitations "sent" (if sendmail option) |
||
2879 | $course_id = api_get_course_int_id(); |
||
2880 | $session_id = api_get_session_id(); |
||
2881 | |||
2882 | if (false == $isAdditionalEmail) { |
||
2883 | $result = CourseManager::separateUsersGroups($users_array); |
||
2884 | $groupList = $result['groups']; |
||
2885 | $users_array = $result['users']; |
||
2886 | |||
2887 | foreach ($groupList as $groupId) { |
||
2888 | $userGroupList = GroupManager::getStudents($groupId, true); |
||
2889 | $userGroupIdList = array_column($userGroupList, 'user_id'); |
||
2890 | $users_array = array_merge($users_array, $userGroupIdList); |
||
2891 | |||
2892 | $params = [ |
||
2893 | 'c_id' => $course_id, |
||
2894 | 'session_id' => $session_id, |
||
2895 | 'group_id' => $groupId, |
||
2896 | 'survey_code' => $survey_data['code'], |
||
2897 | ]; |
||
2898 | |||
2899 | $invitationExists = self::invitationExists( |
||
2900 | $course_id, |
||
2901 | $session_id, |
||
2902 | $groupId, |
||
2903 | $survey_data['code'] |
||
2904 | ); |
||
2905 | if (empty($invitationExists)) { |
||
2906 | self::save_invitation($params); |
||
2907 | } |
||
2908 | } |
||
2909 | } |
||
2910 | |||
2911 | $users_array = array_unique($users_array); |
||
2912 | foreach ($users_array as $key => $value) { |
||
2913 | if (!isset($value) || '' == $value) { |
||
2914 | continue; |
||
2915 | } |
||
2916 | |||
2917 | // Skip user if reminding only unanswered people |
||
2918 | if (in_array($value, $exclude_users)) { |
||
2919 | continue; |
||
2920 | } |
||
2921 | // Get the unique invitation code if we already have it |
||
2922 | if ($reminder == 1 && array_key_exists($value, $survey_invitations)) { |
||
2923 | $invitation_code = $survey_invitations[$value]['invitation_code']; |
||
2924 | } else { |
||
2925 | $invitation_code = md5($value.microtime()); |
||
2926 | } |
||
2927 | $new_user = false; // User not already invited |
||
2928 | // Store the invitation if user_id not in $already_invited['course_users'] OR email is not in $already_invited['additional_users'] |
||
2929 | $addit_users_array = isset($already_invited['additional_users']) && !empty($already_invited['additional_users']) |
||
2930 | ? explode(';', $already_invited['additional_users']) |
||
2931 | : []; |
||
2932 | $my_alredy_invited = $already_invited['course_users'] == null ? [] : $already_invited['course_users']; |
||
2933 | if ((is_numeric($value) && !in_array($value, $my_alredy_invited)) || |
||
2934 | (!is_numeric($value) && !in_array($value, $addit_users_array)) |
||
2935 | ) { |
||
2936 | $new_user = true; |
||
2937 | if (!array_key_exists($value, $survey_invitations)) { |
||
2938 | $params = [ |
||
2939 | 'c_id' => $course_id, |
||
2940 | 'session_id' => $session_id, |
||
2941 | 'user' => $value, |
||
2942 | 'survey_code' => $survey_data['code'], |
||
2943 | 'invitation_code' => $invitation_code, |
||
2944 | 'invitation_date' => api_get_utc_datetime(), |
||
2945 | ]; |
||
2946 | self::save_invitation($params); |
||
2947 | } |
||
2948 | } |
||
2949 | |||
2950 | // Send the email if checkboxed |
||
2951 | if (($new_user || $reminder == 1) && $sendmail) { |
||
2952 | // Make a change for absolute url |
||
2953 | if (isset($invitation_text)) { |
||
2954 | $invitation_text = api_html_entity_decode($invitation_text, ENT_QUOTES); |
||
2955 | $invitation_text = str_replace('src="../../', 'src="'.api_get_path(WEB_PATH), $invitation_text); |
||
2956 | $invitation_text = trim(stripslashes($invitation_text)); |
||
2957 | } |
||
2958 | self::send_invitation_mail( |
||
2959 | $value, |
||
2960 | $invitation_code, |
||
2961 | $invitation_title, |
||
2962 | $invitation_text, |
||
2963 | $hideLink |
||
2964 | ); |
||
2965 | $counter++; |
||
2966 | } |
||
2967 | } |
||
2968 | |||
2969 | return $counter; // Number of invitations sent |
||
2970 | } |
||
2971 | |||
2972 | /** |
||
2973 | * @param $params |
||
2974 | * |
||
2975 | * @return bool|int |
||
2976 | */ |
||
2977 | public static function save_invitation($params) |
||
2978 | { |
||
2979 | // Database table to store the invitations data |
||
2980 | $table = Database::get_course_table(TABLE_SURVEY_INVITATION); |
||
2981 | if (!empty($params['c_id']) && |
||
2982 | (!empty($params['user']) || !empty($params['group_id'])) && |
||
2983 | !empty($params['survey_code']) |
||
2984 | ) { |
||
2985 | if (!isset($params['survey_invitation_id'])) { |
||
2986 | $params['survey_invitation_id'] = 0; |
||
2987 | } |
||
2988 | if (!isset($params['answered'])) { |
||
2989 | $params['answered'] = 0; |
||
2990 | } |
||
2991 | if (!isset($params['group_id'])) { |
||
2992 | $params['group_id'] = 0; |
||
2993 | } |
||
2994 | $insertId = Database::insert($table, $params); |
||
2995 | if ($insertId) { |
||
2996 | $sql = "UPDATE $table |
||
2997 | SET survey_invitation_id = $insertId |
||
2998 | WHERE iid = $insertId"; |
||
2999 | Database::query($sql); |
||
3000 | } |
||
3001 | |||
3002 | return $insertId; |
||
3003 | } |
||
3004 | |||
3005 | return false; |
||
3006 | } |
||
3007 | |||
3008 | /** |
||
3009 | * @param int $courseId |
||
3010 | * @param int $sessionId |
||
3011 | * @param int $groupId |
||
3012 | * @param string $surveyCode |
||
3013 | * |
||
3014 | * @return int |
||
3015 | */ |
||
3016 | public static function invitationExists($courseId, $sessionId, $groupId, $surveyCode) |
||
3034 | } |
||
3035 | |||
3036 | /** |
||
3037 | * Send the invitation by mail. |
||
3038 | * |
||
3039 | * @param int invitedUser - the userId (course user) or emailaddress of additional user |
||
3040 | * $param string $invitation_code - the unique invitation code for the URL |
||
3041 | */ |
||
3042 | public static function send_invitation_mail( |
||
3043 | $invitedUser, |
||
3044 | $invitation_code, |
||
3045 | $invitation_title, |
||
3046 | $invitation_text, |
||
3047 | $hideLink = false |
||
3048 | ) { |
||
3049 | $_user = api_get_user_info(); |
||
3050 | $_course = api_get_course_info(); |
||
3051 | $sessionId = api_get_session_id(); |
||
3052 | |||
3053 | // Replacing the **link** part with a valid link for the user |
||
3054 | $link = self::generateFillSurveyLink($invitation_code, $_course, $sessionId); |
||
3055 | if ($hideLink) { |
||
3056 | $full_invitation_text = str_replace('**link**', '', $invitation_text); |
||
3057 | } else { |
||
3058 | $text_link = '<a href="'.$link.'">'.get_lang('ClickHereToAnswerTheSurvey')."</a><br />\r\n<br />\r\n" |
||
3059 | .get_lang('OrCopyPasteTheFollowingUrl')." <br /> \r\n <br /> \r\n ".$link; |
||
3060 | |||
3061 | $replace_count = 0; |
||
3062 | $full_invitation_text = api_str_ireplace('**link**', $text_link, $invitation_text, $replace_count); |
||
3063 | if ($replace_count < 1) { |
||
3064 | $full_invitation_text = $full_invitation_text."<br />\r\n<br />\r\n".$text_link; |
||
3065 | } |
||
3066 | } |
||
3067 | |||
3068 | // Sending the mail |
||
3069 | $sender_name = api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS); |
||
3070 | $sender_email = $_user['mail']; |
||
3071 | $sender_user_id = api_get_user_id(); |
||
3072 | |||
3073 | $replyto = []; |
||
3074 | if (api_get_setting('survey_email_sender_noreply') == 'noreply') { |
||
3075 | $noreply = api_get_setting('noreply_email_address'); |
||
3076 | if (!empty($noreply)) { |
||
3077 | $replyto['Reply-to'] = $noreply; |
||
3078 | $sender_name = $noreply; |
||
3079 | $sender_email = $noreply; |
||
3080 | $sender_user_id = null; |
||
3081 | } |
||
3082 | } |
||
3083 | |||
3084 | // Optionally: finding the e-mail of the course user |
||
3085 | if (is_numeric($invitedUser)) { |
||
3086 | MessageManager::send_message( |
||
3087 | $invitedUser, |
||
3088 | $invitation_title, |
||
3089 | $full_invitation_text, |
||
3090 | [], |
||
3091 | [], |
||
3092 | null, |
||
3093 | null, |
||
3094 | null, |
||
3095 | null, |
||
3096 | $sender_user_id, |
||
3097 | true |
||
3098 | ); |
||
3099 | } else { |
||
3100 | $extraParameters = []; |
||
3101 | if (api_get_configuration_value('mail_header_from_custom_course_logo') == true) { |
||
3102 | $extraParameters = ['logo' => CourseManager::getCourseEmailPicture($_course)]; |
||
3103 | } |
||
3104 | |||
3105 | @api_mail_html( |
||
3106 | '', |
||
3107 | $invitedUser, |
||
3108 | $invitation_title, |
||
3109 | $full_invitation_text, |
||
3110 | $sender_name, |
||
3111 | $sender_email, |
||
3112 | $replyto, |
||
3113 | [], |
||
3114 | false, |
||
3115 | $extraParameters, |
||
3116 | '' |
||
3117 | ); |
||
3118 | } |
||
3119 | } |
||
3120 | |||
3121 | /** |
||
3122 | * This function recalculates the number of users who have been invited and updates the survey table with this |
||
3123 | * value. |
||
3124 | * |
||
3125 | * @param string Survey code |
||
3126 | * @param int $courseId |
||
3127 | * @param int $sessionId |
||
3128 | * |
||
3129 | * @return int |
||
3130 | * |
||
3131 | * @author Patrick Cool <[email protected]>, Ghent University |
||
3132 | * |
||
3133 | * @version January 2007 |
||
3134 | */ |
||
3135 | public static function update_count_invited($survey_code, $courseId = 0, $sessionId = 0) |
||
3136 | { |
||
3137 | $survey_code = Database::escape_string($survey_code); |
||
3138 | $courseId = (int) $courseId; |
||
3139 | $sessionId = (int) $sessionId; |
||
3140 | |||
3141 | $courseId = $courseId ?: api_get_course_int_id(); |
||
3142 | $sessionId = $sessionId ?: api_get_session_id(); |
||
3143 | $sessionCondition = api_get_session_condition($sessionId); |
||
3144 | |||
3145 | // Database table definition |
||
3146 | $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION); |
||
3147 | $table_survey = Database::get_course_table(TABLE_SURVEY); |
||
3148 | |||
3149 | // Counting the number of people that are invited |
||
3150 | $sql = "SELECT count(user) as total |
||
3151 | FROM $table_survey_invitation |
||
3152 | WHERE |
||
3153 | c_id = $courseId AND |
||
3154 | survey_code = '".$survey_code."' AND |
||
3155 | user <> '' |
||
3156 | $sessionCondition |
||
3157 | "; |
||
3158 | $result = Database::query($sql); |
||
3159 | $row = Database::fetch_array($result); |
||
3160 | $total_invited = $row['total']; |
||
3161 | |||
3162 | // Updating the field in the survey table |
||
3163 | $sql = "UPDATE $table_survey |
||
3164 | SET invited = '".Database::escape_string($total_invited)."' |
||
3165 | WHERE |
||
3166 | c_id = $courseId AND |
||
3167 | code = '".$survey_code."' |
||
3168 | $sessionCondition |
||
3169 | "; |
||
3170 | Database::query($sql); |
||
3171 | |||
3172 | return $total_invited; |
||
3173 | } |
||
3174 | |||
3175 | /** |
||
3176 | * This function gets all the invited users for a given survey code. |
||
3177 | * |
||
3178 | * @param string Survey code |
||
3179 | * @param string optional - course database |
||
3180 | * |
||
3181 | * @return array Array containing the course users and additional users (non course users) |
||
3182 | * |
||
3183 | * @todo consider making $defaults['additional_users'] also an array |
||
3184 | * |
||
3185 | * @author Patrick Cool <[email protected]>, Ghent University |
||
3186 | * @author Julio Montoya, adding c_id fixes - Dec 2012 |
||
3187 | * |
||
3188 | * @version January 2007 |
||
3189 | */ |
||
3190 | public static function get_invited_users($survey_code, $course_code = '', $session_id = 0) |
||
3191 | { |
||
3192 | $session_id = (int) $session_id; |
||
3193 | $survey_code = Database::escape_string($survey_code); |
||
3194 | $course_code = Database::escape_string($course_code); |
||
3195 | |||
3196 | $course_id = api_get_course_int_id(); |
||
3197 | |||
3198 | if (!empty($course_code)) { |
||
3199 | $course_info = api_get_course_info($course_code); |
||
3200 | if ($course_info) { |
||
3201 | $course_id = $course_info['real_id']; |
||
3202 | } |
||
3203 | } |
||
3204 | |||
3205 | if (empty($session_id)) { |
||
3206 | $session_id = api_get_session_id(); |
||
3207 | } |
||
3208 | |||
3209 | $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION); |
||
3210 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
3211 | |||
3212 | // Selecting all the invitations of this survey AND the additional emailaddresses (the left join) |
||
3213 | $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname' : ' ORDER BY lastname, firstname'; |
||
3214 | $sql = "SELECT user, group_id |
||
3215 | FROM $table_survey_invitation as table_invitation |
||
3216 | WHERE |
||
3217 | table_invitation.c_id = $course_id AND |
||
3218 | survey_code='".$survey_code."' AND |
||
3219 | session_id = $session_id |
||
3220 | "; |
||
3221 | |||
3222 | $defaults = []; |
||
3223 | $defaults['course_users'] = []; |
||
3224 | $defaults['additional_users'] = []; // Textarea |
||
3225 | $defaults['users'] = []; // user and groups |
||
3226 | |||
3227 | $result = Database::query($sql); |
||
3228 | while ($row = Database::fetch_array($result)) { |
||
3229 | if (is_numeric($row['user'])) { |
||
3230 | $defaults['course_users'][] = $row['user']; |
||
3231 | $defaults['users'][] = 'USER:'.$row['user']; |
||
3232 | } else { |
||
3233 | if (!empty($row['user'])) { |
||
3234 | $defaults['additional_users'][] = $row['user']; |
||
3235 | } |
||
3236 | } |
||
3237 | |||
3238 | if (isset($row['group_id']) && !empty($row['group_id'])) { |
||
3239 | $defaults['users'][] = 'GROUP:'.$row['group_id']; |
||
3240 | } |
||
3241 | } |
||
3242 | |||
3243 | if (!empty($defaults['course_users'])) { |
||
3244 | $user_ids = implode("','", $defaults['course_users']); |
||
3245 | $sql = "SELECT user_id FROM $table_user WHERE user_id IN ('$user_ids') $order_clause"; |
||
3246 | $result = Database::query($sql); |
||
3247 | $fixed_users = []; |
||
3248 | while ($row = Database::fetch_array($result)) { |
||
3249 | $fixed_users[] = $row['user_id']; |
||
3250 | } |
||
3251 | $defaults['course_users'] = $fixed_users; |
||
3252 | } |
||
3253 | |||
3254 | if (!empty($defaults['additional_users'])) { |
||
3255 | $defaults['additional_users'] = implode(';', $defaults['additional_users']); |
||
3256 | } |
||
3257 | |||
3258 | return $defaults; |
||
3259 | } |
||
3260 | |||
3261 | /** |
||
3262 | * Get all the invitations. |
||
3263 | * |
||
3264 | * @param string Survey code |
||
3265 | * |
||
3266 | * @return array Database rows matching the survey code |
||
3267 | * |
||
3268 | * @author Patrick Cool <[email protected]>, Ghent University |
||
3269 | * |
||
3270 | * @version September 2007 |
||
3271 | */ |
||
3272 | public static function get_invitations($survey_code) |
||
3273 | { |
||
3274 | $course_id = api_get_course_int_id(); |
||
3275 | $sessionId = api_get_session_id(); |
||
3276 | // Database table definition |
||
3277 | $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION); |
||
3278 | |||
3279 | $sql = "SELECT * FROM $table_survey_invitation |
||
3280 | WHERE |
||
3281 | c_id = $course_id AND |
||
3282 | session_id = $sessionId AND |
||
3283 | survey_code = '".Database::escape_string($survey_code)."'"; |
||
3284 | $result = Database::query($sql); |
||
3285 | $return = []; |
||
3286 | while ($row = Database::fetch_array($result)) { |
||
3287 | $return[$row['user']] = $row; |
||
3288 | } |
||
3289 | |||
3290 | return $return; |
||
3291 | } |
||
3292 | |||
3293 | /** |
||
3294 | * This function displays the form for searching a survey. |
||
3295 | * |
||
3296 | * @author Patrick Cool <[email protected]>, Ghent University |
||
3297 | * |
||
3298 | * @version January 2007 |
||
3299 | * |
||
3300 | * @todo consider moving this to surveymanager.inc.lib.php |
||
3301 | */ |
||
3302 | public static function display_survey_search_form() |
||
3303 | { |
||
3304 | $url = api_get_path(WEB_CODE_PATH).'survey/survey_list.php?search=advanced&'.api_get_cidreq(); |
||
3305 | $form = new FormValidator('search', 'get', $url); |
||
3306 | $form->addHeader(get_lang('SearchASurvey')); |
||
3307 | $form->addText('keyword_title', get_lang('Title')); |
||
3308 | $form->addText('keyword_code', get_lang('Code')); |
||
3309 | $form->addSelectLanguage('keyword_language', get_lang('Language')); |
||
3310 | $form->addHidden('cidReq', api_get_course_id()); |
||
3311 | $form->addButtonSearch(get_lang('Search'), 'do_search'); |
||
3312 | $form->display(); |
||
3313 | } |
||
3314 | |||
3315 | /** |
||
3316 | * Show table only visible by DRH users. |
||
3317 | */ |
||
3318 | public static function displaySurveyListForDrh() |
||
3319 | { |
||
3320 | $parameters = []; |
||
3321 | $parameters['cidReq'] = api_get_course_id(); |
||
3322 | |||
3323 | // Create a sortable table with survey-data |
||
3324 | $table = new SortableTable( |
||
3325 | 'surveys', |
||
3326 | 'get_number_of_surveys', |
||
3327 | 'get_survey_data_drh', |
||
3328 | 2 |
||
3329 | ); |
||
3330 | $table->set_additional_parameters($parameters); |
||
3331 | $table->set_header(0, '', false); |
||
3332 | $table->set_header(1, get_lang('SurveyName')); |
||
3333 | $table->set_header(2, get_lang('SurveyCode')); |
||
3334 | $table->set_header(3, get_lang('NumberOfQuestions')); |
||
3335 | $table->set_header(4, get_lang('Author')); |
||
3336 | $table->set_header(5, get_lang('AvailableFrom')); |
||
3337 | $table->set_header(6, get_lang('AvailableUntil')); |
||
3338 | $table->set_header(7, get_lang('Invite')); |
||
3339 | $table->set_header(8, get_lang('Anonymous')); |
||
3340 | |||
3341 | if (api_get_configuration_value('allow_mandatory_survey')) { |
||
3342 | $table->set_header(9, get_lang('IsMandatory')); |
||
3343 | $table->set_header(10, get_lang('Modify'), false, 'width="150"'); |
||
3344 | $table->set_column_filter(9, 'anonymous_filter'); |
||
3345 | $table->set_column_filter(10, 'modify_filter_drh'); |
||
3346 | } else { |
||
3347 | $table->set_header(9, get_lang('Modify'), false, 'width="150"'); |
||
3348 | $table->set_column_filter(9, 'modify_filter_drh'); |
||
3349 | } |
||
3350 | |||
3351 | $table->set_column_filter(8, 'anonymous_filter'); |
||
3352 | $table->display(); |
||
3353 | } |
||
3354 | |||
3355 | /** |
||
3356 | * This function displays the sortable table with all the surveys. |
||
3357 | * |
||
3358 | * @author Patrick Cool <[email protected]>, Ghent University |
||
3359 | * |
||
3360 | * @version January 2007 |
||
3361 | */ |
||
3362 | public static function display_survey_list() |
||
3363 | { |
||
3364 | $parameters = []; |
||
3365 | $parameters['cidReq'] = api_get_course_id(); |
||
3366 | if (isset($_GET['do_search']) && $_GET['do_search']) { |
||
3367 | $message = get_lang('DisplaySearchResults').'<br />'; |
||
3368 | $message .= '<a href="'.api_get_self().'?'.api_get_cidreq().'">'.get_lang('DisplayAll').'</a>'; |
||
3369 | echo Display::return_message($message, 'normal', false); |
||
3370 | } |
||
3371 | |||
3372 | // Create a sortable table with survey-data |
||
3373 | $table = new SortableTable( |
||
3374 | 'surveys', |
||
3375 | 'get_number_of_surveys', |
||
3376 | 'get_survey_data', |
||
3377 | 2 |
||
3378 | ); |
||
3379 | $table->set_additional_parameters($parameters); |
||
3380 | $table->set_header(0, '', false); |
||
3381 | $table->set_header(1, get_lang('SurveyName')); |
||
3382 | $table->set_header(2, get_lang('SurveyCode')); |
||
3383 | $table->set_header(3, get_lang('NumberOfQuestions')); |
||
3384 | $table->set_header(4, get_lang('Author')); |
||
3385 | $table->set_header(5, get_lang('AvailableFrom')); |
||
3386 | $table->set_header(6, get_lang('AvailableUntil')); |
||
3387 | $table->set_header(7, get_lang('Invite')); |
||
3388 | $table->set_header(8, get_lang('Anonymous')); |
||
3389 | |||
3390 | if (api_get_configuration_value('allow_mandatory_survey')) { |
||
3391 | $table->set_header(9, get_lang('IsMandatory')); |
||
3392 | $table->set_header(10, get_lang('Modify'), false, 'width="150"'); |
||
3393 | $table->set_column_filter(8, 'anonymous_filter'); |
||
3394 | $table->set_column_filter(10, 'modify_filter'); |
||
3395 | } else { |
||
3396 | $table->set_header(9, get_lang('Modify'), false, 'width="150"'); |
||
3397 | $table->set_column_filter(9, 'modify_filter'); |
||
3398 | } |
||
3399 | |||
3400 | $table->set_column_filter(8, 'anonymous_filter'); |
||
3401 | $actions = [ |
||
3402 | 'export_all' => get_lang('ExportResults'), |
||
3403 | 'export_by_class' => get_lang('ExportByClass'), |
||
3404 | 'send_to_tutors' => get_lang('SendToGroupTutors'), |
||
3405 | 'multiplicate_by_class' => get_lang('MultiplicateQuestionsByClass'), |
||
3406 | 'multiplicate_by_user' => get_lang('MultiplicateQuestionsByUser'), |
||
3407 | 'delete' => get_lang('DeleteSurvey'), |
||
3408 | ]; |
||
3409 | $table->set_form_actions($actions); |
||
3410 | |||
3411 | $form = new FormValidator( |
||
3412 | 'survey', |
||
3413 | 'post', |
||
3414 | null, |
||
3415 | null, |
||
3416 | ['class' => 'form-vertical'] |
||
3417 | ); |
||
3418 | $form->addElement( |
||
3419 | 'radio', |
||
3420 | 'type', |
||
3421 | null, |
||
3422 | get_lang('MultiplicateQuestionsByClass'), |
||
3423 | 'by_class', |
||
3424 | ['id' => 'by_class'] |
||
3425 | ); |
||
3426 | $form->addElement( |
||
3427 | 'radio', |
||
3428 | 'type', |
||
3429 | null, |
||
3430 | get_lang('MultiplicateQuestionsByUser'), |
||
3431 | 'by_user', |
||
3432 | ['id' => 'by_user'] |
||
3433 | ); |
||
3434 | $form->setDefaults(['type' => 'by_class']); |
||
3435 | $formToString = $form->returnForm(); |
||
3436 | |||
3437 | echo '<div id="dialog-confirm">'.$formToString.'</div>'; |
||
3438 | $table->display(); |
||
3439 | } |
||
3440 | |||
3441 | /** |
||
3442 | * Survey list for coach. |
||
3443 | */ |
||
3444 | public static function display_survey_list_for_coach() |
||
3445 | { |
||
3446 | $parameters = []; |
||
3447 | $parameters['cidReq'] = api_get_course_id(); |
||
3448 | if (isset($_GET['do_search'])) { |
||
3449 | $message = get_lang('DisplaySearchResults').'<br />'; |
||
3450 | $message .= '<a href="'.api_get_self().'?'.api_get_cidreq().'">'.get_lang('DisplayAll').'</a>'; |
||
3451 | echo Display::return_message($message, 'normal', false); |
||
3452 | } |
||
3453 | |||
3454 | // Create a sortable table with survey-data |
||
3455 | $table = new SortableTable( |
||
3456 | 'surveys_coach', |
||
3457 | 'get_number_of_surveys_for_coach', |
||
3458 | 'get_survey_data_for_coach', |
||
3459 | 2 |
||
3460 | ); |
||
3461 | $table->set_additional_parameters($parameters); |
||
3462 | $table->set_header(0, '', false); |
||
3463 | $table->set_header(1, get_lang('SurveyName')); |
||
3464 | $table->set_header(2, get_lang('SurveyCode')); |
||
3465 | $table->set_header(3, get_lang('NumberOfQuestions')); |
||
3466 | $table->set_header(4, get_lang('Author')); |
||
3467 | $table->set_header(5, get_lang('AvailableFrom')); |
||
3468 | $table->set_header(6, get_lang('AvailableUntil')); |
||
3469 | $table->set_header(7, get_lang('Invite')); |
||
3470 | $table->set_header(8, get_lang('Anonymous')); |
||
3471 | |||
3472 | if (api_get_configuration_value('allow_mandatory_survey')) { |
||
3473 | $table->set_header(9, get_lang('Modify'), false, 'width="130"'); |
||
3474 | $table->set_header(10, get_lang('Modify'), false, 'width="130"'); |
||
3475 | $table->set_column_filter(8, 'anonymous_filter'); |
||
3476 | $table->set_column_filter(10, 'modify_filter_for_coach'); |
||
3477 | } else { |
||
3478 | $table->set_header(9, get_lang('Modify'), false, 'width="130"'); |
||
3479 | $table->set_column_filter(9, 'modify_filter_for_coach'); |
||
3480 | } |
||
3481 | |||
3482 | $table->set_column_filter(8, 'anonymous_filter'); |
||
3483 | $table->display(); |
||
3484 | } |
||
3485 | |||
3486 | /** |
||
3487 | * Check if the hide_survey_edition configurations setting is enabled. |
||
3488 | * |
||
3489 | * @param string $surveyCode |
||
3490 | * |
||
3491 | * @return bool |
||
3492 | */ |
||
3493 | public static function checkHideEditionToolsByCode($surveyCode) |
||
3494 | { |
||
3495 | $hideSurveyEdition = api_get_configuration_value('hide_survey_edition'); |
||
3496 | |||
3497 | if (false === $hideSurveyEdition) { |
||
3498 | return false; |
||
3499 | } |
||
3500 | |||
3501 | if ('*' === $hideSurveyEdition['codes']) { |
||
3502 | return true; |
||
3503 | } |
||
3504 | |||
3505 | if (in_array($surveyCode, $hideSurveyEdition['codes'])) { |
||
3506 | return true; |
||
3507 | } |
||
3508 | |||
3509 | return false; |
||
3510 | } |
||
3511 | |||
3512 | /** |
||
3513 | * This function changes the modify column of the sortable table. |
||
3514 | * |
||
3515 | * @param int $survey_id the id of the survey |
||
3516 | * @param bool $drh |
||
3517 | * |
||
3518 | * @return string html code that are the actions that can be performed on any survey |
||
3519 | * |
||
3520 | * @author Patrick Cool <[email protected]>, Ghent University |
||
3521 | * |
||
3522 | * @version January 2007 |
||
3523 | */ |
||
3524 | public static function modify_filter($survey_id, $drh = false) |
||
3525 | { |
||
3526 | /** @var CSurvey $survey */ |
||
3527 | $survey = Database::getManager()->find('ChamiloCourseBundle:CSurvey', $survey_id); |
||
3528 | $hideSurveyEdition = self::checkHideEditionToolsByCode($survey->getCode()); |
||
3529 | |||
3530 | if ($hideSurveyEdition) { |
||
3531 | return ''; |
||
3532 | } |
||
3533 | |||
3534 | if (empty($survey)) { |
||
3535 | return ''; |
||
3536 | } |
||
3537 | |||
3538 | $survey_id = $survey->getSurveyId(); |
||
3539 | $actions = []; |
||
3540 | $hideReportingButton = api_get_configuration_value('hide_survey_reporting_button'); |
||
3541 | $codePath = api_get_path(WEB_CODE_PATH); |
||
3542 | $params = []; |
||
3543 | parse_str(api_get_cidreq(), $params); |
||
3544 | |||
3545 | $reportingLink = Display::url( |
||
3546 | Display::return_icon('statistics.png', get_lang('Reporting')), |
||
3547 | $codePath.'survey/reporting.php?'.http_build_query($params + ['survey_id' => $survey_id]) |
||
3548 | ); |
||
3549 | |||
3550 | if ($drh) { |
||
3551 | return $hideReportingButton ? '-' : $reportingLink; |
||
3552 | } |
||
3553 | |||
3554 | $type = $survey->getSurveyType(); |
||
3555 | |||
3556 | // Coach can see that only if the survey is in his session |
||
3557 | if (api_is_allowed_to_edit() || api_is_element_in_the_session(TOOL_SURVEY, $survey_id)) { |
||
3558 | $configUrl = $codePath.'survey/create_new_survey.php?'. |
||
3559 | http_build_query($params + ['action' => 'edit', 'survey_id' => $survey_id]); |
||
3560 | $editUrl = $codePath.'survey/survey.php?'. |
||
3561 | http_build_query($params + ['survey_id' => $survey_id]); |
||
3562 | if (3 == $survey->getSurveyType()) { |
||
3563 | $configUrl = $codePath.'survey/edit_meeting.php?'. |
||
3564 | http_build_query($params + ['action' => 'edit', 'survey_id' => $survey_id]); |
||
3565 | } |
||
3566 | |||
3567 | $actions[] = Display::url( |
||
3568 | Display::return_icon('edit.png', get_lang('EditQuestions')), |
||
3569 | $editUrl |
||
3570 | ); |
||
3571 | $actions[] = Display::url( |
||
3572 | Display::return_icon('settings.png', get_lang('Configure')), |
||
3573 | $configUrl |
||
3574 | ); |
||
3575 | |||
3576 | if (SurveyManager::survey_generation_hash_available()) { |
||
3577 | $actions[] = Display::url( |
||
3578 | Display::return_icon('new_link.png', get_lang('GenerateSurveyAccessLink')), |
||
3579 | $codePath.'survey/generate_link.php?'.http_build_query($params + ['survey_id' => $survey_id]) |
||
3580 | ); |
||
3581 | } |
||
3582 | |||
3583 | if ($type != 3) { |
||
3584 | $actions[] = Display::url( |
||
3585 | Display::return_icon('backup.png', get_lang('CopySurvey')), |
||
3586 | $codePath.'survey/copy_survey.php?'.http_build_query($params + ['survey_id' => $survey_id]) |
||
3587 | ); |
||
3588 | |||
3589 | $actions[] = Display::url( |
||
3590 | Display::return_icon('copy.png', get_lang('DuplicateSurvey')), |
||
3591 | $codePath.'survey/survey_list.php?' |
||
3592 | .http_build_query($params + ['action' => 'copy_survey', 'survey_id' => $survey_id]) |
||
3593 | ); |
||
3594 | |||
3595 | $actions[] = Display::url( |
||
3596 | Display::return_icon('multiplicate_survey.png', get_lang('MultiplicateQuestions')), |
||
3597 | $codePath.'survey/survey_list.php?' |
||
3598 | .http_build_query($params + ['action' => 'multiplicate', 'survey_id' => $survey_id]), |
||
3599 | ['survey_id' => $survey_id, 'class' => 'multiplicate_popup'] |
||
3600 | ); |
||
3601 | |||
3602 | $actions[] = Display::url( |
||
3603 | Display::return_icon('multiplicate_survey_na.png', get_lang('RemoveMultiplicateQuestions')), |
||
3604 | $codePath.'survey/survey_list.php?' |
||
3605 | .http_build_query($params + ['action' => 'remove_multiplicate', 'survey_id' => $survey_id]) |
||
3606 | ); |
||
3607 | |||
3608 | $warning = addslashes(api_htmlentities(get_lang('EmptySurvey').'?', ENT_QUOTES)); |
||
3609 | $actions[] = Display::url( |
||
3610 | Display::return_icon('clean.png', get_lang('EmptySurvey')), |
||
3611 | $codePath.'survey/survey_list.php?' |
||
3612 | .http_build_query($params + ['action' => 'empty', 'survey_id' => $survey_id]), |
||
3613 | [ |
||
3614 | 'onclick' => "javascript: if (!confirm('".$warning."')) return false;", |
||
3615 | ] |
||
3616 | ); |
||
3617 | } |
||
3618 | } |
||
3619 | |||
3620 | if (3 != $type) { |
||
3621 | $actions[] = Display::url( |
||
3622 | Display::return_icon('preview_view.png', get_lang('Preview')), |
||
3623 | $codePath.'survey/preview.php?'.http_build_query($params + ['survey_id' => $survey_id]) |
||
3624 | ); |
||
3625 | } |
||
3626 | |||
3627 | $actions[] = Display::url( |
||
3628 | Display::return_icon('mail_send.png', get_lang('Publish')), |
||
3629 | $codePath.'survey/survey_invite.php?'.http_build_query($params + ['survey_id' => $survey_id]) |
||
3630 | ); |
||
3631 | |||
3632 | $extraFieldValue = new ExtraFieldValue('survey'); |
||
3633 | $groupData = $extraFieldValue->get_values_by_handler_and_field_variable($survey_id, 'group_id'); |
||
3634 | if ($groupData && !empty($groupData['value'])) { |
||
3635 | $actions[] = Display::url( |
||
3636 | Display::return_icon('teacher.png', get_lang('SendToGroupTutors')), |
||
3637 | $codePath.'survey/survey_list.php?action=send_to_tutors&'.http_build_query($params + ['survey_id' => $survey_id]) |
||
3638 | ); |
||
3639 | } |
||
3640 | |||
3641 | if (3 != $type) { |
||
3642 | $actions[] = $hideReportingButton ? null : $reportingLink; |
||
3643 | } |
||
3644 | |||
3645 | if (api_is_allowed_to_edit() || |
||
3646 | api_is_element_in_the_session(TOOL_SURVEY, $survey_id) |
||
3647 | ) { |
||
3648 | $actions[] = self::getAdditionalTeacherActions($survey_id); |
||
3649 | $warning = addslashes(api_htmlentities(get_lang('DeleteSurvey').'?', ENT_QUOTES)); |
||
3650 | $actions[] = Display::url( |
||
3651 | Display::return_icon('delete.png', get_lang('Delete')), |
||
3652 | $codePath.'survey/survey_list.php?' |
||
3653 | .http_build_query($params + ['action' => 'delete', 'survey_id' => $survey_id]), |
||
3654 | [ |
||
3655 | 'onclick' => "javascript: if (!confirm('".$warning."')) return false;", |
||
3656 | ] |
||
3657 | ); |
||
3658 | } |
||
3659 | |||
3660 | return implode(PHP_EOL, $actions); |
||
3661 | } |
||
3662 | |||
3663 | /** |
||
3664 | * Get the additional actions added in survey_additional_teacher_modify_actions configuration. |
||
3665 | * |
||
3666 | * @param int $surveyId |
||
3667 | * @param int $iconSize |
||
3668 | * |
||
3669 | * @return string |
||
3670 | */ |
||
3671 | public static function getAdditionalTeacherActions($surveyId, $iconSize = ICON_SIZE_SMALL) |
||
3672 | { |
||
3673 | $additionalActions = api_get_configuration_value('survey_additional_teacher_modify_actions') ?: []; |
||
3674 | |||
3675 | if (empty($additionalActions)) { |
||
3676 | return ''; |
||
3677 | } |
||
3678 | |||
3679 | $actions = []; |
||
3680 | foreach ($additionalActions as $additionalAction) { |
||
3681 | $actions[] = call_user_func( |
||
3682 | $additionalAction, |
||
3683 | ['survey_id' => $surveyId, 'icon_size' => $iconSize] |
||
3684 | ); |
||
3685 | } |
||
3686 | |||
3687 | return implode(PHP_EOL, $actions); |
||
3688 | } |
||
3689 | |||
3690 | /** |
||
3691 | * @param int $survey_id |
||
3692 | * |
||
3693 | * @return string |
||
3694 | */ |
||
3695 | public static function modify_filter_for_coach($survey_id) |
||
3696 | { |
||
3697 | $survey_id = (int) $survey_id; |
||
3698 | $actions = []; |
||
3699 | $codePath = api_get_path(WEB_CODE_PATH); |
||
3700 | $params = []; |
||
3701 | parse_str(api_get_cidreq(), $params); |
||
3702 | $actions[] = Display::url( |
||
3703 | Display::return_icon('preview_view.png', get_lang('Preview')), |
||
3704 | $codePath.'survey/preview.php?'.http_build_query($params + ['survey_id' => $survey_id]) |
||
3705 | ); |
||
3706 | $actions[] = Display::url( |
||
3707 | Display::return_icon('mail_send.png', get_lang('Publish')), |
||
3708 | $codePath.'survey/survey_invite.php?'.http_build_query($params + ['survey_id' => $survey_id]) |
||
3709 | ); |
||
3710 | $warning = addslashes(api_htmlentities(get_lang('EmptySurvey').'?', ENT_QUOTES)); |
||
3711 | $actions[] = Display::url( |
||
3712 | Display::return_icon('clean.png', get_lang('EmptySurvey')), |
||
3713 | $codePath.'survey/survey_list.php?' |
||
3714 | .http_build_query($params + ['action' => 'empty', 'survey_id' => $survey_id]), |
||
3715 | [ |
||
3716 | 'onclick' => "javascript: if(!confirm('".$warning."')) return false;", |
||
3717 | ] |
||
3718 | ); |
||
3719 | |||
3720 | return implode(PHP_EOL, $actions); |
||
3721 | } |
||
3722 | |||
3723 | /** |
||
3724 | * Returns "yes" when given parameter is one, "no" for any other value. |
||
3725 | * |
||
3726 | * @param int Whether anonymous or not |
||
3727 | * |
||
3728 | * @return string "Yes" or "No" in the current language |
||
3729 | */ |
||
3730 | public static function anonymous_filter($anonymous) |
||
3731 | { |
||
3732 | if (1 == $anonymous) { |
||
3733 | return get_lang('Yes'); |
||
3734 | } else { |
||
3735 | return get_lang('No'); |
||
3736 | } |
||
3737 | } |
||
3738 | |||
3739 | /** |
||
3740 | * This function handles the search restriction for the SQL statements. |
||
3741 | * |
||
3742 | * @return string Part of a SQL statement or false on error |
||
3743 | * |
||
3744 | * @author Patrick Cool <[email protected]>, Ghent University |
||
3745 | * |
||
3746 | * @version January 2007 |
||
3747 | */ |
||
3748 | public static function survey_search_restriction() |
||
3749 | { |
||
3750 | if (isset($_GET['do_search'])) { |
||
3751 | if ($_GET['keyword_title'] != '') { |
||
3752 | $search_term[] = 'title like "%" \''.Database::escape_string($_GET['keyword_title']).'\' "%"'; |
||
3753 | } |
||
3754 | if ($_GET['keyword_code'] != '') { |
||
3755 | $search_term[] = 'code =\''.Database::escape_string($_GET['keyword_code']).'\''; |
||
3756 | } |
||
3757 | if ($_GET['keyword_language'] != '%') { |
||
3758 | $search_term[] = 'lang =\''.Database::escape_string($_GET['keyword_language']).'\''; |
||
3759 | } |
||
3760 | $my_search_term = ($search_term == null) ? [] : $search_term; |
||
3761 | $search_restriction = implode(' AND ', $my_search_term); |
||
3762 | |||
3763 | return $search_restriction; |
||
3764 | } else { |
||
3765 | return false; |
||
3766 | } |
||
3767 | } |
||
3768 | |||
3769 | /** |
||
3770 | * This function calculates the total number of surveys. |
||
3771 | * |
||
3772 | * @return int Total number of surveys |
||
3773 | * |
||
3774 | * @author Patrick Cool <[email protected]>, Ghent University |
||
3775 | * |
||
3776 | * @version January 2007 |
||
3777 | */ |
||
3778 | public static function get_number_of_surveys() |
||
3779 | { |
||
3780 | $table_survey = Database::get_course_table(TABLE_SURVEY); |
||
3781 | $course_id = api_get_course_int_id(); |
||
3782 | |||
3783 | $search_restriction = self::survey_search_restriction(); |
||
3784 | if ($search_restriction) { |
||
3785 | $search_restriction = 'WHERE c_id = '.$course_id.' AND '.$search_restriction; |
||
3786 | } else { |
||
3787 | $search_restriction = "WHERE c_id = $course_id"; |
||
3788 | } |
||
3789 | // To list the surveys base too |
||
3790 | $showSurveysBase = api_get_configuration_value('show_surveys_base_in_sessions'); |
||
3791 | $sessionCondition = api_get_session_condition(api_get_session_id(), true, $showSurveysBase); |
||
3792 | |||
3793 | $sql = "SELECT count(survey_id) AS total_number_of_items |
||
3794 | FROM $table_survey $search_restriction $sessionCondition"; |
||
3795 | $res = Database::query($sql); |
||
3796 | $obj = Database::fetch_object($res); |
||
3797 | |||
3798 | return $obj->total_number_of_items; |
||
3799 | } |
||
3800 | |||
3801 | /** |
||
3802 | * @return int |
||
3803 | */ |
||
3804 | public static function get_number_of_surveys_for_coach() |
||
3805 | { |
||
3806 | $survey_tree = new SurveyTree(); |
||
3807 | |||
3808 | return count($survey_tree->surveylist); |
||
3809 | } |
||
3810 | |||
3811 | /** |
||
3812 | * This function gets all the survey data that is to be displayed in the sortable table. |
||
3813 | * |
||
3814 | * @param int $from |
||
3815 | * @param int $number_of_items |
||
3816 | * @param int $column |
||
3817 | * @param string $direction |
||
3818 | * @param bool $isDrh |
||
3819 | * |
||
3820 | * @return array |
||
3821 | * |
||
3822 | * @author Patrick Cool <[email protected]>, Ghent University |
||
3823 | * @author Julio Montoya <[email protected]>, Beeznest - Adding intvals |
||
3824 | * |
||
3825 | * @version January 2007 |
||
3826 | */ |
||
3827 | public static function get_survey_data( |
||
3828 | $from, |
||
3829 | $number_of_items, |
||
3830 | $column, |
||
3831 | $direction, |
||
3832 | $isDrh = false |
||
3833 | ) { |
||
3834 | $table_survey = Database::get_course_table(TABLE_SURVEY); |
||
3835 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
3836 | $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); |
||
3837 | $mandatoryAllowed = api_get_configuration_value('allow_mandatory_survey'); |
||
3838 | $_user = api_get_user_info(); |
||
3839 | $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime'); |
||
3840 | |||
3841 | // Searching |
||
3842 | $search_restriction = self::survey_search_restriction(); |
||
3843 | if ($search_restriction) { |
||
3844 | $search_restriction = ' AND '.$search_restriction; |
||
3845 | } |
||
3846 | $from = (int) $from; |
||
3847 | $number_of_items = (int) $number_of_items; |
||
3848 | $column = (int) $column; |
||
3849 | if (!in_array(strtolower($direction), ['asc', 'desc'])) { |
||
3850 | $direction = 'asc'; |
||
3851 | } |
||
3852 | |||
3853 | // To list the surveys base too |
||
3854 | $showSurveysBase = api_get_configuration_value('show_surveys_base_in_sessions'); |
||
3855 | $sessionId = api_get_session_id(); |
||
3856 | $sessionCondition = api_get_session_condition($sessionId, true, $showSurveysBase); |
||
3857 | $course_id = api_get_course_int_id(); |
||
3858 | |||
3859 | $sql = " |
||
3860 | SELECT |
||
3861 | survey.survey_id AS col0, |
||
3862 | survey.title AS col1, |
||
3863 | survey.code AS col2, |
||
3864 | count(survey_question.question_id) AS col3, " |
||
3865 | .(api_is_western_name_order() |
||
3866 | ? "CONCAT(user.firstname, ' ', user.lastname)" |
||
3867 | : "CONCAT(user.lastname, ' ', user.firstname)") |
||
3868 | ." AS col4, |
||
3869 | survey.avail_from AS col5, |
||
3870 | survey.avail_till AS col6, |
||
3871 | survey.invited AS col7, |
||
3872 | survey.anonymous AS col8, |
||
3873 | survey.iid AS col9, |
||
3874 | survey.session_id AS session_id, |
||
3875 | survey.answered, |
||
3876 | survey.invited, |
||
3877 | survey.survey_type |
||
3878 | FROM $table_survey survey |
||
3879 | LEFT JOIN $table_survey_question survey_question |
||
3880 | ON (survey.survey_id = survey_question.survey_id AND survey_question.c_id = $course_id) |
||
3881 | LEFT JOIN $table_user user |
||
3882 | ON (survey.author = user.id) |
||
3883 | WHERE survey.c_id = $course_id |
||
3884 | $search_restriction |
||
3885 | $sessionCondition |
||
3886 | GROUP BY survey.survey_id |
||
3887 | ORDER BY col$column $direction |
||
3888 | LIMIT $from,$number_of_items |
||
3889 | "; |
||
3890 | $res = Database::query($sql); |
||
3891 | $surveys = []; |
||
3892 | $array = []; |
||
3893 | $efv = new ExtraFieldValue('survey'); |
||
3894 | while ($survey = Database::fetch_array($res)) { |
||
3895 | $array[0] = $survey[0]; |
||
3896 | $survey[1] = Security::remove_XSS($survey[1]); |
||
3897 | |||
3898 | if (self::checkHideEditionToolsByCode($survey['col2'])) { |
||
3899 | $array[1] = $survey[1]; |
||
3900 | } else { |
||
3901 | // Doodle |
||
3902 | if (3 == $survey['survey_type']) { |
||
3903 | $array[1] = Display::url( |
||
3904 | $survey[1], |
||
3905 | api_get_path(WEB_CODE_PATH).'survey/meeting.php?survey_id='.$survey[0].'&'.api_get_cidreq() |
||
3906 | ); |
||
3907 | } else { |
||
3908 | $array[1] = Display::url( |
||
3909 | $survey[1], |
||
3910 | api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey[0].'&'.api_get_cidreq() |
||
3911 | ); |
||
3912 | } |
||
3913 | } |
||
3914 | |||
3915 | // Validation when belonging to a session |
||
3916 | $session_img = api_get_session_image($survey['session_id'], $_user['status']); |
||
3917 | $array[2] = $survey[2].$session_img; |
||
3918 | $array[3] = $survey[3]; |
||
3919 | $array[4] = $survey[4]; |
||
3920 | |||
3921 | // Dates |
||
3922 | $array[5] = ''; |
||
3923 | |||
3924 | if (!empty($survey[5]) && $survey[5] !== '0000-00-00' && $survey[5] !== '0000-00-00 00:00:00') { |
||
3925 | $array[5] = api_convert_and_format_date( |
||
3926 | $survey[5], |
||
3927 | $allowSurveyAvailabilityDatetime ? DATE_TIME_FORMAT_LONG : DATE_FORMAT_LONG |
||
3928 | ); |
||
3929 | } |
||
3930 | |||
3931 | $array[6] = ''; |
||
3932 | if (!empty($survey[6]) && $survey[6] !== '0000-00-00' && $survey[6] !== '0000-00-00 00:00:00') { |
||
3933 | $array[6] = api_convert_and_format_date( |
||
3934 | $survey[6], |
||
3935 | $allowSurveyAvailabilityDatetime ? DATE_TIME_FORMAT_LONG : DATE_FORMAT_LONG |
||
3936 | ); |
||
3937 | } |
||
3938 | |||
3939 | $array[7] = |
||
3940 | Display::url( |
||
3941 | $survey['answered'], |
||
3942 | api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&survey_id='.$survey[0].'&' |
||
3943 | .api_get_cidreq() |
||
3944 | ).' / '. |
||
3945 | Display::url( |
||
3946 | $survey['invited'], |
||
3947 | api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&survey_id='.$survey[0].'&' |
||
3948 | .api_get_cidreq() |
||
3949 | ); |
||
3950 | // Anon |
||
3951 | $array[8] = $survey['col8']; |
||
3952 | if ($mandatoryAllowed) { |
||
3953 | $efvMandatory = $efv->get_values_by_handler_and_field_variable( |
||
3954 | $survey[9], |
||
3955 | 'is_mandatory' |
||
3956 | ); |
||
3957 | |||
3958 | $array[9] = $efvMandatory ? $efvMandatory['value'] : 0; |
||
3959 | // Survey id |
||
3960 | $array[10] = $survey['col9']; |
||
3961 | } else { |
||
3962 | // Survey id |
||
3963 | $array[9] = $survey['col9']; |
||
3964 | } |
||
3965 | |||
3966 | if ($isDrh) { |
||
3967 | $array[1] = $survey[1]; |
||
3968 | $array[7] = strip_tags($array[7]); |
||
3969 | } |
||
3970 | |||
3971 | $surveys[] = $array; |
||
3972 | } |
||
3973 | |||
3974 | return $surveys; |
||
3975 | } |
||
3976 | |||
3977 | /** |
||
3978 | * @param $from |
||
3979 | * @param $number_of_items |
||
3980 | * @param $column |
||
3981 | * @param $direction |
||
3982 | * |
||
3983 | * @return array |
||
3984 | */ |
||
3985 | public static function get_survey_data_for_coach($from, $number_of_items, $column, $direction) |
||
3986 | { |
||
3987 | $mandatoryAllowed = api_get_configuration_value('allow_mandatory_survey'); |
||
3988 | $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime'); |
||
3989 | $survey_tree = new SurveyTree(); |
||
3990 | $last_version_surveys = $survey_tree->surveylist; |
||
3991 | $list = []; |
||
3992 | foreach ($last_version_surveys as &$survey) { |
||
3993 | $list[] = $survey['id']; |
||
3994 | } |
||
3995 | if (count($list) > 0) { |
||
3996 | $list_condition = " AND survey.survey_id IN (".implode(',', $list).") "; |
||
3997 | } else { |
||
3998 | $list_condition = ''; |
||
3999 | } |
||
4000 | |||
4001 | $from = (int) $from; |
||
4002 | $number_of_items = (int) $number_of_items; |
||
4003 | $column = (int) $column; |
||
4004 | if (!in_array(strtolower($direction), ['asc', 'desc'])) { |
||
4005 | $direction = 'asc'; |
||
4006 | } |
||
4007 | |||
4008 | $table_survey = Database::get_course_table(TABLE_SURVEY); |
||
4009 | $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); |
||
4010 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
4011 | $course_id = api_get_course_int_id(); |
||
4012 | $efv = new ExtraFieldValue('survey'); |
||
4013 | |||
4014 | $sql = " |
||
4015 | SELECT |
||
4016 | survey.survey_id AS col0, |
||
4017 | survey.title AS col1, |
||
4018 | survey.code AS col2, |
||
4019 | count(survey_question.question_id) AS col3, |
||
4020 | " |
||
4021 | .(api_is_western_name_order() |
||
4022 | ? "CONCAT(user.firstname, ' ', user.lastname)" |
||
4023 | : "CONCAT(user.lastname, ' ', user.firstname)") |
||
4024 | ." AS col4, |
||
4025 | survey.avail_from AS col5, |
||
4026 | survey.avail_till AS col6, |
||
4027 | CONCAT('<a href=\"survey_invitation.php?view=answered&survey_id=',survey.survey_id,'\">',survey.answered,'</a> / <a href=\"survey_invitation.php?view=invited&survey_id=',survey.survey_id,'\">',survey.invited, '</a>') AS col7, |
||
4028 | survey.anonymous AS col8, |
||
4029 | survey.survey_id AS col9 |
||
4030 | FROM $table_survey survey |
||
4031 | LEFT JOIN $table_survey_question survey_question |
||
4032 | ON (survey.survey_id = survey_question.survey_id AND survey.c_id = survey_question.c_id), |
||
4033 | $table_user user |
||
4034 | WHERE survey.author = user.id AND survey.c_id = $course_id $list_condition |
||
4035 | "; |
||
4036 | $sql .= ' GROUP BY survey.survey_id'; |
||
4037 | $sql .= " ORDER BY col$column $direction "; |
||
4038 | $sql .= " LIMIT $from,$number_of_items"; |
||
4039 | |||
4040 | $res = Database::query($sql); |
||
4041 | $surveys = []; |
||
4042 | while ($survey = Database::fetch_array($res)) { |
||
4043 | $survey['col5'] = api_convert_and_format_date( |
||
4044 | $survey['col5'], |
||
4045 | $allowSurveyAvailabilityDatetime ? DATE_TIME_FORMAT_LONG : DATE_FORMAT_LONG |
||
4046 | ); |
||
4047 | $survey['col6'] = api_convert_and_format_date( |
||
4048 | $survey['col6'], |
||
4049 | $allowSurveyAvailabilityDatetime ? DATE_TIME_FORMAT_LONG : DATE_FORMAT_LONG |
||
4050 | ); |
||
4051 | |||
4052 | if ($mandatoryAllowed) { |
||
4053 | $survey['col10'] = $survey['col9']; |
||
4054 | $efvMandatory = $efv->get_values_by_handler_and_field_variable( |
||
4055 | $survey['col9'], |
||
4056 | 'is_mandatory' |
||
4057 | ); |
||
4058 | $survey['col9'] = $efvMandatory['value']; |
||
4059 | } |
||
4060 | $surveys[] = $survey; |
||
4061 | } |
||
4062 | |||
4063 | return $surveys; |
||
4064 | } |
||
4065 | |||
4066 | /** |
||
4067 | * Display all the active surveys for the given course user. |
||
4068 | * |
||
4069 | * @param int $user_id |
||
4070 | * |
||
4071 | * @author Patrick Cool <[email protected]>, Ghent University |
||
4072 | * |
||
4073 | * @version April 2007 |
||
4074 | */ |
||
4075 | public static function getSurveyList($user_id) |
||
4076 | { |
||
4077 | $_course = api_get_course_info(); |
||
4078 | $course_id = $_course['real_id']; |
||
4079 | $user_id = (int) $user_id; |
||
4080 | $sessionId = api_get_session_id(); |
||
4081 | $mandatoryAllowed = api_get_configuration_value('allow_mandatory_survey'); |
||
4082 | $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime'); |
||
4083 | |||
4084 | $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION); |
||
4085 | $table_survey = Database::get_course_table(TABLE_SURVEY); |
||
4086 | |||
4087 | echo '<table id="list-survey" class="table ">'; |
||
4088 | echo '<thead>'; |
||
4089 | echo '<tr>'; |
||
4090 | echo ' <th>'.get_lang('SurveyName').'</th>'; |
||
4091 | echo ' <th class="text-center">'.get_lang('Anonymous').'</th>'; |
||
4092 | if ($mandatoryAllowed) { |
||
4093 | echo '<th class="text-center">'.get_lang('IsMandatory').'</th>'; |
||
4094 | } |
||
4095 | echo '</tr>'; |
||
4096 | echo '</thead>'; |
||
4097 | echo '<tbody>'; |
||
4098 | |||
4099 | /** @var \DateTime $now */ |
||
4100 | $now = api_get_utc_datetime(null, false, true); |
||
4101 | $filterDate = $allowSurveyAvailabilityDatetime ? $now->format('Y-m-d H:i') : $now->format('Y-m-d'); |
||
4102 | |||
4103 | // To list the surveys base too |
||
4104 | $showSurveysBase = api_get_configuration_value('show_surveys_base_in_sessions'); |
||
4105 | $sessionCondition = api_get_session_condition($sessionId, true, $showSurveysBase, 'survey.session_id'); |
||
4106 | |||
4107 | $sql = "SELECT survey_invitation.answered, |
||
4108 | survey_invitation.invitation_code, |
||
4109 | survey_invitation.session_id, |
||
4110 | survey.title, |
||
4111 | survey.visible_results, |
||
4112 | survey.survey_id, |
||
4113 | survey.anonymous |
||
4114 | FROM $table_survey survey |
||
4115 | INNER JOIN |
||
4116 | $table_survey_invitation survey_invitation |
||
4117 | ON ( |
||
4118 | survey.code = survey_invitation.survey_code AND |
||
4119 | survey.c_id = survey_invitation.c_id AND |
||
4120 | survey_invitation.session_id = $sessionId |
||
4121 | ) |
||
4122 | WHERE |
||
4123 | survey_invitation.user = $user_id AND |
||
4124 | survey.avail_from <= '$filterDate' AND |
||
4125 | (survey.avail_till >= '$filterDate' OR |
||
4126 | survey.avail_till IS NULL) AND |
||
4127 | survey.c_id = $course_id AND |
||
4128 | survey_invitation.c_id = $course_id $sessionCondition |
||
4129 | "; |
||
4130 | $result = Database::query($sql); |
||
4131 | |||
4132 | $efv = new ExtraFieldValue('survey'); |
||
4133 | |||
4134 | $surveyIds = []; |
||
4135 | |||
4136 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
4137 | if (in_array($row['survey_id'], $surveyIds)) { |
||
4138 | continue; |
||
4139 | } |
||
4140 | |||
4141 | $title = Security::remove_XSS($row['title']); |
||
4142 | |||
4143 | echo '<tr>'; |
||
4144 | if ($row['answered'] == 0) { |
||
4145 | echo '<td>'; |
||
4146 | $url = self::generateFillSurveyLink($row['invitation_code'], $_course, $row['session_id']); |
||
4147 | $icon = Display::return_icon( |
||
4148 | 'survey.png', |
||
4149 | get_lang('ClickHereToAnswerTheSurvey'), |
||
4150 | ['style' => 'margin-top: -4px'], |
||
4151 | ICON_SIZE_TINY |
||
4152 | ); |
||
4153 | echo '<a href="'.$url.'"> |
||
4154 | '.$icon |
||
4155 | .$title |
||
4156 | .'</a></td>'; |
||
4157 | } else { |
||
4158 | $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh( |
||
4159 | $user_id, |
||
4160 | $_course |
||
4161 | ); |
||
4162 | $icon = Display::return_icon( |
||
4163 | 'survey_na.png', |
||
4164 | get_lang('SurveysDone'), |
||
4165 | [], |
||
4166 | ICON_SIZE_TINY |
||
4167 | ); |
||
4168 | $showLink = (!api_is_allowed_to_edit(false, true) || $isDrhOfCourse) |
||
4169 | && $row['visible_results'] != SURVEY_VISIBLE_TUTOR; |
||
4170 | |||
4171 | echo '<td>'; |
||
4172 | echo $showLink |
||
4173 | ? Display::url( |
||
4174 | $icon.PHP_EOL.$row['title'], |
||
4175 | api_get_path(WEB_CODE_PATH).'survey/reporting.php?'.api_get_cidreq().'&'.http_build_query([ |
||
4176 | 'action' => 'questionreport', |
||
4177 | 'survey_id' => $row['survey_id'], |
||
4178 | ]) |
||
4179 | ) |
||
4180 | : $icon.PHP_EOL.$title; |
||
4181 | echo '</td>'; |
||
4182 | } |
||
4183 | echo '<td class="text-center">'; |
||
4184 | echo ($row['anonymous'] == 1) ? get_lang('Yes') : get_lang('No'); |
||
4185 | echo '</td>'; |
||
4186 | if ($mandatoryAllowed) { |
||
4187 | $efvMandatory = $efv->get_values_by_handler_and_field_variable( |
||
4188 | $row['survey_id'], |
||
4189 | 'is_mandatory' |
||
4190 | ); |
||
4191 | echo '<td class="text-center">'.($efvMandatory['value'] ? get_lang('Yes') : get_lang('No')).'</td>'; |
||
4192 | } |
||
4193 | echo '</tr>'; |
||
4194 | $surveyIds[] = $row['survey_id']; |
||
4195 | } |
||
4196 | echo '</tbody>'; |
||
4197 | echo '</table>'; |
||
4198 | } |
||
4199 | |||
4200 | /** |
||
4201 | * Creates a multi array with the user fields that we can show. |
||
4202 | * We look the visibility with the api_get_setting function |
||
4203 | * The username is always NOT able to change it. |
||
4204 | * |
||
4205 | * @author Julio Montoya Armas <[email protected]>, Chamilo: Personality Test modification |
||
4206 | * |
||
4207 | * @return array array[value_name][name], array[value_name][visibilty] |
||
4208 | */ |
||
4209 | public static function make_field_list() |
||
4210 | { |
||
4211 | // LAST NAME and FIRST NAME |
||
4212 | $field_list_array = []; |
||
4213 | $field_list_array['lastname']['name'] = get_lang('LastName'); |
||
4214 | $field_list_array['firstname']['name'] = get_lang('FirstName'); |
||
4215 | |||
4216 | if (api_get_setting('profile', 'name') != 'true') { |
||
4217 | $field_list_array['firstname']['visibility'] = 0; |
||
4218 | $field_list_array['lastname']['visibility'] = 0; |
||
4219 | } else { |
||
4220 | $field_list_array['firstname']['visibility'] = 1; |
||
4221 | $field_list_array['lastname']['visibility'] = 1; |
||
4222 | } |
||
4223 | |||
4224 | $field_list_array['username']['name'] = get_lang('Username'); |
||
4225 | $field_list_array['username']['visibility'] = 0; |
||
4226 | |||
4227 | // OFFICIAL CODE |
||
4228 | $field_list_array['official_code']['name'] = get_lang('OfficialCode'); |
||
4229 | |||
4230 | if (api_get_setting('profile', 'officialcode') != 'true') { |
||
4231 | $field_list_array['official_code']['visibility'] = 1; |
||
4232 | } else { |
||
4233 | $field_list_array['official_code']['visibility'] = 0; |
||
4234 | } |
||
4235 | |||
4236 | |||
4237 | $field_list_array['email']['name'] = get_lang('Email'); |
||
4238 | if (api_get_setting('profile', 'email') != 'true') { |
||
4239 | $field_list_array['email']['visibility'] = 1; |
||
4240 | } else { |
||
4241 | $field_list_array['email']['visibility'] = 0; |
||
4242 | } |
||
4243 | |||
4244 | // PHONE |
||
4245 | $field_list_array['phone']['name'] = get_lang('Phone'); |
||
4246 | if (api_get_setting('profile', 'phone') != 'true') { |
||
4247 | $field_list_array['phone']['visibility'] = 0; |
||
4248 | } else { |
||
4249 | $field_list_array['phone']['visibility'] = 1; |
||
4250 | } |
||
4251 | // LANGUAGE |
||
4252 | $field_list_array['language']['name'] = get_lang('Language'); |
||
4253 | if (api_get_setting('profile', 'language') != 'true') { |
||
4254 | $field_list_array['language']['visibility'] = 0; |
||
4255 | } else { |
||
4256 | $field_list_array['language']['visibility'] = 1; |
||
4257 | } |
||
4258 | |||
4259 | // EXTRA FIELDS |
||
4260 | $extra = UserManager::get_extra_fields(0, 50, 5, 'ASC'); |
||
4261 | |||
4262 | foreach ($extra as $id => $field_details) { |
||
4263 | if (0 == $field_details[6]) { |
||
4264 | continue; |
||
4265 | } |
||
4266 | switch ($field_details[2]) { |
||
4267 | case UserManager::USER_FIELD_TYPE_TEXT: |
||
4268 | $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; |
||
4269 | if (0 == $field_details[7]) { |
||
4270 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; |
||
4271 | } else { |
||
4272 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 1; |
||
4273 | } |
||
4274 | break; |
||
4275 | case UserManager::USER_FIELD_TYPE_TEXTAREA: |
||
4276 | $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; |
||
4277 | if (0 == $field_details[7]) { |
||
4278 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; |
||
4279 | } else { |
||
4280 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 1; |
||
4281 | } |
||
4282 | break; |
||
4283 | case UserManager::USER_FIELD_TYPE_RADIO: |
||
4284 | $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; |
||
4285 | if (0 == $field_details[7]) { |
||
4286 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; |
||
4287 | } else { |
||
4288 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 1; |
||
4289 | } |
||
4290 | break; |
||
4291 | case UserManager::USER_FIELD_TYPE_SELECT: |
||
4292 | $get_lang_variables = false; |
||
4293 | if (in_array( |
||
4294 | $field_details[1], |
||
4295 | ['mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message'] |
||
4296 | ) |
||
4297 | ) { |
||
4298 | $get_lang_variables = true; |
||
4299 | } |
||
4300 | |||
4301 | if ($get_lang_variables) { |
||
4302 | $field_list_array['extra_'.$field_details[1]]['name'] = get_lang($field_details[3]); |
||
4303 | } else { |
||
4304 | $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; |
||
4305 | } |
||
4306 | |||
4307 | if (0 == $field_details[7]) { |
||
4308 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; |
||
4309 | } else { |
||
4310 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 1; |
||
4311 | } |
||
4312 | break; |
||
4313 | case UserManager::USER_FIELD_TYPE_SELECT_MULTIPLE: |
||
4314 | $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; |
||
4315 | if (0 == $field_details[7]) { |
||
4316 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; |
||
4317 | } else { |
||
4318 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 1; |
||
4319 | } |
||
4320 | break; |
||
4321 | case UserManager::USER_FIELD_TYPE_DATE: |
||
4322 | $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; |
||
4323 | if (0 == $field_details[7]) { |
||
4324 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; |
||
4325 | } else { |
||
4326 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 1; |
||
4327 | } |
||
4328 | break; |
||
4329 | case UserManager::USER_FIELD_TYPE_DATETIME: |
||
4330 | $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; |
||
4331 | if (0 == $field_details[7]) { |
||
4332 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; |
||
4333 | } else { |
||
4334 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 1; |
||
4335 | } |
||
4336 | break; |
||
4337 | case UserManager::USER_FIELD_TYPE_DOUBLE_SELECT: |
||
4338 | $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; |
||
4339 | if (0 == $field_details[7]) { |
||
4340 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; |
||
4341 | } else { |
||
4342 | $field_list_array['extra_'.$field_details[1]]['visibility'] = 1; |
||
4343 | } |
||
4344 | break; |
||
4345 | case UserManager::USER_FIELD_TYPE_DIVIDER: |
||
4346 | //$form->addElement('static',$field_details[1], '<br /><strong>'.$field_details[3].'</strong>'); |
||
4347 | break; |
||
4348 | } |
||
4349 | } |
||
4350 | |||
4351 | return $field_list_array; |
||
4352 | } |
||
4353 | |||
4354 | /** |
||
4355 | * Display survey question chart. |
||
4356 | * |
||
4357 | * @param array $chartData |
||
4358 | * @param bool $hasSerie Tells if the chart has a serie. False by default |
||
4359 | * @param string $chartContainerId |
||
4360 | * |
||
4361 | * @return string (direct output) |
||
4362 | */ |
||
4363 | public static function drawChart($chartData, $hasSerie = false, $chartContainerId = 'chartContainer', $loadLibs = true) |
||
4364 | { |
||
4365 | $htmlChart = ''; |
||
4366 | if (api_browser_support('svg')) { |
||
4367 | $serie = []; |
||
4368 | $order = []; |
||
4369 | $data = ''; |
||
4370 | foreach ($chartData as $chartDataElement) { |
||
4371 | $data .= '{"'; |
||
4372 | $option = str_replace(["\n", "\r"], '', $chartDataElement['option']); |
||
4373 | $serieValue = isset($chartDataElement['serie']) ? $chartDataElement['serie'] : null; |
||
4374 | |||
4375 | if (!$hasSerie) { |
||
4376 | $data .= get_lang('Option').'":"'.$option.'", "'; |
||
4377 | array_push($order, $option); |
||
4378 | } else { |
||
4379 | if (!is_array($serieValue)) { |
||
4380 | $data .= |
||
4381 | get_lang('Option').'":"'.$serieValue.'", "'. |
||
4382 | get_lang('Score').'":"'.$option.'", "'; |
||
4383 | array_push($serie, $serieValue); |
||
4384 | } else { |
||
4385 | $data .= |
||
4386 | get_lang('Serie').'":"'.$serieValue[0].'", "'. |
||
4387 | get_lang('Option').'":"'.$serieValue[1].'", "'. |
||
4388 | get_lang('Score').'":"'.$option.'", "'; |
||
4389 | } |
||
4390 | } |
||
4391 | $data .= get_lang('Votes').'":"'.$chartDataElement['votes'].'"},'; |
||
4392 | rtrim($data, ','); |
||
4393 | } |
||
4394 | |||
4395 | if ($loadLibs) { |
||
4396 | $htmlChart .= api_get_js('d3/d3.v3.5.4.min.js'); |
||
4397 | $htmlChart .= api_get_js('dimple.v2.1.2.min.js'); |
||
4398 | } |
||
4399 | |||
4400 | $htmlChart .= ' |
||
4401 | <script> |
||
4402 | var svg = dimple.newSvg("#'.$chartContainerId.'", 600, 400); |
||
4403 | var data = ['.$data.']; |
||
4404 | var myChart = new dimple.chart(svg, data); |
||
4405 | myChart.setBounds(50, 30, 550, 300); |
||
4406 | var yAxis = myChart.addMeasureAxis("y", "'.get_lang('Votes').'"); |
||
4407 | yAxis.fontSize = "14px"; |
||
4408 | '; |
||
4409 | |||
4410 | if (!$hasSerie) { |
||
4411 | $htmlChart .= ' |
||
4412 | var xAxisCategory = myChart.addCategoryAxis("x", "'.get_lang("Option").'"); |
||
4413 | xAxisCategory.fontSize = "14px"; |
||
4414 | xAxisCategory.addOrderRule('.json_encode($order).'); |
||
4415 | myChart.addSeries("'.get_lang("Option").'", dimple.plot.bar);'; |
||
4416 | } else { |
||
4417 | if (!is_array($chartDataElement['serie'])) { |
||
4418 | $serie = array_values(array_unique($serie)); |
||
4419 | $htmlChart .= ' |
||
4420 | var xAxisCategory = |
||
4421 | myChart.addCategoryAxis("x", ["'.get_lang('Option').'","'.get_lang("Score").'"]); |
||
4422 | xAxisCategory.addOrderRule('.json_encode($serie).'); |
||
4423 | xAxisCategory.addGroupOrderRule("'.get_lang('Score').'"); |
||
4424 | |||
4425 | myChart.addSeries("'.get_lang('Option').'", dimple.plot.bar);'; |
||
4426 | } else { |
||
4427 | $htmlChart .= ' |
||
4428 | myChart.addCategoryAxis("x", ["'.get_lang('Option').'","'.get_lang("Score").'"]); |
||
4429 | myChart.addSeries("'.get_lang('Serie').'", dimple.plot.bar);'; |
||
4430 | } |
||
4431 | } |
||
4432 | |||
4433 | $htmlChart .= 'myChart.draw();'; |
||
4434 | $htmlChart .= '</script>'; |
||
4435 | } |
||
4436 | |||
4437 | return $htmlChart; |
||
4438 | } |
||
4439 | |||
4440 | /** |
||
4441 | * Set a flag to the current survey as answered by the current user. |
||
4442 | * |
||
4443 | * @param string $surveyCode The survey code |
||
4444 | * @param int $courseId The course ID |
||
4445 | */ |
||
4446 | public static function flagSurveyAsAnswered($surveyCode, $courseId) |
||
4447 | { |
||
4448 | $currentUserId = api_get_user_id(); |
||
4449 | $flag = sprintf('%s-%s-%d', $courseId, $surveyCode, $currentUserId); |
||
4450 | |||
4451 | if (!isset($_SESSION['filled_surveys'])) { |
||
4452 | $_SESSION['filled_surveys'] = []; |
||
4453 | } |
||
4454 | |||
4455 | $_SESSION['filled_surveys'][] = $flag; |
||
4456 | } |
||
4457 | |||
4458 | /** |
||
4459 | * Check whether a survey was answered by the current user. |
||
4460 | * |
||
4461 | * @param string $surveyCode The survey code |
||
4462 | * @param int $courseId The course ID |
||
4463 | * |
||
4464 | * @return bool |
||
4465 | */ |
||
4466 | public static function isSurveyAnsweredFlagged($surveyCode, $courseId) |
||
4488 | } |
||
4489 | |||
4490 | /** |
||
4491 | * Check if the current survey has answers. |
||
4492 | * |
||
4493 | * @param int $surveyId |
||
4494 | * |
||
4495 | * @return bool return true if the survey has answers, false otherwise |
||
4496 | */ |
||
4497 | public static function checkIfSurveyHasAnswers($surveyId) |
||
4498 | { |
||
4499 | $tableSurveyAnswer = Database::get_course_table(TABLE_SURVEY_ANSWER); |
||
4500 | $courseId = api_get_course_int_id(); |
||
4501 | $surveyId = (int) $surveyId; |
||
4502 | |||
4503 | if (empty($courseId) || empty($surveyId)) { |
||
4504 | return false; |
||
4505 | } |
||
4506 | |||
4507 | // To select the answers by session |
||
4508 | $sessionId = api_get_session_id(); |
||
4509 | $sessionCondition = ''; |
||
4510 | if (true === api_get_configuration_value('show_surveys_base_in_sessions')) { |
||
4511 | $sessionCondition = api_get_session_condition($sessionId); |
||
4512 | } |
||
4513 | |||
4514 | $sql = "SELECT * FROM $tableSurveyAnswer |
||
4515 | WHERE |
||
4516 | c_id = $courseId AND |
||
4517 | survey_id = '".$surveyId."' |
||
4518 | $sessionCondition |
||
4519 | ORDER BY answer_id, user ASC"; |
||
4520 | $result = Database::query($sql); |
||
4521 | $response = Database::affected_rows($result); |
||
4522 | |||
4523 | return $response > 0; |
||
4524 | } |
||
4525 | |||
4526 | /** |
||
4527 | * Get the pending surveys for a user. |
||
4528 | * |
||
4529 | * @param int $userId |
||
4530 | * |
||
4531 | * @return array |
||
4532 | */ |
||
4533 | public static function getUserPendingInvitations($userId) |
||
4534 | { |
||
4535 | $now = api_get_utc_datetime(null, false, true); |
||
4536 | |||
4537 | $dql = " |
||
4538 | SELECT s, si FROM ChamiloCourseBundle:CSurvey s |
||
4539 | INNER JOIN ChamiloCourseBundle:CSurveyInvitation si |
||
4540 | WITH (s.code = si.surveyCode AND s.cId = si.cId AND s.sessionId = si.sessionId ) |
||
4541 | WHERE |
||
4542 | si.user = :user_id AND |
||
4543 | s.availFrom <= :now AND |
||
4544 | s.availTill >= :now AND |
||
4545 | si.answered = 0 |
||
4546 | ORDER BY s.availTill ASC |
||
4547 | "; |
||
4548 | |||
4549 | $pendingSurveys = Database::getManager() |
||
4550 | ->createQuery($dql) |
||
4551 | ->setParameters(['user_id' => $userId, 'now' => $now->format('Y-m-d')]) |
||
4552 | ->getResult(); |
||
4553 | |||
4554 | return $pendingSurveys; |
||
4555 | } |
||
4556 | |||
4557 | /** |
||
4558 | * @param string $surveyCode |
||
4559 | * @param int $courseId |
||
4560 | * @param int $sessionId |
||
4561 | * |
||
4562 | * @return array |
||
4563 | */ |
||
4564 | public static function getSentInvitations($surveyCode, $courseId, $sessionId = 0) |
||
4565 | { |
||
4566 | $tblUser = Database::get_main_table(TABLE_MAIN_USER); |
||
4567 | $tblSurveyInvitation = Database::get_course_table(TABLE_SURVEY_INVITATION); |
||
4568 | |||
4569 | $sessionCondition = api_get_session_condition($sessionId); |
||
4570 | $surveyCode = Database::escape_string($surveyCode); |
||
4571 | $courseId = (int) $courseId; |
||
4572 | |||
4573 | $sql = "SELECT survey_invitation.*, user.firstname, user.lastname, user.email |
||
4574 | FROM $tblSurveyInvitation survey_invitation |
||
4575 | LEFT JOIN $tblUser user |
||
4576 | ON (survey_invitation.user = user.id AND survey_invitation.c_id = $courseId) |
||
4577 | WHERE |
||
4578 | survey_invitation.survey_code = '$surveyCode' |
||
4579 | AND survey_invitation.c_id = $courseId |
||
4580 | $sessionCondition"; |
||
4581 | |||
4582 | $query = Database::query($sql); |
||
4583 | |||
4584 | return Database::store_result($query); |
||
4585 | } |
||
4586 | |||
4587 | /** |
||
4588 | * @param string $code invitation code |
||
4589 | * @param array $courseInfo |
||
4590 | * @param int $sessionId |
||
4591 | * @param string $surveyCode |
||
4592 | * |
||
4593 | * @return string |
||
4594 | */ |
||
4595 | public static function generateFillSurveyLink($code, $courseInfo, $sessionId, $surveyCode = '') |
||
4619 | } |
||
4620 | } |
||
4621 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.