Skip to content

Commit dbbda28

Browse files
Survey: Ask for survey code when duplicating a survey
1 parent d905b2e commit dbbda28

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed

main/survey/survey.lib.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ public static function generate_unique_code($code)
4848
return $code.$num;
4949
}
5050

51+
/**
52+
* Checks if the survey code is unique.
53+
*
54+
* @param string $courseCode
55+
*
56+
* @return bool
57+
* @assert ('') === false
58+
*/
59+
public static function checkUniqueCode($courseCode)
60+
{
61+
if (empty($courseCode)) {
62+
return false;
63+
}
64+
$courseId = api_get_course_int_id();
65+
$table = Database::get_course_table(TABLE_SURVEY);
66+
$courseCode = Database::escape_string($courseCode);
67+
68+
$sql = "SELECT * FROM $table
69+
WHERE code = '$courseCode' AND c_id = $courseId";
70+
$result = Database::query($sql);
71+
if (Database::num_rows($result)) {
72+
return false;
73+
} else {
74+
return true;
75+
}
76+
}
77+
5178
/**
5279
* Deletes all survey invitations of a user.
5380
*
@@ -735,7 +762,8 @@ public static function delete_survey($survey_id, $shared = false, $course_id = 0
735762
public static function copy_survey(
736763
$survey_id,
737764
$new_survey_id = null,
738-
$targetCourseId = null
765+
$targetCourseId = null,
766+
$surveyCode = null
739767
) {
740768
$course_id = api_get_course_int_id();
741769
if (!$targetCourseId) {
@@ -757,7 +785,14 @@ public static function copy_survey(
757785

758786
if (empty($new_survey_id)) {
759787
$params = $survey_data;
760-
$params['code'] = self::generate_unique_code($params['code']);
788+
789+
if (!empty($surveyCode)) {
790+
$surveyCode = preg_replace('/\s+/', '', $surveyCode);
791+
$params['code'] = $surveyCode;
792+
} else {
793+
$params['code'] = self::generate_unique_code($params['code']);
794+
}
795+
761796
$params['c_id'] = $targetCourseId;
762797
unset($params['survey_id']);
763798
$params['session_id'] = api_get_session_id();

main/survey/surveyUtil.class.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,25 @@ public static function display_survey_list()
34373437
$formToString = $form->returnForm();
34383438

34393439
echo '<div id="dialog-confirm">'.$formToString.'</div>';
3440+
3441+
$form = new FormValidator(
3442+
'copy-survey',
3443+
'post',
3444+
null,
3445+
null,
3446+
['class' => 'form-vertical']
3447+
);
3448+
$form->addElement(
3449+
'text',
3450+
'survey_code',
3451+
get_lang('SurveyCode'),
3452+
['size' => 20, 'maxlength' => 20]
3453+
);
3454+
3455+
$formToString = $form->returnForm();
3456+
3457+
echo '<div id="dialog-copy-confirm">'.$formToString.'</div>';
3458+
34403459
$table->display();
34413460
}
34423461

@@ -3516,14 +3535,15 @@ public static function checkHideEditionToolsByCode($surveyCode)
35163535
*
35173536
* @param int $survey_id the id of the survey
35183537
* @param bool $drh
3538+
* @param bool $surveyCode
35193539
*
35203540
* @return string html code that are the actions that can be performed on any survey
35213541
*
35223542
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
35233543
*
35243544
* @version January 2007
35253545
*/
3526-
public static function modify_filter($survey_id, $drh = false)
3546+
public static function modify_filter($survey_id, $drh = false, $surveyCode = "")
35273547
{
35283548
/** @var CSurvey $survey */
35293549
$survey = Database::getManager()->find('ChamiloCourseBundle:CSurvey', $survey_id);
@@ -3591,7 +3611,8 @@ public static function modify_filter($survey_id, $drh = false)
35913611
$actions[] = Display::url(
35923612
Display::return_icon('copy.png', get_lang('DuplicateSurvey')),
35933613
$codePath.'survey/survey_list.php?'
3594-
.http_build_query($params + ['action' => 'copy_survey', 'survey_id' => $survey_id])
3614+
.http_build_query($params + ['action' => 'copy_survey', 'survey_id' => $survey_id]),
3615+
['survey_id' => $survey_id, 'class' => 'copy_survey_popup']
35953616
);
35963617

35973618
$actions[] = Display::url(

main/survey/survey_list.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
modal: true
5757
});
5858
59+
$("#dialog-copy-confirm").dialog({
60+
autoOpen: false,
61+
show: "blind",
62+
resizable: false,
63+
height:300,
64+
modal: true
65+
});
66+
5967
$(".multiplicate_popup").click(function() {
6068
var targetUrl = $(this).attr("href");
6169
$( "#dialog-confirm" ).dialog({
@@ -72,6 +80,30 @@
7280
$("#dialog-confirm").dialog("open");
7381
return false;
7482
});
83+
84+
$(".copy_survey_popup").click(function() {
85+
var targetUrl = $(this).attr("href");
86+
$( "#dialog-copy-confirm" ).dialog({
87+
width:800,
88+
height:200,
89+
buttons: {
90+
"'.addslashes(get_lang('CopySurvey')).'": function() {
91+
var surveyCode = $("input[name=survey_code]").val();
92+
location.href = targetUrl+"&survey_code="+surveyCode;
93+
$( this ).dialog( "close" );
94+
}
95+
}
96+
});
97+
$("#dialog-copy-confirm").dialog({
98+
open: function( event, ui ) {
99+
var timestampMiliseconds= new Date().getTime();
100+
var timestampSeconds = Math.floor(timestampMiliseconds / 1000);
101+
$("input[name=survey_code]").val(timestampSeconds);
102+
}
103+
});
104+
$("#dialog-copy-confirm").dialog("open");
105+
return false;
106+
});
75107
});
76108
</script>';
77109

@@ -108,6 +140,8 @@
108140

109141
$listUrl = api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq();
110142
$surveyId = isset($_GET['survey_id']) ? $_GET['survey_id'] : 0;
143+
$surveyCode = isset($_GET['survey_code']) ? $_GET['survey_code'] : '';
144+
$surveyCode = Security::remove_XSS($surveyCode);
111145

112146
// Action handling: performing the same action on multiple surveys
113147
if (isset($_POST['action']) && $_POST['action'] && isset($_POST['id']) && is_array($_POST['id'])) {
@@ -810,7 +844,17 @@
810844
break;
811845
case 'copy_survey':
812846
if (!empty($surveyId) && api_is_allowed_to_edit()) {
813-
SurveyManager::copy_survey($surveyId);
847+
if (!empty($surveyCode)) {
848+
if (SurveyManager::checkUniqueCode($surveyCode)) {
849+
SurveyManager::copy_survey($surveyId, null, null, $surveyCode);
850+
} else {
851+
Display::addFlash(Display::return_message(get_lang('CodeAlreadyExists'), 'warning', false));
852+
header('Location: '.$listUrl);
853+
exit;
854+
}
855+
} else {
856+
SurveyManager::copy_survey($surveyId);
857+
}
814858
Display::addFlash(Display::return_message(get_lang('SurveyCopied'), 'confirmation', false));
815859
header('Location: '.$listUrl);
816860
exit;

0 commit comments

Comments
 (0)