Survey: Ask for survey code when duplicating a survey

pull/5867/head
juancp-contidosdixitais 1 year ago committed by GitHub
parent d905b2e825
commit dbbda28c87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 37
      main/survey/survey.lib.php
  2. 25
      main/survey/surveyUtil.class.php
  3. 44
      main/survey/survey_list.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;
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();

@ -3437,6 +3437,25 @@ class SurveyUtil
$formToString = $form->returnForm();
echo '<div id="dialog-confirm">'.$formToString.'</div>';
$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 '<div id="dialog-copy-confirm">'.$formToString.'</div>';
$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(

@ -56,6 +56,14 @@ $htmlHeadXtra[] = '<script>
modal: true
});
$("#dialog-copy-confirm").dialog({
autoOpen: false,
show: "blind",
resizable: false,
height:300,
modal: true
});
$(".multiplicate_popup").click(function() {
var targetUrl = $(this).attr("href");
$( "#dialog-confirm" ).dialog({
@ -72,6 +80,30 @@ $htmlHeadXtra[] = '<script>
$("#dialog-confirm").dialog("open");
return false;
});
$(".copy_survey_popup").click(function() {
var targetUrl = $(this).attr("href");
$( "#dialog-copy-confirm" ).dialog({
width:800,
height:200,
buttons: {
"'.addslashes(get_lang('CopySurvey')).'": function() {
var surveyCode = $("input[name=survey_code]").val();
location.href = targetUrl+"&survey_code="+surveyCode;
$( this ).dialog( "close" );
}
}
});
$("#dialog-copy-confirm").dialog({
open: function( event, ui ) {
var timestampMiliseconds= new Date().getTime();
var timestampSeconds = Math.floor(timestampMiliseconds / 1000);
$("input[name=survey_code]").val(timestampSeconds);
}
});
$("#dialog-copy-confirm").dialog("open");
return false;
});
});
</script>';
@ -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()) {
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;

Loading…
Cancel
Save