Completed
Push — master ( 17e47f...c8c08e )
by Julito
65:46 queued 26:40
created

upload_exercise.php ➔ lp_upload_quiz_action_handling()   F

Complexity

Conditions 102
Paths > 20000

Size

Total Lines 501
Code Lines 341

Duplication

Lines 14
Ratio 2.79 %

Importance

Changes 0
Metric Value
cc 102
eloc 341
nc 1390802
nop 0
dl 14
loc 501
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * 	Upload quiz: This script shows the upload quiz feature
6
 *  Initial work by Isaac flores on Nov 4 of 2010
7
 *  Encoding fixes Julio Montoya
8
 * 	@package chamilo.exercise
9
 */
10
use \ChamiloSession as Session;
11
12
// setting the help
13
$help_content = 'exercise_upload';
14
15
// including the global Dokeos file
16
//require_once '../inc/global.inc.php';
17
18
require_once api_get_path(LIBRARY_PATH) . 'pear/excelreader/reader.php';
19
20
// Security check
21
$is_allowed_to_edit = api_is_allowed_to_edit(null, true);
22
if (!$is_allowed_to_edit) {
23
    api_not_allowed(true);
24
}
25
// setting the tabs
26
$this_section = SECTION_COURSES;
27
$htmlHeadXtra[] = "<script>
28
$(document).ready( function(){
29
    $('#user_custom_score').click(function() {
30
        $('#options').toggle();
31
    });
32
});
33
</script>";
34
35
// Action handling
36
lp_upload_quiz_action_handling();
37
38
$interbreadcrumb[] = array(
39
    "url" => "exercise.php?".api_get_cidreq(),
40
    "name" => get_lang('Exercises'),
41
);
42
43
Display::display_header(get_lang('ImportExcelQuiz'), 'Exercises');
44
45
if (isset($_GET['message'])) {
46
    if (in_array($_GET['message'], array('ExerciseEdited'))) {
47
        Display :: display_confirmation_message(get_lang($_GET['message']));
48
    }
49
}
50
51
// display the actions
52
echo '<div class="actions">';
53
echo lp_upload_quiz_actions();
54
echo '</div>';
55
56
// the main content
57
lp_upload_quiz_main();
58
59
function lp_upload_quiz_actions()
60
{
61
    $return = '<a href="exercise.php?'.api_get_cidReq().'">'.
62
        Display::return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
63
    return $return;
64
}
65
66
function lp_upload_quiz_secondary_actions()
67
{
68
    $return = '<a href="exercise_report.php?' . api_get_cidreq() . '">' .
69
        Display :: return_icon('reporting32.png', get_lang('Tracking')) . get_lang('Tracking') . '</a>';
70
    return $return;
71
}
72
73
function lp_upload_quiz_main() {
74
75
    // variable initialisation
76
    $lp_id = isset($_GET['lp_id']) ? intval($_GET['lp_id']) : null;
77
78
    $form = new FormValidator(
79
        'upload',
80
        'POST',
81
        api_get_self().'?'.api_get_cidreq().'&lp_id='.$lp_id,
82
        '',
83
        array('enctype' => 'multipart/form-data')
84
    );
85
    $form->addElement('header', get_lang('ImportExcelQuiz'));
86
    $form->addElement('file', 'user_upload_quiz', get_lang('FileUpload'));
87
88
    $link = '<a href="../exercice/quiz_template.xls">'.
89
        Display::return_icon('export_excel.png', get_lang('DownloadExcelTemplate')).get_lang('DownloadExcelTemplate').'</a>';
90
    $form->addElement('label', '', $link);
91
    
92
$table = new HTML_Table(array('class' => 'table'));
93
94
    $tableList = array(
95
        UNIQUE_ANSWER => get_lang('UniqueSelect'),
96
        MULTIPLE_ANSWER => get_lang('MultipleSelect'),
97
        FILL_IN_BLANKS => get_lang('FillBlanks'),
98
        MATCHING => get_lang('Matching'),
99
        FREE_ANSWER => get_lang('FreeAnswer'),
100
        GLOBAL_MULTIPLE_ANSWER => get_lang('GlobalMultipleAnswer')
101
    );
102
103
    $table->setHeaderContents(0, 0, get_lang('QuestionType'));
104
    $table->setHeaderContents(0, 1, '#');
105
106
    $row = 1;
107
    foreach ($tableList as $key => $label ) {
108
        $table->setCellContents($row, 0, $label);
109
        $table->setCellContents($row, 1, $key);
110
        $row++;
111
    }
112
    $table = $table->toHtml();
113
114
    $form->addElement('label', get_lang('QuestionType'), $table);
115
    $form->addElement('checkbox', 'user_custom_score', null, get_lang('UseCustomScoreForAllQuestions'), array('id'=> 'user_custom_score'));
116
    $form->addElement('html', '<div id="options" style="display:none">');
117
    $form->addElement('text', 'correct_score', get_lang('CorrectScore'));
118
    $form->addElement('text', 'incorrect_score', get_lang('IncorrectScore'));
119
    $form->addElement('html', '</div>');
120
121
    $form->addRule('user_upload_quiz', get_lang('ThisFieldIsRequired'), 'required');
122
123
    $form->add_progress_bar();
0 ignored issues
show
Deprecated Code introduced by
The method FormValidator::add_progress_bar() has been deprecated with message: ?

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
124
    $form->addButtonUpload(get_lang('Upload'), 'submit_upload_quiz');
125
126
    // Display the upload field
127
    $form->display();
128
}
129
130
/**
131
 * Handles a given Excel spreadsheets as in the template provided
132
 */
133
function lp_upload_quiz_action_handling() {
134
    global $debug;
135
    $_course = api_get_course_info();
136
    $courseId = $_course['real_id'];
137
138
    if (!isset($_POST['submit_upload_quiz'])) {
139
        return;
140
    }
141
142
    // Get the extension of the document.
143
    $path_info = pathinfo($_FILES['user_upload_quiz']['name']);
144
145
    // Check if the document is an Excel document
146
    if ($path_info['extension'] != 'xls') {
147
        return;
148
    }
149
150
    // Read the Excel document
151
    $data = new Spreadsheet_Excel_Reader();
152
    // Set output Encoding.
153
    $data->setOutputEncoding(api_get_system_encoding());
154
    // Reading the xls document.
155
    $data->read($_FILES['user_upload_quiz']['tmp_name']);
156
157
    $correctScore = isset($_POST['correct_score']) ? $_POST['correct_score'] : null;
158
    $incorrectScore = isset($_POST['incorrect_score']) ? $_POST['incorrect_score'] : null;
159
    $useCustomScore = isset($_POST['user_custom_score']) ? true : false;
160
161
    $propagateNegative = 0;
162
    if ($useCustomScore && !empty($incorrectScore)) {
163
        if ($incorrectScore < 0) {
164
            $propagateNegative = 1;
165
        }
166
    }
167
168
    // Variables
169
    $quiz_index = 0;
170
    $question_title_index = array();
171
    $question_name_index_init = array();
172
    $question_name_index_end = array();
173
    $score_index = array();
174
    $feedback_true_index = array();
175
    $feedback_false_index = array();
176
    $number_questions = 0;
177
    $question_description_index = array();
178
    $noNegativeScoreIndex = array();
179
    $questionTypeList = array();
180
    $questionTypeIndex = array();
181
    $categoryList = array();
182
183
    // Reading all the first column items sequentially to create breakpoints
184
    for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
185
        if ($data->sheets[0]['cells'][$i][1] == 'Quiz' && $i == 1) {
186
            $quiz_index = $i; // Quiz title position, only occurs once
187
        } elseif ($data->sheets[0]['cells'][$i][1] == 'Question') {
188
            $question_title_index[] = $i; // Question title position line
189
            $question_name_index_init[] = $i + 1; // Questions name 1st position line
190
            $number_questions++;
191
        } elseif ($data->sheets[0]['cells'][$i][1] == 'Score') {
192
            $question_name_index_end[] = $i - 1; // Question name position
193
            $score_index[] = $i; // Question score position
194
        } elseif ($data->sheets[0]['cells'][$i][1] == 'FeedbackTrue') {
195
            $feedback_true_index[] = $i; // FeedbackTrue position (line)
196
        } elseif ($data->sheets[0]['cells'][$i][1] == 'FeedbackFalse') {
197
            $feedback_false_index[] = $i; // FeedbackFalse position (line)
198
        } elseif ($data->sheets[0]['cells'][$i][1] == 'EnrichQuestion') {
199
            $question_description_index[] = $i;
200
        } elseif ($data->sheets[0]['cells'][$i][1] == 'NoNegativeScore') {
201
            $noNegativeScoreIndex[] = $i;
202
        } elseif ($data->sheets[0]['cells'][$i][1] == 'QuestionType') {
203
            $questionTypeIndex[] = $i;
204
        }
205
    }
206
207
    // Variables
208
    $quiz = array();
209
    $question = array();
210
    $new_answer = array();
211
    $score_list = array();
212
    $feedback_true_list = array();
213
    $feedback_false_list = array();
214
    $question_description = array();
215
    $noNegativeScoreList = array();
216
217
    // Getting questions.
218
    $k = $z = $q = $l = $m = $n = 0;
219
    for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
220
        if (is_array($data->sheets[0]['cells'][$i])) {
221
            $column_data = $data->sheets[0]['cells'][$i];
222
            // Fill all column with data to have a full array
223 View Code Duplication
            for ($x = 1; $x <= $data->sheets[0]['numCols']; $x++) {
224
                if (empty($column_data[$x])) {
225
                    $data->sheets[0]['cells'][$i][$x] = '';
226
                }
227
            }
228
            // Array filled with data
229
            $column_data = $data->sheets[0]['cells'][$i];
230
        } else {
231
            $column_data = '';
232
        }
233
234
        // Fill quiz data
235
        if ($quiz_index == $i) {
236
            // The title always in the first position
237
            $quiz = $column_data;
238
        } elseif (in_array($i, $question_title_index)) {
239
            //a complete line where 1st column is 'Question'
240
            $question[$k] = $column_data;
241
242
            for ($counter = 0; $counter < 12; $counter++) {
243
                $myData = isset($data->sheets[0]['cells'][$i + $counter]) ? $data->sheets[0]['cells'][$i + $counter] : null;
244
                if (isset($myData[1]) && $myData[1] == 'QuestionType') {
245
                    $questionTypeList[$k] = $myData[3];
246
                }
247
                if (isset($myData[1]) && $myData[1] == 'Category') {
248
                    $categoryList[$k] = $myData[2];
249
                }
250
            }
251
252
            if (!isset($questionTypeList[$k])) {
253
                $questionTypeList[$k] = null;
254
            }
255
256
            $k++;
257
        } elseif (in_array($i, $score_index)) {
258
            //a complete line where 1st column is 'Score'
259
            $score_list[$z] = $column_data;
260
            $z++;
261
        } elseif (in_array($i, $feedback_true_index)) {
262
            //a complete line where 1st column is 'FeedbackTrue'
263
            $feedback_true_list[$q] = $column_data;
264
            $q++;
265
        } elseif (in_array($i, $feedback_false_index)) {
266
            //a complete line where 1st column is 'FeedbackFalse' for wrong answers
267
            $feedback_false_list[$l] = $column_data;
268
            $l++;
269
        } elseif (in_array($i, $question_description_index)) {
270
            //a complete line where 1st column is 'EnrichQuestion'
271
            $question_description[$m] = $column_data;
272
            $m++;
273
        } elseif (in_array($i, $noNegativeScoreIndex)) {
274
            //a complete line where 1st column is 'NoNegativeScore'
275
            $noNegativeScoreList[$z-1] = $column_data;
276
        }
277
    }
278
279
    // Get answers
280
    for ($i = 0; $i < count($question_name_index_init); $i++) {
281
        for ($j = $question_name_index_init[$i]; $j <= $question_name_index_end[$i]; $j++) {
282
            if (is_array($data->sheets[0]['cells'][$j])) {
283
                $column_data = $data->sheets[0]['cells'][$j];
284
                // Fill all column with data
285 View Code Duplication
                for ($x = 1; $x <= $data->sheets[0]['numCols']; $x++) {
286
                    if (empty($column_data[$x])) {
287
                        $data->sheets[0]['cells'][$j][$x] = '';
288
                    }
289
                }
290
                $column_data = $data->sheets[0]['cells'][$j];
291
                // Array filled of data
292
                if (is_array($column_data) && count($column_data) > 0) {
293
                    $new_answer[$i][$j] = $column_data;
294
                }
295
            }
296
        }
297
    }
298
299
    // Quiz title.
300
    $quiz_title = $quiz[2];
301
302
    if ($quiz_title != '') {
303
        // Variables
304
        $type = 2;
305
        $random = $active = $results = $max_attempt = $expired_time = 0;
306
        // Make sure feedback is enabled (3 to disable), otherwise the fields
307
        // added to the XLS are not shown, which is confusing
308
        $feedback = 0;
309
310
        // Quiz object
311
        $exercise = new Exercise();
312
        //
313
        $quiz_id = $exercise->createExercise(
314
            $quiz_title,
315
            $expired_time,
316
            $type,
317
            $random,
318
            $active,
319
            $results,
320
            $max_attempt,
321
            $feedback,
322
            $propagateNegative
323
        );
324
325
        if ($quiz_id) {
326
327
            // insert into the item_property table
328
            api_item_property_update(
329
                $_course,
330
                TOOL_QUIZ,
331
                $quiz_id,
332
                'QuizAdded',
333
                api_get_user_id()
334
            );
335
336
            // Import questions.
337
            for ($i = 0; $i < $number_questions; $i++) {
338
                // Question name
339
                $question_title = $question[$i][2];
340
                $description = isset($question_description[$i][2]) ? $question_description[$i][2] : '';
341
                $categoryId = null;
342
                if (isset($categoryList[$i]) && !empty($categoryList[$i])) {
343
                    $categoryName = $categoryList[$i];
344
                    $categoryId = TestCategory::get_category_id_for_title($categoryName, $courseId);
345
                    if (empty($categoryId)) {
346
                        $category = new TestCategory(null, $categoryName, '');
347
                        $categoryId = $category->addCategoryInBDD();
348
                    }
349
                }
350
351
                $question_description_text = "<p></p>";
352
                if (!empty($description)) {
353
                    // Question description.
354
                    $question_description_text = "<p>".$description."</p>";
355
                }
356
357
                // Unique answers are the only question types available for now
358
                // through xls-format import
359
                $answerList = isset($new_answer[$i]) ? $new_answer[$i] : '';
360
361
                $question_id = null;
362
                if (isset($questionTypeList[$i])) {
363
                    $detectQuestionType = intval($questionTypeList[$i]);
364
                } else {
365
                    $detectQuestionType = detectQuestionType(
366
                        $answerList,
367
                        $score_list
368
                    );
369
                }
370
371
                /** @var Question $answer */
372
                switch ($detectQuestionType) {
373
                    case FREE_ANSWER:
374
                        $answer = new FreeAnswer();
375
                        break;
376
                    case GLOBAL_MULTIPLE_ANSWER:
377
                        $answer = new GlobalMultipleAnswer();
378
                        break;
379
                    case MULTIPLE_ANSWER:
380
                        $answer = new MultipleAnswer();
381
                        break;
382
                    case FILL_IN_BLANKS:
383
                        $answer = new FillBlanks();
384
                        $question_description_text = '';
385
                        break;
386
                    case MATCHING:
387
                        $answer = new Matching();
388
                        break;
389
                    case UNIQUE_ANSWER:
390
                    default:
391
                        $answer = new UniqueAnswer();
392
                        break;
393
                }
394
395
                if ($question_title != '') {
396
                    $question_id = $answer->create_question(
397
                        $quiz_id,
398
                        $question_title,
399
                        $question_description_text,
400
                        0, // max score
401
                        $answer->type
402
                    );
403
                    if (!empty($categoryId)) {
404
                        TestCategory::add_category_for_question_id(
405
                            $categoryId,
406
                            $question_id,
407
                            $courseId
408
                        );
409
                    }
410
                }
411
412
                switch ($detectQuestionType) {
413
                    case GLOBAL_MULTIPLE_ANSWER:
414
                    case MULTIPLE_ANSWER:
415
                    case UNIQUE_ANSWER:
416
                        $total = 0;
417
                        if (is_array($answerList) && !empty($question_id)) {
418
                            $id = 1;
419
                            $globalScore = null;
420
                            $objAnswer = new Answer($question_id, $courseId);
421
                            $globalScore = $score_list[$i][3];
422
423
                            // Calculate the number of correct answers to divide the
424
                            // score between them when importing from CSV
425
                            $numberRightAnswers = 0;
426
                            foreach ($answerList as $answer_data) {
427
                                if (strtolower($answer_data[3]) == 'x') {
428
                                    $numberRightAnswers++;
429
                                }
430
                            }
431
432
                            foreach ($answerList as $answer_data) {
433
                                $answerValue = $answer_data[2];
434
                                $correct = 0;
435
                                $score = 0;
436
                                if (strtolower($answer_data[3]) == 'x') {
437
                                    $correct = 1;
438
                                    $score = $score_list[$i][3];
439
                                    $comment = $feedback_true_list[$i][2];
440
                                } else {
441
                                    $comment = $feedback_false_list[$i][2];
442
                                    $floatVal = (float)$answer_data[3];
443
                                    if (is_numeric($floatVal)) {
444
                                        $score = $answer_data[3];
445
                                    }
446
                                }
447
448
                                if ($useCustomScore) {
449
                                    if ($correct) {
450
                                        $score = $correctScore;
451
                                    } else {
452
                                        $score = $incorrectScore;
453
                                    }
454
                                }
455
456
                                // Fixing scores:
457
                                switch ($detectQuestionType) {
458
                                    case GLOBAL_MULTIPLE_ANSWER:
459
                                        if (isset($noNegativeScoreList[$i][3])) {
460
                                            if (!(strtolower($noNegativeScoreList[$i][3]) == 'x') &&
461
                                                !$correct
462
                                            ) {
463
                                                $score = $score_list[$i][3] * -1;
464
                                            }
465
                                        } else {
466
                                            $score = $score_list[$i][3] * -1;
467
                                        }
468
                                        $score /= $numberRightAnswers;
469
                                        break;
470
                                    case UNIQUE_ANSWER:
471
                                        break;
472
                                    case MULTIPLE_ANSWER:
473
                                        if (!$correct) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
474
                                            //$total = $total - $score;
475
                                        }
476
                                        break;
477
                                }
478
479
                                $objAnswer->createAnswer(
480
                                    $answerValue,
481
                                    $correct,
482
                                    $comment,
483
                                    $score,
484
                                    $id
485
                                );
486
487
                                $total += $score;
488
                                $id++;
489
                            }
490
491
                            $objAnswer->save();
492
493
                            $questionObj = Question::read(
494
                                $question_id,
495
                                $courseId
496
                            );
497
498
                            switch ($detectQuestionType) {
499
                                case GLOBAL_MULTIPLE_ANSWER:
500
                                    $questionObj->updateWeighting($globalScore);
501
                                    break;
502
                                case UNIQUE_ANSWER:
503
                                case MULTIPLE_ANSWER:
504
                                default:
505
                                    $questionObj->updateWeighting($total);
506
                                    break;
507
                            }
508
509
                            $questionObj->save();
510
                        }
511
                        break;
512
                    case FREE_ANSWER:
513
                        $questionObj = Question::read($question_id, $courseId);
514
                        $globalScore = $score_list[$i][3];
515
                        $questionObj->updateWeighting($globalScore);
516
                        $questionObj->save();
517
                        break;
518
                    case FILL_IN_BLANKS:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
519
520
                        $scoreList = array();
521
                        $size = array();
522
523
                        $globalScore = 0;
524
                        foreach($answerList as $data) {
525
                            $score = isset($data[3]) ? $data[3] : 0;
526
                            $globalScore += $score;
527
                            $scoreList[] = $score;
528
                            $size[] = 200;
529
                        }
530
531
                        $scoreToString = implode(',', $scoreList);
532
                        $sizeToString = implode(',', $size);
533
534
                        //<p>Texte long avec les [mots] à [remplir] mis entre [crochets]</p>::10,10,10:200.36363999999998,200,200:0@'
535
                        $answerValue  = $description.'::'.$scoreToString.':'.$sizeToString.':0@';
536
                        $objAnswer = new Answer($question_id, $courseId);
537
                        $objAnswer->createAnswer(
538
                            $answerValue,
539
                            '', //$correct,
540
                            '', //$comment,
541
                            $globalScore,
542
                            1
543
                        );
544
545
                        $objAnswer->save();
546
547
                        $questionObj = Question::read($question_id, $courseId);
548
                        $questionObj->updateWeighting($globalScore);
549
                        $questionObj->save();
550
                        break;
551
                    case MATCHING:
552
                        $globalScore = $score_list[$i][3];
553
                        $position = 1;
554
                        $objAnswer = new Answer($question_id, $courseId);
555
                        foreach ($answerList as $data) {
556
                            $option = isset($data[3]) ? $data[3] : '';
557
                            $objAnswer->createAnswer($option, 0, '', 0, $position);
558
                            $position++;
559
                        }
560
561
                        $counter = 1;
562
                        foreach ($answerList as $data) {
563
                            $value = isset($data[2]) ? $data[2] : '';
564
                            $position++;
565
566
                            $objAnswer->createAnswer(
567
                                $value,
568
                                $counter,
569
                                ' ',
570
                                $globalScore,
571
                                $position
572
                            );
573
                            $counter++;
574
                        }
575
576
                        $objAnswer->save();
577
578
                        $questionObj = Question::read($question_id, $courseId);
579
                        $questionObj->updateWeighting($globalScore);
580
                        $questionObj->save();
581
582
                        break;
583
                }
584
            }
585
        }
586
        $lpFromSession = Session::read('lpobject');
587
588
        if (isset($lpFromSession)) {
589
            if ($debug > 0) {
590
                error_log('New LP - SESSION[lpobject] is defined', 0);
591
            }
592
            $oLP = unserialize($lpFromSession);
593
            if (is_object($oLP)) {
594
                if ($debug > 0) {
595
                    error_log('New LP - oLP is object', 0);
596
                }
597
                if ((empty($oLP->cc)) OR $oLP->cc != api_get_course_id()) {
598
                    if ($debug > 0) {
599
                        error_log('New LP - Course has changed, discard lp object', 0);
600
                    }
601
                    $oLP = null;
602
                    Session::erase('oLP');
603
                    Session::erase('lpobject');
604
                } else {
605
                    Session::write('oLP', $oLP);
606
                }
607
            }
608
        }
609
        /** @var learnpath $lpFromSession */
610
        $lpFromSession = Session::read('oLP');
611
        if (isset($lpFromSession) && isset($_GET['lp_id'])) {
612
            $previous = $lpFromSession->select_previous_item_id();
613
            $parent = 0;
614
            // Add a Quiz as Lp Item
615
            $lpFromSession->add_item(
616
                $parent,
617
                $previous,
618
                TOOL_QUIZ,
619
                $quiz_id,
620
                $quiz_title,
621
                ''
622
            );
623
            // Redirect to home page for add more content
624
            header(
625
                'location: ../newscorm/lp_controller.php?'.api_get_cidreq(
626
                ).'&action=add_item&type=step&lp_id='.intval($_GET['lp_id'])
627
            );
628
            exit;
629 View Code Duplication
        } else {
630
            echo '<script>window.location.href = "'.api_get_path(WEB_CODE_PATH).'exercice/admin.php?'.api_get_cidReq().'&exerciseId='.$quiz_id.'&session_id='.api_get_session_id().'"</script>';
631
        }
632
    }
633
}
634
635
/**
636
 * @param array $answers_data
637
 * @return int
638
 */
639
function detectQuestionType($answers_data) {
640
    $correct = 0;
641
    $isNumeric = false;
642
643
    if (empty($answers_data)) {
644
        return FREE_ANSWER;
645
    }
646
    foreach ($answers_data as $answer_data) {
647
        if (strtolower($answer_data[3]) == 'x') {
648
            $correct++;
649
        } else {
650
            if (is_numeric($answer_data[3])) {
651
                $isNumeric = true;
652
            }
653
        }
654
    }
655
656
    if ($correct == 1) {
657
        $type = UNIQUE_ANSWER;
658
    } else if ($correct > 1) {
659
        $type = MULTIPLE_ANSWER;
660
    } else {
661
        $type = FREE_ANSWER;
662
    }
663
664
    if ($type == MULTIPLE_ANSWER) {
665
        if ($isNumeric) {
666
            $type = MULTIPLE_ANSWER;
667
        } else {
668
            $type = GLOBAL_MULTIPLE_ANSWER;
669
        }
670
    }
671
672
    return $type;
673
}
674
675
if (!isset($origin) || isset($origin) && $origin != 'learnpath') {
676
    //so we are not in learnpath tool
677
    Display :: display_footer();
678
}
679