diff --git a/main/survey/survey.lib.php b/main/survey/survey.lib.php index f1765ef223..bb4a906325 100755 --- a/main/survey/survey.lib.php +++ b/main/survey/survey.lib.php @@ -2411,9 +2411,9 @@ class SurveyManager $content = ''; if (empty($answered)) { - $content .= Display::page_subheader(get_lang('Answered')); - } else { $content .= Display::page_subheader(get_lang('Unanswered')); + } else { + $content .= Display::page_subheader(get_lang('Answered')); } if (!empty($invitations)) { @@ -2448,8 +2448,22 @@ class SurveyManager $sessionInfo = api_get_session_info($sessionId); $courseInfo['name'] .= ' ('.$sessionInfo['name'].')'; } + + $surveyData = SurveyManager::get_survey($survey->getSurveyId(), 0, $courseInfo['code']); $table->setCellContents($row, 0, $title); $table->setCellContents($row, 1, $courseInfo['name']); + + if (!empty($answered) && $surveyData['anonymous'] == 0) { + $answers = SurveyUtil::displayCompleteReport( + $surveyData, + $userId, + false, + false + ); + $table->setCellContents(++$row, 0, $answers); + $table->setCellContents(++$row, 1, ''); + } + $row++; } $content .= $table->toHtml(); diff --git a/main/survey/surveyUtil.class.php b/main/survey/surveyUtil.class.php index defb7bd13c..dc75953644 100755 --- a/main/survey/surveyUtil.class.php +++ b/main/survey/surveyUtil.class.php @@ -250,13 +250,13 @@ class SurveyUtil self::display_question_report($survey_data); break; case 'userreport': - self::display_user_report($people_filled, $survey_data); + self::displayUserReport($survey_data, $people_filled); break; case 'comparativereport': self::display_comparative_report(); break; case 'completereport': - self::display_complete_report($survey_data); + echo self::displayCompleteReport($survey_data); break; case 'deleteuserreport': self::delete_user_report($_GET['survey_id'], $_GET['user']); @@ -313,58 +313,18 @@ class SurveyUtil } /** - * This function displays the user report which is basically nothing more - * than a one-page display of all the questions - * of the survey that is filled with the answers of the person who filled the survey. - * - * @return string html code of the one-page survey with the answers of the selected user - * - * @author Patrick Cool , Ghent University + * @param array $survey_data + * @param array $people_filled * - * @version February 2007 - Updated March 2008 + * @return string */ - public static function display_user_report($people_filled, $survey_data) + public static function displayUserReportForm($survey_data, $people_filled) { - // Database table definitions - $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); - $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); - $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); - $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : 0; + $surveyId = $survey_data['survey_id']; - // Actions bar - echo '
'; - echo ''. - Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('ReportingOverview'), '', ICON_SIZE_MEDIUM) - .''; - if (isset($_GET['user'])) { - if (api_is_allowed_to_edit()) { - // The delete link - echo ''. - Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_MEDIUM).''; - } - - // Export the user report - echo '' - .Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), '', ICON_SIZE_MEDIUM).' '; - echo '' - .Display::return_icon('export_excel.png', get_lang('ExportAsXLS'), '', ICON_SIZE_MEDIUM).' '; - echo '
'; - echo ''; - echo ''; - echo '
'; - echo '
'; - echo ''; - echo ''; - echo '
'; - echo '
'; + if (empty($people_filled) || empty($survey_data)) { + return ''; } - echo '
'; // Step 1: selection of the user echo ""; echo get_lang('SelectUserWhoFilledSurvey').'
'; - echo ''; + } - $course_id = api_get_course_int_id(); + /** + * @param int $userId + * @param array $survey_data + * @param bool $addMessage + */ + public static function displayUserReportAnswers($userId, $survey_data, $addMessage = true) + { + // Database table definitions + $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); + $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); + $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); + $course_id = (int) $survey_data['c_id']; + $surveyId = (int) $survey_data['survey_id']; + $userId = Database::escape_string($userId); + + $content = ''; // Step 2: displaying the survey and the answer of the selected users - if (isset($_GET['user'])) { - echo Display::return_message( - get_lang('AllQuestionsOnOnePage'), - 'normal', - false - ); + if (!empty($userId)) { + if ($addMessage) { + $content .= Display::return_message( + get_lang('AllQuestionsOnOnePage'), + 'normal', + false + ); + } // Getting all the questions and options $sql = "SELECT @@ -451,7 +428,7 @@ class SurveyUtil WHERE c_id = $course_id AND survey_id = '".$surveyId."' AND - user = '".Database::escape_string($_GET['user'])."'"; + user = '".$userId."'"; $result = Database::query($sql); while ($row = Database::fetch_array($result, 'ASSOC')) { $answers[$row['question_id']][] = $row['option_id']; @@ -459,7 +436,6 @@ class SurveyUtil } // Displaying all the questions - foreach ($questions as &$question) { // If the question type is a scoring then we have to format the answers differently switch ($question['type']) { @@ -495,10 +471,79 @@ class SurveyUtil $form->addHtml($question['survey_question']); $display->render($form, $question, $finalAnswer); $form->addHtml(''); - $form->display(); + $content .= $form->returnForm(); } } } + + return $content; + } + + /** + * This function displays the user report which is basically nothing more + * than a one-page display of all the questions + * of the survey that is filled with the answers of the person who filled the survey. + * + * @return string html code of the one-page survey with the answers of the selected user + * + * @author Patrick Cool , Ghent University + * + * @version February 2007 - Updated March 2008 + */ + public static function displayUserReport($survey_data, $people_filled, $addActionBar = true) + { + if (empty($survey_data)) { + return ''; + } + + $surveyId = $survey_data['survey_id']; + $reportingUrl = api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.$surveyId.'&'.api_get_cidreq(); + + // Actions bar + if ($addActionBar) { + echo '
'; + echo ''. + Display::return_icon( + 'back.png', + get_lang('BackTo').' '.get_lang('ReportingOverview'), + '', + ICON_SIZE_MEDIUM + ) + .''; + if (isset($_GET['user'])) { + if (api_is_allowed_to_edit()) { + // The delete link + echo ''. + Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_MEDIUM).''; + } + + // Export the user report + echo '' + .Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), '', ICON_SIZE_MEDIUM).' '; + echo '' + .Display::return_icon('export_excel.png', get_lang('ExportAsXLS'), '', ICON_SIZE_MEDIUM).' '; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
'; + echo ''; + echo ''; + echo '
'; + echo '
'; + } + echo '
'; + } + + self::displayUserReportForm($survey_data, $people_filled); + if (isset($_GET['user'])) { + echo self::displayUserReportAnswers($_GET['user'], $survey_data); + } } /** @@ -864,69 +909,85 @@ class SurveyUtil /** * This functions displays the complete reporting. * - * @return string HTML code - * - * @todo open questions are not in the complete report yet. - * - * @author Patrick Cool , Ghent University + * @param array $survey_data + * @param int $userId + * @param bool $addActionBar + * @param bool $addFilters * - * @version February 2007 + * @return string */ - public static function display_complete_report($survey_data) + public static function displayCompleteReport($survey_data, $userId = 0, $addActionBar = true, $addFilters = true) { // Database table definitions $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); - $course_id = api_get_course_int_id(); - $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : 0; - $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : ''; - // Actions bar - echo ''; + $surveyId = (int) $survey_data['survey_id']; + $course_id = (int) $survey_data['c_id']; - // The form - echo ''; - echo ''; - echo ''; - echo ''; - echo '
'; - echo ''; - echo ''; - echo '
'; + if (empty($surveyId) || empty($course_id)) { + return ''; + } - echo '
'; + $content .= '' + .Display::return_icon( + 'back.png', + get_lang('BackTo').' '.get_lang('ReportingOverview'), + [], + ICON_SIZE_MEDIUM + ) + .''; + $content .= '' + .Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), '', ICON_SIZE_MEDIUM).''; + $content .= '' + .Display::return_icon('export_excel.png', get_lang('ExportAsXLS'), '', ICON_SIZE_MEDIUM).''; + $content .= ''; + + // The form + $content .= ''; + $content .= ''; + $content .= ''; + $content .= '
'; + $content .= '
'; + $content .= ''; + $content .= ''; + $content .= '
'; + } + + $content .= '
'; // The table - echo '
'; + $content .= '
'; // Getting the number of options per question - echo ' '; - echo ' '; + $content .= ' '; } - echo ''; - echo ''; $display_extra_user_fields = false; if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter'] || @@ -944,11 +1005,14 @@ class SurveyUtil ); $num = count($extra_user_fields); if ($num > 0) { - echo ''; + $content .= ''; $display_extra_user_fields = true; } } @@ -974,36 +1038,41 @@ class SurveyUtil // 1. there is no question filter and the export button has not been clicked // 2. there is a quesiton filter but the question is selected for display if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter']) || - (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])) + (is_array($_POST['questions_filter']) && + in_array($row['question_id'], $_POST['questions_filter'])) ) { // We do not show comment and pagebreak question types if ($row['type'] != 'pagebreak') { - echo ' 0 && $row['type'] != 'percentage') { - echo ' colspan="'.$row['number_of_options'].'"'; + $content .= ' colspan="'.$row['number_of_options'].'"'; } - echo '>'; - echo ''; - echo ''; + $content .= '>'; + $content .= ''; + $content .= ''; } // No column at all if it's not a question } $questions[$row['question_id']] = $row; } - echo ' '; - // Getting all the questions and options - echo ' '; - echo ' '; // the user column + $content .= ' '; + // Getting all the questions and options + $content .= ' '; + $content .= ' '; // the user column if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter'] || isset($_POST['export_report']) && $_POST['export_report']) || !empty($_POST['fields_filter']) ) { //show the fields names for user fields foreach ($extra_user_fields as &$field) { - echo ''; + $content .= ''; } } @@ -1040,26 +1109,32 @@ class SurveyUtil ) { // we do not show comment and pagebreak question types if ($row['type'] == 'open' || $row['type'] == 'comment') { - echo ''; + $content .= ''; $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; $display_percentage_header = 1; } elseif ($row['type'] == 'percentage' && $display_percentage_header) { - echo ''; + $content .= ''; $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; $display_percentage_header = 0; } elseif ($row['type'] == 'percentage') { $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; } elseif ($row['type'] != 'pagebreak' && $row['type'] != 'percentage') { - echo ''; + $content .= ''; $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; $display_percentage_header = 1; } } } - echo ' '; + $content .= ' '; + + $userCondition = ''; + if (!empty($userId)) { + $userId = (int) $userId; + $userCondition = " AND user = $userId "; + } // Getting all the answers of the users $old_user = ''; @@ -1067,7 +1142,8 @@ class SurveyUtil $sql = "SELECT * FROM $table_survey_answer WHERE c_id = $course_id AND - survey_id='".$surveyId."' + survey_id = $surveyId + $userCondition ORDER BY answer_id, user ASC"; $result = Database::query($sql); $i = 1; @@ -1078,7 +1154,7 @@ class SurveyUtil $userParam = $i; $i++; } - self::display_complete_report_row( + $content .= self::display_complete_report_row( $survey_data, $possible_answers, $answers_of_user, @@ -1098,12 +1174,14 @@ class SurveyUtil } $old_user = $row['user']; } + $userParam = $old_user; if ($survey_data['anonymous'] != 0) { $userParam = $i; $i++; } - self::display_complete_report_row( + + $content .= self::display_complete_report_row( $survey_data, $possible_answers, $answers_of_user, @@ -1111,23 +1189,25 @@ class SurveyUtil $questions, $display_extra_user_fields ); + // This is to display the last user - echo '
'; - if ((isset($_POST['submit_question_filter']) && $_POST['submit_question_filter']) || - (isset($_POST['export_report']) && $_POST['export_report']) - ) { - echo ''; + $content .= '
'; + + if ($addFilters) { + if ((isset($_POST['submit_question_filter']) && $_POST['submit_question_filter']) || + (isset($_POST['export_report']) && $_POST['export_report']) + ) { + $content .= ''; + } + $content .= ''; + $content .= ' 0 ? ' colspan="'.$num.'"' : '').'>'; - echo ''; - echo ' 0 ? ' colspan="'.$num.'"' : '').'>'; + $content .= ''; + $content .= '
 
 '.$field[3].''.$field[3].' -  -  %  % '; - echo $row['option_text']; - echo ''; + $content .= $row['option_text']; + $content .= '
'; - echo '
'; + $content .= ''; + $content .= ''; + + return $content; } /** - * This function displays a row (= a user and his/her answers) in the table of the complete report. + * Return user answers in a row * - * @param array $survey_data - * @param array Possible options - * @param array User answers - * @param mixed User ID or user details string - * @param bool Whether to show extra user fields or not + * @param $survey_data + * @param $possible_options + * @param $answers_of_user + * @param $user + * @param $questions + * @param bool $display_extra_user_fields * - * @author Patrick Cool , Ghent University - * - * @version February 2007 - Updated March 2008 + * @return string */ public static function display_complete_report_row( $survey_data, @@ -1138,24 +1218,31 @@ class SurveyUtil $display_extra_user_fields = false ) { $user = Security::remove_XSS($user); - echo ''; + $surveyId = (int) $survey_data['survey_id']; + + if (empty($surveyId)) { + return ''; + } + + $content = ''; + $url = api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.$surveyId.'&'.api_get_cidreq(); if ($survey_data['anonymous'] == 0) { if (intval($user) !== 0) { $userInfo = api_get_user_info($user); + $user_displayed = '-'; if (!empty($userInfo)) { $user_displayed = $userInfo['complete_name_with_username']; - } else { - $user_displayed = '-'; } - echo ' - ' + + $content .= ' + ' .$user_displayed.' '; // the user column } else { - echo ''.$user.''; // the user column + $content .= ''.$user.''; // the user column } } else { - echo ''.get_lang('Anonymous').' '.$user.''; + $content .= ''.get_lang('Anonymous').' '.$user.''; } if ($display_extra_user_fields) { @@ -1168,33 +1255,33 @@ class SurveyUtil true ); foreach ($user_fields_values as &$value) { - echo ''.$value.''; + $content .= ''.$value.''; } } if (is_array($possible_options)) { foreach ($possible_options as $question_id => &$possible_option) { if ($questions[$question_id]['type'] == 'open' || $questions[$question_id]['type'] == 'comment') { - echo ''; + $content .= ''; if (isset($answers_of_user[$question_id]) && isset($answers_of_user[$question_id]['0'])) { - echo $answers_of_user[$question_id]['0']['option_id']; + $content .= $answers_of_user[$question_id]['0']['option_id']; } - echo ''; + $content .= ''; } else { foreach ($possible_option as $option_id => &$value) { if ($questions[$question_id]['type'] == 'percentage') { if (!empty($answers_of_user[$question_id][$option_id])) { - echo ""; - echo $answers_of_user[$question_id][$option_id]['value']; - echo ""; + $content .= ""; + $content .= $answers_of_user[$question_id][$option_id]['value']; + $content .= ""; } } else { - echo ''; + $content .= ''; if (!empty($answers_of_user[$question_id][$option_id])) { if ($answers_of_user[$question_id][$option_id]['value'] != 0) { - echo $answers_of_user[$question_id][$option_id]['value']; + $content .= $answers_of_user[$question_id][$option_id]['value']; } else { - echo 'v'; + $content .= 'v'; } } } @@ -1202,7 +1289,10 @@ class SurveyUtil } } } - echo ''; + + $content .= ''; + + return $content; } /**