Issues (2128)

main/exercise/question_create.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
require_once __DIR__.'/../inc/global.inc.php';
6
7
// the section (tabs)
8
$this_section = SECTION_COURSES;
9
10
// notice for unauthorized people.
11
api_protect_course_script(true);
12
13
$allow = api_is_allowed_to_edit();
14
15
if (!$allow) {
16
    api_not_allowed(true);
17
}
18
19
// breadcrumbs
20
$interbreadcrumb[] = ["url" => "exercise.php", "name" => get_lang('Exercises')];
21
22
// Tool name
23
$nameTools = get_lang('AddQuestionToExercise');
24
25
// The form
26
$form = new FormValidator('add_question', 'post', api_get_self().'?'.api_get_cidreq());
27
// form title
28
$form->addElement('header', '', get_lang('AddQuestionToExercise'));
29
30
$question_list = Question::getQuestionTypeList();
31
$question_list_options = [];
32
foreach ($question_list as $key => $value) {
33
    $question_list_options[$key] = addslashes(get_lang($value[1]));
34
}
35
$form->addElement(
36
    'select',
37
    'question_type_hidden',
38
    get_lang('QuestionType'),
39
    $question_list_options,
40
    ['id' => 'question_type_hidden']
41
);
42
43
//session id
44
$session_id = api_get_session_id();
45
46
// the exercises
47
$tbl_exercises = Database::get_course_table(TABLE_QUIZ_TEST);
48
$course_id = api_get_course_int_id();
49
50
$sql = "SELECT iid, title, type, description, results_disabled
51
        FROM $tbl_exercises
52
        WHERE c_id = $course_id AND active<>'-1' AND session_id = ".$session_id."
53
        ORDER BY title ASC";
54
$result = Database::query($sql);
55
$exercises['-'] = '-'.get_lang('SelectExercise').'-';
56
while ($row = Database::fetch_array($result)) {
57
    $exercises[$row['iid']] = cut($row['title'], EXERCISE_MAX_NAME_SIZE);
58
}
59
$form->addElement('select', 'exercise', get_lang('Exercise'), $exercises);
60
61
// generate default content
62
$form->addElement(
63
    'checkbox',
64
    'is_content',
65
    null,
66
    get_lang('GenerateDefaultContent'),
67
    ['checked' => true]
68
);
69
70
// the submit button
71
$form->addButtonCreate(get_lang('CreateQuestion'), 'SubmitCreateQuestion');
72
73
// setting the rules
74
$form->addRule('exercise', get_lang('ThisFieldIsRequired'), 'required');
75
$form->addRule('exercise', get_lang('YouHaveToSelectATest'), 'numeric');
76
77
$form->registerRule('validquestiontype', 'callback', 'check_question_type');
78
$form->addRule('question_type_hidden', get_lang('InvalidQuestionType'), 'validquestiontype');
79
80
if ($form->validate()) {
81
    $values = $form->exportValues();
82
    $answer_type = $values['question_type_hidden'];
83
84
    // check feedback_type from current exercise for type of question delineation
85
    $exercise_id = (int) $values['exercise'];
86
    $sql = "SELECT feedback_type FROM $tbl_exercises WHERE iid = $exercise_id";
87
    $rs_feedback_type = Database::query($sql);
88
    $row_feedback_type = Database::fetch_row($rs_feedback_type);
89
    $feedback_type = $row_feedback_type[0];
90
91
    // if question type does not belong to self-evaluation (immediate feedback) it'll send an error
92
    if (($answer_type == HOT_SPOT_DELINEATION && $feedback_type != 1) ||
93
        ($feedback_type == 1 && ($answer_type != HOT_SPOT_DELINEATION && $answer_type != UNIQUE_ANSWER))) {
94
        header('Location: question_create.php?'.api_get_cidreq().'&error=true');
95
        exit;
96
    }
97
    header('Location: admin.php?exerciseId='.$values['exercise'].'&newQuestion=yes&isContent='.$values['is_content'].'&answerType='.$answer_type);
98
    exit;
99
} else {
100
    // header
101
    Display::display_header($nameTools);
102
103
    echo '<div class="actions">';
104
    echo '<a href="exercise.php?show=test">'.Display::return_icon('back.png', get_lang('BackToExercisesList'), '', ICON_SIZE_MEDIUM).'</a>';
105
    echo '</div>';
106
107
    // displaying the form
108
    $form->display();
109
110
    // footer
111
    Display::display_footer();
112
}
113
114
function check_question_type($parameter)
115
{
116
    $question_list = Question::getQuestionTypeList();
117
    foreach ($question_list as $key => $value) {
118
        $valid_question_types[] = $key;
119
    }
120
    if (in_array($parameter, $valid_question_types)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $valid_question_types seems to be defined by a foreach iteration on line 117. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
121
        return true;
122
    } else {
123
        return false;
124
    }
125
}
126