chamilo /
chamilo-lms
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* For licensing terms, see /license.txt */ |
||
| 6 | |||
| 7 | use Chamilo\CoreBundle\Framework\Container; |
||
| 8 | use Chamilo\CourseBundle\Entity\CSurvey; |
||
| 9 | use ChamiloSession as Session; |
||
| 10 | |||
| 11 | $lastQuestion = 0; |
||
| 12 | |||
| 13 | /* |
||
| 14 | * @author unknown, the initial survey that did not make it in 1.8 because of bad code |
||
| 15 | * @author Patrick Cool <[email protected]>, Ghent University: cleanup, |
||
| 16 | * refactoring and rewriting large parts of the code |
||
| 17 | * @author Julio Montoya <[email protected]>, Chamilo: Personality Test |
||
| 18 | * modification and rewriting large parts of the code as well |
||
| 19 | * |
||
| 20 | * @todo check if the user already filled the survey and if this |
||
| 21 | * is the case then the answers have to be updated and not stored again. |
||
| 22 | * @todo performance could be improved if not the survey_id was |
||
| 23 | * stored with the invitation but the survey_code |
||
| 24 | */ |
||
| 25 | |||
| 26 | // Unsetting the course id (because it is in the URL) |
||
| 27 | if (!isset($_GET['cidReq'])) { |
||
| 28 | $cidReset = true; |
||
| 29 | } else { |
||
| 30 | $_cid = $_GET['cidReq']; |
||
| 31 | } |
||
| 32 | |||
| 33 | require_once __DIR__.'/../inc/global.inc.php'; |
||
| 34 | |||
| 35 | // ----------------------------------------------------------------------------- |
||
| 36 | // DB tables |
||
| 37 | // ----------------------------------------------------------------------------- |
||
| 38 | $table_survey = Database::get_course_table(TABLE_SURVEY); |
||
| 39 | $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER); |
||
| 40 | $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); |
||
| 41 | $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); |
||
| 42 | $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION); |
||
| 43 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 44 | |||
| 45 | $allowRequiredSurveyQuestions = true; |
||
| 46 | |||
| 47 | // ----------------------------------------------------------------------------- |
||
| 48 | // Auth / context |
||
| 49 | // ----------------------------------------------------------------------------- |
||
| 50 | $isAnonymous = api_is_anonymous(api_get_user_id(), true); |
||
| 51 | |||
| 52 | $courseInfo = isset($_GET['course']) |
||
| 53 | ? api_get_course_info($_GET['course']) |
||
| 54 | : api_get_course_info(); |
||
| 55 | |||
| 56 | if (empty($courseInfo)) { |
||
| 57 | api_not_allowed(true); |
||
| 58 | } |
||
| 59 | |||
| 60 | $courseId = $courseInfo['real_id']; |
||
| 61 | $userInfo = api_get_user_info(); |
||
| 62 | $sessionId = isset($_GET['sid']) ? (int) $_GET['sid'] : api_get_session_id(); |
||
| 63 | $lpItemId = isset($_GET['lp_item_id']) ? (int) $_GET['lp_item_id'] : 0; |
||
| 64 | $allowSurveyInLp = true; |
||
| 65 | |||
| 66 | if (!empty($userInfo)) { |
||
| 67 | $interbreadcrumb[] = [ |
||
| 68 | 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?cid='.$courseId.'&sid='.$sessionId, |
||
| 69 | 'name' => get_lang('Survey list'), |
||
| 70 | ]; |
||
| 71 | } |
||
| 72 | |||
| 73 | // ----------------------------------------------------------------------------- |
||
| 74 | // Required params |
||
| 75 | // ----------------------------------------------------------------------------- |
||
| 76 | if ((!isset($_GET['course']) || !isset($_GET['invitationcode'])) && !isset($_GET['user_id'])) { |
||
| 77 | api_not_allowed(true, get_lang('There is a parameter missing in the link. Please use copy and past')); |
||
| 78 | } |
||
| 79 | |||
| 80 | $repo = Container::getSurveyRepository(); |
||
| 81 | $surveyId = isset($_GET['iid']) ? (int) $_GET['iid'] : 0; |
||
| 82 | |||
| 83 | if (empty($surveyId) && (isset($_POST['language']) && is_numeric($_POST['language']))) { |
||
| 84 | $surveyId = (int) $_POST['language']; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** @var CSurvey $survey */ |
||
| 88 | $survey = $repo->find($surveyId); |
||
| 89 | if (null === $survey) { |
||
| 90 | api_not_allowed(true); |
||
| 91 | } |
||
| 92 | |||
| 93 | $surveyId = $survey->getIid(); |
||
| 94 | $invitationCode = $_GET['invitationcode'] ?? null; |
||
| 95 | |||
| 96 | $lpItemCondition = ''; |
||
| 97 | if ($allowSurveyInLp && !empty($lpItemId)) { |
||
| 98 | $lpItemCondition = " AND c_lp_item_id = $lpItemId"; |
||
| 99 | } |
||
| 100 | |||
| 101 | $sessionCondition = ''; |
||
| 102 | if (true === api_get_setting('survey.show_surveys_base_in_sessions')) { |
||
| 103 | $sessionCondition = api_get_session_condition($sessionId); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Anonymous restriction |
||
| 107 | if (0 == $survey->getAnonymous() && api_is_anonymous()) { |
||
| 108 | api_not_allowed(true); |
||
| 109 | } |
||
| 110 | |||
| 111 | // ----------------------------------------------------------------------------- |
||
| 112 | // Auto-invitation flow |
||
| 113 | // ----------------------------------------------------------------------------- |
||
| 114 | if ('auto' === $invitationCode) { |
||
| 115 | $userid = api_get_user_id(); |
||
| 116 | $surveyCode = $survey->getCode(); |
||
| 117 | |||
| 118 | if ($isAnonymous) { |
||
| 119 | $autoInvitationCode = 'auto-ANONY_'.md5(time())."-$surveyCode"; |
||
| 120 | } else { |
||
| 121 | $invitations = SurveyManager::getUserInvitationsForSurveyInCourse( |
||
| 122 | $userid, |
||
| 123 | $surveyCode, |
||
| 124 | $courseId, |
||
| 125 | $sessionId, |
||
| 126 | 0, |
||
| 127 | $lpItemId |
||
| 128 | ); |
||
| 129 | $lastInvitation = current($invitations); |
||
| 130 | $autoInvitationCode = $lastInvitation |
||
| 131 | ? $lastInvitation->getInvitationCode() |
||
| 132 | : "auto-$userid-$surveyCode"; |
||
| 133 | } |
||
| 134 | |||
| 135 | SurveyManager::checkTimeAvailability($survey); |
||
| 136 | |||
| 137 | $sql = "SELECT user_id |
||
| 138 | FROM $table_survey_invitation |
||
| 139 | WHERE c_id = $courseId |
||
| 140 | AND invitation_code = '".Database::escape_string($autoInvitationCode)."' |
||
| 141 | $sessionCondition |
||
| 142 | $lpItemCondition"; |
||
| 143 | $result = Database::query($sql); |
||
| 144 | $now = api_get_utc_datetime(); |
||
| 145 | |||
| 146 | if (0 == Database::num_rows($result)) { |
||
| 147 | $params = [ |
||
| 148 | 'c_id' => $courseId, |
||
| 149 | 'survey_id' => $surveyId, |
||
| 150 | 'user_id' => $userid, |
||
| 151 | 'invitation_code' => $autoInvitationCode, |
||
| 152 | 'invitation_date' => $now, |
||
| 153 | 'answered' => 0, |
||
| 154 | 'c_lp_item_id' => $lpItemId, |
||
| 155 | ]; |
||
| 156 | Database::insert($table_survey_invitation, $params); |
||
| 157 | } |
||
| 158 | |||
| 159 | $_GET['invitationcode'] = $autoInvitationCode; |
||
| 160 | Session::write('auto_invitation_code_'.$surveyCode, $autoInvitationCode); |
||
| 161 | $invitationCode = $autoInvitationCode; |
||
| 162 | } |
||
| 163 | |||
| 164 | // Validate invitation code |
||
| 165 | $sql = "SELECT * |
||
| 166 | FROM $table_survey_invitation |
||
| 167 | WHERE c_id = $courseId |
||
| 168 | AND invitation_code = '".Database::escape_string($invitationCode)."' |
||
| 169 | $sessionCondition |
||
| 170 | $lpItemCondition"; |
||
| 171 | $result = Database::query($sql); |
||
| 172 | if (Database::num_rows($result) < 1) { |
||
| 173 | api_not_allowed(true, get_lang('Wrong invitation code')); |
||
| 174 | } |
||
| 175 | |||
| 176 | $survey_invitation = Database::fetch_assoc($result); |
||
| 177 | $surveyUserFromSession = Session::read('surveyuser'); |
||
| 178 | |||
| 179 | // Determine the correct key used to store/load answers. |
||
| 180 | // - Non-anonymous surveys use the numeric user_id from the invitation. |
||
| 181 | // - Anonymous surveys store answers under a per-session identifier ("surveyuser"). |
||
| 182 | $answerUserKey = (int) ($survey_invitation['user_id'] ?? 0); |
||
| 183 | |||
| 184 | if (1 == (int) $survey->getAnonymous()) { |
||
| 185 | $answerUserKey = (string) ($surveyUserFromSession ?? ''); |
||
| 186 | |||
| 187 | if ('' === $answerUserKey) { |
||
| 188 | // Create a stable identifier for this anonymous attempt. |
||
| 189 | $answerUserKey = 'surveyuser_'.sha1(uniqid('', true)); |
||
| 190 | Session::write('surveyuser', $answerUserKey); |
||
| 191 | $surveyUserFromSession = $answerUserKey; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | |||
| 196 | // Block if already answered |
||
| 197 | if ( |
||
| 198 | !isset($_POST['finish_survey']) |
||
| 199 | && ( |
||
| 200 | ($isAnonymous && !empty($surveyUserFromSession) && SurveyUtil::isSurveyAnsweredFlagged($survey->getCode(), $survey_invitation['c_id'])) |
||
| 201 | || (1 == $survey_invitation['answered'] && !isset($_GET['user_id'])) |
||
| 202 | ) |
||
| 203 | ) { |
||
| 204 | api_not_allowed(true, Display::return_message(get_lang('You already filled this survey'))); |
||
| 205 | } |
||
| 206 | |||
| 207 | Event::registerLog([ |
||
|
0 ignored issues
–
show
|
|||
| 208 | 'tool' => TOOL_SURVEY, |
||
| 209 | 'tool_id' => $survey_invitation['iid'], |
||
| 210 | 'action' => 'invitationcode', |
||
| 211 | 'action_details' => $invitationCode, |
||
| 212 | ]); |
||
| 213 | |||
| 214 | $survey_invitation['survey_id'] = $surveyId; |
||
| 215 | |||
| 216 | // Availability check |
||
| 217 | SurveyManager::checkTimeAvailability($survey); |
||
| 218 | |||
| 219 | // Redirect if meeting |
||
| 220 | $surveyType = $survey->getSurveyType(); |
||
| 221 | if (3 === $surveyType) { |
||
| 222 | header( |
||
| 223 | 'Location: '. |
||
| 224 | api_get_path(WEB_CODE_PATH). |
||
| 225 | 'survey/meeting.php?cid='.$courseId.'&sid='.$sessionId.'&invitationcode='.Security::remove_XSS($invitationCode) |
||
| 226 | ); |
||
| 227 | |||
| 228 | exit; |
||
| 229 | } |
||
| 230 | |||
| 231 | if (!empty($survey->getAnonymous())) { |
||
| 232 | define('USER_IN_ANON_SURVEY', true); |
||
| 233 | } |
||
| 234 | |||
| 235 | // ----------------------------------------------------------------------------- |
||
| 236 | // Answer saving |
||
| 237 | // ----------------------------------------------------------------------------- |
||
| 238 | if (count($_POST) > 0) { |
||
| 239 | if (0 === $surveyType) { |
||
| 240 | // Standard survey flow |
||
| 241 | $types = []; |
||
| 242 | $required = []; |
||
| 243 | $questionList = []; |
||
| 244 | |||
| 245 | foreach ($survey->getQuestions() as $question) { |
||
| 246 | $id = $question->getIid(); |
||
| 247 | $questionList[$id] = $question; |
||
| 248 | $types[$id] = $question->getType(); |
||
| 249 | $required[$id] = $allowRequiredSurveyQuestions && $question->isMandatory(); |
||
| 250 | } |
||
| 251 | |||
| 252 | foreach ($_POST as $key => &$value) { |
||
| 253 | // Only question inputs |
||
| 254 | if (!str_contains($key, 'other_question') |
||
| 255 | && str_contains($key, 'question') && '_qf__question' !== $key) { |
||
| 256 | $survey_question_id = str_replace('question', '', $key); |
||
| 257 | if (empty($survey_question_id)) { |
||
| 258 | continue; |
||
| 259 | } |
||
| 260 | |||
| 261 | $other = isset($_POST['other_question'.$survey_question_id]) ? $_POST['other_question'.$survey_question_id] : ''; |
||
| 262 | $question = $questionList[$survey_question_id] ?? null; |
||
| 263 | if (null === $question) { |
||
| 264 | continue; |
||
| 265 | } |
||
| 266 | |||
| 267 | if (is_array($value)) { |
||
| 268 | // Score or multiple |
||
| 269 | SurveyUtil::remove_answer($survey_invitation['user_id'], $surveyId, $survey_question_id, $lpItemId); |
||
| 270 | |||
| 271 | foreach ($value as $answer_key => &$answer_value) { |
||
| 272 | if ('score' === $types[$survey_question_id]) { |
||
| 273 | $option_id = $answer_key; |
||
| 274 | $option_value = $answer_value; |
||
| 275 | } else { |
||
| 276 | $option_id = $answer_value; |
||
| 277 | $option_value = ''; |
||
| 278 | } |
||
| 279 | |||
| 280 | SurveyUtil::saveAnswer( |
||
| 281 | $survey_invitation['user_id'], |
||
| 282 | $survey, |
||
| 283 | $question, |
||
| 284 | $option_id, |
||
| 285 | $option_value, |
||
| 286 | '', |
||
| 287 | $lpItemId |
||
| 288 | ); |
||
| 289 | } |
||
| 290 | } else { |
||
| 291 | // Open / single / percentage |
||
| 292 | $option_value = 0; |
||
| 293 | if (isset($types[$survey_question_id]) && 'percentage' === $types[$survey_question_id]) { |
||
| 294 | $sql = "SELECT * FROM $table_survey_question_option WHERE iid='".(int) $value."'"; |
||
| 295 | $result = Database::query($sql); |
||
| 296 | $row = Database::fetch_assoc($result); |
||
| 297 | if ($row) { |
||
| 298 | $option_value = $row['option_text']; |
||
| 299 | } |
||
| 300 | } elseif (isset($types[$survey_question_id]) && 'open' === $types[$survey_question_id]) { |
||
| 301 | $option_value = $value; |
||
| 302 | } |
||
| 303 | |||
| 304 | SurveyUtil::remove_answer($survey_invitation['user_id'], $surveyId, $survey_question_id, $lpItemId); |
||
| 305 | |||
| 306 | SurveyUtil::saveAnswer( |
||
| 307 | $survey_invitation['user_id'], |
||
| 308 | $survey, |
||
| 309 | $question, |
||
| 310 | $value, |
||
| 311 | $option_value, |
||
| 312 | $other, |
||
| 313 | $lpItemId |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } elseif (1 === $survey->getSurveyType()) { |
||
| 319 | // Conditional / personality test |
||
| 320 | $shuffle = ''; |
||
| 321 | if (1 == $survey->getShuffle()) { |
||
| 322 | $shuffle = ' ORDER BY RAND() '; |
||
| 323 | } |
||
| 324 | |||
| 325 | $questionList = []; |
||
| 326 | foreach ($survey->getQuestions() as $question) { |
||
| 327 | $questionList[$question->getIid()] = $question; |
||
| 328 | } |
||
| 329 | |||
| 330 | foreach ($_POST as $key => &$value) { |
||
| 331 | if (str_contains($key, 'question')) { |
||
| 332 | $survey_question_id = str_replace('question', '', $key); |
||
| 333 | if (empty($survey_question_id)) { |
||
| 334 | continue; |
||
| 335 | } |
||
| 336 | |||
| 337 | $sql = "SELECT value FROM $table_survey_question_option WHERE iid='".(int) $value."'"; |
||
| 338 | $result = Database::query($sql); |
||
| 339 | $row = Database::fetch_assoc($result); |
||
| 340 | $option_value = $row['value']; |
||
| 341 | |||
| 342 | SurveyUtil::remove_answer( |
||
| 343 | $survey_invitation['user_id'], |
||
| 344 | $survey_invitation['survey_id'], |
||
| 345 | $survey_question_id, |
||
| 346 | $lpItemId |
||
| 347 | ); |
||
| 348 | |||
| 349 | $surveyId = (int) $survey_invitation['survey_id']; |
||
| 350 | $repo = Container::getSurveyRepository(); |
||
| 351 | $survey = $repo->find($surveyId); |
||
| 352 | |||
| 353 | SurveyUtil::saveAnswer( |
||
| 354 | api_get_user_entity($survey_invitation['user_id']), |
||
| 355 | $survey, |
||
| 356 | $questionList[$survey_question_id], |
||
| 357 | $value, |
||
| 358 | $option_value, |
||
| 359 | '', |
||
| 360 | $lpItemId |
||
| 361 | ); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | } else { |
||
| 365 | api_not_allowed(true, get_lang('Survey type unknown')); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | // ----------------------------------------------------------------------------- |
||
| 370 | // Profile form (if requested by survey) |
||
| 371 | // ----------------------------------------------------------------------------- |
||
| 372 | $user_id = api_get_user_id(); |
||
| 373 | if (0 == $user_id) { |
||
| 374 | $user_id = $survey_invitation['user_id']; |
||
| 375 | } |
||
| 376 | $user_data = api_get_user_info($user_id); |
||
| 377 | |||
| 378 | if ('' != $survey->getFormFields() && 0 == $survey->getAnonymous() && is_array($user_data)) { |
||
| 379 | $form_fields = explode('@', $survey->getFormFields()); |
||
| 380 | $list = []; |
||
| 381 | foreach ($form_fields as $field) { |
||
| 382 | $field_value = explode(':', $field); |
||
| 383 | if (isset($field_value[1]) && 1 == $field_value[1]) { |
||
| 384 | if ('' != $field_value[0]) { |
||
| 385 | $val = api_substr($field_value[0], 8, api_strlen($field_value[0])); |
||
| 386 | $list[$val] = 1; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | $url = api_get_self().'?'.api_get_cidreq(); |
||
| 392 | $listQueryParams = explode('&', $_SERVER['QUERY_STRING']); |
||
| 393 | foreach ($listQueryParams as $param) { |
||
| 394 | $url .= '&'.Security::remove_XSS($param); |
||
| 395 | } |
||
| 396 | if (!empty($lpItemId)) { |
||
| 397 | $url .= '&lp_item_id='.$lpItemId; |
||
| 398 | } |
||
| 399 | |||
| 400 | // Same form as auth/profile.php |
||
| 401 | $form = new FormValidator('profile', 'post', $url); |
||
| 402 | if (api_is_western_name_order()) { |
||
| 403 | if (isset($list['firstname']) && 1 == $list['firstname']) { |
||
| 404 | $form->addElement('text', 'firstname', get_lang('First name'), ['size' => 40]); |
||
| 405 | if ('true' !== api_get_setting('profile', 'name')) { |
||
| 406 | $form->freeze(['firstname']); |
||
| 407 | } |
||
| 408 | $form->applyFilter(['firstname'], 'stripslashes'); |
||
| 409 | $form->applyFilter(['firstname'], 'trim'); |
||
| 410 | $form->addRule('firstname', get_lang('Required field'), 'required'); |
||
| 411 | } |
||
| 412 | if (isset($list['lastname']) && 1 == $list['lastname']) { |
||
| 413 | $form->addElement('text', 'lastname', get_lang('Last name'), ['size' => 40]); |
||
| 414 | if ('true' !== api_get_setting('profile', 'name')) { |
||
| 415 | $form->freeze(['lastname']); |
||
| 416 | } |
||
| 417 | $form->applyFilter(['lastname'], 'stripslashes'); |
||
| 418 | $form->applyFilter(['lastname'], 'trim'); |
||
| 419 | $form->addRule('lastname', get_lang('Required field'), 'required'); |
||
| 420 | } |
||
| 421 | } else { |
||
| 422 | if (isset($list['lastname']) && 1 == $list['lastname']) { |
||
| 423 | $form->addElement('text', 'lastname', get_lang('Last name'), ['size' => 40]); |
||
| 424 | if ('true' !== api_get_setting('profile', 'name')) { |
||
| 425 | $form->freeze(['lastname']); |
||
| 426 | } |
||
| 427 | $form->applyFilter(['lastname'], 'stripslashes'); |
||
| 428 | $form->applyFilter(['lastname'], 'trim'); |
||
| 429 | $form->addRule('lastname', get_lang('Required field'), 'required'); |
||
| 430 | } |
||
| 431 | if (isset($list['firstname']) && 1 == $list['firstname']) { |
||
| 432 | $form->addElement('text', 'firstname', get_lang('First name'), ['size' => 40]); |
||
| 433 | if ('true' !== api_get_setting('profile', 'name')) { |
||
| 434 | $form->freeze(['firstname']); |
||
| 435 | } |
||
| 436 | $form->applyFilter(['firstname'], 'stripslashes'); |
||
| 437 | $form->applyFilter(['firstname'], 'trim'); |
||
| 438 | $form->addRule('firstname', get_lang('Required field'), 'required'); |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | if (isset($list['official_code']) && 1 == $list['official_code']) { |
||
| 443 | $form->addElement('text', 'official_code', get_lang('Code'), ['size' => 40]); |
||
| 444 | if ('true' !== api_get_setting('profile', 'officialcode')) { |
||
| 445 | $form->freeze('official_code'); |
||
| 446 | } |
||
| 447 | $form->applyFilter('official_code', 'stripslashes'); |
||
| 448 | $form->applyFilter('official_code', 'trim'); |
||
| 449 | if ('true' === api_get_setting('registration', 'officialcode') |
||
| 450 | && 'true' === api_get_setting('profile', 'officialcode')) { |
||
| 451 | $form->addRule('official_code', get_lang('Required field'), 'required'); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | if (isset($list['email']) && 1 == $list['email']) { |
||
| 456 | $form->addElement('text', 'email', get_lang('E-mail'), ['size' => 40]); |
||
| 457 | if ('true' !== api_get_setting('profile', 'email')) { |
||
| 458 | $form->freeze('email'); |
||
| 459 | } |
||
| 460 | $form->applyFilter('email', 'stripslashes'); |
||
| 461 | $form->applyFilter('email', 'trim'); |
||
| 462 | if ('true' === api_get_setting('registration', 'email')) { |
||
| 463 | $form->addRule('email', get_lang('Required field'), 'required'); |
||
| 464 | } |
||
| 465 | $form->addEmailRule('email'); |
||
| 466 | } |
||
| 467 | |||
| 468 | if (isset($list['phone']) && 1 == $list['phone']) { |
||
| 469 | $form->addElement('text', 'phone', get_lang('Phone'), ['size' => 20]); |
||
| 470 | if ('true' !== api_get_setting('profile', 'phone')) { |
||
| 471 | $form->freeze('phone'); |
||
| 472 | } |
||
| 473 | $form->applyFilter('phone', 'stripslashes'); |
||
| 474 | $form->applyFilter('phone', 'trim'); |
||
| 475 | if ('true' == api_get_setting('profile', 'phone')) { |
||
| 476 | $form->addRule('phone', get_lang('Required field'), 'required'); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | if (isset($list['language']) && 1 == $list['language']) { |
||
| 481 | $form->addSelectLanguage('language', get_lang('Language')); |
||
| 482 | if ('true' !== api_get_setting('profile', 'language')) { |
||
| 483 | $form->freeze('language'); |
||
| 484 | } |
||
| 485 | if ('true' === api_get_setting('profile', 'language')) { |
||
| 486 | $form->addRule('language', get_lang('Required field'), 'required'); |
||
| 487 | } |
||
| 488 | } |
||
| 489 | |||
| 490 | $extraField = new ExtraField('user'); |
||
| 491 | $returnParams = $extraField->addElements($form, api_get_user_id()); |
||
| 492 | $jquery_ready_content = $returnParams['jquery_ready_content']; |
||
| 493 | |||
| 494 | $htmlHeadXtra[] = '<script>$(function(){ '.$jquery_ready_content.' });</script>'; |
||
| 495 | $form->addButtonNext(get_lang('Next')); |
||
| 496 | $form->setDefaults($user_data); |
||
| 497 | } |
||
| 498 | |||
| 499 | // ----------------------------------------------------------------------------- |
||
| 500 | // JS assets for selective display and question widgets |
||
| 501 | // ----------------------------------------------------------------------------- |
||
| 502 | $htmlHeadXtra[] = ch_selectivedisplay::getJs(); |
||
| 503 | $htmlHeadXtra[] = survey_question::getJs(); |
||
| 504 | |||
| 505 | // ----------------------------------------------------------------------------- |
||
| 506 | // Header + page container |
||
| 507 | // ----------------------------------------------------------------------------- |
||
| 508 | Display::display_header(get_lang('Surveys')); |
||
| 509 | |||
| 510 | echo '<div class="mx-auto mt-8 bg-white shadow rounded-2xl p-6 border border-gray-20">'; |
||
| 511 | echo '<h2 class="text-2xl font-bold text-gray-800 mb-2">'.Security::remove_XSS(strip_tags($survey->getTitle(), '<span>')).'</h2>'; |
||
| 512 | |||
| 513 | if (!empty($survey->getSubtitle())) { |
||
| 514 | echo '<p class="text-gray-600 mb-4">'.Security::remove_XSS($survey->getSubtitle()).'</p>'; |
||
| 515 | } |
||
| 516 | |||
| 517 | // Intro (first load) |
||
| 518 | if (!isset($_GET['show']) || (isset($_GET['show']) && '' == $_GET['show'])) { |
||
| 519 | Session::erase('paged_questions'); |
||
| 520 | Session::erase('page_questions_sec'); |
||
| 521 | $paged_questions_sec = []; |
||
| 522 | if (!empty($survey->getIntro())) { |
||
| 523 | echo '<div class="prose prose-slate max-w-none mb-6">'.Security::remove_XSS($survey->getIntro()).'</div>'; |
||
| 524 | } |
||
| 525 | $limit = 0; |
||
| 526 | } |
||
| 527 | |||
| 528 | // Profile form handling |
||
| 529 | if ($survey->getFormFields() && 0 == $survey->getAnonymous() && is_array($user_data) && !isset($_GET['show'])) { |
||
| 530 | if ($form->validate()) { |
||
| 531 | $user_data = $form->exportValues(); |
||
| 532 | if (is_array($user_data) && count($user_data) > 0) { |
||
| 533 | $sql = "UPDATE $table_user SET"; |
||
| 534 | $update = false; |
||
| 535 | $allowedFields = ['firstname', 'lastname', 'official_code', 'email', 'phone', 'language']; |
||
| 536 | |||
| 537 | foreach ($user_data as $key => $value) { |
||
| 538 | if (in_array($key, $allowedFields)) { |
||
| 539 | $sql .= " $key = '".Database::escape_string($value)."',"; |
||
| 540 | $update = true; |
||
| 541 | } |
||
| 542 | } |
||
| 543 | $sql = rtrim($sql, ',')." WHERE id = $user_id"; |
||
| 544 | if ($update) { |
||
| 545 | Database::query($sql); |
||
| 546 | } |
||
| 547 | |||
| 548 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 549 | $extraFieldValue->saveFieldValues($user_data); |
||
| 550 | |||
| 551 | echo Display::return_message(get_lang('Information updated').' '.get_lang('Please fill survey'), 'confirm', false); |
||
| 552 | } |
||
| 553 | |||
| 554 | $_GET['show'] = 0; |
||
| 555 | $show = 0; |
||
| 556 | |||
| 557 | Session::erase('paged_questions'); |
||
| 558 | Session::erase('page_questions_sec'); |
||
| 559 | $paged_questions_sec = []; |
||
| 560 | } else { |
||
| 561 | echo '<div class="mb-4 text-gray-700">'.get_lang('Update information').'</div>'; |
||
| 562 | Session::erase('paged_questions'); |
||
| 563 | Session::erase('page_questions_sec'); |
||
| 564 | $paged_questions_sec = []; |
||
| 565 | $form->display(); |
||
| 566 | } |
||
| 567 | } |
||
| 568 | |||
| 569 | // ----------------------------------------------------------------------------- |
||
| 570 | // Finish screen |
||
| 571 | // ----------------------------------------------------------------------------- |
||
| 572 | if (isset($_POST['finish_survey'])) { |
||
| 573 | echo Display::return_message(get_lang('You have finished this survey.'), 'confirm'); |
||
| 574 | echo '<div class="prose prose-slate">'.Security::remove_XSS($survey->getSurveythanks()).'</div>'; |
||
| 575 | |||
| 576 | SurveyManager::updateSurveyAnswered($survey, $survey_invitation['user_id'], $lpItemId); |
||
| 577 | SurveyUtil::flagSurveyAsAnswered($survey->getCode(), $survey_invitation['c_id']); |
||
| 578 | |||
| 579 | if ($courseInfo && !api_is_anonymous() && 'learnpath' !== api_get_origin()) { |
||
| 580 | echo '<div class="mt-6">'; |
||
| 581 | echo Display::toolbarButton( |
||
| 582 | get_lang('Return to Course Homepage'), |
||
| 583 | api_get_course_url($courseInfo['real_id']), |
||
| 584 | 'home-outline' |
||
| 585 | ); |
||
| 586 | echo '</div>'; |
||
| 587 | } |
||
| 588 | |||
| 589 | // Close container + footer and exit early |
||
| 590 | echo '</div>'; |
||
| 591 | Display::display_footer(); |
||
| 592 | |||
| 593 | exit; |
||
| 594 | } |
||
| 595 | |||
| 596 | // ----------------------------------------------------------------------------- |
||
| 597 | // Page / question building |
||
| 598 | // ----------------------------------------------------------------------------- |
||
| 599 | $shuffle = ''; |
||
| 600 | if (1 == $survey->getShuffle()) { |
||
| 601 | $shuffle = ' BY RAND() '; |
||
| 602 | } |
||
| 603 | |||
| 604 | $pageBreakText = []; |
||
| 605 | $paged_questions = []; // keep this defined for later usage |
||
| 606 | $questions_exists = true; |
||
| 607 | |||
| 608 | if ((isset($_GET['show']) && '' != $_GET['show']) || isset($_POST['personality'])) { |
||
| 609 | $questions_displayed = []; |
||
| 610 | $counter = 0; |
||
| 611 | $select = ' survey_question.parent_id, survey_question.parent_option_id, '; |
||
| 612 | |||
| 613 | if (0 === $survey->getSurveyType()) { |
||
| 614 | if (empty($paged_questions)) { |
||
| 615 | $sql = "SELECT * |
||
| 616 | FROM $table_survey_question |
||
| 617 | WHERE survey_question NOT LIKE '%{{%' |
||
| 618 | AND survey_id = '".$surveyId."' |
||
| 619 | ORDER BY sort ASC"; |
||
| 620 | $result = Database::query($sql); |
||
| 621 | if (0 == Database::num_rows($result)) { |
||
| 622 | $questions_exists = false; |
||
| 623 | } |
||
| 624 | while ($row = Database::fetch_assoc($result)) { |
||
| 625 | if (1 == $survey->getOneQuestionPerPage()) { |
||
| 626 | if ('pagebreak' !== $row['type']) { |
||
| 627 | $paged_questions[$counter][] = $row['iid']; |
||
| 628 | $counter++; |
||
| 629 | |||
| 630 | continue; |
||
| 631 | } |
||
| 632 | } else { |
||
| 633 | if ('pagebreak' === $row['type']) { |
||
| 634 | $counter++; |
||
| 635 | $pageBreakText[$counter] = $row['survey_question']; |
||
| 636 | } else { |
||
| 637 | $paged_questions[$counter][] = $row['iid']; |
||
| 638 | } |
||
| 639 | } |
||
| 640 | } |
||
| 641 | Session::write('paged_questions', $paged_questions); |
||
| 642 | } |
||
| 643 | |||
| 644 | // Fix contexts (support ticket #5529) |
||
| 645 | $courseId = $survey_invitation['c_id']; |
||
| 646 | Session::write('_cid', $courseId); |
||
| 647 | Session::write('_real_cid', $courseId); |
||
| 648 | |||
| 649 | if (array_key_exists($_GET['show'], $paged_questions)) { |
||
| 650 | if (isset($_GET['user_id'])) { |
||
| 651 | $my_user_id = 1 == $survey->getAnonymous() ? $surveyUserFromSession : api_get_user_id(); |
||
| 652 | |||
| 653 | $sql = "SELECT |
||
| 654 | survey_question.survey_group_sec1, |
||
| 655 | survey_question.survey_group_sec2, |
||
| 656 | survey_question.survey_group_pri, |
||
| 657 | survey_question.iid question_id, |
||
| 658 | survey_question.survey_id, |
||
| 659 | survey_question.survey_question, |
||
| 660 | survey_question.display, |
||
| 661 | survey_question.sort, |
||
| 662 | survey_question.type, |
||
| 663 | survey_question.max_value, |
||
| 664 | survey_question_option.iid question_option_id, |
||
| 665 | survey_question_option.option_text, |
||
| 666 | $select |
||
| 667 | survey_question_option.sort as option_sort |
||
| 668 | FROM $table_survey_question survey_question |
||
| 669 | LEFT JOIN $table_survey_question_option survey_question_option |
||
| 670 | ON survey_question.iid = survey_question_option.question_id |
||
| 671 | AND survey_question_option.c_id = $courseId |
||
| 672 | WHERE survey_question.survey_id = '".$surveyId."' |
||
| 673 | AND survey_question.iid NOT IN ( |
||
| 674 | SELECT sa.question_id |
||
| 675 | FROM ".$table_survey_answer." sa |
||
| 676 | WHERE sa.user='".$my_user_id."') |
||
| 677 | AND survey_question.c_id = $courseId |
||
| 678 | ORDER BY survey_question.sort, survey_question_option.sort ASC"; |
||
| 679 | } else { |
||
| 680 | $sql = "SELECT |
||
| 681 | survey_question.survey_group_sec1, |
||
| 682 | survey_question.survey_group_sec2, |
||
| 683 | survey_question.survey_group_pri, |
||
| 684 | survey_question.iid question_id, |
||
| 685 | survey_question.survey_id, |
||
| 686 | survey_question.survey_question, |
||
| 687 | survey_question.display, |
||
| 688 | survey_question.sort, |
||
| 689 | survey_question.type, |
||
| 690 | survey_question.max_value, |
||
| 691 | survey_question_option.iid question_option_id, |
||
| 692 | survey_question_option.option_text, |
||
| 693 | $select |
||
| 694 | survey_question_option.sort as option_sort |
||
| 695 | ".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')." |
||
| 696 | FROM $table_survey_question survey_question |
||
| 697 | LEFT JOIN $table_survey_question_option survey_question_option |
||
| 698 | ON survey_question.iid = survey_question_option.question_id |
||
| 699 | WHERE survey_question NOT LIKE '%{{%' |
||
| 700 | AND survey_question.survey_id = '".$surveyId."' |
||
| 701 | AND survey_question.iid IN (".implode(',', $paged_questions[$_GET['show']]).') |
||
| 702 | ORDER BY survey_question.sort, survey_question_option.sort ASC'; |
||
| 703 | } |
||
| 704 | |||
| 705 | $result = Database::query($sql); |
||
| 706 | $question_counter_max = Database::num_rows($result); |
||
| 707 | $counter = 0; |
||
| 708 | $limit = 0; |
||
| 709 | $questions = []; |
||
| 710 | while ($row = Database::fetch_assoc($result)) { |
||
| 711 | if ('pagebreak' !== $row['type']) { |
||
| 712 | $sort = $row['sort']; |
||
| 713 | $questions[$sort]['question_id'] = $row['question_id']; |
||
| 714 | $questions[$sort]['survey_id'] = $row['survey_id']; |
||
| 715 | $questions[$sort]['survey_question'] = $row['survey_question']; |
||
| 716 | $questions[$sort]['display'] = $row['display']; |
||
| 717 | $questions[$sort]['type'] = $row['type']; |
||
| 718 | $questions[$sort]['options'][$row['question_option_id']] = $row['option_text']; |
||
| 719 | $questions[$sort]['maximum_score'] = $row['max_value']; |
||
| 720 | $questions[$sort]['sort'] = $sort; |
||
| 721 | $questions[$sort]['is_required'] = $allowRequiredSurveyQuestions && ($row['is_required'] ?? 0); |
||
| 722 | $questions[$sort]['parent_id'] = $row['parent_id'] ?? 0; |
||
| 723 | $questions[$sort]['parent_option_id'] = $row['parent_option_id'] ?? 0; |
||
| 724 | } |
||
| 725 | $counter++; |
||
| 726 | if (isset($_GET['show']) && (int) $_GET['show'] >= 0) { |
||
| 727 | $lastQuestion = (int) $_GET['show'] - 1; |
||
| 728 | } else { |
||
| 729 | $lastQuestion = (int) $row['question_option_id']; |
||
| 730 | } |
||
| 731 | } |
||
| 732 | } |
||
| 733 | } elseif (1 === $survey->getSurveyType()) { |
||
| 734 | $my_survey_id = (int) $survey_invitation['survey_id']; |
||
| 735 | $current_user = Database::escape_string($survey_invitation['user_id']); |
||
| 736 | |||
| 737 | if (isset($_POST['personality'])) { |
||
| 738 | $order = '' == $shuffle ? 'BY sort ASC ' : $shuffle; |
||
| 739 | |||
| 740 | // Current user results |
||
| 741 | $results = []; |
||
| 742 | $sql = "SELECT survey_group_pri, user, SUM(value) as value |
||
| 743 | FROM $table_survey_answer as survey_answer |
||
| 744 | INNER JOIN $table_survey_question as survey_question |
||
| 745 | ON (survey_question.iid = survey_answer.question_id) |
||
| 746 | WHERE survey_answer.survey_id='".$my_survey_id."' |
||
| 747 | AND survey_answer.user='".$current_user."' |
||
| 748 | GROUP BY survey_group_pri |
||
| 749 | ORDER BY survey_group_pri"; |
||
| 750 | $result = Database::query($sql); |
||
| 751 | while ($row = Database::fetch_array($result)) { |
||
| 752 | $results[] = ['value' => $row['value'], 'group' => $row['survey_group_pri']]; |
||
| 753 | } |
||
| 754 | |||
| 755 | // Totals by group |
||
| 756 | $totals = []; |
||
| 757 | $sql = "SELECT SUM(temp.value) as value, temp.survey_group_pri FROM |
||
| 758 | ( |
||
| 759 | SELECT MAX(value) as value, survey_group_pri, survey_question.iid question_id |
||
| 760 | FROM $table_survey_question as survey_question |
||
| 761 | INNER JOIN $table_survey_question_option as survey_question_option |
||
| 762 | ON (survey_question.iid = survey_question_option.question_id) |
||
| 763 | WHERE survey_question.survey_id='".$my_survey_id."' |
||
| 764 | AND survey_question.c_id = $courseId |
||
| 765 | AND survey_question_option.c_id = $courseId |
||
| 766 | AND survey_group_sec1='0' AND survey_group_sec2='0' |
||
| 767 | GROUP BY survey_group_pri, survey_question.iid |
||
| 768 | ) as temp |
||
| 769 | GROUP BY temp.survey_group_pri |
||
| 770 | ORDER BY temp.survey_group_pri"; |
||
| 771 | $result = Database::query($sql); |
||
| 772 | while ($row = Database::fetch_array($result)) { |
||
| 773 | $totals[] = ['value' => $row['value'], 'group' => $row['survey_group_pri']]; |
||
| 774 | } |
||
| 775 | |||
| 776 | // Percentages |
||
| 777 | $final_results = []; |
||
| 778 | for ($i = 0; $i < count($totals); $i++) { |
||
| 779 | for ($j = 0; $j < count($results); $j++) { |
||
| 780 | if ($totals[$i]['group'] == $results[$j]['group']) { |
||
| 781 | $group = $totals[$i]['group']; |
||
| 782 | $percent = ($results[$j]['value'] / $totals[$i]['value']); |
||
| 783 | $final_results[$group] = $percent; |
||
| 784 | } |
||
| 785 | } |
||
| 786 | } |
||
| 787 | |||
| 788 | arsort($final_results); |
||
| 789 | $groups = array_keys($final_results); |
||
| 790 | $result = []; |
||
| 791 | $count_result = 0; |
||
| 792 | foreach ($final_results as $key => &$sub_result) { |
||
| 793 | $result[] = ['group' => $key, 'value' => $sub_result]; |
||
| 794 | $count_result++; |
||
| 795 | } |
||
| 796 | |||
| 797 | $i = 0; |
||
| 798 | $group_cant = 0; |
||
| 799 | $equal_count = 0; |
||
| 800 | if ($count_result > 0) { |
||
| 801 | while (1) { |
||
| 802 | if (($result[$i]['value'] ?? null) == ($result[$i + 1]['value'] ?? null)) { |
||
| 803 | $equal_count++; |
||
| 804 | } else { |
||
| 805 | break; |
||
| 806 | } |
||
| 807 | $i++; |
||
| 808 | } |
||
| 809 | } else { |
||
| 810 | $equal_count = 10; // force undefined |
||
| 811 | } |
||
| 812 | |||
| 813 | if ($equal_count < 4) { |
||
| 814 | if (0 === $equal_count || 1 === $equal_count) { |
||
| 815 | if (($result[0]['value'] ?? 0) == ($result[1]['value'] ?? 0) && ($result[2]['value'] ?? 0) == ($result[3]['value'] ?? 0)) { |
||
| 816 | $group_cant = 1; |
||
| 817 | } elseif (($result[0]['value'] ?? 0) != ($result[1]['value'] ?? 0) |
||
| 818 | && ($result[1]['value'] ?? 0) == ($result[2]['value'] ?? 0) |
||
| 819 | && ($result[2]['value'] ?? 0) == ($result[3]['value'] ?? 0)) { |
||
| 820 | $group_cant = 0; |
||
| 821 | } else { |
||
| 822 | $group_cant = 2; |
||
| 823 | } |
||
| 824 | } else { |
||
| 825 | $group_cant = $equal_count; |
||
| 826 | } |
||
| 827 | |||
| 828 | if ($group_cant > 0) { |
||
| 829 | $secondary = ''; |
||
| 830 | for ($i = 0; $i <= $group_cant; $i++) { |
||
| 831 | $group1 = $groups[$i] ?? null; |
||
| 832 | $group2 = $groups[$i + 1] ?? null; |
||
| 833 | if (null === $group1 || null === $group2) { |
||
| 834 | continue; |
||
| 835 | } |
||
| 836 | if (2 == $group_cant && $i == $group_cant) { |
||
| 837 | $group2 = $groups[0]; |
||
| 838 | } |
||
| 839 | $secondary .= (empty($secondary) ? '' : ' OR ') |
||
| 840 | ." ( survey_group_sec1 = '$group1' AND survey_group_sec2 = '$group2') " |
||
| 841 | ." OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) "; |
||
| 842 | } |
||
| 843 | |||
| 844 | if (empty($_SESSION['page_questions_sec']) |
||
| 845 | && !is_array($_SESSION['page_questions_sec']) |
||
| 846 | && count(0 == $_SESSION['page_questions_sec'])) { |
||
| 847 | $sql = "SELECT * FROM $table_survey_question |
||
| 848 | WHERE survey_id = '".$my_survey_id."' |
||
| 849 | AND ($secondary) |
||
| 850 | ORDER BY sort ASC"; |
||
| 851 | $result = Database::query($sql); |
||
| 852 | $counter = 0; |
||
| 853 | while ($row = Database::fetch_assoc($result)) { |
||
| 854 | if (1 == $survey->getOneQuestionPerPage()) { |
||
| 855 | $paged_questions_sec[$counter][] = $row['question_id']; |
||
| 856 | $counter++; |
||
| 857 | } elseif ('pagebreak' === $row['type']) { |
||
| 858 | $counter++; |
||
| 859 | $pageBreakText[$counter] = $row['survey_question']; |
||
| 860 | } else { |
||
| 861 | $paged_questions_sec[$counter][] = $row['question_id']; |
||
| 862 | } |
||
| 863 | } |
||
| 864 | Session::write('page_questions_sec', $paged_questions_sec); |
||
| 865 | } else { |
||
| 866 | $paged_questions_sec = Session::read('page_questions_sec'); |
||
| 867 | } |
||
| 868 | |||
| 869 | $paged_questions = Session::read('paged_questions'); |
||
| 870 | if ('' == $shuffle) { |
||
| 871 | $shuffle = ' BY survey_question.sort, survey_question_option.sort ASC '; |
||
| 872 | } |
||
| 873 | $val = (int) $_POST['personality']; |
||
| 874 | if (is_array($paged_questions_sec)) { |
||
| 875 | $sql = "SELECT |
||
| 876 | survey_question.survey_group_sec1, |
||
| 877 | survey_question.survey_group_sec2, |
||
| 878 | survey_question.survey_group_pri, |
||
| 879 | survey_question.iid question_id, |
||
| 880 | survey_question.survey_id, |
||
| 881 | survey_question.survey_question, |
||
| 882 | survey_question.display, |
||
| 883 | survey_question.sort, |
||
| 884 | survey_question.type, |
||
| 885 | survey_question.max_value, |
||
| 886 | survey_question_option.question_option_id, |
||
| 887 | survey_question_option.option_text, |
||
| 888 | survey_question_option.sort as option_sort |
||
| 889 | FROM $table_survey_question survey_question |
||
| 890 | LEFT JOIN $table_survey_question_option survey_question_option |
||
| 891 | ON survey_question.iid = survey_question_option.question_id |
||
| 892 | WHERE survey_question NOT LIKE '%{{%' |
||
| 893 | AND survey_question.survey_id = '".$my_survey_id."' |
||
| 894 | AND survey_question.iid IN (".implode(',', $paged_questions_sec[$val]).") |
||
| 895 | ORDER $shuffle"; |
||
| 896 | $result = Database::query($sql); |
||
| 897 | $question_counter_max = Database::num_rows($result); |
||
| 898 | $counter = 0; |
||
| 899 | $limit = 0; |
||
| 900 | $questions = []; |
||
| 901 | while ($row = Database::fetch_assoc($result)) { |
||
| 902 | if ('pagebreak' !== $row['type']) { |
||
| 903 | $questions[$row['sort']]['question_id'] = $row['question_id']; |
||
| 904 | $questions[$row['sort']]['survey_id'] = $row['survey_id']; |
||
| 905 | $questions[$row['sort']]['survey_question'] = $row['survey_question']; |
||
| 906 | $questions[$row['sort']]['display'] = $row['display']; |
||
| 907 | $questions[$row['sort']]['type'] = $row['type']; |
||
| 908 | $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text']; |
||
| 909 | $questions[$row['sort']]['maximum_score'] = $row['max_value']; |
||
| 910 | $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1']; |
||
| 911 | $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2']; |
||
| 912 | $questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri']; |
||
| 913 | $questions[$row['sort']]['sort'] = $row['sort']; |
||
| 914 | } else { |
||
| 915 | break; |
||
| 916 | } |
||
| 917 | $counter++; |
||
| 918 | } |
||
| 919 | } else { |
||
| 920 | echo get_lang('Survey undefined'); |
||
| 921 | } |
||
| 922 | } else { |
||
| 923 | echo get_lang('Survey undefined'); |
||
| 924 | } |
||
| 925 | } else { |
||
| 926 | echo get_lang('Survey undefined'); |
||
| 927 | } |
||
| 928 | } else { |
||
| 929 | // First personality phase |
||
| 930 | Session::erase('page_questions_sec'); |
||
| 931 | $paged_questions_sec = []; |
||
| 932 | |||
| 933 | $order_sql = '' == $shuffle ? ' BY question_id ' : $shuffle; |
||
| 934 | |||
| 935 | if (empty($_SESSION['paged_questions'])) { |
||
| 936 | $sql = "SELECT * FROM $table_survey_question |
||
| 937 | WHERE survey_id = '".$surveyId."' |
||
| 938 | AND survey_group_sec1='0' |
||
| 939 | AND survey_group_sec2='0' |
||
| 940 | ORDER ".$order_sql.' '; |
||
| 941 | $result = Database::query($sql); |
||
| 942 | $counter = 0; |
||
| 943 | while ($row = Database::fetch_assoc($result)) { |
||
| 944 | if (1 == $survey->getOneQuestionPerPage()) { |
||
| 945 | $paged_questions[$counter][] = $row['question_id']; |
||
| 946 | $counter++; |
||
| 947 | } else { |
||
| 948 | if ('pagebreak' === $row['type']) { |
||
| 949 | $counter++; |
||
| 950 | $pageBreakText[$counter] = $row['survey_question']; |
||
| 951 | } else { |
||
| 952 | $paged_questions[$counter][] = $row['question_id']; |
||
| 953 | } |
||
| 954 | } |
||
| 955 | } |
||
| 956 | Session::write('paged_questions', $paged_questions); |
||
| 957 | } else { |
||
| 958 | $paged_questions = Session::read('paged_questions'); |
||
| 959 | } |
||
| 960 | |||
| 961 | $order_sql = '' == $shuffle ? ' BY survey_question.sort, survey_question_option.sort ASC ' : $shuffle; |
||
| 962 | $val = $_GET['show'] ?? ''; |
||
| 963 | $result = null; |
||
| 964 | if ('' != $val) { |
||
| 965 | $imploded = Database::escape_string(implode(',', $paged_questions[$val])); |
||
| 966 | if ('' != $imploded) { |
||
| 967 | $order_sql = ' BY survey_question.sort, survey_question_option.sort ASC '; |
||
| 968 | $sql = 'SELECT |
||
| 969 | survey_question.survey_group_sec1, |
||
| 970 | survey_question.survey_group_sec2, |
||
| 971 | survey_question.survey_group_pri, |
||
| 972 | survey_question.iid question_id, |
||
| 973 | survey_question.survey_id, |
||
| 974 | survey_question.survey_question, |
||
| 975 | survey_question.display, |
||
| 976 | survey_question.sort, |
||
| 977 | survey_question.type, |
||
| 978 | survey_question.max_value, |
||
| 979 | survey_question_option.question_option_id, |
||
| 980 | survey_question_option.option_text, |
||
| 981 | survey_question_option.sort as option_sort |
||
| 982 | '.($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')." |
||
| 983 | FROM $table_survey_question survey_question |
||
| 984 | LEFT JOIN $table_survey_question_option survey_question_option |
||
| 985 | ON survey_question.iid = survey_question_option.question_id |
||
| 986 | AND survey_question_option.c_id = $courseId |
||
| 987 | WHERE survey_question NOT LIKE '%{{%' |
||
| 988 | AND survey_question.survey_id = '".(int) $survey_invitation['survey_id']."' |
||
| 989 | AND survey_question.c_id = $courseId |
||
| 990 | AND survey_question.iid IN (".$imploded.") |
||
| 991 | ORDER $order_sql"; |
||
| 992 | $result = Database::query($sql); |
||
| 993 | $question_counter_max = Database::num_rows($result); |
||
| 994 | } |
||
| 995 | } |
||
| 996 | |||
| 997 | if (null !== $result) { |
||
| 998 | $counter = 0; |
||
| 999 | $limit = 0; |
||
| 1000 | $questions = []; |
||
| 1001 | while ($row = Database::fetch_assoc($result)) { |
||
| 1002 | if ('pagebreak' !== $row['type']) { |
||
| 1003 | $questions[$row['sort']]['question_id'] = $row['question_id']; |
||
| 1004 | $questions[$row['sort']]['survey_id'] = $row['survey_id']; |
||
| 1005 | $questions[$row['sort']]['survey_question'] = $row['survey_question']; |
||
| 1006 | $questions[$row['sort']]['display'] = $row['display']; |
||
| 1007 | $questions[$row['sort']]['type'] = $row['type']; |
||
| 1008 | $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text']; |
||
| 1009 | $questions[$row['sort']]['maximum_score'] = $row['max_value']; |
||
| 1010 | $questions[$row['sort']]['is_required'] = $allowRequiredSurveyQuestions && ($row['is_required'] ?? 0); |
||
| 1011 | $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1']; |
||
| 1012 | $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2']; |
||
| 1013 | $questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri']; |
||
| 1014 | $questions[$row['sort']]['sort'] = $row['sort']; |
||
| 1015 | } |
||
| 1016 | $counter++; |
||
| 1017 | } |
||
| 1018 | } |
||
| 1019 | } |
||
| 1020 | } else { |
||
| 1021 | echo get_lang('Survey type unknown'); |
||
| 1022 | } |
||
| 1023 | } |
||
| 1024 | |||
| 1025 | // ----------------------------------------------------------------------------- |
||
| 1026 | // Page counters / params |
||
| 1027 | // ----------------------------------------------------------------------------- |
||
| 1028 | $numberOfPages = SurveyManager::getCountPages($survey); |
||
| 1029 | |||
| 1030 | $show = 0; |
||
| 1031 | if (isset($_GET['show']) && '' != $_GET['show']) { |
||
| 1032 | $show = (int) $_GET['show'] + 1; |
||
| 1033 | } |
||
| 1034 | |||
| 1035 | $displayFinishButton = true; |
||
| 1036 | if (isset($_GET['show']) && '' != $_GET['show']) { |
||
| 1037 | $pagesIndexes = array_keys($paged_questions); |
||
| 1038 | $pagesIndexes[] = count($pagesIndexes); |
||
| 1039 | if (end($pagesIndexes) <= $show - 1 && empty($_POST)) { |
||
| 1040 | $displayFinishButton = false; |
||
| 1041 | } |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | $personality = isset($_POST['personality']) ? (int) $_POST['personality'] + 1 : 0; |
||
| 1045 | |||
| 1046 | $g_c = isset($_GET['course']) ? Security::remove_XSS($_GET['course']) : ''; |
||
| 1047 | $g_ic = isset($_GET['invitationcode']) ? Security::remove_XSS($_GET['invitationcode']) : ''; |
||
| 1048 | $g_cr = isset($_GET['cidReq']) ? Security::remove_XSS($_GET['cidReq']) : ''; |
||
| 1049 | $p_l = isset($_POST['language']) ? Security::remove_XSS($_POST['language']) : ''; |
||
| 1050 | |||
| 1051 | $add_parameters = isset($_GET['user_id']) ? '&user_id='.(int) $_GET['user_id'] : ''; |
||
| 1052 | |||
| 1053 | $url = api_get_self().'?'.api_get_cidreq().$add_parameters. |
||
| 1054 | '&course='.$g_c. |
||
| 1055 | '&invitationcode='.$g_ic. |
||
| 1056 | '&show='.$show. |
||
| 1057 | '&iid='.$surveyId; |
||
| 1058 | |||
| 1059 | if (!empty($_GET['language'])) { |
||
| 1060 | $lang = Security::remove_XSS($_GET['language']); |
||
| 1061 | $url .= '&language='.$lang; |
||
| 1062 | } |
||
| 1063 | if (!empty($lpItemId)) { |
||
| 1064 | $url .= '&lp_item_id='.$lpItemId; |
||
| 1065 | } |
||
| 1066 | |||
| 1067 | // ----------------------------------------------------------------------------- |
||
| 1068 | // Form |
||
| 1069 | // ----------------------------------------------------------------------------- |
||
| 1070 | $form = new FormValidator('question', 'post', $url, null, null, FormValidator::LAYOUT_HORIZONTAL); |
||
| 1071 | $form->addHidden('language', $p_l); |
||
| 1072 | |||
| 1073 | // Numbering control |
||
| 1074 | $showNumber = !SurveyManager::hasDependency($survey); |
||
| 1075 | |||
| 1076 | // ----------------------------------------------------------------------------- |
||
| 1077 | // Render questions (cards) |
||
| 1078 | // ----------------------------------------------------------------------------- |
||
| 1079 | if (isset($questions) && is_array($questions)) { |
||
| 1080 | $originalShow = isset($_GET['show']) ? (int) $_GET['show'] : 0; |
||
| 1081 | $questionCounter = 1; |
||
| 1082 | if (!empty($originalShow)) { |
||
| 1083 | $before = 0; |
||
| 1084 | foreach ($paged_questions as $keyQuestion => $list) { |
||
| 1085 | if ($originalShow > $keyQuestion) { |
||
| 1086 | $before += count($list); |
||
| 1087 | } |
||
| 1088 | } |
||
| 1089 | $questionCounter = $before + 1; |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | // Page-break caption |
||
| 1093 | $js = ''; |
||
| 1094 | if (isset($pageBreakText[$originalShow]) && !empty(strip_tags($pageBreakText[$originalShow]))) { |
||
| 1095 | $form->addHtml('<div class="mb-4 p-3 bg-gray-30 rounded">'.Security::remove_XSS($pageBreakText[$originalShow]).'</div>'); |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | foreach ($questions as $key => &$question) { |
||
| 1099 | $ch_type = 'ch_'.$question['type']; |
||
| 1100 | $questionNumber = $questionCounter; |
||
| 1101 | |||
| 1102 | // Use concrete question renderer; keep finalAnswer for prefill |
||
| 1103 | $display = new $ch_type(); |
||
| 1104 | $parent = $question['parent_id']; |
||
| 1105 | $parentClass = ''; |
||
| 1106 | |||
| 1107 | if (!empty($parent)) { |
||
| 1108 | $parentClass = ' with_parent with_parent_'.$question['question_id']; |
||
| 1109 | $parents = survey_question::getParents($question['question_id']); |
||
| 1110 | if (!empty($parents)) { |
||
| 1111 | foreach ($parents as $parentId) { |
||
| 1112 | $parentClass .= ' with_parent_only_hide_'.$parentId; |
||
| 1113 | } |
||
| 1114 | } |
||
| 1115 | } |
||
| 1116 | |||
| 1117 | $js .= survey_question::getQuestionJs($question); |
||
| 1118 | |||
| 1119 | $form->addHtml('<div class="survey_question '.$ch_type.' '.$parentClass.' mb-6 p-4 bg-gray-10 rounded-lg border border-gray-20">'); |
||
| 1120 | if ($showNumber && $survey->isDisplayQuestionNumber()) { |
||
| 1121 | $form->addHtml('<div class="font-semibold text-blue-700 mb-1"> '.$questionNumber.'. </div>'); |
||
| 1122 | } |
||
| 1123 | $form->addHtml('<div class="text-gray-800 mb-2">'.Security::remove_XSS($question['survey_question']).'</div>'); |
||
| 1124 | |||
| 1125 | // Prefill user answer if exists |
||
| 1126 | $userAnswerData = SurveyUtil::get_answers_of_question_by_user( |
||
| 1127 | $question['survey_id'], |
||
| 1128 | $question['question_id'], |
||
| 1129 | $lpItemId |
||
| 1130 | ); |
||
| 1131 | |||
| 1132 | $finalAnswer = null; |
||
| 1133 | if (!empty($userAnswerData[$answerUserKey])) { |
||
| 1134 | $userAnswer = $userAnswerData[$answerUserKey]; |
||
| 1135 | |||
| 1136 | switch ($question['type']) { |
||
| 1137 | case 'score': |
||
| 1138 | $finalAnswer = []; |
||
| 1139 | foreach ($userAnswer as $userChoice) { |
||
| 1140 | [$choiceId, $choiceValue] = explode('*', $userChoice); |
||
| 1141 | $finalAnswer[$choiceId] = $choiceValue; |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | break; |
||
| 1145 | |||
| 1146 | case 'percentage': |
||
| 1147 | [$choiceId, $choiceValue] = explode('*', current($userAnswer)); |
||
| 1148 | $finalAnswer = $choiceId; |
||
| 1149 | |||
| 1150 | break; |
||
| 1151 | |||
| 1152 | default: |
||
| 1153 | $finalAnswer = $userAnswer; |
||
| 1154 | |||
| 1155 | break; |
||
| 1156 | } |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | $display->render($form, $question, $finalAnswer); |
||
| 1160 | $form->addHtml('</div>'); |
||
| 1161 | $questionCounter++; |
||
| 1162 | } |
||
| 1163 | |||
| 1164 | $form->addHtml($js); |
||
| 1165 | } |
||
| 1166 | |||
| 1167 | // ----------------------------------------------------------------------------- |
||
| 1168 | // Navigation buttons (Previous / Next / Finish) |
||
| 1169 | // ----------------------------------------------------------------------------- |
||
| 1170 | $form->addHtml('<div class="flex justify-between mt-6 gap-3">'); |
||
| 1171 | if ( |
||
| 1172 | isset($_GET['show']) |
||
| 1173 | && $_GET['show'] > 0 |
||
| 1174 | && 'true' === api_get_setting('survey.survey_backwards_enable') |
||
| 1175 | && 1 === (int) $survey->getOneQuestionPerPage() |
||
| 1176 | ) { |
||
| 1177 | $currentShow = (int) $_GET['show']; |
||
| 1178 | $prevShow = max(0, $currentShow - 1); |
||
| 1179 | |||
| 1180 | $prevUrl = api_get_self().'?'.api_get_cidreq(). |
||
| 1181 | '&course='.urlencode($g_c). |
||
| 1182 | '&invitationcode='.urlencode($g_ic). |
||
| 1183 | '&iid='.$surveyId. |
||
| 1184 | '&show='.$prevShow; |
||
| 1185 | |||
| 1186 | if (!empty($_GET['language'])) { |
||
| 1187 | $prevUrl .= '&language='.urlencode($_GET['language']); |
||
| 1188 | } |
||
| 1189 | if (!empty($lpItemId)) { |
||
| 1190 | $prevUrl .= '&lp_item_id='.$lpItemId; |
||
| 1191 | } |
||
| 1192 | if (isset($_GET['user_id'])) { |
||
| 1193 | $prevUrl .= '&user_id='.(int) $_GET['user_id']; |
||
| 1194 | } |
||
| 1195 | |||
| 1196 | $form->addHtml( |
||
| 1197 | '<a href="'.$prevUrl.'" class="btn btn--plain-outline"> |
||
| 1198 | <i class="mdi mdi-arrow-left mr-2"></i>'.get_lang('Previous question').' |
||
| 1199 | </a>' |
||
| 1200 | ); |
||
| 1201 | } |
||
| 1202 | |||
| 1203 | // Next / Start |
||
| 1204 | if ('0' == (string) $survey->getSurveyType()) { |
||
| 1205 | if (0 == $survey->getShowFormProfile()) { |
||
| 1206 | if ($show < $numberOfPages) { |
||
| 1207 | $label = 0 == $show ? get_lang('Start the Survey') : get_lang('Next question'); |
||
| 1208 | $form->addButton('next_survey_page', $label, 'arrow-right', 'success'); |
||
| 1209 | } |
||
| 1210 | if ($show >= $numberOfPages && $displayFinishButton) { |
||
| 1211 | $form->addButton('finish_survey', get_lang('Finish survey'), 'check', 'success'); |
||
| 1212 | } |
||
| 1213 | } else { |
||
| 1214 | if (isset($_GET['show'])) { |
||
| 1215 | $pageCount = count($paged_questions); |
||
| 1216 | if ($show < $pageCount) { |
||
| 1217 | $label = 0 == $show ? get_lang('Start the Survey') : get_lang('Next question'); |
||
| 1218 | $form->addButton('next_survey_page', $label, 'arrow-right', 'success'); |
||
| 1219 | } |
||
| 1220 | if ($show >= $pageCount && $displayFinishButton) { |
||
| 1221 | $form->addButton('finish_survey', get_lang('Finish survey'), 'check', 'success'); |
||
| 1222 | } |
||
| 1223 | } |
||
| 1224 | } |
||
| 1225 | } elseif (1 === $survey->getSurveyType()) { |
||
| 1226 | if (isset($_GET['show']) || isset($_POST['personality'])) { |
||
| 1227 | $pageCount = count($paged_questions); |
||
| 1228 | if (!empty($paged_questions_sec) && count($paged_questions_sec) > 0) { |
||
| 1229 | $pageCount += count($paged_questions_sec); |
||
| 1230 | } else { |
||
| 1231 | Session::erase('page_questions_sec'); |
||
| 1232 | $paged_questions_sec = []; |
||
| 1233 | } |
||
| 1234 | |||
| 1235 | if (0 === $personality) { |
||
| 1236 | if (($show <= $pageCount) || !$_GET['show']) { |
||
| 1237 | $form->addButton('next_survey_page', get_lang('Next'), 'arrow-right', 'success'); |
||
| 1238 | if (0 == $survey->getOneQuestionPerPage()) { |
||
| 1239 | if ($personality >= 0) { |
||
| 1240 | $form->addHidden('personality', $personality); |
||
| 1241 | } |
||
| 1242 | } else { |
||
| 1243 | if ($personality > 0) { |
||
| 1244 | $form->addHidden('personality', $personality); |
||
| 1245 | } |
||
| 1246 | } |
||
| 1247 | if ($pageCount == $show) { |
||
| 1248 | $form->addHidden('personality', $personality); |
||
| 1249 | } |
||
| 1250 | } |
||
| 1251 | } |
||
| 1252 | |||
| 1253 | if ($show > $pageCount && $_GET['show'] && 0 === $personality) { |
||
| 1254 | $form->addHidden('personality', $personality); |
||
| 1255 | } elseif ($personality > 0) { |
||
| 1256 | if (1 == $survey->getOneQuestionPerPage()) { |
||
| 1257 | if ($show >= $pageCount) { |
||
| 1258 | $form->addButton('finish_survey', get_lang('Finish survey'), 'check', 'success'); |
||
| 1259 | } else { |
||
| 1260 | $form->addHidden('personality', $personality); |
||
| 1261 | $form->addButton('next_survey_page', get_lang('Next'), 'arrow-right', 'success'); |
||
| 1262 | } |
||
| 1263 | } else { |
||
| 1264 | $form->addButton('finish_survey', get_lang('Finish survey'), 'check'); |
||
| 1265 | } |
||
| 1266 | } |
||
| 1267 | } elseif ('' == $survey->getFormFields() || !is_array($user_data)) { |
||
| 1268 | $form->addButton('next_survey_page', get_lang('Next'), 'arrow-right', 'success'); |
||
| 1269 | } |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | $form->addHtml('</div>'); |
||
| 1273 | |||
| 1274 | // No-questions notice at end |
||
| 1275 | if (isset($_GET['show']) && ($show >= $numberOfPages || empty($questions))) { |
||
| 1276 | if (false == $questions_exists) { |
||
| 1277 | echo '<p class="text-gray-600">'.get_lang('There are no questions for this survey').'</p>'; |
||
| 1278 | } |
||
| 1279 | } |
||
| 1280 | |||
| 1281 | // ----------------------------------------------------------------------------- |
||
| 1282 | // Render form + close container + footer |
||
| 1283 | // ----------------------------------------------------------------------------- |
||
| 1284 | $form->display(); |
||
| 1285 | echo '</div>'; |
||
| 1286 | Display::display_footer(); |
||
| 1287 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.