Passed
Push — master ( 05b235...952819 )
by Yannick
29:42 queued 20:45
created

CourseSelectForm::get_posted_course()   F

Complexity

Conditions 67
Paths 52

Size

Total Lines 198
Code Lines 121

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 67
eloc 121
nc 52
nop 4
dl 0
loc 198
rs 3.3333
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
namespace Chamilo\CourseBundle\Component\CourseCopy;
5
6
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Document;
7
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Resource;
8
use Database;
9
use Display;
10
11
/**
12
 * Class to show a form to select resources.
13
 *
14
 * @author Bart Mollet <[email protected]>
15
 * @author Julio Montoya <[email protected]>
16
 *
17
 * @package chamilo.backup
18
 */
19
class CourseSelectForm
20
{
21
    /**
22
     * @return array
23
     */
24
    public static function getResourceTitleList()
25
    {
26
        $list = [];
27
        $list[RESOURCE_LEARNPATH_CATEGORY] = get_lang('Learnpath').' '.get_lang('Category');
28
        $list[RESOURCE_ASSET] = get_lang('Assets');
29
        $list[RESOURCE_GRADEBOOK] = get_lang('Gradebook');
30
        $list[RESOURCE_EVENT] = get_lang('Events');
31
        $list[RESOURCE_ANNOUNCEMENT] = get_lang('Announcements');
32
        $list[RESOURCE_DOCUMENT] = get_lang('Documents');
33
        $list[RESOURCE_LINK] = get_lang('Links');
34
        $list[RESOURCE_COURSEDESCRIPTION] = get_lang('CourseDescription');
35
        $list[RESOURCE_FORUM] = get_lang('Forums');
36
        $list[RESOURCE_FORUMCATEGORY] = get_lang('ForumCategory');
37
        $list[RESOURCE_QUIZ] = get_lang('Tests');
38
        $list[RESOURCE_TEST_CATEGORY] = get_lang('QuestionCategory');
39
        $list[RESOURCE_LEARNPATH] = get_lang('ToolLearnpath');
40
        $list[RESOURCE_SCORM] = 'SCORM';
41
        $list[RESOURCE_TOOL_INTRO] = get_lang('ToolIntro');
42
        $list[RESOURCE_SURVEY] = get_lang('Survey');
43
        $list[RESOURCE_GLOSSARY] = get_lang('Glossary');
44
        $list[RESOURCE_WIKI] = get_lang('Wiki');
45
        $list[RESOURCE_THEMATIC] = get_lang('Thematic');
46
        $list[RESOURCE_ATTENDANCE] = get_lang('Attendance');
47
        $list[RESOURCE_WORK] = get_lang('ToolStudentPublication');
48
49
        return $list;
50
    }
51
52
    /**
53
     * Display the form.
54
     *
55
     * @param array $course
56
     * @param array $hidden_fields     hidden fields to add to the form
57
     * @param bool  $avoidSerialize    the document array will be serialize.
58
     *                                 This is used in the course_copy.php file
59
     * @param bool  $avoidCourseInForm
60
     */
61
    public static function display_form(
62
        $course,
63
        $hidden_fields = null,
64
        $avoidSerialize = false,
65
        $avoidCourseInForm = false
66
    ) {
67
        global $charset; ?>
68
        <script>
69
            function exp(item) {
70
                el = document.getElementById('div_'+item);
71
                if (el.style.display == 'none') {
72
                    el.style.display = '';
73
                    $('#img_'+item).removeClass();
74
                    $('#img_'+item).addClass('fa fa-minus-square-o fa-lg');
75
76
                } else {
77
                    el.style.display = 'none';
78
                    $('#img_'+item).removeClass();
79
                    $('#img_'+item).addClass('fa fa-plus-square-o fa-lg');
80
                }
81
            }
82
83
            function setCheckboxForum(type, value, item_id) {
84
                d = document.course_select_form;
85
                for (i = 0; i < d.elements.length; i++) {
86
                    if (d.elements[i].type == "checkbox") {
87
                        var name = d.elements[i].attributes.getNamedItem('name').nodeValue;
88
                        if (name.indexOf(type) > 0 || type == 'all') {
89
                            if ($(d.elements[i]).attr('rel') == item_id) {
90
                                d.elements[i].checked = value;
91
                            }
92
                        }
93
                    }
94
                }
95
            }
96
97
            function setCheckbox(type,value) {
98
                d = document.course_select_form;
99
                for (i = 0; i < d.elements.length; i++) {
100
                    if (d.elements[i].type == "checkbox") {
101
                        var name = d.elements[i].attributes.getNamedItem('name').nodeValue;
102
                        if( name.indexOf(type) > 0 || type == 'all' ){
103
                             d.elements[i].checked = value;
104
                        }
105
                    }
106
                }
107
            }
108
109
            function checkLearnPath(message){
110
                d = document.course_select_form;
111
                for (i = 0; i < d.elements.length; i++) {
112
                    if (d.elements[i].type == "checkbox") {
113
                        var name = d.elements[i].attributes.getNamedItem('name').nodeValue;
114
                        if( name.indexOf('learnpath') > 0){
115
                            if(d.elements[i].checked){
116
                                setCheckbox('document',true);
117
                                alert(message);
118
                                break;
119
                            }
120
                        }
121
                    }
122
                }
123
            }
124
125
            function check_forum(obj) {
126
                var id = $(obj).attr('rel');
127
                var my_id = $(obj).attr('my_rel');
128
                var checked = false;
129
                if ($('#resource_forum_'+my_id).attr('checked')) {
130
                    checked = true;
131
                }
132
                setCheckboxForum('thread', checked, my_id);
133
                $('#resource_Forum_Category_'+id).attr('checked','checked');
134
            }
135
136
             function check_category(obj) {
137
                var my_id = $(obj).attr('my_rel');
138
                var checked = false;
139
                if ($('#resource_Forum_Category_'+my_id).attr('checked')) {
140
                    checked = true;
141
                }
142
                $('.resource_forum').each(function(index, value) {
143
                    if ($(value).attr('rel') == my_id) {
144
                        $(value).attr('checked', checked);
145
                    }
146
                });
147
148
                $('.resource_topic').each(function(index, value) {
149
                    if ($(value).attr('cat_id') == my_id) {
150
                        $(value).attr('checked', checked);
151
                    }
152
                });
153
            }
154
155
            function check_topic(obj) {
156
                var my_id = $(obj).attr('cat_id');
157
                var forum_id = $(obj).attr('forum_id');
158
                $('#resource_Forum_Category_'+my_id).attr('checked','checked');
159
                $('#resource_forum_'+forum_id).attr('checked','checked');
160
            }
161
        </script>
162
        <?php
163
        // get destination course title
164
        if (!empty($hidden_fields['destination_course'])) {
165
            $sessionTitle = !empty($hidden_fields['destination_session']) ? ' ('.api_get_session_name($hidden_fields['destination_session']).')' : null;
166
            $courseInfo = api_get_course_info($hidden_fields['destination_course']);
167
            echo '<h3>';
168
            echo get_lang('DestinationCourse').' : '.$courseInfo['title'].' ('.$courseInfo['code'].') '.$sessionTitle;
169
            echo '</h3>';
170
        }
171
172
        echo '<script src="'.api_get_path(WEB_CODE_PATH).'inc/lib/javascript/upload.js" type="text/javascript"></script>';
173
        echo '<div class="tool-backups-options">';
174
        echo '<form method="post" id="upload_form" name="course_select_form">';
175
        echo '<input type="hidden" name="action" value="course_select_form"/>';
176
177
        if (!empty($hidden_fields['destination_course']) &&
178
            !empty($hidden_fields['origin_course']) &&
179
            !empty($hidden_fields['destination_session']) &&
180
            !empty($hidden_fields['origin_session'])
181
        ) {
182
            echo '<input type="hidden" name="destination_course" value="'.$hidden_fields['destination_course'].'"/>';
183
            echo '<input type="hidden" name="origin_course" value="'.$hidden_fields['origin_course'].'"/>';
184
            echo '<input type="hidden" name="destination_session" value="'.$hidden_fields['destination_session'].'"/>';
185
            echo '<input type="hidden" name="origin_session" value="'.$hidden_fields['origin_session'].'"/>';
186
        }
187
188
        $forum_categories = [];
189
        $forums = [];
190
        $forum_topics = [];
191
192
        echo '<p>';
193
        echo get_lang('SelectResources');
194
        echo '</p>';
195
        echo Display::return_message(get_lang('DontForgetToSelectTheMediaFilesIfYourResourceNeedIt'));
196
197
        $resource_titles = self::getResourceTitleList();
198
        $element_count = self::parseResources($resource_titles, $course->resources, true, true);
199
200
        // Fixes forum order
201
        if (!empty($forum_categories)) {
202
            $type = RESOURCE_FORUMCATEGORY;
203
            echo '<div class="item-backup" onclick="javascript:exp('."'$type'".');">';
204
            echo '<em id="img_'.$type.'" class="fa fa-minus-square-o fa-lg"></em>';
205
            echo '<span class="title">'.$resource_titles[RESOURCE_FORUM].'</span></div>';
206
            echo '<div class="item-content" id="div_'.$type.'">';
207
            echo '<ul class="list-backups-options">';
208
            foreach ($forum_categories as $forum_category_id => $forum_category) {
209
                echo '<li>';
210
                echo '<label class="checkbox">';
211
                echo '<input type="checkbox" 
212
                    id="resource_'.RESOURCE_FORUMCATEGORY.'_'.$forum_category_id.'" 
213
                    my_rel="'.$forum_category_id.'" 
214
                    onclick="javascript:check_category(this);" 
215
                    name="resource['.RESOURCE_FORUMCATEGORY.']['.$forum_category_id.']" /> ';
216
                $forum_category->show();
217
                echo '</label>';
218
                echo '</li>';
219
220
                if (isset($forums[$forum_category_id])) {
221
                    $my_forums = $forums[$forum_category_id];
222
                    echo '<ul>';
223
                    foreach ($my_forums as $forum_id => $forum) {
224
                        echo '<li>';
225
                        echo '<label class="checkbox">';
226
                        echo '<input type="checkbox" 
227
                            class="resource_forum" 
228
                            id="resource_'.RESOURCE_FORUM.'_'.$forum_id.'" 
229
                            onclick="javascript:check_forum(this);" 
230
                            my_rel="'.$forum_id.'" 
231
                            rel="'.$forum_category_id.'" 
232
                            name="resource['.RESOURCE_FORUM.']['.$forum_id.']" />';
233
                        $forum->show();
234
                        echo '</label>';
235
                        echo '</li>';
236
                        if (isset($forum_topics[$forum_id])) {
237
                            $my_forum_topics = $forum_topics[$forum_id];
238
                            if (!empty($my_forum_topics)) {
239
                                echo '<ul>';
240
                                foreach ($my_forum_topics as $topic_id => $topic) {
241
                                    echo '<li>';
242
                                    echo '<label class="checkbox">';
243
                                    echo '<input 
244
                                        type="checkbox" 
245
                                        id="resource_'.RESOURCE_FORUMTOPIC.'_'.$topic_id.'" 
246
                                        onclick="javascript:check_topic(this);" class="resource_topic" 
247
                                        forum_id="'.$forum_id.'"  
248
                                        rel="'.$forum_id.'" 
249
                                        cat_id="'.$forum_category_id.'" 
250
                                        name="resource['.RESOURCE_FORUMTOPIC.']['.$topic_id.']" />';
251
                                    $topic->show();
252
                                    echo '</label>';
253
                                    echo '</li>';
254
                                }
255
                                echo '</ul>';
256
                            }
257
                        }
258
                    }
259
                    echo '</ul>';
260
                }
261
                echo '<hr/>';
262
            }
263
            echo '</ul>';
264
            echo '</div>';
265
            echo '<script language="javascript">exp('."'$type'".')</script>';
266
        }
267
268
        if ($avoidSerialize) {
269
            /*Documents are avoided due the huge amount of memory that the serialize php function "eats"
270
            (when there are directories with hundred/thousand of files) */
271
            // this is a known issue of serialize
272
            $course->resources['document'] = null;
273
        }
274
275
        if ($avoidCourseInForm === false) {
276
            /** @var Course $course */
277
            $courseSerialized = base64_encode(Course::serialize($course));
278
            echo '<input type="hidden" name="course" value="'.$courseSerialized.'"/>';
279
        }
280
281
        if (is_array($hidden_fields)) {
282
            foreach ($hidden_fields as $key => $value) {
283
                echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
284
            }
285
        }
286
287
        $recycleOption = isset($_POST['recycle_option']) ? true : false;
288
        if (empty($element_count)) {
289
            echo Display::return_message(get_lang('NoDataAvailable'), 'warning');
290
        } else {
291
            if (!empty($hidden_fields['destination_session'])) {
292
                echo '<br />
293
                      <button 
294
                        class="save" 
295
                        type="submit" 
296
                        onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES, $charset))."'".')) return false;" >'.
297
                    get_lang('Ok').'</button>';
298
            } else {
299
                if ($recycleOption) {
300
                    echo '<br /><button class="save" type="submit">'.get_lang('Ok').'</button>';
301
                } else {
302
                    echo '<br />
303
                          <button 
304
                                class="save btn btn-primary" 
305
                                type="submit" 
306
                                onclick="checkLearnPath(\''.addslashes(get_lang('DocumentsWillBeAddedToo')).'\')">'.
307
                    get_lang('Ok').'</button>';
308
                }
309
            }
310
        }
311
312
        self::display_hidden_quiz_questions($course);
313
        self::display_hidden_scorm_directories($course);
314
        echo '</form>';
315
        echo '</div>';
316
        echo '<div id="dynamic_div" style="display:block;margin-left:40%;margin-top:10px;height:50px;"></div>';
317
    }
318
319
    /**
320
     * @param array $resource_titles
321
     * @param array $resourceList
322
     * @param bool  $showHeader
323
     * @param bool  $showItems
324
     *
325
     * @return int
326
     */
327
    public static function parseResources(
328
        $resource_titles,
329
        $resourceList,
330
        $showHeader = true,
331
        $showItems = true
332
    ) {
333
        $element_count = 0;
334
        foreach ($resourceList as $type => $resources) {
335
            if (count($resources) > 0) {
336
                switch ($type) {
337
                    // Resources to avoid
338
                    case RESOURCE_FORUMCATEGORY:
339
                        foreach ($resources as $id => $resource) {
340
                            $forum_categories[$id] = $resource;
341
                        }
342
                        $element_count++;
343
                        break;
344
                    case RESOURCE_FORUM:
345
                        foreach ($resources as $id => $resource) {
346
                            $forums[$resource->obj->forum_category][$id] = $resource;
347
                        }
348
                        $element_count++;
349
                        break;
350
                    case RESOURCE_FORUMTOPIC:
351
                        foreach ($resources as $id => $resource) {
352
                            $forum_topics[$resource->obj->forum_id][$id] = $resource;
353
                        }
354
                        $element_count++;
355
                        break;
356
                    case RESOURCE_LINKCATEGORY:
357
                    case RESOURCE_FORUMPOST:
358
                    case RESOURCE_QUIZQUESTION:
359
                    case RESOURCE_SURVEYQUESTION:
360
                    case RESOURCE_SURVEYINVITATION:
361
                    case RESOURCE_SCORM:
362
                        break;
363
                    default:
364
                        if ($showHeader) {
365
                            echo '<div class="item-backup" onclick="javascript:exp('."'$type'".');">';
366
                            echo '<em id="img_'.$type.'" class="fa fa-plus-square-o fa-lg"></em>';
367
                            echo '<span class="title">'.$resource_titles[$type].'</span>';
368
                            echo '</div>';
369
                            echo '<div class="item-content" id="div_'.$type.'">';
370
                        }
371
372
                        if ($type == RESOURCE_LEARNPATH) {
373
                            echo Display::return_message(
374
                                get_lang(
375
                                    'ToExportLearnpathWithQuizYouHaveToSelectQuiz'
376
                                ),
377
                                'warning'
378
                            );
379
                            echo Display::return_message(
380
                                get_lang(
381
                                    'IfYourLPsHaveAudioFilesIncludedYouShouldSelectThemFromTheDocuments'
382
                                ),
383
                                'warning'
384
                            );
385
                        }
386
387
                        if ($type == RESOURCE_DOCUMENT) {
388
                            if (api_get_setting('show_glossary_in_documents') != 'none') {
389
                                echo Display::return_message(
390
                                    get_lang(
391
                                        'ToExportDocumentsWithGlossaryYouHaveToSelectGlossary'
392
                                    ),
393
                                    'warning'
394
                                );
395
                            }
396
                        }
397
398
                        if ($showItems) {
399
                            echo '<div class="well">';
400
                            echo '<div class="btn-group">';
401
                            echo "<a class=\"btn btn-default\" 
402
                                        href=\"javascript: void(0);\" 
403
                                        onclick=\"javascript: setCheckbox('$type',true);\" >".get_lang('All')."</a>";
404
                            echo "<a class=\"btn btn-default\" 
405
                                        href=\"javascript: void(0);\" 
406
                                        onclick=\"javascript:setCheckbox('$type',false);\" >".get_lang('None')."</a>";
407
                            echo '</div>';
408
                            echo '<ul class="list-backups-options">';
409
                            foreach ($resources as $id => $resource) {
410
                                if ($resource) {
411
                                    echo '<li>';
412
                                    // Event obj in 1.9.x in 1.10.x the class is CalendarEvent
413
                                    Resource::setClassType($resource);
414
                                    echo '<label class="checkbox">';
415
                                    echo '<input 
416
                                        type="checkbox" 
417
                                        name="resource['.$type.']['.$id.']"  
418
                                        id="resource['.$type.']['.$id.']" />';
419
                                    $resource->show();
420
                                    echo '</label>';
421
                                    echo '</li>';
422
                                }
423
                            }
424
                            echo '</ul>';
425
                            echo '</div>';
426
                        }
427
428
                        if ($showHeader) {
429
                            echo '</div>';
430
                            echo '<script language="javascript">exp('."'$type'".')</script>';
431
                        }
432
                        $element_count++;
433
                }
434
            }
435
        }
436
437
        return $element_count;
438
    }
439
440
    /**
441
     * @param $course
442
     */
443
    public static function display_hidden_quiz_questions($course)
444
    {
445
        if (is_array($course->resources)) {
446
            foreach ($course->resources as $type => $resources) {
447
                if (count($resources) > 0) {
448
                    switch ($type) {
449
                        case RESOURCE_QUIZQUESTION:
450
                            foreach ($resources as $id => $resource) {
451
                                echo '<input 
452
                                    type="hidden" 
453
                                    name="resource['.RESOURCE_QUIZQUESTION.']['.$id.']" 
454
                                    id="resource['.RESOURCE_QUIZQUESTION.']['.$id.']" value="On" />';
455
                            }
456
                            break;
457
                    }
458
                }
459
            }
460
        }
461
    }
462
463
    /**
464
     * @param $course
465
     */
466
    public static function display_hidden_scorm_directories($course)
467
    {
468
        if (is_array($course->resources)) {
469
            foreach ($course->resources as $type => $resources) {
470
                if (count($resources) > 0) {
471
                    switch ($type) {
472
                        case RESOURCE_SCORM:
473
                            foreach ($resources as $id => $resource) {
474
                                echo '<input 
475
                                    type="hidden" 
476
                                    name="resource['.RESOURCE_SCORM.']['.$id.']" 
477
                                    id="resource['.RESOURCE_SCORM.']['.$id.']" value="On" />';
478
                            }
479
                            break;
480
                    }
481
                }
482
            }
483
        }
484
    }
485
486
    /**
487
     * Get the posted course.
488
     *
489
     * @param string $from         who calls the function?
490
     *                             It can be copy_course, create_backup, import_backup or recycle_course
491
     * @param int    $session_id
492
     * @param string $course_code
493
     * @param Course $postedCourse
494
     *
495
     * @return Course The course-object with all resources selected by the user
496
     *                in the form given by display_form(...)
497
     */
498
    public static function get_posted_course($from = '', $session_id = 0, $course_code = '', $postedCourse = null)
499
    {
500
        $course = null;
501
        if (isset($_POST['course'])) {
502
            $course = Course::unserialize(base64_decode($_POST['course']));
503
        }
504
505
        if ($postedCourse) {
506
            $course = $postedCourse;
507
        }
508
509
        if (empty($course)) {
510
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type Chamilo\CourseBundle\Component\CourseCopy\Course.
Loading history...
511
        }
512
513
        // Create the resource DOCUMENT objects
514
        // Loading the results from the checkboxes of ethe javascript
515
        $resource = isset($_POST['resource'][RESOURCE_DOCUMENT]) ? $_POST['resource'][RESOURCE_DOCUMENT] : null;
516
517
        $course_info = api_get_course_info($course_code);
518
        $table_doc = Database::get_course_table(TABLE_DOCUMENT);
519
        $table_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
520
        $course_id = $course_info['real_id'];
521
522
        /* Searching the documents resource that have been set to null because
523
        $avoidSerialize is true in the display_form() function*/
524
        if ($from === 'copy_course') {
525
            if (is_array($resource)) {
526
                $resource = array_keys($resource);
527
                foreach ($resource as $resource_item) {
528
                    $conditionSession = '';
529
                    if (!empty($session_id)) {
530
                        $session_id = intval($session_id);
531
                        $conditionSession = ' AND d.session_id ='.$session_id;
532
                    }
533
534
                    $sql = 'SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
535
                            FROM '.$table_doc.' d 
536
                            INNER JOIN '.$table_prop.' p
537
                            ON (d.c_id = p.c_id)
538
                            WHERE
539
                                d.c_id = '.$course_id.' AND
540
                                p.c_id = '.$course_id.' AND
541
                                tool = \''.TOOL_DOCUMENT.'\' AND
542
                                p.ref = d.id AND p.visibility != 2 AND
543
                                d.id = '.$resource_item.$conditionSession.'
544
                            ORDER BY path';
545
                    $db_result = Database::query($sql);
546
                    while ($obj = Database::fetch_object($db_result)) {
547
                        $doc = new Document(
548
                            $obj->id,
549
                            $obj->path,
550
                            $obj->comment,
551
                            $obj->title,
552
                            $obj->filetype,
553
                            $obj->size
554
                        );
555
                        if ($doc) {
556
                            $course->add_resource($doc);
557
                            // adding item property
558
                            $sql = "SELECT * FROM $table_prop
559
                                    WHERE
560
                                        c_id = $course_id AND
561
                                        tool = '".RESOURCE_DOCUMENT."' AND
562
                                        ref = $resource_item ";
563
                            $res = Database::query($sql);
564
                            $all_properties = [];
565
                            while ($item_property = Database::fetch_array($res, 'ASSOC')) {
566
                                $all_properties[] = $item_property;
567
                            }
568
                            $course->resources[RESOURCE_DOCUMENT][$resource_item]->item_properties = $all_properties;
569
                        }
570
                    }
571
                }
572
            }
573
        }
574
575
        if (is_array($course->resources)) {
576
            foreach ($course->resources as $type => $resources) {
577
                switch ($type) {
578
                    case RESOURCE_SURVEYQUESTION:
579
                        foreach ($resources as $id => $obj) {
580
                            if (isset($_POST['resource'][RESOURCE_SURVEY]) &&
581
                                is_array($_POST['resource'][RESOURCE_SURVEY]) &&
582
                                !in_array($obj->survey_id, array_keys($_POST['resource'][RESOURCE_SURVEY]))
583
                            ) {
584
                                unset($course->resources[$type][$id]);
585
                            }
586
                        }
587
                        break;
588
                    case RESOURCE_FORUMTOPIC:
589
                    case RESOURCE_FORUMPOST:
590
                       //Add post from topic
591
                        if ($type == RESOURCE_FORUMTOPIC) {
592
                            $posts_to_save = [];
593
                            $posts = $course->resources[RESOURCE_FORUMPOST];
594
                            foreach ($resources as $thread_id => $obj) {
595
                                if (!isset($_POST['resource'][RESOURCE_FORUMTOPIC][$thread_id])) {
596
                                    unset($course->resources[RESOURCE_FORUMTOPIC][$thread_id]);
597
                                    continue;
598
                                }
599
                                $forum_id = $obj->obj->forum_id;
600
                                $title = $obj->obj->thread_title;
601
                                foreach ($posts as $post_id => $post) {
602
                                    if ($post->obj->thread_id == $thread_id &&
603
                                        $forum_id == $post->obj->forum_id &&
604
                                        $title == $post->obj->post_title
605
                                    ) {
606
                                        $posts_to_save[] = $post_id;
607
                                    }
608
                                }
609
                            }
610
                            if (!empty($posts)) {
611
                                foreach ($posts as $post_id => $post) {
612
                                    if (!in_array($post_id, $posts_to_save)) {
613
                                        unset($course->resources[RESOURCE_FORUMPOST][$post_id]);
614
                                    }
615
                                }
616
                            }
617
                        }
618
                        break;
619
                    case RESOURCE_LEARNPATH:
620
                        $lps = isset($_POST['resource'][RESOURCE_LEARNPATH]) ? $_POST['resource'][RESOURCE_LEARNPATH] : null;
621
622
                        if (!empty($lps)) {
623
                            foreach ($lps as $id => $obj) {
624
                                $lp_resource = $course->resources[RESOURCE_LEARNPATH][$id];
625
626
                                if (isset($lp_resource) && !empty($lp_resource) && isset($lp_resource->items)) {
627
                                    foreach ($lp_resource->items as $item) {
628
                                        switch ($item['item_type']) {
629
                                            //Add links added in a LP see #5760
630
                                            case 'link':
631
                                                $_POST['resource'][RESOURCE_LINK][$item['path']] = 1;
632
                                                break;
633
                                        }
634
                                    }
635
                                }
636
                            }
637
                        }
638
                        // no break
639
                    case RESOURCE_LINKCATEGORY:
640
                    case RESOURCE_FORUMCATEGORY:
641
                    case RESOURCE_QUIZQUESTION:
642
                    case RESOURCE_DOCUMENT:
643
                        // Mark folders to import which are not selected by the user to import,
644
                        // but in which a document was selected.
645
                        $documents = isset($_POST['resource'][RESOURCE_DOCUMENT]) ? $_POST['resource'][RESOURCE_DOCUMENT] : null;
646
                        if (!empty($resources) && is_array($resources)) {
647
                            foreach ($resources as $id => $obj) {
648
                                if (isset($obj->file_type) && $obj->file_type == 'folder' &&
649
                                    !isset($_POST['resource'][RESOURCE_DOCUMENT][$id]) &&
650
                                    is_array($documents)
651
                                ) {
652
                                    foreach ($documents as $id_to_check => $post_value) {
653
                                        if (isset($resources[$id_to_check])) {
654
                                            $obj_to_check = $resources[$id_to_check];
655
                                            $shared_path_part = substr(
656
                                                $obj_to_check->path,
657
                                                0,
658
                                                strlen($obj->path)
659
                                            );
660
                                            if ($id_to_check != $id && $obj->path == $shared_path_part) {
661
                                                $_POST['resource'][RESOURCE_DOCUMENT][$id] = 1;
662
                                                break;
663
                                            }
664
                                        }
665
                                    }
666
                                }
667
                            }
668
                        }
669
                        // no break
670
                    default:
671
                        if (!empty($resources) && is_array($resources)) {
672
                            foreach ($resources as $id => $obj) {
673
                                $resource_is_used_elsewhere = $course->is_linked_resource($obj);
674
                                // check if document is in a quiz (audio/video)
675
                                if ($type == RESOURCE_DOCUMENT && $course->has_resources(RESOURCE_QUIZ)) {
676
                                    foreach ($course->resources[RESOURCE_QUIZ] as $quiz) {
677
                                        $quiz = $quiz->obj;
678
                                        if (isset($quiz->media) && $quiz->media == $id) {
679
                                            $resource_is_used_elsewhere = true;
680
                                        }
681
                                    }
682
                                }
683
                                // quiz question can be, not attached to an exercise
684
                                if ($type != RESOURCE_QUIZQUESTION) {
685
                                    if (!isset($_POST['resource'][$type][$id]) && !$resource_is_used_elsewhere) {
686
                                        unset($course->resources[$type][$id]);
687
                                    }
688
                                }
689
                            }
690
                        }
691
                }
692
            }
693
        }
694
695
        return $course;
696
    }
697
698
    /**
699
     * Display the form session export.
700
     *
701
     * @param array $list_course
702
     * @param array $hidden_fields  hidden fields to add to the form
703
     * @param bool  $avoidSerialize the document array will be serialize. This is used in the course_copy.php file
704
     */
705
    public static function display_form_session_export(
706
        $list_course,
707
        $hidden_fields = null,
708
        $avoidSerialize = false
709
    ) {
710
        ?>
711
        <script>
712
            function exp(item) {
713
                el = document.getElementById('div_'+item);
714
                if (el.style.display == 'none') {
715
                    el.style.display = '';
716
                    if (document.getElementById('img_'+item).length)
717
                    document.getElementById('img_'+item).className('fa fa-minus-square-o fa-lg');
718
                } else {
719
                    el.style.display = 'none';
720
                    if (document.getElementById('img_'+item).length)
721
                    document.getElementById('img_'+item).className('fa fa-plus-square-o fa-lg');
722
                }
723
            }
724
725
            function setCheckbox(type,value) {
726
                d = document.course_select_form;
727
                for (i = 0; i < d.elements.length; i++) {
728
                    if (d.elements[i].type == "checkbox") {
729
                        var name = d.elements[i].attributes.getNamedItem('name').nodeValue;
730
                        if( name.indexOf(type) > 0 || type == 'all' ){
731
                             d.elements[i].checked = value;
732
                        }
733
                    }
734
                }
735
            }
736
            function checkLearnPath(message){
737
                d = document.course_select_form;
738
                for (i = 0; i < d.elements.length; i++) {
739
                    if (d.elements[i].type == "checkbox") {
740
                        var name = d.elements[i].attributes.getNamedItem('name').nodeValue;
741
                        if( name.indexOf('learnpath') > 0){
742
                            if(d.elements[i].checked){
743
                                setCheckbox('document',true);
744
                                alert(message);
745
                                break;
746
                            }
747
                        }
748
                    }
749
                }
750
            }
751
        </script>
752
        <?php
753
754
        //get destination course title
755
        if (!empty($hidden_fields['destination_course'])) {
756
            $sessionTitle = null;
757
            if (!empty($hidden_fields['destination_session'])) {
758
                $sessionTitle = ' ('.api_get_session_name($hidden_fields['destination_session']).')';
759
            }
760
            $courseInfo = api_get_course_info($hidden_fields['destination_course']);
761
            echo '<h3>';
762
            echo get_lang('DestinationCourse').' : '.$courseInfo['title'].$sessionTitle;
763
            echo '</h3>';
764
        }
765
766
        echo '<script src="'.api_get_path(WEB_CODE_PATH).'inc/lib/javascript/upload.js" type="text/javascript"></script>';
767
        $icon = Display::returnIconPath('progress_bar.gif');
768
        echo '<div class="tool-backups-options">';
769
        echo '<form method="post" id="upload_form" name="course_select_form">';
770
        echo '<input type="hidden" name="action" value="course_select_form"/>';
771
        foreach ($list_course as $course) {
772
            foreach ($course->resources as $type => $resources) {
773
                if (count($resources) > 0) {
774
                    echo '<div class="item-backup" onclick="javascript:exp('."'$course->code'".');">';
775
                    echo '<em id="img_'.$course->code.'" class="fa fa-minus-square-o fa-lg"></em>';
776
                    echo '<span class="title"> '.$course->code.'</span></div>';
777
                    echo '<div class="item-content" id="div_'.$course->code.'">';
778
                    echo '<blockquote>';
779
780
                    echo '<div class="btn-group">';
781
                    echo "<a class=\"btn\" href=\"#\" onclick=\"javascript:setCheckbox('".$course->code."',true);\" >".get_lang('All')."</a>";
782
                    echo "<a class=\"btn\" href=\"#\" onclick=\"javascript:setCheckbox('".$course->code."',false);\" >".get_lang('None')."</a>";
783
                    echo '</div>';
784
785
                    foreach ($resources as $id => $resource) {
786
                        echo '<label class="checkbox" for="resource['.$course->code.']['.$id.']">';
787
                        echo '<input type="checkbox" name="resource['.$course->code.']['.$id.']" id="resource['.$course->code.']['.$id.']"/>';
788
                        $resource->show();
789
                        echo '</label>';
790
                    }
791
                    echo '</blockquote>';
792
                    echo '</div>';
793
                    echo '<script type="text/javascript">exp('."'$course->code'".')</script>';
794
                }
795
            }
796
        }
797
        if ($avoidSerialize) {
798
            // Documents are avoided due the huge amount of memory that the serialize php
799
            // function "eats" (when there are directories with hundred/thousand of files)
800
            // this is a known issue of serialize
801
            $course->resources['document'] = null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $course seems to be defined by a foreach iteration on line 771. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
802
        }
803
        echo '<input type="hidden" name="course" value="'.base64_encode(Course::serialize($course)).'"/>';
804
        if (is_array($hidden_fields)) {
805
            foreach ($hidden_fields as $key => $value) {
806
                echo "\n";
807
                echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
808
            }
809
        }
810
        echo '<br /><button class="save" type="submit" 
811
            onclick="checkLearnPath(\''.addslashes(get_lang('DocumentsWillBeAddedToo')).'\')">'.
812
            get_lang('Ok').'</button>';
813
        self::display_hidden_quiz_questions($course);
814
        self::display_hidden_scorm_directories($course);
815
        echo '</form>';
816
        echo '</div>';
817
        echo '<div id="dynamic_div" style="display:block;margin-left:40%;margin-top:10px;height:50px;"></div>';
818
    }
819
}
820