Project

General

Profile

Bug #5395 » lp_controller.php

Yoselyn Castillo, 14/09/2012 17:23

 
1
<?php
2
/* For licensing terms, see /license.txt */
3

    
4
/**
5
 * Controller script. Prepares the common background variables to give to the scripts corresponding to
6
 * the requested action
7
 * @package chamilo.learnpath
8
 * @author Yannick Warnier <ywarnier@beeznest.org>
9
 */
10

    
11
use \ChamiloSession as Session;
12

    
13
$debug = 0;
14
if ($debug > 0) error_log('New LP -+- Entered lp_controller.php -+- (action: '.$_REQUEST['action'].')', 0);
15

    
16
// Language files that needs to be included.
17
if (isset($_GET['action'])) {
18
    if ($_GET['action'] == 'export') {
19
        // Only needed on export.
20
        $language_file[] = 'hotspot';
21
    }
22
}
23
$language_file[] = 'course_home';
24
$language_file[] = 'scormdocument';
25
$language_file[] = 'document';
26
$language_file[] = 'scorm';
27
$language_file[] = 'learnpath';
28
$language_file[] = 'resourcelinker';
29
$language_file[] = 'registration';
30
$language_file[] = 'exercice';
31

    
32
// Including the global initialization file.
33
require_once '../inc/global.inc.php';
34
$current_course_tool  = TOOL_LEARNPATH;
35

    
36
if (api_get_setting('show_glossary_in_documents') == 'ismanual' || api_get_setting('show_glossary_in_documents') == 'isautomatic' ) {
37
    $htmlHeadXtra[] = '<script type="text/javascript">
38
<!--
39
    var jQueryFrameReadyConfigPath = \''.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.min.js\';
40
-->
41
</script>';
42
    $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.frameready.js" type="text/javascript" language="javascript"></script>';
43
    $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.highlight.js" type="text/javascript" language="javascript"></script>';
44
}
45

    
46
$htmlHeadXtra[] = '<script type="text/javascript">
47
function setFocus(){
48
    $("#idTitle").focus();
49
}
50
$(window).load(function () {
51
    setFocus();
52
});
53
</script>
54
<style>
55
form .label {
56
    padding: 1px 3px 2px;
57
    font-size: 100%;
58
    font-weight: normal;
59
    color: #ffffff;
60
    text-transform: none;
61
    background: none;
62
    border-radius: none;
63
    color: #404040;
64
    float: left;
65
    line-height: 18px;
66
    padding-top: 6px;
67
    text-align: right;
68
    width: 150px;
69
    text-shadow:none;
70
}
71
</style>';
72

    
73
// Flag to allow for anonymous user - needs to be set before global.inc.php.
74
$use_anonymous = true;
75

    
76
// Include class definitions before session_start() to ensure availability when touching
77
// session vars containing learning paths.
78
require_once 'learnpath.class.php';
79
if ($debug > 0) error_log('New LP - Included learnpath', 0);
80
require_once 'learnpathItem.class.php';
81
if ($debug > 0) error_log('New LP - Included learnpathItem', 0);
82
require_once 'scorm.class.php';
83
if ($debug > 0) error_log('New LP - Included scorm', 0);
84
require_once 'scormItem.class.php';
85
if ($debug > 0) error_log('New LP - Included scormItem', 0);
86
require_once 'aicc.class.php';
87
if ($debug > 0) error_log('New LP - Included aicc', 0);
88
require_once 'aiccItem.class.php';
89
if ($debug > 0) error_log('New LP - Included aiccItem', 0);
90

    
91
require_once 'back_compat.inc.php';
92
if ($debug > 0) error_log('New LP - Included back_compat', 0);
93

    
94
$session_id = api_get_session_id();
95

    
96
api_protect_course_script(true);
97

    
98
require_once api_get_path(LIBRARY_PATH).'fckeditor/fckeditor.php';
99
$lpfound = false;
100

    
101
$myrefresh = 0;
102
$myrefresh_id = 0;
103
if (!empty($_SESSION['refresh']) && $_SESSION['refresh'] == 1) {
104
    // Check if we should do a refresh of the oLP object (for example after editing the LP).
105
    // If refresh is set, we regenerate the oLP object from the database (kind of flush).
106
    Session::erase('refresh');
107
    $myrefresh = 1;
108
    if ($debug > 0) error_log('New LP - Refresh asked', 0);
109
}
110

    
111
if ($debug > 0) error_log('New LP - Passed refresh check', 0);
112

    
113
if (!empty($_REQUEST['dialog_box'])) {
114
    $dialog_box = stripslashes(urldecode($_REQUEST['dialog_box']));
115
}
116

    
117
$lp_controller_touched = 1;
118
$lp_found = false;
119

    
120
if (isset($_SESSION['lpobject'])) {
121
    if ($debug > 0) error_log('New LP - SESSION[lpobject] is defined', 0);
122
    $oLP = unserialize($_SESSION['lpobject']);
123
    if (isset($oLP) && is_object($oLP)) {
124
        if ($debug > 0) error_log('New LP - oLP is object', 0);
125
        if ($myrefresh == 1 OR (empty($oLP->cc)) OR $oLP->cc != api_get_course_id() OR $oLP->lp_view_session_id != $session_id OR $oLP->scorm_debug == '1') {
126
            if ($debug > 0) error_log('New LP - Course has changed, discard lp object', 0);
127
            if ($myrefresh == 1) { $myrefresh_id = $oLP->get_id(); }
128
            $oLP = null;
129
            Session::erase('oLP');
130
            Session::erase('lpobject');
131
        } else {
132
            $_SESSION['oLP'] = $oLP;
133
            $lp_found = true;
134
        }
135
    }
136
}
137

    
138
$course_id = api_get_course_int_id();
139

    
140
if ($debug>0) error_log('New LP - Passed data remains check', 0);
141

    
142
if (!$lp_found || (!empty($_REQUEST['lp_id']) && $_SESSION['oLP']->get_id() != $_REQUEST['lp_id'])) {
143
    if ($debug > 0) error_log('New LP - oLP is not object, has changed or refresh been asked, getting new', 0);
144
    // Regenerate a new lp object? Not always as some pages don't need the object (like upload?)
145
    if (!empty($_REQUEST['lp_id']) || !empty($myrefresh_id)) {
146
        if ($debug > 0) error_log('New LP - lp_id is defined', 0);
147
        // Select the lp in the database and check which type it is (scorm/dokeos/aicc) to generate the
148
        // right object.        
149
        if (!empty($_REQUEST['lp_id'])) {
150
            $lp_id = intval($_REQUEST['lp_id']);
151
        } else {
152
            $lp_id = intval($myrefresh_id);
153
        }
154
        
155
        $lp_table = Database::get_course_table(TABLE_LP_MAIN);
156
        if (is_numeric($lp_id)) {
157
            $sel = "SELECT lp_type FROM $lp_table WHERE c_id = $course_id AND id = $lp_id";
158
            if ($debug > 0) error_log('New LP - querying '.$sel, 0);
159
            $res = Database::query($sel);
160
            if (Database::num_rows($res)) {
161
                $row = Database::fetch_array($res);
162
                $type = $row['lp_type'];
163
                if ($debug > 0) error_log('New LP - found row - type '.$type. ' - Calling constructor with '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id(), 0);
164
                switch ($type) {
165
                    case 1:
166
                        if ($debug > 0) error_log('New LP - found row - type dokeos - Calling constructor with '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id(), 0);
167
                        $oLP = new learnpath(api_get_course_id(), $lp_id, api_get_user_id());
168
                        if ($oLP !== false) { $lp_found = true; } else { error_log($oLP->error, 0); }
169
                         break;
170
                    case 2:
171
                        if ($debug > 0) error_log('New LP - found row - type scorm - Calling constructor with '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id(), 0);
172
                        $oLP = new scorm(api_get_course_id(), $lp_id, api_get_user_id());
173
                        if ($oLP !== false) { $lp_found = true; } else { error_log($oLP->error, 0); }
174
                        break;
175
                    case 3:
176
                        if ($debug > 0) error_log('New LP - found row - type aicc - Calling constructor with '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id(), 0);
177
                        $oLP = new aicc(api_get_course_id(),$lp_id,api_get_user_id());
178
                        if ($oLP !== false) { $lp_found = true; } else { error_log($oLP->error, 0); }
179
                        break;
180
                    default:
181
                        if ($debug > 0) error_log('New LP - found row - type other - Calling constructor with '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id(), 0);
182
                        $oLP = new learnpath(api_get_course_id(),$lp_id,api_get_user_id());
183
                        if ($oLP !== false) { $lp_found = true; } else { error_log($oLP->error, 0); }
184
                        break;
185
                }
186
            }
187
        } else {
188
            if ($debug > 0) error_log('New LP - Request[lp_id] is not numeric', 0);
189
        }
190
    } else {
191
        if ($debug > 0) error_log('New LP - Request[lp_id] and refresh_id were empty', 0);
192
    }
193
    if ($lp_found) {
194
        $_SESSION['oLP'] = $oLP;
195
    }
196
}
197
if ($debug > 0) error_log('New LP - Passed oLP creation check', 0);
198

    
199
/**
200
 * Actions switching
201
 */
202
if (isset($_SESSION['oLP'])) {
203
    $_SESSION['oLP']->update_queue = array(); // Reinitialises array used by javascript to update items in the TOC.
204
    $_SESSION['oLP']->message = ''; // Should use ->clear_message() method but doesn't work.
205
}
206

    
207
if (isset($_GET['isStudentView']) && $_GET['isStudentView'] == 'true') {
208
    if ($_REQUEST['action'] != 'list' AND $_REQUEST['action'] != 'view') {
209
        if (!empty($_REQUEST['lp_id'])) {
210
            $_REQUEST['action'] = 'view';
211
        } else {
212
            $_REQUEST['action'] = 'list';
213
        }
214
    }
215
}
216

    
217
$is_allowed_to_edit = api_is_allowed_to_edit(null, true);
218

    
219
$action = (!empty($_REQUEST['action']) ? $_REQUEST['action'] : '');
220

    
221
switch ($action) {
222
    case 'add_item':
223
        if (!$is_allowed_to_edit) {
224
            api_not_allowed(true);
225
        }
226
        if ($debug > 0) error_log('New LP - add item action triggered', 0);
227

    
228
        if (!$lp_found) {
229
            //check if the learnpath ID was defined, otherwise send back to list
230
            if ($debug > 0) error_log('New LP - No learnpath given for add item', 0);
231
            require 'lp_list.php';
232
        } else {
233
            $_SESSION['refresh'] = 1;
234

    
235
            if (isset($_POST['submit_button']) && !empty($_POST['title'])) {
236
                // If a title was sumbitted:
237

    
238
                //Updating the lp.modified_on
239
                $_SESSION['oLP']->set_modified_on();
240

    
241
                if (isset($_SESSION['post_time']) && $_SESSION['post_time'] == $_POST['post_time']) {
242
                    // Check post_time to ensure ??? (counter-hacking measure?)
243
                    require 'lp_add_item.php';
244
                } else {
245
                    $_SESSION['post_time'] = $_POST['post_time'];
246
                    if ($_POST['type'] == TOOL_DOCUMENT) {
247
                        if (isset($_POST['path']) && $_GET['edit'] != 'true') {
248
                            $document_id = $_POST['path'];
249
                        } else {
250
                            $document_id = $_SESSION['oLP']->create_document($_course);
251
                        }
252
                        $new_item_id = $_SESSION['oLP']->add_item($_POST['parent'], $_POST['previous'], $_POST['type'], $document_id, $_POST['title'], $_POST['description'], $_POST['prerequisites']);
253
                    } else {
254
                        // For all other item types than documents, load the item using the item type and path rather than its ID.
255
                        $new_item_id = $_SESSION['oLP']->add_item($_POST['parent'], $_POST['previous'], $_POST['type'], $_POST['path'], $_POST['title'], $_POST['description'], $_POST['prerequisites'], $_POST['maxTimeAllowed']);
256
                    }
257
                    // Display.
258
                    require 'lp_add_item.php';
259
                }
260
            } else {
261
                require 'lp_add_item.php';
262
            }
263
        }
264
        break;
265
    case 'add_audio':
266
        if (!$is_allowed_to_edit) {
267
            api_not_allowed(true);
268
        }
269
        if ($debug > 0) error_log('New LP - add audio action triggered', 0);
270

    
271
        if (!$lp_found) {
272
            //check if the learnpath ID was defined, otherwise send back to list
273
            if ($debug > 0) error_log('New LP - No learnpath given for add audio', 0);
274
            require 'lp_list.php';
275
        } else {
276
            $_SESSION['refresh'] = 1;           
277

    
278
            if (isset($_REQUEST['id'])) {                
279
                $lp_item_obj = new learnpathItem($_REQUEST['id']);
280
                
281
                //Remove audio
282
                if (isset($_POST['delete_file']) && $_POST['delete_file'] == 1) {
283
                     $lp_item_obj->remove_audio();                    
284
                }
285
                
286
                //Upload audio
287
                if (isset($_FILES['file']) && !empty($_FILES['file'])) {
288
                    //Updating the lp.modified_on
289
                    $_SESSION['oLP']->set_modified_on();                    
290
                    $lp_item_obj->add_audio();
291
                }
292
                
293
                //Add audio file from documents
294
                if (isset($_REQUEST['document_id']) && !empty($_REQUEST['document_id'])) {
295
                    $_SESSION['oLP']->set_modified_on();                    
296
                    $lp_item_obj->add_audio_from_documents($_REQUEST['document_id']);
297
                }
298

    
299
                // Display.
300
                require 'lp_add_audio.php';                
301
            } else {
302
                require 'lp_add_audio.php';
303
            }
304
        }
305
        break;           
306
    case 'add_lp':
307
        if (!$is_allowed_to_edit) {
308
            api_not_allowed(true);
309
        }
310
        if ($debug > 0) error_log('New LP - add_lp action triggered', 0);
311
        $_REQUEST['lp_name'] = trim($_REQUEST['lp_name']);
312
        if (!empty($_REQUEST['lp_name'])) {
313
            $_SESSION['refresh'] = 1;
314

    
315
            if (isset($_SESSION['post_time']) && $_SESSION['post_time'] == $_REQUEST['post_time']) {
316
                require 'lp_add.php';
317
            } else {
318
                $_SESSION['post_time'] = $_REQUEST['post_time'];
319

    
320
                if (isset($_REQUEST['activate_start_date_check']) && $_REQUEST['activate_start_date_check'] == 1) {
321
                	$publicated_on  = $_REQUEST['publicated_on'];
322
                	$publicated_on  = $publicated_on['Y'].'-'.$publicated_on['F'].'-'.$publicated_on['d'].' '.$publicated_on['H'].':'.$publicated_on['i'].':00';
323
                } else {
324
                	$publicated_on = null;
325
                }
326

    
327
                if (isset($_REQUEST['activate_end_date_check']) && $_REQUEST['activate_end_date_check'] == 1) {
328
                	$expired_on   = $_REQUEST['expired_on'];
329
                	$expired_on   = $expired_on['Y'].'-'.$expired_on['F'].'-'.$expired_on['d'].' '.$expired_on['H'].':'.$expired_on['i'].':00';
330
                } else {
331
                	$expired_on   = null;
332
                }
333

    
334
                $new_lp_id = learnpath::add_lp(api_get_course_id(), Security::remove_XSS($_REQUEST['lp_name']), '', 'chamilo', 'manual', '', $publicated_on, $expired_on);
335

    
336
                if (is_numeric($new_lp_id)) {
337
                    // TODO: Maybe create a first module directly to avoid bugging the user with useless queries
338
                    $_SESSION['oLP'] = new learnpath(api_get_course_id(),$new_lp_id,api_get_user_id());
339
                    require 'lp_build.php';
340
                }
341
            }
342
        } else {
343
            require 'lp_add.php';
344
        }
345

    
346
        break;
347

    
348
    case 'admin_view':
349
        if (!$is_allowed_to_edit) {
350
            api_not_allowed(true);
351
        }
352
        if ($debug > 0) error_log('New LP - admin_view action triggered', 0);
353
        if (!$lp_found) { error_log('New LP - No learnpath given for admin_view', 0); require 'lp_list.php'; }
354
        else {
355
            $_SESSION['refresh'] = 1;
356
            require 'lp_admin_view.php';
357
        }
358
        break;
359
    case 'auto_launch':
360
        if (api_get_course_setting('enable_lp_auto_launch') == 1) { //Redirect to a specific LP
361
            if (!$is_allowed_to_edit) {
362
                api_not_allowed(true);
363
            }
364
            if ($debug > 0) error_log('New LP - auto_launch action triggered', 0);
365
            if (!$lp_found) { error_log('New LP - No learnpath given for set_autolunch', 0); require 'lp_list.php'; }
366
            else {
367
                $_SESSION['oLP']->set_autolunch($_GET['lp_id'], $_GET['status']);
368
                require 'lp_list.php';
369
                exit;
370
            }
371
        }
372
        break;
373
    case 'build':
374
        if (!$is_allowed_to_edit) {
375
            api_not_allowed(true);
376
        }
377
        if ($debug > 0) error_log('New LP - build action triggered', 0);
378

    
379
        if (!$lp_found) { error_log('New LP - No learnpath given for build', 0); require 'lp_list.php'; }
380
        else {
381
            $_SESSION['refresh'] = 1;
382
            require 'lp_build.php';
383
        }
384
        break;
385
    case 'edit_item':
386
        if (!$is_allowed_to_edit) {
387
            api_not_allowed(true);
388
        }
389
        if ($debug > 0) error_log('New LP - edit item action triggered', 0);
390

    
391
        if (!$lp_found) { error_log('New LP - No learnpath given for edit item', 0); require 'lp_list.php'; }
392
        else {
393
            $_SESSION['refresh'] = 1;
394
            if (isset($_POST['submit_button']) && !empty($_POST['title'])) {
395

    
396
                //Updating the lp.modified_on
397
                $_SESSION['oLP']->set_modified_on();
398

    
399
                //$_SESSION['oLP']->edit_item($_GET['id'], $_POST['parent'], $_POST['previous'], $_POST['title'], $_POST['description'], $_POST['prerequisites']);
400
                // TODO: mp3 edit
401
                $audio = array();
402
                
403
                if (isset($_FILES['mp3'])) $audio = $_FILES['mp3'];
404
                
405
                $_SESSION['oLP']->edit_item($_GET['id'], $_POST['parent'], $_POST['previous'], $_POST['title'], $_POST['description'], $_POST['prerequisites'], $audio, $_POST['maxTimeAllowed']);
406

    
407
                if (isset($_POST['content_lp'])) {
408
                    $_SESSION['oLP']->edit_document($_course);
409
                }
410
                $is_success = true;
411
            }
412
            if (isset($_GET['view']) && $_GET['view'] == 'build') {
413
                require 'lp_edit_item.php';
414
            } else {
415
                require 'lp_admin_view.php';
416
            }
417
        }
418
        break;
419

    
420
    case 'edit_item_prereq':
421
        if (!$is_allowed_to_edit) {
422
            api_not_allowed(true);
423
        }
424
        if ($debug > 0) error_log('New LP - edit item prereq action triggered', 0);
425

    
426
        if (!$lp_found) { error_log('New LP - No learnpath given for edit item prereq', 0); require 'lp_list.php'; }
427
        else {
428
            if (isset($_POST['submit_button'])) {
429
                //Updating the lp.modified_on
430
                $_SESSION['oLP']->set_modified_on();
431

    
432
                $_SESSION['refresh'] = 1;
433
                if ($_SESSION['oLP']->edit_item_prereq($_GET['id'], $_POST['prerequisites'], $_POST['min_' . $_POST['prerequisites']], $_POST['max_' . $_POST['prerequisites']])) {
434
                    $is_success = true;
435
                }
436
            }
437
            require 'lp_edit_item_prereq.php';
438
        }
439
        break;
440

    
441
    case 'move_item':
442
        if (!$is_allowed_to_edit) {
443
            api_not_allowed(true);
444
        }
445
        if ($debug > 0) error_log('New LP - move item action triggered', 0);
446

    
447
        if (!$lp_found) { error_log('New LP - No learnpath given for move item', 0); require 'lp_list.php'; }
448
        else {
449
            $_SESSION['refresh'] = 1;
450
            if (isset($_POST['submit_button'])) {
451
                //Updating the lp.modified_on
452
                $_SESSION['oLP']->set_modified_on();
453

    
454
                $_SESSION['oLP']->edit_item($_GET['id'], $_POST['parent'], $_POST['previous'], $_POST['title'], $_POST['description']);
455
                $is_success = true;
456
            }
457
            if (isset($_GET['view']) && $_GET['view'] == 'build') {
458
                require 'lp_move_item.php';
459
            } else {
460
                // Avoids weird behaviours see CT#967.
461
                $check = Security::check_token('get');
462
                if ($check) {
463
                    $_SESSION['oLP']->move_item($_GET['id'], $_GET['direction']);
464
                }
465
                Security::clear_token();
466
                require 'lp_admin_view.php';
467
            }
468
        }
469
        break;
470
    case 'view_item':
471
        if (!$is_allowed_to_edit) {
472
            api_not_allowed(true);
473
        }
474
        if ($debug > 0) error_log('New LP - view_item action triggered', 0);
475
        if (!$lp_found) {
476
            error_log('New LP - No learnpath given for view item', 0); require 'lp_list.php';
477
        } else {
478
            $_SESSION['refresh'] = 1;
479
            require 'lp_view_item.php';
480
        }
481
        break;
482
    case 'upload':
483
        if (!$is_allowed_to_edit) {
484
            api_not_allowed(true);
485
        }
486
        if ($debug > 0) error_log('New LP - upload action triggered', 0);
487
        $cwdir = getcwd();
488
        require 'lp_upload.php';
489
        // Reinit current working directory as many functions in upload change it.
490
        chdir($cwdir);
491
        require 'lp_list.php';
492
        break;
493

    
494
    case 'copy':
495
        if (!$is_allowed_to_edit) {
496
            api_not_allowed(true);
497
        }
498
        if ($debug > 0) error_log('New LP - export action triggered', 0);
499
        if (!$lp_found) { error_log('New LP - No learnpath given for copy', 0); require 'lp_list.php'; }
500
        else {
501
            $_SESSION['oLP']->copy();
502
        }
503
        require 'lp_list.php';
504
        break;
505
    case 'export':
506
        if (!$is_allowed_to_edit) {
507
            api_not_allowed(true);
508
        }
509
        if ($debug > 0) error_log('New LP - export action triggered', 0);
510
        if (!$lp_found) { error_log('New LP - No learnpath given for export', 0); require 'lp_list.php'; }
511
        else {
512
            $_SESSION['oLP']->scorm_export();
513
            exit();
514
            //require 'lp_list.php';
515
        }
516
        break;
517
    case 'export_to_pdf':
518

    
519
        if (!learnpath::is_lp_visible_for_student($_SESSION['oLP']->lp_id, api_get_user_id())) {
520
            api_not_allowed();
521
        }
522
        if ($debug > 0) error_log('New LP - export action triggered', 0);
523
        if (!$lp_found) { error_log('New LP - No learnpath given for export_to_pdf', 0); require 'lp_list.php';
524
        } else {
525
            $result = $_SESSION['oLP']->scorm_export_to_pdf($_GET['lp_id']);
526
            if (!$result) {
527
                require 'lp_list.php';
528
            }
529
            exit;
530
        }
531
        break;
532
    case 'delete':
533
        if (!$is_allowed_to_edit) {
534
            api_not_allowed(true);
535
        }
536
        if ($debug > 0) error_log('New LP - delete action triggered', 0);
537
        if (!$lp_found) { error_log('New LP - No learnpath given for delete', 0); require 'lp_list.php'; }
538
        else {
539
            $_SESSION['refresh'] = 1;
540
            // Remove lp from homepage if it is there.
541
            //$_SESSION['oLP']->toggle_visibility((int)$_GET['lp_id'],'i');
542
            $_SESSION['oLP']->delete(null,(int)$_GET['lp_id'],'remove');
543
            Session::erase('oLP');
544
            require 'lp_list.php';
545
        }
546
        break;
547

    
548
    case 'toggle_visible': // Change lp visibility (inside lp tool).
549
        if (!$is_allowed_to_edit) {
550
            api_not_allowed(true);
551
        }
552
        if ($debug > 0) error_log('New LP - visibility action triggered', 0);
553
        if (!$lp_found) { error_log('New LP - No learnpath given for visibility', 0); require 'lp_list.php'; }
554
        else {
555
            learnpath::toggle_visibility($_REQUEST['lp_id'], $_REQUEST['new_status']);
556
            require 'lp_list.php';
557
        }
558
        break;
559

    
560
    case 'toggle_publish': // Change lp published status (visibility on homepage).
561
        if (!$is_allowed_to_edit) {
562
            api_not_allowed(true);
563
        }
564
        if ($debug > 0) error_log('New LP - publish action triggered', 0);
565
        if (!$lp_found) { error_log('New LP - No learnpath given for publish', 0); require 'lp_list.php'; }
566
        else {
567
            learnpath::toggle_publish($_REQUEST['lp_id'],$_REQUEST['new_status']);
568
            require 'lp_list.php';
569
        }
570
        break;
571

    
572
    case 'move_lp_up': // Change lp published status (visibility on homepage)
573
        if (!$is_allowed_to_edit) {
574
            api_not_allowed(true);
575
        }
576
        if ($debug > 0) error_log('New LP - publish action triggered', 0);
577
        if (!$lp_found) {
578
            error_log('New LP - No learnpath given for publish', 0);
579
            require 'lp_list.php';
580
        } else {
581
            learnpath::move_up($_REQUEST['lp_id']);
582
            require 'lp_list.php';
583
        }
584
        break;
585

    
586
    case 'move_lp_down': //change lp published status (visibility on homepage)
587
        if (!$is_allowed_to_edit) {
588
            api_not_allowed(true);
589
        }
590
        if ($debug > 0) error_log('New LP - publish action triggered', 0);
591
        if (!$lp_found) {
592
            error_log('New LP - No learnpath given for publish', 0);
593
            require 'lp_list.php';
594
        } else {
595
            learnpath::move_down($_REQUEST['lp_id']);
596
            require 'lp_list.php';
597
        }
598
        break;
599

    
600
    case 'edit':
601
        if (!$is_allowed_to_edit) {
602
            api_not_allowed(true);
603
        }
604
        if ($debug > 0) error_log('New LP - edit action triggered', 0);
605
        if (!$lp_found) { error_log('New LP - No learnpath given for edit', 0); require 'lp_list.php'; }
606
        else {
607
            $_SESSION['refresh'] = 1;
608
            require 'lp_edit.php';
609
        }
610
        break;
611

    
612
    case 'update_lp':
613
        if (!$is_allowed_to_edit) {
614
            api_not_allowed(true);
615
        }
616
        if ($debug > 0) error_log('New LP - update_lp action triggered', 0);
617
        if (!$lp_found) { error_log('New LP - No learnpath given for edit', 0); require 'lp_list.php'; }
618
        else {
619
            $_SESSION['refresh'] = 1;
620
            $lp_name = Security::remove_XSS($_REQUEST['lp_name']);
621
            $_SESSION['oLP']->set_name($lp_name);
622
            $author = $_REQUEST['lp_author'];
623
            // Fixing the author name (no body or html tags).
624
            $auth_init = stripos($author, '<p>');
625
            if ($auth_init === false) {
626
                $auth_init = stripos($author, '<body>');
627
                $auth_end = $auth_init + stripos(substr($author, $auth_init + 6), '</body>') + 7;
628
                $len = $auth_end - $auth_init + 6;
629
            } else {
630
                $auth_end = strripos($author, '</p>');
631
                $len = $auth_end - $auth_init + 4;
632
            }
633

    
634
            $author_fixed = substr($author, $auth_init, $len);
635
            //$author_fixed = $author;
636

    
637
            $_SESSION['oLP']->set_author($author_fixed);
638
            $_SESSION['oLP']->set_encoding($_REQUEST['lp_encoding']);  // TODO (as of Chamilo 1.8.8): Check in the future whether this field is needed.
639
            $_SESSION['oLP']->set_maker($_REQUEST['lp_maker']);
640
            $_SESSION['oLP']->set_proximity($_REQUEST['lp_proximity']);
641
            $_SESSION['oLP']->set_theme($_REQUEST['lp_theme']);
642
			$_SESSION['oLP']->set_hide_toc_frame($_REQUEST['hide_toc_frame']);
643
            $_SESSION['oLP']->set_prerequisite($_REQUEST['prerequisites']);
644
            $_SESSION['oLP']->set_use_max_score($_REQUEST['use_max_score']);
645

    
646

    
647
            if (isset($_REQUEST['activate_start_date_check']) && $_REQUEST['activate_start_date_check'] == 1) {
648
            	$publicated_on  = $_REQUEST['publicated_on'];
649
            	$publicated_on  = $publicated_on['Y'].'-'.$publicated_on['F'].'-'.$publicated_on['d'].' '.$publicated_on['H'].':'.$publicated_on['i'].':00';
650
            } else {
651
            	$publicated_on = null;
652
            }
653

    
654
            if (isset($_REQUEST['activate_end_date_check']) && $_REQUEST['activate_end_date_check'] == 1) {
655
            	$expired_on   = $_REQUEST['expired_on'];
656
            	$expired_on   = $expired_on['Y'].'-'.$expired_on['F'].'-'.$expired_on['d'].' '.$expired_on['H'].':'.$expired_on['i'].':00';
657
            } else {
658
            	$expired_on   = null;
659
            }
660

    
661
            $_SESSION['oLP']->set_modified_on();
662
            $_SESSION['oLP']->set_publicated_on($publicated_on);
663
            $_SESSION['oLP']->set_expired_on($expired_on);
664

    
665
            if ($_REQUEST['remove_picture']) {
666
                $_SESSION['oLP']->delete_lp_image();
667
            }
668

    
669
            if ($_FILES['lp_preview_image']['size'] > 0)
670
                $_SESSION['oLP']->upload_image($_FILES['lp_preview_image']);
671

    
672
            if (api_get_setting('search_enabled') === 'true') {
673
                require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
674
                $specific_fields = get_specific_field_list();
675
                foreach ($specific_fields as $specific_field) {
676
                    $_SESSION['oLP']->set_terms_by_prefix($_REQUEST[$specific_field['code']], $specific_field['code']);
677
                    $new_values = explode(',', trim($_REQUEST[$specific_field['code']]));
678
                    if (!empty($new_values)) {
679
                        array_walk($new_values, 'trim');
680
                        delete_all_specific_field_value(api_get_course_id(), $specific_field['id'], TOOL_LEARNPATH, $_SESSION['oLP']->lp_id);
681

    
682
                        foreach ($new_values as $value) {
683
                            if (!empty($value)) {
684
                                add_specific_field_value($specific_field['id'], api_get_course_id(), TOOL_LEARNPATH, $_SESSION['oLP']->lp_id, $value);
685
                            }
686
                        }
687
                    }
688
                }
689
            }
690
            require 'lp_build.php';
691
        }
692
        break;
693

    
694
    case 'add_sub_item': // Add an item inside a chapter.
695
        if (!$is_allowed_to_edit) {
696
            api_not_allowed(true);
697
        }
698
        if ($debug > 0) error_log('New LP - add sub item action triggered', 0);
699
        if (!$lp_found) { error_log('New LP - No learnpath given for add sub item', 0); require 'lp_list.php'; }
700
        else {
701
            $_SESSION['refresh'] = 1;
702
            if (!empty($_REQUEST['parent_item_id'])) {
703
                $_SESSION['from_learnpath']='yes';
704
                $_SESSION['origintoolurl'] = 'lp_controller.php?action=admin_view&lp_id='.Security::remove_XSS($_REQUEST['lp_id']);
705
                require 'resourcelinker.php';
706
                //$_SESSION['oLP']->add_sub_item($_REQUEST['parent_item_id'], $_REQUEST['previous'], $_REQUEST['type'], $_REQUEST['path'], $_REQUEST['title']);
707
            } else {
708
                require 'lp_admin_view.php';
709
            }
710
        }
711
        break;
712
    case 'deleteitem':
713
    case 'delete_item':
714
        if (!$is_allowed_to_edit) {
715
            api_not_allowed(true);
716
        }
717
        if ($debug > 0) error_log('New LP - delete item action triggered', 0);
718
        if (!$lp_found) { error_log('New LP - No learnpath given for delete item', 0); require 'lp_list.php'; }
719
        else {
720
            //$_SESSION['refresh'] = 1;
721
            if (!empty($_REQUEST['id'])) {
722
                $_SESSION['oLP']->delete_item($_REQUEST['id']);
723
            }
724
            $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_REQUEST['lp_id']);
725
            header('Location: '.$url);
726
            exit;
727
        }
728
        break;
729
    case 'edititemprereq':
730
    case 'edit_item_prereq':
731
        if (!$is_allowed_to_edit) {
732
            api_not_allowed(true);
733
        }
734
        if ($debug > 0) error_log('New LP - edit item prereq action triggered', 0);
735
        if (!$lp_found) { error_log('New LP - No learnpath given for edit item prereq', 0); require 'lp_list.php'; }
736
        else {
737
            if (!empty($_REQUEST['id']) && !empty($_REQUEST['submit_item'])) {
738
                $_SESSION['refresh'] = 1;
739
                $_SESSION['oLP']->edit_item_prereq($_REQUEST['id'], $_REQUEST['prereq']);
740
            }
741
            require 'lp_admin_view.php';
742
        }
743
        break;
744

    
745
    case 'restart':
746
        if ($debug > 0) error_log('New LP - restart action triggered', 0);
747
        if (!$lp_found) { error_log('New LP - No learnpath given for restart', 0); require 'lp_list.php'; }
748
        else {
749
            $_SESSION['oLP']->restart();
750
            require 'lp_view.php';
751
        }
752
        break;
753

    
754
    case 'last':
755
        if ($debug > 0) error_log('New LP - last action triggered', 0);
756
        if (!$lp_found) { error_log('New LP - No learnpath given for last', 0); require 'lp_list.php'; }
757
        else {
758
            $_SESSION['oLP']->last();
759
            require 'lp_view.php';
760
        }
761
        break;
762

    
763
    case 'first':
764
        if ($debug > 0) error_log('New LP - first action triggered', 0);
765
        if (!$lp_found) { error_log('New LP - No learnpath given for first', 0); require 'lp_list.php'; }
766
        else {
767
            $_SESSION['oLP']->first();
768
            require 'lp_view.php';
769
        }
770
        break;
771

    
772
    case 'next':
773
        if ($debug > 0) error_log('New LP - next action triggered', 0);
774
        if (!$lp_found) { error_log('New LP - No learnpath given for next', 0); require 'lp_list.php'; }
775
        else {
776
            $_SESSION['oLP']->next();
777
            require 'lp_view.php';
778
        }
779
        break;
780
    case 'previous':
781
        if ($debug > 0) error_log('New LP - previous action triggered', 0);
782
        if (!$lp_found) { error_log('New LP - No learnpath given for previous', 0); require 'lp_list.php'; }
783
        else {
784
            $_SESSION['oLP']->previous();
785
            require 'lp_view.php';
786
        }
787
        break;
788
    case 'content':
789
        if ($debug > 0) error_log('New LP - content action triggered', 0);
790
        if ($debug > 0) error_log('New LP - Item id is '.$_GET['item_id'], 0);
791
        if (!$lp_found) { error_log('New LP - No learnpath given for content', 0); require 'lp_list.php'; }
792
        else {
793
            $_SESSION['oLP']->save_last();
794
            $_SESSION['oLP']->set_current_item($_GET['item_id']);
795
            $_SESSION['oLP']->start_current_item();
796
            require 'lp_content.php';
797
        }
798
        break;
799
    case 'view':
800
        if ($debug > 0)
801
            error_log('New LP - view action triggered', 0);
802
        if (!$lp_found) {
803
            error_log('New LP - No learnpath given for view', 0);
804
            require 'lp_list.php';
805
        } else {
806
            if ($debug > 0) {error_log('New LP - Trying to set current item to ' . $_REQUEST['item_id'], 0); }
807
            if ( !empty($_REQUEST['item_id']) ) {
808
                $_SESSION['oLP']->set_current_item($_REQUEST['item_id']);
809
            }
810
            require 'lp_view.php';
811
        }
812
        break;
813
    case 'save':
814
        if ($debug > 0) error_log('New LP - save action triggered', 0);
815
        if (!$lp_found) { error_log('New LP - No learnpath given for save', 0); require 'lp_list.php'; }
816
        else {
817
            $_SESSION['oLP']->save_item();
818
            require 'lp_save.php';
819
        }
820
        break;
821

    
822
    case 'stats':
823
        if ($debug > 0) error_log('New LP - stats action triggered', 0);
824
        if (!$lp_found) { error_log('New LP - No learnpath given for stats', 0); require 'lp_list.php'; }
825
        else {
826
            $_SESSION['oLP']->save_current();
827
            $_SESSION['oLP']->save_last();                   
828
            require 'lp_stats.php';
829
        }
830
        break;
831
    case 'list':
832
        if ($debug > 0) error_log('New LP - list action triggered', 0);
833
        if ($lp_found) {
834
            $_SESSION['refresh'] = 1;
835
            $_SESSION['oLP']->save_last();
836
        }
837
        require 'lp_list.php';
838
        break;
839

    
840
    case 'mode':
841
        // Switch between fullscreen and embedded mode.
842
        if ($debug > 0) error_log('New LP - mode change triggered', 0);
843
        $mode = $_REQUEST['mode'];
844
        if ($mode == 'fullscreen') {
845
            $_SESSION['oLP']->mode = 'fullscreen';
846
        } elseif ($mode == 'embedded') {
847
            $_SESSION['oLP']->mode = 'embedded';
848
        } elseif ($mode == 'embedframe') {
849
        	$_SESSION['oLP']->mode = 'embedframe';
850
        }
851
        require 'lp_view.php';
852
        break;
853

    
854
    case 'switch_view_mode':
855
        if ($debug > 0) error_log('New LP - switch_view_mode action triggered', 0);
856
        if (!$lp_found) { error_log('New LP - No learnpath given for switch', 0); require 'lp_list.php'; }
857
        $_SESSION['refresh'] = 1;
858
        $_SESSION['oLP']->update_default_view_mode();
859
        require 'lp_list.php';
860
        break;
861

    
862
    case 'switch_force_commit':
863
        if ($debug > 0) error_log('New LP - switch_force_commit action triggered', 0);
864
        if (!$lp_found) { error_log('New LP - No learnpath given for switch', 0); require 'lp_list.php'; }
865
        $_SESSION['refresh'] = 1;
866
        $_SESSION['oLP']->update_default_scorm_commit();
867
        require 'lp_list.php';
868
        break;
869
    /* Those 2 switches have been replaced by switc_attempt_mode switch
870
    case 'switch_reinit':
871
        if ($debug > 0) error_log('New LP - switch_reinit action triggered', 0);
872
        if (!$lp_found) { error_log('New LP - No learnpath given for switch', 0); require 'lp_list.php'; }
873
        $_SESSION['refresh'] = 1;
874
        $_SESSION['oLP']->update_reinit();
875
		require 'lp_list.php';
876
		break;
877
	case 'switch_seriousgame_mode':
878
		if($debug>0) error_log('New LP - switch_seriousgame_mode action triggered',0);
879
		if(!$lp_found){ error_log('New LP - No learnpath given for switch',0); require 'lp_list.php'; }
880
		$_SESSION['refresh'] = 1;
881
		$_SESSION['oLP']->set_seriousgame_mode();
882
		require 'lp_list.php';
883
		break;
884
     */
885
	case 'switch_attempt_mode':
886
		if($debug>0) error_log('New LP - switch_reinit action triggered',0);
887
		if(!$lp_found){ error_log('New LP - No learnpath given for switch',0); require 'lp_list.php'; }
888
		$_SESSION['refresh'] = 1;
889
		$_SESSION['oLP']->switch_attempt_mode();
890
        require 'lp_list.php';
891
        break;
892

    
893
    case 'switch_scorm_debug':
894
        if ($debug > 0) error_log('New LP - switch_scorm_debug action triggered', 0);
895
        if (!$lp_found) { error_log('New LP - No learnpath given for switch', 0); require 'lp_list.php'; }
896
        $_SESSION['refresh'] = 1;
897
        $_SESSION['oLP']->update_scorm_debug();
898
        require 'lp_list.php';
899
        break;
900

    
901
    case 'intro_cmdAdd':
902
        if ($debug > 0) error_log('New LP - intro_cmdAdd action triggered', 0);
903
        // Add introduction section page.
904
        break;
905

    
906
    case 'js_api_refresh':
907
        if ($debug > 0) error_log('New LP - js_api_refresh action triggered', 0);
908
        if (!$lp_found) { error_log('New LP - No learnpath given for js_api_refresh', 0); require 'lp_message.php'; }
909
        if (isset($_REQUEST['item_id'])) {
910
            $htmlHeadXtra[] = $_SESSION['oLP']->get_js_info($_REQUEST['item_id']);
911
        }
912
        require 'lp_message.php';
913
        break;
914
    case 'return_to_course_homepage':
915
        if (!$lp_found) { error_log('New LP - No learnpath given for stats', 0); require 'lp_list.php'; }
916
        else {
917
            $_SESSION['oLP']->save_current();
918
            $_SESSION['oLP']->save_last();            
919
            header('location: '.api_get_path(WEB_COURSE_PATH).api_get_course_path().'/?id_session='.api_get_session_id());
920
        }
921
        break;
922
    case 'search':
923
        /* Include the search script, it's smart enough to know when we are
924
         * searching or not.
925
         */
926
        require 'lp_list_search.php';
927
        break;
928
    case 'impress':
929
        if ($debug > 0)
930
            error_log('New LP - view action triggered', 0);
931
        if (!$lp_found) {
932
            error_log('New LP - No learnpath given for view', 0);
933
            require 'lp_list.php';
934
        } else {
935
            if ($debug > 0) {error_log('New LP - Trying to impress this LP item to ' . $_REQUEST['item_id'], 0); }
936
            if ( !empty($_REQUEST['item_id']) ) {
937
                $_SESSION['oLP']->set_current_item($_REQUEST['item_id']);
938
            }
939
            require 'lp_impress.php';
940
        }
941
        break;
942
    default:
943
        if ($debug > 0) error_log('New LP - default action triggered', 0);
944
        //$_SESSION['refresh'] = 1;
945
        require 'lp_list.php';
946
        break;
947
}
948

    
949
if (!empty($_SESSION['oLP'])) {
950
    $_SESSION['lpobject'] = serialize($_SESSION['oLP']);
951
    if ($debug > 0) error_log('New LP - lpobject is serialized in session', 0);
952
}
(2-2/4)