Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Survey: Permitir modificar código encuesta al copiar encuesta #5124

Open
wants to merge 2 commits into
base: 1.11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 37 additions & 2 deletions main/survey/survey.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ public static function generate_unique_code($code)
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.
*
Expand Down Expand Up @@ -735,7 +762,8 @@ public static function delete_survey($survey_id, $shared = false, $course_id = 0
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) {
Expand All @@ -757,7 +785,14 @@ public static function copy_survey(

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();
Expand Down
25 changes: 23 additions & 2 deletions main/survey/surveyUtil.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3435,6 +3435,25 @@ public static function display_survey_list()
$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();
}

Expand Down Expand Up @@ -3514,14 +3533,15 @@ public static function checkHideEditionToolsByCode($surveyCode)
*
* @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
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
*
* @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);
Expand Down Expand Up @@ -3589,7 +3609,8 @@ public static function modify_filter($survey_id, $drh = false)
$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(
Expand Down
46 changes: 45 additions & 1 deletion main/survey/survey_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
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({
Expand All @@ -72,6 +80,30 @@
$("#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>';

Expand Down Expand Up @@ -108,6 +140,8 @@

$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'])) {
Expand Down Expand Up @@ -810,7 +844,17 @@
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;
Expand Down