diff --git a/main/survey/survey.lib.php b/main/survey/survey.lib.php
index 04afc7f50b..cd6c8f1c2f 100755
--- a/main/survey/survey.lib.php
+++ b/main/survey/survey.lib.php
@@ -48,6 +48,33 @@ class SurveyManager
return $code.$num;
}
+ /**
+ * Checks if the survey code is unique.
+ *
+ * @param string $courseCode
+ *
+ * @return bool
+ * @assert ('') === false
+ */
+ public static function checkUniqueCode($courseCode)
+ {
+ if (empty($courseCode)) {
+ return false;
+ }
+ $courseId = api_get_course_int_id();
+ $table = Database::get_course_table(TABLE_SURVEY);
+ $courseCode = Database::escape_string($courseCode);
+
+ $sql = "SELECT * FROM $table
+ WHERE code = '$courseCode' AND c_id = $courseId";
+ $result = Database::query($sql);
+ if (Database::num_rows($result)) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
/**
* Deletes all survey invitations of a user.
*
@@ -735,7 +762,8 @@ class SurveyManager
public static function copy_survey(
$survey_id,
$new_survey_id = null,
- $targetCourseId = null
+ $targetCourseId = null,
+ $surveyCode = null
) {
$course_id = api_get_course_int_id();
if (!$targetCourseId) {
@@ -757,7 +785,14 @@ class SurveyManager
if (empty($new_survey_id)) {
$params = $survey_data;
- $params['code'] = self::generate_unique_code($params['code']);
+
+ if (!empty($surveyCode)) {
+ $surveyCode = preg_replace('/\s+/', '', $surveyCode);
+ $params['code'] = $surveyCode;
+ } else {
+ $params['code'] = self::generate_unique_code($params['code']);
+ }
+
$params['c_id'] = $targetCourseId;
unset($params['survey_id']);
$params['session_id'] = api_get_session_id();
diff --git a/main/survey/surveyUtil.class.php b/main/survey/surveyUtil.class.php
index 49a95949d8..eff2013985 100755
--- a/main/survey/surveyUtil.class.php
+++ b/main/survey/surveyUtil.class.php
@@ -3437,6 +3437,25 @@ class SurveyUtil
$formToString = $form->returnForm();
echo '
'.$formToString.'
';
+
+ $form = new FormValidator(
+ 'copy-survey',
+ 'post',
+ null,
+ null,
+ ['class' => 'form-vertical']
+ );
+ $form->addElement(
+ 'text',
+ 'survey_code',
+ get_lang('SurveyCode'),
+ ['size' => 20, 'maxlength' => 20]
+ );
+
+ $formToString = $form->returnForm();
+
+ echo ''.$formToString.'
';
+
$table->display();
}
@@ -3516,6 +3535,7 @@ class SurveyUtil
*
* @param int $survey_id the id of the survey
* @param bool $drh
+ * @param bool $surveyCode
*
* @return string html code that are the actions that can be performed on any survey
*
@@ -3523,7 +3543,7 @@ class SurveyUtil
*
* @version January 2007
*/
- public static function modify_filter($survey_id, $drh = false)
+ public static function modify_filter($survey_id, $drh = false, $surveyCode = "")
{
/** @var CSurvey $survey */
$survey = Database::getManager()->find('ChamiloCourseBundle:CSurvey', $survey_id);
@@ -3591,7 +3611,8 @@ class SurveyUtil
$actions[] = Display::url(
Display::return_icon('copy.png', get_lang('DuplicateSurvey')),
$codePath.'survey/survey_list.php?'
- .http_build_query($params + ['action' => 'copy_survey', 'survey_id' => $survey_id])
+ .http_build_query($params + ['action' => 'copy_survey', 'survey_id' => $survey_id]),
+ ['survey_id' => $survey_id, 'class' => 'copy_survey_popup']
);
$actions[] = Display::url(
diff --git a/main/survey/survey_list.php b/main/survey/survey_list.php
index 3c89414aee..836267323c 100755
--- a/main/survey/survey_list.php
+++ b/main/survey/survey_list.php
@@ -56,6 +56,14 @@ $htmlHeadXtra[] = '';
@@ -108,6 +140,8 @@ if (isset($_GET['search']) && 'advanced' === $_GET['search']) {
$listUrl = api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq();
$surveyId = isset($_GET['survey_id']) ? $_GET['survey_id'] : 0;
+$surveyCode = isset($_GET['survey_code']) ? $_GET['survey_code'] : '';
+$surveyCode = Security::remove_XSS($surveyCode);
// Action handling: performing the same action on multiple surveys
if (isset($_POST['action']) && $_POST['action'] && isset($_POST['id']) && is_array($_POST['id'])) {
@@ -810,7 +844,17 @@ switch ($action) {
break;
case 'copy_survey':
if (!empty($surveyId) && api_is_allowed_to_edit()) {
- SurveyManager::copy_survey($surveyId);
+ if (!empty($surveyCode)) {
+ if (SurveyManager::checkUniqueCode($surveyCode)) {
+ SurveyManager::copy_survey($surveyId, null, null, $surveyCode);
+ } else {
+ Display::addFlash(Display::return_message(get_lang('CodeAlreadyExists'), 'warning', false));
+ header('Location: '.$listUrl);
+ exit;
+ }
+ } else {
+ SurveyManager::copy_survey($surveyId);
+ }
Display::addFlash(Display::return_message(get_lang('SurveyCopied'), 'confirmation', false));
header('Location: '.$listUrl);
exit;