Survey: Enable questions duplication + add error message for question deletion - refs BT#16553

pull/3285/head
Yannick Warnier 5 years ago
parent 4bb960c42e
commit 0288088589
  1. 95
      main/survey/survey.lib.php
  2. 36
      main/survey/survey.php

@ -684,6 +684,7 @@ class SurveyManager
}
/**
* Copy given survey to a new (optional) given survey ID
* @param int $survey_id
* @param int $new_survey_id
* @param int $targetCourseId
@ -1495,7 +1496,7 @@ class SurveyManager
* @param int $survey_id the id of the survey
* @param int $question_id the id of the question
* @param bool $shared
*
* @return mixed False on error, true if the question could be deleted
* @todo also delete the answers to this question
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
@ -1518,12 +1519,15 @@ class SurveyManager
$sql = "DELETE FROM $table
WHERE
c_id = $course_id AND
survey_id='".$survey_id."' AND
question_id='".$question_id."'";
Database::query($sql);
survey_id = $survey_id AND
question_id = $question_id";
$result = Database::query($sql);
if (false == $result) {
return false;
}
// Deleting the options of the question of the survey
self::delete_survey_question_option($survey_id, $question_id, $shared);
return true;
}
/**
@ -2116,7 +2120,7 @@ class SurveyManager
}
/**
* This function copy survey specifying course id and session id where will be copied.
* Copy survey specifying course ID and session ID where will be copied.
*
* @param int $surveyId
* @param int $targetCourseId target course id
@ -2245,6 +2249,85 @@ class SurveyManager
return false;
}
/**
* Copy/duplicate one question (into the same survey).
* Note: Relies on the question iid to find all necessary info.
* @param int $questionId
* @return int The new question's iid, or 0 on error
*/
public static function copyQuestion($questionId)
{
if (empty($questionId)) {
return 0;
}
$questionId = (int) $questionId;
$questionTable = Database::get_course_table(TABLE_SURVEY_QUESTION);
$optionsTable = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
// Get questions
$sql = "SELECT * FROM $questionTable WHERE iid = $questionId";
$res = Database::query($sql);
if (false == $res) {
// Could not find this question
return 0;
}
$row = Database::fetch_array($res, 'ASSOC');
$params = [
'c_id' => $row['c_id'],
'survey_id' => $row['survey_id'],
'survey_question' => trim($row['survey_question'])."'",
'survey_question_comment' => $row['survey_question_comment'],
'type' => $row['type'],
'display' => $row['display'],
'shared_question_id' => $row['shared_question_id'],
'max_value' => $row['max_value'],
'survey_group_pri' => $row['survey_group_pri'],
'survey_group_sec1' => $row['survey_group_sec1'],
'survey_group_sec2' => $row['survey_group_sec2'],
];
if (api_get_configuration_value('allow_required_survey_questions')) {
if (isset($row['is_required'])) {
$params['is_required'] = $row['is_required'];
}
}
// Get question position
$sqlSort = "SELECT max(sort) as sort FROM $questionTable
WHERE survey_id = ".$row['survey_id'];
$resSort = Database::query($sqlSort);
$rowSort = Database::fetch_assoc($resSort);
$params['sort'] = $rowSort['sort'] + 1;
// Insert the new question
$insertId = Database::insert($questionTable, $params);
if (false == $insertId) {
return 0;
}
// Normalize question_id with iid
$sql = "UPDATE $questionTable
SET question_id = iid
WHERE iid = $insertId";
Database::query($sql);
// Get questions options
$sql = "SELECT * FROM $optionsTable WHERE question_id = $questionId";
$res = Database::query($sql);
while ($row = Database::fetch_assoc($res)) {
$params = [
'c_id' => $row['c_id'],
'question_id' => $insertId,
'survey_id' => $row['survey_id'],
'option_text' => $row['option_text'],
'sort' => $row['sort'],
'value' => $row['value'],
];
$optionId = Database::insert($optionsTable, $params);
if ($optionId) {
$sql = "UPDATE $optionsTable SET question_option_id = $optionId WHERE iid = $optionId";
Database::query($sql);
}
}
return $insertId;
}
/**
* @param array $surveyData
*

@ -97,11 +97,29 @@ if ($is_survey_type_1 && ($action === 'addgroup' || $action === 'deletegroup'))
exit;
}
if ('copyquestion' == $action) {
$copied = SurveyManager::copyQuestion($_GET['question_id']);
if (false !== $copied) {
$sendmsg = 'QuestionAdded';
} else {
$sendmsg = 'ErrorOccurred';
}
header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&sendmsg='.$sendmsg);
exit;
}
$htmlHeadXtra[] = '<script>'.api_get_language_translate_html().'</script>';
// Displaying the header
Display::display_header($tool_name, 'Survey');
if (!$is_survey_type_1 && !empty($_GET['sendmsg'])) {
echo Display::return_message(
get_lang($_GET['sendmsg']),
'confirmation',
false
);
}
// Action handling
$my_action_survey = Security::remove_XSS($action);
$my_question_id_survey = isset($_GET['question_id']) ? Security::remove_XSS($_GET['question_id']) : null;
@ -118,11 +136,24 @@ if (isset($action)) {
echo Display::return_message(get_lang('SurveyQuestionMoved'), 'confirmation');
}
if ($action === 'delete' && is_numeric($_GET['question_id'])) {
SurveyManager::delete_survey_question(
$result = SurveyManager::delete_survey_question(
$my_survey_id_survey,
$my_question_id_survey,
$survey_data['is_shared']
);
if (false == $result) {
echo Display::return_message(
get_lang('Error'),
'error',
false
);
} else {
echo Display::return_message(
get_lang('Deleted'),
'confirmation',
false
);
}
}
}
@ -314,6 +345,9 @@ while ($row = Database::fetch_array($result, 'ASSOC')) {
Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
}
echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?'.api_get_cidreq().'&action=copyquestion&type='.$row['type'].'&survey_id='.$survey_id.'&question_id='.$row['question_id'].'">'.
Display::return_icon('copy.png', get_lang('Copy'), '', ICON_SIZE_SMALL).'</a>';
echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?'.api_get_cidreq().'&action=delete&survey_id='.$survey_id.'&question_id='.$row['question_id'].'" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang("DeleteSurveyQuestion").'?', ENT_QUOTES, $charset)).'\')) return false;">'.
Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
if ($survey_data['survey_type'] != 3) {

Loading…
Cancel
Save