Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 1765 | class SurveyUtil |
||
| 1766 | { |
||
| 1767 | /** |
||
| 1768 | * Checks whether the given survey has a pagebreak question as the first or the last question. |
||
| 1769 | * If so, break the current process, displaying an error message |
||
| 1770 | * @param integer Survey ID (database ID) |
||
| 1771 | * @param boolean Optional. Whether to continue the current process or exit when breaking condition found. Defaults to true (do not break). |
||
| 1772 | * @return void |
||
| 1773 | */ |
||
| 1774 | static function check_first_last_question($survey_id, $continue = true) |
||
| 1775 | { |
||
| 1776 | // Table definitions |
||
| 1777 | $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); |
||
| 1778 | $course_id = api_get_course_int_id(); |
||
| 1779 | |||
| 1780 | // Getting the information of the question |
||
| 1781 | $sql = "SELECT * FROM $tbl_survey_question |
||
| 1782 | WHERE c_id = $course_id AND survey_id='".Database::escape_string($survey_id)."' |
||
| 1783 | ORDER BY sort ASC"; |
||
| 1784 | $result = Database::query($sql); |
||
| 1785 | $total = Database::num_rows($result); |
||
| 1786 | $counter = 1; |
||
| 1787 | $error = false; |
||
| 1788 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 1789 | View Code Duplication | if ($counter == 1 && $row['type'] == 'pagebreak') { |
|
| 1790 | |||
| 1791 | Display::display_error_message(get_lang('PagebreakNotFirst'), false); |
||
| 1792 | $error = true; |
||
| 1793 | } |
||
| 1794 | View Code Duplication | if ($counter == $total && $row['type'] == 'pagebreak') { |
|
| 1795 | Display::display_error_message(get_lang('PagebreakNotLast'), false); |
||
| 1796 | $error = true; |
||
| 1797 | } |
||
| 1798 | $counter++; |
||
| 1799 | } |
||
| 1800 | |||
| 1801 | if (!$continue && $error) { |
||
| 1802 | Display::display_footer(); |
||
| 1803 | exit; |
||
| 1804 | } |
||
| 1805 | } |
||
| 1806 | |||
| 1807 | /** |
||
| 1808 | * This function removes an (or multiple) answer(s) of a user on a question of a survey |
||
| 1809 | * |
||
| 1810 | * @param mixed The user id or email of the person who fills the survey |
||
| 1811 | * @param integer The survey id |
||
| 1812 | * @param integer The question id |
||
| 1813 | * @param integer The option id |
||
| 1814 | * |
||
| 1815 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 1816 | * @version January 2007 |
||
| 1817 | */ |
||
| 1818 | static function remove_answer($user, $survey_id, $question_id, $course_id) { |
||
| 1819 | $course_id = intval($course_id); |
||
| 1820 | // table definition |
||
| 1821 | $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); |
||
| 1822 | $sql = "DELETE FROM $table_survey_answer |
||
| 1823 | WHERE |
||
| 1824 | c_id = $course_id AND |
||
| 1825 | user = '".Database::escape_string($user)."' AND |
||
| 1826 | survey_id = '".intval($survey_id)."' AND |
||
| 1827 | question_id = '".intval($question_id)."'"; |
||
| 1828 | Database::query($sql); |
||
| 1829 | } |
||
| 1830 | |||
| 1831 | /** |
||
| 1832 | * This function stores an answer of a user on a question of a survey |
||
| 1833 | * |
||
| 1834 | * @param mixed The user id or email of the person who fills the survey |
||
| 1835 | * @param integer Survey id |
||
| 1836 | * @param integer Question id |
||
| 1837 | * @param integer Option id |
||
| 1838 | * @param string Option value |
||
| 1839 | * @param array Survey data settings |
||
| 1840 | * |
||
| 1841 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 1842 | * @version January 2007 |
||
| 1843 | */ |
||
| 1844 | static function store_answer($user, $survey_id, $question_id, $option_id, $option_value, $survey_data) |
||
| 1845 | { |
||
| 1846 | // Table definition |
||
| 1847 | $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); |
||
| 1848 | |||
| 1849 | // Make the survey anonymous |
||
| 1850 | if ($survey_data['anonymous'] == 1) { |
||
| 1851 | if (!isset($_SESSION['surveyuser'])) { |
||
| 1852 | $user = md5($user.time()); |
||
| 1853 | $_SESSION['surveyuser'] = $user; |
||
| 1854 | } else { |
||
| 1855 | $user = $_SESSION['surveyuser']; |
||
| 1856 | } |
||
| 1857 | } |
||
| 1858 | |||
| 1859 | $course_id = $survey_data['c_id']; |
||
| 1860 | |||
| 1861 | $sql = "INSERT INTO $table_survey_answer (c_id, user, survey_id, question_id, option_id, value) VALUES ( |
||
| 1862 | $course_id, |
||
| 1863 | '".Database::escape_string($user)."', |
||
| 1864 | '".Database::escape_string($survey_id)."', |
||
| 1865 | '".Database::escape_string($question_id)."', |
||
| 1866 | '".Database::escape_string($option_id)."', |
||
| 1867 | '".Database::escape_string($option_value)."' |
||
| 1868 | )"; |
||
| 1869 | Database::query($sql); |
||
| 1870 | $insertId = Database::insert_id(); |
||
| 1871 | |||
| 1872 | $sql = "UPDATE $table_survey_answer SET answer_id = $insertId WHERE iid = $insertId"; |
||
| 1873 | Database::query($sql); |
||
| 1874 | } |
||
| 1875 | |||
| 1876 | /** |
||
| 1877 | * This function checks the parameters that are used in this page |
||
| 1878 | * |
||
| 1879 | * @return string The header, an error and the footer if any parameter fails, else it returns true |
||
| 1880 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 1881 | * @version February 2007 |
||
| 1882 | */ |
||
| 1883 | static function check_parameters($people_filled) |
||
| 1884 | { |
||
| 1885 | $error = false; |
||
| 1886 | |||
| 1887 | // Getting the survey data |
||
| 1888 | $survey_data = SurveyManager::get_survey($_GET['survey_id']); |
||
| 1889 | |||
| 1890 | // $_GET['survey_id'] has to be numeric |
||
| 1891 | if (!is_numeric($_GET['survey_id'])) { |
||
| 1892 | $error = get_lang('IllegalSurveyId'); |
||
| 1893 | } |
||
| 1894 | |||
| 1895 | // $_GET['action'] |
||
| 1896 | $allowed_actions = array( |
||
| 1897 | 'overview', |
||
| 1898 | 'questionreport', |
||
| 1899 | 'userreport', |
||
| 1900 | 'comparativereport', |
||
| 1901 | 'completereport', |
||
| 1902 | 'deleteuserreport' |
||
| 1903 | ); |
||
| 1904 | if (isset($_GET['action']) && !in_array($_GET['action'], $allowed_actions)) { |
||
| 1905 | $error = get_lang('ActionNotAllowed'); |
||
| 1906 | } |
||
| 1907 | |||
| 1908 | // User report |
||
| 1909 | if (isset($_GET['action']) && $_GET['action'] == 'userreport') { |
||
| 1910 | if ($survey_data['anonymous'] == 0) { |
||
| 1911 | foreach ($people_filled as $key => & $value) { |
||
| 1912 | $people_filled_userids[] = $value['invited_user']; |
||
| 1913 | } |
||
| 1914 | } else { |
||
| 1915 | $people_filled_userids = $people_filled; |
||
| 1916 | } |
||
| 1917 | |||
| 1918 | if (isset($_GET['user']) && !in_array($_GET['user'], $people_filled_userids)) { |
||
| 1919 | $error = get_lang('UnknowUser'); |
||
| 1920 | } |
||
| 1921 | } |
||
| 1922 | |||
| 1923 | // Question report |
||
| 1924 | if (isset($_GET['action']) && $_GET['action'] == 'questionreport') { |
||
| 1925 | if (isset($_GET['question']) && !is_numeric($_GET['question'])) { |
||
| 1926 | $error = get_lang('UnknowQuestion'); |
||
| 1927 | } |
||
| 1928 | } |
||
| 1929 | |||
| 1930 | if ($error) { |
||
| 1931 | $tool_name = get_lang('Reporting'); |
||
| 1932 | Display::display_header($tool_name); |
||
| 1933 | Display::display_error_message(get_lang('Error').': '.$error, false); |
||
| 1934 | Display::display_footer(); |
||
| 1935 | exit; |
||
| 1936 | } else { |
||
| 1937 | return true; |
||
| 1938 | } |
||
| 1939 | } |
||
| 1940 | |||
| 1941 | /** |
||
| 1942 | * This function deals with the action handling |
||
| 1943 | * @return void |
||
| 1944 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 1945 | * @version February 2007 |
||
| 1946 | */ |
||
| 1947 | public static function handle_reporting_actions($survey_data, $people_filled) |
||
| 1948 | { |
||
| 1949 | $action = isset($_GET['action']) ? $_GET['action'] : null; |
||
| 1950 | |||
| 1951 | // Getting the number of question |
||
| 1952 | $temp_questions_data = SurveyManager::get_questions($_GET['survey_id']); |
||
| 1953 | |||
| 1954 | // Sorting like they should be displayed and removing the non-answer question types (comment and pagebreak) |
||
| 1955 | $my_temp_questions_data = $temp_questions_data == null ? array() : $temp_questions_data; |
||
| 1956 | $questions_data = array(); |
||
| 1957 | |||
| 1958 | foreach ($my_temp_questions_data as $key => & $value) { |
||
| 1959 | if ($value['type'] != 'comment' && $value['type'] != 'pagebreak') { |
||
| 1960 | $questions_data[$value['sort']] = $value; |
||
| 1961 | } |
||
| 1962 | } |
||
| 1963 | |||
| 1964 | // Counting the number of questions that are relevant for the reporting |
||
| 1965 | $survey_data['number_of_questions'] = count($questions_data); |
||
| 1966 | |||
| 1967 | if ($action == 'questionreport') { |
||
| 1968 | SurveyUtil::display_question_report($survey_data); |
||
| 1969 | } |
||
| 1970 | if ($action == 'userreport') { |
||
| 1971 | SurveyUtil::display_user_report($people_filled, $survey_data); |
||
| 1972 | } |
||
| 1973 | if ($action == 'comparativereport') { |
||
| 1974 | SurveyUtil::display_comparative_report(); |
||
| 1975 | } |
||
| 1976 | if ($action == 'completereport') { |
||
| 1977 | SurveyUtil::display_complete_report($survey_data); |
||
| 1978 | } |
||
| 1979 | if ($action == 'deleteuserreport') { |
||
| 1980 | SurveyUtil::delete_user_report($_GET['survey_id'], $_GET['user']); |
||
| 1981 | } |
||
| 1982 | } |
||
| 1983 | |||
| 1984 | /** |
||
| 1985 | * This function deletes the report of an user who wants to retake the survey |
||
| 1986 | * @param integer survey_id |
||
| 1987 | * @param integer user_id |
||
| 1988 | * @return void |
||
| 1989 | * @author Christian Fasanando Flores <[email protected]> |
||
| 1990 | * @version November 2008 |
||
| 1991 | */ |
||
| 1992 | function delete_user_report($survey_id, $user_id) |
||
| 1993 | { |
||
| 1994 | $table_survey_answer = Database:: get_course_table(TABLE_SURVEY_ANSWER); |
||
| 1995 | $table_survey_invitation = Database:: get_course_table(TABLE_SURVEY_INVITATION); |
||
| 1996 | $table_survey = Database:: get_course_table(TABLE_SURVEY); |
||
| 1997 | |||
| 1998 | $course_id = api_get_course_int_id(); |
||
| 1999 | $survey_id = (int) $survey_id; |
||
| 2000 | |||
| 2001 | if (!empty($survey_id) && !empty($user_id)) { |
||
| 2002 | $user_id = Database::escape_string($user_id); |
||
| 2003 | // delete data from survey_answer by user_id and survey_id |
||
| 2004 | $sql = "DELETE FROM $table_survey_answer |
||
| 2005 | WHERE c_id = $course_id AND survey_id = '".$survey_id."' AND user = '".$user_id."'"; |
||
| 2006 | Database::query($sql); |
||
| 2007 | // update field answered from survey_invitation by user_id and survey_id |
||
| 2008 | $sql = "UPDATE $table_survey_invitation SET answered = '0' |
||
| 2009 | WHERE |
||
| 2010 | c_id = $course_id AND |
||
| 2011 | survey_code = ( |
||
| 2012 | SELECT code FROM $table_survey |
||
| 2013 | WHERE |
||
| 2014 | c_id = $course_id AND |
||
| 2015 | survey_id = '".(int)$survey_id."' |
||
| 2016 | ) AND |
||
| 2017 | user = '".$user_id."'"; |
||
| 2018 | $result = Database::query($sql); |
||
| 2019 | } |
||
| 2020 | |||
| 2021 | View Code Duplication | if ($result !== false) { |
|
| 2022 | $message = get_lang('SurveyUserAnswersHaveBeenRemovedSuccessfully').'<br /> |
||
| 2023 | <a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=userreport&survey_id='.Security::remove_XSS($survey_id).'">'.get_lang('GoBack').'</a>'; |
||
| 2024 | Display::display_confirmation_message($message, false); |
||
| 2025 | } |
||
| 2026 | } |
||
| 2027 | |||
| 2028 | /** |
||
| 2029 | * This function displays the user report which is basically nothing more |
||
| 2030 | * than a one-page display of all the questions |
||
| 2031 | * of the survey that is filled with the answers of the person who filled the survey. |
||
| 2032 | * |
||
| 2033 | * @return string html code of the one-page survey with the answers of the selected user |
||
| 2034 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 2035 | * @version February 2007 - Updated March 2008 |
||
| 2036 | */ |
||
| 2037 | public static function display_user_report($people_filled, $survey_data) |
||
| 2038 | { |
||
| 2039 | // Database table definitions |
||
| 2040 | $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); |
||
| 2041 | $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); |
||
| 2042 | $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); |
||
| 2043 | |||
| 2044 | // Actions bar |
||
| 2045 | echo '<div class="actions">'; |
||
| 2046 | echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.Security::remove_XSS($_GET['survey_id']).'">'. |
||
| 2047 | Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('ReportingOverview'),'',ICON_SIZE_MEDIUM).'</a>'; |
||
| 2048 | if (isset($_GET['user'])) { |
||
| 2049 | if (api_is_allowed_to_edit()) { |
||
| 2050 | // The delete link |
||
| 2051 | echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=deleteuserreport&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.Security::remove_XSS($_GET['user']).'" >'. |
||
| 2052 | Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_MEDIUM).'</a>'; |
||
| 2053 | } |
||
| 2054 | |||
| 2055 | // Export the user report |
||
| 2056 | echo '<a href="javascript: void(0);" onclick="document.form1a.submit();">'. |
||
| 2057 | Display::return_icon('export_csv.png', get_lang('ExportAsCSV'),'',ICON_SIZE_MEDIUM).'</a> '; |
||
| 2058 | echo '<a href="javascript: void(0);" onclick="document.form1b.submit();">'. |
||
| 2059 | Display::return_icon('export_excel.png', get_lang('ExportAsXLS'),'',ICON_SIZE_MEDIUM).'</a> '; |
||
| 2060 | echo '<form id="form1a" name="form1a" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user_id='.Security::remove_XSS($_GET['user']).'">'; |
||
| 2061 | echo '<input type="hidden" name="export_report" value="export_report">'; |
||
| 2062 | echo '<input type="hidden" name="export_format" value="csv">'; |
||
| 2063 | echo '</form>'; |
||
| 2064 | echo '<form id="form1b" name="form1b" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user_id='.Security::remove_XSS($_GET['user']).'">'; |
||
| 2065 | echo '<input type="hidden" name="export_report" value="export_report">'; |
||
| 2066 | echo '<input type="hidden" name="export_format" value="xls">'; |
||
| 2067 | echo '</form>'; |
||
| 2068 | echo '<form id="form2" name="form2" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">'; |
||
| 2069 | } |
||
| 2070 | echo '</div>'; |
||
| 2071 | |||
| 2072 | // Step 1: selection of the user |
||
| 2073 | echo "<script> |
||
| 2074 | function jumpMenu(targ,selObj,restore) { |
||
| 2075 | eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\"); |
||
| 2076 | if (restore) selObj.selectedIndex=0; |
||
| 2077 | } |
||
| 2078 | </script>"; |
||
| 2079 | echo get_lang('SelectUserWhoFilledSurvey').'<br />'; |
||
| 2080 | echo '<select name="user" onchange="jumpMenu(\'parent\',this,0)">'; |
||
| 2081 | echo '<option value="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">'.get_lang('SelectUser').'</option>'; |
||
| 2082 | |||
| 2083 | foreach ($people_filled as $key => & $person) { |
||
| 2084 | if ($survey_data['anonymous'] == 0) { |
||
| 2085 | $name = api_get_person_name($person['firstname'], $person['lastname']); |
||
| 2086 | $id = $person['user_id']; |
||
| 2087 | if ($id == '') { |
||
| 2088 | $id = $person['invited_user']; |
||
| 2089 | $name = $person['invited_user']; |
||
| 2090 | } |
||
| 2091 | } else { |
||
| 2092 | $name = get_lang('Anonymous') . ' ' . ($key + 1); |
||
| 2093 | $id = $person; |
||
| 2094 | } |
||
| 2095 | echo '<option value="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.Security::remove_XSS($id).'" '; |
||
| 2096 | if (isset($_GET['user']) && $_GET['user'] == $id) { |
||
| 2097 | echo 'selected="selected"'; |
||
| 2098 | } |
||
| 2099 | echo '>'.$name.'</option>'; |
||
| 2100 | } |
||
| 2101 | echo '</select>'; |
||
| 2102 | |||
| 2103 | $course_id = api_get_course_int_id(); |
||
| 2104 | // Step 2: displaying the survey and the answer of the selected users |
||
| 2105 | if (isset($_GET['user'])) { |
||
| 2106 | Display::display_normal_message( |
||
| 2107 | get_lang('AllQuestionsOnOnePage'), |
||
| 2108 | false |
||
| 2109 | ); |
||
| 2110 | |||
| 2111 | // Getting all the questions and options |
||
| 2112 | $sql = "SELECT |
||
| 2113 | survey_question.question_id, |
||
| 2114 | survey_question.survey_id, |
||
| 2115 | survey_question.survey_question, |
||
| 2116 | survey_question.display, |
||
| 2117 | survey_question.max_value, |
||
| 2118 | survey_question.sort, |
||
| 2119 | survey_question.type, |
||
| 2120 | survey_question_option.question_option_id, |
||
| 2121 | survey_question_option.option_text, |
||
| 2122 | survey_question_option.sort as option_sort |
||
| 2123 | FROM $table_survey_question survey_question |
||
| 2124 | LEFT JOIN $table_survey_question_option survey_question_option |
||
| 2125 | ON |
||
| 2126 | survey_question.question_id = survey_question_option.question_id AND |
||
| 2127 | survey_question_option.c_id = $course_id |
||
| 2128 | WHERE |
||
| 2129 | survey_question.survey_id = '".Database::escape_string( |
||
| 2130 | $_GET['survey_id'] |
||
| 2131 | )."' AND |
||
| 2132 | survey_question.c_id = $course_id |
||
| 2133 | ORDER BY survey_question.sort, survey_question_option.sort ASC"; |
||
| 2134 | $result = Database::query($sql); |
||
| 2135 | View Code Duplication | while ($row = Database::fetch_array($result, 'ASSOC')) { |
|
| 2136 | if ($row['type'] != 'pagebreak') { |
||
| 2137 | $questions[$row['sort']]['question_id'] = $row['question_id']; |
||
| 2138 | $questions[$row['sort']]['survey_id'] = $row['survey_id']; |
||
| 2139 | $questions[$row['sort']]['survey_question'] = $row['survey_question']; |
||
| 2140 | $questions[$row['sort']]['display'] = $row['display']; |
||
| 2141 | $questions[$row['sort']]['type'] = $row['type']; |
||
| 2142 | $questions[$row['sort']]['maximum_score'] = $row['max_value']; |
||
| 2143 | $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text']; |
||
| 2144 | } |
||
| 2145 | } |
||
| 2146 | |||
| 2147 | // Getting all the answers of the user |
||
| 2148 | $sql = "SELECT * FROM $table_survey_answer |
||
| 2149 | WHERE |
||
| 2150 | c_id = $course_id AND |
||
| 2151 | survey_id = '".intval($_GET['survey_id'])."' AND |
||
| 2152 | user = '".Database::escape_string($_GET['user'])."'"; |
||
| 2153 | $result = Database::query($sql); |
||
| 2154 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 2155 | $answers[$row['question_id']][] = $row['option_id']; |
||
| 2156 | $all_answers[$row['question_id']][] = $row; |
||
| 2157 | } |
||
| 2158 | |||
| 2159 | // Displaying all the questions |
||
| 2160 | |||
| 2161 | foreach ($questions as & $question) { |
||
| 2162 | // If the question type is a scoring then we have to format the answers differently |
||
| 2163 | switch ($question['type']) { |
||
| 2164 | case 'score': |
||
| 2165 | $finalAnswer = array(); |
||
| 2166 | if (is_array($question) && is_array($all_answers)) { |
||
| 2167 | foreach ($all_answers[$question['question_id']] as $key => & $answer_array) { |
||
| 2168 | $finalAnswer[$answer_array['option_id']] = $answer_array['value']; |
||
| 2169 | } |
||
| 2170 | } |
||
| 2171 | break; |
||
| 2172 | case 'multipleresponse': |
||
| 2173 | $finalAnswer = isset($answers[$question['question_id']]) ? $answers[$question['question_id']] : ''; |
||
| 2174 | break; |
||
| 2175 | default: |
||
| 2176 | $finalAnswer = ''; |
||
| 2177 | if (isset($all_answers[$question['question_id']])) { |
||
| 2178 | $finalAnswer = $all_answers[$question['question_id']][0]['option_id']; |
||
| 2179 | } |
||
| 2180 | break; |
||
| 2181 | } |
||
| 2182 | |||
| 2183 | $ch_type = 'ch_'.$question['type']; |
||
| 2184 | /** @var survey_question $display */ |
||
| 2185 | $display = new $ch_type; |
||
| 2186 | |||
| 2187 | $url = api_get_self(); |
||
| 2188 | $form = new FormValidator('question', 'post', $url); |
||
| 2189 | $form->addHtml('<div class="survey_question_wrapper"><div class="survey_question">'); |
||
| 2190 | $form->addHtml($question['survey_question']); |
||
| 2191 | $display->render($form, $question, $finalAnswer); |
||
| 2192 | $form->addHtml('</div></div>'); |
||
| 2193 | $form->display(); |
||
| 2194 | } |
||
| 2195 | } |
||
| 2196 | } |
||
| 2197 | |||
| 2198 | /** |
||
| 2199 | * This function displays the report by question. |
||
| 2200 | * |
||
| 2201 | * It displays a table with all the options of the question and the number of users who have answered positively on the option. |
||
| 2202 | * The number of users who answered positive on a given option is expressed in an absolute number, in a percentage of the total |
||
| 2203 | * and graphically using bars |
||
| 2204 | * By clicking on the absolute number you get a list with the persons who have answered this. |
||
| 2205 | * You can then click on the name of the person and you will then go to the report by user where you see all the |
||
| 2206 | * answers of that user. |
||
| 2207 | * |
||
| 2208 | * @param array All the survey data |
||
| 2209 | * @return string html code that displays the report by question |
||
| 2210 | * @todo allow switching between horizontal and vertical. |
||
| 2211 | * @todo multiple response: percentage are probably not OK |
||
| 2212 | * @todo the question and option text have to be shortened and should expand when the user clicks on it. |
||
| 2213 | * @todo the pagebreak and comment question types should not be shown => removed from $survey_data before |
||
| 2214 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 2215 | * @version February 2007 - Updated March 2008 |
||
| 2216 | */ |
||
| 2217 | public static function display_question_report($survey_data) |
||
| 2218 | { |
||
| 2219 | $singlePage = isset($_GET['single_page']) ? intval($_GET['single_page']) : 0; |
||
| 2220 | $course_id = api_get_course_int_id(); |
||
| 2221 | // Database table definitions |
||
| 2222 | $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); |
||
| 2223 | $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); |
||
| 2224 | $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); |
||
| 2225 | |||
| 2226 | // Determining the offset of the sql statement (the n-th question of the survey) |
||
| 2227 | $offset = !isset($_GET['question']) ? 0 : intval($_GET['question']); |
||
| 2228 | $currentQuestion = isset($_GET['question']) ? intval($_GET['question']) : 0; |
||
| 2229 | $questions = array(); |
||
| 2230 | $surveyId = intval($_GET['survey_id']); |
||
| 2231 | $action = Security::remove_XSS($_GET['action']); |
||
| 2232 | |||
| 2233 | echo '<div class="actions">'; |
||
| 2234 | echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.$surveyId.'">'. |
||
| 2235 | Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('ReportingOverview'),'',ICON_SIZE_MEDIUM).'</a>'; |
||
| 2236 | echo '</div>'; |
||
| 2237 | |||
| 2238 | if ($survey_data['number_of_questions'] > 0) { |
||
| 2239 | $limitStatement = null; |
||
| 2240 | if (!$singlePage) { |
||
| 2241 | echo '<div id="question_report_questionnumbers" class="pagination">'; |
||
| 2242 | if ($currentQuestion != 0) { |
||
| 2243 | echo '<li><a href="' . api_get_path(WEB_CODE_PATH) . 'survey/reporting.php?action=' . $action . '&' . api_get_cidreq() . '&survey_id=' . $surveyId . '&question=' . ($offset - 1) . '">' . get_lang('PreviousQuestion') . '</a></li>'; |
||
| 2244 | } |
||
| 2245 | |||
| 2246 | for ($i = 1; $i <= $survey_data['number_of_questions']; $i++) { |
||
| 2247 | if ($offset != $i - 1) { |
||
| 2248 | echo '<li><a href="' . api_get_path(WEB_CODE_PATH) . 'survey/reporting.php?action=' . $action . '&' . api_get_cidreq() . '&survey_id=' . $surveyId . '&question=' . ($i - 1) . '">' . $i . '</a></li>'; |
||
| 2249 | } else { |
||
| 2250 | echo '<li class="disabled"s><a href="#">' . $i . '</a></li>'; |
||
| 2251 | } |
||
| 2252 | } |
||
| 2253 | if ($currentQuestion < ($survey_data['number_of_questions'] - 1)) { |
||
| 2254 | echo '<li><a href="' . api_get_path(WEB_CODE_PATH) . 'survey/reporting.php?action=' . $action . '&' . api_get_cidreq() . '&survey_id=' . $surveyId . '&question=' . ($offset + 1) . '">' . get_lang('NextQuestion') . '</li></a>'; |
||
| 2255 | } |
||
| 2256 | echo '</ul>'; |
||
| 2257 | echo '</div>'; |
||
| 2258 | $limitStatement = " LIMIT $offset, 1"; |
||
| 2259 | } |
||
| 2260 | |||
| 2261 | // Getting the question information |
||
| 2262 | $sql = "SELECT * FROM $table_survey_question |
||
| 2263 | WHERE |
||
| 2264 | c_id = $course_id AND |
||
| 2265 | survey_id='".Database::escape_string($_GET['survey_id'])."' AND |
||
| 2266 | type<>'pagebreak' AND type<>'comment' |
||
| 2267 | ORDER BY sort ASC |
||
| 2268 | $limitStatement"; |
||
| 2269 | $result = Database::query($sql); |
||
| 2270 | |||
| 2271 | while ($row = Database::fetch_array($result)) { |
||
| 2272 | $questions[$row['question_id']] = $row; |
||
| 2273 | } |
||
| 2274 | } |
||
| 2275 | |||
| 2276 | foreach ($questions as $question) { |
||
| 2277 | $chartData = array(); |
||
| 2278 | $options = array(); |
||
| 2279 | echo '<div class="title-question">'; |
||
| 2280 | echo strip_tags(isset($question['survey_question']) ? $question['survey_question'] : null); |
||
| 2281 | echo '</div>'; |
||
| 2282 | |||
| 2283 | if ($question['type'] == 'score') { |
||
| 2284 | /** @todo This function should return the options as this is needed further in the code */ |
||
| 2285 | $options = SurveyUtil::display_question_report_score($survey_data, $question, $offset); |
||
| 2286 | } elseif ($question['type'] == 'open') { |
||
| 2287 | /** @todo Also get the user who has answered this */ |
||
| 2288 | $sql = "SELECT * FROM $table_survey_answer |
||
| 2289 | WHERE |
||
| 2290 | c_id = $course_id AND |
||
| 2291 | survey_id='" . intval($_GET['survey_id']) . "' AND |
||
| 2292 | question_id = '" . intval($question['question_id']) . "'"; |
||
| 2293 | $result = Database::query($sql); |
||
| 2294 | while ($row = Database::fetch_array($result)) { |
||
| 2295 | echo $row['option_id'] . '<hr noshade="noshade" size="1" />'; |
||
| 2296 | } |
||
| 2297 | } else { |
||
| 2298 | // Getting the options ORDER BY sort ASC |
||
| 2299 | $sql = "SELECT * FROM $table_survey_question_option |
||
| 2300 | WHERE |
||
| 2301 | c_id = $course_id AND |
||
| 2302 | survey_id='" . intval($_GET['survey_id']) . "' |
||
| 2303 | AND question_id = '" . intval($question['question_id']) . "' |
||
| 2304 | ORDER BY sort ASC"; |
||
| 2305 | $result = Database::query($sql); |
||
| 2306 | while ($row = Database::fetch_array($result)) { |
||
| 2307 | $options[$row['question_option_id']] = $row; |
||
| 2308 | } |
||
| 2309 | |||
| 2310 | // Getting the answers |
||
| 2311 | $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer |
||
| 2312 | WHERE |
||
| 2313 | c_id = $course_id AND |
||
| 2314 | survey_id='" . intval($_GET['survey_id']) . "' |
||
| 2315 | AND question_id = '" . intval($question['question_id']) . "' |
||
| 2316 | GROUP BY option_id, value"; |
||
| 2317 | $result = Database::query($sql); |
||
| 2318 | $number_of_answers = array(); |
||
| 2319 | $data = array(); |
||
| 2320 | View Code Duplication | while ($row = Database::fetch_array($result)) { |
|
| 2321 | if (!isset($number_of_answers[$row['question_id']])) { |
||
| 2322 | $number_of_answers[$row['question_id']] = 0; |
||
| 2323 | } |
||
| 2324 | $number_of_answers[$row['question_id']] += $row['total']; |
||
| 2325 | $data[$row['option_id']] = $row; |
||
| 2326 | } |
||
| 2327 | |||
| 2328 | foreach ($options as $option) { |
||
| 2329 | $optionText = strip_tags($option['option_text']); |
||
| 2330 | $optionText = html_entity_decode($optionText); |
||
| 2331 | $votes = isset($data[$option['question_option_id']]['total']) ? |
||
| 2332 | $data[$option['question_option_id']]['total'] : |
||
| 2333 | '0'; |
||
| 2334 | array_push($chartData, array('option' => $optionText, 'votes' => $votes)); |
||
| 2335 | } |
||
| 2336 | $chartContainerId = 'chartContainer'.$question['question_id']; |
||
| 2337 | echo '<div id="'.$chartContainerId.'" class="col-md-12">'; |
||
| 2338 | echo self::drawChart($chartData, false, $chartContainerId); |
||
| 2339 | |||
| 2340 | // displaying the table: headers |
||
| 2341 | |||
| 2342 | echo '<table class="display-survey table">'; |
||
| 2343 | echo ' <tr>'; |
||
| 2344 | echo ' <th> </th>'; |
||
| 2345 | echo ' <th>' . get_lang('AbsoluteTotal') . '</th>'; |
||
| 2346 | echo ' <th>' . get_lang('Percentage') . '</th>'; |
||
| 2347 | echo ' <th>' . get_lang('VisualRepresentation') . '</th>'; |
||
| 2348 | echo ' <tr>'; |
||
| 2349 | |||
| 2350 | // Displaying the table: the content |
||
| 2351 | if (is_array($options)) { |
||
| 2352 | foreach ($options as $key => & $value) { |
||
| 2353 | $absolute_number = null; |
||
| 2354 | if (isset($data[$value['question_option_id']])) { |
||
| 2355 | $absolute_number = $data[$value['question_option_id']]['total']; |
||
| 2356 | } |
||
| 2357 | if ($question['type'] == 'percentage' && empty($absolute_number)) { |
||
| 2358 | continue; |
||
| 2359 | } |
||
| 2360 | if ($number_of_answers[$option['question_id']] == 0) { |
||
| 2361 | $answers_number = 0; |
||
| 2362 | } else { |
||
| 2363 | $answers_number = $absolute_number / $number_of_answers[$option['question_id']] * 100; |
||
| 2364 | } |
||
| 2365 | echo ' <tr>'; |
||
| 2366 | echo ' <td class="center">' . $value['option_text'] . '</td>'; |
||
| 2367 | echo ' <td class="center">'; |
||
| 2368 | if ($absolute_number != 0) { |
||
| 2369 | echo '<a href="' . api_get_path(WEB_CODE_PATH) . 'survey/reporting.php?action=' . $action . '&survey_id=' . $surveyId . '&question=' . $offset . '&viewoption=' . $value['question_option_id'] . '">' . $absolute_number . '</a>'; |
||
| 2370 | } else { |
||
| 2371 | echo '0'; |
||
| 2372 | } |
||
| 2373 | |||
| 2374 | echo ' </td>'; |
||
| 2375 | echo ' <td class="center">' . round($answers_number, 2) . ' %</td>'; |
||
| 2376 | echo ' <td class="center">'; |
||
| 2377 | $size = $answers_number * 2; |
||
| 2378 | if ($size > 0) { |
||
| 2379 | echo '<div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:' . $size . 'px"> </div>'; |
||
| 2380 | } else { |
||
| 2381 | echo '<div style="text-align: left;">' . get_lang("NoDataAvailable") . '</div>'; |
||
| 2382 | } |
||
| 2383 | echo ' </td>'; |
||
| 2384 | echo ' </tr>'; |
||
| 2385 | } |
||
| 2386 | } |
||
| 2387 | // displaying the table: footer (totals) |
||
| 2388 | echo ' <tr>'; |
||
| 2389 | echo ' <td class="total"><b>' . get_lang('Total') . '</b></td>'; |
||
| 2390 | echo ' <td class="total"><b>' . ($number_of_answers[$option['question_id']] == 0 ? '0' : $number_of_answers[$option['question_id']]) . '</b></td>'; |
||
| 2391 | echo ' <td class="total"> </td>'; |
||
| 2392 | echo ' <td class="total"> </td>'; |
||
| 2393 | echo ' </tr>'; |
||
| 2394 | |||
| 2395 | echo '</table>'; |
||
| 2396 | |||
| 2397 | echo '</div>'; |
||
| 2398 | } |
||
| 2399 | } |
||
| 2400 | if (isset($_GET['viewoption'])) { |
||
| 2401 | echo '<div class="answered-people">'; |
||
| 2402 | |||
| 2403 | echo '<h4>'.get_lang('PeopleWhoAnswered').': '.strip_tags($options[Security::remove_XSS($_GET['viewoption'])]['option_text']).'</h4>'; |
||
| 2404 | |||
| 2405 | if (is_numeric($_GET['value'])) { |
||
| 2406 | $sql_restriction = "AND value='".Database::escape_string($_GET['value'])."'"; |
||
| 2407 | } |
||
| 2408 | |||
| 2409 | $sql = "SELECT user FROM $table_survey_answer |
||
| 2410 | WHERE |
||
| 2411 | c_id = $course_id AND |
||
| 2412 | option_id = '".Database::escape_string($_GET['viewoption'])."' |
||
| 2413 | $sql_restriction"; |
||
| 2414 | $result = Database::query($sql); |
||
| 2415 | echo '<ul>'; |
||
| 2416 | View Code Duplication | while ($row = Database::fetch_array($result)) { |
|
| 2417 | $user_info = api_get_user_info($row['user']); |
||
| 2418 | echo '<li><a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=userreport&survey_id='.$surveyId.'&user='.$row['user'].'">'.$user_info['complete_name'].'</a></li>'; |
||
| 2419 | } |
||
| 2420 | echo '</ul>'; |
||
| 2421 | echo '</div>'; |
||
| 2422 | } |
||
| 2423 | } |
||
| 2424 | |||
| 2425 | /** |
||
| 2426 | * Display score data about a survey question |
||
| 2427 | * @param array Question info |
||
| 2428 | * @param integer The offset of results shown |
||
| 2429 | * @return void (direct output) |
||
| 2430 | */ |
||
| 2431 | public static function display_question_report_score($survey_data, $question, $offset) |
||
| 2432 | { |
||
| 2433 | // Database table definitions |
||
| 2434 | $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); |
||
| 2435 | $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); |
||
| 2436 | |||
| 2437 | $course_id = api_get_course_int_id(); |
||
| 2438 | |||
| 2439 | // Getting the options |
||
| 2440 | $sql = "SELECT * FROM $table_survey_question_option |
||
| 2441 | WHERE |
||
| 2442 | c_id = $course_id AND |
||
| 2443 | survey_id='".Database::escape_string($_GET['survey_id'])."' AND |
||
| 2444 | question_id = '".Database::escape_string($question['question_id'])."' |
||
| 2445 | ORDER BY sort ASC"; |
||
| 2446 | $result = Database::query($sql); |
||
| 2447 | while ($row = Database::fetch_array($result)) { |
||
| 2448 | $options[$row['question_option_id']] = $row; |
||
| 2449 | } |
||
| 2450 | |||
| 2451 | // Getting the answers |
||
| 2452 | $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer |
||
| 2453 | WHERE |
||
| 2454 | c_id = $course_id AND |
||
| 2455 | survey_id='".Database::escape_string($_GET['survey_id'])."' AND |
||
| 2456 | question_id = '".Database::escape_string($question['question_id'])."' |
||
| 2457 | GROUP BY option_id, value"; |
||
| 2458 | $result = Database::query($sql); |
||
| 2459 | $number_of_answers = 0; |
||
| 2460 | View Code Duplication | while ($row = Database::fetch_array($result)) { |
|
| 2461 | $number_of_answers += $row['total']; |
||
| 2462 | $data[$row['option_id']][$row['value']] = $row; |
||
| 2463 | } |
||
| 2464 | |||
| 2465 | $chartData = array(); |
||
| 2466 | foreach ($options as $option) { |
||
| 2467 | $optionText = strip_tags($option['option_text']); |
||
| 2468 | $optionText = html_entity_decode($optionText); |
||
| 2469 | for ($i = 1; $i <= $question['max_value']; $i++) { |
||
| 2470 | $votes = $data[$option['question_option_id']][$i]['total']; |
||
| 2471 | if (empty($votes)) { |
||
| 2472 | $votes = '0'; |
||
| 2473 | } |
||
| 2474 | array_push( |
||
| 2475 | $chartData, |
||
| 2476 | array( |
||
| 2477 | 'serie' => $optionText, |
||
| 2478 | 'option' => $i, |
||
| 2479 | 'votes' => $votes |
||
| 2480 | ) |
||
| 2481 | ); |
||
| 2482 | } |
||
| 2483 | } |
||
| 2484 | echo '<div id="chartContainer" class="col-md-12">'; |
||
| 2485 | echo self::drawChart($chartData, true); |
||
| 2486 | echo '</div>'; |
||
| 2487 | |||
| 2488 | // Displaying the table: headers |
||
| 2489 | echo '<table class="data_table">'; |
||
| 2490 | echo ' <tr>'; |
||
| 2491 | echo ' <th> </th>'; |
||
| 2492 | echo ' <th>'.get_lang('Score').'</th>'; |
||
| 2493 | echo ' <th>'.get_lang('AbsoluteTotal').'</th>'; |
||
| 2494 | echo ' <th>'.get_lang('Percentage').'</th>'; |
||
| 2495 | echo ' <th>'.get_lang('VisualRepresentation').'</th>'; |
||
| 2496 | echo ' <tr>'; |
||
| 2497 | // Displaying the table: the content |
||
| 2498 | foreach ($options as $key => & $value) { |
||
| 2499 | for ($i = 1; $i <= $question['max_value']; $i++) { |
||
| 2500 | $absolute_number = $data[$value['question_option_id']][$i]['total']; |
||
| 2501 | echo ' <tr>'; |
||
| 2502 | echo ' <td>'.$value['option_text'].'</td>'; |
||
| 2503 | echo ' <td>'.$i.'</td>'; |
||
| 2504 | echo ' <td><a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action='.$action.'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&question='.Security::remove_XSS($offset).'&viewoption='.$value['question_option_id'].'&value='.$i.'">'.$absolute_number.'</a></td>'; |
||
| 2505 | echo ' <td>'.round($absolute_number/$number_of_answers*100, 2).' %</td>'; |
||
| 2506 | echo ' <td>'; |
||
| 2507 | $size = ($absolute_number/$number_of_answers*100*2); |
||
| 2508 | if ($size > 0) { |
||
| 2509 | echo ' <div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px"> </div>'; |
||
| 2510 | } |
||
| 2511 | echo ' </td>'; |
||
| 2512 | echo ' </tr>'; |
||
| 2513 | } |
||
| 2514 | } |
||
| 2515 | // Displaying the table: footer (totals) |
||
| 2516 | echo ' <tr>'; |
||
| 2517 | echo ' <td style="border-top:1px solid black"><b>'.get_lang('Total').'</b></td>'; |
||
| 2518 | echo ' <td style="border-top:1px solid black"> </td>'; |
||
| 2519 | echo ' <td style="border-top:1px solid black"><b>'.$number_of_answers.'</b></td>'; |
||
| 2520 | echo ' <td style="border-top:1px solid black"> </td>'; |
||
| 2521 | echo ' <td style="border-top:1px solid black"> </td>'; |
||
| 2522 | echo ' </tr>'; |
||
| 2523 | |||
| 2524 | echo '</table>'; |
||
| 2525 | } |
||
| 2526 | |||
| 2527 | /** |
||
| 2528 | * This functions displays the complete reporting |
||
| 2529 | * @return string HTML code |
||
| 2530 | * @todo open questions are not in the complete report yet. |
||
| 2531 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 2532 | * @version February 2007 |
||
| 2533 | */ |
||
| 2534 | public static function display_complete_report($survey_data) |
||
| 2535 | { |
||
| 2536 | // Database table definitions |
||
| 2537 | $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); |
||
| 2538 | $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); |
||
| 2539 | $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); |
||
| 2540 | |||
| 2541 | // Actions bar |
||
| 2542 | echo '<div class="actions">'; |
||
| 2543 | echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.Security::remove_XSS($_GET['survey_id']).'"> |
||
| 2544 | '.Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('ReportingOverview'),'',ICON_SIZE_MEDIUM).'</a>'; |
||
| 2545 | echo '<a class="survey_export_link" href="javascript: void(0);" onclick="document.form1a.submit();"> |
||
| 2546 | '.Display::return_icon('export_csv.png',get_lang('ExportAsCSV'),'',ICON_SIZE_MEDIUM).'</a>'; |
||
| 2547 | echo '<a class="survey_export_link" href="javascript: void(0);" onclick="document.form1b.submit();"> |
||
| 2548 | '.Display::return_icon('export_excel.png',get_lang('ExportAsXLS'),'',ICON_SIZE_MEDIUM).'</a>'; |
||
| 2549 | echo '</div>'; |
||
| 2550 | |||
| 2551 | // The form |
||
| 2552 | echo '<form id="form1a" name="form1a" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">'; |
||
| 2553 | echo '<input type="hidden" name="export_report" value="export_report">'; |
||
| 2554 | echo '<input type="hidden" name="export_format" value="csv">'; |
||
| 2555 | echo '</form>'; |
||
| 2556 | echo '<form id="form1b" name="form1b" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">'; |
||
| 2557 | echo '<input type="hidden" name="export_report" value="export_report">'; |
||
| 2558 | echo '<input type="hidden" name="export_format" value="xls">'; |
||
| 2559 | echo '</form>'; |
||
| 2560 | |||
| 2561 | echo '<form id="form2" name="form2" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">'; |
||
| 2562 | |||
| 2563 | // The table |
||
| 2564 | echo '<br /><table class="data_table" border="1">'; |
||
| 2565 | // Getting the number of options per question |
||
| 2566 | echo ' <tr>'; |
||
| 2567 | echo ' <th>'; |
||
| 2568 | if (isset($_POST['submit_question_filter']) && $_POST['submit_question_filter'] || |
||
| 2569 | isset($_POST['export_report']) && $_POST['export_report']) { |
||
| 2570 | echo '<button class="cancel" type="submit" name="reset_question_filter" value="'.get_lang('ResetQuestionFilter').'">'.get_lang('ResetQuestionFilter').'</button>'; |
||
| 2571 | } |
||
| 2572 | echo '<button class="save" type="submit" name="submit_question_filter" value="'.get_lang('SubmitQuestionFilter').'">'.get_lang('SubmitQuestionFilter').'</button>'; |
||
| 2573 | echo '</th>'; |
||
| 2574 | |||
| 2575 | $display_extra_user_fields = false; |
||
| 2576 | if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter'] || |
||
| 2577 | isset($_POST['export_report']) && $_POST['export_report']) || !empty($_POST['fields_filter'])) { |
||
| 2578 | // Show user fields section with a big th colspan that spans over all fields |
||
| 2579 | $extra_user_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', false, true); |
||
| 2580 | $num = count($extra_user_fields); |
||
| 2581 | if ($num > 0 ) { |
||
| 2582 | echo '<th '.($num>0?' colspan="'.$num.'"':'').'>'; |
||
| 2583 | echo '<label><input type="checkbox" name="fields_filter" value="1" checked="checked"/> '; |
||
| 2584 | echo get_lang('UserFields'); |
||
| 2585 | echo '</label>'; |
||
| 2586 | echo '</th>'; |
||
| 2587 | $display_extra_user_fields = true; |
||
| 2588 | } |
||
| 2589 | } |
||
| 2590 | |||
| 2591 | $course_id = api_get_course_int_id(); |
||
| 2592 | |||
| 2593 | // Get all the questions ordered by the "sort" column |
||
| 2594 | // <hub> modify the query to display open questions too |
||
| 2595 | // $sql = "SELECT q.question_id, q.type, q.survey_question, count(o.question_option_id) as number_of_options |
||
| 2596 | // FROM $table_survey_question q LEFT JOIN $table_survey_question_option o |
||
| 2597 | // ON q.question_id = o.question_id |
||
| 2598 | // WHERE q.question_id = o.question_id |
||
| 2599 | // AND q.survey_id = '".Database::escape_string($_GET['survey_id'])."' |
||
| 2600 | // GROUP BY q.question_id |
||
| 2601 | // ORDER BY q.sort ASC"; |
||
| 2602 | $sql = "SELECT q.question_id, q.type, q.survey_question, count(o.question_option_id) as number_of_options |
||
| 2603 | FROM $table_survey_question q LEFT JOIN $table_survey_question_option o |
||
| 2604 | ON q.question_id = o.question_id |
||
| 2605 | WHERE q.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND |
||
| 2606 | q.c_id = $course_id AND |
||
| 2607 | o.c_id = $course_id |
||
| 2608 | GROUP BY q.question_id |
||
| 2609 | ORDER BY q.sort ASC"; |
||
| 2610 | // </hub> |
||
| 2611 | $result = Database::query($sql); |
||
| 2612 | while ($row = Database::fetch_array($result)) { |
||
| 2613 | // We show the questions if |
||
| 2614 | // 1. there is no question filter and the export button has not been clicked |
||
| 2615 | // 2. there is a quesiton filter but the question is selected for display |
||
| 2616 | //if (!($_POST['submit_question_filter'] || $_POST['export_report']) || in_array($row['question_id'], $_POST['questions_filter'])) { |
||
| 2617 | if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter']) || |
||
| 2618 | (is_array($_POST['questions_filter']) && |
||
| 2619 | in_array($row['question_id'], $_POST['questions_filter']))) { |
||
| 2620 | // We do not show comment and pagebreak question types |
||
| 2621 | if ($row['type'] != 'comment' && $row['type'] != 'pagebreak') { |
||
| 2622 | echo ' <th'; |
||
| 2623 | // <hub> modified tst to include percentage |
||
| 2624 | if ($row['number_of_options'] > 0 && $row['type'] != 'percentage') { |
||
| 2625 | // </hub> |
||
| 2626 | echo ' colspan="'.$row['number_of_options'].'"'; |
||
| 2627 | } |
||
| 2628 | echo '>'; |
||
| 2629 | |||
| 2630 | echo '<label><input type="checkbox" name="questions_filter[]" value="'.$row['question_id'].'" checked="checked"/> '; |
||
| 2631 | echo $row['survey_question']; |
||
| 2632 | echo '</label>'; |
||
| 2633 | echo '</th>'; |
||
| 2634 | } |
||
| 2635 | // No column at all if it's not a question |
||
| 2636 | } |
||
| 2637 | $questions[$row['question_id']] = $row; |
||
| 2638 | } |
||
| 2639 | echo ' </tr>'; |
||
| 2640 | // Getting all the questions and options |
||
| 2641 | echo ' <tr>'; |
||
| 2642 | echo ' <th> </th>'; // the user column |
||
| 2643 | |||
| 2644 | if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter'] || |
||
| 2645 | isset($_POST['export_report']) && $_POST['export_report']) || !empty($_POST['fields_filter'])) { |
||
| 2646 | //show the fields names for user fields |
||
| 2647 | foreach($extra_user_fields as & $field) { |
||
| 2648 | echo '<th>'.$field[3].'</th>'; |
||
| 2649 | } |
||
| 2650 | } |
||
| 2651 | |||
| 2652 | // cells with option (none for open question) |
||
| 2653 | $sql = "SELECT sq.question_id, sq.survey_id, |
||
| 2654 | sq.survey_question, sq.display, |
||
| 2655 | sq.sort, sq.type, sqo.question_option_id, |
||
| 2656 | sqo.option_text, sqo.sort as option_sort |
||
| 2657 | FROM $table_survey_question sq |
||
| 2658 | LEFT JOIN $table_survey_question_option sqo |
||
| 2659 | ON sq.question_id = sqo.question_id |
||
| 2660 | WHERE |
||
| 2661 | sq.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND |
||
| 2662 | sq.c_id = $course_id AND |
||
| 2663 | sqo.c_id = $course_id |
||
| 2664 | ORDER BY sq.sort ASC, sqo.sort ASC"; |
||
| 2665 | $result = Database::query($sql); |
||
| 2666 | |||
| 2667 | $display_percentage_header = 1; // in order to display only once the cell option (and not 100 times) |
||
| 2668 | while ($row = Database::fetch_array($result)) { |
||
| 2669 | // We show the options if |
||
| 2670 | // 1. there is no question filter and the export button has not been clicked |
||
| 2671 | // 2. there is a question filter but the question is selected for display |
||
| 2672 | //if (!($_POST['submit_question_filter'] || $_POST['export_report']) || in_array($row['question_id'], $_POST['questions_filter'])) { |
||
| 2673 | if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter']) || |
||
| 2674 | (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])) |
||
| 2675 | ) { |
||
| 2676 | // <hub> modif 05-05-2010 |
||
| 2677 | // we do not show comment and pagebreak question types |
||
| 2678 | if ($row['type'] == 'open') { |
||
| 2679 | echo '<th> - </th>'; |
||
| 2680 | $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; |
||
| 2681 | $display_percentage_header = 1; |
||
| 2682 | } else if ($row['type'] == 'percentage' && $display_percentage_header) { |
||
| 2683 | echo '<th> % </th>'; |
||
| 2684 | $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; |
||
| 2685 | $display_percentage_header = 0; |
||
| 2686 | } else if ($row['type'] == 'percentage') { |
||
| 2687 | $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; |
||
| 2688 | } else if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak' AND $row['type'] <> 'percentage') { |
||
| 2689 | echo '<th>'; |
||
| 2690 | echo $row['option_text']; |
||
| 2691 | echo '</th>'; |
||
| 2692 | $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; |
||
| 2693 | $display_percentage_header = 1; |
||
| 2694 | } |
||
| 2695 | //no column at all if the question was not a question |
||
| 2696 | // </hub> |
||
| 2697 | } |
||
| 2698 | } |
||
| 2699 | |||
| 2700 | echo ' </tr>'; |
||
| 2701 | |||
| 2702 | // Getting all the answers of the users |
||
| 2703 | $old_user = ''; |
||
| 2704 | $answers_of_user = array(); |
||
| 2705 | $sql = "SELECT * FROM $table_survey_answer |
||
| 2706 | WHERE |
||
| 2707 | c_id = $course_id AND |
||
| 2708 | survey_id='".intval($_GET['survey_id'])."' |
||
| 2709 | ORDER BY answer_id, user ASC"; |
||
| 2710 | $result = Database::query($sql); |
||
| 2711 | $i = 1; |
||
| 2712 | while ($row = Database::fetch_array($result)) { |
||
| 2713 | if ($old_user != $row['user'] && $old_user != '') { |
||
| 2714 | $userParam = $old_user; |
||
| 2715 | if ($survey_data['anonymous'] != 0) { |
||
| 2716 | $userParam = $i; |
||
| 2717 | $i++; |
||
| 2718 | } |
||
| 2719 | SurveyUtil::display_complete_report_row( |
||
| 2720 | $survey_data, |
||
| 2721 | $possible_answers, |
||
| 2722 | $answers_of_user, |
||
| 2723 | $userParam, |
||
| 2724 | $questions, |
||
| 2725 | $display_extra_user_fields |
||
| 2726 | ); |
||
| 2727 | $answers_of_user=array(); |
||
| 2728 | } |
||
| 2729 | if (isset($questions[$row['question_id']]) && $questions[$row['question_id']]['type'] != 'open') { |
||
| 2730 | $answers_of_user[$row['question_id']][$row['option_id']] = $row; |
||
| 2731 | } else { |
||
| 2732 | $answers_of_user[$row['question_id']][0] = $row; |
||
| 2733 | } |
||
| 2734 | $old_user = $row['user']; |
||
| 2735 | } |
||
| 2736 | $userParam = $old_user; |
||
| 2737 | if ($survey_data['anonymous'] != 0) { |
||
| 2738 | $userParam = $i; |
||
| 2739 | $i++; |
||
| 2740 | } |
||
| 2741 | SurveyUtil::display_complete_report_row( |
||
| 2742 | $survey_data, |
||
| 2743 | $possible_answers, |
||
| 2744 | $answers_of_user, |
||
| 2745 | $userParam, |
||
| 2746 | $questions, |
||
| 2747 | $display_extra_user_fields |
||
| 2748 | ); |
||
| 2749 | // This is to display the last user |
||
| 2750 | echo '</table>'; |
||
| 2751 | echo '</form>'; |
||
| 2752 | } |
||
| 2753 | |||
| 2754 | /** |
||
| 2755 | * This function displays a row (= a user and his/her answers) in the table of the complete report. |
||
| 2756 | * |
||
| 2757 | * @param array $survey_data |
||
| 2758 | * @param array Possible options |
||
| 2759 | * @param array User answers |
||
| 2760 | * @param mixed User ID or user details string |
||
| 2761 | * @param boolean Whether to show extra user fields or not |
||
| 2762 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 2763 | * @version February 2007 - Updated March 2008 |
||
| 2764 | */ |
||
| 2765 | static function display_complete_report_row( |
||
| 2766 | $survey_data, |
||
| 2767 | $possible_options, |
||
| 2768 | $answers_of_user, |
||
| 2769 | $user, |
||
| 2770 | $questions, |
||
| 2771 | $display_extra_user_fields = false |
||
| 2772 | ) { |
||
| 2773 | $user = Security::remove_XSS($user); |
||
| 2774 | echo '<tr>'; |
||
| 2775 | if ($survey_data['anonymous'] == 0) { |
||
| 2776 | if (intval($user) !== 0) { |
||
| 2777 | $userInfo = api_get_user_info($user); |
||
| 2778 | if (!empty($userInfo)) { |
||
| 2779 | $user_displayed = $userInfo['complete_name']; |
||
| 2780 | } else { |
||
| 2781 | $user_displayed = '-'; |
||
| 2782 | } |
||
| 2783 | echo '<th><a href="'.api_get_self().'?action=userreport&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.$user.'">'. |
||
| 2784 | $user_displayed.'</a></th>'; // the user column |
||
| 2785 | } else { |
||
| 2786 | echo '<th>'.$user.'</th>'; // the user column |
||
| 2787 | } |
||
| 2788 | } else { |
||
| 2789 | echo '<th>' . get_lang('Anonymous') . ' ' . $user . '</th>'; |
||
| 2790 | } |
||
| 2791 | |||
| 2792 | if ($display_extra_user_fields) { |
||
| 2793 | // Show user fields data, if any, for this user |
||
| 2794 | $user_fields_values = UserManager::get_extra_user_data(intval($user), false, false, false, true); |
||
| 2795 | foreach ($user_fields_values as & $value) { |
||
| 2796 | echo '<td align="center">'.$value.'</td>'; |
||
| 2797 | } |
||
| 2798 | } |
||
| 2799 | if (is_array($possible_options)) { |
||
| 2800 | // <hub> modified to display open answers and percentage |
||
| 2801 | foreach ($possible_options as $question_id => & $possible_option) { |
||
| 2802 | if ($questions[$question_id]['type'] == 'open') { |
||
| 2803 | echo '<td align="center">'; |
||
| 2804 | echo $answers_of_user[$question_id]['0']['option_id']; |
||
| 2805 | echo '</td>'; |
||
| 2806 | } else { |
||
| 2807 | foreach ($possible_option as $option_id => & $value) { |
||
| 2808 | if ($questions[$question_id]['type'] == 'percentage') { |
||
| 2809 | View Code Duplication | if (!empty($answers_of_user[$question_id][$option_id])) { |
|
| 2810 | echo "<td align='center'>"; |
||
| 2811 | echo $answers_of_user[$question_id][$option_id]['value']; |
||
| 2812 | echo "</td>"; |
||
| 2813 | } |
||
| 2814 | } |
||
| 2815 | else { |
||
| 2816 | echo '<td align="center">'; |
||
| 2817 | if (!empty($answers_of_user[$question_id][$option_id])) { |
||
| 2818 | View Code Duplication | if ($answers_of_user[$question_id][$option_id]['value'] != 0) { |
|
| 2819 | echo $answers_of_user[$question_id][$option_id]['value']; |
||
| 2820 | } |
||
| 2821 | else { |
||
| 2822 | echo 'v'; |
||
| 2823 | } |
||
| 2824 | } |
||
| 2825 | } // </hub> |
||
| 2826 | } |
||
| 2827 | } |
||
| 2828 | } |
||
| 2829 | } |
||
| 2830 | echo '</tr>'; |
||
| 2831 | } |
||
| 2832 | |||
| 2833 | /** |
||
| 2834 | * Quite similar to display_complete_report(), returns an HTML string |
||
| 2835 | * that can be used in a csv file |
||
| 2836 | * @todo consider merging this function with display_complete_report |
||
| 2837 | * @return string The contents of a csv file |
||
| 2838 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 2839 | * @version February 2007 |
||
| 2840 | */ |
||
| 2841 | public static function export_complete_report($survey_data, $user_id = 0) |
||
| 2842 | { |
||
| 2843 | // Database table definitions |
||
| 2844 | $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); |
||
| 2845 | $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); |
||
| 2846 | $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); |
||
| 2847 | |||
| 2848 | // The first column |
||
| 2849 | $return = ';'; |
||
| 2850 | |||
| 2851 | // Show extra fields blank space (enough for extra fields on next line) |
||
| 2852 | |||
| 2853 | $extra_user_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', false, true); |
||
| 2854 | |||
| 2855 | $num = count($extra_user_fields); |
||
| 2856 | $return .= str_repeat(';', $num); |
||
| 2857 | |||
| 2858 | $course_id = api_get_course_int_id(); |
||
| 2859 | |||
| 2860 | $sql = "SELECT |
||
| 2861 | questions.question_id, |
||
| 2862 | questions.type, |
||
| 2863 | questions.survey_question, |
||
| 2864 | count(options.question_option_id) as number_of_options |
||
| 2865 | FROM $table_survey_question questions |
||
| 2866 | LEFT JOIN $table_survey_question_option options |
||
| 2867 | ON questions.question_id = options.question_id AND options.c_id = $course_id |
||
| 2868 | WHERE |
||
| 2869 | questions.survey_id = '".intval($_GET['survey_id'])."' AND |
||
| 2870 | questions.c_id = $course_id |
||
| 2871 | GROUP BY questions.question_id |
||
| 2872 | ORDER BY questions.sort ASC"; |
||
| 2873 | $result = Database::query($sql); |
||
| 2874 | while ($row = Database::fetch_array($result)) { |
||
| 2875 | // We show the questions if |
||
| 2876 | // 1. there is no question filter and the export button has not been clicked |
||
| 2877 | // 2. there is a quesiton filter but the question is selected for display |
||
| 2878 | if (!($_POST['submit_question_filter']) || |
||
| 2879 | (is_array($_POST['questions_filter']) && |
||
| 2880 | in_array($row['question_id'], $_POST['questions_filter'])) |
||
| 2881 | ) { |
||
| 2882 | // We do not show comment and pagebreak question types |
||
| 2883 | if ($row['type'] != 'comment' && $row['type'] != 'pagebreak') { |
||
| 2884 | if ($row['number_of_options'] == 0 && $row['type'] == 'open') { |
||
| 2885 | $return .= str_replace("\r\n",' ', api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES)).';'; |
||
| 2886 | } else { |
||
| 2887 | for ($ii = 0; $ii < $row['number_of_options']; $ii++) { |
||
| 2888 | $return .= str_replace("\r\n",' ', api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES)).';'; |
||
| 2889 | } |
||
| 2890 | } |
||
| 2891 | } |
||
| 2892 | } |
||
| 2893 | } |
||
| 2894 | $return .= "\n"; |
||
| 2895 | |||
| 2896 | // Getting all the questions and options |
||
| 2897 | $return .= ';'; |
||
| 2898 | |||
| 2899 | // Show the fields names for user fields |
||
| 2900 | if (!empty($extra_user_fields)) { |
||
| 2901 | foreach ($extra_user_fields as & $field) { |
||
| 2902 | $return .= '"'.str_replace("\r\n",' ',api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES)).'";'; |
||
| 2903 | } |
||
| 2904 | } |
||
| 2905 | |||
| 2906 | $sql = "SELECT |
||
| 2907 | survey_question.question_id, |
||
| 2908 | survey_question.survey_id, |
||
| 2909 | survey_question.survey_question, |
||
| 2910 | survey_question.display, |
||
| 2911 | survey_question.sort, |
||
| 2912 | survey_question.type, |
||
| 2913 | survey_question_option.question_option_id, |
||
| 2914 | survey_question_option.option_text, |
||
| 2915 | survey_question_option.sort as option_sort |
||
| 2916 | FROM $table_survey_question survey_question |
||
| 2917 | LEFT JOIN $table_survey_question_option survey_question_option |
||
| 2918 | ON |
||
| 2919 | survey_question.question_id = survey_question_option.question_id AND |
||
| 2920 | survey_question_option.c_id = $course_id |
||
| 2921 | WHERE |
||
| 2922 | survey_question.survey_id = '".intval($_GET['survey_id'])."' AND |
||
| 2923 | survey_question.c_id = $course_id |
||
| 2924 | ORDER BY survey_question.sort ASC, survey_question_option.sort ASC"; |
||
| 2925 | $result = Database::query($sql); |
||
| 2926 | $possible_answers = array(); |
||
| 2927 | $possible_answers_type = array(); |
||
| 2928 | while ($row = Database::fetch_array($result)) { |
||
| 2929 | // We show the options if |
||
| 2930 | // 1. there is no question filter and the export button has not been clicked |
||
| 2931 | // 2. there is a quesiton filter but the question is selected for display |
||
| 2932 | if (!($_POST['submit_question_filter']) || (is_array($_POST['questions_filter']) && |
||
| 2933 | in_array($row['question_id'], $_POST['questions_filter'])) |
||
| 2934 | ) { |
||
| 2935 | // We do not show comment and pagebreak question types |
||
| 2936 | if ($row['type'] != 'comment' && $row['type'] != 'pagebreak') { |
||
| 2937 | $row['option_text'] = str_replace(array("\r","\n"),array('',''),$row['option_text']); |
||
| 2938 | $return .= api_html_entity_decode(strip_tags($row['option_text']), ENT_QUOTES).';'; |
||
| 2939 | $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; |
||
| 2940 | $possible_answers_type[$row['question_id']] = $row['type']; |
||
| 2941 | } |
||
| 2942 | } |
||
| 2943 | } |
||
| 2944 | $return .= "\n"; |
||
| 2945 | |||
| 2946 | // Getting all the answers of the users |
||
| 2947 | $old_user = ''; |
||
| 2948 | $answers_of_user = array(); |
||
| 2949 | $sql = "SELECT * FROM $table_survey_answer |
||
| 2950 | WHERE c_id = $course_id AND survey_id='".Database::escape_string($_GET['survey_id'])."'"; |
||
| 2951 | if ($user_id != 0) { |
||
| 2952 | $sql .= "AND user='".Database::escape_string($user_id)."' "; |
||
| 2953 | } |
||
| 2954 | $sql .= "ORDER BY user ASC"; |
||
| 2955 | |||
| 2956 | $open_question_iterator = 1; |
||
| 2957 | $result = Database::query($sql); |
||
| 2958 | while ($row = Database::fetch_array($result)) { |
||
| 2959 | if ($old_user != $row['user'] && $old_user != '') { |
||
| 2960 | $return .= SurveyUtil::export_complete_report_row( |
||
| 2961 | $survey_data, |
||
| 2962 | $possible_answers, |
||
| 2963 | $answers_of_user, |
||
| 2964 | $old_user, |
||
| 2965 | true |
||
| 2966 | ); |
||
| 2967 | $answers_of_user=array(); |
||
| 2968 | } |
||
| 2969 | View Code Duplication | if($possible_answers_type[$row['question_id']] == 'open') { |
|
| 2970 | $temp_id = 'open'.$open_question_iterator; |
||
| 2971 | $answers_of_user[$row['question_id']][$temp_id] = $row; |
||
| 2972 | $open_question_iterator++; |
||
| 2973 | } else { |
||
| 2974 | $answers_of_user[$row['question_id']][$row['option_id']] = $row; |
||
| 2975 | } |
||
| 2976 | $old_user = $row['user']; |
||
| 2977 | } |
||
| 2978 | // This is to display the last user |
||
| 2979 | $return .= SurveyUtil::export_complete_report_row( |
||
| 2980 | $survey_data, |
||
| 2981 | $possible_answers, |
||
| 2982 | $answers_of_user, |
||
| 2983 | $old_user, |
||
| 2984 | true |
||
| 2985 | ); |
||
| 2986 | |||
| 2987 | return $return; |
||
| 2988 | } |
||
| 2989 | |||
| 2990 | /** |
||
| 2991 | * Add a line to the csv file |
||
| 2992 | * |
||
| 2993 | * @param array Possible answers |
||
| 2994 | * @param array User's answers |
||
| 2995 | * @param mixed User ID or user details as string - Used as a string in the result string |
||
| 2996 | * @param boolean Whether to display user fields or not |
||
| 2997 | * @return string One line of the csv file |
||
| 2998 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 2999 | * @version February 2007 |
||
| 3000 | */ |
||
| 3001 | static function export_complete_report_row( |
||
| 3002 | $survey_data, |
||
| 3003 | $possible_options, |
||
| 3004 | $answers_of_user, |
||
| 3005 | $user, |
||
| 3006 | $display_extra_user_fields = false |
||
| 3007 | ) { |
||
| 3008 | $return = ''; |
||
| 3009 | if ($survey_data['anonymous'] == 0) { |
||
| 3010 | if (intval($user) !== 0) { |
||
| 3011 | $userInfo = api_get_user_info($user); |
||
| 3012 | |||
| 3013 | if (!empty($userInfo)) { |
||
| 3014 | $user_displayed = $userInfo['complete_name']; |
||
| 3015 | } else { |
||
| 3016 | $user_displayed = '-'; |
||
| 3017 | } |
||
| 3018 | $return .= $user_displayed.';'; |
||
| 3019 | } else { |
||
| 3020 | $return .= $user.';'; |
||
| 3021 | } |
||
| 3022 | } else { |
||
| 3023 | $return .= '-;'; // The user column |
||
| 3024 | } |
||
| 3025 | |||
| 3026 | View Code Duplication | if ($display_extra_user_fields) { |
|
| 3027 | // Show user fields data, if any, for this user |
||
| 3028 | $user_fields_values = UserManager::get_extra_user_data($user,false,false, false, true); |
||
| 3029 | foreach ($user_fields_values as & $value) { |
||
| 3030 | $return .= '"'.str_replace('"', '""', api_html_entity_decode(strip_tags($value), ENT_QUOTES)).'";'; |
||
| 3031 | } |
||
| 3032 | } |
||
| 3033 | |||
| 3034 | if (is_array($possible_options)) { |
||
| 3035 | foreach ($possible_options as $question_id => $possible_option) { |
||
| 3036 | if (is_array($possible_option) && count($possible_option) > 0) { |
||
| 3037 | foreach ($possible_option as $option_id => & $value) { |
||
| 3038 | $my_answer_of_user = ($answers_of_user[$question_id] == null) ? array() : $answers_of_user[$question_id]; |
||
| 3039 | $key = array_keys($my_answer_of_user); |
||
| 3040 | if (substr($key[0], 0, 4) == 'open') { |
||
| 3041 | $return .= '"'.str_replace('"', '""', api_html_entity_decode(strip_tags($answers_of_user[$question_id][$key[0]]['option_id']), ENT_QUOTES)).'"'; |
||
| 3042 | } elseif (!empty($answers_of_user[$question_id][$option_id])) { |
||
| 3043 | //$return .= 'v'; |
||
| 3044 | if ($answers_of_user[$question_id][$option_id]['value'] != 0) { |
||
| 3045 | $return .= $answers_of_user[$question_id][$option_id]['value']; |
||
| 3046 | } else { |
||
| 3047 | $return .= 'v'; |
||
| 3048 | } |
||
| 3049 | } |
||
| 3050 | $return .= ';'; |
||
| 3051 | } |
||
| 3052 | } |
||
| 3053 | } |
||
| 3054 | } |
||
| 3055 | $return .= "\n"; |
||
| 3056 | return $return; |
||
| 3057 | } |
||
| 3058 | |||
| 3059 | /** |
||
| 3060 | * Quite similar to display_complete_report(), returns an HTML string |
||
| 3061 | * that can be used in a csv file |
||
| 3062 | * @todo consider merging this function with display_complete_report |
||
| 3063 | * @return string The contents of a csv file |
||
| 3064 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 3065 | * @version February 2007 |
||
| 3066 | */ |
||
| 3067 | static function export_complete_report_xls($survey_data, $filename, $user_id = 0) |
||
| 3068 | { |
||
| 3069 | $spreadsheet = new PHPExcel(); |
||
| 3070 | $spreadsheet->setActiveSheetIndex(0); |
||
| 3071 | $worksheet = $spreadsheet->getActiveSheet(); |
||
| 3072 | $line = 1; |
||
| 3073 | $column = 1; // Skip the first column (row titles) |
||
| 3074 | |||
| 3075 | // Show extra fields blank space (enough for extra fields on next line) |
||
| 3076 | // Show user fields section with a big th colspan that spans over all fields |
||
| 3077 | $extra_user_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', false, true); |
||
| 3078 | $num = count($extra_user_fields); |
||
| 3079 | for ($i = 0; $i < $num; $i++) { |
||
| 3080 | $worksheet->setCellValueByColumnAndRow($column, $line, ''); |
||
| 3081 | $column++; |
||
| 3082 | } |
||
| 3083 | $display_extra_user_fields = true; |
||
| 3084 | |||
| 3085 | // Database table definitions |
||
| 3086 | $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); |
||
| 3087 | $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); |
||
| 3088 | $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); |
||
| 3089 | |||
| 3090 | $course_id = api_get_course_int_id(); |
||
| 3091 | |||
| 3092 | // First line (questions) |
||
| 3093 | $sql = "SELECT |
||
| 3094 | questions.question_id, |
||
| 3095 | questions.type, |
||
| 3096 | questions.survey_question, |
||
| 3097 | count(options.question_option_id) as number_of_options |
||
| 3098 | FROM $table_survey_question questions |
||
| 3099 | LEFT JOIN $table_survey_question_option options |
||
| 3100 | ON questions.question_id = options.question_id AND options.c_id = $course_id |
||
| 3101 | WHERE |
||
| 3102 | questions.survey_id = '".intval($_GET['survey_id'])."' AND |
||
| 3103 | questions.c_id = $course_id |
||
| 3104 | GROUP BY questions.question_id |
||
| 3105 | ORDER BY questions.sort ASC"; |
||
| 3106 | $result = Database::query($sql); |
||
| 3107 | while ($row = Database::fetch_array($result)) { |
||
| 3108 | // We show the questions if |
||
| 3109 | // 1. there is no question filter and the export button has not been clicked |
||
| 3110 | // 2. there is a quesiton filter but the question is selected for display |
||
| 3111 | if (!(isset($_POST['submit_question_filter'])) || (is_array($_POST['questions_filter']) && |
||
| 3112 | in_array($row['question_id'], $_POST['questions_filter'])) |
||
| 3113 | ) { |
||
| 3114 | // We do not show comment and pagebreak question types |
||
| 3115 | if ($row['type'] != 'comment' && $row['type'] != 'pagebreak') { |
||
| 3116 | if ($row['number_of_options'] == 0 && $row['type'] == 'open') { |
||
| 3117 | $worksheet->setCellValueByColumnAndRow( |
||
| 3118 | $column, |
||
| 3119 | $line, |
||
| 3120 | api_html_entity_decode( |
||
| 3121 | strip_tags($row['survey_question']), |
||
| 3122 | ENT_QUOTES |
||
| 3123 | ) |
||
| 3124 | ); |
||
| 3125 | $column ++; |
||
| 3126 | } else { |
||
| 3127 | for ($ii = 0; $ii < $row['number_of_options']; $ii ++) { |
||
| 3128 | $worksheet->setCellValueByColumnAndRow( |
||
| 3129 | $column, |
||
| 3130 | $line, |
||
| 3131 | api_html_entity_decode( |
||
| 3132 | strip_tags($row['survey_question']), |
||
| 3133 | ENT_QUOTES |
||
| 3134 | ) |
||
| 3135 | ); |
||
| 3136 | $column ++; |
||
| 3137 | } |
||
| 3138 | } |
||
| 3139 | } |
||
| 3140 | } |
||
| 3141 | } |
||
| 3142 | $line++; |
||
| 3143 | $column = 1; |
||
| 3144 | |||
| 3145 | // Show extra field values |
||
| 3146 | if ($display_extra_user_fields) { |
||
| 3147 | // Show the fields names for user fields |
||
| 3148 | foreach ($extra_user_fields as & $field) { |
||
| 3149 | $worksheet->setCellValueByColumnAndRow( |
||
| 3150 | $column, |
||
| 3151 | $line, |
||
| 3152 | api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES) |
||
| 3153 | ); |
||
| 3154 | $column++; |
||
| 3155 | } |
||
| 3156 | } |
||
| 3157 | |||
| 3158 | // Getting all the questions and options (second line) |
||
| 3159 | $sql = "SELECT |
||
| 3160 | survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, |
||
| 3161 | survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort |
||
| 3162 | FROM $table_survey_question survey_question |
||
| 3163 | LEFT JOIN $table_survey_question_option survey_question_option |
||
| 3164 | ON survey_question.question_id = survey_question_option.question_id AND survey_question_option.c_id = $course_id |
||
| 3165 | WHERE survey_question.survey_id = '".intval($_GET['survey_id'])."' AND |
||
| 3166 | survey_question.c_id = $course_id |
||
| 3167 | ORDER BY survey_question.sort ASC, survey_question_option.sort ASC"; |
||
| 3168 | $result = Database::query($sql); |
||
| 3169 | $possible_answers = array(); |
||
| 3170 | $possible_answers_type = array(); |
||
| 3171 | while ($row = Database::fetch_array($result)) { |
||
| 3172 | // We show the options if |
||
| 3173 | // 1. there is no question filter and the export button has not been clicked |
||
| 3174 | // 2. there is a quesiton filter but the question is selected for display |
||
| 3175 | if (!(isset($_POST['submit_question_filter'])) || |
||
| 3176 | (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])) |
||
| 3177 | ) { |
||
| 3178 | // We do not show comment and pagebreak question types |
||
| 3179 | if ($row['type'] != 'comment' && $row['type'] != 'pagebreak') { |
||
| 3180 | $worksheet->setCellValueByColumnAndRow( |
||
| 3181 | $column, |
||
| 3182 | $line, |
||
| 3183 | api_html_entity_decode( |
||
| 3184 | strip_tags($row['option_text']), |
||
| 3185 | ENT_QUOTES |
||
| 3186 | ) |
||
| 3187 | ); |
||
| 3188 | $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; |
||
| 3189 | $possible_answers_type[$row['question_id']] = $row['type']; |
||
| 3190 | $column++; |
||
| 3191 | } |
||
| 3192 | } |
||
| 3193 | } |
||
| 3194 | |||
| 3195 | // Getting all the answers of the users |
||
| 3196 | $line ++; |
||
| 3197 | $column = 0; |
||
| 3198 | $old_user = ''; |
||
| 3199 | $answers_of_user = array(); |
||
| 3200 | $sql = "SELECT * FROM $table_survey_answer |
||
| 3201 | WHERE c_id = $course_id AND survey_id='".intval($_GET['survey_id'])."' "; |
||
| 3202 | if ($user_id != 0) { |
||
| 3203 | $sql .= "AND user='".intval($user_id)."' "; |
||
| 3204 | } |
||
| 3205 | $sql .= "ORDER BY user ASC"; |
||
| 3206 | |||
| 3207 | $open_question_iterator = 1; |
||
| 3208 | $result = Database::query($sql); |
||
| 3209 | while ($row = Database::fetch_array($result)) { |
||
| 3210 | if ($old_user != $row['user'] && $old_user != '') { |
||
| 3211 | $return = SurveyUtil::export_complete_report_row_xls( |
||
| 3212 | $survey_data, |
||
| 3213 | $possible_answers, |
||
| 3214 | $answers_of_user, |
||
| 3215 | $old_user, |
||
| 3216 | true |
||
| 3217 | ); |
||
| 3218 | foreach ($return as $elem) { |
||
| 3219 | $worksheet->setCellValueByColumnAndRow($column, $line, $elem); |
||
| 3220 | $column++; |
||
| 3221 | } |
||
| 3222 | $answers_of_user = array(); |
||
| 3223 | $line++; |
||
| 3224 | $column = 0; |
||
| 3225 | } |
||
| 3226 | View Code Duplication | if ($possible_answers_type[$row['question_id']] == 'open') { |
|
| 3227 | $temp_id = 'open'.$open_question_iterator; |
||
| 3228 | $answers_of_user[$row['question_id']][$temp_id] = $row; |
||
| 3229 | $open_question_iterator++; |
||
| 3230 | } else { |
||
| 3231 | $answers_of_user[$row['question_id']][$row['option_id']] = $row; |
||
| 3232 | } |
||
| 3233 | $old_user = $row['user']; |
||
| 3234 | } |
||
| 3235 | $return = SurveyUtil::export_complete_report_row_xls( |
||
| 3236 | $survey_data, |
||
| 3237 | $possible_answers, |
||
| 3238 | $answers_of_user, |
||
| 3239 | $old_user, |
||
| 3240 | true |
||
| 3241 | ); |
||
| 3242 | |||
| 3243 | // this is to display the last user |
||
| 3244 | foreach ($return as $elem) { |
||
| 3245 | $worksheet->setCellValueByColumnAndRow($column, $line, $elem); |
||
| 3246 | $column++; |
||
| 3247 | } |
||
| 3248 | |||
| 3249 | $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); |
||
| 3250 | $writer = new PHPExcel_Writer_Excel2007($spreadsheet); |
||
| 3251 | $writer->save($file); |
||
| 3252 | DocumentManager::file_send_for_download($file, true, $filename); |
||
| 3253 | |||
| 3254 | return null; |
||
| 3255 | } |
||
| 3256 | |||
| 3257 | /** |
||
| 3258 | * Add a line to the csv file |
||
| 3259 | * |
||
| 3260 | * @param array Possible answers |
||
| 3261 | * @param array User's answers |
||
| 3262 | * @param mixed User ID or user details as string - Used as a string in the result string |
||
| 3263 | * @param boolean Whether to display user fields or not |
||
| 3264 | * @return string One line of the csv file |
||
| 3265 | */ |
||
| 3266 | public static function export_complete_report_row_xls( |
||
| 3267 | $survey_data, |
||
| 3268 | $possible_options, |
||
| 3269 | $answers_of_user, |
||
| 3270 | $user, |
||
| 3271 | $display_extra_user_fields = false |
||
| 3272 | ) { |
||
| 3273 | $return = array(); |
||
| 3274 | if ($survey_data['anonymous'] == 0) { |
||
| 3275 | if (intval($user) !== 0) { |
||
| 3276 | $sql = 'SELECT firstname, lastname |
||
| 3277 | FROM '.Database::get_main_table(TABLE_MAIN_USER).' |
||
| 3278 | WHERE user_id='.intval($user); |
||
| 3279 | $rs = Database::query($sql); |
||
| 3280 | if($row = Database::fetch_array($rs)) { |
||
| 3281 | $user_displayed = api_get_person_name($row['firstname'], $row['lastname']); |
||
| 3282 | } else { |
||
| 3283 | $user_displayed = '-'; |
||
| 3284 | } |
||
| 3285 | $return[] = $user_displayed; |
||
| 3286 | } else { |
||
| 3287 | $return[] = $user; |
||
| 3288 | } |
||
| 3289 | } else { |
||
| 3290 | $return[] = '-'; // The user column |
||
| 3291 | } |
||
| 3292 | |||
| 3293 | if ($display_extra_user_fields) { |
||
| 3294 | //show user fields data, if any, for this user |
||
| 3295 | $user_fields_values = UserManager::get_extra_user_data(intval($user),false,false, false, true); |
||
| 3296 | foreach($user_fields_values as $value) { |
||
| 3297 | $return[] = api_html_entity_decode(strip_tags($value), ENT_QUOTES); |
||
| 3298 | } |
||
| 3299 | } |
||
| 3300 | |||
| 3301 | if (is_array($possible_options)) { |
||
| 3302 | foreach ($possible_options as $question_id => & $possible_option) { |
||
| 3303 | if (is_array($possible_option) && count($possible_option) > 0) { |
||
| 3304 | foreach ($possible_option as $option_id => & $value) { |
||
| 3305 | $my_answers_of_user = isset($answers_of_user[$question_id]) ? $answers_of_user[$question_id] : []; |
||
| 3306 | $key = array_keys($my_answers_of_user); |
||
| 3307 | if (isset($key[0]) && substr($key[0], 0, 4) == 'open') { |
||
| 3308 | $return[] = api_html_entity_decode(strip_tags($answers_of_user[$question_id][$key[0]]['option_id']), ENT_QUOTES); |
||
| 3309 | } elseif (!empty($answers_of_user[$question_id][$option_id])) { |
||
| 3310 | //$return .= 'v'; |
||
| 3311 | if ($answers_of_user[$question_id][$option_id]['value'] != 0) { |
||
| 3312 | $return[] = $answers_of_user[$question_id][$option_id]['value']; |
||
| 3313 | } else { |
||
| 3314 | $return[] = 'v'; |
||
| 3315 | } |
||
| 3316 | } else { |
||
| 3317 | $return[] = ''; |
||
| 3318 | } |
||
| 3319 | } |
||
| 3320 | } |
||
| 3321 | } |
||
| 3322 | } |
||
| 3323 | |||
| 3324 | return $return; |
||
| 3325 | } |
||
| 3326 | |||
| 3327 | /** |
||
| 3328 | * This function displays the comparative report which allows you to compare two questions |
||
| 3329 | * A comparative report creates a table where one question is on the x axis and a second question is on the y axis. |
||
| 3330 | * In the intersection is the number of people who have answerd positive on both options. |
||
| 3331 | * |
||
| 3332 | * @return string HTML code |
||
| 3333 | * |
||
| 3334 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 3335 | * @version February 2007 |
||
| 3336 | */ |
||
| 3337 | public static function display_comparative_report() |
||
| 3338 | { |
||
| 3339 | // Allowed question types for comparative report |
||
| 3340 | $allowed_question_types = array( |
||
| 3341 | 'yesno', |
||
| 3342 | 'multiplechoice', |
||
| 3343 | 'multipleresponse', |
||
| 3344 | 'dropdown', |
||
| 3345 | 'percentage', |
||
| 3346 | 'score', |
||
| 3347 | ); |
||
| 3348 | |||
| 3349 | // Getting all the questions |
||
| 3350 | $questions = SurveyManager::get_questions($_GET['survey_id']); |
||
| 3351 | |||
| 3352 | // Actions bar |
||
| 3353 | echo '<div class="actions">'; |
||
| 3354 | echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.intval($_GET['survey_id']).'">'. |
||
| 3355 | Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('ReportingOverview'),'',ICON_SIZE_MEDIUM).'</a>'; |
||
| 3356 | echo '</div>'; |
||
| 3357 | |||
| 3358 | // Displaying an information message that only the questions with predefined answers can be used in a comparative report |
||
| 3359 | Display::display_normal_message(get_lang('OnlyQuestionsWithPredefinedAnswers'), false); |
||
| 3360 | |||
| 3361 | // The form for selecting the axis of the table |
||
| 3362 | echo '<form id="form1" name="form1" method="get" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.intval($_GET['survey_id']).'&xaxis='.Security::remove_XSS($_GET['xaxis']).'&y='.Security::remove_XSS($_GET['yaxis']).'">'; |
||
| 3363 | // Survey_id |
||
| 3364 | echo '<input type="hidden" name="action" value="'.Security::remove_XSS($_GET['action']).'"/>'; |
||
| 3365 | echo '<input type="hidden" name="survey_id" value="'.Security::remove_XSS($_GET['survey_id']).'"/>'; |
||
| 3366 | // X axis |
||
| 3367 | echo get_lang('SelectXAxis').': '; |
||
| 3368 | echo '<select name="xaxis">'; |
||
| 3369 | echo '<option value="">---</option>'; |
||
| 3370 | View Code Duplication | foreach ($questions as $key => & $question) { |
|
| 3371 | if (is_array($allowed_question_types)) { |
||
| 3372 | if (in_array($question['type'], $allowed_question_types)) { |
||
| 3373 | echo '<option value="'.$question['question_id'].'"'; |
||
| 3374 | if (isset($_GET['xaxis']) && $_GET['xaxis'] == $question['question_id']) { |
||
| 3375 | echo ' selected="selected"'; |
||
| 3376 | } |
||
| 3377 | echo '">'.api_substr(strip_tags($question['question']), 0, 50).'</option>'; |
||
| 3378 | } |
||
| 3379 | } |
||
| 3380 | |||
| 3381 | } |
||
| 3382 | echo '</select><br /><br />'; |
||
| 3383 | // Y axis |
||
| 3384 | echo get_lang('SelectYAxis').': '; |
||
| 3385 | echo '<select name="yaxis">'; |
||
| 3386 | echo '<option value="">---</option>'; |
||
| 3387 | View Code Duplication | foreach ($questions as $key => &$question) { |
|
| 3388 | if (in_array($question['type'], $allowed_question_types)) { |
||
| 3389 | echo '<option value="'.$question['question_id'].'"'; |
||
| 3390 | if (isset($_GET['yaxis']) && $_GET['yaxis'] == $question['question_id']) { |
||
| 3391 | echo ' selected="selected"'; |
||
| 3392 | } |
||
| 3393 | echo '">'.api_substr(strip_tags($question['question']), 0, 50).'</option>'; |
||
| 3394 | } |
||
| 3395 | } |
||
| 3396 | echo '</select><br /><br />'; |
||
| 3397 | echo '<button class="save" type="submit" name="Submit" value="Submit">'.get_lang('CompareQuestions').'</button>'; |
||
| 3398 | echo '</form>'; |
||
| 3399 | |||
| 3400 | // Getting all the information of the x axis |
||
| 3401 | View Code Duplication | if (isset($_GET['xaxis']) && is_numeric($_GET['xaxis'])) { |
|
| 3402 | $question_x = SurveyManager::get_question($_GET['xaxis']); |
||
| 3403 | } |
||
| 3404 | |||
| 3405 | // Getting all the information of the y axis |
||
| 3406 | View Code Duplication | if (isset($_GET['yaxis']) && is_numeric($_GET['yaxis'])) { |
|
| 3407 | $question_y = SurveyManager::get_question($_GET['yaxis']); |
||
| 3408 | } |
||
| 3409 | |||
| 3410 | if (isset($_GET['xaxis']) && is_numeric($_GET['xaxis']) && isset($_GET['yaxis']) && is_numeric($_GET['yaxis'])) { |
||
| 3411 | // Getting the answers of the two questions |
||
| 3412 | $answers_x = SurveyUtil::get_answers_of_question_by_user($_GET['survey_id'], $_GET['xaxis']); |
||
| 3413 | $answers_y = SurveyUtil::get_answers_of_question_by_user($_GET['survey_id'], $_GET['yaxis']); |
||
| 3414 | |||
| 3415 | // Displaying the table |
||
| 3416 | $tableHtml = '<table border="1" class="data_table">'; |
||
| 3417 | |||
| 3418 | $xOptions = array(); |
||
| 3419 | // The header |
||
| 3420 | $tableHtml .= ' <tr>'; |
||
| 3421 | for ($ii = 0; $ii <= count($question_x['answers']); $ii++) { |
||
| 3422 | if ($ii == 0) { |
||
| 3423 | $tableHtml .= ' <th> </th>'; |
||
| 3424 | } else { |
||
| 3425 | if ($question_x['type'] == 'score') { |
||
| 3426 | for ($x = 1; $x <= $question_x['maximum_score']; $x++) { |
||
| 3427 | $tableHtml .= ' <th>'.$question_x['answers'][($ii-1)].'<br />'.$x.'</th>'; |
||
| 3428 | } |
||
| 3429 | $x = ''; |
||
| 3430 | } else { |
||
| 3431 | $tableHtml .= ' <th>'.$question_x['answers'][($ii-1)].'</th>'; |
||
| 3432 | } |
||
| 3433 | $optionText = strip_tags($question_x['answers'][$ii-1]); |
||
| 3434 | $optionText = html_entity_decode($optionText); |
||
| 3435 | array_push($xOptions, trim($optionText)); |
||
| 3436 | } |
||
| 3437 | } |
||
| 3438 | $tableHtml .= ' </tr>'; |
||
| 3439 | $chartData = array(); |
||
| 3440 | |||
| 3441 | // The main part |
||
| 3442 | for ($ij = 0; $ij < count($question_y['answers']); $ij++) { |
||
| 3443 | $currentYQuestion = strip_tags($question_y['answers'][$ij]); |
||
| 3444 | $currentYQuestion = html_entity_decode($currentYQuestion); |
||
| 3445 | // The Y axis is a scoring question type so we have more rows than the options (actually options * maximum score) |
||
| 3446 | if ($question_y['type'] == 'score') { |
||
| 3447 | for ($y = 1; $y <= $question_y['maximum_score']; $y++) { |
||
| 3448 | $tableHtml .= ' <tr>'; |
||
| 3449 | for ($ii = 0; $ii <= count($question_x['answers']); $ii++) { |
||
| 3450 | if ($question_x['type'] == 'score') { |
||
| 3451 | View Code Duplication | for ($x = 1; $x <= $question_x['maximum_score']; $x++) { |
|
| 3452 | if ($ii == 0) { |
||
| 3453 | $tableHtml .= ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>'; |
||
| 3454 | break; |
||
| 3455 | } else { |
||
| 3456 | $tableHtml .= ' <td align="center">'; |
||
| 3457 | $votes = SurveyUtil::comparative_check( |
||
| 3458 | $answers_x, |
||
| 3459 | $answers_y, |
||
| 3460 | $question_x['answersid'][($ii - 1)], |
||
| 3461 | $question_y['answersid'][($ij)], |
||
| 3462 | $x, |
||
| 3463 | $y |
||
| 3464 | ); |
||
| 3465 | $tableHtml .= $votes; |
||
| 3466 | array_push( |
||
| 3467 | $chartData, |
||
| 3468 | array( |
||
| 3469 | 'serie' => array($currentYQuestion, $xOptions[$ii-1]), |
||
| 3470 | 'option' => $x, |
||
| 3471 | 'votes' => $votes |
||
| 3472 | ) |
||
| 3473 | ); |
||
| 3474 | $tableHtml .= '</td>'; |
||
| 3475 | } |
||
| 3476 | } |
||
| 3477 | } else { |
||
| 3478 | if ($ii == 0) { |
||
| 3479 | $tableHtml .= '<th>'.$question_y['answers'][$ij].' '.$y.'</th>'; |
||
| 3480 | } else { |
||
| 3481 | $tableHtml .= '<td align="center">'; |
||
| 3482 | $votes = SurveyUtil::comparative_check( |
||
| 3483 | $answers_x, |
||
| 3484 | $answers_y, |
||
| 3485 | $question_x['answersid'][($ii - 1)], |
||
| 3486 | $question_y['answersid'][($ij)], |
||
| 3487 | 0, |
||
| 3488 | $y |
||
| 3489 | ); |
||
| 3490 | $tableHtml .= $votes; |
||
| 3491 | array_push( |
||
| 3492 | $chartData, |
||
| 3493 | array( |
||
| 3494 | 'serie' => array($currentYQuestion, $xOptions[$ii-1]), |
||
| 3495 | 'option' => $y, |
||
| 3496 | 'votes' => $votes |
||
| 3497 | ) |
||
| 3498 | ); |
||
| 3499 | $tableHtml .= '</td>'; |
||
| 3500 | } |
||
| 3501 | } |
||
| 3502 | } |
||
| 3503 | $tableHtml .= ' </tr>'; |
||
| 3504 | } |
||
| 3505 | } |
||
| 3506 | // The Y axis is NOT a score question type so the number of rows = the number of options |
||
| 3507 | else { |
||
| 3508 | $tableHtml .= ' <tr>'; |
||
| 3509 | for ($ii = 0; $ii <= count($question_x['answers']); $ii++) { |
||
| 3510 | if ($question_x['type'] == 'score') { |
||
| 3511 | View Code Duplication | for ($x = 1; $x <= $question_x['maximum_score']; $x++) { |
|
| 3512 | if ($ii == 0) { |
||
| 3513 | $tableHtml .= ' <th>'.$question_y['answers'][$ij].'</th>'; |
||
| 3514 | break; |
||
| 3515 | } else { |
||
| 3516 | $tableHtml .= ' <td align="center">'; |
||
| 3517 | $votes = SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, 0); |
||
| 3518 | $tableHtml .= $votes; |
||
| 3519 | array_push( |
||
| 3520 | $chartData, |
||
| 3521 | array( |
||
| 3522 | 'serie' => array($currentYQuestion, $xOptions[$ii-1]), |
||
| 3523 | 'option' => $x, |
||
| 3524 | 'votes' => $votes |
||
| 3525 | ) |
||
| 3526 | ); |
||
| 3527 | $tableHtml .= '</td>'; |
||
| 3528 | } |
||
| 3529 | } |
||
| 3530 | } else { |
||
| 3531 | if ($ii == 0) { |
||
| 3532 | $tableHtml .= ' <th>'.$question_y['answers'][($ij)].'</th>'; |
||
| 3533 | } else { |
||
| 3534 | $tableHtml .= ' <td align="center">'; |
||
| 3535 | $votes = SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)]); |
||
| 3536 | $tableHtml .= $votes; |
||
| 3537 | array_push( |
||
| 3538 | $chartData, |
||
| 3539 | array( |
||
| 3540 | 'serie' => $xOptions[$ii-1], |
||
| 3541 | 'option' => $currentYQuestion, |
||
| 3542 | 'votes' => $votes |
||
| 3543 | ) |
||
| 3544 | ); |
||
| 3545 | $tableHtml .= '</td>'; |
||
| 3546 | } |
||
| 3547 | } |
||
| 3548 | } |
||
| 3549 | $tableHtml .= ' </tr>'; |
||
| 3550 | } |
||
| 3551 | } |
||
| 3552 | $tableHtml .= '</table>'; |
||
| 3553 | echo '<div id="chartContainer" class="col-md-12">'; |
||
| 3554 | echo self::drawChart($chartData, true); |
||
| 3555 | echo '</div>'; |
||
| 3556 | echo $tableHtml; |
||
| 3557 | } |
||
| 3558 | } |
||
| 3559 | |||
| 3560 | /** |
||
| 3561 | * Get all the answers of a question grouped by user |
||
| 3562 | * |
||
| 3563 | * @param integer Survey ID |
||
| 3564 | * @param integer Question ID |
||
| 3565 | * @return Array Array containing all answers of all users, grouped by user |
||
| 3566 | * |
||
| 3567 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 3568 | * @version February 2007 - Updated March 2008 |
||
| 3569 | */ |
||
| 3570 | public static function get_answers_of_question_by_user($survey_id, $question_id) |
||
| 3571 | { |
||
| 3572 | $course_id = api_get_course_int_id(); |
||
| 3573 | $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); |
||
| 3574 | |||
| 3575 | $sql = "SELECT * FROM $table_survey_answer |
||
| 3576 | WHERE c_id = $course_id AND survey_id='".intval($survey_id)."' |
||
| 3577 | AND question_id='".intval($question_id)."' |
||
| 3578 | ORDER BY USER ASC"; |
||
| 3579 | $result = Database::query($sql); |
||
| 3580 | while ($row = Database::fetch_array($result)) { |
||
| 3581 | if ($row['value'] == 0) { |
||
| 3582 | $return[$row['user']][] = $row['option_id']; |
||
| 3583 | } else { |
||
| 3584 | $return[$row['user']][] = $row['option_id'].'*'.$row['value']; |
||
| 3585 | } |
||
| 3586 | } |
||
| 3587 | return $return; |
||
| 3588 | } |
||
| 3589 | |||
| 3590 | /** |
||
| 3591 | * Count the number of users who answer positively on both options |
||
| 3592 | * |
||
| 3593 | * @param array All answers of the x axis |
||
| 3594 | * @param array All answers of the y axis |
||
| 3595 | * @param integer x axis value (= the option_id of the first question) |
||
| 3596 | * @param integer y axis value (= the option_id of the second question) |
||
| 3597 | * @return integer Number of users who have answered positively to both options |
||
| 3598 | * |
||
| 3599 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 3600 | * @version February 2007 |
||
| 3601 | */ |
||
| 3602 | public static function comparative_check($answers_x, $answers_y, $option_x, $option_y, $value_x = 0, $value_y = 0) |
||
| 3630 | |||
| 3631 | /** |
||
| 3632 | * Get all the information about the invitations of a certain survey |
||
| 3633 | * |
||
| 3634 | * @return array Lines of invitation [user, code, date, empty element] |
||
| 3635 | * |
||
| 3636 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 3637 | * @version January 2007 |
||
| 3638 | * |
||
| 3639 | * @todo use survey_id parameter instead of $_GET |
||
| 3640 | */ |
||
| 3641 | public static function get_survey_invitations_data() |
||
| 3642 | { |
||
| 3643 | $course_id = api_get_course_int_id(); |
||
| 3644 | // Database table definition |
||
| 3645 | $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION); |
||
| 3646 | $table_user = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 3647 | |||
| 3648 | $sql = "SELECT |
||
| 3649 | survey_invitation.user as col1, |
||
| 3650 | survey_invitation.invitation_code as col2, |
||
| 3651 | survey_invitation.invitation_date as col3, |
||
| 3652 | '' as col4 |
||
| 3653 | FROM $table_survey_invitation survey_invitation |
||
| 3654 | LEFT JOIN $table_user user |
||
| 3655 | ON survey_invitation.user = user.user_id |
||
| 3656 | WHERE |
||
| 3657 | survey_invitation.c_id = $course_id AND |
||
| 3658 | survey_invitation.survey_id = '".intval($_GET['survey_id'])."' AND |
||
| 3659 | session_id='".api_get_session_id()."' "; |
||
| 3660 | $res = Database::query($sql); |
||
| 3661 | while ($row = Database::fetch_array($res)) { |
||
| 3662 | $survey_invitation_data[] = $row; |
||
| 3663 | } |
||
| 3664 | |||
| 3665 | return $survey_invitation_data; |
||
| 3666 | } |
||
| 3667 | |||
| 3668 | /** |
||
| 3669 | * Get the total number of survey invitations for a given survey (through $_GET['survey_id']) |
||
| 3670 | * |
||
| 3671 | * @return integer Total number of survey invitations |
||
| 3672 | * |
||
| 3673 | * @todo use survey_id parameter instead of $_GET |
||
| 3674 | * |
||
| 3675 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 3676 | * @version January 2007 |
||
| 3677 | */ |
||
| 3678 | View Code Duplication | public static function get_number_of_survey_invitations() |
|
| 3679 | { |
||
| 3680 | $course_id = api_get_course_int_id(); |
||
| 3681 | |||
| 3682 | // Database table definition |
||
| 3683 | $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION); |
||
| 3684 | |||
| 3685 | $sql = "SELECT count(user) AS total |
||
| 3686 | FROM $table_survey_invitation |
||
| 3687 | WHERE |
||
| 3688 | c_id = $course_id AND |
||
| 3689 | survey_id='".intval($_GET['survey_id'])."' AND |
||
| 3690 | session_id='".api_get_session_id()."' "; |
||
| 3691 | $res = Database::query($sql); |
||
| 3692 | $row = Database::fetch_array($res,'ASSOC'); |
||
| 3693 | |||
| 3694 | return $row['total']; |
||
| 3695 | } |
||
| 3696 | |||
| 3697 | /** |
||
| 3698 | * Save the invitation mail |
||
| 3699 | * |
||
| 3700 | * @param string Text of the e-mail |
||
| 3701 | * @param integer Whether the mail contents are for invite mail (0, default) or reminder mail (1) |
||
| 3702 | * |
||
| 3703 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 3704 | * @version January 2007 |
||
| 3705 | */ |
||
| 3706 | static function save_invite_mail($mailtext, $mail_subject, $reminder = 0) |
||
| 3707 | { |
||
| 3708 | $course_id = api_get_course_int_id(); |
||
| 3709 | // Database table definition |
||
| 3710 | $table_survey = Database :: get_course_table(TABLE_SURVEY); |
||
| 3711 | |||
| 3712 | // Reminder or not |
||
| 3713 | if ($reminder == 0) { |
||
| 3714 | $mail_field = 'invite_mail'; |
||
| 3715 | } else { |
||
| 3716 | $mail_field = 'reminder_mail'; |
||
| 3717 | } |
||
| 3718 | |||
| 3719 | $sql = "UPDATE $table_survey |
||
| 3720 | SET |
||
| 3721 | mail_subject='".Database::escape_string($mail_subject)."', |
||
| 3722 | $mail_field = '".Database::escape_string($mailtext)."' |
||
| 3723 | WHERE c_id = $course_id AND survey_id = '".intval($_GET['survey_id'])."'"; |
||
| 3724 | Database::query($sql); |
||
| 3725 | } |
||
| 3726 | |||
| 3727 | /** |
||
| 3728 | * This function saves all the invitations of course users and additional users in the database |
||
| 3729 | * and sends the invitations by email |
||
| 3730 | * |
||
| 3731 | * @param array Users array can be both a list of course uids AND a list of additional emailaddresses |
||
| 3732 | * @param string Title of the invitation, used as the title of the mail |
||
| 3733 | * @param string Text of the invitation, used as the text of the mail. |
||
| 3734 | * The text has to contain a **link** string or this will automatically be added to the end |
||
| 3735 | * |
||
| 3736 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 3737 | * @author Julio Montoya - Adding auto-generated link support |
||
| 3738 | * @version January 2007 |
||
| 3739 | * |
||
| 3740 | */ |
||
| 3741 | public static function saveInvitations( |
||
| 3854 | |||
| 3855 | /** |
||
| 3856 | * @param $params |
||
| 3857 | * @return bool|int |
||
| 3858 | */ |
||
| 3859 | public static function save_invitation($params) |
||
| 3878 | |||
| 3879 | /** |
||
| 3880 | * @param int $courseId |
||
| 3881 | * @param int $sessionId |
||
| 3882 | * @param int $groupId |
||
| 3883 | * @param string $surveyCode |
||
| 3884 | * @return int |
||
| 3885 | */ |
||
| 3886 | public static function invitationExists($courseId, $sessionId, $groupId, $surveyCode) |
||
| 3905 | |||
| 3906 | /** |
||
| 3907 | * Send the invitation by mail. |
||
| 3908 | * |
||
| 3909 | * @param int invitedUser - the userId (course user) or emailaddress of additional user |
||
| 3910 | * $param string $invitation_code - the unique invitation code for the URL |
||
| 3911 | * @return void |
||
| 3912 | */ |
||
| 3913 | public static function send_invitation_mail($invitedUser, $invitation_code, $invitation_title, $invitation_text) |
||
| 3981 | |||
| 3982 | /** |
||
| 3983 | * This function recalculates the number of users who have been invited and updates the survey table with this value. |
||
| 3984 | * |
||
| 3985 | * @param string Survey code |
||
| 3986 | * @return void |
||
| 3987 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 3988 | * @version January 2007 |
||
| 3989 | */ |
||
| 3990 | static function update_count_invited($survey_code) |
||
| 4019 | |||
| 4020 | /** |
||
| 4021 | * This function gets all the invited users for a given survey code. |
||
| 4022 | * |
||
| 4023 | * @param string Survey code |
||
| 4024 | * @param string optional - course database |
||
| 4025 | * @return array Array containing the course users and additional users (non course users) |
||
| 4026 | * |
||
| 4027 | * @todo consider making $defaults['additional_users'] also an array |
||
| 4028 | * |
||
| 4029 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 4030 | * @author Julio Montoya, adding c_id fixes - Dec 2012 |
||
| 4031 | * @version January 2007 |
||
| 4032 | */ |
||
| 4033 | public static function get_invited_users($survey_code, $course_code = '', $session_id = 0) |
||
| 4096 | |||
| 4097 | /** |
||
| 4098 | * Get all the invitations |
||
| 4099 | * |
||
| 4100 | * @param string Survey code |
||
| 4101 | * @return array Database rows matching the survey code |
||
| 4102 | * |
||
| 4103 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 4104 | * @version September 2007 |
||
| 4105 | */ |
||
| 4106 | static function get_invitations($survey_code) |
||
| 4107 | { |
||
| 4108 | $course_id = api_get_course_int_id(); |
||
| 4109 | // Database table definition |
||
| 4110 | $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION); |
||
| 4111 | |||
| 4112 | $sql = "SELECT * FROM $table_survey_invitation |
||
| 4113 | WHERE |
||
| 4114 | c_id = $course_id AND |
||
| 4115 | survey_code = '".Database::escape_string($survey_code)."'"; |
||
| 4116 | $result = Database::query($sql); |
||
| 4123 | |||
| 4124 | /** |
||
| 4125 | * This function displays the form for searching a survey |
||
| 4126 | * |
||
| 4127 | * @return void (direct output) |
||
| 4128 | * |
||
| 4129 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 4130 | * @version January 2007 |
||
| 4131 | * |
||
| 4132 | * @todo use quickforms |
||
| 4133 | * @todo consider moving this to surveymanager.inc.lib.php |
||
| 4134 | */ |
||
| 4135 | static function display_survey_search_form() |
||
| 4177 | |||
| 4178 | /** |
||
| 4179 | * Show table only visible by DRH users |
||
| 4180 | */ |
||
| 4181 | public static function displaySurveyListForDrh() |
||
| 4212 | |||
| 4213 | /** |
||
| 4214 | * This function displays the sortable table with all the surveys |
||
| 4215 | * |
||
| 4216 | * @return void (direct output) |
||
| 4217 | * |
||
| 4218 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 4219 | * @version January 2007 |
||
| 4220 | */ |
||
| 4221 | static function display_survey_list() |
||
| 4260 | |||
| 4261 | function display_survey_list_for_coach() |
||
| 4299 | |||
| 4300 | /** |
||
| 4301 | * This function changes the modify column of the sortable table |
||
| 4302 | * |
||
| 4303 | * @param integer $survey_id the id of the survey |
||
| 4304 | * @param bool $drh |
||
| 4305 | * @return string html code that are the actions that can be performed on any survey |
||
| 4306 | * |
||
| 4307 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 4308 | * @version January 2007 |
||
| 4309 | */ |
||
| 4310 | static function modify_filter($survey_id, $drh = false) |
||
| 4355 | |||
| 4356 | static function modify_filter_for_coach($survey_id) |
||
| 4369 | |||
| 4370 | /** |
||
| 4371 | * Returns "yes" when given parameter is one, "no" for any other value |
||
| 4372 | * @param integer Whether anonymous or not |
||
| 4373 | * @return string "Yes" or "No" in the current language |
||
| 4374 | */ |
||
| 4375 | static function anonymous_filter($anonymous) |
||
| 4383 | |||
| 4384 | /** |
||
| 4385 | * This function handles the search restriction for the SQL statements |
||
| 4386 | * |
||
| 4387 | * @return string Part of a SQL statement or false on error |
||
| 4388 | * |
||
| 4389 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 4390 | * @version January 2007 |
||
| 4391 | */ |
||
| 4392 | static function survey_search_restriction() |
||
| 4411 | |||
| 4412 | /** |
||
| 4413 | * This function calculates the total number of surveys |
||
| 4414 | * |
||
| 4415 | * @return integer Total number of surveys |
||
| 4416 | * |
||
| 4417 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 4418 | * @version January 2007 |
||
| 4419 | */ |
||
| 4420 | static function get_number_of_surveys() |
||
| 4437 | |||
| 4438 | static function get_number_of_surveys_for_coach() |
||
| 4443 | |||
| 4444 | /** |
||
| 4445 | * This function gets all the survey data that is to be displayed in the sortable table |
||
| 4446 | * |
||
| 4447 | * @param int $from |
||
| 4448 | * @param int $number_of_items |
||
| 4449 | * @param int $column |
||
| 4450 | * @param string $direction |
||
| 4451 | * @param bool $isDrh |
||
| 4452 | * @return unknown |
||
| 4453 | * |
||
| 4454 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 4455 | * @author Julio Montoya <[email protected]>, Beeznest - Adding intvals |
||
| 4456 | * @version January 2007 |
||
| 4457 | */ |
||
| 4458 | static function get_survey_data($from, $number_of_items, $column, $direction, $isDrh = false) |
||
| 4558 | |||
| 4559 | static function get_survey_data_for_coach($from, $number_of_items, $column, $direction) |
||
| 4621 | |||
| 4622 | /** |
||
| 4623 | * Display all the active surveys for the given course user |
||
| 4624 | * |
||
| 4625 | * @param int $user_id |
||
| 4626 | * |
||
| 4627 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 4628 | * @version April 2007 |
||
| 4629 | */ |
||
| 4630 | public static function getSurveyList($user_id) |
||
| 4744 | |||
| 4745 | /** |
||
| 4746 | * Creates a multi array with the user fields that we can show. We look the visibility with the api_get_setting function |
||
| 4747 | * The username is always NOT able to change it. |
||
| 4748 | * @author Julio Montoya Armas <[email protected]>, Chamilo: Personality Test modification |
||
| 4749 | * @return array[value_name][name] |
||
| 4750 | * array[value_name][visibilty] |
||
| 4751 | */ |
||
| 4752 | static function make_field_list() |
||
| 4891 | |||
| 4892 | /** |
||
| 4893 | * @author Isaac Flores Paz <[email protected]> |
||
| 4894 | * @param int $user_id - User ID |
||
| 4895 | * @param int $user_id_answer - User in survey answer table (user id or anonymus) |
||
| 4896 | * @return boolean |
||
| 4897 | */ |
||
| 4898 | static function show_link_available($user_id, $survey_code, $user_answer) |
||
| 4936 | |||
| 4937 | /** |
||
| 4938 | * Display survey question chart |
||
| 4939 | * @param array Chart data |
||
| 4940 | * @param boolean Tells if the chart has a serie. False by default |
||
| 4941 | * @return void (direct output) |
||
| 4942 | */ |
||
| 4943 | public static function drawChart($chartData, $hasSerie = false, $chartContainerId = 'chartContainer') |
||
| 4998 | |||
| 4999 | /** |
||
| 5000 | * Set a flag to the current survey as answered by the current user |
||
| 5001 | * @param string $surveyCode The survey code |
||
| 5002 | * @param int $courseId The course ID |
||
| 5003 | */ |
||
| 5004 | public static function flagSurveyAsAnswered($surveyCode, $courseId) |
||
| 5015 | |||
| 5016 | /** |
||
| 5017 | * Check whether a survey was answered by the current user |
||
| 5018 | * @param string $surveyCode The survey code |
||
| 5019 | * @param int $courseId The course ID |
||
| 5020 | * @return boolean |
||
| 5021 | */ |
||
| 5022 | public static function isSurveyAnsweredFlagged($surveyCode, $courseId) |
||
| 5045 | } |
||
| 5046 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: