diff --git a/main/inc/lib/SurveyTree .php b/main/inc/lib/SurveyTree .php new file mode 100644 index 0000000000..a7b6f206ca --- /dev/null +++ b/main/inc/lib/SurveyTree .php @@ -0,0 +1,127 @@ +surveylist = $list; + $this->plainsurveylist = $plain_array; + } + + /** + * This function gets the parent id of a survey. + * + * @param int $id survey id + * + * @return int survey parent id + * + * @author Julio Montoya , Dokeos + * + * @version September 2008 + */ + public function getParentId($id) + { + $node = $this->plainsurveylist[$id]; + if (is_array($node) && !empty($node['parent_id'])) { + return $node['parent_id']; + } else { + return -1; + } + } + + /** + * This function creates a list of all surveys id. + * + * @param array $list of nodes + * + * @return array with the structure survey_id => survey_name + * + * @author Julio Montoya + * + * @version September 2008 + */ + public function createList($list) + { + $result = []; + if (is_array($list)) { + foreach ($list as $key => $node) { + if (isset($node['children']) && is_array($node['children'])) { + $result[$key] = $node['name']; + $re = self::createList($node['children']); + if (!empty($re)) { + if (is_array($re)) { + foreach ($re as $key => $r) { + $result[$key] = ''.$r; + } + } else { + $result[] = $re; + } + } + } else { + $result[$key] = $node['name']; + } + } + } + + return $result; + } +} diff --git a/main/inc/lib/surveymanager.lib.php b/main/inc/lib/surveymanager.lib.php index a7b6f206ca..b3d9bbc7f3 100755 --- a/main/inc/lib/surveymanager.lib.php +++ b/main/inc/lib/surveymanager.lib.php @@ -1,127 +1 @@ surveylist = $list; - $this->plainsurveylist = $plain_array; - } - - /** - * This function gets the parent id of a survey. - * - * @param int $id survey id - * - * @return int survey parent id - * - * @author Julio Montoya , Dokeos - * - * @version September 2008 - */ - public function getParentId($id) - { - $node = $this->plainsurveylist[$id]; - if (is_array($node) && !empty($node['parent_id'])) { - return $node['parent_id']; - } else { - return -1; - } - } - - /** - * This function creates a list of all surveys id. - * - * @param array $list of nodes - * - * @return array with the structure survey_id => survey_name - * - * @author Julio Montoya - * - * @version September 2008 - */ - public function createList($list) - { - $result = []; - if (is_array($list)) { - foreach ($list as $key => $node) { - if (isset($node['children']) && is_array($node['children'])) { - $result[$key] = $node['name']; - $re = self::createList($node['children']); - if (!empty($re)) { - if (is_array($re)) { - foreach ($re as $key => $r) { - $result[$key] = ''.$r; - } - } else { - $result[] = $re; - } - } - } else { - $result[$key] = $node['name']; - } - } - } - - return $result; - } -} diff --git a/main/survey/survey.lib.php b/main/survey/survey.lib.php index 17678ec00a..b45b54f2c1 100755 --- a/main/survey/survey.lib.php +++ b/main/survey/survey.lib.php @@ -1,7 +1,10 @@ setShowFormProfile($values['show_form_profile']); if ($values['show_form_profile'] == 1) { // Input_name_list @@ -274,18 +280,19 @@ class SurveyManager } else { $extraParams['form_fields'] = ''; } + $survey->setFormFields($extraParams['form_fields']); } else { - // Input_name_list - $extraParams['show_form_profile'] = 0; - $extraParams['form_fields'] = ''; + $survey->setShowFormProfile(0); + $survey->setFormFields(0); } if ($values['survey_type'] == 1) { - $extraParams['survey_type'] = 1; - $extraParams['shuffle'] = $values['shuffle']; - $extraParams['one_question_per_page'] = $values['one_question_per_page']; - $extraParams['parent_id'] = $values['parent_id']; - + $survey + ->setSurveyType(1) + ->setShuffle($values['shuffle']) + ->setOneQuestionPerPage($values['one_question_per_page']) + ->setParentId($values['parent_id']) + ; // Logic for versioning surveys if (!empty($values['parent_id'])) { $versionValue = ''; @@ -328,35 +335,41 @@ class SurveyManager $versionValue = $insertnewversion; } } - $extraParams['survey_version'] = $versionValue; + $survey->setSurveyVersion($versionValue); } } - $params = [ - 'c_id' => $course_id, - 'code' => self::generateSurveyCode($values['survey_code']), - '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'], - 'creation_date' => api_get_utc_datetime(), - 'anonymous' => $values['anonymous'], - 'session_id' => api_get_session_id(), - 'visible_results' => $values['visible_results'], - ]; - $params = array_merge($params, $extraParams); - $survey_id = Database::insert($table_survey, $params); + $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); + } + + $survey + ->setCId($course_id) + ->setCode(self::generateSurveyCode($values['survey_code'])) + ->setTitle($values['survey_title']) + ->setSubtitle($values['survey_title']) + ->setAuthor($_user['user_id']) + ->setLang($values['survey_language']) + ->setAvailFrom($from) + ->setAvailTill($until) + ->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']) + ; + + $em = Database::getManager(); + $em->persist($survey); + $em->flush(); + + $survey_id = $survey->getIid(); if ($survey_id > 0) { $sql = "UPDATE $table_survey SET survey_id = $survey_id WHERE iid = $survey_id"; @@ -710,8 +723,7 @@ class SurveyManager $params['session_id'] = api_get_session_id(); $params['title'] = $params['title'].' '.get_lang('Copy'); unset($params['iid']); - Database::insert($table_survey, $params); - $new_survey_id = Database::insert_id(); + $new_survey_id = Database::insert($table_survey, $params); if ($new_survey_id) { $sql = "UPDATE $table_survey SET survey_id = $new_survey_id @@ -772,7 +784,6 @@ class SurveyManager $insertId = Database::insert($table_survey_question, $params); $sql = "UPDATE $table_survey_question SET question_id = iid WHERE iid = $insertId"; Database::query($sql); - $question_id[$row['question_id']] = $insertId; } @@ -803,7 +814,7 @@ class SurveyManager /** * This function duplicates a survey (and also all the question in that survey. * - * @param int $survey_id id of the survey that has to be duplicated + * @param int $surveyId id of the survey that has to be duplicated * @param int $courseId id of the course which survey has to be duplicated * * @return true @@ -812,7 +823,7 @@ class SurveyManager * * @version October 2007 */ - public static function empty_survey($survey_id, $courseId = 0) + public static function empty_survey($surveyId, $courseId = 0) { // Database table definitions $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION); @@ -821,8 +832,9 @@ class SurveyManager $courseId = (int) $courseId; $courseId = empty($courseId) ? api_get_course_int_id() : $courseId; + $surveyId = (int) $surveyId; - $datas = self::get_survey($survey_id); + $datas = self::get_survey($surveyId); $session_where = ''; if (api_get_session_id() != 0) { $session_where = ' AND session_id = "'.api_get_session_id().'" '; @@ -835,11 +847,11 @@ class SurveyManager Database::query($sql); $sql = 'DELETE FROM '.$table_survey_answer.' - WHERE c_id = '.$courseId.' AND survey_id='.intval($survey_id); + WHERE c_id = '.$courseId.' AND survey_id='.$surveyId; Database::query($sql); $sql = 'UPDATE '.$table_survey.' SET invited=0, answered=0 - WHERE c_id = '.$courseId.' AND survey_id='.intval($survey_id); + WHERE c_id = '.$courseId.' AND survey_id='.$surveyId; Database::query($sql); return true; @@ -1032,8 +1044,8 @@ class SurveyManager /** * This function gets all the question of any given survey. * - * @param int $survey_id the id of the survey - * @param int $course_id + * @param int $surveyId the id of the survey + * @param int $courseId * * @return array containing all the questions of the survey * @@ -1043,20 +1055,21 @@ class SurveyManager * * @todo one sql call should do the trick */ - public static function get_questions($survey_id, $course_id = 0) + public static function get_questions($surveyId, $courseId = 0) { - // 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 = (int) $course_id; - if (empty($course_id)) { - $course_id = api_get_course_int_id(); + $courseId = (int) $courseId; + $surveyId = (int) $surveyId; + + if (empty($courseId)) { + $courseId = api_get_course_int_id(); } // Getting the information of the question $sql = "SELECT * FROM $tbl_survey_question - WHERE c_id = $course_id AND survey_id='".intval($survey_id)."'"; + WHERE c_id = $courseId AND survey_id='".$surveyId."'"; $result = Database::query($sql); $return = []; while ($row = Database::fetch_array($result, 'ASSOC')) { @@ -1067,11 +1080,12 @@ class SurveyManager $return[$row['question_id']]['horizontalvertical'] = $row['display']; $return[$row['question_id']]['maximum_score'] = $row['max_value']; $return[$row['question_id']]['sort'] = $row['sort']; + $return[$row['question_id']]['survey_question_comment'] = $row['survey_question_comment']; } // Getting the information of the question options $sql = "SELECT * FROM $table_survey_question_option - WHERE c_id = $course_id AND survey_id='".intval($survey_id)."'"; + WHERE c_id = $courseId AND survey_id='".$surveyId."'"; $result = Database::query($sql); while ($row = Database::fetch_array($result, 'ASSOC')) { $return[$row['question_id']]['answers'][] = $row['option_text']; @@ -1133,7 +1147,6 @@ class SurveyManager } } $course_id = api_get_course_int_id(); - if (!$empty_answer) { // Table definitions $tbl_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); @@ -1157,41 +1170,40 @@ class SurveyManager $row = Database::fetch_array($result, 'ASSOC'); $max_sort = $row['max_sort']; + $question = new CSurveyQuestion(); + // Some variables defined for survey-test type $extraParams = []; if (isset($_POST['choose'])) { if ($_POST['choose'] == 1) { - $extraParams['survey_group_pri'] = $_POST['assigned']; + $question->setSurveyGroupPri($_POST['assigned']); } elseif ($_POST['choose'] == 2) { - $extraParams['survey_group_sec1'] = $_POST['assigned1']; - $extraParams['survey_group_sec2'] = $_POST['assigned2']; + $question->setSurveyGroupSec1($_POST['assigned1']); + $question->setSurveyGroupSec2($_POST['assigned2']); } } - $questionComment = isset($form_content['question_comment']) - ? $form_content['question_comment'] - : ''; - $maxScore = isset($form_content['maximum_score']) ? $form_content['maximum_score'] : ''; - $display = isset($form_content['horizontalvertical']) ? $form_content['horizontalvertical'] : ''; - - $params = [ - 'c_id' => $course_id, - 'survey_id' => $form_content['survey_id'], - 'survey_question' => $form_content['question'], - 'survey_question_comment' => $questionComment, - 'type' => $form_content['type'], - 'display' => $display, - 'sort' => $max_sort + 1, - 'shared_question_id' => $form_content['shared_question_id'], - 'max_value' => $maxScore, - ]; + $question + ->setSurveyQuestionComment($form_content['question_comment'] ?? '') + ->setMaxValue($form_content['maximum_score'] ?? 0) + ->setDisplay($form_content['horizontalvertical'] ?? '') + ->setCId($course_id) + ->setSurveyId($form_content['survey_id']) + ->setSurveyQuestion($form_content['question']) + ->setType($form_content['type']) + ->setSort($max_sort + 1) + ->setSharedQuestionId($form_content['shared_question_id']) + ; if (api_get_configuration_value('allow_required_survey_questions')) { - $params['is_required'] = isset($form_content['is_required']); + $question->setIsMandatory(isset($form_content['is_required'])); } - $params = array_merge($params, $extraParams); - $question_id = Database::insert($tbl_survey_question, $params); + $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 WHERE iid = $question_id"; @@ -1510,7 +1522,7 @@ class SurveyManager { $course_id = api_get_course_int_id(); // A percentage question type has options 1 -> 100 - if ($form_content['type'] == 'percentage') { + if ($form_content['type'] === 'percentage') { for ($i = 1; $i < 101; $i++) { $form_content['answers'][] = $i; } @@ -1531,19 +1543,24 @@ class SurveyManager } $counter = 1; + $em = Database::getManager(); if (isset($form_content['answers']) && is_array($form_content['answers'])) { for ($i = 0; $i < count($form_content['answers']); $i++) { - $values = isset($form_content['values']) ? $form_content['values'][$i] : ''; - - $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_survey_question_option, $params); + $values = isset($form_content['values']) ? $form_content['values'][$i] : 0; + $option = new CSurveyQuestionOption(); + $option + ->setCId($course_id) + ->setQuestionId($form_content['question_id']) + ->setSurveyId($form_content['survey_id']) + ->setOptionText($form_content['answers'][$i]) + ->setValue($values) + ->setSort($counter) + ; + + $em->persist($option); + $em->flush(); + + $insertId = $option->getIid(); if ($insertId) { $sql = "UPDATE $table_survey_question_option SET question_option_id = $insertId @@ -1648,14 +1665,14 @@ class SurveyManager $course_condition = " c_id = $course_id AND "; // Table definitions - $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); + $table = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); if ($shared) { $course_condition = ''; - $table_survey_question_option = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); + $table = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); } // Deleting the options of the survey questions - $sql = "DELETE from $table_survey_question_option + $sql = "DELETE FROM $table WHERE $course_condition survey_id='".intval($survey_id)."' AND question_id='".intval($question_id)."'"; @@ -1887,13 +1904,15 @@ class SurveyManager WITH (s.code = i.surveyCode AND s.cId = i.cId AND s.sessionId = i.sessionId) INNER JOIN ChamiloCoreBundle:ExtraFieldValues efv WITH efv.itemId = s.iid INNER JOIN ChamiloCoreBundle:ExtraField ef WITH efv.field = ef.id - WHERE i.answered = 0 - AND i.cId = :course - AND i.user = :user - AND i.sessionId = :session - AND :now BETWEEN s.availFrom AND s.availTill - AND ef.variable = :variable - AND efv.value = 1 + WHERE + i.answered = 0 AND + i.cId = :course AND + i.user = :user AND + i.sessionId = :session AND + :now BETWEEN s.availFrom AND s.availTill AND + ef.variable = :variable AND + efv.value = 1 AND + s.surveyType != 3 ORDER BY s.availTill ASC ") ->setMaxResults(1) @@ -2084,4 +2103,125 @@ class SurveyManager return false; } + + public static function removeMultiplicateQuestions($surveyData) + { + if (empty($surveyData)) { + return false; + } + $surveyId = $surveyData['survey_id']; + $courseId = $surveyData['c_id']; + + if (empty($surveyId) || empty($courseId)) { + return false; + } + + $questions = self::get_questions($surveyId); + foreach ($questions as $question) { + // Questions marked with "geneated" were created using the "multiplicate" feature. + if ($question['survey_question_comment'] === 'generated') { + self::delete_survey_question($surveyId, $question['question_id']); + } + } + } + + /** + * @param array $surveyData + * + * @return bool + */ + public static function multiplicateQuestions($surveyData) + { + if (empty($surveyData)) { + return false; + } + $surveyId = $surveyData['survey_id']; + $courseId = $surveyData['c_id']; + + if (empty($surveyId) || empty($courseId)) { + return false; + } + + $questions = self::get_questions($surveyId); + + $obj = new UserGroup(); + + $options['where'] = [' usergroup.course_id = ? ' => $courseId]; + $classList = $obj->getUserGroupInCourse($options); + + $classTag = '{{class_name}}'; + $studentTag = '{{student_full_name}}'; + $classCounter = 0; + foreach ($classList as $class) { + $className = $class['name']; + foreach ($questions as $question) { + $users = $obj->get_users_by_usergroup($class['id']); + if (empty($users)) { + continue; + } + + $text = $question['question']; + if (strpos($text, $classTag) !== false) { + $replacedText = str_replace($classTag, $className, $text); + $values = [ + 'c_id' => $courseId, + 'question_comment' => 'generated', + 'type' => $question['type'], + 'display' => $question['horizontalvertical'], + 'question' => $replacedText, + 'survey_id' => $surveyId, + 'question_id' => 0, + 'shared_question_id' => 0, + ]; + self::save_question($surveyData, $values); + $classCounter++; + continue; + } + + foreach ($users as $userId) { + $userInfo = api_get_user_info($userId); + + if (strpos($text, $studentTag) !== false) { + $replacedText = str_replace($studentTag, $userInfo['complete_name'], $text); + $values = [ + 'c_id' => $courseId, + 'question_comment' => 'generated', + 'type' => $question['type'], + 'display' => $question['horizontalvertical'], + 'maximum_score' => $question['maximum_score'], + 'question' => $replacedText, + 'survey_id' => $surveyId, + 'question_id' => 0, + 'shared_question_id' => 0, + ]; + + $answers = []; + if (!empty($question['answers'])) { + foreach ($question['answers'] as $answer) { + $replacedText = str_replace($studentTag, $userInfo['complete_name'], $answer); + $answers[] = $replacedText; + } + } + $values['answers'] = $answers; + self::save_question($surveyData, $values); + } + } + + if ($classCounter < count($classList)) { + // Add end page + $values = [ + 'c_id' => $courseId, + 'question_comment' => 'generated', + 'type' => 'pagebreak', + 'display' => 'horizontal', + 'question' => get_lang('QuestionForNextClass'), + 'survey_id' => $surveyId, + 'question_id' => 0, + 'shared_question_id' => 0, + ]; + self::save_question($surveyData, $values); + } + } + } + } } diff --git a/main/survey/survey.php b/main/survey/survey.php index b06c79743d..93a6158f57 100755 --- a/main/survey/survey.php +++ b/main/survey/survey.php @@ -44,7 +44,7 @@ $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_ $table_course = Database::get_main_table(TABLE_MAIN_COURSE); $table_user = Database::get_main_table(TABLE_MAIN_USER); -$survey_id = intval($_GET['survey_id']); +$survey_id = (int) $_GET['survey_id']; $course_id = api_get_course_int_id(); $action = isset($_GET['action']) ? $_GET['action'] : null; @@ -89,7 +89,9 @@ if ($is_survey_type_1 && ($action == 'addgroup' || $action == 'deletegroup')) { } if ($action == 'deletegroup') { - Database::query('DELETE FROM '.$table_survey_question_group.' WHERE c_id = '.$course_id.' AND id = '.intval($_GET['gid']).' and survey_id = '.intval($survey_id)); + $sql = 'DELETE FROM '.$table_survey_question_group.' + WHERE c_id = '.$course_id.' AND id = '.intval($_GET['gid']).' AND survey_id = '.intval($survey_id); + Database::query($sql); $sendmsg = 'GroupDeletedSuccessfully'; } header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&sendmsg='.$sendmsg); @@ -97,7 +99,6 @@ if ($is_survey_type_1 && ($action == 'addgroup' || $action == 'deletegroup')) { } // Displaying the header - Display::display_header($tool_name, 'Survey'); // Action handling @@ -132,26 +133,36 @@ if (!empty($survey_data['survey_version'])) { SurveyUtil::check_first_last_question($_GET['survey_id']); // Action links -$survey_actions = ''. - Display::return_icon('edit.png', get_lang('EditSurvey'), '', ICON_SIZE_MEDIUM).''; +$survey_actions = ''; +if ($survey_data['survey_type'] != 3) { + $survey_actions = ''. + Display::return_icon('edit.png', get_lang('EditSurvey'), '', ICON_SIZE_MEDIUM).''; +} $survey_actions .= ''. Display::return_icon('delete.png', get_lang('DeleteSurvey'), '', ICON_SIZE_MEDIUM).''; -$survey_actions .= ''. - Display::return_icon('preview_view.png', get_lang('Preview'), '', ICON_SIZE_MEDIUM).''; + +if ($survey_data['survey_type'] != 3) { + $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 (!api_get_configuration_value('hide_survey_reporting_button')) { - $survey_actions .= Display::url( - Display::return_icon('stats.png', get_lang('Reporting'), [], ICON_SIZE_MEDIUM), - api_get_path(WEB_CODE_PATH).'survey/reporting.php?'.api_get_cidreq().'&survey_id='.$survey_id - ); +if ($survey_data['survey_type'] != 3) { + if (!api_get_configuration_value('hide_survey_reporting_button')) { + $survey_actions .= Display::url( + Display::return_icon('stats.png', get_lang('Reporting'), [], ICON_SIZE_MEDIUM), + api_get_path(WEB_CODE_PATH).'survey/reporting.php?'.api_get_cidreq().'&survey_id='.$survey_id + ); + } } echo '
'.$survey_actions.'
'; +$urlQuestion = api_get_path(WEB_CODE_PATH).'survey/question.php?'.api_get_cidreq().'&action=add'; if ($survey_data['survey_type'] == 0) { - $urlQuestion = api_get_path(WEB_CODE_PATH).'survey/question.php?'.api_get_cidreq().'&action=add'; echo '
'; echo Display::url( Display::return_icon('yesno.png', get_lang('YesNo'), null, ICON_SIZE_BIG), @@ -191,12 +202,14 @@ if ($survey_data['survey_type'] == 0) { ); echo '
'; } else { - echo '
'; - echo Display::url( - Display::return_icon('yesno.png', get_lang('YesNo'), null, ICON_SIZE_BIG), - $urlQuestion.'&type=personality&survey_id='.$survey_id - ); - echo '
'; + if ($survey_data['survey_type'] != 3) { + echo '
'; + echo Display::url( + Display::return_icon('yesno.png', get_lang('YesNo'), null, ICON_SIZE_BIG), + $urlQuestion.'&type=personality&survey_id='.$survey_id + ); + echo '
'; + } } // Displaying the table header with all the questions @@ -218,7 +231,7 @@ 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 = ".intval($survey_id)." + WHERE c_id = $course_id AND survey_id = $survey_id ORDER BY id"; $result = Database::query($sql); $groups = []; @@ -243,10 +256,16 @@ while ($row = Database::fetch_array($result, 'ASSOC')) { echo ''; echo ' '.$question_counter.''; echo ' '; - if (api_strlen($row['survey_question']) > 100) { - echo api_substr(strip_tags($row['survey_question']), 0, 100).' ... '; + + if ($survey_data['survey_type'] != 3) { + if (api_strlen($row['survey_question']) > 100) { + echo api_substr(strip_tags($row['survey_question']), 0, 100).' ... '; + } else { + echo $row['survey_question']; + } } else { - echo $row['survey_question']; + $parts = explode('@@', $row['survey_question']); + echo api_get_local_time($parts[0]).' - '.api_get_local_time($parts[1]); } if ($row['type'] == 'yesno') { @@ -261,21 +280,28 @@ while ($row = Database::fetch_array($result, 'ASSOC')) { echo ''.$tool_name.''; echo ''.$row['number_of_options'].''; echo ''; - echo ''. - Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).''; + if ($survey_data['survey_type'] != 3) { + echo ''. + Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).''; + } + echo ''. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).''; - if ($question_counter > 1) { - echo ''. - Display::return_icon('up.png', get_lang('MoveUp'), '', ICON_SIZE_SMALL).''; - } else { - Display::display_icon('up_na.png', ' ', '', ICON_SIZE_SMALL); - } - if ($question_counter < $question_counter_max) { - echo ''. - Display::return_icon('down.png', get_lang('MoveDown'), '', ICON_SIZE_SMALL).''; - } else { - Display::display_icon('down_na.png', ' ', '', ICON_SIZE_SMALL); + if ($survey_data['survey_type'] != 3) { + if ($question_counter > 1) { + echo ''. + Display::return_icon('up.png', get_lang('MoveUp'), '', ICON_SIZE_SMALL).''; + } else { + Display::display_icon('up_na.png', ' ', '', ICON_SIZE_SMALL); + } + if ($question_counter < $question_counter_max) { + echo ''. + Display::return_icon('down.png', get_lang('MoveDown'), '', ICON_SIZE_SMALL).''; + } else { + Display::display_icon('down_na.png', ' ', '', ICON_SIZE_SMALL); + } } echo ' '; $question_counter++; @@ -345,11 +371,11 @@ if ($is_survey_type_1) { $rs = Database::query($sql); while ($row = Database::fetch_array($rs, 'ASSOC')) { $grouplist .= ''.$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).''. - ''; + ''. + Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).' '. + ''. + Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).''. + ''; } echo $grouplist.''; } diff --git a/main/survey/surveyUtil.class.php b/main/survey/surveyUtil.class.php index 457fd6d6ec..ad06c5c48f 100755 --- a/main/survey/surveyUtil.class.php +++ b/main/survey/surveyUtil.class.php @@ -2,6 +2,7 @@ /* For licensing terms, see /license.txt */ use Chamilo\CourseBundle\Entity\CSurvey; +use Chamilo\CourseBundle\Entity\CSurveyAnswer; use ChamiloSession as Session; /** @@ -106,6 +107,7 @@ class SurveyUtil if (empty($question_id)) { return false; } + // Table definition $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); @@ -120,22 +122,26 @@ class SurveyUtil } } - $course_id = $survey_data['c_id']; + $answer = new CSurveyAnswer(); + $answer + ->setCId($survey_data['c_id']) + ->setUser($user) + ->setSurveyId($survey_id) + ->setQuestionId($question_id) + ->setOptionId($option_id) + ->setValue($option_value) + ; - $sql = "INSERT INTO $table_survey_answer (c_id, user, survey_id, question_id, option_id, value) VALUES ( - $course_id, - '".Database::escape_string($user)."', - '".Database::escape_string($survey_id)."', - '".Database::escape_string($question_id)."', - '".Database::escape_string($option_id)."', - '".Database::escape_string($option_value)."' - )"; - Database::query($sql); - $insertId = Database::insert_id(); + $em = Database::getManager(); + $em->persist($answer); + $em->flush(); - $sql = "UPDATE $table_survey_answer SET answer_id = $insertId + $insertId = $answer->getIid(); + if ($insertId) { + $sql = "UPDATE $table_survey_answer SET answer_id = $insertId WHERE iid = $insertId"; - Database::query($sql); + Database::query($sql); + } return true; } @@ -480,16 +486,18 @@ class SurveyUtil } $ch_type = 'ch_'.$question['type']; - /** @var survey_question $display */ - $display = new $ch_type(); - - $url = api_get_self(); - $form = new FormValidator('question', 'post', $url); - $form->addHtml('
'); - $form->addHtml($question['survey_question']); - $display->render($form, $question, $finalAnswer); - $form->addHtml('
'); - $form->display(); + if (class_exists($ch_type)) { + /** @var survey_question $display */ + $display = new $ch_type(); + + $url = api_get_self(); + $form = new FormValidator('question', 'post', $url); + $form->addHtml('
'); + $form->addHtml($question['survey_question']); + $display->render($form, $question, $finalAnswer); + $form->addHtml('
'); + $form->display(); + } } } } @@ -698,14 +706,20 @@ class SurveyUtil echo ' '; } } + // displaying the table: footer (totals) + $optionResult = ''; + if (isset($option['question_id']) && isset($number_of_answers[$option['question_id']])) { + if ($number_of_answers[$option['question_id']] == 0) { + $optionResult = '0'; + } else { + $optionResult = $number_of_answers[$option['question_id']]; + } + } + // displaying the table: footer (totals) echo ' '; echo ' '.get_lang('Total').''; - echo ' ' - .($number_of_answers[$option['question_id']] == 0 - ? '0' - : $number_of_answers[$option['question_id']]) - .''; + echo ' '.$optionResult.''; echo '  '; echo '  '; echo ' '; @@ -1484,18 +1498,13 @@ class SurveyUtil public static function export_complete_report_xls($survey_data, $filename, $user_id = 0) { $course_id = api_get_course_int_id(); + $user_id = (int) $user_id; $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : 0; if (empty($course_id) || empty($surveyId)) { return false; } - $spreadsheet = new PHPExcel(); - $spreadsheet->setActiveSheetIndex(0); - $worksheet = $spreadsheet->getActiveSheet(); - $line = 1; - $column = 1; // Skip the first column (row titles) - // Show extra fields blank space (enough for extra fields on next line) // Show user fields section with a big th colspan that spans over all fields $extra_user_fields = UserManager::get_extra_fields( @@ -1506,10 +1515,10 @@ class SurveyUtil false, true ); + $list = []; $num = count($extra_user_fields); for ($i = 0; $i < $num; $i++) { - $worksheet->setCellValueByColumnAndRow($column, $line, ''); - $column++; + $list[0][] = ''; } $display_extra_user_fields = true; @@ -1534,6 +1543,8 @@ class SurveyUtil GROUP BY questions.question_id ORDER BY questions.sort ASC"; $result = Database::query($sql); + $line = 1; + $column = 1; while ($row = Database::fetch_array($result)) { // We show the questions if // 1. there is no question filter and the export button has not been clicked @@ -1545,24 +1556,16 @@ class SurveyUtil // We do not show comment and pagebreak question types if ($row['type'] != 'pagebreak') { if ($row['number_of_options'] == 0 && ($row['type'] == 'open' || $row['type'] == 'comment')) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['survey_question']), - ENT_QUOTES - ) + $list[$line][$column] = api_html_entity_decode( + strip_tags($row['survey_question']), + ENT_QUOTES ); $column++; } else { for ($ii = 0; $ii < $row['number_of_options']; $ii++) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['survey_question']), - ENT_QUOTES - ) + $list[$line][$column] = api_html_entity_decode( + strip_tags($row['survey_question']), + ENT_QUOTES ); $column++; } @@ -1577,11 +1580,7 @@ class SurveyUtil if ($display_extra_user_fields) { // Show the fields names for user fields foreach ($extra_user_fields as &$field) { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES) - ); + $list[$line][$column] = api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES); $column++; } } @@ -1619,13 +1618,9 @@ class SurveyUtil ) { // We do not show comment and pagebreak question types if ($row['type'] != 'pagebreak') { - $worksheet->setCellValueByColumnAndRow( - $column, - $line, - api_html_entity_decode( - strip_tags($row['option_text']), - ENT_QUOTES - ) + $list[$line][$column] = api_html_entity_decode( + strip_tags($row['option_text']), + ENT_QUOTES ); $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id']; $possible_answers_type[$row['question_id']] = $row['type']; @@ -1642,7 +1637,7 @@ class SurveyUtil $sql = "SELECT * FROM $table_survey_answer WHERE c_id = $course_id AND survey_id = $surveyId"; if ($user_id != 0) { - $sql .= " AND user='".intval($user_id)."' "; + $sql .= " AND user='".$user_id."' "; } $sql .= " ORDER BY user ASC"; @@ -1658,7 +1653,7 @@ class SurveyUtil true ); foreach ($return as $elem) { - $worksheet->setCellValueByColumnAndRow($column, $line, $elem); + $list[$line][$column] = $elem; $column++; } $answers_of_user = []; @@ -1685,14 +1680,11 @@ class SurveyUtil // this is to display the last user foreach ($return as $elem) { - $worksheet->setCellValueByColumnAndRow($column, $line, $elem); + $list[$line][$column] = $elem; $column++; } - $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); - $writer = new PHPExcel_Writer_Excel2007($spreadsheet); - $writer->save($file); - DocumentManager::file_send_for_download($file, true, $filename); + Export::arrayToXls($list, $filename); return null; } @@ -1837,7 +1829,6 @@ class SurveyUtil foreach ($questions as $key => &$question) { if (is_array($allowed_question_types)) { if (in_array($question['type'], $allowed_question_types)) { - //echo '