, Ghent University: * cleanup, refactoring and rewriting large parts of the code * @author Julio Montoya */ require_once __DIR__.'/../inc/global.inc.php'; $this_section = SECTION_COURSES; $current_course_tool = TOOL_SURVEY; api_protect_course_script(true); /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/ // Coach can't view this page $extend_rights_for_coachs = api_get_setting('extend_rights_for_coach_on_survey'); $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(api_get_user_id(), api_get_course_info()); if ($isDrhOfCourse) { header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq()); exit; } if (!api_is_allowed_to_edit(false, true) || (api_is_session_general_coach() && 'false' == $extend_rights_for_coachs) ) { api_not_allowed(true); exit; } // Database table definitions $table_survey = Database::get_course_table(TABLE_SURVEY); $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP); $table_course = Database::get_main_table(TABLE_MAIN_COURSE); $table_user = Database::get_main_table(TABLE_MAIN_USER); $survey_id = (int) $_GET['survey_id']; $course_id = api_get_course_int_id(); $action = isset($_GET['action']) ? $_GET['action'] : null; // Breadcrumbs $interbreadcrumb[] = [ 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php', 'name' => get_lang('Survey list'), ]; Session::erase('answer_count'); Session::erase('answer_list'); // Getting the survey information if (!empty($_GET['survey_id'])) { $course_code = api_get_course_id(); if (-1 != $course_code) { $survey_data = SurveyManager::get_survey($survey_id); } else { api_not_allowed(true); } } else { api_not_allowed(true); } $tool_name = strip_tags($survey_data['title'], ''); $is_survey_type_1 = 1 == $survey_data['survey_type']; if (api_strlen(strip_tags($survey_data['title'])) > 40) { $tool_name .= '...'; } if ($is_survey_type_1 && ('addgroup' == $action || 'deletegroup' == $action)) { $_POST['name'] = trim($_POST['name']); if ('addgroup' == $action) { if (!empty($_POST['group_id'])) { Database::query('UPDATE '.$table_survey_question_group.' SET description = \''.Database::escape_string($_POST['description']).'\' WHERE c_id = '.$course_id.' AND id = \''.Database::escape_string($_POST['group_id']).'\''); $sendmsg = 'GroupUpdatedSuccessfully'; } elseif (!empty($_POST['name'])) { Database::query('INSERT INTO '.$table_survey_question_group.' (c_id, name,description,survey_id) values ('.$course_id.', \''.Database::escape_string($_POST['name']).'\',\''.Database::escape_string($_POST['description']).'\',\''.$survey_id.'\') '); $sendmsg = 'GroupCreatedSuccessfully'; } else { $sendmsg = 'GroupNeedName'; } } if ('deletegroup' == $action) { $sql = 'DELETE FROM '.$table_survey_question_group.' WHERE c_id = '.$course_id.' AND id = '.intval($_GET['gid']).' AND survey_id = '.$survey_id; Database::query($sql); $sendmsg = 'GroupDeletedSuccessfully'; } header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&sendmsg='.$sendmsg); exit; } $my_question_id_survey = isset($_GET['question_id']) ? (int) $_GET['question_id'] : null; $my_survey_id_survey = (int) $_GET['survey_id']; $message_information = isset($_GET['message']) ? Security::remove_XSS($_GET['message']) : null; // Displaying the header if (!empty($action)) { switch ($action) { case 'copyquestion': $copied = SurveyManager::copyQuestion($_GET['question_id']); if (false !== $copied) { $sendmsg = 'QuestionAdded'; } else { $sendmsg = 'ErrorOccurred'; } break; case 'delete': $result = SurveyManager::delete_survey_question( $my_survey_id_survey, $my_question_id_survey, $survey_data['is_shared'] ); if (false == $result) { $sendmsg = 'ErrorOccured'; } else { $sendmsg = 'Deleted'; } break; case 'moveup': case 'movedown': SurveyManager::move_survey_question( $action, $my_question_id_survey, $my_survey_id_survey ); $sendmsg = 'SurveyQuestionMoved'; break; } header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&sendmsg='.$sendmsg); exit; } $htmlHeadXtra[] = ''; // Displaying the header Display::display_header($tool_name, 'Survey'); // Show error/success messages, if any if (!$is_survey_type_1 && !empty($_GET['sendmsg'])) { $messageType = 'confirmation'; if (in_array($_GET['sendmsg'], ['ErrorOccurred'])) { $messageType = 'error'; } echo Display::return_message( get_lang($_GET['sendmsg']), 'confirmation', false ); } if (!empty($survey_data['survey_version'])) { echo ''.get_lang('Version').': '.$survey_data['survey_version'].''; } // We exit here is the first or last question is a pagebreak (which causes errors) SurveyUtil::check_first_last_question($_GET['survey_id']); // Action links $survey_actions = ''; if (3 != $survey_data['survey_type']) { $survey_actions = ''. Display::return_icon('edit.png', get_lang('Edit survey'), '', ICON_SIZE_MEDIUM).''; } $survey_actions .= ''. Display::return_icon('delete.png', get_lang('Delete survey'), '', ICON_SIZE_MEDIUM).''; if (3 != $survey_data['survey_type']) { $survey_actions .= ''. Display::return_icon('preview_view.png', get_lang('Preview'), '', ICON_SIZE_MEDIUM).''; } $survey_actions .= ''. Display::return_icon('mail_send.png', get_lang('Publish'), '', ICON_SIZE_MEDIUM).''; if (3 != $survey_data['survey_type']) { if (!api_get_configuration_value('hide_survey_reporting_button')) { $survey_actions .= Display::url( Display::return_icon('statistics.png', get_lang('Reporting'), [], ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH).'survey/reporting.php?'.api_get_cidreq().'&survey_id='.$survey_id ); } } $survey_actions .= SurveyUtil::getAdditionalTeacherActions($survey_id, ICON_SIZE_MEDIUM); echo '
'.$survey_actions.'
'; $urlQuestion = api_get_path(WEB_CODE_PATH).'survey/question.php?'.api_get_cidreq().'&action=add'; if (0 == $survey_data['survey_type']) { echo '
'; echo Display::url( Display::return_icon('yesno.png', get_lang('Yes / No'), null, ICON_SIZE_BIG), $urlQuestion.'&type=yesno&survey_id='.$survey_id ); echo Display::url( Display::return_icon('mcua.png', get_lang('Multiple choice'), null, ICON_SIZE_BIG), $urlQuestion.'&type=multiplechoice&survey_id='.$survey_id ); echo Display::url( Display::return_icon('mcma.png', get_lang('Multiple answers'), null, ICON_SIZE_BIG), $urlQuestion.'&type=multipleresponse&survey_id='.$survey_id ); echo Display::url( Display::return_icon('open_answer.png', get_lang('Open'), null, ICON_SIZE_BIG), $urlQuestion.'&type=open&survey_id='.$survey_id ); echo Display::url( Display::return_icon('dropdown.png', get_lang('Dropdown'), null, ICON_SIZE_BIG), $urlQuestion.'&type=dropdown&survey_id='.$survey_id ); echo Display::url( Display::return_icon('percentagequestion.png', get_lang('Percentage'), null, ICON_SIZE_BIG), $urlQuestion.'&type=percentage&survey_id='.$survey_id ); echo Display::url( Display::return_icon('scorequestion.png', get_lang('Score'), null, ICON_SIZE_BIG), $urlQuestion.'&type=score&survey_id='.$survey_id ); echo Display::url( Display::return_icon('commentquestion.png', get_lang('Comment'), null, ICON_SIZE_BIG), $urlQuestion.'&type=comment&survey_id='.$survey_id ); echo Display::url( Display::return_icon('mcua.png', get_lang('SurveyMultipleAnswerWithOther'), null, ICON_SIZE_BIG), $urlQuestion.'&type=multiplechoiceother&survey_id='.$survey_id ); if (0 == $survey_data['one_question_per_page']) { echo Display::url( Display::return_icon('yesno.png', get_lang('SurveyQuestionSelectiveDisplay'), null, ICON_SIZE_BIG), $urlQuestion.'&type=selectivedisplay&survey_id='.$survey_id ); echo Display::url( Display::return_icon('page_end.png', get_lang('Pagebreak'), null, ICON_SIZE_BIG), $urlQuestion.'&type=pagebreak&survey_id='.$survey_id ); } echo '
'; } else { if (3 != $survey_data['survey_type']) { echo '
'; echo Display::url( Display::return_icon('yesno.png', get_lang('Yes / No'), null, ICON_SIZE_BIG), $urlQuestion.'&type=personality&survey_id='.$survey_id ); echo '
'; } } // Displaying the table header with all the questions echo ''; echo ''; echo ''; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; if ($is_survey_type_1) { echo ''; echo ''; } echo ' '; echo ''; // Displaying the table contents with all the questions $question_counter = 1; $sql = "SELECT * FROM $table_survey_question_group WHERE c_id = $course_id AND survey_id = $survey_id ORDER BY iid"; $result = Database::query($sql); $groups = []; while ($row = Database::fetch_array($result)) { $groups[$row['id']] = $row['name']; } $sql = "SELECT survey_question.*, count(survey_question_option.iid) as number_of_options FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option ON survey_question.iid = survey_question_option.question_id AND survey_question_option.c_id = $course_id WHERE survey_question.survey_id = $survey_id AND survey_question.c_id = $course_id GROUP BY survey_question.iid ORDER BY survey_question.sort ASC"; $result = Database::query($sql); $question_counter_max = Database::num_rows($result); $questionsGroupClass = ''; while ($row = Database::fetch_array($result, 'ASSOC')) { $questionId = $row['iid']; $breakClass = ''; // Visually impact questions between page breaks by changing the bg color if ('pagebreak' === $row['type']) { $breakClass = ' highlight'; if (empty($questionsGroupClass)) { $questionsGroupClass = 'row_even'; } else { $questionsGroupClass = ''; } } echo ''; echo ' '; echo ' '; echo ''; echo ''; echo ''; $question_counter++; if ($is_survey_type_1) { echo ''; echo ''; } echo ''; } echo '
'.get_lang('N°').''.get_lang('Title').''.get_lang('Type').''.get_lang('Options').''.get_lang('Edit').''.get_lang('Condition').''.get_lang('Group').'
'.$question_counter.''; if (3 != $survey_data['survey_type']) { if (api_strlen($row['survey_question']) > 100) { echo api_substr(strip_tags($row['survey_question']), 0, 100).' ... '; } else { echo $row['survey_question']; } } else { $parts = explode('@@', $row['survey_question']); echo api_get_local_time($parts[0]).' - '.api_get_local_time($parts[1]); } if ('yesno' === $row['type']) { $tool_name = get_lang('YesNo'); } elseif ('multiplechoice' === $row['type']) { $tool_name = get_lang('UniqueSelect'); } elseif ('multipleresponse' === $row['type']) { $tool_name = get_lang('MultipleChoiceMultipleAnswers'); } elseif ('selectivedisplay' === $row['type']) { $tool_name = get_lang('SurveyQuestionSelectiveDisplay'); } else { $tool_name = get_lang(api_ucfirst(Security::remove_XSS($row['type']))); } echo ''.$tool_name.''.$row['number_of_options'].''; if (3 != $survey_data['survey_type']) { echo ''. Display::return_icon('edit.png', get_lang('Edit')).''; } echo ''. Display::return_icon('copy.png', get_lang('Copy'), '', ICON_SIZE_SMALL).''; echo ''. Display::return_icon('delete.png', get_lang('Delete')).''; if (3 != $survey_data['survey_type']) { if ($question_counter > 1) { echo ''. Display::return_icon('up.png', get_lang('Move up')).''; } else { Display::display_icon('up_na.png', ' ', '', ICON_SIZE_SMALL); } if ($question_counter < $question_counter_max) { echo ''. Display::return_icon('down.png', get_lang('Move down')).''; } else { Display::display_icon('down_na.png', ' ', '', ICON_SIZE_SMALL); } } echo ' '.((0 == $row['survey_group_pri']) ? get_lang('Secondary') : get_lang('Primary')).''.((0 == $row['survey_group_pri']) ? $groups[$row['survey_group_sec1']].'-'.$groups[$row['survey_group_sec2']] : $groups[$row['survey_group_pri']]).'
'; if ($is_survey_type_1) { echo '

'.get_lang('Manage groups').'

'; if (in_array( $_GET['sendmsg'], ['GroupUpdatedSuccessfully', 'GroupDeletedSuccessfully', 'GroupCreatedSuccessfully'] ) ) { echo Display::return_message( get_lang($_GET['sendmsg']), 'confirmation', false ); } if (in_array($_GET['sendmsg'], ['GroupNeedName'])) { echo Display::return_message( get_lang($_GET['sendmsg']), 'warning', false ); } echo '
'.get_lang('Name').''.get_lang('Description').'
'; echo '
'; if ('editgroup' === $_GET['action']) { $sql = 'SELECT name,description FROM '.$table_survey_question_group.' WHERE id = '.intval($_GET['gid']).' AND survey_id = '.$survey_id.' LIMIT 1'; $rs = Database::query($sql); $editedrow = Database::fetch_array($rs, 'ASSOC'); echo ''; echo ''; echo ''; echo ''; } else { echo ''; echo ''; echo '
'; echo ''; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; $sql = 'SELECT id,name,description FROM '.$table_survey_question_group.' WHERE c_id = '.$course_id.' AND survey_id = '.intval($survey_id).' ORDER BY name'; $rs = Database::query($sql); $grouplist = ''; while ($row = Database::fetch_array($rs, 'ASSOC')) { $grouplist .= ''; } echo $grouplist.'
'.get_lang('Name').''.get_lang('Description').''.get_lang('Edit').'
'.$row['name'].''.$row['description'].''. ''. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).' '. ''. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).''. '
'; } Session::erase('answer_count'); Display::display_footer();