Issues (2128)

main/survey/fillsurvey.php (3 issues)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use ChamiloSession as Session;
6
7
$lastQuestion = 0;
8
9
/**
10
 * @author unknown, the initial survey that did not make it in 1.8 because of bad code
11
 * @author Patrick Cool <[email protected]>, Ghent University: cleanup,
12
 * refactoring and rewriting large parts of the code
13
 * @author Julio Montoya <[email protected]>, Chamilo: Personality Test
14
 * modification and rewriting large parts of the code as well
15
 *
16
 * @todo check if the user already filled the survey and if this
17
 * is the case then the answers have to be updated and not stored again.
18
 * @todo performance could be improved if not the survey_id was
19
 * stored with the invitation but the survey_code
20
 */
21
22
// Unsetting the course id (because it is in the URL)
23
if (!isset($_GET['cidReq'])) {
24
    $cidReset = true;
25
} else {
26
    $_cid = $_GET['cidReq'];
27
}
28
29
require_once __DIR__.'/../inc/global.inc.php';
30
31
// Database table definitions
32
$table_survey = Database::get_course_table(TABLE_SURVEY);
33
$table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER);
34
$table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
35
$table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
36
$table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
37
$table_user = Database::get_main_table(TABLE_MAIN_USER);
38
39
$allowRequiredSurveyQuestions = api_get_configuration_value('allow_required_survey_questions');
40
41
// Check if user is anonymous or not
42
$isAnonymous = false;
43
if (api_is_anonymous(api_get_user_id(), true)) {
44
    $isAnonymous = true;
45
}
46
47
// getting all the course information
48
if (isset($_GET['course'])) {
49
    $courseInfo = api_get_course_info($_GET['course']);
50
} else {
51
    $courseInfo = api_get_course_info();
52
}
53
54
if (empty($courseInfo)) {
55
    api_not_allowed(true);
56
}
57
58
$userInfo = api_get_user_info();
59
$sessionId = isset($_GET['id_session']) ? (int) $_GET['id_session'] : api_get_session_id();
60
$lpItemId = isset($_GET['lp_item_id']) ? (int) $_GET['lp_item_id'] : 0;
61
$allowSurveyInLp = api_get_configuration_value('allow_survey_tool_in_lp');
62
63
// Breadcrumbs
64
if (!empty($userInfo)) {
65
    $interbreadcrumb[] = [
66
        'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?cidReq='.$courseInfo['code'].'&id_session='.$sessionId,
67
        'name' => get_lang('SurveyList'),
68
    ];
69
}
70
71
$course_id = $courseInfo['real_id'];
72
$surveyCode = isset($_GET['scode']) ? Database::escape_string($_GET['scode']) : '';
73
74
if ($surveyCode != '') {
75
    // Firstly we check if this survey is ready for anonymous use:
76
    $sql = "SELECT anonymous FROM $table_survey
77
            WHERE c_id = $course_id AND code ='$surveyCode'";
78
    $resultAnonymous = Database::query($sql);
79
    $rowAnonymous = Database::fetch_array($resultAnonymous, 'ASSOC');
80
    // If is anonymous and is not allowed to take the survey to anonymous users, forbid access:
81
    if (!isset($rowAnonymous['anonymous']) ||
82
        ($rowAnonymous['anonymous'] == 0 && api_is_anonymous()) ||
83
        count($rowAnonymous) == 0
84
    ) {
85
        api_not_allowed(true);
86
    }
87
    // If is anonymous and it is allowed to take the survey as anonymous, mark survey as anonymous.
88
}
89
90
// First we check if the needed parameters are present
91
if ((!isset($_GET['course']) || !isset($_GET['invitationcode'])) && !isset($_GET['user_id'])) {
92
    api_not_allowed(true, get_lang('SurveyParametersMissingUseCopyPaste'));
93
}
94
95
$invitationcode = $_GET['invitationcode'];
96
$lpItemCondition = '';
97
if ($allowSurveyInLp) {
98
    $lpItemCondition = " AND c_lp_item_id = $lpItemId";
99
}
100
101
$sessionCondition = '';
102
if (true === api_get_configuration_value('show_surveys_base_in_sessions')) {
103
    $sessionCondition = api_get_session_condition($sessionId);
104
}
105
106
// Start auto-invitation feature FS#3403 (all-users-can-do-the-survey-URL handling)
107
if ('auto' === $invitationcode && isset($_GET['scode'])) {
108
    $userid = api_get_user_id();
109
    // Survey_code of the survey
110
    $surveyCode = $_GET['scode'];
111
    if ($isAnonymous) {
112
        $autoInvitationcode = 'auto-ANONY_'.md5(time())."-$surveyCode";
113
    } else {
114
        $invitations = SurveyManager::getUserInvitationsForSurveyInCourse(
115
            $userid,
116
            $surveyCode,
117
            $courseInfo['real_id'],
118
            $sessionId,
119
            $lpItemId
120
        );
121
        $lastInvitation = current($invitations);
122
123
        if (!$lastInvitation) {
124
            // New invitation code from userid
125
            $autoInvitationcode = "auto-$userid-$surveyCode";
126
        } else {
127
            $autoInvitationcode = $lastInvitation->getInvitationCode();
128
        }
129
    }
130
131
    // The survey code must exist in this course, or the URL is invalid
132
    $sql = "SELECT * FROM $table_survey
133
            WHERE c_id = $course_id AND code = '".Database::escape_string($surveyCode)."'";
134
    $result = Database::query($sql);
135
    if (Database::num_rows($result) > 0) {
136
        // Check availability
137
        $row = Database::fetch_array($result, 'ASSOC');
138
        $tempdata = SurveyManager::get_survey($row['survey_id']);
139
        SurveyManager::checkTimeAvailability($tempdata);
140
        // Check for double invitation records (insert should be done once)
141
        $sql = "SELECT user
142
                FROM $table_survey_invitation
143
                WHERE
144
                    c_id = $course_id AND
145
                    invitation_code = '".Database::escape_string($autoInvitationcode)."'
146
                    $sessionCondition
147
                    $lpItemCondition";
148
        $result = Database::query($sql);
149
        $now = api_get_utc_datetime();
150
        if (0 == Database::num_rows($result)) {
151
            $params = [
152
                'c_id' => $course_id,
153
                'survey_code' => $surveyCode,
154
                'user' => $userid,
155
                'invitation_code' => $autoInvitationcode,
156
                'invitation_date' => $now,
157
                'session_id' => $sessionId,
158
            ];
159
            if ($allowSurveyInLp) {
160
                $params['c_lp_item_id'] = $lpItemId;
161
            }
162
            Database::insert($table_survey_invitation, $params);
163
        }
164
        // From here we use the new invitationcode auto-userid-surveycode string
165
        $_GET['invitationcode'] = $autoInvitationcode;
166
        Session::write('auto_invitation_code_'.$surveyCode, $autoInvitationcode);
167
        $invitationcode = $autoInvitationcode;
168
    }
169
}
170
171
// Now we check if the invitation code is valid
172
$sql = "SELECT * FROM $table_survey_invitation
173
        WHERE
174
            c_id = $course_id AND
175
            invitation_code = '".Database::escape_string($invitationcode)."'
176
            $sessionCondition
177
            $lpItemCondition";
178
$result = Database::query($sql);
179
if (Database::num_rows($result) < 1) {
180
    api_not_allowed(true, get_lang('WrongInvitationCode'));
181
}
182
183
$survey_invitation = Database::fetch_array($result, 'ASSOC');
184
$surveyUserFromSession = Session::read('surveyuser');
185
// Now we check if the user already filled the survey
186
if (!isset($_POST['finish_survey']) &&
0 ignored issues
show
Consider adding parentheses for clarity. Current Interpretation: (! IssetNode && $isAnony...d'] == 1 && ! IssetNode, Probably Intended Meaning: ! IssetNode && ($isAnony...'] == 1 && ! IssetNode)
Loading history...
187
    (
188
        $isAnonymous &&
189
        !empty($surveyUserFromSession) &&
190
        SurveyUtil::isSurveyAnsweredFlagged($survey_invitation['survey_code'], $survey_invitation['c_id'])
191
    ) ||
192
    ($survey_invitation['answered'] == 1 && !isset($_GET['user_id']))
193
) {
194
    api_not_allowed(true, Display::return_message(get_lang('YouAlreadyFilledThisSurvey')));
195
}
196
197
$logInfo = [
198
    'tool' => TOOL_SURVEY,
199
    'tool_id' => $survey_invitation['survey_invitation_id'],
200
    'action' => 'invitationcode',
201
    'action_details' => $invitationcode,
202
];
203
Event::registerLog($logInfo);
0 ignored issues
show
The method registerLog() does not exist on Event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

203
Event::/** @scrutinizer ignore-call */ 
204
       registerLog($logInfo);

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.

Loading history...
204
205
// Checking if there is another survey with this code.
206
// If this is the case there will be a language choice
207
$sql = "SELECT * FROM $table_survey
208
        WHERE
209
            c_id = $course_id AND
210
            code = '".Database::escape_string($survey_invitation['survey_code'])."'";
211
if (true === api_get_configuration_value('show_surveys_base_in_sessions')) {
212
    // It lists the surveys base too
213
    $sql .= api_get_session_condition($sessionId, true, true);
214
} else {
215
    $sql .= api_get_session_condition($sessionId);
216
}
217
$result = Database::query($sql);
218
219
if (Database::num_rows($result) > 1) {
220
    if ($_POST['language']) {
221
        $survey_invitation['survey_id'] = $_POST['language'];
222
    } else {
223
        Display::display_header(get_lang('ToolSurvey'));
224
        $frmLangUrl = api_get_self().'?'.api_get_cidreq().'&'
225
            .http_build_query([
226
                'course' => Security::remove_XSS($_GET['course']),
227
                'invitationcode' => Security::remove_XSS($_GET['invitationcode']),
228
            ]);
229
230
        echo '<form id="language" name="language" method="POST" action="'.$frmLangUrl.'">';
231
        echo '<select name="language">';
232
        while ($row = Database::fetch_array($result, 'ASSOC')) {
233
            echo '<option value="'.$row['survey_id'].'">'.$row['lang'].'</option>';
234
        }
235
        echo '</select>';
236
        echo '<button type="submit" name="Submit" class="next">'.get_lang('Ok').'</button>';
237
        echo '</form>';
238
        Display::display_footer();
239
        exit();
240
    }
241
} else {
242
    $row = Database::fetch_array($result, 'ASSOC');
243
    $survey_invitation['survey_id'] = $row['survey_id'];
244
}
245
246
// Getting the survey information
247
$survey_data = SurveyManager::get_survey($survey_invitation['survey_id']);
248
if (empty($survey_data)) {
249
    api_not_allowed(true);
250
}
251
252
// Checking time availability
253
SurveyManager::checkTimeAvailability($survey_data);
254
$survey_data['survey_id'] = $survey_invitation['survey_id'];
255
256
if ($survey_data['survey_type'] === '3') {
257
    header('Location: '.
258
        api_get_path(WEB_CODE_PATH).
259
        'survey/meeting.php?cidReq='.$courseInfo['code'].'&id_session='.$sessionId.'&invitationcode='.Security::remove_XSS($invitationcode)
260
    );
261
    exit;
262
}
263
264
if (!empty($survey_data['anonymous'])) {
265
    define('USER_IN_ANON_SURVEY', true);
266
}
267
268
// Storing the answers
269
if (count($_POST) > 0) {
270
    if ($survey_data['survey_type'] === '0') {
271
        $types = [];
272
        $required = [];
273
        // Getting all the types of the question
274
        // (because of the special treatment of the score question type
275
        $sql = "SELECT * FROM $table_survey_question
276
                WHERE
277
                    c_id = $course_id AND
278
                    survey_id = '".intval($survey_invitation['survey_id'])."'";
279
        $result = Database::query($sql);
280
281
        while ($row = Database::fetch_array($result, 'ASSOC')) {
282
            $types[$row['question_id']] = $row['type'];
283
            $required[$row['question_id']] = $allowRequiredSurveyQuestions && $row['is_required'];
284
        }
285
286
        // Looping through all the post values
287
        foreach ($_POST as $key => &$value) {
288
            // If the post value key contains the string 'question' then it is an answer on a question
289
            if (strpos($key, 'other_question') === false &&
290
                strpos($key, 'question') !== false && $key !== '_qf__question'
291
            ) {
292
                // Finding the question id by removing 'question'
293
                $survey_question_id = str_replace('question', '', $key);
294
                // If not question ID was defined, we're on the start
295
                // screen or something else that doesn't require
296
                // saving an answer
297
                if (empty($survey_question_id)) {
298
                    continue;
299
                }
300
301
                $other = isset($_POST['other_question'.$survey_question_id]) ? $_POST['other_question'.$survey_question_id] : '';
302
303
                /* If the post value is an array then we have a multiple response question or a scoring question type
304
                remark: when it is a multiple response then the value of the array is the option_id
305
                when it is a scoring question then the key of the array is the option_id and the value is the value
306
                */
307
                if (is_array($value)) {
308
                    SurveyUtil::remove_answer(
309
                        $survey_invitation['user'],
310
                        $survey_invitation['survey_id'],
311
                        $survey_question_id,
312
                        $course_id,
313
                        $sessionId,
314
                        $lpItemId
315
                    );
316
317
                    foreach ($value as $answer_key => &$answer_value) {
318
                        if ('score' == $types[$survey_question_id]) {
319
                            $option_id = $answer_key;
320
                            $option_value = $answer_value;
321
                        } else {
322
                            $option_id = $answer_value;
323
                            $option_value = '';
324
                        }
325
326
                        SurveyUtil::store_answer(
327
                            $survey_invitation['user'],
328
                            $survey_invitation['survey_id'],
329
                            $survey_question_id,
330
                            $option_id,
331
                            $option_value,
332
                            $survey_data,
333
                            '',
334
                            $sessionId,
335
                            $lpItemId
336
                        );
337
                    }
338
                } else {
339
                    // All the other question types (open question, multiple choice, percentage, ...)
340
                    if (isset($types[$survey_question_id]) &&
341
                        'percentage' === $types[$survey_question_id]) {
342
                        $sql = "SELECT * FROM $table_survey_question_option
343
                                WHERE
344
                                    c_id = $course_id AND
345
                                    question_option_id='".intval($value)."'";
346
                        $result = Database::query($sql);
347
                        $row = Database::fetch_array($result, 'ASSOC');
348
                        $option_value = $row['option_text'];
349
                    } else {
350
                        $option_value = 0;
351
                        if (isset($types[$survey_question_id]) &&
352
                            'open' === $types[$survey_question_id]
353
                        ) {
354
                            $option_value = $value;
355
                        }
356
                    }
357
358
                    $survey_question_answer = $value;
359
                    SurveyUtil::remove_answer(
360
                        $survey_invitation['user'],
361
                        $survey_invitation['survey_id'],
362
                        $survey_question_id,
363
                        $course_id,
364
                        $sessionId,
365
                        $lpItemId
366
                    );
367
368
                    SurveyUtil::store_answer(
369
                        $survey_invitation['user'],
370
                        $survey_invitation['survey_id'],
371
                        $survey_question_id,
372
                        $value,
373
                        $option_value,
374
                        $survey_data,
375
                        $other,
376
                        $sessionId,
377
                        $lpItemId
378
                    );
379
                }
380
            }
381
        }
382
    } elseif ($survey_data['survey_type'] === '1') {
383
        //conditional/personality-test type surveys
384
        // Getting all the types of the question (because of the special treatment of the score question type
385
        $shuffle = '';
386
        if ($survey_data['shuffle'] == '1') {
387
            $shuffle = ' ORDER BY RAND() ';
388
        }
389
        $sql = "SELECT * FROM $table_survey_question
390
                WHERE
391
                    c_id = $course_id AND
392
                    survey_id = '".intval($survey_invitation['survey_id'])."' AND
393
                    survey_group_pri = '0' $shuffle";
394
        $result = Database::query($sql);
395
        // There is only one question type for conditional surveys
396
        while ($row = Database::fetch_array($result, 'ASSOC')) {
397
            $types[$row['question_id']] = $row['type'];
398
        }
399
400
        // Looping through all the post values
401
        foreach ($_POST as $key => &$value) {
402
            // If the post value key contains the string 'question' then it is an answer to a question
403
            if (false !== strpos($key, 'question')) {
404
                // Finding the question id by removing 'question'
405
                $survey_question_id = str_replace('question', '', $key);
406
                // If not question ID was defined, we're on the start
407
                // screen or something else that doesn't require
408
                // saving an answer
409
                if (empty($survey_question_id)) {
410
                    continue;
411
                }
412
                // We select the correct answer and the puntuacion
413
                $sql = "SELECT value FROM $table_survey_question_option
414
                        WHERE c_id = $course_id AND question_option_id='".intval($value)."'";
415
                $result = Database::query($sql);
416
                $row = Database::fetch_array($result, 'ASSOC');
417
                $option_value = $row['value'];
418
                $survey_question_answer = $value;
419
420
                // We save the answer after making sure that a possible previous attempt is deleted
421
                SurveyUtil::remove_answer(
422
                    $survey_invitation['user'],
423
                    $survey_invitation['survey_id'],
424
                    $survey_question_id,
425
                    $course_id,
426
                    $sessionId,
427
                    $lpItemId
428
                );
429
430
                SurveyUtil::store_answer(
431
                    $survey_invitation['user'],
432
                    $survey_invitation['survey_id'],
433
                    $survey_question_id,
434
                    $value,
435
                    $option_value,
436
                    $survey_data,
437
                    '',
438
                    $sessionId,
439
                    $lpItemId
440
                );
441
            }
442
        }
443
    } else {
444
        // In case it's another type than 0 or 1
445
        api_not_allowed(true, get_lang('ErrorSurveyTypeUnknown'));
446
    }
447
}
448
449
$user_id = api_get_user_id();
450
if ($user_id == 0) {
451
    $user_id = $survey_invitation['user'];
452
}
453
$user_data = api_get_user_info($user_id);
454
455
if ($survey_data['form_fields'] != '' &&
456
    $survey_data['anonymous'] == 0 &&
457
    is_array($user_data)
458
) {
459
    $form_fields = explode('@', $survey_data['form_fields']);
460
    $list = [];
461
    foreach ($form_fields as $field) {
462
        $field_value = explode(':', $field);
463
        if (isset($field_value[1]) && $field_value[1] == 1) {
464
            if ($field_value[0] != '') {
465
                $val = api_substr($field_value[0], 8, api_strlen($field_value[0]));
466
                $list[$val] = 1;
467
            }
468
        }
469
    }
470
471
    $url = api_get_self().
472
        '?cidReq='.$courseInfo['code'].
473
        '&id_session='.$sessionId;
474
    $listQueryParams = preg_split('/&/', $_SERVER['QUERY_STRING']);
475
    foreach ($listQueryParams as $param) {
476
        $url .= '&'.Security::remove_XSS($param);
477
    }
478
    if (!empty($lpItemId) && $allowSurveyInLp) {
479
        $url .= '&lp_item_id='.$lpItemId.'&origin=learnpath';
480
    }
481
482
    // We use the same form as in auth/profile.php
483
    $form = new FormValidator('profile', 'post', $url);
484
    if (api_is_western_name_order()) {
485
        if (isset($list['firstname']) && $list['firstname'] == 1) {
486
            //FIRST NAME
487
            $form->addElement('text', 'firstname', get_lang('FirstName'), ['size' => 40]);
488
            if (api_get_setting('profile', 'name') !== 'true') {
489
                $form->freeze(['firstname']);
490
            }
491
            $form->applyFilter(['firstname'], 'stripslashes');
492
            $form->applyFilter(['firstname'], 'trim');
493
            $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
494
        }
495
        if (isset($list['lastname']) && $list['lastname'] == 1) {
496
            //    LAST NAME
497
            $form->addElement('text', 'lastname', get_lang('LastName'), ['size' => 40]);
498
            if (api_get_setting('profile', 'name') !== 'true') {
499
                $form->freeze(['lastname']);
500
            }
501
            $form->applyFilter(['lastname'], 'stripslashes');
502
            $form->applyFilter(['lastname'], 'trim');
503
            $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
504
        }
505
    } else {
506
        if (isset($list['lastname']) && $list['lastname'] == 1) {
507
            //    LAST NAME
508
            $form->addElement('text', 'lastname', get_lang('LastName'), ['size' => 40]);
509
            if (api_get_setting('profile', 'name') !== 'true') {
510
                $form->freeze(['lastname']);
511
            }
512
            $form->applyFilter(['lastname'], 'stripslashes');
513
            $form->applyFilter(['lastname'], 'trim');
514
            $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
515
        }
516
        if (isset($list['firstname']) && $list['firstname'] == 1) {
517
            //FIRST NAME
518
            $form->addElement('text', 'firstname', get_lang('FirstName'), ['size' => 40]);
519
            if (api_get_setting('profile', 'name') !== 'true') {
520
                $form->freeze(['firstname']);
521
            }
522
            $form->applyFilter(['firstname'], 'stripslashes');
523
            $form->applyFilter(['firstname'], 'trim');
524
            $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
525
        }
526
    }
527
528
    if (isset($list['official_code']) && $list['official_code'] == 1) {
529
        // OFFICIAL CODE
530
        if (CONFVAL_ASK_FOR_OFFICIAL_CODE) {
0 ignored issues
show
The constant CONFVAL_ASK_FOR_OFFICIAL_CODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
531
            $form->addElement('text', 'official_code', get_lang('OfficialCode'), ['size' => 40]);
532
            if (api_get_setting('profile', 'officialcode') !== 'true') {
533
                $form->freeze('official_code');
534
            }
535
            $form->applyFilter('official_code', 'stripslashes');
536
            $form->applyFilter('official_code', 'trim');
537
            if (api_get_setting('registration', 'officialcode') === 'true' &&
538
                api_get_setting('profile', 'officialcode') === 'true'
539
            ) {
540
                $form->addRule('official_code', get_lang('ThisFieldIsRequired'), 'required');
541
            }
542
        }
543
    }
544
545
    if (isset($list['email']) && $list['email'] == 1) {
546
        //    EMAIL
547
        $form->addElement('text', 'email', get_lang('Email'), ['size' => 40]);
548
        if (api_get_setting('profile', 'email') !== 'true') {
549
            $form->freeze('email');
550
        }
551
        $form->applyFilter('email', 'stripslashes');
552
        $form->applyFilter('email', 'trim');
553
        if (api_get_setting('registration', 'email') === 'true') {
554
            $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
555
        }
556
        $form->addRule('email', get_lang('EmailWrong'), 'email');
557
    }
558
559
    if (isset($list['phone']) && $list['phone'] == 1) {
560
        // PHONE
561
        $form->addElement('text', 'phone', get_lang('Phone'), ['size' => 20]);
562
        if (api_get_setting('profile', 'phone') !== 'true') {
563
            $form->freeze('phone');
564
        }
565
        $form->applyFilter('phone', 'stripslashes');
566
        $form->applyFilter('phone', 'trim');
567
        if (api_get_setting('profile', 'phone') === 'true') {
568
            $form->addRule('phone', get_lang('ThisFieldIsRequired'), 'required');
569
        }
570
    }
571
572
    if (isset($list['language']) && $list['language'] == 1) {
573
        // LANGUAGE
574
        $form->addSelectLanguage('language', get_lang('Language'));
575
        if (api_get_setting('profile', 'language') !== 'true') {
576
            $form->freeze('language');
577
        }
578
        if (api_get_setting('profile', 'language') === 'true') {
579
            $form->addRule('language', get_lang('ThisFieldIsRequired'), 'required');
580
        }
581
    }
582
583
    // EXTRA FIELDS
584
    $extraField = new ExtraField('user');
585
    $returnParams = $extraField->addElements($form, api_get_user_id());
586
    $jquery_ready_content = $returnParams['jquery_ready_content'];
587
588
    // the $jquery_ready_content variable collects all functions
589
    // that will be load in the $(document).ready javascript function
590
    $htmlHeadXtra[] = '<script>
591
    $(function() {
592
        '.$jquery_ready_content.'
593
    });
594
    </script>';
595
596
    $form->addButtonNext(get_lang('Next'));
597
    $form->setDefaults($user_data);
598
}
599
600
$htmlHeadXtra[] = '<script>'.api_get_language_translate_html().'</script>';
601
$htmlHeadXtra[] = ch_selectivedisplay::getJs();
602
$htmlHeadXtra[] = survey_question::getJs();
603
604
Display::display_header(get_lang('ToolSurvey'));
605
echo '<div class="survey-block">';
606
echo '<div class="page-header">';
607
echo '<h2>';
608
echo Security::remove_XSS($survey_data['survey_title']).'</h2></div>';
609
if (!empty($survey_data['survey_subtitle'])) {
610
    echo '<div class="survey_subtitle"><p>'.Security::remove_XSS($survey_data['survey_subtitle']).'</p></div>';
611
}
612
613
// Displaying the survey introduction
614
if (
615
    !isset($_GET['show']) ||
616
    (isset($_GET['show'])) && $_GET['show'] == '') {
617
    // The first thing we do is delete the session
618
    Session::erase('paged_questions');
619
    Session::erase('page_questions_sec');
620
621
    $paged_questions_sec = [];
622
    if (!empty($survey_data['survey_introduction'])) {
623
        echo '<div class="survey_content">'.Security::remove_XSS($survey_data['survey_introduction']).'</div>';
624
    }
625
    $limit = 0;
626
}
627
628
if ($survey_data['form_fields'] &&
629
    $survey_data['anonymous'] == 0 &&
630
    is_array($user_data) &&
631
    !isset($_GET['show'])
632
) {
633
    if ($form->validate()) {
634
        $user_data = $form->exportValues();
635
        if (is_array($user_data)) {
636
            if (count($user_data) > 0) {
637
                $extras = [];
638
                // Build SQL query
639
                $sql = "UPDATE $table_user SET";
640
                $update = false;
641
                $allowedFields = [
642
                    'firstname',
643
                    'lastname',
644
                    'official_code',
645
                    'email',
646
                    'phone',
647
                    'language',
648
                ];
649
650
                foreach ($user_data as $key => $value) {
651
                    if (in_array($key, $allowedFields)) {
652
                        $sql .= " $key = '".Database::escape_string($value)."',";
653
                        $update = true;
654
                    }
655
                }
656
                // Remove trailing , from the query we have so far
657
                $sql = rtrim($sql, ',');
658
                $sql .= " WHERE id  = $user_id";
659
660
                if ($update) {
661
                    Database::query($sql);
662
                }
663
664
                $extraFieldValue = new ExtraFieldValue('user');
665
                $extraFieldValue->saveFieldValues($user_data);
666
667
                echo '<div id="survey_content" class="survey_content">'.
668
                    get_lang('InformationUpdated').' '.get_lang('PleaseFillSurvey').'</div>';
669
            }
670
        }
671
        $_GET['show'] = 0;
672
        $show = 0;
673
        // We unset the sessions
674
        Session::erase('paged_questions');
675
        Session::erase('page_questions_sec');
676
        $paged_questions_sec = [];
677
    } else {
678
        echo '<div id="survey_content" class="survey_content">'.get_lang('UpdateInformation').'</div>';
679
        // We unset the sessions
680
        Session::erase('paged_questions');
681
        Session::erase('page_questions_sec');
682
        $paged_questions_sec = [];
683
        $form->display();
684
    }
685
}
686
687
// Displaying the survey thanks message
688
if (isset($_POST['finish_survey'])) {
689
    echo Display::return_message(get_lang('SurveyFinished'), 'confirm');
690
    echo Security::remove_XSS($survey_data['survey_thanks']);
691
692
    SurveyManager::update_survey_answered(
693
        $survey_data,
694
        $survey_invitation['user'],
695
        $survey_invitation['survey_code'],
696
        $lpItemId
697
    );
698
699
    SurveyUtil::flagSurveyAsAnswered(
700
        $survey_invitation['survey_code'],
701
        $survey_invitation['c_id']
702
    );
703
704
    if ($courseInfo && !api_is_anonymous() && empty($lpItemId)) {
705
        echo '<br /><br />';
706
        echo Display::toolbarButton(
707
            get_lang('ReturnToCourseHomepage'),
708
            api_get_course_url($courseInfo['code']),
709
            'home'
710
        );
711
    }
712
713
    Session::erase('paged_questions');
714
    Session::erase('page_questions_sec');
715
    Session::erase('auto_invitation_code_'.$survey_data['code']);
716
    Display::display_footer();
717
    exit();
718
}
719
720
// Sets the random questions
721
$shuffle = '';
722
if (1 == $survey_data['shuffle']) {
723
    $shuffle = ' BY RAND() ';
724
}
725
726
$pageBreakText = [];
727
if ((isset($_GET['show']) && $_GET['show'] != '') ||
728
    isset($_POST['personality'])
729
) {
730
    // Getting all the questions for this page and add them to a
731
    // multidimensional array where the first index is the page.
732
    // As long as there is no pagebreak fount we keep adding questions to the page
733
    $questions_displayed = [];
734
    $counter = 0;
735
    $paged_questions = [];
736
737
    $select = '';
738
    if (true === api_get_configuration_value('survey_question_dependency')) {
739
        $select = ' survey_question.parent_id, survey_question.parent_option_id, ';
740
    }
741
742
    // If non-conditional survey
743
    if ($survey_data['survey_type'] == '0') {
744
        if (empty($paged_questions)) {
745
            $sql = "SELECT * FROM $table_survey_question
746
                    WHERE
747
                        survey_question NOT LIKE '%{{%' AND
748
                        c_id = $course_id AND
749
                        survey_id = '".intval($survey_invitation['survey_id'])."'
750
                    ORDER BY sort ASC";
751
            $result = Database::query($sql);
752
            while ($row = Database::fetch_array($result, 'ASSOC')) {
753
                if ($survey_data['one_question_per_page'] == 1) {
754
                    if ($row['type'] !== 'pagebreak') {
755
                        $paged_questions[$counter][] = $row['question_id'];
756
                        $counter++;
757
                        continue;
758
                    }
759
                } else {
760
                    if ($row['type'] === 'pagebreak') {
761
                        $counter++;
762
                        $pageBreakText[$counter] = $row['survey_question'];
763
                    } else {
764
                        $paged_questions[$counter][] = $row['question_id'];
765
                    }
766
                }
767
            }
768
            Session::write('paged_questions', $paged_questions);
769
        }
770
771
        // Redefinition of variables and session ids to fix issue of survey not
772
        //  showing questions - see support.chamilo.org #5529
773
        $course_id = $survey_invitation['c_id'];
774
        Session::write('_cid', $course_id);
775
        Session::write('_real_cid', $course_id);
776
777
        if (array_key_exists($_GET['show'], $paged_questions)) {
778
            if (isset($_GET['user_id'])) {
779
                // Get the user into survey answer table (user or anonymus)
780
                $my_user_id = $survey_data['anonymous'] == 1 ? $surveyUserFromSession : api_get_user_id();
781
782
                // To show the answers by lp item
783
                $lpItemCondition = '';
784
                if ($allowSurveyInLp) {
785
                    $lpItemCondition = " AND sa.c_lp_item_id = $lpItemId";
786
                }
787
                // To show the answers by session
788
                $sessionCondition = '';
789
                if (true === api_get_configuration_value('show_surveys_base_in_sessions')) {
790
                    $sessionCondition = api_get_session_condition($sessionId, true, false, 'sa.session_id');
791
                }
792
793
                $sql = "SELECT
794
                            survey_question.survey_group_sec1,
795
                            survey_question.survey_group_sec2,
796
                            survey_question.survey_group_pri,
797
                            survey_question.question_id,
798
                            survey_question.survey_id,
799
                            survey_question.survey_question,
800
                            survey_question.display,
801
                            survey_question.sort,
802
                            survey_question.type,
803
                            survey_question.max_value,
804
                            survey_question_option.question_option_id,
805
                            survey_question_option.option_text,
806
                            $select
807
                            survey_question_option.sort as option_sort
808
                        FROM $table_survey_question survey_question
809
                        LEFT JOIN $table_survey_question_option survey_question_option
810
                        ON survey_question.question_id = survey_question_option.question_id AND
811
                        survey_question_option.c_id = $course_id
812
                        WHERE
813
                            survey_question.survey_id = '".Database::escape_string($survey_invitation['survey_id'])."' AND
814
                            survey_question.question_id NOT IN (
815
                                SELECT sa.question_id
816
                                FROM ".$table_survey_answer." sa
817
                                WHERE
818
                                    sa.user='".$my_user_id."' $sessionCondition $lpItemCondition) AND
819
                                    survey_question.c_id =  $course_id
820
                                ORDER BY survey_question.sort, survey_question_option.sort ASC";
821
            } else {
822
                $sql = "SELECT
823
                            survey_question.survey_group_sec1,
824
                            survey_question.survey_group_sec2,
825
                            survey_question.survey_group_pri,
826
                            survey_question.question_id,
827
                            survey_question.survey_id,
828
                            survey_question.survey_question,
829
                            survey_question.display,
830
                            survey_question.sort,
831
                            survey_question.type,
832
                            survey_question.max_value,
833
                            survey_question_option.question_option_id,
834
                            survey_question_option.option_text,
835
                            $select
836
                            survey_question_option.sort as option_sort
837
                            ".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')."
838
                        FROM $table_survey_question survey_question
839
                        LEFT JOIN $table_survey_question_option survey_question_option
840
                        ON survey_question.question_id = survey_question_option.question_id AND
841
                            survey_question_option.c_id = $course_id
842
                        WHERE
843
                            survey_question NOT LIKE '%{{%' AND
844
                            survey_question.survey_id = '".intval($survey_invitation['survey_id'])."' AND
845
                            survey_question.question_id IN (".implode(',', $paged_questions[$_GET['show']]).") AND
846
                            survey_question.c_id =  $course_id
847
                        ORDER BY survey_question.sort, survey_question_option.sort ASC";
848
            }
849
850
            $result = Database::query($sql);
851
            $question_counter_max = Database::num_rows($result);
852
            $counter = 0;
853
            $limit = 0;
854
            $questions = [];
855
            while ($row = Database::fetch_array($result, 'ASSOC')) {
856
                // If the type is not a pagebreak we store it in the $questions array
857
                if ($row['type'] !== 'pagebreak') {
858
                    $sort = $row['sort'];
859
                    $questions[$sort]['question_id'] = $row['question_id'];
860
                    $questions[$sort]['survey_id'] = $row['survey_id'];
861
                    $questions[$sort]['survey_question'] = $row['survey_question'];
862
                    $questions[$sort]['display'] = $row['display'];
863
                    $questions[$sort]['type'] = $row['type'];
864
                    $questions[$sort]['options'][$row['question_option_id']] = Security::remove_XSS($row['option_text']);
865
                    $questions[$sort]['maximum_score'] = $row['max_value'];
866
                    $questions[$sort]['sort'] = $sort;
867
                    $questions[$sort]['is_required'] = $allowRequiredSurveyQuestions && $row['is_required'];
868
                    $questions[$sort]['parent_id'] = isset($row['parent_id']) ? $row['parent_id'] : 0;
869
                    $questions[$sort]['parent_option_id'] = isset($row['parent_option_id']) ? $row['parent_option_id'] : 0;
870
                }
871
                $counter++;
872
                // see GH#3582
873
                if (isset($_GET['show']) && (int) $_GET['show'] >= 0) {
874
                    $lastQuestion = (int) $_GET['show'] - 1;
875
                } else {
876
                    $lastQuestion = (int) $row['question_option_id'];
877
                }
878
            }
879
        }
880
    } elseif ('1' === $survey_data['survey_type']) {
881
        $my_survey_id = (int) $survey_invitation['survey_id'];
882
        $current_user = Database::escape_string($survey_invitation['user']);
883
884
        if (isset($_POST['personality'])) {
885
            // Compute the results to get the 3 groups nearest to the user's personality
886
            if ('' == $shuffle) {
887
                $order = 'BY sort ASC ';
888
            } else {
889
                $order = $shuffle;
890
            }
891
            $answer_list = [];
892
            // Get current user results
893
            $results = [];
894
895
            // To display de answers by Lp Item
896
            $lpItemCondition = '';
897
            if ($allowSurveyInLp) {
898
                $lpItemCondition = " AND survey_answer.c_lp_item_id = $lpItemId";
899
            }
900
            // To display the answers by session
901
            $sessionCondition = '';
902
            if (true === api_get_configuration_value('show_surveys_base_in_sessions')) {
903
                $sessionCondition = api_get_session_condition($sessionId, true, false, 'survey_answer.session_id');
904
            }
905
906
            $sql = "SELECT
907
                      survey_group_pri,
908
                      user,
909
                      SUM(value) as value
910
                    FROM $table_survey_answer as survey_answer
911
                    INNER JOIN $table_survey_question as survey_question
912
                    ON (survey_question.question_id = survey_answer.question_id)
913
                    WHERE
914
                        survey_answer.survey_id='".$my_survey_id."' AND
915
                        survey_answer.user='".$current_user."' AND
916
                        survey_answer.c_id = $course_id AND
917
                        survey_question.c_id = $course_id
918
                        $sessionCondition
919
                        $lpItemCondition
920
                    GROUP BY survey_group_pri
921
                    ORDER BY survey_group_pri
922
                    ";
923
924
            $result = Database::query($sql);
925
            while ($row = Database::fetch_array($result)) {
926
                $answer_list['value'] = $row['value'];
927
                $answer_list['group'] = $row['survey_group_pri'];
928
                $results[] = $answer_list;
929
            }
930
931
            // Get the total score for each group of questions
932
            $totals = [];
933
            $sql = "SELECT SUM(temp.value) as value, temp.survey_group_pri FROM
934
                    (
935
                        SELECT
936
                            MAX(value) as value,
937
                            survey_group_pri,
938
                            survey_question.question_id
939
                        FROM $table_survey_question as survey_question
940
                        INNER JOIN $table_survey_question_option as survey_question_option
941
                        ON (survey_question.question_id = survey_question_option.question_id)
942
                        WHERE
943
                            survey_question.survey_id='".$my_survey_id."'  AND
944
                            survey_question.c_id = $course_id AND
945
                            survey_question_option.c_id = $course_id AND
946
                            survey_group_sec1='0' AND
947
                            survey_group_sec2='0'
948
                        GROUP BY survey_group_pri, survey_question.question_id
949
                    ) as temp
950
                    GROUP BY temp.survey_group_pri
951
                    ORDER BY temp.survey_group_pri";
952
953
            $result = Database::query($sql);
954
            while ($row = Database::fetch_array($result)) {
955
                $list['value'] = $row['value'];
956
                $list['group'] = $row['survey_group_pri'];
957
                $totals[] = $list;
958
            }
959
            $final_results = [];
960
            // Get a percentage score for each group
961
            for ($i = 0; $i < count($totals); $i++) {
962
                for ($j = 0; $j < count($results); $j++) {
963
                    if ($totals[$i]['group'] == $results[$j]['group']) {
964
                        $group = $totals[$i]['group'];
965
                        $porcen = ($results[$j]['value'] / $totals[$i]['value']);
966
                        $final_results[$group] = $porcen;
967
                    }
968
                }
969
            }
970
971
            // Sort the results by score (getting a list of group IDs by score into $groups)
972
            arsort($final_results);
973
            $groups = array_keys($final_results);
974
            $result = [];
975
            $count_result = 0;
976
            foreach ($final_results as $key => &$sub_result) {
977
                $result[] = ['group' => $key, 'value' => $sub_result];
978
                $count_result++;
979
            }
980
981
            /*
982
              //i.e 70% - 70% -70% 70%  $equal_count =3
983
              while (1) {
984
              if ($result[$i]['value']  == $result[$i+1]['value']) {
985
              $equal_count++;
986
              } else {
987
              break;
988
              }
989
              $i++;
990
              }
991
              echo 'eq'. $equal_count;
992
              echo '<br />';
993
              if     ($equal_count == 0) {
994
              //i.e 70% 70% -60% 60%  $equal_count = 1 only we get the first 2 options
995
              if (($result[0]['value'] == $result[1]['value'])  &&  ($result[2]['value'] == $result[3]['value'])) {
996
              $group_cant = 1;
997
              } else {
998
              // By default we chose the highest 3
999
              $group_cant=2;
1000
              }
1001
              } elseif ($equal_count == 2) {
1002
              $group_cant = 2;
1003
              } else {
1004
              $group_cant = -1;
1005
              }
1006
             */
1007
1008
            // i.e 70% - 70% -70% 70%  $equal_count =3
1009
            $i = 0;
1010
            $group_cant = 0;
1011
            $equal_count = 0;
1012
            // This is the case if the user does not select any question
1013
            if ($count_result > 0) {
1014
                // Count the number of scores equal to the first
1015
                while (1) {
1016
                    if ($result[$i]['value'] == $result[$i + 1]['value']) {
1017
                        $equal_count++;
1018
                    } else {
1019
                        break;
1020
                    }
1021
                    $i++;
1022
                }
1023
            } else {
1024
                // We force the exit of the survey undeterminated
1025
                $equal_count = 10;
1026
            }
1027
1028
            // If we have only 3 or less equal scores (i.e. 0,1 or 2 equalities), then we can use the three first groups
1029
            if ($equal_count < 4) {
1030
                // If there is one or less score equalities
1031
                if ($equal_count === 0 || $equal_count === 1) {
1032
                    // i.e 70% - 70% -60% - 60%  $equal_count = 1 we only get the first 2 options
1033
                    if (($result[0]['value'] == $result[1]['value']) &&
1034
                        ($result[2]['value'] == $result[3]['value'])
1035
                    ) {
1036
                        $group_cant = 1;
1037
                    } elseif (($result[0]['value'] != $result[1]['value']) &&
1038
                        ($result[1]['value'] == $result[2]['value']) && ($result[2]['value'] == $result[3]['value'])
1039
                    ) {
1040
                        // i.e 70% - 70% -0% - 0%     -    $equal_count = 0 we only get the first 2 options
1041
                        /* elseif (($result[0]['value'] == $result[1]['value']) && ($result[1]['value'] != $result[2]['value'])) {
1042
                          $group_cant = 0;
1043
                          } */
1044
                        /*
1045
                          // i.e 70% - 70% -60% - 60%  $equal_count = 0 we only get the first 2 options
1046
                          elseif (($result[0]['value'] == $result[1]['value'])  &&  ($result[2]['value'] == $result[3]['value'])) {
1047
                          $group_cant = 0;
1048
                          } */
1049
                        // i.e. 80% - 70% - 70% - 70%
1050
                        $group_cant = 0;
1051
                    } else {
1052
                        // i.e. 80% - 70% - 70% - 50
1053
                        // i.e. 80% - 80% - 70% - 50
1054
                        // By default we choose the highest 3
1055
                        $group_cant = 2;
1056
                    }
1057
                } else {
1058
                    // If there are two score equalities
1059
                    $group_cant = $equal_count;
1060
                }
1061
1062
                //@todo Translate these comments.
1063
                // conditional_status
1064
                // 0 no determinado
1065
                // 1 determinado
1066
                // 2 un solo valor
1067
                // 3 valores iguales
1068
                if ($group_cant > 0) {
1069
                    //echo '$equal_count'.$group_cant;
1070
                    // We only get highest 3
1071
                    $secondary = '';
1072
                    $combi = '';
1073
                    for ($i = 0; $i <= $group_cant; $i++) {
1074
                        $group1 = $groups[$i];
1075
                        $group2 = $groups[$i + 1];
1076
                        // Here we made all the posibilities with the 3 groups
1077
                        if ($group_cant == 2 && $i == $group_cant) {
1078
                            $group2 = $groups[0];
1079
                            $secondary .= " OR ( survey_group_sec1 = '$group1' AND  survey_group_sec2 = '$group2') ";
1080
                            $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
1081
                            $combi .= $group1.' - '.$group2." or ".$group2.' - '.$group1.'<br />';
1082
                        } else {
1083
                            if ($i != 0) {
1084
                                $secondary .= " OR ( survey_group_sec1 = '$group1' AND  survey_group_sec2 = '$group2') ";
1085
                                $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
1086
                                $combi .= $group1.' - '.$group2." or ".$group2.' - '.$group1.'<br />';
1087
                            } else {
1088
                                $secondary .= " ( survey_group_sec1 = '$group1' AND  survey_group_sec2 = '$group2') ";
1089
                                $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
1090
                                $combi .= $group1.' - '.$group2." or ".$group2.' - '.$group1.'<br />';
1091
                            }
1092
                        }
1093
                    }
1094
                    // Create the new select with the questions from the secondary phase
1095
                    if (empty($_SESSION['page_questions_sec']) &&
1096
                        !is_array($_SESSION['page_questions_sec']) &&
1097
                        count($_SESSION['page_questions_sec'] == 0)
1098
                    ) {
1099
                        $sql = "SELECT * FROM $table_survey_question
1100
                                 WHERE
1101
                                    c_id = $course_id AND
1102
                                    survey_id = '".$my_survey_id."' AND
1103
                                    ($secondary )
1104
                                 ORDER BY sort ASC";
1105
                        $result = Database::query($sql);
1106
                        $counter = 0;
1107
                        while ($row = Database::fetch_array($result, 'ASSOC')) {
1108
                            if ($survey_data['one_question_per_page'] == 1) {
1109
                                $paged_questions_sec[$counter][] = $row['question_id'];
1110
                                $counter++;
1111
                            } elseif ($row['type'] == 'pagebreak') {
1112
                                $counter++;
1113
                                $pageBreakText[$counter] = $row['survey_question'];
1114
                            } else {
1115
                                // ids from question of the current survey
1116
                                $paged_questions_sec[$counter][] = $row['question_id'];
1117
                            }
1118
                        }
1119
                        Session::write('page_questions_sec', $paged_questions_sec);
1120
                    } else {
1121
                        $paged_questions_sec = Session::read('page_questions_sec');
1122
                    }
1123
                    $paged_questions = Session::read('paged_questions'); // For the sake of pages counting
1124
                    if ($shuffle == '') {
1125
                        $shuffle = ' BY survey_question.sort, survey_question_option.sort ASC ';
1126
                    }
1127
                    $val = (int) $_POST['personality'];
1128
                    if (is_array($paged_questions_sec)) {
1129
                        $sql = "SELECT
1130
                                    survey_question.survey_group_sec1,
1131
                                    survey_question.survey_group_sec2,
1132
                                    survey_question.survey_group_pri,
1133
                                    survey_question.question_id,
1134
                                    survey_question.survey_id,
1135
                                    survey_question.survey_question,
1136
                                    survey_question.display,
1137
                                    survey_question.sort,
1138
                                    survey_question.type,
1139
                                    survey_question.max_value,
1140
                                    survey_question_option.question_option_id,
1141
                                    survey_question_option.option_text,
1142
                                    survey_question_option.sort as option_sort
1143
                                FROM $table_survey_question survey_question
1144
                                LEFT JOIN $table_survey_question_option survey_question_option
1145
                                ON survey_question.question_id = survey_question_option.question_id AND
1146
                                survey_question_option.c_id = $course_id
1147
                                WHERE
1148
                                    survey_question NOT LIKE '%{{%' AND
1149
                                    survey_question.survey_id = '".$my_survey_id."' AND
1150
                                    survey_question.c_id = $course_id AND
1151
                                    survey_question.question_id IN (".implode(',', $paged_questions_sec[$val]).")
1152
                                ORDER  $shuffle ";
1153
1154
                        $result = Database::query($sql);
1155
                        $question_counter_max = Database::num_rows($result);
1156
                        $counter = 0;
1157
                        $limit = 0;
1158
                        $questions = [];
1159
                        while ($row = Database::fetch_array($result, 'ASSOC')) {
1160
                            // If the type is not a pagebreak we store it in the $questions array
1161
                            if ('pagebreak' !== $row['type']) {
1162
                                $questions[$row['sort']]['question_id'] = $row['question_id'];
1163
                                $questions[$row['sort']]['survey_id'] = $row['survey_id'];
1164
                                $questions[$row['sort']]['survey_question'] = $row['survey_question'];
1165
                                $questions[$row['sort']]['display'] = $row['display'];
1166
                                $questions[$row['sort']]['type'] = $row['type'];
1167
                                $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
1168
                                $questions[$row['sort']]['maximum_score'] = $row['max_value'];
1169
                                // Personality params
1170
                                $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
1171
                                $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];
1172
                                $questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri'];
1173
                                $questions[$row['sort']]['sort'] = $row['sort'];
1174
                            } else {
1175
                                // If the type is a pagebreak we are finished loading the questions for this page
1176
                                break;
1177
                            }
1178
                            $counter++;
1179
                        }
1180
                    } else {
1181
                        echo get_lang('SurveyUndetermined');
1182
                    }
1183
                } else {
1184
                    echo get_lang('SurveyUndetermined');
1185
                }
1186
            } else {
1187
                echo get_lang('SurveyUndetermined');
1188
            }
1189
        } else {
1190
            // We need this variable only in the 2nd set of questions when personality is set.
1191
            Session::erase('page_questions_sec');
1192
            $paged_questions_sec = [];
1193
1194
            // Only the questions from the basic group
1195
            // the 50 questions A B C D E F G
1196
            $order_sql = $shuffle;
1197
            if ($shuffle == '') {
1198
                $order_sql = ' BY question_id ';
1199
            }
1200
1201
            if (empty($_SESSION['paged_questions'])) {
1202
                $sql = "SELECT * FROM $table_survey_question
1203
                        WHERE
1204
                            c_id = $course_id AND
1205
                            survey_id = '".intval($survey_invitation['survey_id'])."' AND
1206
                            survey_group_sec1='0' AND
1207
                            survey_group_sec2='0'
1208
                        ORDER ".$order_sql." ";
1209
                $result = Database::query($sql);
1210
                $counter = 0;
1211
                while ($row = Database::fetch_array($result, 'ASSOC')) {
1212
                    if ($survey_data['one_question_per_page'] == 1) {
1213
                        $paged_questions[$counter][] = $row['question_id'];
1214
                        $counter++;
1215
                    } else {
1216
                        if ($row['type'] == 'pagebreak') {
1217
                            $counter++;
1218
                            $pageBreakText[$counter] = $row['survey_question'];
1219
                        } else {
1220
                            // ids from question of the current survey
1221
                            $paged_questions[$counter][] = $row['question_id'];
1222
                        }
1223
                    }
1224
                }
1225
                Session::write('paged_questions', $paged_questions);
1226
            } else {
1227
                $paged_questions = Session::read('paged_questions');
1228
            }
1229
            $order_sql = $shuffle;
1230
            if ($shuffle == '') {
1231
                $order_sql = ' BY survey_question.sort, survey_question_option.sort ASC ';
1232
            }
1233
            $val = $_GET['show'];
1234
            $result = null;
1235
            if ($val != '') {
1236
                $imploded = Database::escape_string(implode(',', $paged_questions[$val]));
1237
                if ($imploded != '') {
1238
                    // The answers are always in the same order NO shuffle
1239
                    $order_sql = ' BY survey_question.sort, survey_question_option.sort ASC ';
1240
                    $sql = "SELECT
1241
                                survey_question.survey_group_sec1,
1242
                                survey_question.survey_group_sec2,
1243
                                survey_question.survey_group_pri,
1244
                                survey_question.question_id,
1245
                                survey_question.survey_id,
1246
                                survey_question.survey_question,
1247
                                survey_question.display,
1248
                                survey_question.sort,
1249
                                survey_question.type,
1250
                                survey_question.max_value,
1251
                                survey_question_option.question_option_id,
1252
                                survey_question_option.option_text,
1253
                                survey_question_option.sort as option_sort
1254
                                ".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')."
1255
                            FROM $table_survey_question survey_question
1256
                            LEFT JOIN $table_survey_question_option survey_question_option
1257
                            ON survey_question.question_id = survey_question_option.question_id AND
1258
                            survey_question_option.c_id = $course_id
1259
                            WHERE
1260
                                survey_question NOT LIKE '%{{%' AND
1261
                                survey_question.survey_id = '".intval($survey_invitation['survey_id'])."' AND
1262
                                survey_question.c_id = $course_id  AND
1263
                                survey_question.question_id IN (".$imploded.")
1264
                            ORDER $order_sql ";
1265
                    $result = Database::query($sql);
1266
                    $question_counter_max = Database::num_rows($result);
1267
                }
1268
            }
1269
1270
            if (!is_null($result)) {
1271
                $counter = 0;
1272
                $limit = 0;
1273
                $questions = [];
1274
                while ($row = Database::fetch_array($result, 'ASSOC')) {
1275
                    // If the type is not a pagebreak we store it in the $questions array
1276
                    if ('pagebreak' !== $row['type']) {
1277
                        $questions[$row['sort']]['question_id'] = $row['question_id'];
1278
                        $questions[$row['sort']]['survey_id'] = $row['survey_id'];
1279
                        $questions[$row['sort']]['survey_question'] = $row['survey_question'];
1280
                        $questions[$row['sort']]['display'] = $row['display'];
1281
                        $questions[$row['sort']]['type'] = $row['type'];
1282
                        $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
1283
                        $questions[$row['sort']]['maximum_score'] = $row['max_value'];
1284
                        $questions[$row['sort']]['is_required'] = $allowRequiredSurveyQuestions && $row['is_required'];
1285
                        // Personality params
1286
                        $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
1287
                        $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];
1288
                        $questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri'];
1289
                        $questions[$row['sort']]['sort'] = $row['sort'];
1290
                    } else {
1291
                        // If the type is a page break we are finished loading the questions for this page
1292
                        //break;
1293
                    }
1294
                    $counter++;
1295
                }
1296
            }
1297
        }
1298
    } else { // In case it's another type than 0 or 1
1299
        echo get_lang('ErrorSurveyTypeUnknown');
1300
    }
1301
}
1302
1303
$numberOfPages = SurveyManager::getCountPages($survey_data);
1304
1305
// Displaying the form with the questions
1306
$show = 0;
1307
if (isset($_GET['show']) && $_GET['show'] != '') {
1308
    $show = (int) $_GET['show'] + 1;
1309
}
1310
1311
$displayFinishButton = true;
1312
if (isset($_GET['show']) && $_GET['show'] != '') {
1313
    $pagesIndexes = array_keys($paged_questions);
1314
    $pagesIndexes[] = count($pagesIndexes);
1315
1316
    if (end($pagesIndexes) <= $show - 1 && empty($_POST)) {
1317
        $displayFinishButton = false;
1318
    }
1319
}
1320
1321
// Displaying the form with the questions
1322
$personality = 0;
1323
if (isset($_POST['personality'])) {
1324
    $personality = (int) $_POST['personality'] + 1;
1325
}
1326
1327
// Displaying the form with the questions
1328
$g_c = isset($_GET['course']) ? Security::remove_XSS($_GET['course']) : '';
1329
$g_ic = isset($_GET['invitationcode']) ? Security::remove_XSS($_GET['invitationcode']) : '';
1330
$g_cr = isset($_GET['cidReq']) ? Security::remove_XSS($_GET['cidReq']) : '';
1331
$p_l = isset($_POST['language']) ? Security::remove_XSS($_POST['language']) : '';
1332
$add_parameters = isset($_GET['user_id']) ? '&user_id='.intval($_GET['user_id']) : '';
1333
$url = api_get_self().'?cidReq='.$courseInfo['code'].
1334
    '&id_session='.$sessionId.
1335
    $add_parameters.
1336
    '&course='.$g_c.
1337
    '&invitationcode='.$g_ic.
1338
    '&show='.$show;
1339
if (!empty($_GET['language'])) {
1340
    $lang = Security::remove_XSS($_GET['language']);
1341
    $url .= '&language='.$lang;
1342
}
1343
if (!empty($lpItemId) && $allowSurveyInLp) {
1344
    $url .= '&lp_item_id='.$lpItemId.'&origin=learnpath';
1345
}
1346
$form = new FormValidator(
1347
    'question',
1348
    'post',
1349
    $url,
1350
    null,
1351
    null,
1352
    FormValidator::LAYOUT_INLINE
1353
);
1354
$form->addHidden('language', $p_l);
1355
1356
$showNumber = true;
1357
if (SurveyManager::hasDependency($survey_data)) {
1358
    $showNumber = false;
1359
}
1360
1361
if (isset($questions) && is_array($questions)) {
1362
    $originalShow = isset($_GET['show']) ? (int) $_GET['show'] : 0;
1363
    $questionCounter = 1;
1364
    if (!empty($originalShow)) {
1365
        $before = 0;
1366
        foreach ($paged_questions as $keyQuestion => $list) {
1367
            if ($originalShow > $keyQuestion) {
1368
                $before += count($list);
1369
            }
1370
        }
1371
        $questionCounter = $before + 1;
1372
    }
1373
1374
    $form->addHtml('<div class="start-survey">');
1375
    $js = '';
1376
1377
    if (isset($pageBreakText[$originalShow]) && !empty(strip_tags($pageBreakText[$originalShow]))) {
1378
        // Only show page-break texts if there is something there, apart from
1379
        // HTML tags
1380
        $form->addHtml(
1381
            '<div>'.
1382
            Security::remove_XSS($pageBreakText[$originalShow]).
1383
            '</div>'
1384
        );
1385
        $form->addHtml('<br />');
1386
    }
1387
1388
    foreach ($questions as $key => &$question) {
1389
        $ch_type = 'ch_'.$question['type'];
1390
        $questionNumber = $questionCounter;
1391
        $display = new $ch_type();
1392
        $parent = $question['parent_id'];
1393
        $parentClass = '';
1394
1395
        if (!empty($parent)) {
1396
            $parentClass = ' with_parent with_parent_'.$question['question_id'];
1397
            $parents = survey_question::getParents($question['question_id']);
1398
            if (!empty($parents)) {
1399
                foreach ($parents as $parentId) {
1400
                    $parentClass .= ' with_parent_only_hide_'.$parentId;
1401
                }
1402
            }
1403
        }
1404
1405
        $js .= survey_question::getQuestionJs($question);
1406
1407
        // @todo move this in a function.
1408
        $form->addHtml('<div class="survey_question '.$ch_type.' '.$parentClass.'">');
1409
        if ($showNumber) {
1410
            $form->addHtml('<div style="float:left; font-weight: bold; margin-right: 5px;"> '.$questionNumber.'. </div>');
1411
        }
1412
        $form->addHtml('<div>'.Security::remove_XSS($question['survey_question']).'</div>');
1413
1414
        $userAnswerData = SurveyUtil::get_answers_of_question_by_user($question['survey_id'], $question['question_id'], $lpItemId);
1415
        $finalAnswer = null;
1416
1417
        if (!empty($userAnswerData[$user_id])) {
1418
            $userAnswer = $userAnswerData[$user_id];
1419
            switch ($question['type']) {
1420
                case 'score':
1421
                    $finalAnswer = [];
1422
                    foreach ($userAnswer as $userChoice) {
1423
                        list($choiceId, $choiceValue) = explode('*', $userChoice);
1424
                        $finalAnswer[$choiceId] = $choiceValue;
1425
                    }
1426
                    break;
1427
                case 'percentage':
1428
                    list($choiceId, $choiceValue) = explode('*', current($userAnswer));
1429
                    $finalAnswer = $choiceId;
1430
                    break;
1431
                default:
1432
                    $finalAnswer = $userAnswer;
1433
                    break;
1434
            }
1435
        }
1436
        $display->render($form, $question, $finalAnswer);
1437
        $form->addHtml('</div>');
1438
        $questionCounter++;
1439
    }
1440
1441
    $form->addHtml($js);
1442
}
1443
1444
$form->addHtml('<div class="start-survey">');
1445
if ($survey_data['survey_type'] == '0') {
1446
    if ($survey_data['show_form_profile'] == 0) {
1447
        // The normal survey as always
1448
        if ($show < $numberOfPages) {
1449
            if ($show == 0) {
1450
                $form->addButton(
1451
                    'next_survey_page',
1452
                    get_lang('StartSurvey'),
1453
                    'arrow-right',
1454
                    'success'
1455
                );
1456
            } else {
1457
                // see GH#3582
1458
                if (
1459
                api_get_configuration_value('survey_backwards_enable')
1460
                ) {
1461
                    if ($lastQuestion >= 0) {
1462
                        $form->addHtml(
1463
                            "<a class=\" btn btn-warning \" href=\"$url&show=$lastQuestion\">".
1464
                            "<em class=\"fa fa-arrow-left\"></em> "
1465
                            .get_lang('Back')." </a>"
1466
                        );
1467
                    }
1468
                }
1469
                $form->addButton(
1470
                    'next_survey_page',
1471
                    get_lang('Next'),
1472
                    'arrow-right',
1473
                    'success'
1474
                );
1475
            }
1476
        }
1477
        if ($show >= $numberOfPages && $displayFinishButton) {
1478
            $form->addButton(
1479
                'finish_survey',
1480
                get_lang('FinishSurvey'),
1481
                'arrow-right',
1482
                'success'
1483
            );
1484
        }
1485
    } else {
1486
        // The normal survey as always but with the form profile
1487
        if (isset($_GET['show'])) {
1488
            $numberOfPages = count($paged_questions);
1489
            if ($show < $numberOfPages) {
1490
                if ($show == 0) {
1491
                    $form->addButton(
1492
                        'next_survey_page',
1493
                        get_lang('StartSurvey'),
1494
                        'arrow-right',
1495
                        'success'
1496
                    );
1497
                } else {
1498
                    $form->addButton(
1499
                        'next_survey_page',
1500
                        get_lang('Next'),
1501
                        'arrow-right',
1502
                        'success'
1503
                    );
1504
                }
1505
            }
1506
1507
            if ($show >= $numberOfPages && $displayFinishButton) {
1508
                $form->addButton(
1509
                    'finish_survey',
1510
                    get_lang('FinishSurvey'),
1511
                    'arrow-right',
1512
                    'success'
1513
                );
1514
            }
1515
        }
1516
    }
1517
} elseif ($survey_data['survey_type'] == '1') {
1518
    // Conditional/personality-test type survey
1519
    if (isset($_GET['show']) || isset($_POST['personality'])) {
1520
        $numberOfPages = count($paged_questions);
1521
        if (!empty($paged_questions_sec) && count($paged_questions_sec) > 0) {
1522
            // In case we're in the second phase, also sum the second group questions
1523
            $numberOfPages += count($paged_questions_sec);
1524
        } else {
1525
            // We need this variable only if personality == 1
1526
            Session::erase('page_questions_sec');
1527
            $paged_questions_sec = [];
1528
        }
1529
1530
        if ($personality == 0) {
1531
            if (($show <= $numberOfPages) || !$_GET['show']) {
1532
                $form->addButton('next_survey_page', get_lang('Next'), 'arrow-right', 'success');
1533
                if ($survey_data['one_question_per_page'] == 0) {
1534
                    if ($personality >= 0) {
1535
                        $form->addHidden('personality', $personality);
1536
                    }
1537
                } else {
1538
                    if ($personality > 0) {
1539
                        $form->addHidden('personality', $personality);
1540
                    }
1541
                }
1542
1543
                if ($numberOfPages == $show) {
1544
                    $form->addHidden('personality', $personality);
1545
                }
1546
            }
1547
        }
1548
1549
        if ($show > $numberOfPages && $_GET['show'] && $personality == 0) {
1550
            $form->addHidden('personality', $personality);
1551
        } elseif ($personality > 0) {
1552
            if ($survey_data['one_question_per_page'] == 1) {
1553
                if ($show >= $numberOfPages) {
1554
                    $form->addButton('finish_survey', get_lang('FinishSurvey'), 'arrow-right', 'success');
1555
                } else {
1556
                    $form->addHidden('personality', $personality);
1557
                    $form->addButton('next_survey_page', get_lang('Next'), 'arrow-right', 'success');
1558
                }
1559
            } else {
1560
                // if the personality test hidden input was set.
1561
                $form->addButton('finish_survey', get_lang('FinishSurvey'), 'arrow-right');
1562
            }
1563
        }
1564
    } elseif ($survey_data['form_fields'] == '') {
1565
        // This is the case when the show_profile_form is true but there are not form_fields
1566
        $form->addButton('next_survey_page', get_lang('Next'), 'arrow-right', 'success');
1567
    } elseif (!is_array($user_data)) {
1568
        // If the user is not registered in the platform we do not show the form to update his information
1569
        $form->addButton('next_survey_page', get_lang('Next'), 'arrow-right', 'success');
1570
    }
1571
}
1572
$form->addHtml('</div>');
1573
$form->display();
1574
Display::display_footer();
1575