From 6c740d3d56819abae6bb2c6f7f6adaa823e5c60a Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 23 Dec 2020 14:15:11 +0100 Subject: [PATCH] Surveys: Fix legacy code use iid --- public/main/survey/ch_dropdown.php | 6 +- public/main/survey/ch_multiplechoice.php | 6 +- public/main/survey/ch_multiplechoiceother.php | 4 +- public/main/survey/ch_percentage.php | 2 +- public/main/survey/ch_personality.php | 41 ++- public/main/survey/ch_score.php | 6 +- public/main/survey/create_new_survey.php | 56 +-- public/main/survey/fillsurvey.php | 42 +-- public/main/survey/pending.php | 2 +- public/main/survey/preview.php | 22 +- public/main/survey/survey.lib.php | 343 ++++++++---------- public/main/survey/survey.php | 82 +++-- public/main/survey/surveyUtil.class.php | 136 ++++--- public/main/survey/survey_invite.php | 6 +- public/main/survey/survey_list.php | 6 +- 15 files changed, 374 insertions(+), 386 deletions(-) diff --git a/public/main/survey/ch_dropdown.php b/public/main/survey/ch_dropdown.php index 53b33ebed2..82d328a6d1 100644 --- a/public/main/survey/ch_dropdown.php +++ b/public/main/survey/ch_dropdown.php @@ -7,12 +7,12 @@ class ch_dropdown extends survey_question { /** - * @param array $survey_data + * @param array $surveyData * @param $formData */ - public function createForm($survey_data, $formData) + public function createForm($surveyData, $formData) { - parent::createForm($survey_data, $formData); + parent::createForm($surveyData, $formData); if (is_array($formData['answers'])) { foreach ($formData['answers'] as $key => $value) { diff --git a/public/main/survey/ch_multiplechoice.php b/public/main/survey/ch_multiplechoice.php index 4a3749815f..bf542e1d4b 100644 --- a/public/main/survey/ch_multiplechoice.php +++ b/public/main/survey/ch_multiplechoice.php @@ -7,14 +7,14 @@ class ch_multiplechoice extends survey_question { /** - * @param array $survey_data + * @param array $surveyData * @param array $formData * * @return FormValidator */ - public function createForm($survey_data, $formData) + public function createForm($surveyData, $formData) { - parent::createForm($survey_data, $formData); + parent::createForm($surveyData, $formData); $options = [ 'horizontal' => get_lang('Horizontal'), diff --git a/public/main/survey/ch_multiplechoiceother.php b/public/main/survey/ch_multiplechoiceother.php index d1d1554720..4b28070907 100644 --- a/public/main/survey/ch_multiplechoiceother.php +++ b/public/main/survey/ch_multiplechoiceother.php @@ -10,9 +10,9 @@ class ch_multiplechoiceother extends survey_question * * @return FormValidator */ - public function createForm($survey_data, $formData) + public function createForm($surveyData, $formData) { - parent::createForm($survey_data, $formData); + parent::createForm($surveyData, $formData); $options = [ 'horizontal' => get_lang('Horizontal'), diff --git a/public/main/survey/ch_percentage.php b/public/main/survey/ch_percentage.php index b724059aca..fa1d7d85d7 100644 --- a/public/main/survey/ch_percentage.php +++ b/public/main/survey/ch_percentage.php @@ -8,7 +8,7 @@ class ch_percentage extends survey_question { /** * @param array $questionData - * @param array $answers + * @param string $answers */ public function render(FormValidator $form, $questionData = [], $answers = '') { diff --git a/public/main/survey/ch_personality.php b/public/main/survey/ch_personality.php index 6dd8652dcb..afe516c918 100644 --- a/public/main/survey/ch_personality.php +++ b/public/main/survey/ch_personality.php @@ -13,9 +13,9 @@ class ch_personality extends survey_question * * @version January 2007 */ - public function createForm($survey_data, $form_content) + public function createForm($surveyData, $formData) { - parent::createForm($survey_data, $form_content); + parent::createForm($surveyData, $formData); $this->html .= ' '; $this->html .= ' '.get_lang('Display').''; $this->html .= ' '; @@ -24,13 +24,13 @@ class ch_personality extends survey_question $this->html .= '  '; $this->html .= ' '; $this->html .= ' html .= 'checked="checked"'; } $this->html .= '/>'.get_lang('Horizontal').'
'; $this->html .= ' html .= 'checked="checked"'; } @@ -46,22 +46,31 @@ class ch_personality extends survey_question $this->html .= ' '; $this->html .= ' '.get_lang('Answer options').''; $this->html .= ' '; - $total_number_of_answers = count($form_content['answers']); + $total_number_of_answers = count($formData['answers']); $question_values = []; // Values of question options - if (is_array($form_content['values'])) { // Check if data is correct - foreach ($form_content['values'] as $key => &$value) { + if (is_array($formData['values'])) { // Check if data is correct + foreach ($formData['values'] as $key => &$value) { $question_values[] = ''; } } $count = 0; - if (is_array($form_content['answers'])) { - foreach ($form_content['answers'] as $key => &$value) { + if (is_array($formData['answers'])) { + foreach ($formData['answers'] as $key => &$value) { $this->html .= ''; $this->html .= ''; - $this->html .= ''.api_return_html_area('answers['.$key.']', api_html_entity_decode(stripslashes($form_content['answers'][$key])), '', '', null, ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120']).''; + $this->html .= ''; + $this->html .= api_return_html_area( + 'answers['.$key.']', + api_html_entity_decode(stripslashes($formData['answers'][$key])), + '', + '', + null, + ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120'] + ); + $this->html .= ''; $this->html .= ''; if ($total_number_of_answers > 2) { @@ -69,13 +78,19 @@ class ch_personality extends survey_question } if ($key < $total_number_of_answers - 1) { - $this->html .= ''; + $this->html .= ''; } if ($key > 0) { - $this->html .= ''; + $this->html .= ''; } if ($total_number_of_answers > 2) { - $this->html .= ''; + $this->html .= ''; } $this->html .= ''; $this->html .= ''; diff --git a/public/main/survey/ch_score.php b/public/main/survey/ch_score.php index 5d3880fb03..52d4d6ceab 100644 --- a/public/main/survey/ch_score.php +++ b/public/main/survey/ch_score.php @@ -7,12 +7,12 @@ class ch_score extends survey_question { /** - * @param array $survey_data + * @param array $surveyData * @param array $formData */ - public function createForm($survey_data, $formData) + public function createForm($surveyData, $formData) { - parent::createForm($survey_data, $formData); + parent::createForm($surveyData, $formData); $this->getForm()->addText('maximum_score', get_lang('Score')); $config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120']; if (is_array($formData['answers'])) { diff --git a/public/main/survey/create_new_survey.php b/public/main/survey/create_new_survey.php index d4ce8b9f9c..a3d5f21d6b 100644 --- a/public/main/survey/create_new_survey.php +++ b/public/main/survey/create_new_survey.php @@ -1,4 +1,5 @@ , Chamilo: Personality * Test modification and rewriting large parts of the code * - * @todo only the available platform languages should be used => need an + * @todo only the available platform languages should be used => need an * api get_languages and and api_get_available_languages (or a parameter) */ require_once __DIR__.'/../inc/global.inc.php'; $_course = api_get_course_info(); $this_section = SECTION_COURSES; - -$allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime'); $table_gradebook_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK); /** @todo this has to be moved to a more appropriate place (after the display_header of the code) */ @@ -23,7 +22,7 @@ $table_gradebook_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK); if (!api_is_allowed_to_edit()) { if (!api_is_session_general_coach() || (!empty($_GET['survey_id']) && - !api_is_element_in_the_session(TOOL_SURVEY, $_GET['survey_id'])) + !api_is_element_in_the_session(TOOL_SURVEY, $_GET['survey_id'])) ) { api_not_allowed(true); } @@ -61,17 +60,14 @@ if ('edit' == $action && is_numeric($survey_id)) { } $gradebook_link_id = null; // Getting the default values -if ('edit' == $action && isset($survey_id) && is_numeric($survey_id)) { +if ('edit' === $action && isset($survey_id) && is_numeric($survey_id)) { $defaults = $survey_data; $defaults['survey_id'] = $survey_id; $defaults['anonymous'] = $survey_data['anonymous']; - - if ($allowSurveyAvailabilityDatetime) { - $defaults['avail_from'] = api_get_local_time($defaults['avail_from'], null, 'UTC'); - $defaults['avail_till'] = api_get_local_time($defaults['avail_till'], null, 'UTC'); - $defaults['start_date'] = $defaults['avail_from']; - $defaults['end_date'] = $defaults['avail_till']; - } + $defaults['avail_from'] = api_get_local_time($defaults['avail_from'], null, 'UTC'); + $defaults['avail_till'] = api_get_local_time($defaults['avail_till'], null, 'UTC'); + $defaults['start_date'] = $defaults['avail_from']; + $defaults['end_date'] = $defaults['avail_till']; $link_info = GradebookUtils::isResourceInCourseGradebook( $course_id, @@ -94,15 +90,9 @@ if ('edit' == $action && isset($survey_id) && is_numeric($survey_id)) { } } else { $defaults['survey_language'] = $_course['language']; - $defaults['start_date'] = date( - $allowSurveyAvailabilityDatetime ? 'Y-m-d 00:00:00' : 'Y-m-d', - api_strtotime(api_get_local_time()) - ); + $defaults['start_date'] = date('Y-m-d 00:00:00', api_strtotime(api_get_local_time())); $startdateandxdays = time() + 864000; // today + 10 days - $defaults['end_date'] = date( - $allowSurveyAvailabilityDatetime ? 'Y-m-d 23:59:59' : 'Y-m-d', - $startdateandxdays - ); + $defaults['end_date'] = date('Y-m-d 23:59:59', $startdateandxdays); $defaults['anonymous'] = 0; } @@ -154,20 +144,10 @@ $form->addElement( // Pass the language of the survey in the form $form->addElement('hidden', 'survey_language'); - -$allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime'); - -if ($allowSurveyAvailabilityDatetime) { - $startDateElement = $form->addDateTimePicker('start_date', get_lang('Start Date')); - $endDateElement = $form->addDateTimePicker('end_date', get_lang('End Date')); - $form->addRule('start_date', get_lang('Invalid date'), 'datetime'); - $form->addRule('end_date', get_lang('Invalid date'), 'datetime'); -} else { - $startDateElement = $form->addElement('date_picker', 'start_date', get_lang('Start Date')); - $endDateElement = $form->addElement('date_picker', 'end_date', get_lang('End Date')); - $form->addRule('start_date', get_lang('Invalid date'), 'date'); - $form->addRule('end_date', get_lang('Invalid date'), 'date'); -} +$startDateElement = $form->addDateTimePicker('start_date', get_lang('Start Date')); +$endDateElement = $form->addDateTimePicker('end_date', get_lang('End Date')); +$form->addRule('start_date', get_lang('Invalid date'), 'datetime'); +$form->addRule('end_date', get_lang('Invalid date'), 'datetime'); $form->setRequired($startDateElement); $form->setRequired($endDateElement); @@ -323,20 +303,20 @@ $skillList = Skill::addSkillsToForm($form, ITEM_TYPE_SURVEY, $survey_id); $form->addElement('html', '
'); -if (isset($_GET['survey_id']) && 'edit' == $action) { +if (isset($_GET['survey_id']) && 'edit' === $action) { $form->addButtonUpdate(get_lang('Edit survey'), 'submit_survey'); } else { $form->addButtonCreate(get_lang('Create survey'), 'submit_survey'); } // Setting the rules -if ('add' == $action) { +if ('add' === $action) { $form->addRule('survey_code', get_lang('Required field'), 'required'); $form->addRule('survey_code', '', 'maxlength', 20); } $form->addRule('survey_title', get_lang('Required field'), 'required'); -$form->addRule('start_date', get_lang('Invalid date'), $allowSurveyAvailabilityDatetime ? 'datetime' : 'date'); -$form->addRule('end_date', get_lang('Invalid date'), $allowSurveyAvailabilityDatetime ? 'datetime' : 'date'); +$form->addRule('start_date', get_lang('Invalid date'), 'datetime'); +$form->addRule('end_date', get_lang('Invalid date'), 'datetime'); $form->addRule( ['start_date', 'end_date'], get_lang('Start DateShouldBeBeforeEnd Date'), diff --git a/public/main/survey/fillsurvey.php b/public/main/survey/fillsurvey.php index ac52d13f14..7f54039c52 100644 --- a/public/main/survey/fillsurvey.php +++ b/public/main/survey/fillsurvey.php @@ -306,11 +306,11 @@ if (count($_POST) > 0) { } else { // All the other question types (open question, multiple choice, percentage, ...) if (isset($types[$survey_question_id]) && - 'percentage' == $types[$survey_question_id]) { + 'percentage' === $types[$survey_question_id]) { $sql = "SELECT * FROM $table_survey_question_option WHERE c_id = $course_id AND - question_option_id='".intval($value)."'"; + iid='".intval($value)."'"; $result = Database::query($sql); $row = Database::fetch_array($result, 'ASSOC'); $option_value = $row['option_text']; @@ -375,7 +375,7 @@ if (count($_POST) > 0) { } // We select the correct answer and the puntuacion $sql = "SELECT value FROM $table_survey_question_option - WHERE c_id = $course_id AND question_option_id='".intval($value)."'"; + WHERE c_id = $course_id AND iid='".intval($value)."'"; $result = Database::query($sql); $row = Database::fetch_array($result, 'ASSOC'); $option_value = $row['value']; @@ -734,24 +734,24 @@ if ((isset($_GET['show']) && '' != $_GET['show']) || survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri, - survey_question.question_id, + survey_question.iid question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, survey_question.max_value, - survey_question_option.question_option_id, + survey_question_option.iid question_option_id, survey_question_option.option_text, $select survey_question_option.sort as option_sort FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option - ON survey_question.question_id = survey_question_option.question_id AND + ON survey_question.iid = survey_question_option.question_id AND survey_question_option.c_id = $course_id WHERE survey_question.survey_id = '".Database::escape_string($survey_invitation['survey_id'])."' AND - survey_question.question_id NOT IN ( + survey_question.iid NOT IN ( SELECT sa.question_id FROM ".$table_survey_answer." sa WHERE @@ -763,26 +763,26 @@ if ((isset($_GET['show']) && '' != $_GET['show']) || survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri, - survey_question.question_id, + survey_question.iid question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, survey_question.max_value, - survey_question_option.question_option_id, + survey_question_option.iid question_option_id, survey_question_option.option_text, $select survey_question_option.sort as option_sort ".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')." FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option - ON survey_question.question_id = survey_question_option.question_id AND + ON survey_question.iid = survey_question_option.question_id AND survey_question_option.c_id = $course_id WHERE survey_question NOT LIKE '%{{%' AND survey_question.survey_id = '".intval($survey_invitation['survey_id'])."' AND - survey_question.question_id IN (".implode(',', $paged_questions[$_GET['show']]).") AND + survey_question.iid IN (".implode(',', $paged_questions[$_GET['show']]).") AND survey_question.c_id = $course_id ORDER BY survey_question.sort, survey_question_option.sort ASC"; } @@ -836,7 +836,7 @@ if ((isset($_GET['show']) && '' != $_GET['show']) || SUM(value) as value FROM $table_survey_answer as survey_answer INNER JOIN $table_survey_question as survey_question - ON (survey_question.question_id = survey_answer.question_id) + ON (survey_question.iid = survey_answer.question_id) WHERE survey_answer.survey_id='".$my_survey_id."' AND survey_answer.user='".$current_user."' AND @@ -860,17 +860,17 @@ if ((isset($_GET['show']) && '' != $_GET['show']) || SELECT MAX(value) as value, survey_group_pri, - survey_question.question_id + survey_question.iid question_id FROM $table_survey_question as survey_question INNER JOIN $table_survey_question_option as survey_question_option - ON (survey_question.question_id = survey_question_option.question_id) + ON (survey_question.iid = survey_question_option.question_id) WHERE survey_question.survey_id='".$my_survey_id."' AND survey_question.c_id = $course_id AND survey_question_option.c_id = $course_id AND survey_group_sec1='0' AND survey_group_sec2='0' - GROUP BY survey_group_pri, survey_question.question_id + GROUP BY survey_group_pri, survey_question.iid ) as temp GROUP BY temp.survey_group_pri ORDER BY temp.survey_group_pri"; @@ -1028,7 +1028,7 @@ if ((isset($_GET['show']) && '' != $_GET['show']) || survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri, - survey_question.question_id, + survey_question.iid question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, @@ -1040,13 +1040,13 @@ if ((isset($_GET['show']) && '' != $_GET['show']) || survey_question_option.sort as option_sort FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option - ON survey_question.question_id = survey_question_option.question_id AND + ON survey_question.iid = survey_question_option.question_id AND survey_question_option.c_id = $course_id WHERE survey_question NOT LIKE '%{{%' AND survey_question.survey_id = '".$my_survey_id."' AND survey_question.c_id = $course_id AND - survey_question.question_id IN (".implode(',', $paged_questions_sec[$val]).") + survey_question.iid IN (".implode(',', $paged_questions_sec[$val]).") ORDER $shuffle "; $result = Database::query($sql); @@ -1139,7 +1139,7 @@ if ((isset($_GET['show']) && '' != $_GET['show']) || survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri, - survey_question.question_id, + survey_question.iid question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, @@ -1152,13 +1152,13 @@ if ((isset($_GET['show']) && '' != $_GET['show']) || ".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')." FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option - ON survey_question.question_id = survey_question_option.question_id AND + ON survey_question.iid = survey_question_option.question_id AND survey_question_option.c_id = $course_id WHERE survey_question NOT LIKE '%{{%' AND survey_question.survey_id = '".intval($survey_invitation['survey_id'])."' AND survey_question.c_id = $course_id AND - survey_question.question_id IN (".$imploded.") + survey_question.iid IN (".$imploded.") ORDER $order_sql "; $result = Database::query($sql); $question_counter_max = Database :: num_rows($result); diff --git a/public/main/survey/pending.php b/public/main/survey/pending.php index 87bafec522..a83f21d0de 100644 --- a/public/main/survey/pending.php +++ b/public/main/survey/pending.php @@ -34,7 +34,7 @@ foreach ($pending as $i => $item) { $course = $course ? ['id' => $course->getId(), 'title' => $course->getTitle(), 'code' => $course->getCode()] : null; $session = $session ? ['id' => $session->getId(), 'name' => $session->getName()] : null; $courseInfo = api_get_course_info_by_id($course->getId()); - $surveysData[$survey->getSurveyId()] = [ + $surveysData[$survey->getIid()] = [ 'title' => $survey->getTitle(), 'avail_from' => $survey->getAvailFrom(), 'avail_till' => $survey->getAvailTill(), diff --git a/public/main/survey/preview.php b/public/main/survey/preview.php index b9e370cf36..1e449e820f 100644 --- a/public/main/survey/preview.php +++ b/public/main/survey/preview.php @@ -85,7 +85,7 @@ if (isset($_GET['show'])) { $counter = 0; $sql = "SELECT * FROM $table_survey_question WHERE - survey_question NOT LIKE '%{{%' AND + survey_question NOT LIKE '%{{%' AND c_id = $course_id AND survey_id = $surveyId ORDER BY sort ASC"; @@ -94,17 +94,17 @@ if (isset($_GET['show'])) { if (Database::num_rows($result)) { while ($row = Database::fetch_array($result)) { if (1 == $survey_data['one_question_per_page']) { - if ('pagebreak' != $row['type']) { - $paged_questions[$counter][] = $row['question_id']; + if ('pagebreak' !== $row['type']) { + $paged_questions[$counter][] = $row['iid']; $counter++; continue; } } else { - if ('pagebreak' == $row['type']) { + if ('pagebreak' === $row['type']) { $counter++; $pageBreakText[$counter] = $row['survey_question']; } else { - $paged_questions[$counter][] = $row['question_id']; + $paged_questions[$counter][] = $row['iid']; } } } @@ -118,14 +118,14 @@ if (isset($_GET['show'])) { $select = ' survey_question.parent_id, survey_question.parent_option_id, '; } $sql = "SELECT - survey_question.question_id, + survey_question.iid question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, survey_question.max_value, - survey_question_option.question_option_id, + survey_question_option.iid as question_option_id, survey_question_option.option_text, $select survey_question_option.sort as option_sort @@ -133,19 +133,19 @@ if (isset($_GET['show'])) { FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option ON - survey_question.question_id = survey_question_option.question_id AND + survey_question.iid = survey_question_option.question_id AND survey_question_option.c_id = survey_question.c_id WHERE survey_question.survey_id = '".$surveyId."' AND - survey_question.question_id IN (".Database::escape_string(implode(',', $paged_questions[$_GET['show']]), null, false).") AND + survey_question.iid IN (".Database::escape_string(implode(',', $paged_questions[$_GET['show']]), null, false).") AND survey_question.c_id = $course_id AND - survey_question NOT LIKE '%{{%' + survey_question NOT LIKE '%{{%' ORDER BY survey_question.sort, survey_question_option.sort ASC"; $result = Database::query($sql); while ($row = Database::fetch_array($result)) { // If the type is not a pagebreak we store it in the $questions array - if ('pagebreak' != $row['type']) { + if ('pagebreak' !== $row['type']) { $sort = $row['sort']; $questions[$sort]['question_id'] = $row['question_id']; $questions[$sort]['survey_id'] = $row['survey_id']; diff --git a/public/main/survey/survey.lib.php b/public/main/survey/survey.lib.php index 3aa2dd7e45..3cd1212d1c 100644 --- a/public/main/survey/survey.lib.php +++ b/public/main/survey/survey.lib.php @@ -2,9 +2,11 @@ /* For licensing terms, see /license.txt */ +use Chamilo\CoreBundle\Framework\Container; use Chamilo\CourseBundle\Entity\CSurvey; use Chamilo\CourseBundle\Entity\CSurveyInvitation; use Chamilo\CourseBundle\Entity\CSurveyQuestion; +use Chamilo\CourseBundle\Entity\CSurveyQuestionOption; /** * Class SurveyManager. @@ -116,7 +118,7 @@ class SurveyManager * Retrieves all the survey information. * * @param int $survey_id the id of the survey - * @param bool $shared this parameter determines if + * @param int $shared this parameter determines if * we have to get the information of a survey from the central (shared) database or from the * course database * @param string course code optional @@ -152,14 +154,14 @@ class SurveyManager if (0 != $shared) { $table_survey = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); $sql = "SELECT * FROM $table_survey - WHERE survey_id='".$survey_id."' "; + WHERE iid =' ".$survey_id."' "; } else { if (empty($courseInfo)) { return []; } $sql = "SELECT * FROM $table_survey WHERE - survey_id='".$survey_id."' AND + iid = '".$survey_id."' AND c_id = ".$courseInfo['real_id']; } @@ -220,13 +222,13 @@ class SurveyManager */ public static function store_survey($values) { - $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime'); $_user = api_get_user_info(); $course_id = api_get_course_int_id(); $session_id = api_get_session_id(); $courseCode = api_get_course_id(); $table_survey = Database::get_course_table(TABLE_SURVEY); $shared_survey_id = 0; + $repo = Container::getSurveyRepository(); if (!isset($values['survey_id'])) { // Check if the code doesn't soon exists in this language @@ -256,7 +258,6 @@ class SurveyManager $values['anonymous'] = (int) $values['anonymous']; $survey = new CSurvey(); - $extraParams = []; if (0 == $values['anonymous']) { // Input_name_list @@ -341,12 +342,11 @@ class SurveyManager } } - $from = api_get_utc_datetime($values['start_date'].'00:00:00', true, true); - $until = api_get_utc_datetime($values['end_date'].'23:59:59', true, true); - if ($allowSurveyAvailabilityDatetime) { - $from = api_get_utc_datetime($values['start_date'].':00', true, true); - $until = api_get_utc_datetime($values['end_date'].':59', true, true); - } + $from = api_get_utc_datetime($values['start_date'].':00', true, true); + $until = api_get_utc_datetime($values['end_date'].':59', true, true); + + $course = api_get_course_entity(); + $session = api_get_session_entity(); $survey ->setCId($course_id) @@ -364,11 +364,11 @@ class SurveyManager ->setAnonymous($values['anonymous']) ->setSessionId(api_get_session_id()) ->setVisibleResults($values['visible_results']) + ->setParent($course) + ->addCourseLink($course, $session) ; - $em = Database::getManager(); - $em->persist($survey); - $em->flush(); + $repo->create($survey); $survey_id = $survey->getIid(); if ($survey_id > 0) { @@ -383,13 +383,13 @@ class SurveyManager ); // Insert into item_property - api_item_property_update( + /*api_item_property_update( api_get_course_info(), TOOL_SURVEY, $survey_id, 'SurveyAdded', api_get_user_id() - ); + );*/ } if (1 == $values['survey_type'] && !empty($values['parent_id'])) { @@ -410,7 +410,7 @@ class SurveyManager c_id = '.$course_id.' AND code = "'.Database::escape_string($values['survey_code']).'" AND lang = "'.Database::escape_string($values['survey_language']).'" AND - survey_id !='.intval($values['survey_id']); + iid !='.intval($values['survey_id']); $rs = Database::query($sql); if (Database::num_rows($rs) > 0) { Display::addFlash( @@ -431,13 +431,17 @@ class SurveyManager $values['anonymous'] = 0; } + /** @var CSurvey $survey */ + $survey = $repo->find($values['survey_id']); + $extraParams = []; - $extraParams['one_question_per_page'] = isset($values['one_question_per_page']) ? $values['one_question_per_page'] : 0; - $extraParams['shuffle'] = isset($values['shuffle']) ? $values['shuffle'] : 0; + $survey->setOneQuestionPerPage(isset($values['one_question_per_page']) ? $values['one_question_per_page'] : 0); + $survey->setShuffle(isset($values['shuffle']) ? $values['shuffle'] : 0); if (0 == $values['anonymous']) { - $extraParams['show_form_profile'] = isset($values['show_form_profile']) ? $values['show_form_profile'] : 0; - if (1 == $extraParams['show_form_profile']) { + $survey->setShowFormProfile(isset($values['show_form_profile']) ? $values['show_form_profile'] : 0); + $isFormProfile = isset($values['show_form_profile']) ? $values['show_form_profile'] : 0; + if (1 == $isFormProfile) { $fields = explode(',', $values['input_name_list']); $field_values = ''; foreach ($fields as &$field) { @@ -450,47 +454,33 @@ class SurveyManager $field_values .= $field.':'.$values[$field].'@'; } } - $extraParams['form_fields'] = $field_values; + $survey->setFormFields($field_values); } else { - $extraParams['form_fields'] = ''; + $survey->setFormFields(''); } } else { - $extraParams['show_form_profile'] = 0; - $extraParams['form_fields'] = ''; + $survey->setFormFields(''); + $survey->setShowFormProfile(0); } - $params = [ - 'title' => $values['survey_title'], - 'subtitle' => $values['survey_subtitle'], - 'author' => $_user['user_id'], - 'lang' => $values['survey_language'], - 'avail_from' => $allowSurveyAvailabilityDatetime - ? api_get_utc_datetime($values['start_date'].':00') - : $values['start_date'], - 'avail_till' => $allowSurveyAvailabilityDatetime - ? api_get_utc_datetime($values['end_date'].':59') - : $values['end_date'], - 'is_shared' => $shared_survey_id, - 'template' => 'template', - 'intro' => $values['survey_introduction'], - 'surveythanks' => $values['survey_thanks'], - 'anonymous' => $values['anonymous'], - 'session_id' => api_get_session_id(), - 'visible_results' => $values['visible_results'], - ]; - - $params = array_merge($params, $extraParams); - Database::update( - $table_survey, - $params, - [ - 'c_id = ? AND survey_id = ?' => [ - $course_id, - $values['survey_id'], - ], - ] - ); + $survey + ->setTitle($values['survey_title']) + ->setSubtitle($values['survey_title']) + ->setAuthor($_user['user_id']) + ->setLang($values['survey_language']) + ->setAvailFrom(api_get_utc_datetime($values['start_date'].':00', true, true)) + ->setAvailTill(api_get_utc_datetime($values['end_date'].':59', true, true)) + ->setIsShared($shared_survey_id) + ->setTemplate('template') + ->setIntro($values['survey_introduction']) + ->setSurveyThanks($values['survey_thanks']) + ->setAnonymous($values['anonymous']) + ->setSessionId(api_get_session_id()) + ->setVisibleResults($values['visible_results']) + ; + $repo->update($survey); + /* // Update into item_property (update) api_item_property_update( api_get_course_info(), @@ -498,7 +488,7 @@ class SurveyManager $values['survey_id'], 'SurveyUpdated', api_get_user_id() - ); + );*/ Display::addFlash( Display::return_message( @@ -657,11 +647,11 @@ class SurveyManager $table_survey = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY); // Deleting the survey $sql = "DELETE FROM $table_survey - WHERE survey_id='".$survey_id."'"; + WHERE iid ='".$survey_id."'"; Database::query($sql); } else { $sql = "DELETE FROM $table_survey - WHERE c_id = $course_id AND survey_id='".$survey_id."'"; + WHERE c_id = $course_id AND iid ='".$survey_id."'"; Database::query($sql); } @@ -676,7 +666,7 @@ class SurveyManager ); // Deleting groups of this survey $sql = "DELETE FROM $table_survey_question_group - WHERE c_id = $course_id AND survey_id='".$survey_id."'"; + WHERE c_id = $course_id AND iid='".$survey_id."'"; Database::query($sql); // Deleting the questions of the survey @@ -705,11 +695,8 @@ class SurveyManager * * @return bool */ - public static function copy_survey( - $survey_id, - $new_survey_id = null, - $targetCourseId = null - ) { + public static function copy_survey($survey_id, $new_survey_id = null, $targetCourseId = null) + { $course_id = api_get_course_int_id(); if (!$targetCourseId) { $targetCourseId = $course_id; @@ -741,25 +728,21 @@ class SurveyManager $new_survey_id = Database::insert($table_survey, $params); if ($new_survey_id) { - $sql = "UPDATE $table_survey SET survey_id = $new_survey_id - WHERE iid = $new_survey_id"; - Database::query($sql); - // Insert into item_property - api_item_property_update( + /*api_item_property_update( api_get_course_info(), TOOL_SURVEY, $new_survey_id, 'SurveyAdded', api_get_user_id() - ); + );*/ } } else { $new_survey_id = (int) $new_survey_id; } $sql = "SELECT * FROM $table_survey_question_group - WHERE c_id = $course_id AND survey_id = $survey_id"; + WHERE c_id = $course_id AND iid = $survey_id"; $res = Database::query($sql); while ($row = Database::fetch_array($res, 'ASSOC')) { $params = [ @@ -805,8 +788,8 @@ class SurveyManager $insertId = Database::insert($table_survey_question, $params); if ($insertId) { - $sql = "UPDATE $table_survey_question SET question_id = iid WHERE iid = $insertId"; - Database::query($sql); + /*$sql = "UPDATE $table_survey_question SET question_id = iid WHERE iid = $insertId"; + Database::query($sql);*/ $question_id[$row['question_id']] = $insertId; } } @@ -927,7 +910,7 @@ class SurveyManager SET answered = answered + 1 WHERE c_id = $course_id AND - survey_id = ".$survey_id; + iid = ".$survey_id; Database::query($sql); $allow = api_get_configuration_value('survey_answered_at_field'); @@ -1011,7 +994,6 @@ class SurveyManager */ public static function get_question($question_id, $shared = false) { - // Table definitions $tbl_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); $course_id = api_get_course_int_id(); @@ -1022,7 +1004,7 @@ class SurveyManager } $sql = "SELECT * FROM $tbl_survey_question - WHERE c_id = $course_id AND question_id = $question_id + WHERE c_id = $course_id AND iid = $question_id ORDER BY `sort` "; $sqlOption = " SELECT * FROM $table_survey_question_option @@ -1034,7 +1016,7 @@ class SurveyManager $table_survey_question_option = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); $sql = "SELECT * FROM $tbl_survey_question - WHERE question_id = $question_id + WHERE iid = $question_id ORDER BY `sort` "; $sqlOption = "SELECT * FROM $table_survey_question_option WHERE question_id = $question_id @@ -1048,7 +1030,7 @@ class SurveyManager $return['survey_id'] = $row['survey_id']; $return['parent_id'] = isset($row['parent_id']) ? $row['parent_id'] : 0; $return['parent_option_id'] = isset($row['parent_option_id']) ? $row['parent_option_id'] : 0; - $return['question_id'] = $row['question_id']; + $return['question_id'] = $row['iid']; $return['type'] = $row['type']; $return['question'] = $row['survey_question']; $return['horizontalvertical'] = $row['display']; @@ -1077,7 +1059,7 @@ class SurveyManager $return['answer_data'][$counter]['data'] = $row['option_text']; $return['answer_data'][$counter]['iid'] = $row['iid']; /** @todo this can be done more elegantly (used in reporting) */ - $return['answersid'][] = $row['question_option_id']; + $return['answersid'][] = $row['iid']; $counter++; } @@ -1116,7 +1098,7 @@ class SurveyManager $result = Database::query($sql); $questions = []; while ($row = Database::fetch_array($result, 'ASSOC')) { - $questionId = $row['question_id']; + $questionId = $row['iid']; $questions[$questionId]['survey_id'] = $surveyId; $questions[$questionId]['question_id'] = $questionId; $questions[$questionId]['type'] = $row['type']; @@ -1180,14 +1162,14 @@ class SurveyManager } } - if ('score' == $form_content['type']) { + if ('score' === $form_content['type']) { if (strlen($form_content['maximum_score']) < 1) { $empty_answer = true; } } + $em = Database::getManager(); $course_id = api_get_course_int_id(); - if (!$empty_answer) { // Table definitions $tbl_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); @@ -1215,7 +1197,6 @@ class SurveyManager $question = new CSurveyQuestion(); // Some variables defined for survey-test type - $extraParams = []; if (isset($_POST['choose'])) { if (1 == $_POST['choose']) { $question->setSurveyGroupPri($_POST['assigned']); @@ -1254,48 +1235,48 @@ class SurveyManager } } - $em = Database::getManager(); $em->persist($question); $em->flush(); $question_id = $question->getIid(); if ($question_id) { - $sql = "UPDATE $tbl_survey_question SET question_id = $question_id + /*$sql = "UPDATE $tbl_survey_question SET question_id = $question_id WHERE iid = $question_id"; - Database::query($sql); + Database::query($sql);*/ $form_content['question_id'] = $question_id; $return_message = 'QuestionAdded'; } } else { + $repo = $em->getRepository(CSurveyQuestion::class); + $repoOption = $em->getRepository(CSurveyQuestionOption::class); + /** @var CSurveyQuestion $question */ + $question = $repo->find($form_content['question_id']); + // Updating an existing question $extraParams = []; if (isset($_POST['choose'])) { if (1 == $_POST['choose']) { - $extraParams['survey_group_pri'] = $_POST['assigned']; - $extraParams['survey_group_sec1'] = 0; - $extraParams['survey_group_sec2'] = 0; + $question->setSurveyGroupPri($_POST['assigned']); + $question->setSurveyGroupSec1(0); + $question->setSurveyGroupSec2(0); } elseif (2 == $_POST['choose']) { - $extraParams['survey_group_pri'] = 0; - $extraParams['survey_group_sec1'] = $_POST['assigned1']; - $extraParams['survey_group_sec2'] = $_POST['assigned2']; + $question->setSurveyGroupPri(0); + $question->setSurveyGroupSec1($_POST['assigned1']); + $question->setSurveyGroupSec2($_POST['assigned2']); } } $maxScore = isset($form_content['maximum_score']) ? $form_content['maximum_score'] : null; - $questionComment = isset($form_content['question_comment']) - ? $form_content['question_comment'] - : null; - - // Adding the question to the survey_question table - $params = [ - 'survey_question' => $form_content['question'], - 'survey_question_comment' => $questionComment, - 'display' => $form_content['horizontalvertical'], - ]; + $questionComment = $form_content['question_comment'] ?? ''; + $question + ->setSurveyQuestionComment($questionComment) + ->setSurveyQuestion($form_content['question']) + ->setDisplay($form_content['horizontalvertical']) + ; if (api_get_configuration_value('allow_required_survey_questions')) { - $params['is_required'] = isset($form_content['is_required']); + $question->isMandatory(isset($form_content['is_required'])); } if (api_get_configuration_value('survey_question_dependency')) { @@ -1306,34 +1287,35 @@ class SurveyManager !empty($form_content['parent_id']) && !empty($form_content['parent_option_id']) ) { - $params['parent_id'] = $form_content['parent_id']; - $params['parent_option_id'] = $form_content['parent_option_id']; + $question->setParent($repo->find($form_content['parent_id'])); + $question->setParentOption($repoOption->find($form_content['parent_option_id'])); } } - $params = array_merge($params, $extraParams); - Database::update( + $em->persist($question); + $em->flush(); + /*Database::update( $tbl_survey_question, $params, [ - 'c_id = ? AND question_id = ?' => [ + 'c_id = ? AND iid = ?' => [ $course_id, $form_content['question_id'], ], ] - ); + );*/ $return_message = 'QuestionUpdated'; } if (!empty($form_content['survey_id'])) { //Updating survey - api_item_property_update( + /*api_item_property_update( api_get_course_info(), TOOL_SURVEY, $form_content['survey_id'], 'SurveyUpdated', api_get_user_id() - ); + );*/ } // Storing the options of the question @@ -1426,19 +1408,16 @@ class SurveyManager * * @version January 2007 */ - public static function move_survey_question( - $direction, - $survey_question_id, - $survey_id - ) { + public static function move_survey_question($direction, $survey_question_id, $survey_id) + { // Table definition $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); $course_id = api_get_course_int_id(); - if ('moveup' == $direction) { + if ('moveup' === $direction) { $sort = 'DESC'; } - if ('movedown' == $direction) { + if ('movedown' === $direction) { $sort = 'ASC'; } @@ -1546,7 +1525,7 @@ class SurveyManager WHERE c_id = $course_id AND survey_id = $survey_id AND - question_id = $question_id"; + iid = $question_id"; $result = Database::query($sql); if (false == $result) { return false; @@ -1610,6 +1589,7 @@ class SurveyManager $form_content['answers'][] = $i; } } + $em = Database::getManager(); if (is_numeric($survey_data['survey_share']) && 0 != $survey_data['survey_share']) { self::save_shared_question_options($form_content, $survey_data); @@ -1654,29 +1634,39 @@ class SurveyManager $answerId = $form_content['answersid'][$i]; } if (empty($answerId)) { - $params = [ - 'c_id' => $course_id, - 'question_id' => $form_content['question_id'], - 'survey_id' => $form_content['survey_id'], - 'option_text' => $form_content['answers'][$i], - 'value' => $values, - 'sort' => $counter, - ]; - $insertId = Database::insert($table, $params); + $option = new CSurveyQuestionOption(); + $option + ->setCId($course_id) + ->setQuestionId($form_content['question_id']) + ->setOptionText($form_content['answers'][$i]) + ->setSurveyId($form_content['survey_id']) + ->setValue($values) + ->setSort($counter) + ; + $em->persist($option); + $em->flush(); + $insertId = $option->getIid(); + if ($insertId) { - $sql = "UPDATE $table + /*$sql = "UPDATE $table SET question_option_id = $insertId WHERE iid = $insertId"; - Database::query($sql); + Database::query($sql);*/ $counter++; } } else { - $params = [ - 'option_text' => $form_content['answers'][$i], - 'value' => $values, - 'sort' => $counter, - ]; - Database::update($table, $params, ['iid = ?' => [$answerId]]); + $repo = $em->getRepository(CSurveyQuestionOption::class); + /** @var CSurveyQuestionOption $option */ + $option = $repo->find($answerId); + if ($option) { + $option + ->setOptionText($form_content['answers'][$i]) + ->setValue($values) + ->setSort($counter) + ; + $em->persist($option); + $em->flush(); + } $counter++; } } @@ -1694,10 +1684,10 @@ class SurveyManager ]; $insertId = Database::insert($table, $params); if ($insertId) { - $sql = "UPDATE $table + /*$sql = "UPDATE $table SET question_option_id = $insertId WHERE iid = $insertId"; - Database::query($sql); + Database::query($sql);*/ } } else { $params = [ @@ -1823,7 +1813,7 @@ class SurveyManager $sql = "DELETE FROM $table WHERE $course_condition survey_id='".intval($survey_id)."' AND - question_id='".intval($question_id)."'"; + iid ='".intval($question_id)."'"; Database::query($sql); return true; @@ -2236,10 +2226,10 @@ class SurveyManager $insertId = Database::insert($surveyQuestionTable, $params); if ($insertId) { - $sql = "UPDATE $surveyQuestionTable + /*$sql = "UPDATE $surveyQuestionTable SET question_id = iid WHERE iid = $insertId"; - Database::query($sql); + Database::query($sql);*/ $question_id[$row['question_id']] = $insertId; } @@ -2326,11 +2316,6 @@ class SurveyManager if (false == $insertId) { return 0; } - // Normalize question_id with iid - $sql = "UPDATE $questionTable - SET question_id = iid - WHERE iid = $insertId"; - Database::query($sql); // Get questions options $sql = "SELECT * FROM $optionsTable WHERE question_id = $questionId"; @@ -2344,11 +2329,7 @@ class SurveyManager 'sort' => $row['sort'], 'value' => $row['value'], ]; - $optionId = Database::insert($optionsTable, $params); - if ($optionId) { - $sql = "UPDATE $optionsTable SET question_option_id = $optionId WHERE iid = $optionId"; - Database::query($sql); - } + Database::insert($optionsTable, $params); } return $insertId; @@ -2411,22 +2392,21 @@ class SurveyManager } if (null === $groupId) { - $obj = new UserGroup(); - $options['where'] = [' usergroup.course_id = ? ' => $courseId]; - $classList = $obj->getUserGroupInCourse($options); - - $classToParse = []; - foreach ($classList as $class) { - $users = $obj->get_users_by_usergroup($class['id']); - if (empty($users)) { - continue; + $obj = new UserGroup(); + $options['where'] = [' usergroup.course_id = ? ' => $courseId]; + $classList = $obj->getUserGroupInCourse($options); + + $classToParse = []; + foreach ($classList as $class) { + $users = $obj->get_users_by_usergroup($class['id']); + if (empty($users)) { + continue; + } + $classToParse[] = [ + 'name' => $class['name'], + 'users' => $users, + ]; } - $classToParse[] = [ - 'name' => $class['name'], - 'users' => $users, - ]; - } - self::parseMultiplicateUserList($classToParse, $questions, $courseId, $surveyData, true); } else { $groupInfo = GroupManager::get_group_properties($groupId); @@ -2436,10 +2416,10 @@ class SurveyManager $users = array_column($users, 'id'); self::parseMultiplicateUserList( [ - [ - 'name' => $groupInfo['name'], - 'users' => $users, - ], + [ + 'name' => $groupInfo['name'], + 'users' => $users, + ], ], $questions, $courseId, @@ -2575,7 +2555,6 @@ class SurveyManager $surveyId = (int) $survey['survey_id']; $table = Database::get_course_table(TABLE_SURVEY_QUESTION); - $sql = "SELECT COUNT(iid) count FROM $table WHERE c_id = $courseId AND @@ -2605,7 +2584,7 @@ class SurveyManager } $courseId = (int) $survey['c_id']; - $surveyId = (int) $survey['survey_id']; + $surveyId = (int) $survey['iid']; $table = Database::get_course_table(TABLE_SURVEY_QUESTION); @@ -2655,14 +2634,12 @@ class SurveyManager api_not_allowed(true); } - $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime'); $utcZone = new DateTimeZone('UTC'); $startDate = new DateTime($surveyData['start_date'], $utcZone); $endDate = new DateTime($surveyData['end_date'], $utcZone); $currentDate = new DateTime('now', $utcZone); - if (!$allowSurveyAvailabilityDatetime) { - $currentDate->modify('today'); - } + $currentDate->modify('today'); + if ($currentDate < $startDate) { api_not_allowed( true, @@ -2702,7 +2679,7 @@ class SurveyManager $sessionId = 0, $groupId = 0 ) { - $invitationRepo = Database::getManager()->getRepository('ChamiloCourseBundle:CSurveyInvitation'); + $invitationRepo = Database::getManager()->getRepository(CSurveyInvitation::class); return $invitationRepo->findBy( [ @@ -2732,8 +2709,8 @@ class SurveyManager } $em = Database::getManager(); - $repo = $em->getRepository('ChamiloCourseBundle:CSurveyInvitation'); - $repoSurvey = $em->getRepository('ChamiloCourseBundle:CSurvey'); + $repo = $em->getRepository(CSurveyInvitation::class); + $repoSurvey = $em->getRepository(CSurvey::class); $invitations = $repo->findBy(['user' => $userId, 'answered' => $answered]); $mainUrl = api_get_path(WEB_CODE_PATH).'survey/survey.php?'; $content = ''; @@ -2797,7 +2774,7 @@ class SurveyManager continue; } - $url = $mainUrl.'survey_id='.$survey->getSurveyId().'&cid='.$courseId.'&sid='.$sessionId; + $url = $mainUrl.'survey_id='.$survey->getIid().'&cid='.$courseId.'&sid='.$sessionId; $title = $survey->getTitle(); $title = Display::url($title, $url); @@ -2806,7 +2783,7 @@ class SurveyManager $courseInfo['name'] .= ' ('.$sessionInfo['name'].')'; } - $surveyData = self::get_survey($survey->getSurveyId(), 0, $courseCode); + $surveyData = self::get_survey($survey->getIid(), 0, $courseCode); $table->setCellContents($row, 0, $title); $table->setCellContents($row, 1, $courseInfo['name']); @@ -2848,7 +2825,7 @@ class SurveyManager public static function sendToTutors($surveyId) { - $survey = Database::getManager()->getRepository('ChamiloCourseBundle:CSurvey')->find($surveyId); + $survey = Database::getManager()->getRepository(CSurvey::class)->find($surveyId); if (null === $survey) { return false; } diff --git a/public/main/survey/survey.php b/public/main/survey/survey.php index 4e00c8f1fc..7a4364a472 100644 --- a/public/main/survey/survey.php +++ b/public/main/survey/survey.php @@ -88,7 +88,7 @@ if ($is_survey_type_1 && ('addgroup' == $action || 'deletegroup' == $action)) { } if ('deletegroup' == $action) { - $sql = 'DELETE FROM '.$table_survey_question_group.' + $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'; @@ -273,32 +273,34 @@ 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 id"; + 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.question_option_id) as number_of_options +$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.question_id = survey_question_option.question_id AND + 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.question_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']) { + if ('pagebreak' === $row['type']) { $breakClass = ' highlight'; if (empty($questionsGroupClass)) { $questionsGroupClass = 'row_even'; @@ -339,27 +341,36 @@ while ($row = Database::fetch_array($result, 'ASSOC')) { echo ''.$row['number_of_options'].''; echo ''; if (3 != $survey_data['survey_type']) { - echo ''. - Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).''; + echo ''. + Display::return_icon('edit.png', get_lang('Edit')).''; } - echo ''. + echo ''. Display::return_icon('copy.png', get_lang('Copy'), '', ICON_SIZE_SMALL).''; - echo ''. - Display::return_icon('delete.png', get_lang('Delete'), '', 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'), '', ICON_SIZE_SMALL).''; + 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'), '', ICON_SIZE_SMALL).''; + echo ''. + Display::return_icon('down.png', get_lang('Move down')).''; } else { Display::display_icon('down_na.png', ' ', '', ICON_SIZE_SMALL); } @@ -397,24 +408,30 @@ if ($is_survey_type_1) { 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'; + 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 ''; + echo ''; } else { echo ''; echo ''; echo '
'; - echo ''; echo ' '; echo ' '; @@ -422,11 +439,11 @@ if ($is_survey_type_1) { echo ' '; echo ' '; - $sql = 'SELECT id,name,description + $sql = 'SELECT id,name,description FROM '.$table_survey_question_group.' - WHERE - c_id = '.$course_id.' AND - survey_id = '.intval($survey_id).' + WHERE + c_id = '.$course_id.' AND + survey_id = '.intval($survey_id).' ORDER BY name'; $rs = Database::query($sql); @@ -434,7 +451,9 @@ if ($is_survey_type_1) { $grouplist .= ''; } @@ -442,5 +461,4 @@ if ($is_survey_type_1) { } Session::erase('answer_count'); - Display::display_footer(); diff --git a/public/main/survey/surveyUtil.class.php b/public/main/survey/surveyUtil.class.php index 092e5ebc8f..c671fcc283 100644 --- a/public/main/survey/surveyUtil.class.php +++ b/public/main/survey/surveyUtil.class.php @@ -2,6 +2,7 @@ /* For licensing terms, see /license.txt */ +use Chamilo\CoreBundle\Framework\Container; use Chamilo\CourseBundle\Entity\CSurvey; use Chamilo\CourseBundle\Entity\CSurveyAnswer; use ChamiloSession as Session; @@ -143,9 +144,6 @@ class SurveyUtil $insertId = $answer->getIid(); if ($insertId) { - $sql = "UPDATE $table_survey_answer SET answer_id = $insertId - WHERE iid = $insertId"; - Database::query($sql); return true; } @@ -241,7 +239,7 @@ class SurveyUtil $action = isset($_GET['action']) ? $_GET['action'] : ''; // Getting the number of question - $questions = SurveyManager::get_questions($survey_data['survey_id']); + $questions = SurveyManager::get_questions($survey_data['iid']); $counter = 0; foreach ($questions as $key => $value) { @@ -327,7 +325,7 @@ class SurveyUtil */ public static function displayUserReportForm($survey_data, $people_filled) { - $surveyId = $survey_data['survey_id']; + $surveyId = $survey_data['iid']; if (empty($survey_data)) { return ''; @@ -381,7 +379,7 @@ class SurveyUtil $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']; + $surveyId = (int) $survey_data['iid']; $userId = Database::escape_string($userId); $content = ''; @@ -397,20 +395,20 @@ class SurveyUtil // Getting all the questions and options $sql = "SELECT - survey_question.question_id, + survey_question.iid question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.max_value, survey_question.sort, survey_question.type, - survey_question_option.question_option_id, + survey_question_option.iid question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option ON - survey_question.question_id = survey_question_option.question_id AND + survey_question.iid = survey_question_option.question_id AND survey_question_option.c_id = $course_id WHERE survey_question NOT LIKE '%{{%' AND @@ -496,7 +494,7 @@ class SurveyUtil return ''; } - $surveyId = $survey_data['survey_id']; + $surveyId = $survey_data['iid']; $reportingUrl = api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.$surveyId.'&'.api_get_cidreq(); // Actions bar @@ -574,7 +572,7 @@ class SurveyUtil // Determining the offset of the sql statement (the n-th question of the survey) $offset = !isset($_GET['question']) ? 0 : (int) $_GET['question']; $currentQuestion = isset($_GET['question']) ? (int) $_GET['question'] : 0; - $surveyId = (int) $survey_data['survey_id']; + $surveyId = (int) $survey_data['iid']; $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : ''; $course_id = api_get_course_int_id(); @@ -660,7 +658,7 @@ class SurveyUtil $limitStatement"; $result = Database::query($sql); while ($row = Database::fetch_array($result)) { - $questions[$row['question_id']] = $row; + $questions[$row['iid']] = $row; } } echo '
'; @@ -668,7 +666,7 @@ class SurveyUtil echo '
'; $chartData = []; $options = []; - $questionId = (int) $question['question_id']; + $questionId = (int) $question['iid']; echo '
'; echo strip_tags(isset($question['survey_question']) ? $question['survey_question'] : null); echo '
'; @@ -699,10 +697,10 @@ class SurveyUtil ORDER BY sort ASC"; $result = Database::query($sql); while ($row = Database::fetch_array($result, 'ASSOC')) { - $options[$row['question_option_id']] = $row; + $options[$row['iid']] = $row; } // Getting the answers - $sql = "SELECT *, count(answer_id) as total + $sql = "SELECT *, count(iid) as total FROM $table_survey_answer WHERE c_id = $course_id AND @@ -717,12 +715,10 @@ class SurveyUtil $number_of_answers[$row['question_id']] = 0; } $number_of_answers[$row['question_id']] += $row['total']; - if ('multiplechoiceother' === $question['type']) { $parts = ch_multiplechoiceother::decodeOptionValue($row['option_id']); $row['option_id'] = $parts[0]; } - $data[$row['option_id']] = $row; } @@ -730,12 +726,12 @@ class SurveyUtil $optionText = strip_tags($option['option_text']); $optionText = html_entity_decode($optionText); $votes = 0; - if (isset($data[$option['question_option_id']]['total'])) { - $votes = $data[$option['question_option_id']]['total']; + if (isset($data[$option['iid']]['total'])) { + $votes = $data[$option['iid']]['total']; } array_push($chartData, ['option' => $optionText, 'votes' => $votes]); } - $chartContainerId = 'chartContainer'.$question['question_id']; + $chartContainerId = 'chartContainer'.$question['iid']; echo '
'; echo self::drawChart($chartData, false, $chartContainerId, false); echo '
'; @@ -759,10 +755,10 @@ class SurveyUtil } $absolute_number = null; - if (isset($data[$value['question_option_id']])) { - $absolute_number = $data[$value['question_option_id']]['total']; + if (isset($data[$value['iid']])) { + $absolute_number = $data[$value['iid']]['total']; } - if ('percentage' == $question['type'] && empty($absolute_number)) { + if ('percentage' === $question['type'] && empty($absolute_number)) { continue; } $number_of_answers[$option['question_id']] = isset($number_of_answers[$option['question_id']]) @@ -869,7 +865,7 @@ class SurveyUtil $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 = $survey_data['survey_id']; + $surveyId = $survey_data['iid']; // Getting the options $sql = "SELECT * FROM $table_survey_question_option @@ -880,11 +876,11 @@ class SurveyUtil ORDER BY sort ASC"; $result = Database::query($sql); while ($row = Database::fetch_array($result)) { - $options[$row['question_option_id']] = $row; + $options[$row['iid']] = $row; } // Getting the answers - $sql = "SELECT *, count(answer_id) as total + $sql = "SELECT *, count(iid) as total FROM $table_survey_answer WHERE c_id = $course_id AND @@ -992,7 +988,7 @@ class SurveyUtil $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); - $surveyId = (int) $survey_data['survey_id']; + $surveyId = (int) $survey_data['iid']; $course_id = (int) $survey_data['c_id']; if (empty($surveyId) || empty($course_id)) { @@ -1097,18 +1093,18 @@ class SurveyUtil } $sql = "SELECT - q.question_id, + q.iid question_id, q.type, q.survey_question, - count(o.question_option_id) as number_of_options + count(o.iid) as number_of_options FROM $table_survey_question q LEFT JOIN $table_survey_question_option o - ON q.question_id = o.question_id AND q.c_id = o.c_id + ON q.iid = o.question_id AND q.c_id = o.c_id WHERE survey_question NOT LIKE '%{{%' AND q.survey_id = '".$surveyId."' AND q.c_id = $course_id - GROUP BY q.question_id + GROUP BY q.iid ORDER BY q.sort ASC"; $result = Database::query($sql); $questions = []; @@ -1160,18 +1156,18 @@ class SurveyUtil // cells with option (none for open question) $sql = "SELECT - sq.question_id, + sq.iid question_id, sq.survey_id, sq.survey_question, sq.display, sq.sort, sq.type, - sqo.question_option_id, + sqo.iid question_option_id, sqo.option_text, sqo.sort as option_sort FROM $table_survey_question sq LEFT JOIN $table_survey_question_option sqo - ON sq.question_id = sqo.question_id AND sq.c_id = sqo.c_id + ON sq.iid = sqo.question_id AND sq.c_id = sqo.c_id WHERE survey_question NOT LIKE '%{{%' AND sq.survey_id = '".$surveyId."' AND @@ -1226,7 +1222,7 @@ class SurveyUtil c_id = $course_id AND survey_id = $surveyId $userCondition - ORDER BY answer_id, user ASC"; + ORDER BY iid, user ASC"; $result = Database::query($sql); $i = 1; while ($row = Database::fetch_array($result)) { @@ -1300,7 +1296,7 @@ class SurveyUtil $display_extra_user_fields = false ) { $user = Security::remove_XSS($user); - $surveyId = (int) $survey_data['survey_id']; + $surveyId = (int) $survey_data['iid']; if (empty($surveyId)) { return ''; @@ -1447,11 +1443,11 @@ class SurveyUtil questions.question_id, questions.type, questions.survey_question, - count(options.question_option_id) as number_of_options + count(options.iid) as number_of_options FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options ON - questions.question_id = options.question_id AND + questions.iid = options.question_id AND options.c_id = questions.c_id WHERE survey_question NOT LIKE '%{{%' AND @@ -1514,19 +1510,19 @@ class SurveyUtil } $sql = "SELECT DISTINCT - survey_question.question_id, + survey_question.iid question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, - survey_question_option.question_option_id, + survey_question_option.iid question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option ON - survey_question.question_id = survey_question_option.question_id AND + survey_question.iid = survey_question_option.question_id AND survey_question_option.c_id = survey_question.c_id WHERE survey_question NOT LIKE '%{{%' AND @@ -1756,7 +1752,7 @@ class SurveyUtil { $course_id = api_get_course_int_id(); $user_id = (int) $user_id; - $surveyId = $survey_data['survey_id']; + $surveyId = $survey_data['iid']; if (empty($course_id) || empty($surveyId)) { return false; @@ -1790,11 +1786,11 @@ class SurveyUtil questions.question_id, questions.type, questions.survey_question, - count(options.question_option_id) as number_of_options + count(options.iid) as number_of_options FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options ON - questions.question_id = options.question_id AND + questions.iid = options.question_id AND options.c_id = questions.c_id WHERE survey_question NOT LIKE '%{{%' AND @@ -1848,19 +1844,19 @@ class SurveyUtil // Getting all the questions and options (second line) $sql = "SELECT - survey_question.question_id, + survey_question.iid question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, - survey_question_option.question_option_id, + survey_question_option.iid question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option ON - survey_question.question_id = survey_question_option.question_id AND + survey_question.iid = survey_question_option.question_id AND survey_question_option.c_id = survey_question.c_id WHERE survey_question NOT LIKE '%{{%' AND @@ -3052,8 +3048,9 @@ class SurveyUtil */ public static function modify_filter($survey_id, $drh = false) { + $repo = Container::getSurveyRepository(); /** @var CSurvey $survey */ - $survey = Database::getManager()->find('ChamiloCourseBundle:CSurvey', $survey_id); + $survey = $repo->find($survey_id); $hideSurveyEdition = self::checkHideEditionToolsByCode($survey->getCode()); if ($hideSurveyEdition) { @@ -3064,7 +3061,7 @@ class SurveyUtil return ''; } - $survey_id = $survey->getSurveyId(); + $survey_id = $survey->getIid(); $actions = []; $hideReportingButton = api_get_configuration_value('hide_survey_reporting_button'); $codePath = api_get_path(WEB_CODE_PATH); @@ -3315,7 +3312,7 @@ class SurveyUtil } else { $search_restriction = "WHERE c_id = $course_id"; } - $sql = "SELECT count(survey_id) AS total_number_of_items + $sql = "SELECT count(iid) AS total_number_of_items FROM $table_survey $search_restriction"; $res = Database::query($sql); $obj = Database::fetch_object($res); @@ -3382,10 +3379,10 @@ class SurveyUtil $sql = " SELECT - survey.survey_id AS col0, + survey.iid AS col0, survey.title AS col1, survey.code AS col2, - count(survey_question.question_id) AS col3, " + count(survey_question.iid) AS col3, " .(api_is_western_name_order() ? "CONCAT(user.firstname, ' ', user.lastname)" : "CONCAT(user.lastname, ' ', user.firstname)") @@ -3401,13 +3398,13 @@ class SurveyUtil survey.survey_type FROM $table_survey survey LEFT JOIN $table_survey_question survey_question - ON (survey.survey_id = survey_question.survey_id AND survey_question.c_id = $course_id) + ON (survey.iid = survey_question.survey_id AND survey_question.c_id = $course_id) LEFT JOIN $table_user user ON (survey.author = user.id) WHERE survey.c_id = $course_id $search_restriction $condition_session - GROUP BY survey.survey_id + GROUP BY survey.iid ORDER BY col$column $direction LIMIT $from,$number_of_items "; @@ -3515,7 +3512,7 @@ class SurveyUtil $list[] = $survey['id']; } if (count($list) > 0) { - $list_condition = " AND survey.survey_id IN (".implode(',', $list).") "; + $list_condition = " AND survey.iid IN (".implode(',', $list).") "; } else { $list_condition = ''; } @@ -3535,10 +3532,10 @@ class SurveyUtil $sql = " SELECT - survey.survey_id AS col0, + survey.iid AS col0, survey.title AS col1, survey.code AS col2, - count(survey_question.question_id) AS col3, + count(survey_question.iid) AS col3, " .(api_is_western_name_order() ? "CONCAT(user.firstname, ' ', user.lastname)" @@ -3546,16 +3543,16 @@ class SurveyUtil ." AS col4, survey.avail_from AS col5, survey.avail_till AS col6, - CONCAT('',survey.answered,' / ',survey.invited, '') AS col7, + CONCAT('',survey.answered,' / ',survey.invited, '') AS col7, survey.anonymous AS col8, - survey.survey_id AS col9 + survey.iid AS col9 FROM $table_survey survey LEFT JOIN $table_survey_question survey_question - ON (survey.survey_id = survey_question.survey_id AND survey.c_id = survey_question.c_id), + ON (survey.iid = survey_question.survey_id AND survey.c_id = survey_question.c_id), $table_user user WHERE survey.author = user.id AND survey.c_id = $course_id $list_condition "; - $sql .= ' GROUP BY survey.survey_id'; + $sql .= ' GROUP BY survey.iid'; $sql .= " ORDER BY col$column $direction "; $sql .= " LIMIT $from,$number_of_items"; @@ -3628,7 +3625,7 @@ class SurveyUtil survey_invitation.session_id, survey.title, survey.visible_results, - survey.survey_id, + survey.iid, survey.anonymous FROM $table_survey survey INNER JOIN @@ -3647,13 +3644,11 @@ class SurveyUtil survey_invitation.c_id = $course_id "; $result = Database::query($sql); - $efv = new ExtraFieldValue('survey'); - $surveyIds = []; - while ($row = Database::fetch_array($result, 'ASSOC')) { - if (in_array($row['survey_id'], $surveyIds)) { + $surveyId = $row['iid']; + if (in_array($surveyId, $surveyIds)) { continue; } @@ -3691,14 +3686,14 @@ class SurveyUtil $icon.PHP_EOL.$row['title'], api_get_path(WEB_CODE_PATH).'survey/reporting.php?'.api_get_cidreq().'&'.http_build_query([ 'action' => 'questionreport', - 'survey_id' => $row['survey_id'], + 'survey_id' => $surveyId, ]) ) : $icon.PHP_EOL.$row['title']; echo ''; } echo '
'; if ($mandatoryAllowed) { $efvMandatory = $efv->get_values_by_handler_and_field_variable( @@ -3708,8 +3703,7 @@ class SurveyUtil echo ''; } echo ''; - - $surveyIds[] = $row['survey_id']; + $surveyIds[] = $surveyId; } echo ''; echo '
'.get_lang('Name').''.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).''. '
'; - echo (1 == $row['anonymous']) ? get_lang('Yes') : get_lang('No'); + echo 1 == $row['anonymous'] ? get_lang('Yes') : get_lang('No'); echo ''.($efvMandatory['value'] ? get_lang('Yes') : get_lang('No')).'
'; @@ -4022,7 +4016,7 @@ class SurveyUtil WHERE c_id = $courseId AND survey_id = '".$surveyId."' - ORDER BY answer_id, user ASC"; + ORDER BY iid, user ASC"; $result = Database::query($sql); $response = Database::affected_rows($result); diff --git a/public/main/survey/survey_invite.php b/public/main/survey/survey_invite.php index 35f1366fd5..edd41792f2 100644 --- a/public/main/survey/survey_invite.php +++ b/public/main/survey/survey_invite.php @@ -11,6 +11,9 @@ * @todo check if the mailtext contains the **link** part, if not, add the link to the end * @todo add rules: title and text cannot be empty */ + +use Chamilo\CoreBundle\Framework\Container; + require_once __DIR__.'/../inc/global.inc.php'; $this_section = SECTION_COURSES; @@ -180,7 +183,8 @@ if ($form->validate()) { } } - $survey = Database::getManager()->getRepository('ChamiloCourseBundle:CSurvey')->find($survey_data['survey_id']); + $repo = Container::getSurveyRepository(); + $survey = $repo->find($survey_data['survey_id']); // Save the invitation mail SurveyUtil::saveInviteMail( diff --git a/public/main/survey/survey_list.php b/public/main/survey/survey_list.php index 407cdb7c25..2de2c6fc3d 100644 --- a/public/main/survey/survey_list.php +++ b/public/main/survey/survey_list.php @@ -136,20 +136,20 @@ if (isset($_POST['action']) && $_POST['action'] && isset($_POST['id']) && is_arr } $sql = "SELECT - survey_question.question_id, + survey_question.iid question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.max_value, survey_question.sort, survey_question.type, - survey_question_option.question_option_id, + survey_question_option.iid question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option ON - survey_question.question_id = survey_question_option.question_id AND + survey_question.iid = survey_question_option.question_id AND survey_question_option.c_id = $course_id WHERE survey_question NOT LIKE '%{{%' AND