Completed
Push — master ( 6c6efe...024a20 )
by Julito
09:28
created

getExtraFieldConditions()   B

Complexity

Conditions 9
Paths 18

Size

Total Lines 61
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 41
c 0
b 0
f 0
nc 18
nop 2
dl 0
loc 61
rs 7.7084

How to fix   Long Method   

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
use ChamiloSession as Session;
5
use Knp\Component\Pager\Paginator;
6
7
/**
8
 * Question Pool
9
 * This script allows administrators to manage questions and add them into their exercises.
10
 * One question can be in several exercises.
11
 *
12
 * @author Olivier Brouckaert
13
 * @author Julio Montoya adding support to query all questions from all session, courses, exercises
14
 * @author Modify by hubert borderiou 2011-10-21 Question's category
15
 */
16
require_once __DIR__.'/../inc/global.inc.php';
17
18
api_protect_course_script(true);
19
20
$this_section = SECTION_COURSES;
21
22
$is_allowedToEdit = api_is_allowed_to_edit(null, true);
23
24
$delete = isset($_GET['delete']) ? (int) $_GET['delete'] : null;
25
$recup = isset($_GET['recup']) ? (int) $_GET['recup'] : null;
26
$fromExercise = isset($_REQUEST['fromExercise']) ? (int) $_REQUEST['fromExercise'] : null;
27
$exerciseId = isset($_REQUEST['exerciseId']) ? (int) $_REQUEST['exerciseId'] : null;
28
$courseCategoryId = isset($_REQUEST['courseCategoryId']) ? (int) $_REQUEST['courseCategoryId'] : null;
29
$exerciseLevel = isset($_REQUEST['exerciseLevel']) ? (int) $_REQUEST['exerciseLevel'] : -1;
30
$answerType = isset($_REQUEST['answerType']) ? (int) $_REQUEST['answerType'] : null;
31
$question_copy = isset($_REQUEST['question_copy']) ? (int) $_REQUEST['question_copy'] : 0;
32
$session_id = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : null;
33
$selected_course = isset($_GET['selected_course']) ? (int) $_GET['selected_course'] : null;
34
// save the id of the previous course selected by user to reset menu if we detect that user change course hub 13-10-2011
35
$course_id_changed = isset($_GET['course_id_changed']) ? (int) $_GET['course_id_changed'] : null;
36
// save the id of the previous exercise selected by user to reset menu if we detect that user change course hub 13-10-2011
37
$exercise_id_changed = isset($_GET['exercise_id_changed']) ? (int) $_GET['exercise_id_changed'] : null;
38
$questionId = isset($_GET['question_id']) && !empty($_GET['question_id']) ? (int) $_GET['question_id'] : '';
39
$description = isset($_GET['description']) ? Database::escape_string($_GET['description']) : '';
40
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
41
42
// by default when we go to the page for the first time, we select the current course
43
if (!isset($_GET['selected_course']) && !isset($_GET['exerciseId'])) {
44
    $selected_course = api_get_course_int_id();
45
}
46
47
$_course = api_get_course_info();
48
$objExercise = new Exercise();
49
if (!empty($fromExercise)) {
50
    $objExercise->read($fromExercise, false);
51
}
52
53
$nameTools = get_lang('Recycle existing questions');
54
$interbreadcrumb[] = ['url' => 'exercise.php?'.api_get_cidreq(), 'name' => get_lang('Tests')];
55
56
if (!empty($objExercise->id)) {
57
    $interbreadcrumb[] = [
58
        'url' => 'admin.php?exerciseId='.$objExercise->id.'&'.api_get_cidreq(),
59
        'name' => $objExercise->selectTitle(true),
60
    ];
61
}
62
63
// message to be displayed if actions successful
64
$displayMessage = '';
65
if ($is_allowedToEdit) {
66
    // Duplicating a Question
67
    if (!isset($_POST['recup']) && 0 != $question_copy && isset($fromExercise)) {
68
        $origin_course_id = (int) $_GET['course_id'];
69
        $origin_course_info = api_get_course_info_by_id($origin_course_id);
70
        $current_course = api_get_course_info();
71
        $old_question_id = $question_copy;
72
        // Reading the source question
73
        $old_question_obj = Question::read($old_question_id, $origin_course_info);
74
        $courseId = $current_course['real_id'];
75
        if ($old_question_obj) {
76
            $old_question_obj->updateTitle($old_question_obj->selectTitle().' - '.get_lang('Copy'));
77
            //Duplicating the source question, in the current course
78
            $new_id = $old_question_obj->duplicate($current_course);
79
            //Reading new question
80
            $new_question_obj = Question::read($new_id);
81
            $new_question_obj->addToList($fromExercise);
82
            //Reading Answers obj of the current course
83
            $new_answer_obj = new Answer($old_question_id, $origin_course_id);
84
            $new_answer_obj->read();
85
            //Duplicating the Answers in the current course
86
            $new_answer_obj->duplicate($new_question_obj, $current_course);
87
            // destruction of the Question object
88
            unset($new_question_obj);
89
            unset($old_question_obj);
90
91
            $objExercise = new Exercise($courseId);
92
            $objExercise->read($fromExercise);
93
            Session::write('objExercise', $objExercise);
94
        }
95
        $displayMessage = get_lang('Item added');
96
    }
97
98
    // Deletes a question from the database and all exercises
99
    if ($delete) {
100
        $limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access');
101
        if ($limitTeacherAccess && !api_is_platform_admin()) {
102
            api_not_allowed(true);
103
        }
104
        // Construction of the Question object
105
        $objQuestionTmp = Question::read($delete);
106
        // if the question exists
107
        if ($objQuestionTmp) {
108
            // deletes the question from all exercises
109
            $objQuestionTmp->delete();
110
        }
111
        // destruction of the Question object
112
        unset($objQuestionTmp);
113
    } elseif ($recup && $fromExercise) {
114
        // gets an existing question and copies it into a new exercise
115
        $objQuestionTmp = Question::read($recup);
116
        // if the question exists
117
        if ($objQuestionTmp) {
118
            /* Adds the exercise ID represented by $fromExercise into the list
119
            of exercises for the current question */
120
            $objQuestionTmp->addToList($fromExercise);
121
        }
122
        // destruction of the Question object
123
        unset($objQuestionTmp);
124
125
        if (!$objExercise instanceof Exercise) {
126
            $objExercise = new Exercise();
127
            $objExercise->read($fromExercise);
128
        }
129
        // Adds the question ID represented by $recup into the list of questions for the current exercise
130
        $objExercise->addToList($recup);
131
        Session::write('objExercise', $objExercise);
132
        Display::addFlash(Display::return_message(get_lang('Item added'), 'success'));
133
    } elseif (isset($_POST['recup']) && is_array($_POST['recup']) && $fromExercise) {
134
        $list_recup = $_POST['recup'];
135
        foreach ($list_recup as $course_id => $question_data) {
136
            $origin_course_id = (int) $course_id;
137
            $origin_course_info = api_get_course_info_by_id($origin_course_id);
138
            $current_course = api_get_course_info();
139
            foreach ($question_data as $old_question_id) {
140
                // Reading the source question
141
                $old_question_obj = Question::read($old_question_id, $origin_course_info);
142
                if ($old_question_obj) {
143
                    $old_question_obj->updateTitle(
144
                        $old_question_obj->selectTitle().' - '.get_lang('Copy')
145
                    );
146
147
                    // Duplicating the source question, in the current course
148
                    $new_id = $old_question_obj->duplicate($current_course);
149
150
                    // Reading new question
151
                    $new_question_obj = Question::read($new_id);
152
                    $new_question_obj->addToList($fromExercise);
153
154
                    //Reading Answers obj of the current course
155
                    $new_answer_obj = new Answer($old_question_id, $origin_course_id);
156
                    $new_answer_obj->read();
157
158
                    //Duplicating the Answers in the current course
159
                    $new_answer_obj->duplicate($new_question_obj, $current_course);
160
161
                    // destruction of the Question object
162
                    unset($new_question_obj);
163
                    unset($old_question_obj);
164
165
                    if (!$objExercise instanceof Exercise) {
166
                        $objExercise = new Exercise();
167
                        $objExercise->read($fromExercise);
168
                    }
169
                }
170
            }
171
        }
172
        Session::write('objExercise', $objExercise);
173
    }
174
}
175
176
if (api_is_in_gradebook()) {
177
    $interbreadcrumb[] = [
178
        'url' => Category::getUrl(),
179
        'name' => get_lang('Assessments'),
180
    ];
181
}
182
183
// if admin of course
184
if (!$is_allowedToEdit) {
185
    api_not_allowed(true);
186
}
187
188
$confirmYourChoice = addslashes(api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES));
189
$htmlHeadXtra[] = "
190
<script>
191
    function submit_form(obj) {
192
        document.question_pool.submit();
193
    }
194
195
    function mark_course_id_changed() {
196
        $('#course_id_changed').val('1');
197
    }
198
199
    function mark_exercise_id_changed() {
200
        $('#exercise_id_changed').val('1');
201
    }
202
203
    function confirm_your_choice() {
204
        return confirm('$confirmYourChoice');
205
    }
206
</script>";
207
208
$url = api_get_self().'?'.api_get_cidreq().'&'.http_build_query(
209
    [
210
        'fromExercise' => $fromExercise,
211
        'session_id' => $session_id,
212
        'selected_course' => $selected_course,
213
        'courseCategoryId' => $courseCategoryId,
214
        'exerciseId' => $exerciseId,
215
        'exerciseLevel' => $exerciseLevel,
216
        'answerType' => $answerType,
217
        'question_id' => $questionId,
218
        'description' => Security::remove_XSS($description),
219
        'course_id_changed' => $course_id_changed,
220
        'exercise_id_changed' => $exercise_id_changed,
221
    ]
222
);
223
224
if (isset($_REQUEST['action'])) {
225
    switch ($_REQUEST['action']) {
226
        case 'reuse':
227
            if (!empty($_REQUEST['questions']) && !empty($fromExercise)) {
228
                $questions = $_REQUEST['questions'];
229
                $objExercise = new Exercise();
230
                $objExercise->read($fromExercise, false);
231
232
                if (count($questions) > 0) {
233
                    foreach ($questions as $questionId) {
234
                        // gets an existing question and copies it into a new exercise
235
                        $objQuestionTmp = Question::read($questionId);
236
                        // if the question exists
237
                        if ($objQuestionTmp) {
238
                            if (false === $objExercise->hasQuestion($questionId)) {
239
                                $objExercise->addToList($questionId);
240
                                $objQuestionTmp->addToList($fromExercise);
241
                            }
242
                        }
243
                    }
244
                }
245
246
                Display::addFlash(Display::return_message(get_lang('Added')));
247
                header('Location: '.$url);
248
                exit;
249
            }
250
251
            break;
252
        case 'clone':
253
            if (!empty($_REQUEST['questions']) && !empty($fromExercise)) {
254
                $questions = $_REQUEST['questions'];
255
                $origin_course_id = (int) $_GET['course_id'];
256
257
                $origin_course_info = api_get_course_info_by_id($origin_course_id);
258
                $current_course = api_get_course_info();
259
260
                if (count($questions) > 0) {
261
                    foreach ($questions as $questionId) {
262
                        // gets an existing question and copies it into a new exercise
263
                        // Reading the source question
264
                        $old_question_obj = Question::read($questionId, $origin_course_info);
265
                        $courseId = $current_course['real_id'];
266
                        if ($old_question_obj) {
267
                            $old_question_obj->updateTitle($old_question_obj->selectTitle().' - '.get_lang('Copy'));
268
                            // Duplicating the source question, in the current course
269
                            $new_id = $old_question_obj->duplicate($current_course);
270
                            // Reading new question
271
                            $new_question_obj = Question::read($new_id);
272
                            $new_question_obj->addToList($fromExercise);
273
                            //Reading Answers obj of the current course
274
                            $new_answer_obj = new Answer($questionId, $origin_course_id);
275
                            $new_answer_obj->read();
276
                            //Duplicating the Answers in the current course
277
                            $new_answer_obj->duplicate($new_question_obj, $current_course);
278
                            // destruction of the Question object
279
                            unset($new_question_obj);
280
                            unset($old_question_obj);
281
                        }
282
                    }
283
                }
284
285
                Display::addFlash(Display::return_message(get_lang('Added')));
286
                header('Location: '.$url);
287
                exit;
288
            }
289
290
            break;
291
    }
292
}
293
294
Display::display_header($nameTools, 'Exercise');
295
296
// Menu
297
echo '<div class="actions">';
298
if (isset($fromExercise) && $fromExercise > 0) {
299
    echo '<a href="admin.php?'.api_get_cidreq().'&exerciseId='.$fromExercise.'">'.
300
            Display::return_icon('back.png', get_lang('Go back to the questions list'), '', ICON_SIZE_MEDIUM).'</a>';
301
    $titleAdd = get_lang('Add question to test');
302
} else {
303
    echo '<a href="exercise.php?'.api_get_cidreq().'">'.
304
        Display::return_icon('back.png', get_lang('BackToTestsList'), '', ICON_SIZE_MEDIUM).'</a>';
305
    echo "<a href='admin.php?exerciseId=0'>".
306
        Display::return_icon('add_question.gif', get_lang('New question'), '', ICON_SIZE_MEDIUM).'</a>';
307
    $titleAdd = get_lang('Manage all questions');
308
}
309
echo '</div>';
310
311
if ('' != $displayMessage) {
312
    echo Display::return_message($displayMessage, 'confirm');
313
}
314
315
// Form
316
$sessionList = SessionManager::get_sessions_by_user(api_get_user_id(), api_is_platform_admin());
317
$session_select_list = ['-1' => get_lang('Select')];
318
foreach ($sessionList as $item) {
319
    $session_select_list[$item['session_id']] = $item['session_name'];
320
}
321
322
// Course list, get course list of session, or for course where user is admin
323
$course_list = [];
324
325
// Course list, get course list of session, or for course where user is admin
326
if (!empty($session_id) && $session_id != '-1' && !empty($sessionList)) {
327
    $sessionInfo = [];
328
    foreach ($sessionList as $session) {
329
        if ($session['session_id'] == $session_id) {
330
            $sessionInfo = $session;
331
        }
332
    }
333
    $course_list = $sessionInfo['courses'];
334
} else {
335
    if (api_is_platform_admin()) {
336
        $course_list = CourseManager::get_courses_list(0, 0, 'title');
337
    } else {
338
        $course_list = CourseManager::get_course_list_of_user_as_course_admin(api_get_user_id());
339
    }
340
341
    // Admin fix, add the current course in the question pool.
342
    if (api_is_platform_admin()) {
343
        $courseInfo = api_get_course_info();
344
        if (!empty($course_list)) {
345
            if (!in_array($courseInfo['real_id'], $course_list)) {
346
                $course_list = array_merge($course_list, [$courseInfo]);
347
            }
348
        } else {
349
            $course_list = [$courseInfo];
350
        }
351
    }
352
}
353
354
$course_select_list = ['-1' => get_lang('Select')];
355
foreach ($course_list as $item) {
356
    $courseItemId = $item['real_id'];
357
    $courseInfo = api_get_course_info_by_id($courseItemId);
358
    $course_select_list[$courseItemId] = '';
359
    if ($courseItemId == api_get_course_int_id()) {
360
        $course_select_list[$courseItemId] = '>&nbsp;&nbsp;&nbsp;&nbsp;';
361
    }
362
    $course_select_list[$courseItemId] .= $courseInfo['title'];
363
}
364
365
if (empty($selected_course) || $selected_course == '-1') {
366
    $course_info = api_get_course_info();
367
    // no course selected, reset menu test / difficult� / type de reponse
368
    reset_menu_exo_lvl_type();
369
} else {
370
    $course_info = api_get_course_info_by_id($selected_course);
371
}
372
// If course has changed, reset the menu default
373
if ($course_id_changed) {
374
    reset_menu_exo_lvl_type();
375
}
376
377
// Get category list for the course $selected_course
378
$categoryList = TestCategory::getCategoriesForSelect();
379
$selectCourseCategory = Display::select(
380
    'courseCategoryId',
381
    $categoryList,
382
    $courseCategoryId,
383
    ['onchange' => 'submit_form(this);'],
384
    false
385
);
386
echo Display::form_row(get_lang('Questions category'), $selectCourseCategory);
387
388
// Get exercise list for this course
389
$exercise_list = ExerciseLib::get_all_exercises_for_course_id(
390
    $course_info,
391
    $session_id,
392
    $selected_course,
393
    false
394
);
395
396
if (1 == $exercise_id_changed) {
397
    reset_menu_lvl_type();
398
}
399
400
// Exercise List
401
$my_exercise_list = [];
402
$my_exercise_list['0'] = get_lang('AllTests');
403
$my_exercise_list['-1'] = get_lang('Orphan questions');
404
$titleSavedAsHtml = api_get_configuration_value('save_titles_as_html');
405
if (is_array($exercise_list)) {
406
    foreach ($exercise_list as $row) {
407
        $my_exercise_list[$row['id']] = '';
408
        if ($row['id'] == $fromExercise && $selected_course == api_get_course_int_id()) {
409
            $my_exercise_list[$row['id']] = '>&nbsp;&nbsp;&nbsp;&nbsp;';
410
        }
411
412
        $exerciseTitle = $row['title'];
413
        if ($titleSavedAsHtml) {
414
            $exerciseTitle = strip_tags(api_html_entity_decode(trim($exerciseTitle)));
415
        }
416
        $my_exercise_list[$row['id']] .= $exerciseTitle;
417
    }
418
}
419
420
// Difficulty list (only from 0 to 5)
421
$levels = [
422
    -1 => get_lang('All'),
423
    0 => 0,
424
    1 => 1,
425
    2 => 2,
426
    3 => 3,
427
    4 => 4,
428
    5 => 5,
429
];
430
431
// Answer type
432
$question_list = Question::getQuestionTypeList();
433
434
$new_question_list = [];
435
$new_question_list['-1'] = get_lang('All');
436
if (!empty($_course)) {
437
    foreach ($question_list as $key => $item) {
438
        if (in_array(
439
            $objExercise->getFeedbackType(),
440
            [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP]
441
        )) {
442
            if (!in_array($key, [HOT_SPOT_DELINEATION, UNIQUE_ANSWER])) {
443
                continue;
444
            }
445
            $new_question_list[$key] = get_lang($item[1]);
446
        } else {
447
            if (HOT_SPOT_DELINEATION == $key) {
448
                continue;
449
            }
450
            $new_question_list[$key] = get_lang($item[1]);
451
        }
452
    }
453
}
454
455
// Answer type list
456
$form = new FormValidator('question_pool', 'GET', $url);
457
$form->addHeader($nameTools.' - '.$titleAdd);
458
$form->addHidden('fromExercise', $fromExercise);
459
$form
460
    ->addSelect(
461
        'session_id',
462
        get_lang('Session'),
463
        $session_select_list,
464
        ['onchange' => 'submit_form(this)', 'id' => 'session_id']
465
    )
466
    ->setSelected($session_id);
467
$form
468
    ->addSelect(
469
        'selected_course',
470
        get_lang('Course'),
471
        $course_select_list,
472
        ['onchange' => 'mark_course_id_changed(); submit_form(this);', 'id' => 'selected_course']
473
    )
474
    ->setSelected($selected_course);
475
$form
476
    ->addSelect(
477
        'courseCategoryId',
478
        get_lang('QuestionCategory'),
479
        $categoryList,
480
        ['onchange' => 'submit_form(this);', 'id' => 'courseCategoryId']
481
    )
482
    ->setSelected($courseCategoryId);
483
$form
484
    ->addSelect(
485
        'exerciseId',
486
        get_lang('Exercise'),
487
        $my_exercise_list,
488
        ['onchange' => 'mark_exercise_id_changed(); submit_form(this);', 'id' => 'exerciseId']
489
    )
490
    ->setSelected($exerciseId);
491
$form
492
    ->addSelect(
493
        'exerciseLevel',
494
        get_lang('Difficulty'),
495
        $levels,
496
        ['onchange' => 'submit_form(this);', 'id' => 'exerciseLevel']
497
    )
498
    ->setSelected($exerciseLevel);
499
$form
500
    ->addSelect(
501
    'answerType',
502
        get_lang('AnswerType'),
503
    $new_question_list,
504
        ['onchange' => 'submit_form(this);', 'id' => 'answerType']
505
    )
506
    ->setSelected($answerType);
507
$form
508
    ->addText('question_id', get_lang('Id'), false)
509
    ->setValue($questionId);
510
$form
511
    ->addText('description', get_lang('Description'), false)
512
    ->setValue(Security::remove_XSS($description));
513
514
$form->addHidden('course_id_changed', '0');
515
$form->addHidden('exercise_id_changed', '0');
516
517
$extraField = new ExtraField('question');
518
$jsForExtraFields = $extraField->addElements($form, 0, [], true);
519
520
$form->addButtonFilter(get_lang('Filter'), 'name');
521
522
echo $form->display();
523
524
echo '<script>$(function () {
525
        '.$jsForExtraFields['jquery_ready_content'].'
526
    })</script>';
527
?>
528
<div class="clear"></div>
529
<?php
530
531
/**
532
 * @return array
533
 */
534
function getExtraFieldConditions(array $formValues, $queryType = 'from')
535
{
536
    $extraField = new ExtraField('question');
537
    $fields = $extraField->get_all(
538
        ['visible_to_self = ? AND filter = ?' => [1, 1]],
539
        'display_text'
540
    );
541
542
    $from = '';
543
    $where = '';
544
545
    foreach ($fields as $field) {
546
        $variable = $field['variable'];
547
548
        if (empty($formValues["extra_$variable"])) {
549
            continue;
550
        }
551
552
        $value = $formValues["extra_$variable"];
553
554
        switch ($field['field_type']) {
555
            case ExtraField::FIELD_TYPE_CHECKBOX:
556
                $value = $value["extra_$variable"];
557
                break;
558
            case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
559
                if (!isset($value["extra_{$variable}_second"])) {
560
                    $value = null;
561
                    break;
562
                }
563
564
                $value = $value["extra_$variable"].'::'.$value["extra_{$variable}_second"];
565
                break;
566
        }
567
568
        if (empty($value)) {
569
            continue;
570
        }
571
572
        if ($queryType === 'from') {
573
            $from .= ", extra_field_values efv_$variable, extra_field ef_$variable";
574
            $where .= "AND (
575
                    qu.iid = efv_$variable.item_id
576
                    AND efv_$variable.field_id = ef_$variable.id
577
                    AND ef_$variable.extra_field_type = ".ExtraFieldEntity::QUESTION_FIELD_TYPE."
578
                    AND ef_$variable.variable = '$variable'
579
                    AND efv_$variable.value = '$value'
580
                )";
581
        } elseif ($queryType === 'join') {
582
            $from .= " INNER JOIN extra_field_values efv_$variable ON qu.iid = efv_$variable.item_id
583
                INNER JOIN extra_field ef_$variable ON efv_$variable.field_id = ef_$variable.id";
584
            $where .= "AND (
585
                    ef_$variable.extra_field_type = ".ExtraFieldEntity::QUESTION_FIELD_TYPE."
586
                    AND ef_$variable.variable = '$variable'
587
                    AND efv_$variable.value = '$value'
588
                )";
589
        }
590
    }
591
592
    return [
593
        'from' => $from,
594
        'where' => $where,
595
    ];
596
}
597
598
function getQuestions(
599
    $getCount,
600
    $start,
601
    $length,
602
    $exerciseId,
603
    $courseCategoryId,
604
    $selected_course,
605
    $session_id,
606
    $exerciseLevel,
607
    $answerType,
608
    $questionId,
609
    $description,
610
    $fromExercise = 0,
611
    $formValues = []
612
) {
613
    $start = (int) $start;
614
    $length = (int) $length;
615
    $exerciseId = (int) $exerciseId;
616
    $courseCategoryId = (int) $courseCategoryId;
617
    $selected_course = (int) $selected_course;
618
    $session_id = (int) $session_id;
619
    $exerciseLevel = (int) $exerciseLevel;
620
    $answerType = (int) $answerType;
621
    $questionId = (int) $questionId;
622
    $fromExercise = (int) $fromExercise;
623
    $description = Database::escape_string($description);
624
625
    $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
626
    $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
627
    $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
628
    $TBL_COURSE_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
629
630
    $currentExerciseCondition = '';
631
    if (!empty($fromExercise)) {
632
        $currentCourseId = api_get_course_int_id();
633
        $currentExerciseCondition = "
634
            AND qu.id NOT IN (
635
                SELECT question_id FROM $TBL_EXERCISE_QUESTION
636
                WHERE exercice_id = $fromExercise AND c_id = $currentCourseId
637
            )";
638
    }
639
640
    // if we have selected an exercise in the list-box 'Filter'
641
    if ($exerciseId > 0) {
642
        $efConditions = getExtraFieldConditions($formValues, 'from');
643
644
        $where = '';
645
        $from = '';
646
        if (isset($courseCategoryId) && $courseCategoryId > 0) {
647
            $from = ", $TBL_COURSE_REL_CATEGORY crc ";
648
            $where .= " AND
649
                    crc.c_id = $selected_course AND
650
                    crc.question_id = qu.id AND
651
                    crc.category_id = $courseCategoryId";
652
        }
653
        if (isset($exerciseLevel) && -1 != $exerciseLevel) {
654
            $where .= ' AND level='.$exerciseLevel;
655
        }
656
        if (isset($answerType) && $answerType > 0) {
657
            $where .= ' AND type='.$answerType;
658
        }
659
660
        if (!empty($questionId)) {
661
            $where .= ' AND qu.iid='.$questionId;
662
        }
663
664
        if (!empty($description)) {
665
            $where .= " AND qu.description LIKE '%$description%'";
666
        }
667
668
        $select = 'DISTINCT
669
                    id,
670
                    question,
671
                    type,
672
                    level,
673
                    qt.exercice_id exerciseId';
674
        if ($getCount) {
675
            $select = 'count(qu.iid) as count';
676
        }
677
        $sql = "SELECT $select
678
                FROM
679
                    $TBL_EXERCISE_QUESTION qt
680
                    INNER JOIN $TBL_QUESTIONS qu
681
                    ON qt.question_id = qu.id
682
                    $from
683
                    {$efConditions['from']}
684
                WHERE
685
                    qt.exercice_id = $exerciseId AND
686
                    qt.c_id = $selected_course  AND
687
                    qu.c_id = $selected_course
688
                    $where
689
                    $currentExerciseCondition
690
                    {$efConditions['where']}
691
                ORDER BY BINARY qu.question ASC
692
                 ";
693
    } elseif ($exerciseId == -1) {
694
        $efConditions = getExtraFieldConditions($formValues, 'join');
695
        // If we have selected the option 'Orphan questions' in the list-box 'Filter'
696
        $level_where = '';
697
        $from = '';
698
        if (isset($courseCategoryId) && $courseCategoryId > 0) {
699
            $from = " INNER JOIN $TBL_COURSE_REL_CATEGORY crc
700
                      ON crc.question_id = q.id AND crc.c_id = q.c_id ";
701
            $level_where .= " AND
702
                    crc.c_id = $selected_course AND
703
                    crc.category_id = $courseCategoryId";
704
        }
705
        if (isset($exerciseLevel) && -1 != $exerciseLevel) {
706
            $level_where = ' AND level='.$exerciseLevel;
707
        }
708
        $answer_where = '';
709
        if (isset($answerType) && $answerType > 0 - 1) {
710
            $answer_where = ' AND qu.type='.$answerType;
711
        }
712
713
        if (!empty($questionId)) {
714
            $answer_where .= ' AND q.iid='.$questionId;
715
        }
716
717
        if (!empty($description)) {
718
            $answer_where .= " AND q.description LIKE '%$description%'";
719
        }
720
721
        $select = ' qu.*, r.exercice_id exerciseId  ';
722
        if ($getCount) {
723
            $select = 'count(qu.iid) as count';
724
        }
725
726
        // @todo fix this query with the new id field
727
        $sql = " (
728
                    SELECT $select
729
                    FROM $TBL_QUESTIONS qu
730
                    INNER JOIN $TBL_EXERCISE_QUESTION r
731
                    ON (qu.c_id = r.c_id AND qu.id = r.question_id)
732
                    INNER JOIN $TBL_EXERCISES ex
733
                    ON (ex.id = r.exercice_id AND ex.c_id = r.c_id)
734
                    $from
735
                    {$efConditions['from']}
736
                    WHERE
737
                        ex.c_id = '$selected_course' AND
738
                        ex.active = '-1'
739
                        $level_where
740
                        $answer_where
741
                        {$efConditions['where']}
742
                )
743
                UNION
744
                (
745
                    SELECT $select
746
                    FROM $TBL_QUESTIONS qu
747
                    LEFT OUTER JOIN $TBL_EXERCISE_QUESTION r
748
                    ON (qu.c_id = r.c_id AND qu.id = r.question_id)
749
                    $from
750
                    {$efConditions['from']}
751
                    WHERE
752
                        qu.c_id = '$selected_course' AND
753
                        r.question_id is null
754
                        $level_where
755
                        $answer_where
756
                        {$efConditions['where']}
757
                )
758
                UNION
759
                (
760
                        SELECT $select
761
                        FROM $TBL_QUESTIONS qu
762
                        INNER JOIN $TBL_EXERCISE_QUESTION r
763
                        ON (qu.c_id = r.c_id AND qu.id = r.question_id)
764
                        $from
765
                        {$efConditions['from']}
766
                        WHERE
767
                            r.c_id = '$selected_course' AND
768
                            (r.exercice_id = '-1' OR r.exercice_id = '0')
769
                            $level_where
770
                            $answer_where
771
                            {$efConditions['where']}
772
                    )
773
                 ";
774
        if ($getCount) {
775
            $sql = "SELECT SUM(count) count FROM ($sql) as total";
776
        }
777
    } else {
778
        $efConditions = getExtraFieldConditions($formValues, 'from');
779
        // All tests for selected course
780
        // If we have not selected any option in the list-box 'Filter'
781
        $filter = '';
782
        $from = '';
783
        if (isset($courseCategoryId) && $courseCategoryId > 0) {
784
            $from = ", $TBL_COURSE_REL_CATEGORY crc ";
785
            $filter .= " AND
786
                        crc.c_id = $selected_course AND
787
                        crc.question_id = qu.id AND
788
                        crc.category_id = $courseCategoryId";
789
        }
790
        if (isset($exerciseLevel) && -1 != $exerciseLevel) {
791
            $filter .= ' AND level='.$exerciseLevel.' ';
792
        }
793
        if (isset($answerType) && $answerType > 0) {
794
            $filter .= ' AND qu.type='.$answerType.' ';
795
        }
796
797
        if (!empty($questionId)) {
798
            $filter .= ' AND qu.iid='.$questionId;
799
        }
800
801
        if (!empty($description)) {
802
            $filter .= " AND qu.description LIKE '%$description%'";
803
        }
804
805
        if (-1 == $session_id || empty($session_id)) {
806
            $session_id = 0;
807
        }
808
        $sessionCondition = api_get_session_condition($session_id, true, 'q.session_id');
809
810
        $select = 'qu.id, question, qu.type, level, q.session_id, qt.exercice_id exerciseId  ';
811
        if ($getCount) {
812
            $select = 'count(qu.iid) as count';
813
        }
814
815
        // All tests for the course selected, not in session
816
        $sql = "SELECT DISTINCT
817
                    $select
818
                FROM
819
                $TBL_QUESTIONS as qu,
820
                $TBL_EXERCISE_QUESTION as qt,
821
                $TBL_EXERCISES as q
822
                {$efConditions['from']}
823
                $from
824
                WHERE
825
                    qu.c_id = $selected_course AND
826
                    qt.c_id = $selected_course AND
827
                    q.c_id = $selected_course AND
828
                    qu.id = qt.question_id
829
                    $sessionCondition AND
830
                    q.id = qt.exercice_id
831
                    $filter
832
                    $currentExerciseCondition
833
                    {$efConditions['where']}
834
                GROUP BY qu.iid
835
                ORDER BY BINARY qu.question ASC
836
                ";
837
    }
838
839
    if ($getCount) {
840
        $result = Database::query($sql);
841
        $row = Database::fetch_array($result, 'ASSOC');
842
        if ($row) {
843
            return (int) $row['count'];
844
        }
845
846
        return 0;
847
    }
848
849
    $sql .= " LIMIT $start, $length";
850
    $result = Database::query($sql);
851
    $mainQuestionList = [];
852
    while ($row = Database::fetch_array($result, 'ASSOC')) {
853
        if ($exerciseId == -1 && isQuestionInActiveQuiz($row['iid'])) {
854
            continue;
855
        }
856
857
        $mainQuestionList[] = $row;
858
    }
859
860
    return $mainQuestionList;
861
}
862
863
$formValues = $form->validate() ? $form->exportValues() : [];
864
865
$nbrQuestions = getQuestions(
866
    true,
867
    null,
868
    null,
869
    $exerciseId,
870
    $courseCategoryId,
871
    $selected_course,
872
    $session_id,
873
    $exerciseLevel,
874
    $answerType,
875
    $questionId,
876
    $description,
877
    $fromExercise,
878
    $formValues
879
);
880
881
$length = api_get_configuration_value('question_pagination_length');
882
if (empty($length)) {
883
    $length = 20;
884
}
885
886
$start = ($page - 1) * $length;
887
888
$paginator = new Paginator();
889
$pagination = $paginator->paginate([]);
890
$pagination->setTotalItemCount($nbrQuestions);
891
$pagination->setItemNumberPerPage($length);
892
$pagination->setCurrentPageNumber($page);
893
894
$pagination->renderer = function ($data) use ($url) {
895
    $render = '';
896
    if ($data['pageCount'] > 1) {
897
        $render = '<ul class="pagination">';
898
        for ($i = 1; $i <= $data['pageCount']; $i++) {
899
            $pageContent = '<li><a href="'.$url.'&page='.$i.'">'.$i.'</a></li>';
900
            if ($data['current'] == $i) {
901
                $pageContent = '<li class="active"><a href="#" >'.$i.'</a></li>';
902
            }
903
            $render .= $pageContent;
904
        }
905
        $render .= '</ul>';
906
    }
907
908
    return $render;
909
};
910
911
$mainQuestionList = getQuestions(
912
    false,
913
    $start,
914
    $length,
915
    $exerciseId,
916
    $courseCategoryId,
917
    $selected_course,
918
    $session_id,
919
    $exerciseLevel,
920
    $answerType,
921
    $questionId,
922
    $description,
923
    $fromExercise,
924
    $formValues
925
);
926
927
// build the line of the array to display questions
928
// Actions are different if you launch the question_pool page
929
// They are different too if you have displayed questions from your course
930
// Or from another course you are the admin(or session admin)
931
// from a test or not
932
/*
933
+--------------------------------------------+--------------------------------------------+
934
|   NOT IN A TEST                            |         IN A TEST                          |
935
+----------------------+---------------------+---------------------+----------------------+
936
|IN THE COURSE (*)  "x | NOT IN THE COURSE o | IN THE COURSE    +  | NOT IN THE COURSE  o |
937
+----------------------+---------------------+---------------------+----------------------+
938
|Edit the question     | Do nothing          | Add question to test|Clone question in test|
939
|Delete the question   |                     |                     |                      |
940
|(true delete)         |                     |                     |                      |
941
+----------------------+---------------------+---------------------+----------------------+
942
(*) this is the only way to delete or modify orphan questions
943
*/
944
945
if ($fromExercise <= 0) {
946
    // NOT IN A TEST - NOT IN THE COURSE
947
    $actionLabel = get_lang('Re-use in current test');
948
    $actionIcon1 = get_lang('Must be in a test');
949
    $actionIcon2 = '';
950
    // We are not in this course, to messy if we link to the question in another course
951
    $questionTagA = 0;
952
    if ($selected_course == api_get_course_int_id()) {
953
        // NOT IN A TEST - IN THE COURSE
954
        $actionLabel = get_lang('Edit');
955
        $actionIcon1 = 'edit';
956
        $actionIcon2 = 'delete';
957
        // We are in the course, question title can be a link to the question edit page
958
        $questionTagA = 1;
959
    }
960
} else {
961
    // IN A TEST - NOT IN THE COURSE
962
    $actionLabel = get_lang('Re-use a copy inside the current test');
963
    $actionIcon1 = 'clone';
964
    $actionIcon2 = '';
965
    $questionTagA = 0;
966
967
    if ($selected_course == api_get_course_int_id()) {
968
        // IN A TEST - IN THE COURSE
969
        $actionLabel = get_lang('Re-use in current test');
970
        $actionIcon1 = 'add';
971
        $actionIcon2 = '';
972
        $questionTagA = 1;
973
    }
974
}
975
976
$data = [];
977
if (is_array($mainQuestionList)) {
978
    foreach ($mainQuestionList as $question) {
979
        $row = [];
980
        // This function checks if the question can be read
981
        $question_type = get_question_type_for_question($selected_course, $question['id']);
982
983
        if (empty($question_type)) {
984
            continue;
985
        }
986
        $sessionId = isset($question['session_id']) ? $question['session_id'] : null;
987
        if (!$objExercise->hasQuestion($question['id'])) {
988
            $row[] = Display::input(
989
                'checkbox',
990
                'questions[]',
991
                $question['id'],
992
                ['class' => 'question_checkbox']
993
            );
994
        } else {
995
            $row[] = '';
996
        }
997
998
        $row[] = getLinkForQuestion(
999
            $questionTagA,
1000
            $fromExercise,
1001
            $question['id'],
1002
            $question['type'],
1003
            $question['question'],
1004
            $sessionId,
1005
            $question['exerciseId']
1006
        );
1007
1008
        $row[] = $question_type;
1009
        $row[] = TestCategory::getCategoryNameForQuestion($question['id'], $selected_course);
1010
        $row[] = $question['level'];
1011
        $row[] = get_action_icon_for_question(
1012
            $actionIcon1,
1013
            $fromExercise,
1014
            $question['id'],
1015
            $question['type'],
1016
            $question['question'],
1017
            $selected_course,
1018
            $courseCategoryId,
1019
            $exerciseLevel,
1020
            $answerType,
1021
            $session_id,
1022
            $question['exerciseId'],
1023
            $objExercise
1024
        ).'&nbsp;'.
1025
        get_action_icon_for_question(
1026
            $actionIcon2,
1027
            $fromExercise,
1028
            $question['id'],
1029
            $question['type'],
1030
            $question['question'],
1031
            $selected_course,
1032
            $courseCategoryId,
1033
            $exerciseLevel,
1034
            $answerType,
1035
            $session_id,
1036
            $question['exerciseId'],
1037
            $objExercise
1038
        );
1039
        $data[] = $row;
1040
    }
1041
}
1042
1043
$headers = [
1044
        '',
1045
    get_lang('QuestionUpperCaseFirstLetter'),
1046
        get_lang('Type'),
1047
    get_lang('QuestionCategory'),
1048
        get_lang('Difficulty'),
1049
        $actionLabel,
1050
];
1051
1052
echo $pagination;
1053
echo '<form id="question_pool_id" method="get" action="'.$url.'">';
1054
echo '<input type="hidden" name="fromExercise" value="'.$fromExercise.'">';
1055
echo '<input type="hidden" name="cidReq" value="'.$_course['code'].'">';
1056
echo '<input type="hidden" name="selected_course" value="'.$selected_course.'">';
1057
echo '<input type="hidden" name="course_id" value="'.$selected_course.'">';
1058
1059
$table = new HTML_Table(['class' => 'table table-bordered data_table'], false);
1060
$row = 0;
1061
$column = 0;
1062
foreach ($headers as $header) {
1063
    $table->setHeaderContents($row, $column, $header);
1064
1065
    $column++;
1066
}
1067
1068
$row = 1;
1069
foreach ($data as $rows) {
1070
    $column = 0;
1071
    foreach ($rows as $value) {
1072
        $table->setCellContents($row, $column, $value);
1073
        $table->updateCellAttributes(
1074
            $row,
1075
            $column,
1076
            $value
1077
);
1078
        $column++;
1079
    }
1080
    $row++;
1081
}
1082
1083
$table->display();
1084
1085
echo '</form>';
1086
1087
$tableId = 'question_pool_id';
1088
$html = '<div class="btn-toolbar">';
1089
$html .= '<div class="btn-group">';
1090
$html .= '<a class="btn btn-default" href="?'.$url.'selectall=1" onclick="javascript: setCheckbox(true, \''.$tableId.'\'); return false;">'.
1091
    get_lang('Select all').'</a>';
1092
$html .= '<a class="btn btn-default" href="?'.$url.'" onclick="javascript: setCheckbox(false, \''.$tableId.'\'); return false;">'.get_lang('UnSelect all').'</a> ';
1093
$html .= '</div>';
1094
$html .= '<div class="btn-group">
1095
            <button class="btn btn-default" onclick="javascript:return false;">'.get_lang('Detail').'</button>
1096
            <button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
1097
                <span class="caret"></span>
1098
            </button>';
1099
$html .= '<ul class="dropdown-menu">';
1100
1101
$actionLabel = get_lang('Re-use a copy inside the current test');
1102
$actions = ['clone' => get_lang('Re-use a copy inside the current test')];
1103
if ($selected_course == api_get_course_int_id()) {
1104
    $actions = ['reuse' => get_lang('Re-use in current testQuestion')];
1105
}
1106
1107
foreach ($actions as $action => &$label) {
1108
    $html .= '<li>
1109
                <a data-action ="'.$action.'" href="#" onclick="javascript:action_click(this, \''.$tableId.'\');">'.
1110
                    $label.'
1111
                </a>
1112
              </li>';
1113
}
1114
$html .= '</ul>';
1115
$html .= '</div>'; //btn-group
1116
$html .= '</div>'; //toolbar
1117
1118
echo $html;
1119
1120
Display::display_footer();
1121
1122
/**
1123
 * Put the menu entry for level and type to default "Choice"
1124
 * It is useful if you change the exercise, you need to reset the other menus.
1125
 *
1126
 * @author hubert.borderiou 13-10-2011
1127
 */
1128
function reset_menu_lvl_type()
1129
{
1130
    global $exerciseLevel, $answerType;
1131
    $answerType = -1;
1132
    $exerciseLevel = -1;
1133
}
1134
1135
/**
1136
 * Put the menu entry for exercise and level and type to default "Choice"
1137
 * It is useful if you change the course, you need to reset the other menus.
1138
 *
1139
 * @author hubert.borderiou 13-10-2011
1140
 */
1141
function reset_menu_exo_lvl_type()
1142
{
1143
    global $exerciseId, $courseCategoryId;
1144
    reset_menu_lvl_type();
1145
    $exerciseId = 0;
1146
    $courseCategoryId = 0;
1147
}
1148
1149
/**
1150
 * return the <a> link to admin question, if needed.
1151
 *
1152
 * @param int    $in_addA
1153
 * @param int    $fromExercise
1154
 * @param int    $questionId
1155
 * @param int    $questionType
1156
 * @param string $questionName
1157
 * @param int    $sessionId
1158
 * @param int    $exerciseId
1159
 *
1160
 * @return string
1161
 *
1162
 * @author hubert.borderiou
1163
 */
1164
function getLinkForQuestion(
1165
    $in_addA,
1166
    $fromExercise,
1167
    $questionId,
1168
    $questionType,
1169
    $questionName,
1170
    $sessionId,
1171
    $exerciseId
1172
) {
1173
    $result = $questionName;
1174
    if ($in_addA) {
1175
        $sessionIcon = '';
1176
        if (!empty($sessionId) && -1 != $sessionId) {
1177
            $sessionIcon = ' '.Display::return_icon('star.png', get_lang('Session'));
1178
        }
1179
        $exerciseId = (int) $exerciseId;
1180
        $questionId = (int) $questionId;
1181
        $questionType = (int) $questionType;
1182
        $fromExercise = (int) $fromExercise;
1183
1184
        $result = Display::url(
1185
            $questionName.$sessionIcon,
1186
            'admin.php?'.api_get_cidreq().
1187
            "&exerciseId=$exerciseId&editQuestion=$questionId&type=$questionType&fromExercise=$fromExercise"
1188
        );
1189
    }
1190
1191
    return $result;
1192
}
1193
1194
/**
1195
    Return the <a> html code for delete, add, clone, edit a question
1196
    in_action = the code of the action triggered by the button
1197
    from_exercise = the id of the current exercise from which we click on question pool
1198
    in_questionid = the id of the current question
1199
    in_questiontype = the code of the type of the current question
1200
    in_questionname = the name of the question
1201
    in_selected_course = the if of the course chosen in the FILTERING MENU
1202
    in_courseCategoryId = the id of the category chosen in the FILTERING MENU
1203
    in_exerciseLevel = the level of the exercise chosen in the FILTERING MENU
1204
    in_answerType = the code of the type of the question chosen in the FILTERING MENU
1205
    in_session_id = the id of the session_id chosen in the FILTERING MENU
1206
    in_exercise_id = the id of the exercise chosen in the FILTERING MENU
1207
 */
1208
function get_action_icon_for_question(
1209
    $in_action,
1210
    $from_exercise,
1211
    $in_questionid,
1212
    $in_questiontype,
1213
    $in_questionname,
1214
    $in_selected_course,
1215
    $in_courseCategoryId,
1216
    $in_exerciseLevel,
1217
    $in_answerType,
1218
    $in_session_id,
1219
    $in_exercise_id,
1220
    Exercise $myObjEx
1221
) {
1222
    $limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access');
1223
    $getParams = "&selected_course=$in_selected_course&courseCategoryId=$in_courseCategoryId&exerciseId=$in_exercise_id&exerciseLevel=$in_exerciseLevel&answerType=$in_answerType&session_id=$in_session_id";
1224
    $res = '';
1225
    switch ($in_action) {
1226
        case 'delete':
1227
            if ($limitTeacherAccess && !api_is_platform_admin()) {
1228
                break;
1229
            }
1230
1231
            if (isQuestionInActiveQuiz($in_questionid)) {
1232
                $res = Display::return_icon('delete_na.png', get_lang('ThisQuestionExistsInAnotherExercisesWarning'));
1233
            } else {
1234
                $res = "<a href='".api_get_self()."?".
1235
                api_get_cidreq().$getParams."&delete=$in_questionid' onclick='return confirm_your_choice()'>";
1236
                $res .= Display::return_icon('delete.png', get_lang('Delete'));
1237
                $res .= "</a>";
1238
            }
1239
1240
            break;
1241
        case 'edit':
1242
            $res = getLinkForQuestion(
1243
                1,
1244
                $from_exercise,
1245
                $in_questionid,
1246
                $in_questiontype,
1247
                Display::return_icon('edit.png', get_lang('Edit')),
1248
                $in_session_id,
1249
                $in_exercise_id
1250
            );
1251
1252
            break;
1253
        case 'add':
1254
            $res = '-';
1255
            if (!$myObjEx->hasQuestion($in_questionid)) {
1256
                $res = "<a href='".api_get_self().'?'.
1257
                    api_get_cidreq().$getParams."&recup=$in_questionid&fromExercise=$from_exercise'>";
1258
                $res .= Display::return_icon('view_more_stats.gif', get_lang('InsertALinkToThisQuestionInTheExercise'));
1259
                $res .= '</a>';
1260
            }
1261
1262
            break;
1263
        case 'clone':
1264
            $url = api_get_self().'?'.api_get_cidreq().$getParams.
1265
                "&question_copy=$in_questionid&course_id=$in_selected_course&fromExercise=$from_exercise";
1266
            $res = Display::url(
1267
                Display::return_icon('cd.png', get_lang('Re-use a copy inside the current test')),
1268
                $url
1269
            );
1270
1271
            break;
1272
        default:
1273
            $res = $in_action;
1274
1275
            break;
1276
    }
1277
1278
    return $res;
1279
}
1280
1281
/**
1282
 * @param int $questionId
1283
 *
1284
 * @return bool
1285
 */
1286
function isQuestionInActiveQuiz($questionId)
1287
{
1288
    $tblQuizRelQuestion = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
1289
    $tblQuiz = Database::get_course_table(TABLE_QUIZ_TEST);
1290
1291
    $result = Database::fetch_assoc(
1292
        Database::query(
1293
            "SELECT COUNT(qq.question_id) c FROM $tblQuizRelQuestion qq
1294
                INNER JOIN $tblQuiz q ON qq.exercice_id = q.iid
1295
                WHERE q.active = 1
1296
                AND qq.question_id = $questionId"
1297
        )
1298
    );
1299
1300
    return $result['c'] > 0;
1301
}
1302
1303
/**
1304
 * Return the icon for the question type.
1305
 *
1306
 * @author hubert.borderiou 13-10-2011
1307
 */
1308
function get_question_type_for_question($in_selectedcourse, $in_questionid)
1309
{
1310
    $courseInfo = api_get_course_info_by_id($in_selectedcourse);
1311
    $question = Question::read($in_questionid, $courseInfo);
1312
    $questionType = null;
1313
    if (!empty($question)) {
1314
        $typeImg = $question->getTypePicture();
1315
        $typeExpl = $question->getExplanation();
1316
1317
        $questionType = Display::tag('div', Display::return_icon($typeImg, $typeExpl, [], 32), []);
1318
    }
1319
1320
    return $questionType;
1321
}
1322