Survey: Fix survey workflow and response handling

pull/5105/head
christianbeeznst 2 years ago
parent 664e337608
commit 9d5c20f68e
  1. 3
      assets/css/app.scss
  2. 2
      public/main/gradebook/lib/be/category.class.php
  3. 43
      public/main/inc/lib/formvalidator/Element/DatePicker.php
  4. 2
      public/main/inc/lib/formvalidator/Element/DateTimePicker.php
  5. 96
      public/main/survey/survey.lib.php
  6. 2
      public/main/survey/survey.php
  7. 23
      public/main/survey/surveyUtil.class.php
  8. 21
      public/main/survey/survey_question.php

@ -468,7 +468,8 @@ table#skill_holder {
padding: 20px 0px; padding: 20px 0px;
} }
#date_fields label, .p-float-label label[for='date_time'] { #date_fields label,
.p-float-label .datepicker-label {
top: 0px; top: 0px;
left: 0.5rem; left: 0.5rem;
--tw-bg-opacity: 1; --tw-bg-opacity: 1;

@ -2568,7 +2568,7 @@ class Category implements GradebookItem
$cat->set_visible($data['visible']); $cat->set_visible($data['visible']);
$cat->set_session_id($data['session_id']); $cat->set_session_id($data['session_id']);
$cat->set_certificate_min_score($data['certif_min_score']); $cat->set_certificate_min_score($data['certif_min_score']);
$cat->set_grade_model_id($data['grade_model_id']); $cat->set_grade_model_id((int) $data['grade_model_id']);
$cat->set_locked($data['locked']); $cat->set_locked($data['locked']);
$cat->setGenerateCertificates($data['generate_certificates']); $cat->setGenerateCertificates($data['generate_certificates']);
$cat->setIsRequirement($data['is_requirement']); $cat->setIsRequirement($data['is_requirement']);

@ -113,49 +113,8 @@ class DatePicker extends HTML_QuickForm_text
if ($('label[for=\"".$id."\"]').length > 0) { if ($('label[for=\"".$id."\"]').length > 0) {
$('label[for=\"".$id."\"]').hide(); $('label[for=\"".$id."\"]').hide();
} }
});
</script>";
return $js;
$js .= "<script>
$(function() {
var txtDate = $('#$id'),
inputGroup = txtDate.parents('.input-group'),
txtDateAlt = $('#{$id}_alt'),
txtDateAltText = $('#{$id}_alt_text');
txtDate document.querySelector('label[for=\"' + '{$id}' + '\"]').classList.add('datepicker-label');
.hide()
.datepicker({
defaultDate: '".$this->getValue()."',
dateFormat: 'yy-mm-dd',
altField: '#{$id}_alt',
altFormat: \"".get_lang('MM dd, yy')."\",
showOn: 'both',
buttonImage: '".Display::getMdiIcon(ToolIcon::ATTENDANCE, 'ch-tool-icon', null, ICON_SIZE_TINY)."',
buttonImageOnly: true,
buttonText: '".get_lang('Select date')."',
changeMonth: true,
changeYear: true,
yearRange: 'c-60y:c+5y'
})
.on('change', function (e) {
txtDateAltText.text(txtDateAlt.val());
});
txtDateAltText.on('click', function () {
txtDate.datepicker('show');
});
inputGroup
.find('button')
.on('click', function (e) {
e.preventDefault();
$('#$id, #{$id}_alt').val('');
$('#{$id}_alt_text').html('');
});
}); });
</script>"; </script>";

@ -88,6 +88,8 @@ class DateTimePicker extends HTML_QuickForm_text
time_24hr: true, time_24hr: true,
wrap: false wrap: false
}); });
document.querySelector('label[for=\"' + '{$id}' + '\"]').classList.add('datepicker-label');
}); });
</script>"; </script>";

@ -27,7 +27,7 @@ class SurveyManager
* *
* @return string * @return string
*/ */
public static function generate_unique_code($code) public static function generate_unique_code(string $code, ?int $surveyId = null)
{ {
if (empty($code)) { if (empty($code)) {
return false; return false;
@ -38,8 +38,15 @@ class SurveyManager
$num = 0; $num = 0;
$new_code = $code; $new_code = $code;
while (true) { while (true) {
if (isset($surveyId)) {
$sql = "SELECT * FROM $table
WHERE code = '$new_code' AND iid = $surveyId";
} else {
$sql = "SELECT * FROM $table $sql = "SELECT * FROM $table
WHERE code = '$new_code' AND c_id = $course_id"; WHERE code = '$new_code'";
}
$result = Database::query($sql); $result = Database::query($sql);
if (Database::num_rows($result)) { if (Database::num_rows($result)) {
$num++; $num++;
@ -492,7 +499,7 @@ class SurveyManager
$survey_weight = floatval($_POST['survey_weight']); $survey_weight = floatval($_POST['survey_weight']);
$max_score = 1; $max_score = 1;
if (!$gradebook_link_id) { if (!$gradebook_link_id && isset($values['category_id'])) {
GradebookUtils::add_resource_to_course_gradebook( GradebookUtils::add_resource_to_course_gradebook(
$values['category_id'], $values['category_id'],
$courseCode, $courseCode,
@ -643,7 +650,7 @@ class SurveyManager
$new_survey_id = (int) $new_survey_id; $new_survey_id = (int) $new_survey_id;
} }
$sql = "SELECT * FROM $table_survey_question_group /*$sql = "SELECT * FROM $table_survey_question_group
WHERE iid = $survey_id"; WHERE iid = $survey_id";
$res = Database::query($sql); $res = Database::query($sql);
@ -662,7 +669,7 @@ class SurveyManager
Database::query($sql); Database::query($sql);
$group_id[$row['id']] = $insertId; $group_id[$row['id']] = $insertId;
} }*/
// Get questions // Get questions
$sql = "SELECT * FROM $table_survey_question $sql = "SELECT * FROM $table_survey_question
@ -671,7 +678,6 @@ class SurveyManager
$res = Database::query($sql); $res = Database::query($sql);
while ($row = Database::fetch_assoc($res)) { while ($row = Database::fetch_assoc($res)) {
$params = [ $params = [
'c_id' => $targetCourseId,
'survey_id' => $new_survey_id, 'survey_id' => $new_survey_id,
'survey_question' => $row['survey_question'], 'survey_question' => $row['survey_question'],
'survey_question_comment' => $row['survey_question_comment'], 'survey_question_comment' => $row['survey_question_comment'],
@ -706,7 +712,6 @@ class SurveyManager
$res = Database::query($sql); $res = Database::query($sql);
while ($row = Database::fetch_assoc($res)) { while ($row = Database::fetch_assoc($res)) {
$params = [ $params = [
'c_id' => $targetCourseId,
'question_id' => $question_id[$row['question_id']], 'question_id' => $question_id[$row['question_id']],
'survey_id' => $new_survey_id, 'survey_id' => $new_survey_id,
'option_text' => $row['option_text'], 'option_text' => $row['option_text'],
@ -714,11 +719,6 @@ class SurveyManager
'value' => $row['value'], 'value' => $row['value'],
]; ];
$insertId = Database::insert($table_survey_options, $params); $insertId = Database::insert($table_survey_options, $params);
if ($insertId) {
$sql = "UPDATE $table_survey_options SET question_option_id = $insertId
WHERE iid = $insertId";
Database::query($sql);
}
} }
return $new_survey_id; return $new_survey_id;
@ -755,16 +755,16 @@ class SurveyManager
$sql = 'DELETE FROM '.$table_survey_invitation.' $sql = 'DELETE FROM '.$table_survey_invitation.'
WHERE WHERE
c_id = '.$courseId.' AND survey_id = "'.$surveyId.'" '.$session_where.' ';
survey_code = "'.Database::escape_string($datas['code']).'" '.$session_where.' ';
Database::query($sql); Database::query($sql);
$sql = 'DELETE FROM '.$table_survey_answer.' $sql = 'DELETE FROM '.$table_survey_answer.'
WHERE c_id = '.$courseId.' AND survey_id='.$surveyId; WHERE
survey_id = "'.$surveyId.'" '.$session_where.' ';
Database::query($sql); Database::query($sql);
$sql = 'UPDATE '.$table_survey.' SET invited=0, answered=0 $sql = 'UPDATE '.$table_survey.' SET invited=0, answered=0
WHERE c_id = '.$courseId.' AND iid ='.$surveyId; WHERE iid ='.$surveyId;
Database::query($sql); Database::query($sql);
Event::addEvent( Event::addEvent(
@ -1027,7 +1027,7 @@ class SurveyManager
$survey_data = self::get_survey($surveyId); $survey_data = self::get_survey($surveyId);
// Storing a new question // Storing a new question
if ('' == $form_content['question_id'] || !is_numeric($form_content['question_id'])) { if (empty($form_content['question_id']) || !is_numeric($form_content['question_id'])) {
// Finding the max sort order of the questions in the given survey // Finding the max sort order of the questions in the given survey
$sql = "SELECT max(sort) AS max_sort $sql = "SELECT max(sort) AS max_sort
FROM $tbl_survey_question FROM $tbl_survey_question
@ -1163,59 +1163,41 @@ class SurveyManager
} }
/** /**
* This functions moves a question of a survey up or down. * Moves a survey question within the ordered list.
*
* @param string $direction
* @param int $survey_question_id
* @param int $survey_id
* *
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University * @param string $direction The direction to move the question ('moveup' or 'movedown').
* * @param int $surveyQuestionId The ID of the survey question to move.
* @version January 2007 * @param int $surveyId The ID of the survey to which the question belongs.
*/ */
public static function move_survey_question($direction, $survey_question_id, $survey_id) public static function moveSurveyQuestion(string $direction, int $surveyQuestionId, int $surveyId): void
{ {
// Table definition $em = Database::getManager();
$table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); $repo = $em->getRepository(CSurveyQuestion::class);
$course_id = api_get_course_int_id(); $sortDirection = $direction === 'moveup' ? 'DESC' : 'ASC';
$questions = $repo->findBy(['survey' => $surveyId], ['sort' => $sortDirection]);
if ('moveup' === $direction) {
$sort = 'DESC';
}
if ('movedown' === $direction) {
$sort = 'ASC';
}
$survey_id = (int) $survey_id;
// Finding the two questions that needs to be swapped
$sql = "SELECT * FROM $table_survey_question
WHERE c_id = $course_id AND survey_id='".$survey_id."'
ORDER BY sort $sort";
$result = Database::query($sql);
$found = false; $found = false;
while ($row = Database::fetch_assoc($result)) { foreach ($questions as $question) {
if ($found) { if ($found) {
$question_id_two = $row['question_id']; $secondQuestion = $question;
$question_sort_two = $row['sort'];
$found = false; $found = false;
break;
} }
if ($row['question_id'] == $survey_question_id) { if ($question->getIid() == $surveyQuestionId) {
$found = true; $found = true;
$question_id_one = $row['question_id']; $firstQuestion = $question;
$question_sort_one = $row['sort'];
} }
} }
$sql = "UPDATE $table_survey_question if (isset($firstQuestion) && isset($secondQuestion)) {
SET sort = '".Database::escape_string($question_sort_two)."' $tempSort = $firstQuestion->getSort();
WHERE c_id = $course_id AND question_id='".intval($question_id_one)."'"; $firstQuestion->setSort($secondQuestion->getSort());
Database::query($sql); $secondQuestion->setSort($tempSort);
$sql = "UPDATE $table_survey_question $em->persist($firstQuestion);
SET sort = '".Database::escape_string($question_sort_one)."' $em->persist($secondQuestion);
WHERE c_id = $course_id AND question_id='".intval($question_id_two)."'"; $em->flush();
Database::query($sql); }
} }
/** /**

@ -123,7 +123,7 @@ if (!empty($action)) {
break; break;
case 'moveup': case 'moveup':
case 'movedown': case 'movedown':
SurveyManager::move_survey_question( SurveyManager::moveSurveyQuestion(
$action, $action,
$my_question_id_survey, $my_question_id_survey,
$my_survey_id_survey $my_survey_id_survey

@ -2425,13 +2425,22 @@ class SurveyUtil
? explode(';', $already_invited['additional_users']) ? explode(';', $already_invited['additional_users'])
: []; : [];
$my_alredy_invited = $already_invited['course_users'] ?? []; $my_alredy_invited = $already_invited['course_users'] ?? [];
if ((is_numeric($value) && !in_array($value, $my_alredy_invited)) ||
(!is_numeric($value) && !in_array($value, $addit_users_array)) $userId = 0;
) { if (is_string($value) && filter_var($value, FILTER_VALIDATE_EMAIL)) {
$userInfo = api_get_user_info_from_email($value);
if ($userInfo && isset($userInfo['id'])) {
$userId = $userInfo['id'];
}
} elseif (is_numeric($value)) {
$userId = $value;
}
if ($userId && !in_array($userId, $my_alredy_invited)) {
$new_user = true; $new_user = true;
if (!array_key_exists($value, $survey_invitations)) { if (!array_key_exists($userId, $survey_invitations)) {
self::saveInvitation( self::saveInvitation(
api_get_user_entity($value), api_get_user_entity($userId),
$invitation_code, $invitation_code,
api_get_utc_datetime(time(), null, true), api_get_utc_datetime(time(), null, true),
$survey, $survey,
@ -2820,7 +2829,9 @@ class SurveyUtil
public static function display_survey_list() public static function display_survey_list()
{ {
$parameters = []; $parameters = [];
$parameters['cidReq'] = api_get_course_id(); $parameters['cid'] = api_get_course_int_id();
$parameters['sid'] = api_get_session_id();
$parameters['gid'] = api_get_group_id();
if (isset($_GET['do_search']) && $_GET['do_search']) { if (isset($_GET['do_search']) && $_GET['do_search']) {
$message = get_lang('Display search results').'<br />'; $message = get_lang('Display search results').'<br />';
$message .= '<a href="'.api_get_self().'?'.api_get_cidreq().'">'.get_lang('Display all').'</a>'; $message .= '<a href="'.api_get_self().'?'.api_get_cidreq().'">'.get_lang('Display all').'</a>';

@ -3,6 +3,7 @@
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
use Chamilo\CourseBundle\Entity\CSurvey; use Chamilo\CourseBundle\Entity\CSurvey;
use Chamilo\CourseBundle\Entity\CSurveyQuestion;
use ChamiloSession as Session; use ChamiloSession as Session;
/** /**
@ -445,7 +446,7 @@ class survey_question
// Adding an answer // Adding an answer
if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) { if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) {
if (isset($_REQUEST['type']) && 'multiplechoiceother' === $_REQUEST['type']) { if (isset($_REQUEST['type']) && 'multiplechoiceother' === $_REQUEST['type'] && $counter > 2) {
$counter--; $counter--;
} }
$counter++; $counter++;
@ -689,22 +690,22 @@ class survey_question
* *
* @return array The questions that have the given question as parent * @return array The questions that have the given question as parent
*/ */
public static function getDependency($question) public static function getDependency(array $question): ?array
{ {
if ('false' === api_get_setting('survey.survey_question_dependency')) { if ('false' === api_get_setting('survey.survey_question_dependency')) {
return []; return [];
} }
$table = Database::get_course_table(TABLE_SURVEY_QUESTION);
$questionId = $question['question_id']; $questionId = $question['question_id'];
$courseId = api_get_course_int_id();
// Getting the information of the question $em = Database::getManager();
$sql = "SELECT * FROM $table
WHERE c_id = $courseId AND parent_id = $questionId "; $queryBuilder = $em->createQueryBuilder();
$result = Database::query($sql); $queryBuilder->select('q')
$row = Database::store_result($result, 'ASSOC'); ->from(CSurveyQuestion::class, 'q')
->where('q.parent = :parent')
->setParameter('parent', $questionId);
return $row; return $queryBuilder->getQuery()->getArrayResult();
} }
/** /**

Loading…
Cancel
Save