Project

General

Profile

Bug #4142 ยป learnpath.class.php

Yoselyn Castillo, 22/08/2012 18:19

 
1
<?php
2
/* For licensing terms, see /license.txt */
3
/**
4
 * This class defines the parent attributes and methods for Chamilo learnpaths and SCORM
5
 * learnpaths. It is used by the scorm class.
6
 *
7
 * @package chamilo.learnpath
8
 * @author	Yannick Warnier <ywarnier@beeznest.org>
9
 * @author	Julio Montoya   <gugli100@gmail.com> Several improvements and fixes
10
 */
11
/**
12
 * Defines the learnpath parent class
13
 * @package chamilo.learnpath
14
 */
15
class learnpath {
16

    
17
    public $attempt = 0; // The number for the current ID view.
18
    public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
19
    public $current; // Id of the current item the user is viewing.
20
    public $current_score; // The score of the current item.
21
    public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
22
    public $current_time_stop; // The time the user closed this resource.
23
    public $default_status = 'not attempted';
24
    public $encoding = 'UTF-8';
25
    public $error = '';
26
    public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
27
    public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
28
    public $index; // The index of the active learnpath_item in $ordered_items array.
29
    public $items = array();
30
    public $last; // item_id of last item viewed in the learning path.
31
    public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
32
    public $license; // Which license this course has been given - not used yet on 20060522.
33
    public $lp_id; // DB ID for this learnpath.
34
    public $lp_view_id; // DB ID for lp_view
35
    public $log_file; // File where to log learnpath API msg.
36
    public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
37
    public $message = '';
38
    public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
39
    public $name; // Learnpath name (they generally have one).
40
    public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
41
    public $path = ''; // Path inside the scorm directory (if scorm).
42
    public $theme; // The current theme of the learning path.
43
    public $preview_image; // The current image of the learning path.
44

    
45
    // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
46
    public $prevent_reinit = 1;
47

    
48
    // Describes the mode of progress bar display.
49
    public $seriousgame_mode = 0;
50

    
51
    public $progress_bar_mode = '%';
52

    
53
    // Percentage progress as saved in the db.
54
    public $progress_db = '0';
55
    public $proximity; // Wether the content is distant or local or unknown.
56
    public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
57
    // !!!This array (refs_list) is built differently depending on the nature of the LP.
58
    // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
59
    public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
60
    // TODO: Check if this type variable is useful here (instead of just in the controller script).
61
    public $user_id; //ID of the user that is viewing/using the course
62
    public $update_queue = array();
63
    public $scorm_debug = 0;
64

    
65
    public $arrMenu = array(); // Array for the menu items.
66

    
67
    public $debug = 0; // Logging level.
68

    
69
    public $lp_session_id = 0;
70
    public $lp_view_session_id = 0; // The specific view might be bound to a session.
71

    
72
    public $prerequisite = 0;
73
    public $use_max_score = 1; // 1 or 0
74

    
75
    public $created_on      = '';
76
    public $modified_on     = '';
77
    public $publicated_on   = '';
78
    public $expired_on      = '';
79
    public $ref = null;
80

    
81
    /**
82
     * Class constructor. Needs a database handler, a course code and a learnpath id from the database.
83
     * Also builds the list of items into $this->items.
84
     * @param	string		Course code
85
     * @param	integer		Learnpath ID
86
     * @param	integer		User ID
87
     * @return	boolean		True on success, false on error
88
     */
89
    public function __construct($course, $lp_id, $user_id) {
90
        $this->encoding = api_get_system_encoding(); // Chamilo 1.8.8: We intend always to use the system encoding.
91
        // Check params.
92
        // Check course code.
93
        $course_id = api_get_course_int_id();
94

    
95
        if ($this->debug > 0) {error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0); }
96
        if (empty($course)) {
97
            $this->error = 'Course code is empty';
98
            return false;
99
        } else {
100
            $main_table = Database::get_main_table(TABLE_MAIN_COURSE);
101
            $course = Database::escape_string($course);
102
            $sql = "SELECT * FROM $main_table WHERE code = '$course'";
103
            if ($this->debug > 2) { error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying course: '.$sql, 0); }
104
            $res = Database::query($sql);
105
            if (Database::num_rows($res) > 0) {
106
                $this->cc 			= $course;
107
                $row_course         = Database::fetch_array($res);
108
                $course_id 	        = $row_course['id'];
109
            } else {
110
                $this->error = 'Course code does not exist in database ('.$sql.')';
111
                return false;
112
            }
113
        }
114

    
115
        // Check learnpath ID.
116
        if (empty($lp_id)) {
117
            $this->error = 'Learnpath ID is empty';
118
            return false;
119
        } else {
120
            // TODO: Make it flexible to use any course_code (still using env course code here).
121
            $lp_table = Database::get_course_table(TABLE_LP_MAIN);
122
            $lp_id = intval($lp_id);
123
            $sql = "SELECT * FROM $lp_table WHERE id = '$lp_id' AND c_id = $course_id";
124
            if ($this->debug > 2) { error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0); }
125
            $res = Database::query($sql);
126
            if (Database::num_rows($res) > 0) {
127
                $this->lp_id            = $lp_id;
128
                $row                    = Database::fetch_array($res);
129
                $this->type             = $row['lp_type'];
130
                $this->name             = stripslashes($row['name']);
131
                //$this->encoding       = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
132
                $this->proximity        = $row['content_local'];
133
                $this->theme            = $row['theme'];
134
                $this->maker            = $row['content_maker'];
135
                $this->prevent_reinit   = $row['prevent_reinit'];
136
    	        $this->seriousgame_mode = $row['seriousgame_mode'];
137
                $this->license          = $row['content_license'];
138
                $this->scorm_debug      = $row['debug'];
139
                $this->js_lib           = $row['js_lib'];
140
                $this->path             = $row['path'];
141
                $this->preview_image    = $row['preview_image'];
142
                $this->author           = $row['author'];
143
                $this->hide_toc_frame   = $row['hide_toc_frame'];
144
                $this->lp_session_id    = $row['session_id'];
145
                $this->use_max_score    = $row['use_max_score'];
146

    
147
                $this->created_on       = $row['created_on'];
148
                $this->modified_on      = $row['modified_on'];                
149
                $this->ref              = $row['ref'];
150

    
151
                if ($row['publicated_on'] != '0000-00-00 00:00:00') {
152
                    $this->publicated_on   = $row['publicated_on'];
153
                }
154

    
155
                if ($row['expired_on'] != '0000-00-00 00:00:00') {
156
                    $this->expired_on     = $row['expired_on'];
157
                }
158
                if ($this->type == 2) {
159
                    if ($row['force_commit'] == 1) {
160
                        $this->force_commit = true;
161
                    }
162
                }
163
                $this->mode = $row['default_view_mod'];
164
            } else {
165
                $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
166
                return false;
167
            }
168
        }
169

    
170
        // Check user ID.
171
        if (empty($user_id)) {
172
            $this->error = 'User ID is empty';
173
            return false;
174
        } else {            
175
            $user_info  = api_get_user_info($user_id);
176
            if (!empty($user_info)) {
177
                $this->user_id = $user_info['user_id'];
178
            } else {
179
                $this->error = 'User ID does not exist in database ('.$sql.')';
180
                return false;
181
            }
182
        }
183
        // End of variables checking.
184

    
185
        $session_id = api_get_session_id();
186
        //  Get the session condition for learning paths of the base + session.
187
        $session = api_get_session_condition($session_id);
188
        // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
189
        $lp_table = Database::get_course_table(TABLE_LP_VIEW);
190
        // Selecting by view_count descending allows to get the highest view_count first.
191
        $sql = "SELECT * FROM $lp_table WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session ORDER BY view_count DESC";
192
        
193
        if ($this->debug > 2) { error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0); }
194
        $res = Database::query($sql);
195
        $view_id = 0; // Used later to query lp_item_view.
196
        if (Database :: num_rows($res) > 0) {
197
            if ($this->debug > 2) {
198
                error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
199
            }
200
            $row = Database :: fetch_array($res);
201
            $this->attempt              = $row['view_count'];
202
            $this->lp_view_id           = $row['id'];
203
            $this->last_item_seen       = $row['last_item'];
204
            $this->progress_db          = $row['progress'];
205
            $this->lp_view_session_id   = $row['session_id'];
206
        } else {
207
            if ($this->debug > 2) {
208
                error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
209
            }
210
            $this->attempt = 1;
211
            $sql_ins = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id) VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";            
212
            $res_ins = Database::query($sql_ins);
213
            $this->lp_view_id = Database :: insert_id();
214
            if ($this->debug > 2) {
215
                error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql_ins, 0);
216
            }
217
        }
218

    
219
        // Initialise items.
220
        $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
221
        $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' ORDER BY parent_item_id, display_order";                
222
        $res = Database::query($sql);
223
        
224
        if ($this->debug > 2) {
225
            error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
226
            error_log('-- Start while--', 0);
227
        }
228
        
229
        $lp_item_id_list = array();
230
        while ($row = Database::fetch_array($res)) {
231
            $oItem = '';
232
            $lp_item_id_list[] = $row['id'];
233
            switch ($this->type) {
234
                case 3: //aicc
235
                    $oItem = new aiccItem('db', $row['id'], $course_id);
236
                    if (is_object($oItem)) {
237
                        $my_item_id = $oItem->get_id();
238
                        $oItem->set_lp_view($this->lp_view_id, $course_id);
239
                        $oItem->set_prevent_reinit($this->prevent_reinit);
240
                        // Don't use reference here as the next loop will make the pointed object change.
241
                        $this->items[$my_item_id] = $oItem;
242
                        $this->refs_list[$oItem->ref] = $my_item_id;
243
                        if ($this->debug > 2) {
244
                            error_log('New LP - learnpath::__construct() - aicc object with id ' . $my_item_id . ' set in items[]', 0);
245
                        }
246
                    }
247
                    break;
248
                case 2:
249
                    require_once 'scorm.class.php';
250
                    require_once 'scormItem.class.php';
251
                    $oItem = new scormItem('db', $row['id'], $course_id);
252
                    if (is_object($oItem)) {
253
                        $my_item_id = $oItem->get_id();
254
                        $oItem->set_lp_view($this->lp_view_id, $course_id);
255
                        $oItem->set_prevent_reinit($this->prevent_reinit);
256
                        // Don't use reference here as the next loop will make the pointed object change.
257

    
258
                        $this->items[$my_item_id] = $oItem;
259
                        $this->refs_list[$oItem->ref] = $my_item_id;
260
                        if ($this->debug > 2) {
261
                            error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
262
                        }
263
                    }
264
                    break;
265
                case 1:
266
                default:
267
                    if ($this->debug > 2) {
268
                        error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
269
                    }
270
                    $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
271
                    if ($this->debug > 2) {
272
                        error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
273
                    }
274
                    if (is_object($oItem)) {
275
                        $my_item_id = $oItem->get_id();
276
                        //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
277
                        $oItem->set_prevent_reinit($this->prevent_reinit);
278
                        // Don't use reference here as the next loop will make the pointed object change.
279
                        $this->items[$my_item_id] = $oItem;
280
                        $this->refs_list[$my_item_id] = $my_item_id;
281
                        if ($this->debug > 2) {
282
                            error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - object with id ' . $my_item_id . ' set in items[]', 0);
283
                        }
284
                    }
285
                    break;
286
            }
287

    
288
            // Items is a list of pointers to all items, classified by DB ID, not SCO id.
289
            if ($row['parent_item_id'] == 0 || empty ($this->items[$row['parent_item_id']])) {
290
                if (is_object($this->items[$row['id']])) {
291
                  $this->items[$row['id']]->set_level(0);
292
                }
293
            } else {
294
                $level = $this->items[$row['parent_item_id']]->get_level() + 1;
295
                $this->items[$row['id']]->set_level($level);
296
                if (is_object($this->items[$row['parent_item_id']])) {
297
                    // Items is a list of pointers from item DB ids to item objects.
298
                    $this->items[$row['parent_item_id']]->add_child($row['id']);
299
                } else {
300
                    if ($this->debug > 2) {
301
                        error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - The parent item (' . $row['parent_item_id'] . ') of item ' . $row['id'] . ' could not be found', 0);
302
                    }
303
                }
304
            }
305
            
306
            // Setting the view in the item object.
307
            if (is_object($this->items[$row['id']])) {
308
                $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
309
            }
310
        }
311
        
312
        if ($this->debug > 2) {
313
            error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
314
        }
315
        
316
        if (!empty($lp_item_id_list)) {            
317
            $lp_item_id_list_to_string = implode("','", $lp_item_id_list);        
318
        
319
            // Get last viewing vars.
320
            $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
321
            // This query should only return one or zero result.
322
            $sql = "SELECT lp_item_id, status FROM $lp_item_view_table
323
                    WHERE c_id = $course_id AND lp_view_id = ".$this->lp_view_id." AND lp_item_id IN ('".$lp_item_id_list_to_string."')
324
                    ORDER BY view_count DESC ";
325
            
326
            if ($this->debug > 2) {
327
                error_log('New LP - learnpath::__construct() - Selecting item_views: ' . $sql, 0);
328
            }
329
            $status_list = array();            
330
            $res = Database::query($sql);
331
            while ($row = Database :: fetch_array($res) ) {
332
                $status_list[$row['lp_item_id']] = $row['status'];
333
            }
334
                
335
            foreach ($lp_item_id_list as $item_id) {
336
                if (isset($status_list[$item_id])) {                    
337
                    $status = $status_list[$item_id];
338
                    if (is_object($this->items[$item_id])) {
339
                        $this->items[$item_id]->set_status($status);
340
                        if (empty ($status)) {
341
                            $this->items[$item_id]->set_status($this->default_status);
342
                        }
343
                    }
344
                } else {
345
                    if (is_object($this->items[$item_id])) {
346
                        $this->items[$item_id]->set_status($this->default_status);
347
                    }
348
                    // Add that row to the lp_item_view table so that we have something to show in the stats page.
349
                    $sql_ins = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
350
                                VALUES ($course_id, ".$item_id . "," . $this->lp_view_id . ",1,'not attempted')";
351
                    if ($this->debug > 2) {
352
                        error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Inserting blank item_view : ' . $sql_ins, 0);
353
                    }
354
                    $res_ins = Database::query($sql_ins);
355
                }
356
            }            
357
        }
358
            
359
        
360
        $this->ordered_items = $this->get_flat_ordered_items_list($this->get_id(), 0, $course_id);
361
        $this->max_ordered_items = 0;
362
        foreach ($this->ordered_items as $index => $dummy) {
363
            if ($index > $this->max_ordered_items && !empty($dummy)) {
364
                $this->max_ordered_items = $index;
365
            }
366
        }
367
        // TODO: Define the current item better.
368
        $this->first();
369
        if ($this->debug > 2) {
370
            error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
371
        }
372
    }
373

    
374
    /**
375
     * Function rewritten based on old_add_item() from Yannick Warnier. Due the fact that users can decide where the item should come, I had to overlook this function and
376
     * I found it better to rewrite it. Old function is still available. Added also the possibility to add a description.
377
     *
378
     * @param int $parent
379
     * @param int $previous
380
     * @param string $type
381
     * @param int  resource ID (ref)
382
     * @param string $title
383
     * @param string $description
384
     * @return int
385
     */
386
    public function add_item($parent, $previous, $type = 'dokeos_chapter', $id, $title, $description, $prerequisites = 0, $max_time_allowed = 0) {
387
        $course_id = api_get_course_int_id();
388
        if ($this->debug > 0) {
389
            error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
390
        }
391
        $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
392

    
393
        $parent = intval($parent);
394
        $previous = intval($previous);
395
        $type = Database::escape_string($type);
396
        $id = intval($id);
397
        $max_time_allowed = Database::escape_string(htmlentities($max_time_allowed));
398
        if (empty ($max_time_allowed)) {
399
            $max_time_allowed = 0;
400
        }
401
        $title       = Database::escape_string($title);
402
        $description = Database::escape_string($description);
403
        $sql_count = "	SELECT COUNT(id) AS num
404
                        FROM $tbl_lp_item
405
                        WHERE c_id = $course_id AND lp_id = " . $this->get_id() . " AND parent_item_id = " . $parent;
406

    
407
        $res_count = Database::query($sql_count);
408
        $row = Database :: fetch_array($res_count);
409
        $num = $row['num'];
410

    
411
        if ($num > 0) {
412
            if ($previous == 0) {
413
                $sql = "SELECT id, next_item_id, display_order
414
                           FROM " . $tbl_lp_item . "
415
                           WHERE   c_id = $course_id AND
416
                                   lp_id = " . $this->get_id() . " AND
417
                                   parent_item_id = " . $parent . " AND
418
                                   previous_item_id = 0 OR previous_item_id=" . $parent;
419
                $result = Database::query($sql);
420
                $row = Database :: fetch_array($result);
421

    
422
                $tmp_previous = 0;
423
                $next = $row['id'];
424
                $display_order = 0;
425
            } else {
426
                $previous = (int) $previous;
427
                $sql = "SELECT id, previous_item_id, next_item_id, display_order
428
						FROM $tbl_lp_item
429
                        WHERE c_id = $course_id AND lp_id = " . $this->get_id() . " AND id = " . $previous;
430

    
431
                $result = Database::query($sql);
432
                $row 	= Database :: fetch_array($result);
433

    
434
                $tmp_previous = $row['id'];
435
                $next = $row['next_item_id'];
436

    
437
                $display_order = $row['display_order'];
438
            }
439
        } else {
440
            $tmp_previous = 0;
441
            $next = 0;
442
            $display_order = 0;
443
        }
444

    
445
        $new_item_id = -1;
446
        $id = Database::escape_string($id);
447

    
448
        if ($type == 'quiz') {
449
            $sql = 'SELECT SUM(ponderation)
450
					FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
451
                    INNER JOIN  ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
452
                    ON quiz_question.id = quiz_rel_question.question_id
453
                    WHERE   quiz_rel_question.exercice_id = '.$id." AND
454
	            			quiz_question.c_id = $course_id AND
455
	            			quiz_rel_question.c_id = $course_id ";
456
            $rsQuiz = Database::query($sql);
457
            $max_score = Database :: result($rsQuiz, 0, 0);
458

    
459
            //Disabling the exercise if we add it inside a LP
460
            $exercise = new Exercise();
461
            $exercise->read($id);
462
            $exercise->disable();
463
            $exercise->save();
464
        } else {
465
            $max_score = 100;
466
        }
467

    
468
        if ($prerequisites != 0) {
469
            $sql_ins = "INSERT INTO " . $tbl_lp_item . " (
470
            					c_id,
471
                                lp_id, ".
472
                                "item_type, ".
473
                                "ref, ".
474
                                "title, ".
475
                                "description, ".
476
                                "path, ".
477
                                "max_score, ".
478
                                "parent_item_id, ".
479
                                "previous_item_id, ".
480
                                "next_item_id, ".
481
                                "display_order, ".
482
                                "prerequisite, ".
483
                                "max_time_allowed ".
484
                            ") VALUES (
485
                            	$course_id ,
486
                                ".$this->get_id() . ", ".
487
                                "'" . $type . "', ".
488
                                "'', ".
489
                                "'" . $title . "', ".
490
                                "'" . $description . "', ".
491
                                "'" . $id . "', ".
492
                                "'" . $max_score . "', ".
493
                                $parent . ", ".
494
                                $previous . ", ".
495
                                $next . ", ".
496
                                ($display_order +1) . ", ".
497
                                $prerequisites . ", ".
498
                                $max_time_allowed .
499
                            ")";
500
        } else {
501
            // Insert new item.
502
            $sql_ins = "
503
                            INSERT INTO " . $tbl_lp_item . " ( ".
504
            					"c_id, ".
505
                                "lp_id, ".
506
                                "item_type, ".
507
                                "ref, ".
508
                                "title, ".
509
                                "description, ".
510
                                "path, ".
511
                                "max_score, ".
512
                                "parent_item_id, ".
513
                                "previous_item_id, ".
514
                                "next_item_id, ".
515
                                "display_order, ".
516
                                "max_time_allowed ".
517
                            ") VALUES (".
518
            					$course_id. ",".
519
                                $this->get_id() . ",".
520
                                "'" . $type . "',".
521
                                "'',".
522
                                "'" . $title . "',".
523
                                "'" . $description . "',".
524
                                "'" . $id . "',".
525
                                "'" . $max_score . "',".
526
                                $parent . ",".
527
                                $previous . ",".
528
                                $next . ",".
529
                                ($display_order +1) . ",".
530
                                $max_time_allowed .
531
                            ")";
532
        }
533

    
534
        if ($this->debug > 2) {
535
            error_log('New LP - Inserting dokeos_chapter: ' . $sql_ins, 0);
536
        }
537

    
538
        $res_ins = Database::query($sql_ins);
539

    
540
        if ($res_ins > 0) {
541
            $new_item_id = Database :: insert_id($res_ins);
542

    
543
            // Update the item that should come after the new item.
544
            $sql_update_next = "
545
                            UPDATE " . $tbl_lp_item . "
546
                            SET previous_item_id = " . $new_item_id . "
547
                            WHERE c_id = $course_id AND id = " . $next;
548

    
549
            Database::query($sql_update_next);
550

    
551
            // Update the item that should be before the new item.
552
            $sql_update_previous = "
553
                            UPDATE " . $tbl_lp_item . "
554
                            SET next_item_id = " . $new_item_id . "
555
                            WHERE c_id = $course_id AND id = " . $tmp_previous;
556

    
557
            Database::query($sql_update_previous);
558

    
559
            // Update all the items after the new item.
560
            $sql_update_order = "
561
                            UPDATE " . $tbl_lp_item . "
562
                            SET display_order = display_order + 1
563
                            WHERE
564
                                c_id = $course_id AND
565
                                lp_id = " . $this->get_id() . " AND
566
                                id <> " . $new_item_id . " AND
567
                                parent_item_id = " . $parent . " AND
568
                                display_order > " . $display_order;
569

    
570
            Database::query($sql_update_order);
571

    
572
            // Update the item that should come after the new item.
573
            $sql_update_ref = "UPDATE " . $tbl_lp_item . "
574
                               SET ref = " . $new_item_id . "
575
                               WHERE c_id = $course_id AND id = " . $new_item_id;
576
            Database::query($sql_update_ref);
577

    
578
        }
579

    
580
        // Upload audio.
581
        if (!empty ($_FILES['mp3']['name'])) {
582
            // Create the audio folder if it does not exist yet.
583
            global $_course;
584
            $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
585
            if (!is_dir($filepath . 'audio')) {
586
                mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
587
                $audio_id = add_document($_course, '/audio', 'folder', 0, 'audio');
588
                api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'FolderCreated', api_get_user_id(), null, null, null, null, api_get_session_id());
589
				api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'invisible', api_get_user_id(), null, null, null, null, api_get_session_id());
590
            }
591

    
592
            // Upload the file in the documents tool.
593
            include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
594
            $file_path = handle_uploaded_document($_course, $_FILES['mp3'], api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document', '/audio', api_get_user_id(), '', '', '', '', '', false);
595

    
596
            // Getting the filename only.
597
            $file_components = explode('/', $file_path);
598
            $file = $file_components[count($file_components) - 1];
599

    
600
            // Store the mp3 file in the lp_item table.
601
            $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '" . Database::escape_string($file) . "' WHERE id = '" . Database::escape_string($new_item_id) . "'";
602
            Database::query($sql_insert_audio);
603
        }
604
        return $new_item_id;
605
    }
606

    
607
    /**
608
     * Static admin function allowing addition of a learnpath to a course.
609
     * @param	string	Course code
610
     * @param	string	Learnpath name
611
     * @param	string	Learnpath description string, if provided
612
     * @param	string	Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
613
     * @param	string	Type of files origin (default = 'zip', others = 'dir','web_dir',...)
614
     * @param	string	Zip file containing the learnpath or directory containing the learnpath
615
     * @return	integer	The new learnpath ID on success, 0 on failure
616
     */
617
    public static function add_lp($course, $name, $description = '', $learnpath = 'guess', $origin = 'zip', $zipname = '', $publicated_on = '', $expired_on = '') {
618
        global $charset;
619
        $course_id = api_get_course_int_id();
620
        $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
621
        // Check course code exists.
622
        // Check lp_name doesn't exist, otherwise append something.
623
        $i = 0;
624
        $name = Database::escape_string($name);
625

    
626
        // Session id.
627
        $session_id = api_get_session_id();
628

    
629
        $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
630
        //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
631
        $res_name = Database::query($check_name);
632

    
633
        if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
634
            //by default the publication date is the same that the creation date
635
            //The behaviour above was changed due BT#2800
636
        	global $_custom;
637
        	if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
638
            	$publicated_on = '';
639
        	} else {
640
        		$publicated_on = api_get_utc_datetime();
641
        	}
642
        } else {
643
            $publicated_on   = Database::escape_string(api_get_utc_datetime($publicated_on));
644
        }
645

    
646
        if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
647
            $expired_on = '';
648
        } else {
649
            $expired_on   = Database::escape_string(api_get_utc_datetime($expired_on));
650
        }
651

    
652
        while (Database :: num_rows($res_name)) {
653
            // There is already one such name, update the current one a bit.
654
            $i++;
655
            $name = $name . ' - ' . $i;
656
            $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
657
            //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
658
            $res_name = Database::query($check_name);
659
        }
660
        // New name does not exist yet; keep it.
661
        // Escape description.
662
        $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset)); // Kevin: added htmlentities().
663
        $type = 1;
664
        switch ($learnpath) {
665
            case 'guess':
666
                break;
667
            case 'dokeos':
668
            case 'chamilo':
669
                $type = 1;
670
                break;
671
            case 'aicc':
672
                break;
673
        }
674
        switch ($origin) {
675
            case 'zip':
676
                // Check zipname string. If empty, we are currently creating a new Chamilo learnpath.
677
                break;
678
            case 'manual':
679
            default:
680
                $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
681
                $res_max = Database::query($get_max);
682
                if (Database :: num_rows($res_max) < 1) {
683
                    $dsp = 1;
684
                } else {
685
                    $row = Database :: fetch_array($res_max);
686
                    $dsp = $row[0] + 1;
687
                }
688

    
689
                $sql_insert = "INSERT INTO $tbl_lp (c_id, lp_type,name,description,path,default_view_mod, default_encoding,display_order,content_maker,content_local,js_lib,session_id, created_on, publicated_on, expired_on) " .
690
                              "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."')";
691

    
692
                $res_insert = Database::query($sql_insert);
693
                $id = Database :: insert_id();
694
                if ($id > 0) {
695
                    // Insert into item_property.
696
                    api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
697
                    return $id;
698
                }
699
                break;
700
        }
701
    }
702

    
703
    /**
704
     * Appends a message to the message attribute
705
     * @param	string	Message to append.
706
     */
707
    public function append_message($string) {
708
        if ($this->debug > 0) {
709
            error_log('New LP - In learnpath::append_message()', 0);
710
        }
711
        $this->message .= $string;
712
    }
713

    
714
    /**
715
     * Autocompletes the parents of an item in case it's been completed or passed
716
     * @param	integer	Optional ID of the item from which to look for parents
717
     */
718
    public function autocomplete_parents($item) {
719
        $course_id = api_get_course_int_id();
720
        if ($this->debug > 0) {
721
            error_log('New LP - In learnpath::autocomplete_parents()', 0);
722
        }
723
        if (empty ($item)) {
724
            $item = $this->current;
725
        }
726
        $parent_id = $this->items[$item]->get_parent();
727
        if ($this->debug > 2) {
728
            error_log('New LP - autocompleting parent of item ' . $item . ' (item ' . $parent_id . ')', 0);
729
        }
730
        if (is_object($this->items[$item]) and !empty ($parent_id)) {
731
            // if $item points to an object and there is a parent.
732
            if ($this->debug > 2) {
733
                error_log('New LP - ' . $item . ' is an item, proceed', 0);
734
            }
735
            $current_item = & $this->items[$item];
736
            $parent = & $this->items[$parent_id]; // Get the parent.
737
            // New experiment including failed and browsed in completed status.
738
            $current_status = $current_item->get_status();
739
            if ($current_item->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
740
                // If the current item is completed or passes or succeeded.
741
                $completed = true;
742
                if ($this->debug > 2) {
743
                    error_log('New LP - Status of current item is alright', 0);
744
                }
745
                foreach ($parent->get_children() as $child) {
746
                    // Check all his brothers (his parent's children) for completion status.
747
                    if ($child != $item) {
748
                        if ($this->debug > 2) {
749
                            error_log('New LP - Looking at brother with ID ' . $child . ', status is ' . $this->items[$child]->get_status(), 0);
750
                        }
751
                        //if($this->items[$child]->status_is(array('completed','passed','succeeded')))
752
                        // Trying completing parents of failed and browsed items as well.
753
                        if ($this->items[$child]->status_is(array (
754
                                'completed',
755
                                'passed',
756
                                'succeeded',
757
                                'browsed',
758
                                'failed'
759
                            ))) {
760
                            // Keep completion status to true.
761
                        } else {
762
                            if ($this->debug > 2) {
763
                                error_log('New LP - Found one incomplete child of ' . $parent_id . ': ' . $child . ' is ' . $this->items[$child]->get_status(), 0);
764
                            }
765
                            $completed = false;
766
                        }
767
                    }
768
                }
769
                if ($completed) { // If all the children were completed:
770
                    $parent->set_status('completed');
771
                    $parent->save(false, $this->prerequisites_match($parent->get_id()));
772
                    $this->update_queue[$parent->get_id()] = $parent->get_status();
773
                    if ($this->debug > 2) {
774
                        error_log('New LP - Added parent to update queue ' . print_r($this->update_queue, true), 0);
775
                    }
776
                    $this->autocomplete_parents($parent->get_id()); // Recursive call.
777
                }
778
            } else {
779
                //error_log('New LP - status of current item is not enough to get bothered with it', 0);
780
            }
781
        }
782
    }
783

    
784
    /**
785
     * Autosaves the current results into the database for the whole learnpath
786
     */
787
    public function autosave() {
788
        if ($this->debug > 0) {
789
            error_log('New LP - In learnpath::autosave()', 0);
790
        }
791
        // TODO: Add save operations for the learnpath itself.
792
    }
793

    
794
    /**
795
     * Clears the message attribute
796
     */
797
    public function clear_message() {
798
        if ($this->debug > 0) {
799
            error_log('New LP - In learnpath::clear_message()', 0);
800
        }
801
        $this->message = '';
802
    }
803

    
804
    /**
805
     * Closes the current resource
806
     *
807
     * Stops the timer
808
     * Saves into the database if required
809
     * Clears the current resource data from this object
810
     * @return	boolean	True on success, false on failure
811
     */
812
    public function close() {
813
        $course_id = api_get_course_int_id();
814
        if ($this->debug > 0) {
815
            error_log('New LP - In learnpath::close()', 0);
816
        }
817
        if (empty ($this->lp_id)) {
818
            $this->error = 'Trying to close this learnpath but no ID is set';
819
            return false;
820
        }
821
        $this->current_time_stop = time();
822
        if ($this->save) {
823
            $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
824
            /*
825
            $sql = "UPDATE $learnpath_view_table " .
826
                    "SET " .
827
                    "stop_time = ".$this->current_time_stop.", " .
828
                    "score = ".$this->current_score.", ".
829
                    "WHERE learnpath_id = '".$this->lp_id."'";
830
            //$res = Database::query($sql);
831
            $res = Database::query($res);
832
            if (Database::affected_rows($res) < 1) {
833
                $this->error = 'Could not update learnpath_view table while closing learnpath';
834
                return false;
835
            }
836
            */
837
        }
838
        $this->ordered_items = array ();
839
        $this->index = 0;
840
        unset ($this->lp_id);
841
        //unset other stuff
842
        return true;
843
    }
844

    
845
    /**
846
     * Static admin function allowing removal of a learnpath
847
     * @param	string	Course code
848
     * @param	integer	Learnpath ID
849
     * @param	string	Whether to delete data or keep it (default: 'keep', others: 'remove')
850
     * @return	boolean	True on success, false on failure (might change that to return number of elements deleted)
851
     */
852
    public function delete($course = null, $id = null, $delete = 'keep') {
853
        $course_id = api_get_course_int_id();
854

    
855
        // TODO: Implement a way of getting this to work when the current object is not set.
856
        // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
857
        //if (empty($course)) { $course = api_get_course_id(); }
858
        //if (empty($id)) { $id = $this->get_id(); }
859
        // If an ID is specifically given and the current LP is not the same, prevent delete.
860
        if (!empty ($id) && ($id != $this->lp_id)) {
861
            return false;
862
        }
863

    
864
        $lp             = Database :: get_course_table(TABLE_LP_MAIN);
865
        $lp_item        = Database :: get_course_table(TABLE_LP_ITEM); // Proposed by Christophe (clefevre), see below.
866
        $lp_view        = Database :: get_course_table(TABLE_LP_VIEW);
867
        $lp_item_view   = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
868

    
869

    
870
        //if ($this->debug > 0) { error_log('New LP - In learnpath::delete()', 0); }
871
        // Delete lp item id.
872
        foreach ($this->items as $id => $dummy) {
873
            //$this->items[$id]->delete();
874
            $sql_del_view = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
875
            $res_del_item_view = Database::query($sql_del_view);
876
        }
877

    
878
        // Proposed by Christophe (nickname: clefevre), see http://www.dokeos.com/forum/viewtopic.php?t=29673
879
        $sql_del_item = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
880
        $res_del_item = Database::query($sql_del_item);
881

    
882
        $sql_del_view = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
883
        //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
884
        $res_del_view = Database::query($sql_del_view);
885
        self::toggle_publish($this->lp_id, 'i');
886
        //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
887
        if ($this->type == 2 || $this->type == 3) {
888
            // This is a scorm learning path, delete the files as well.
889
            $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
890
            $res = Database::query($sql);
891
            if (Database :: num_rows($res) > 0) {
892
                $row = Database :: fetch_array($res);
893
                $path = $row['path'];
894
                $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
895
                $res = Database::query($sql);
896
                if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
897
                    if ($this->debug > 2) {
898
                        error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
899
                    }
900
                } else {
901
                    // No other LP uses that directory, delete it.
902
                    $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
903
                    $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
904
                    if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
905
                        if ($this->debug > 2) {
906
                            error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
907
                        }
908
                        // Proposed by Christophe (clefevre).
909
                        if (strcmp(substr($path, -2), "/.") == 0) {
910
                            $path = substr($path, 0, -1); // Remove "." at the end.
911
                        }
912
                        //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
913
                        rmdirr($course_scorm_dir . $path);
914
                    }
915
                }
916
            }
917
        }
918
        $sql_del_lp = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
919
        //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.': '.$sql_del_lp, 0); }
920
        $res_del_lp = Database::query($sql_del_lp);
921
        $this->update_display_order(); // Updates the display order of all lps.
922
        api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $this->lp_id, 'delete', api_get_user_id());
923

    
924
        require_once '../gradebook/lib/be.inc.php';
925

    
926
        // Delete link of gradebook tool
927
        //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
928
        /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
929
        $result = Database::query($sql);
930
        $row = Database :: fetch_array($result, 'ASSOC');*/
931

    
932
        // Fixing gradebook link deleted see #5229.
933
        /*
934
        if (!empty($row['id'])) {
935
               $link = LinkFactory :: load($row['id']);
936
            if ($link[0] != null) {
937
                   $link[0]->delete();
938
            }
939
        }*/
940
        require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
941
        $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
942
        if ($link_info !== false) {
943
            remove_resource_from_course_gradebook($link_info['id']);
944
        }
945

    
946
        if (api_get_setting('search_enabled') == 'true') {
947
            require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
948
            $r = delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
949
        }
950
    }
951

    
952
    /**
953
     * Removes all the children of one item - dangerous!
954
     * @param	integer	Element ID of which children have to be removed
955
     * @return	integer	Total number of children removed
956
     */
957
    public function delete_children_items($id) {
958
        $course_id = api_get_course_int_id();
959
        if ($this->debug > 0) {
960
            error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
961
        }
962
        $num = 0;
963
        if (empty ($id) || $id != strval(intval($id))) {
964
            return false;
965
        }
966
        $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
967
        $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
968
        $res = Database::query($sql);
969
        while ($row = Database :: fetch_array($res)) {
970
            $num += $this->delete_children_items($row['id']);
971
            $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
972
            $res_del = Database::query($sql_del);
973
            $num++;
974
        }
975
        return $num;
976
    }
977

    
978
    /**
979
     * Removes an item from the current learnpath
980
     * @param	integer	Elem ID (0 if first)
981
     * @param	integer	Whether to remove the resource/data from the system or leave it (default: 'keep', others 'remove')
982
     * @return	integer	Number of elements moved
983
     * @todo implement resource removal
984
     */
985
    public function delete_item($id, $remove = 'keep') {
986
        $course_id = api_get_course_int_id();
987
        if ($this->debug > 0) {
988
            error_log('New LP - In learnpath::delete_item()', 0);
989
        }
990
        // TODO: Implement the resource removal.
991
        if (empty ($id) || $id != strval(intval($id))) {
992
            return false;
993
        }
994
        // First select item to get previous, next, and display order.
995
        $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
996
        $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
997
        $res_sel = Database::query($sql_sel);
998
        if (Database :: num_rows($res_sel) < 1) {
999
            return false;
1000
        }
1001
        $row = Database :: fetch_array($res_sel);
1002
        $previous = $row['previous_item_id'];
1003
        $next = $row['next_item_id'];
1004
        $display = $row['display_order'];
1005
        $parent = $row['parent_item_id'];
1006
        $lp = $row['lp_id'];
1007
        // Delete children items.
1008
        $num = $this->delete_children_items($id);
1009
        if ($this->debug > 2) {
1010
            error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
1011
        }
1012
        // Now delete the item.
1013
        $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
1014
        if ($this->debug > 2) {
1015
            error_log('New LP - Deleting item: ' . $sql_del, 0);
1016
        }
1017
        $res_del = Database::query($sql_del);
1018
        // Now update surrounding items.
1019
        $sql_upd = "UPDATE $lp_item SET next_item_id = $next WHERE c_id = ".$course_id." AND id = $previous";
1020
        $res_upd = Database::query($sql_upd);
1021
        $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous WHERE c_id = ".$course_id." AND id = $next";
1022
        $res_upd = Database::query($sql_upd);
1023
        // Now update all following items with new display order.
1024
        $sql_all = "UPDATE $lp_item SET display_order = display_order-1 WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
1025
        $res_all = Database::query($sql_all);
1026

    
1027
        //Removing prerequisites since the item will not longer exist
1028
        $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
1029
        $res_all = Database::query($sql_all);
1030

    
1031
        // Remove from search engine if enabled.
1032
        if (api_get_setting('search_enabled') == 'true') {
1033
            $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
1034
            $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
1035
            $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
1036
            $res = Database::query($sql);
1037
            if (Database :: num_rows($res) > 0) {
1038
                $row2 = Database :: fetch_array($res);
1039
                require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
1040
                $di = new ChamiloIndexer();
1041
                $di->remove_document((int) $row2['search_did']);
1042
            }
1043
            $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
1044
            $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
1045
            Database::query($sql);
1046
        }
1047
    }
1048

    
1049
    /**
1050
     * Updates an item's content in place
1051
     * @param	integer	Element ID
1052
     * @param	integer	Parent item ID
1053
     * @param	integer Previous item ID
1054
     * @param   string	Item title
1055
     * @param   string  Item description
1056
     * @param   string  Prerequisites (optional)
1057
     * @param   string  Indexing terms (optional)
1058
     * @param   array   The array resulting of the $_FILES[mp3] element
1059
     * @return	boolean	True on success, false on error
1060
     */
1061
    public function edit_item($id, $parent, $previous, $title, $description, $prerequisites = 0, $audio = null, $max_time_allowed = 0) {
1062
        $course_id = api_get_course_int_id();
1063
        if ($this->debug > 0) {
1064
            error_log('New LP - In learnpath::edit_item()', 0);
1065
        }
1066
        if (empty ($max_time_allowed)) {
1067
            $max_time_allowed = 0;
1068
        }
1069
        if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
1070
            return false;
1071
        }
1072
        
1073
        $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
1074
        $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
1075
        $res_select = Database::query($sql_select);
1076
        $row_select = Database :: fetch_array($res_select);
1077
        $audio_update_sql = '';
1078
        if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
1079
            // Create the audio folder if it does not exist yet.
1080
            global $_course;
1081
            $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
1082
            if (!is_dir($filepath . 'audio')) {
1083
                mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
1084
                $audio_id = add_document($_course, '/audio', 'folder', 0, 'audio');
1085
                api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'FolderCreated', api_get_user_id(), null, null, null, null, api_get_session_id());
1086
				api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'invisible', api_get_user_id(), null, null, null, null, api_get_session_id());
1087
            }
1088

    
1089
            // Upload file in documents.
1090
            $pi = pathinfo($audio['name']);
1091
            if ($pi['extension'] == 'mp3') {
1092
                $c_det = api_get_course_info($this->cc);
1093
                $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
1094
                $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, '', 0, 'rename', false, 0);
1095
                $path = substr($path, 7);
1096
                // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
1097
                $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
1098
            }
1099
        }
1100

    
1101
        $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
1102
        $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
1103

    
1104
        // TODO: htmlspecialchars to be checked for encoding related problems.
1105
        if ($same_parent && $same_previous) {
1106
            // Only update title and description.
1107
            $sql_update = " UPDATE " . $tbl_lp_item . "
1108
                            SET title = '" . Database::escape_string($title) . "',
1109
                                prerequisite = '" . $prerequisites . "',
1110
                                description = '" . Database::escape_string($description) . "'
1111
                                " . $audio_update_sql . ",
1112
                                max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
1113
                            WHERE c_id = ".$course_id." AND id = " . $id;
1114
            $res_update = Database::query($sql_update);
1115
        } else {
1116
            $old_parent = $row_select['parent_item_id'];
1117
            $old_previous = $row_select['previous_item_id'];
1118
            $old_next = $row_select['next_item_id'];
1119
            $old_order = $row_select['display_order'];
1120
            $old_prerequisite = $row_select['prerequisite'];
1121
            $old_max_time_allowed = $row_select['max_time_allowed'];
1122

    
1123
            /* BEGIN -- virtually remove the current item id */
1124
            /* for the next and previous item it is like the current item doesn't exist anymore */
1125

    
1126
            if ($old_previous != 0) {
1127
                $sql_update_next = "
1128
                                    UPDATE " . $tbl_lp_item . "
1129
                                    SET next_item_id = " . $old_next . "
1130
                                    WHERE c_id = ".$course_id." AND id = " . $old_previous;
1131
                $res_update_next = Database::query($sql_update_next);
1132
                //echo '<p>' . $sql_update_next . '</p>';
1133
            }
1134

    
1135
            if ($old_next != 0) {
1136
                $sql_update_previous = "
1137
                                    UPDATE " . $tbl_lp_item . "
1138
                                    SET previous_item_id = " . $old_previous . "
1139
                                    WHERE c_id = ".$course_id." AND id = " . $old_next;
1140
                $res_update_previous = Database::query($sql_update_previous);
1141

    
1142
                //echo '<p>' . $sql_update_previous . '</p>';
1143
            }
1144

    
1145
            // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
1146
            $sql_update_order = "
1147
                            UPDATE " . $tbl_lp_item . "
1148
                            SET display_order = display_order - 1
1149
                            WHERE
1150
                                c_id = ".$course_id." AND
1151
                                display_order > " . $old_order . " AND lp_id = " . $this->lp_id . " AND
1152
                                parent_item_id = " . $old_parent;
1153
            $res_update_order = Database::query($sql_update_order);
1154

    
1155
            //echo '<p>' . $sql_update_order . '</p>';
1156

    
1157
            /* END -- virtually remove the current item id */
1158

    
1159
            /* BEGIN -- update the current item id to his new location */
1160

    
1161
            if ($previous == 0) {
1162
                // Select the data of the item that should come after the current item.
1163
                $sql_select_old = "SELECT id, display_order
1164
                                    FROM " . $tbl_lp_item . "
1165
                                    WHERE
1166
                                        c_id = ".$course_id." AND
1167
                                        lp_id = " . $this->lp_id . " AND
1168
                                        parent_item_id = " . $parent . " AND
1169
                                        previous_item_id = " . $previous;
1170
                $res_select_old = Database::query($sql_select_old);
1171
                $row_select_old = Database :: fetch_array($res_select_old);
1172

    
1173
                //echo '<p>' . $sql_select_old . '</p>';
1174

    
1175
                // If the new parent didn't have children before.
1176
                if (Database :: num_rows($res_select_old) == 0) {
1177
                    $new_next = 0;
1178
                    $new_order = 1;
1179
                } else {
1180
                    $new_next = $row_select_old['id'];
1181
                    $new_order = $row_select_old['display_order'];
1182
                }
1183

    
1184
                //echo 'New next_item_id of current item: ' . $new_next . '<br />';
1185
                //echo 'New previous_item_id of current item: ' . $previous . '<br />';
1186
                //echo 'New display_order of current item: ' . $new_order . '<br />';
1187

    
1188
            } else {
1189
                // Select the data of the item that should come before the current item.
1190
                $sql_select_old = " SELECT next_item_id, display_order
1191
                                    FROM " . $tbl_lp_item . "
1192
                                    WHERE c_id = ".$course_id." AND id = " . $previous;
1193
                $res_select_old = Database::query($sql_select_old);
1194
                $row_select_old = Database :: fetch_array($res_select_old);
1195

    
1196
                //echo '<p>' . $sql_select_old . '</p>';
1197

    
1198
                //echo 'New next_item_id of current item: ' . $row_select_old['next_item_id'] . '<br />';
1199
                //echo 'New previous_item_id of current item: ' . $previous . '<br />';
1200
                //echo 'New display_order of current item: ' . ($row_select_old['display_order'] + 1) . '<br />';
1201

    
1202
                $new_next = $row_select_old['next_item_id'];
1203
                $new_order = $row_select_old['display_order'] + 1;
1204
            }
1205

    
1206
            // TODO: htmlspecialchars to be checked for encoding related problems.
1207
            // Update the current item with the new data.
1208
            $sql_update = "UPDATE " . $tbl_lp_item . "
1209
                            SET
1210
                                title = '" . Database::escape_string($title) . "',
1211
                                description = '" . Database::escape_string($description) . "',
1212
                                parent_item_id = " . $parent . ",
1213
                                previous_item_id = " . $previous . ",
1214
                                next_item_id = " . $new_next . ",
1215
                                display_order = " . $new_order . "
1216
                                " . $audio_update_sql . "
1217
                            WHERE c_id = ".$course_id." AND id = " . $id;
1218
            $res_update_next = Database::query($sql_update);
1219
            //echo '<p>' . $sql_update . '</p>';
1220

    
1221
            if ($previous != 0) {
1222
                // Update the previous item's next_item_id.
1223
                $sql_update_previous = "
1224
                                    UPDATE " . $tbl_lp_item . "
1225
                                    SET next_item_id = " . $id . "
1226
                                    WHERE c_id = ".$course_id." AND id = " . $previous;
1227
                $res_update_next = Database::query($sql_update_previous);
1228
                //echo '<p>' . $sql_update_previous . '</p>';
1229
            }
1230

    
1231
            if ($new_next != 0) {
1232
                // Update the next item's previous_item_id.
1233
                $sql_update_next = "
1234
                                    UPDATE " . $tbl_lp_item . "
1235
                                    SET previous_item_id = " . $id . "
1236
                                    WHERE c_id = ".$course_id." AND id = " . $new_next;
1237
                $res_update_next = Database::query($sql_update_next);
1238
                //echo '<p>' . $sql_update_next . '</p>';
1239
            }
1240

    
1241
            if ($old_prerequisite != $prerequisites) {
1242
                $sql_update_next = "
1243
                                    UPDATE " . $tbl_lp_item . "
1244
                                    SET prerequisite = " . $prerequisites . "
1245
                                    WHERE c_id = ".$course_id." AND id = " . $id;
1246
                $res_update_next = Database::query($sql_update_next);
1247
            }
1248

    
1249
            if ($old_max_time_allowed != $max_time_allowed) {
1250
                $sql_update_max_time_allowed = "
1251
                                    UPDATE " . $tbl_lp_item . "
1252
                                    SET max_time_allowed = " . $max_time_allowed . "
1253
                                    WHERE c_id = ".$course_id." AND id = " . $id;
1254
                $res_update_max_time_allowed = Database::query($sql_update_max_time_allowed);
1255
            }
1256

    
1257
            // Update all the items with the same or a bigger display_order than the current item.
1258
            $sql_update_order = "
1259
                               UPDATE " . $tbl_lp_item . "
1260
                               SET display_order = display_order + 1
1261
                               WHERE
1262
                                   c_id = ".$course_id." AND
1263
                                   lp_id = " . $this->get_id() . " AND
1264
                                   id <> " . $id . " AND
1265
                                   parent_item_id = " . $parent . " AND
1266
                                   display_order >= " . $new_order;
1267

    
1268
            $res_update_next = Database::query($sql_update_order);
1269
        }
1270
    }
1271

    
1272
    /**
1273
     * Updates an item's prereq in place
1274
     * @param	integer	Element ID
1275
     * @param	string	Prerequisite Element ID
1276
     * @param	string	Prerequisite item type
1277
     * @param	string	Prerequisite min score
1278
     * @param	string	Prerequisite max score
1279
     * @return	boolean	True on success, false on error
1280
     */
1281
    public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100) {
1282
        $course_id = api_get_course_int_id();
1283
        if ($this->debug > 0) {
1284
            error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
1285
        }
1286

    
1287
        if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
1288
            return false;
1289
        }
1290

    
1291
        $prerequisite_id = Database::escape_string($prerequisite_id);
1292

    
1293
        $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
1294

    
1295
        if (!is_numeric($mastery_score) || $mastery_score < 0)
1296
            $mastery_score = 0;
1297

    
1298
        if (!is_numeric($max_score) || $max_score < 0)
1299
            $max_score = 100;
1300

    
1301
        if ($mastery_score > $max_score)
1302
            $max_score = $mastery_score;
1303

    
1304
        if (!is_numeric($prerequisite_id))
1305
            $prerequisite_id = 'NULL';
1306

    
1307
        $sql_upd = " UPDATE " . $tbl_lp_item . "
1308
                     SET prerequisite = " . $prerequisite_id . " WHERE c_id = ".$course_id." AND id = " . $id;
1309
        $res_upd = Database::query($sql_upd);
1310

    
1311
        if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
1312
            $sql_upd = " UPDATE " . $tbl_lp_item . " SET
1313
                         mastery_score = " . $mastery_score .
1314
                         //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
1315
                        " WHERE c_id = ".$course_id." AND ref = '" . $prerequisite_id . "'"; // Will this be enough to ensure unicity?
1316
            $res_upd = Database::query($sql_upd);
1317
        }
1318
        // TODO: Update the item object (can be ignored for now because refreshed).
1319
        return true;
1320
    }
1321

    
1322
    /**
1323
     * Escapes a string with the available database escape function
1324
     * @param	string	String to escape
1325
     * @return	string	String escaped
1326
     * @deprecated use  Database::escape_string
1327
     */
1328
    public function escape_string($string) {
1329
        //if ($this->debug > 0) { error_log('New LP - In learnpath::escape_string('.$string.')', 0); }
1330
        return Database::escape_string($string);
1331
    }
1332

    
1333
    /**
1334
     * Static admin function exporting a learnpath into a zip file
1335
     * @param	string	Export type (scorm, zip, cd)
1336
     * @param	string	Course code
1337
     * @param	integer Learnpath ID
1338
     * @param	string	Zip file name
1339
     * @return	string	Zip file path (or false on error)
1340
     */
1341
    public function export_lp($type, $course, $id, $zipname) {
1342
        $course_id = api_get_course_int_id();
1343
        //if ($this->debug > 0) { error_log('New LP - In learnpath::export_lp()', 0); }
1344
        if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
1345
            return false;
1346
        }
1347
        $url = '';
1348
        switch ($type) {
1349
            case 'scorm':
1350
                break;
1351
            case 'zip':
1352
                break;
1353
            case 'cdrom':
1354
                break;
1355
        }
1356
        return $url;
1357
    }
1358

    
1359
    /**
1360
     * Gets all the chapters belonging to the same parent as the item/chapter given
1361
     * Can also be called as abstract method
1362
     * @param	integer	Item ID
1363
     * @return	array	A list of all the "brother items" (or an empty array on failure)
1364
     */
1365
    public function get_brother_chapters($id) {
1366
        $course_id = api_get_course_int_id();
1367
        if ($this->debug > 0) {
1368
            error_log('New LP - In learnpath::get_brother_chapters()', 0);
1369
        }
1370

    
1371
        if (empty ($id) OR $id != strval(intval($id))) {
1372
            return array ();
1373
        }
1374

    
1375
        $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
1376
        $sql_parent = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
1377
        $res_parent = Database::query($sql_parent);
1378
        if (Database :: num_rows($res_parent) > 0) {
1379
            $row_parent = Database :: fetch_array($res_parent);
1380
            $parent = $row_parent['parent_item_id'];
1381
            $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent AND id = $id AND item_type='dokeos_chapter' ORDER BY display_order";
1382
            $res_bros = Database::query($sql_bros);
1383
            $list = array ();
1384
            while ($row_bro = Database :: fetch_array($res_bros)) {
1385
                $list[] = $row_bro;
1386
            }
1387
            return $list;
1388
        }
1389
        return array ();
1390
    }
1391

    
1392
    /**
1393
     * Gets all the items belonging to the same parent as the item given
1394
     * Can also be called as abstract method
1395
     * @param	integer	Item ID
1396
     * @return	array	A list of all the "brother items" (or an empty array on failure)
1397
     */
1398
    public function get_brother_items($id) {
1399
        $course_id = api_get_course_int_id();
1400
        if ($this->debug > 0) {
1401
            error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
1402
        }
1403

    
1404
        if (empty ($id) OR $id != strval(intval($id))) {
1405
            return array ();
1406
        }
1407

    
1408
        $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
1409
        $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
1410
        $res_parent = Database::query($sql_parent);
1411
        if (Database :: num_rows($res_parent) > 0) {
1412
            $row_parent = Database :: fetch_array($res_parent);
1413
            $parent = $row_parent['parent_item_id'];
1414
            $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent ORDER BY display_order";
1415
            $res_bros = Database::query($sql_bros);
1416
            $list = array ();
1417
            while ($row_bro = Database :: fetch_array($res_bros)) {
1418
                $list[] = $row_bro;
1419
            }
1420
            return $list;
1421
        }
1422
        return array ();
1423
    }
1424

    
1425
    /**
1426
     * Get the specific prefix index terms of this learning path
1427
     * @return  array Array of terms
1428
     */
1429
    public function get_common_index_terms_by_prefix($prefix) {
1430
        require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
1431
        $terms = get_specific_field_values_list_by_prefix($prefix, $this->cc, TOOL_LEARNPATH, $this->lp_id);
1432
        $prefix_terms = array();
1433
        if (!empty($terms)) {
1434
            foreach ($terms as $term) {
1435
                $prefix_terms[] = $term['value'];
1436
            }
1437
        }
1438
        return $prefix_terms;
1439
    }
1440

    
1441
    /**
1442
     * Gets the number of items currently completed
1443
     * @return integer The number of items currently completed
1444
     */
1445
    public function get_complete_items_count() {
1446
        if ($this->debug > 0) {
1447
            error_log('New LP - In learnpath::get_complete_items_count()', 0);
1448
        }
1449
        $i = 0;
1450
        foreach ($this->items as $id => $dummy) {
1451
            // Trying failed and browsed considered "progressed" as well.
1452
            if ($this->items[$id]->status_is(array (
1453
                    'completed',
1454
                    'passed',
1455
                    'succeeded',
1456
                    'browsed',
1457
                    'failed'
1458
                )) && $this->items[$id]->get_type() != 'dokeos_chapter' && $this->items[$id]->get_type() != 'dir') {
1459
                $i++;
1460
            }
1461
        }
1462
        return $i;
1463
    }
1464

    
1465
    /**
1466
     * Gets the current item ID
1467
     * @return	integer	The current learnpath item id
1468
     */
1469
    public function get_current_item_id() {
1470
        $current = 0;
1471
        if ($this->debug > 0) {
1472
            error_log('New LP - In learnpath::get_current_item_id()', 0);
1473
        }
1474
        if (!empty ($this->current)) {
1475
            $current = $this->current;
1476
        }
1477
        if ($this->debug > 2) {
1478
            error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
1479
        }
1480
        return $current;
1481
    }
1482

    
1483
    /**
1484
     * Force to get the first learnpath item id
1485
     * @return	integer	The current learnpath item id
1486
     */
1487
    public function get_first_item_id() {
1488
        $current = 0;
1489
        if (is_array($this->ordered_items)) {
1490
            $current = $this->ordered_items[0];
1491
        }
1492
        return $current;
1493
    }
1494

    
1495
    /**
1496
     * Gets the total number of items available for viewing in this SCORM
1497
     * @return	integer	The total number of items
1498
     */
1499
    public function get_total_items_count() {
1500
        if ($this->debug > 0) {
1501
            error_log('New LP - In learnpath::get_total_items_count()', 0);
1502
        }
1503
        return count($this->items);
1504
    }
1505

    
1506
    /**
1507
     * Gets the total number of items available for viewing in this SCORM but without chapters
1508
     * @return	integer	The total no-chapters number of items
1509
     */
1510
    public function get_total_items_count_without_chapters() {
1511
        if ($this->debug > 0) {
1512
            error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
1513
        }
1514
        $total = 0;
1515
        foreach ($this->items as $temp2) {
1516
            if (!in_array($temp2->get_type(), array (
1517
                    'dokeos_chapter',
1518
                    'chapter',
1519
                    'dir'
1520
                )))
1521
                $total++;
1522
        }
1523
        return $total;
1524
    }
1525

    
1526
    /**
1527
     * Gets the first element URL.
1528
     * @return	string	URL to load into the viewer
1529
     */
1530
    public function first() {        
1531
        if ($this->debug > 0) {
1532
            error_log('New LP - In learnpath::first()', 0);
1533
            error_log('$this->last_item_seen '.$this->last_item_seen);
1534
            //error_log('$this->items '.print_r($this->items, 1));
1535
            //error_log('$this->ordered_items '.print_r($this->ordered_items, 1));            
1536
        }
1537
        
1538
        // Test if the last_item_seen exists and is not a dir.
1539
        if (count($this->ordered_items) == 0) {
1540
            $this->index = 0;
1541
        }
1542
        
1543
        if ($this->debug > 0) {
1544
            if (isset($this->items[$this->last_item_seen])) {
1545
                $status = $this->items[$this->last_item_seen]->get_status();
1546
            }
1547
            error_log('status '.$status);
1548
        }
1549
        
1550
         if (!empty($this->last_item_seen) && 
1551
             !empty($this->items[$this->last_item_seen]) && 
1552
             $this->items[$this->last_item_seen]->get_type() != 'dir' &&
1553
             $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
1554
             //with this change (below) the LP will NOT go to the next item, it will take lp item we left
1555
             //&& !$this->items[$this->last_item_seen]->is_done()
1556
             ) {
1557
                
1558
            if ($this->debug > 2) {
1559
                error_log('New LP - In learnpath::first() - Last item seen is ' . $this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(), 0);
1560
            }
1561
            $index = -1;
1562
            foreach ($this->ordered_items as $myindex => $item_id) {
1563
                if ($item_id == $this->last_item_seen) {
1564
                    $index = $myindex;
1565
                    break;
1566
                }
1567
            }
1568
            if ($index == -1) {
1569
                // Index hasn't changed, so item not found - panic (this shouldn't happen).
1570
                if ($this->debug > 2) {
1571
                    error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
1572
                }
1573
                return false;
1574
            } else {
1575
                $this->last     = $this->last_item_seen;
1576
                $this->current  = $this->last_item_seen;
1577
                $this->index    = $index;
1578
            }
1579
        } else {
1580
            if ($this->debug > 2) {
1581
                error_log('New LP - In learnpath::first() - No last item seen', 0);
1582
            }
1583
            $index = 0;
1584
            // Loop through all ordered items and stop at the first item that is
1585
            // not a directory *and* that has not been completed yet.
1586
            while ( !empty($this->ordered_items[$index]) AND 
1587
                    is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND 
1588
                    (
1589
                            $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR 
1590
                            $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR 
1591
                            $this->items[$this->ordered_items[$index]]->is_done() === true
1592
                    ) AND $index < $this->max_ordered_items) {
1593
                $index++;
1594
            }
1595
            $this->last     = $this->current;
1596
            // current is
1597
            $this->current  = $this->ordered_items[$index];
1598
            $this->index    = $index;
1599
            if ($this->debug > 2) {
1600
                error_log('$index ' . $index);
1601
            }
1602
            if ($this->debug > 2) {
1603
                error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
1604
            }
1605
        }
1606
        if ($this->debug > 2) {
1607
            error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
1608
        }        
1609
    }
1610

    
1611
    /**
1612
     * Gets the information about an item in a format usable as JavaScript to update
1613
     * the JS API by just printing this content into the <head> section of the message frame
1614
     * @param	integer		Item ID
1615
     * @return	string
1616
     */
1617
    public function get_js_info($item_id = '') {
1618
        if ($this->debug > 0) {
1619
            error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
1620
        }
1621

    
1622
        $info = '';
1623
        $item_id = Database::escape_string($item_id);
1624

    
1625
        if (!empty($item_id) && is_object($this->items[$item_id])) {
1626
            //if item is defined, return values from DB
1627
            $oItem = $this->items[$item_id];
1628
            $info .= '<script language="javascript">';
1629
            $info .= "top.set_score(" . $oItem->get_score() . ");\n";
1630
            $info .= "top.set_max(" . $oItem->get_max() . ");\n";
1631
            $info .= "top.set_min(" . $oItem->get_min() . ");\n";
1632
            $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
1633
            $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
1634
            $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
1635
            $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
1636
            $info .= "top.set_flag_synchronized();";
1637
            $info .= '</script>';
1638
            if ($this->debug > 2) {
1639
                error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
1640
            }
1641
            return $info;
1642

    
1643
        } else {
1644

    
1645
            // If item_id is empty, just update to default SCORM data.
1646
            $info .= '<script language="javascript">';
1647
            $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
1648
            $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
1649
            $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
1650
            $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
1651
            $info .= "top.set_session_time('" . learnpathItem :: get_scorm_time('js') . "');";
1652
            $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
1653
            $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
1654
            $info .= "top.set_flag_synchronized();";
1655
            $info .= '</script>';
1656
            if ($this->debug > 2) {
1657
                error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
1658
            }
1659
            return $info;
1660
        }
1661
    }
1662

    
1663
    /**
1664
     * Gets the js library from the database
1665
     * @return	string	The name of the javascript library to be used
1666
     */
1667
    public function get_js_lib() {
1668
        $lib = '';
1669
        if (!empty ($this->js_lib)) {
1670
            $lib = $this->js_lib;
1671
        }
1672
        return $lib;
1673
    }
1674

    
1675
    /**
1676
     * Gets the learnpath database ID
1677
     * @return	integer	Learnpath ID in the lp table
1678
     */
1679
    public function get_id() {
1680
        //if ($this->debug > 0) { error_log('New LP - In learnpath::get_id()', 0); }
1681
        if (!empty ($this->lp_id)) {
1682
            return $this->lp_id;
1683
        } else {
1684
            return 0;
1685
        }
1686
    }
1687

    
1688
    /**
1689
     * Gets the last element URL.
1690
     * @return string URL to load into the viewer
1691
     */
1692
    public function get_last() {
1693
        if ($this->debug > 0) {
1694
            error_log('New LP - In learnpath::get_last()', 0);
1695
        }
1696
        $this->index = count($this->ordered_items) - 1;
1697
        return $this->ordered_items[$this->index];
1698
    }
1699

    
1700
    /**
1701
     * Gets the navigation bar for the learnpath display screen
1702
     * @return	string	The HTML string to use as a navigation bar
1703
     */
1704
    public function get_navigation_bar() {
1705
        if ($this->debug > 0) {
1706
            error_log('New LP - In learnpath::get_navigation_bar()', 0);
1707
        }
1708

    
1709
        // TODO: Find a good value for the following variables.
1710
        $file = '';
1711
        $openDir = '';
1712
        $edoceo = '';
1713
        $time = 0;
1714
        $navbar = '';
1715
        $RequestUri = '';
1716
        $mycurrentitemid = $this->get_current_item_id();
1717
        if ($this->mode == 'fullscreen') {
1718
            $navbar = '
1719
                  <div class="buttons">
1720
                    <a href="lp_controller.php?action=stats" onClick="window.parent.API.save_asset();return true;" target="content_name_blank" title="stats" id="stats_link"><img border="0" src="../img/lp_stats.gif" title="' . get_lang('Reporting') . '"></a>
1721
                    <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="' . get_lang('ScormPrevious') . '"></a>
1722
                    <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next"  ><img border="0" src="../img/lp_rightarrow.gif" title="' . get_lang('ScormNext') . '"></a>.
1723
                    <a href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode"><img border="0" src="../img/view_choose.gif" title="'.get_lang('ScormExitFullScreen').'"></a>
1724
                  </div>';
1725

    
1726
        } else {
1727
            $navbar = '
1728
                  <div class="buttons">
1729
                    <a href="lp_controller.php?action=stats" onClick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link"><img border="0" src="../img/lp_stats.gif" title="' . get_lang('Reporting') . '"></a>
1730
                    <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="' . get_lang('ScormPrevious') . '"></a>
1731
                    <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next"  ><img border="0" src="../img/lp_rightarrow.gif" title="' . get_lang('ScormNext') . '"></a>
1732
                  </div>';
1733
        }
1734
        return $navbar;
1735
    }
1736

    
1737
    /**
1738
     * Gets the next resource in queue (url).
1739
     * @return	string	URL to load into the viewer
1740
     */
1741
    public function get_next_index() {
1742
        if ($this->debug > 0) {
1743
            error_log('New LP - In learnpath::get_next_index()', 0);
1744
        }
1745
        // TODO
1746
        $index = $this->index;
1747
        $index++;
1748
        if ($this->debug > 2) {
1749
            error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
1750
        }
1751
        while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
1752
            $index++;
1753
            if ($index == $this->max_ordered_items){
1754
                if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
1755
                    return $this->index;
1756
                } else {
1757
                    return $index;
1758
                }
1759
            }
1760
        }
1761
        if (empty ($this->ordered_items[$index])) {
1762
            return $this->index;
1763
        }
1764
        if ($this->debug > 2) {
1765
            error_log('New LP - index is now ' . $index, 0);
1766
        }
1767
        return $index;
1768
    }
1769

    
1770
    /**
1771
     * Gets item_id for the next element
1772
     * @return	integer	Next item (DB) ID
1773
     */
1774
    public function get_next_item_id() {
1775
        if ($this->debug > 0) {
1776
            error_log('New LP - In learnpath::get_next_item_id()', 0);
1777
        }
1778
        $new_index = $this->get_next_index();
1779
        if (!empty ($new_index)) {
1780
            if (isset ($this->ordered_items[$new_index])) {
1781
                if ($this->debug > 2) {
1782
                    error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
1783
                }
1784
                return $this->ordered_items[$new_index];
1785
            }
1786
        }
1787
        if ($this->debug > 2) {
1788
            error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
1789
        }
1790
        return 0;
1791
    }
1792

    
1793
    /**
1794
     * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
1795
     *
1796
     * Generally, the package provided is in the form of a zip file, so the function
1797
     * has been written to test a zip file. If not a zip, the function will return the
1798
     * default return value: ''
1799
     * @param	string	the path to the file
1800
     * @param	string 	the original name of the file
1801
     * @return	string	'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
1802
     */
1803
    public function get_package_type($file_path, $file_name) {
1804

    
1805
        // Get name of the zip file without the extension.
1806
        $file_info = pathinfo($file_name);
1807
        $filename = $file_info['basename']; // Name including extension.
1808
        $extension = $file_info['extension']; // Extension only.
1809

    
1810
        if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array (
1811
                'dll',
1812
                'exe'
1813
            ))) {
1814
            return 'oogie';
1815
        }
1816
        if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array (
1817
                'dll',
1818
                'exe'
1819
            ))) {
1820
            return 'woogie';
1821
        }
1822

    
1823
        $file_base_name = str_replace('.' . $extension, '', $filename); // Filename without its extension.
1824

    
1825
        $zipFile = new PclZip($file_path);
1826
        // Check the zip content (real size and file extension).
1827
        $zipContentArray = $zipFile->listContent();
1828
        $package_type = '';
1829
        $at_root = false;
1830
        $manifest = '';
1831

    
1832
        // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
1833
        if (is_array($zipContentArray) && count($zipContentArray) > 0) {
1834
            foreach ($zipContentArray as $thisContent) {
1835
                if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
1836
                    // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
1837
                }
1838
                elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
1839
                    $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
1840
                    $package_type = 'scorm';
1841
                    break; // Exit the foreach loop.
1842
                }
1843
                elseif (preg_match('/aicc\//i', $thisContent['filename'])) {
1844
                    // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
1845
                    $package_type = 'aicc';
1846
                    //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
1847
                } else {
1848
                    $package_type = '';
1849
                }
1850
            }
1851
        }
1852
        return $package_type;
1853
    }
1854

    
1855
    /**
1856
     * Gets the previous resource in queue (url). Also initialises time values for this viewing
1857
     * @return string URL to load into the viewer
1858
     */
1859
    public function get_previous_index() {
1860
        if ($this->debug > 0) {
1861
            error_log('New LP - In learnpath::get_previous_index()', 0);
1862
        }
1863
        $index = $this->index;
1864
        if (isset ($this->ordered_items[$index -1])) {
1865
            $index--;
1866
            while (isset ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
1867
                $index--;
1868
                if ($index < 0) {
1869
                    return $this->index;
1870
                }
1871
            }
1872
        } else {
1873
            if ($this->debug > 2) {
1874
                error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
1875
            }
1876
            // There is no previous item.
1877
        }
1878
        return $index;
1879
    }
1880

    
1881
    /**
1882
     * Gets item_id for the next element
1883
     * @return	integer	Previous item (DB) ID
1884
     */
1885
    public function get_previous_item_id() {
1886
        if ($this->debug > 0) {
1887
            error_log('New LP - In learnpath::get_previous_item_id()', 0);
1888
        }
1889
        $new_index = $this->get_previous_index();
1890
        return $this->ordered_items[$new_index];
1891
    }
1892

    
1893
    /**
1894
     * Gets the progress value from the progress_db attribute
1895
     * @return	integer	Current progress value
1896
     */
1897
    public function get_progress() {
1898
        if ($this->debug > 0) {
1899
            error_log('New LP - In learnpath::get_progress()', 0);
1900
        }
1901
        if (!empty ($this->progress_db)) {
1902
            return $this->progress_db;
1903
        }
1904
        return 0;
1905
    }
1906

    
1907
    /**
1908
     * Gets the progress value from the progress field in the database (allows use as abstract method)
1909
     * @param	integer	Learnpath ID
1910
     * @param	integer	User ID
1911
     * @param	string	Mode of display ('%','abs' or 'both')
1912
     * @param	string	Course database name (optional, defaults to '')
1913
     * @param	boolean	Whether to return null if no record was found (true), or 0 (false) (optional, defaults to false)
1914
     * @return	integer	Current progress value as found in the database
1915
     */
1916
    public static function get_db_progress($lp_id, $user_id, $mode = '%', $course_code = '', $sincere = false,$session_id = 0) {
1917

    
1918
        //if ($this->debug > 0) { error_log('New LP - In learnpath::get_db_progress()', 0); }
1919
        $session_id = intval($session_id);
1920
        $course_info = api_get_course_info($course_code);
1921
        $session_condition = api_get_session_condition($session_id);
1922
        $course_id = $course_info['real_id'];
1923
        $table = Database :: get_course_table(TABLE_LP_VIEW);
1924
        $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
1925
        $res = Database::query($sql);
1926
        $view_id = 0;
1927
        if (Database :: num_rows($res) > 0) {
1928
            $row = Database :: fetch_array($res);
1929
            $progress = $row['progress'];
1930
            $view_id = $row['id'];
1931
        } else {
1932
            if ($sincere) {
1933
                return null;
1934
            }
1935
        }
1936

    
1937
        if (empty ($progress)) {
1938
            $progress = '0';
1939
        }
1940

    
1941
        if ($mode == '%') {
1942
            return $progress . '%';
1943
        } else {
1944
            // Get the number of items completed and the number of items total.
1945
            $tbl = Database :: get_course_table(TABLE_LP_ITEM);
1946
            $sql = "SELECT count(*) FROM $tbl
1947
            		WHERE c_id = $course_id AND c_id = ".$course_id." AND lp_id = " . $lp_id . " AND item_type NOT IN('dokeos_chapter','chapter','dir')";
1948
            $res = Database::query($sql);
1949
            $row = Database :: fetch_array($res);
1950
            $total = $row[0];
1951
            $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
1952
            $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
1953

    
1954
            //$sql = "SELECT count(distinct(lp_item_id)) FROM $tbl WHERE lp_view_id = ".$view_id." AND status IN ('passed','completed','succeeded')";
1955
            // Trying as also counting browsed and failed items.
1956
            $sql = "SELECT count(distinct(lp_item_id))
1957
                    FROM $tbl_item_view as item_view
1958
                    INNER JOIN $tbl_item as item
1959
                    ON item.id = item_view.lp_item_id
1960
                    AND item_type NOT IN('dokeos_chapter','chapter','dir')
1961
                    WHERE
1962
                    	item_view.c_id 	= $course_id AND
1963
                    	item.c_id 		= $course_id  AND
1964
                    	lp_view_id 		= " . $view_id . " AND
1965
            			status IN ('passed','completed','succeeded','browsed','failed')"; //echo '<br />';
1966
            $res = Database::query($sql);
1967
            $row = Database :: fetch_array($res);
1968
            $completed = $row[0];
1969
            if ($mode == 'abs') {
1970
                return $completed . '/' . $total;
1971
            } elseif ($mode == 'both') {
1972
                if ($progress < ($completed / ($total ? $total : 1))) {
1973
                    $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
1974
                }
1975
                return $progress . '% (' . $completed . '/' . $total . ')';
1976
            }
1977
        }
1978
        return $progress;
1979
    }
1980

    
1981
    /**
1982
     * Returns the HTML necessary to print a mediaplayer block inside a page
1983
     * @return string	The mediaplayer HTML
1984
     */
1985
    public function get_mediaplayer($autostart='true') {
1986
        $course_id = api_get_course_int_id();
1987
        global $_course;
1988
        $tbl_lp_item 		= Database :: get_course_table(TABLE_LP_ITEM);
1989
        $tbl_lp_item_view 	= Database :: get_course_table(TABLE_LP_ITEM_VIEW);
1990

    
1991
        // Getting all the information about the item.
1992
        $sql = "SELECT * FROM " . $tbl_lp_item . " as lp INNER  JOIN " . $tbl_lp_item_view . " as lp_view on lp.id = lp_view.lp_item_id " .
1993
                "WHERE  lp.id = '" . $_SESSION['oLP']->current . "' AND
1994
                        lp.c_id = $course_id AND
1995
                        lp_view.c_id = $course_id";
1996
        $result = Database::query($sql);
1997
        $row 	= Database::fetch_assoc($result);
1998
        $output = '';
1999

    
2000
        if (!empty ($row['audio'])) {
2001

    
2002
            $list = $_SESSION['oLP']->get_toc();
2003
            $type_quiz = false;
2004

    
2005
            foreach($list as $toc) {
2006
                if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
2007
                    $type_quiz = true;
2008
                }
2009
            }
2010

    
2011
            if ($type_quiz) {
2012
                if ($_SESSION['oLP']->prevent_reinit == 1) {
2013
                    $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
2014
                } else {
2015
                    $autostart_audio = $autostart;
2016
                }
2017
            } else {
2018
                $autostart_audio = 'true';
2019
            }
2020

    
2021
            // The mp3 player.
2022
            $output  = '<div id="container">';
2023
            $output .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
2024
            $output .= '<script type="text/javascript">
2025
                            var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
2026
                            s1.addParam("allowscriptaccess","always");
2027
                                s1.addParam("flashvars","file=' . api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=' . $autostart_audio.'");
2028
                            s1.write("container");
2029
						</script>
2030
                        </div>';
2031
        }
2032
        return $output;
2033
    }
2034

    
2035
    /**
2036
     * This function checks if the learnpath is visible for student after the progress of its prerequisite is completed, and considering time availability
2037
     * @param	int		Learnpath id
2038
     * @param	int		Student id
2039
     * @param   string  Course code (optional)
2040
     * @return	bool	True if
2041
     */
2042
    public static function is_lp_visible_for_student($lp_id, $student_id, $course = null) {
2043
        $lp_id = (int)$lp_id;
2044
        $course = api_get_course_info($course);
2045
        $tbl_learnpath = Database :: get_course_table(TABLE_LP_MAIN);
2046
        // Get current prerequisite
2047
        $sql = "SELECT id, prerequisite, publicated_on, expired_on FROM $tbl_learnpath WHERE c_id = ".$course['real_id']." AND id = $lp_id";
2048
        $rs  = Database::query($sql);
2049
        $now = time();
2050
        if (Database::num_rows($rs)>0) {
2051
            $row = Database::fetch_array($rs, 'ASSOC');
2052
            $prerequisite = $row['prerequisite'];
2053
            $is_visible = true;
2054
            $progress = 0;
2055

    
2056
            if (!empty($prerequisite)) {
2057
                $progress = self::get_db_progress($prerequisite,$student_id,'%', '', false, api_get_session_id());
2058
                $progress = intval($progress);
2059
                if ($progress < 100) {
2060
                    $is_visible = false;
2061
                }
2062
            }
2063

    
2064
            // Also check the time availability of the LP
2065

    
2066
            if ($is_visible) {
2067
	            //Adding visibility reestrinctions
2068
	            if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
2069
	            	if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
2070
	            		//api_not_allowed();
2071
	            		$is_visible = false;
2072
	            	}
2073
	            }
2074

    
2075
	            //Blocking empty start times see BT#2800
2076
	            global $_custom;
2077
	            if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
2078
		            if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
2079
		            	//api_not_allowed();
2080
		            	$is_visible = false;
2081
		            }
2082
	            }
2083

    
2084
	            if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
2085
	            	if ($now > api_strtotime($row['expired_on'], 'UTC')) {
2086
	            		//api_not_allowed();
2087
	            		$is_visible = false;
2088
	            	}
2089
	            }
2090
            }
2091
            return $is_visible;
2092
        }
2093
        return false;
2094
    }
2095
    /**
2096
     * Gets a progress bar for the learnpath by counting the number of items in it and the number of items
2097
     * completed so far.
2098
     * @param	string	Mode in which we want the values
2099
     * @param	integer	Progress value to display (optional but mandatory if used in abstract context)
2100
     * @param	string	Text to display near the progress value (optional but mandatory in abstract context)
2101
     * @param	boolean true if it comes from a Diplay LP view
2102
     * @return	string	HTML string containing the progress bar
2103
     */
2104
    public function get_progress_bar($mode = '', $percentage = -1, $text_add = '', $from_lp = false) {
2105
        //if ($this->debug > 0) {error_log('New LP - In learnpath::get_progress_bar('.$mode.','.$percentage.','.$text_add.','.$from_lp.')', 0); }
2106
        global $lp_theme_css;
2107

    
2108
        // Setting up the CSS path of the current style if exists.
2109
        if (!empty ($lp_theme_css)) {
2110
            $css_path = api_get_path(WEB_CODE_PATH) . 'css/' . $lp_theme_css . '/images/';
2111
        } else {
2112
            $css_path = '../img/';
2113
        }
2114

    
2115
        //if ($this->debug > 0) { error_log('New LP - In learnpath::get_progress_bar()', 0); }
2116
        if (isset($this) && is_object($this) && ($percentage == '-1' OR $text_add == '')) {
2117
            list($percentage, $text_add) = $this->get_progress_bar_text($mode);
2118
        }
2119
        $text = $percentage . $text_add;
2120
        //@todo use Display::display_progress();
2121
        $output .= '<div class="progress progress-striped">
2122
                        <div id="progress_bar_value" class="bar" style="width: '.$text.';"></div>
2123
                    </div>
2124
                    <div class="progresstext" id="progress_text">' . $text . '</div>';
2125

    
2126
        return $output;
2127
    }
2128

    
2129
    /**
2130
     * Gets the progress bar info to display inside the progress bar. Also used by scorm_api.php
2131
     * @param	string	Mode of display (can be '%' or 'abs').abs means we display a number of completed elements per total elements
2132
     * @param	integer	Additional steps to fake as completed
2133
     * @return	list	Percentage or number and symbol (% or /xx)
2134
     */
2135
    public function get_progress_bar_text($mode = '', $add = 0) {
2136
        if ($this->debug > 0) {
2137
            error_log('New LP - In learnpath::get_progress_bar_text()', 0);
2138
        }
2139
        if (empty ($mode)) {
2140
            $mode = $this->progress_bar_mode;
2141
        }
2142
        $total_items = $this->get_total_items_count_without_chapters();
2143
        if ($this->debug > 2) {
2144
            error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
2145
        }
2146
        $i = $this->get_complete_items_count();
2147
        if ($this->debug > 2) {
2148
            error_log('New LP - Items completed so far: ' . $i, 0);
2149
        }
2150
        if ($add != 0) {
2151
            $i += $add;
2152
            if ($this->debug > 2) {
2153
                error_log('New LP - Items completed so far (+modifier): ' . $i, 0);
2154
            }
2155
        }
2156
        $text = '';
2157
        if ($i > $total_items) {
2158
            $i = $total_items;
2159
        }
2160
        if ($mode == '%') {
2161
            if ($total_items > 0) {
2162
                $percentage = ((float) $i / (float) $total_items) * 100;
2163
            } else {
2164
                $percentage = 0;
2165
            }
2166
            $percentage = number_format($percentage, 0);
2167
            $text = '%';
2168
        }
2169
        elseif ($mode == 'abs') {
2170
            $percentage = $i;
2171
            $text = '/' . $total_items;
2172
        }
2173
        return array (
2174
            $percentage,
2175
            $text
2176
        );
2177
    }
2178

    
2179
    /**
2180
     * Gets the progress bar mode
2181
     * @return	string	The progress bar mode attribute
2182
     */
2183
    public function get_progress_bar_mode() {
2184
        if ($this->debug > 0) {
2185
            error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
2186
        }
2187
        if (!empty ($this->progress_bar_mode)) {
2188
            return $this->progress_bar_mode;
2189
        } else {
2190
            return '%';
2191
        }
2192
    }
2193

    
2194
    /**
2195
     * Gets the learnpath proximity (remote or local)
2196
     * @return	string	Learnpath proximity
2197
     */
2198
    public function get_proximity() {
2199
        if ($this->debug > 0) {
2200
            error_log('New LP - In learnpath::get_proximity()', 0);
2201
        }
2202
        if (!empty ($this->proximity)) {
2203
            return $this->proximity;
2204
        } else {
2205
            return '';
2206
        }
2207
    }
2208

    
2209
    /**
2210
     * Gets the learnpath theme (remote or local)
2211
     * @return	string	Learnpath theme
2212
     */
2213
    public function get_theme() {
2214
        if ($this->debug > 0) {
2215
            error_log('New LP - In learnpath::get_theme()', 0);
2216
        }
2217
        if (!empty ($this->theme)) {
2218
            return $this->theme;
2219
        } else {
2220
            return '';
2221
        }
2222
    }
2223

    
2224
    /**
2225
     * Gets the learnpath session id
2226
     * @return	string	Learnpath theme
2227
     */
2228
    public function get_lp_session_id() {
2229
        if ($this->debug > 0) {
2230
            error_log('New LP - In learnpath::get_lp_session_id()', 0);
2231
        }
2232
        if (!empty ($this->lp_session_id)) {
2233
            return $this->lp_session_id;
2234
        } else {
2235
            return 0;
2236
        }
2237
    }
2238

    
2239
    /**
2240
     * Gets the learnpath image
2241
     * @return	string	Web URL of the LP image
2242
     */
2243
    public function get_preview_image() {
2244
        if ($this->debug > 0) {
2245
            error_log('New LP - In learnpath::get_preview_image()', 0);
2246
        }
2247
        if (!empty ($this->preview_image)) {
2248
            return $this->preview_image;
2249
        } else {
2250
            return '';
2251
        }
2252
    }
2253

    
2254
    /**
2255
     * Gets the learnpath author
2256
     * @return	string	LP's author
2257
     */
2258
    public function get_author() {
2259
        if ($this->debug > 0) {
2260
            error_log('New LP - In learnpath::get_author()', 0);
2261
        }
2262
        if (!empty ($this->author)) {
2263
            return $this->author;
2264
        } else {
2265
            return '';
2266
        }
2267
    }
2268
	/**
2269
	 * Gets the learnpath author
2270
	 * @return	string	LP's author
2271
	 */
2272
	public function get_hide_toc_frame() {
2273
		if ($this->debug > 0) {
2274
			error_log('New LP - In learnpath::get_author()', 0);
2275
		}
2276
		if (!empty ($this->hide_toc_frame)) {
2277
			return $this->hide_toc_frame;
2278
		} else {
2279
			return '';
2280
		}
2281
	}
2282

    
2283
    /**
2284
     * Generate a new prerequisites string for a given item. If this item was a sco and
2285
     * its prerequisites were strings (instead of IDs), then transform those strings into
2286
     * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
2287
     * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
2288
     * same rule as the scorm_export() method
2289
     * @param	integer		Item ID
2290
     * @return	string		Prerequisites string ready for the export as SCORM
2291
     */
2292
    public function get_scorm_prereq_string($item_id) {
2293
        if ($this->debug > 0) {
2294
            error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
2295
        }
2296
        if (!is_object($this->items[$item_id])) {
2297
            return false;
2298
        }
2299
        $oItem = $this->items[$item_id];
2300
        $prereq = $oItem->get_prereq_string();
2301
        if (empty ($prereq)) {
2302
            return '';
2303
        }
2304
        if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {	// If the prerequisite is a simple integer ID and this ID exists as an item ID,
2305
                                                                                    // then simply return it (with the ITEM_ prefix).
2306
            return 'ITEM_' . $prereq;
2307
        } else {
2308
            if (isset ($this->refs_list[$prereq])) {
2309
                // It's a simple string item from which the ID can be found in the refs list,
2310
                // so we can transform it directly to an ID for export.
2311
                return 'ITEM_' . $this->refs_list[$prereq];
2312
            } else {
2313
                // The last case, if it's a complex form, then find all the IDs (SCORM strings)
2314
                // and replace them, one by one, by the internal IDs (chamilo db)
2315
                // TODO: Modify the '*' replacement to replace the multiplier in front of it
2316
                // by a space as well.
2317
                $find = array (
2318
                    '&',
2319
                    '|',
2320
                    '~',
2321
                    '=',
2322
                    '<>',
2323
                    '{',
2324
                    '}',
2325
                    '*',
2326
                    '(',
2327
                    ')'
2328
                );
2329
                $replace = array (
2330
                    ' ',
2331
                    ' ',
2332
                    ' ',
2333
                    ' ',
2334
                    ' ',
2335
                    ' ',
2336
                    ' ',
2337
                    ' ',
2338
                    ' ',
2339
                    ' '
2340
                );
2341
                $prereq_mod = str_replace($find, $replace, $prereq);
2342
                $ids = split(' ', $prereq_mod);
2343
                foreach ($ids as $id) {
2344
                    $id = trim($id);
2345
                    if (isset ($this->refs_list[$id])) {
2346
                        $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
2347
                    }
2348
                }
2349
                error_log('New LP - In learnpath::get_scorm_prereq_string(): returning modified string: ' . $prereq, 0);
2350
                return $prereq;
2351
            }
2352
        }
2353
    }
2354

    
2355
    /**
2356
     * Returns the XML DOM document's node
2357
     * @param	resource	Reference to a list of objects to search for the given ITEM_*
2358
     * @param	string		The identifier to look for
2359
     * @return	mixed		The reference to the element found with that identifier. False if not found
2360
     */
2361
    public function get_scorm_xml_node(& $children, $id) {
2362
        for ($i = 0; $i < $children->length; $i++) {
2363
            $item_temp = $children->item($i);
2364
            if ($item_temp->nodeName == 'item') {
2365
                if ($item_temp->getAttribute('identifier') == $id) {
2366
                    return $item_temp;
2367
                }
2368
            }
2369
            $subchildren = $item_temp->childNodes;
2370
            if ($subchildren->length > 0) {
2371
                $val = $this->get_scorm_xml_node($subchildren, $id);
2372
                if (is_object($val)) {
2373
                    return $val;
2374
                }
2375
            }
2376
        }
2377
        return false;
2378
    }
2379

    
2380
    /**
2381
     * Returns a usable array of stats related to the current learnpath and user
2382
     * @return array	Well-formatted array containing status for the current learnpath
2383
     */
2384
    public function get_stats() {
2385
        if ($this->debug > 0) {
2386
            error_log('New LP - In learnpath::get_stats()', 0);
2387
        }
2388
        // TODO
2389
    }
2390

    
2391
    /**
2392
     * Static method. Can be re-implemented by children. Gives an array of statistics for
2393
     * the given course (for all learnpaths and all users)
2394
     * @param	string	Course code
2395
     * @return array	Well-formatted array containing status for the course's learnpaths
2396
     */
2397
    public function get_stats_course($course) {
2398
        //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
2399
        // TODO
2400
    }
2401

    
2402
    /**
2403
     * Static method. Can be re-implemented by children. Gives an array of statistics for
2404
     * the given course and learnpath (for all users)
2405
     * @param	string	Course code
2406
     * @param	integer	Learnpath ID
2407
     * @return array	Well-formatted array containing status for the specified learnpath
2408
     */
2409
    public function get_stats_lp($course, $lp) {
2410
        //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
2411
        // TODO
2412
    }
2413

    
2414
    /**
2415
     * Static method. Can be re-implemented by children. Gives an array of statistics for
2416
     * the given course, learnpath and user.
2417
     * @param	string	Course code
2418
     * @param	integer	Learnpath ID
2419
     * @param	integer	User ID
2420
     * @return array	Well-formatted array containing status for the specified learnpath and user
2421
     */
2422
    public function get_stats_lp_user($course, $lp, $user) {
2423
        //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
2424
        // TODO
2425
    }
2426

    
2427
    /**
2428
     * Static method. Can be re-implemented by children. Gives an array of statistics for
2429
     * the given course and learnpath (for all users)
2430
     * @param	string	Course code
2431
     * @param	integer	User ID
2432
     * @return array	Well-formatted array containing status for the user's learnpaths
2433
     */
2434
    public function get_stats_user($course, $user) {
2435
        //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
2436
        // TODO
2437
    }
2438

    
2439
    /**
2440
     * Gets the status list for all LP's items
2441
     * @return	array	Array of [index] => [item ID => current status]
2442
     */
2443
    public function get_items_status_list() {
2444
        if ($this->debug > 0) {
2445
            error_log('New LP - In learnpath::get_items_status_list()', 0);
2446
        }
2447
        $list = array ();
2448
        foreach ($this->ordered_items as $item_id) {
2449
            $list[] = array (
2450
                $item_id => $this->items[$item_id]->get_status()
2451
            );
2452
        }
2453
        return $list;
2454
    }
2455

    
2456
    /**
2457
     * Return the number of interactions for the given learnpath Item View ID.
2458
     * This method can be used as static.
2459
     * @param	integer	Item View ID
2460
     * @param   integer course id
2461
     * @return	integer	Number of interactions
2462
     */
2463
    public static function get_interactions_count_from_db($lp_iv_id, $course_id) {
2464
        $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
2465
        $lp_iv_id = intval($lp_iv_id);
2466
        $course_id = intval($course_id);
2467

    
2468
        $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
2469
        $res = Database::query($sql);
2470
        $res = 0;
2471
        if (Database::num_rows($res)) {
2472
            $row = Database::fetch_array($res);
2473
            $num = $row[0];
2474
        }
2475
        return $num;
2476
    }
2477

    
2478
    /**
2479
     * Return the interactions as an array for the given lp_iv_id.
2480
     * This method can be used as static.
2481
     * @param	integer	Learnpath Item View ID
2482
     * @return	array
2483
     * @todo 	Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
2484
     */
2485
    public static function get_iv_interactions_array($lp_iv_id = 0) {
2486
        $course_id = api_get_course_int_id();
2487
        $list = array ();
2488
        $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
2489
        $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
2490
        $res = Database::query($sql);
2491
        $num = Database :: num_rows($res);
2492
        if ($num > 0) {
2493
            $list[] = array (
2494
                'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
2495
                'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
2496
                'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
2497
                'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
2498
                'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
2499
                'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
2500
                'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
2501
                'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
2502
            );
2503
            while ($row = Database :: fetch_array($res)) {
2504
                $list[] = array (
2505
                    'order_id' => ($row['order_id'] + 1),
2506
                    'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
2507
                    'type' => $row['interaction_type'],
2508
                    'time' => $row['completion_time'],
2509
                    //'correct_responses' => $row['correct_responses'],
2510
                    'correct_responses' => '', // Hide correct responses from students.
2511
                    'student_response' => $row['student_response'],
2512
                    'result' => $row['result'],
2513
                    'latency' => $row['latency']
2514
                );
2515
            }
2516
        }
2517
        return $list;
2518
    }
2519

    
2520
    /**
2521
     * Return the number of objectives for the given learnpath Item View ID.
2522
     * This method can be used as static.
2523
     * @param	integer	Item View ID
2524
     * @return	integer	Number of objectives
2525
     */
2526
    public static function get_objectives_count_from_db($lp_iv_id, $course_id) {
2527
        $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
2528
        $course_id = intval($course_id);
2529
        $lp_iv_id = intval($lp_iv_id);
2530
        $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
2531
        //@todo seems that this always returns 0
2532
        $res = Database::query($sql);
2533
        $res = 0;
2534
        if (Database::num_rows($res)) {
2535
            $row = Database :: fetch_array($res);
2536
            $num = $row[0];
2537
        }
2538
        return $num;
2539
    }
2540

    
2541
    /**
2542
     * Return the objectives as an array for the given lp_iv_id.
2543
     * This method can be used as static.
2544
     * @param	integer	Learnpath Item View ID
2545
     * @return	array
2546
     * @todo 	Translate labels
2547
     */
2548
    public function get_iv_objectives_array($lp_iv_id = 0) {
2549
        $course_id = api_get_course_int_id();
2550
        $list = array();
2551
        $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
2552
        $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
2553
        $res = Database::query($sql);
2554
        $num = Database :: num_rows($res);
2555
        if ($num > 0) {
2556
            $list[] = array (
2557
                'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
2558
                'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
2559
                'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
2560
                'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
2561
                'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
2562
                'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
2563
            );
2564
            while ($row = Database :: fetch_array($res)) {
2565
                $list[] = array (
2566
                    'order_id' => ($row['order_id'] + 1),
2567
                    'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
2568
                    'score_raw' => $row['score_raw'],
2569
                    'score_max' => $row['score_max'],
2570
                    'score_min' => $row['score_min'],
2571
                    'status' => $row['status']
2572
                );
2573
            }
2574
        }
2575
        return $list;
2576
    }
2577

    
2578
    /**
2579
     * Generate and return the table of contents for this learnpath. The (flat) table returned can be
2580
     * used by get_html_toc() to be ready to display
2581
     * @return	array	TOC as a table with 4 elements per row: title, link, status and level
2582
     */
2583
    public function get_toc() {
2584
        if ($this->debug > 0) {
2585
            error_log('New LP - In learnpath::get_toc()', 0);
2586
        }
2587
        $toc = array();
2588
        //echo "<pre>".print_r($this->items,true)."</pre>";
2589
        foreach ($this->ordered_items as $item_id) {
2590
            if ($this->debug > 2) {
2591
                error_log('New LP - learnpath::get_toc(): getting info for item ' . $item_id, 0);
2592
            }
2593
            // TODO: Change this link generation and use new function instead.
2594
            $toc[] = array (
2595
                'id'            => $item_id,
2596
                'title'         => $this->items[$item_id]->get_title(),
2597
                //'link' => get_addedresource_link_in_learnpath('document', $item_id, 1),
2598
                'status'        => $this->items[$item_id]->get_status(),
2599
                'level'         => $this->items[$item_id]->get_level(),
2600
                'type'          => $this->items[$item_id]->get_type(),
2601
                'description'   => $this->items[$item_id]->get_description(),
2602
                'path'          => $this->items[$item_id]->get_path(),
2603
            );
2604
        }
2605
        if ($this->debug > 2) {
2606
            error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
2607
        }
2608
        return $toc;
2609
    }
2610

    
2611

    
2612
    /**
2613
     * Generate and return the table of contents for this learnpath. The JS
2614
     * table returned is used inside of scorm_api.php
2615
     * @return  string  A JS array vairiable construction
2616
     */
2617
    public function get_items_details_as_js($varname = 'olms.lms_item_types') {
2618
        if ($this->debug > 0) {
2619
            error_log('New LP - In learnpath::get_items_details_as_js()', 0);
2620
        }
2621
        $toc = $varname.' = new Array();';
2622
        //echo "<pre>".print_r($this->items,true)."</pre>";
2623
        foreach ($this->ordered_items as $item_id) {            
2624
            $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
2625
        }
2626
        if ($this->debug > 2) {
2627
            error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
2628
        }
2629
        return $toc;
2630
    }
2631

    
2632
    /**
2633
     * Gets the learning path type
2634
     * @param	boolean		Return the name? If false, return the ID. Default is false.
2635
     * @return	mixed		Type ID or name, depending on the parameter
2636
     */
2637
    public function get_type($get_name = false) {
2638
        $res = false;
2639
        if ($this->debug > 0) {
2640
            error_log('New LP - In learnpath::get_type()', 0);
2641
        }
2642
        if (!empty ($this->type)) {
2643
            if ($get_name) {
2644
                // Get it from the lp_type table in main db.
2645
            } else {
2646
                $res = $this->type;
2647
            }
2648
        }
2649
        if ($this->debug > 2) {
2650
            error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
2651
        }
2652
        return $res;
2653
    }
2654

    
2655
    /**
2656
     * Gets the learning path type as static method
2657
     * @param	boolean		Return the name? If false, return the ID. Default is false.
2658
     * @return	mixed		Type ID or name, depending on the parameter
2659
     */
2660
    public static function get_type_static($lp_id = 0) {
2661
        $course_id = api_get_course_int_id();
2662
        $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
2663
        $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '" . $lp_id . "'";
2664
        $res = Database::query($sql);
2665
        if ($res === false) {
2666
            return null;
2667
        }
2668
        if (Database :: num_rows($res) <= 0) {
2669
            return null;
2670
        }
2671
        $row = Database :: fetch_array($res);
2672
        return $row['lp_type'];
2673
    }
2674

    
2675
    /**
2676
     * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
2677
     * This method can be used as abstract and is recursive
2678
     * @param	integer	Learnpath ID
2679
     * @param	integer	Parent ID of the items to look for
2680
     * @return	mixed	Ordered list of item IDs or false on error
2681
     */
2682
    public function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null) {
2683
        if (empty($course_id)) {
2684
            $course_id = api_get_course_int_id();
2685
        } else {
2686
            $course_id = intval($course_id);
2687
        }
2688
        $list = array();
2689
        if (empty ($lp)) {
2690
            return false;
2691
        }
2692
        $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
2693
        $sql = "SELECT id FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent ORDER BY display_order";
2694
        $res = Database::query($sql);
2695
        while ($row = Database :: fetch_array($res)) {
2696
            $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
2697
            $list[] = $row['id'];
2698
            foreach ($sublist as $item) {
2699
                $list[] = $item;
2700
            }
2701
        }
2702
        return $list;
2703
    }
2704

    
2705
    /**
2706
     * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
2707
     * @return	string	HTML TOC ready to display
2708
     */
2709
    public function get_html_toc($toc_list = null) {
2710
        $course_id      = api_get_course_int_id();
2711
        $course_code    = api_get_course_id();
2712
        $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
2713

    
2714
        $charset = api_get_system_encoding();
2715
        $display_action_links_with_icons = false;
2716

    
2717
        if ($this->debug > 0) {
2718
            error_log('New LP - In learnpath::get_html_toc()', 0);
2719
        }
2720
        if (empty($toc_list)) {
2721
            $toc_list = $this->get_toc();
2722
        }
2723
        //echo $this->current;
2724
        //$parent = $this->items[$this->current]->get_parent();
2725
        //if (empty($parent)) { $parent = $this->ordered_items[$this->items[$this->current]->get_previous_index()]; }
2726
        $html = '<div id="scorm_title" class="scorm_title">' . Security::remove_XSS($this->get_name()) . '</div>';
2727
        // Build, display.
2728
        if ($is_allowed_to_edit) {
2729
            $gradebook = Security :: remove_XSS($_GET['gradebook']);
2730
            if ($this->get_lp_session_id() == api_get_session_id()) {
2731
                $html .= '<div id="actions_lp" class="actions_lp">';
2732
                if ($display_action_links_with_icons) {
2733
                    $html .= '<div class = "btn-group">';
2734
                    $html .= "<a href='lp_controller.php?" . api_get_cidreq() . "&amp;action=build&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . Display :: return_icon('build_learnpath.png', get_lang('Build'),'',ICON_SIZE_MEDIUM)."</a>";
2735
                    //$html .= "<a href='lp_controller.php?" . api_get_cidreq() . "&amp;action=admin_view&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . Display :: return_icon('move_learnpath.png', get_lang('BasicOverview'),'',ICON_SIZE_MEDIUM)."</a>";
2736
                    //$html .= '<span>' . Display :: return_icon('view_remove_na.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</span><br />';
2737
                    $html .= '<a href="lp_controller.php?' . api_get_cidreq() . '">'. get_lang('ReturnToLPList') . '</a>';
2738
                    $html .= '</div>';
2739
                } else {
2740
                    $html .= '<div class="btn-group">';
2741
                    $html .= "<a class='btn' href='lp_controller.php?" . api_get_cidreq()."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Overview') . "</a>";
2742
                    $html .= "<a class='btn' href='lp_controller.php?" . api_get_cidreq()."&amp;action=add_item&amp;type=step&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Edit') . "</a>";
2743
                    //$html .= '<span><b>' . get_lang('Display') . '</b></span><br />';
2744
                    $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq()."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=" . $this->lp_id.'">'.get_lang('Settings').'</a>';
2745
                    $html .= '</div>';
2746
                }
2747
                $html .= '</div>';
2748
            }
2749

    
2750
        }
2751
        $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
2752
        require_once 'resourcelinker.inc.php';
2753

    
2754
        // Temporary variables.
2755
        $mycurrentitemid = $this->get_current_item_id();
2756
        $color_counter = 0;
2757
        $i = 0;
2758
        
2759
        foreach ($toc_list as $item) {
2760
            if ($this->debug > 2) {
2761
                //error_log('New LP - learnpath::get_html_toc(): using item ' . $item['id'], 0);
2762
            }
2763
            // TODO: Complete this.
2764
            $icon_name = array (
2765
                'not attempted' => '../img/notattempted.gif',
2766
                'incomplete'    => '../img/incomplete.png',
2767
                'failed'        => '../img/delete.png',
2768
                'completed'     => '../img/completed.png',
2769
                'passed'        => '../img/passed.png',
2770
                'succeeded'     => '../img/succeeded.png',
2771
                'browsed'       => '../img/completed.png',
2772
            );
2773

    
2774
            $style = 'scorm_item';
2775
            $scorm_color_background = 'scorm_item';
2776
            $style_item = 'scorm_item';
2777
            $current = false;
2778

    
2779
            if ($item['id'] == $this->current) {
2780
                $style = 'scorm_item_highlight';
2781
                $scorm_color_background = 'scorm_item_highlight';
2782
            } else {
2783

    
2784
                if ($color_counter % 2 == 0) {
2785
                    $scorm_color_background = 'scorm_item_1';
2786
                } else {
2787
                    $scorm_color_background = 'scorm_item_2';
2788
                }
2789
                if ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
2790
                	$scorm_color_background =' scorm_item_section ';
2791
                }
2792
            }
2793

    
2794
            if ($scorm_color_background != '') {
2795
                $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . '">';
2796
            }
2797

    
2798
            // The anchor will let us center the TOC on the currently viewed item &^D
2799
            if ($item['type'] != 'dokeos_module' && $item['type'] != 'dokeos_chapter') {
2800
                $html .= '<div class="' . $style_item . '" style="padding-left: ' . ($item['level'] * 1.5) . 'em; padding-right:' . ($item['level'] / 2) . 'em"             title="' . $item['description'] . '" >';
2801
                $html .= '<a name="atoc_' . $item['id'] . '" />';
2802
            } else {
2803
                $html .= '<div class="' . $style_item . '" style="padding-left: ' . ($item['level'] * 2) . 'em; padding-right:' . ($item['level'] * 1.5) . 'em"             title="' . $item['description'] . '" >';
2804
            }
2805
            $title = $item['title'];
2806
            if (empty ($title)) {
2807
                $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
2808
            }
2809

    
2810
            $title = Security::remove_XSS($title);
2811
            if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
2812
                //$html .= "<a href='lp_controller.php?".api_get_cidreq()."&action=content&lp_id=".$this->get_id()."&item_id=".$item['id']."' target='lp_content_frame_name'>".$title."</a>" ;
2813
                $url = $this->get_link('http', $item['id'], $toc_list);
2814
                //$html .= '<a href="'.$url.'" target="content_name" onClick="top.load_item('.$item['id'].',\''.$url.'\');">'.$title.'</a>' ;
2815
                //$html .= '<a href="" onClick="top.load_item('.$item['id'].',\''.$url.'\');return false;">'.$title.'</a>' ;
2816

    
2817
                //<img align="absbottom" width="13" height="13" src="../img/lp_document.png">&nbsp;background:#aaa;
2818
                $html .= '<a href="" onClick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
2819
            } elseif ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
2820
                $html .= "<img align='absbottom' width='13' height='13' src='../img/lp_dokeos_module.png'>&nbsp;" . stripslashes($title);
2821
            } elseif ($item['type'] == 'dir') {
2822
                $html .= stripslashes($title);
2823
            }
2824

    
2825
            /*$tbl_track_e_exercises = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
2826
            $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
2827
            $user_id = api_get_user_id();
2828

    
2829
            $sql = "SELECT path  FROM $tbl_track_e_exercises, $tbl_lp_item
2830
                    WHERE   c_id = $course_id AND
2831
                            path =   '" . $item['path'] . "' AND
2832
                            exe_user_id =  '$user_id' AND
2833
                            exe_cours_id = '$course_code' AND
2834
                            path = exe_exo_id AND
2835
                            status <> 'incomplete'";
2836
            $result = Database::query($sql);
2837
            $count = Database :: num_rows($result);*/
2838
            if ($item['type'] == 'quiz') {
2839
                if ($item['status'] == 'completed') {
2840
                    $html .= "&nbsp;<img id='toc_img_" . $item['id'] . "' src='" . $icon_name[$item['status']] . "' alt='" . substr($item['status'], 0, 1) . "' width='14'  />";
2841
                }
2842
            } else {
2843
                if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dokeos_module' && $item['type'] != 'dir') {
2844
                    $html .= "&nbsp;<img id='toc_img_" . $item['id'] . "' src='" . $icon_name[$item['status']] . "' alt='" . substr($item['status'], 0, 1) . "' width='14' />";
2845
                }
2846
            }
2847

    
2848
            $html .= "</div>";
2849

    
2850
            if ($scorm_color_background != '') {
2851
                $html .= '</div>';
2852
            }
2853

    
2854
            $color_counter++;
2855
        }
2856
        $html .= "</div>";
2857
        return $html;
2858
    }
2859

    
2860
    /**
2861
     * Gets the learnpath maker name - generally the editor's name
2862
     * @return	string	Learnpath maker name
2863
     */
2864
    public function get_maker() {
2865
        if ($this->debug > 0) {
2866
            error_log('New LP - In learnpath::get_maker()', 0);
2867
        }
2868
        if (!empty ($this->maker)) {
2869
            return $this->maker;
2870
        } else {
2871
            return '';
2872
        }
2873
    }
2874

    
2875
    /**
2876
     * Gets the user-friendly message stored in $this->message
2877
     * @return	string	Message
2878
     */
2879
    public function get_message() {
2880

    
2881
        if ($this->debug > 0) {
2882
            error_log('New LP - In learnpath::get_message()', 0);
2883
        }
2884
        return $this->message;
2885
    }
2886

    
2887
    /**
2888
     * Gets the learnpath name/title
2889
     * @return	string	Learnpath name/title
2890
     */
2891
    public function get_name() {
2892
        if ($this->debug > 0) {
2893
            error_log('New LP - In learnpath::get_name()', 0);
2894
        }
2895
        if (!empty ($this->name)) {
2896
            return $this->name;
2897
        } else {
2898
            return 'N/A';
2899
        }
2900
    }
2901

    
2902
    /**
2903
     * Gets a link to the resource from the present location, depending on item ID.
2904
     * @param	string	Type of link expected
2905
     * @param	integer	Learnpath item ID
2906
     * @return	string	Link to the lp_item resource
2907
     */
2908
    public function get_link($type = 'http', $item_id = null, $provided_toc = false) {
2909
        $course_id = api_get_course_int_id();
2910
        if ($this->debug > 0) {
2911
            error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
2912
        }
2913
        if (empty($item_id)) {
2914
            if ($this->debug > 2) {
2915
                error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
2916
            }
2917
            $item_id = $this->get_current_item_id();
2918
        }
2919

    
2920
        if (empty($item_id)) {
2921
            if ($this->debug > 2) {
2922
                error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
2923
            }
2924
            //still empty, this means there was no item_id given and we are not in an object context or
2925
            //the object property is empty, return empty link
2926
            $item_id = $this->first();
2927
            return '';
2928
        }
2929

    
2930
        $file = '';
2931
        $lp_table 			= Database::get_course_table(TABLE_LP_MAIN);
2932
        $lp_item_table 		= Database::get_course_table(TABLE_LP_ITEM);
2933
        $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
2934
        $item_id 			= Database::escape_string($item_id);
2935

    
2936
        $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
2937
        		FROM $lp_table l, $lp_item_table li
2938
        		WHERE 	l.c_id = $course_id AND
2939
        				li.c_id = $course_id AND
2940
        				li.id = $item_id AND
2941
        				li.lp_id = l.id";
2942
        if ($this->debug > 2) {
2943
            error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
2944
        }
2945
        $res = Database::query($sql);
2946
        if (Database :: num_rows($res) > 0) {
2947
            $row = Database :: fetch_array($res);
2948
            $lp_type = $row['ltype'];
2949
            $lp_path = $row['lpath'];
2950
            $lp_item_type = $row['litype'];
2951
            $lp_item_path = $row['lipath'];
2952
            $lp_item_params = $row['liparams'];
2953
            if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
2954
                list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
2955
            }
2956
            //$lp_item_params = '?'.$lp_item_params;
2957

    
2958
            //add ? if none - left commented to give freedom to scorm implementation
2959
            //if(substr($lp_item_params,0,1)!='?'){
2960
            //	$lp_item_params = '?'.$lp_item_params;
2961
            //}
2962
            $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
2963
            if ($type == 'http') {
2964
                $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
2965
            } else {
2966
                $course_path = $sys_course_path; //system path
2967
            }
2968

    
2969
            // Fixed issue BT#1272 - If the item type is a Chamilo Item (quiz, link, etc), then change the lp type to thread it as a normal Chamilo LP not a SCO.
2970
            if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
2971
                $lp_type = 1;
2972
            }
2973
            
2974
            if ($this->debug > 2) {
2975
                error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
2976
                error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
2977
            }
2978
                        
2979
            // Now go through the specific cases to get the end of the path.
2980

    
2981
            // @todo Use constants instead of int values.
2982
            switch ($lp_type) {
2983
                case 1 :
2984
                    if ($lp_item_type == 'dokeos_chapter') {
2985
                        $file = 'lp_content.php?type=dir';
2986
                    } else {
2987
                        require_once 'resourcelinker.inc.php';
2988
                                               
2989
                        $file = rl_get_resource_link_for_learnpath(api_get_course_id(), $this->get_id(), $item_id);   
2990
                        
2991
                        if ($this->debug > 0) {
2992
                            error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
2993
                        }
2994

    
2995
                        if ($lp_item_type == 'link') {
2996
                            require_once api_get_path(LIBRARY_PATH).'link.lib.php';
2997
                            if (is_youtube_link($file)) {
2998
                                $src  = get_youtube_video_id($file);
2999
                                $file = 'embed.php?type=youtube&src='.$src;
3000
                            }
3001
                        } else {
3002
                            // check how much attempts of a exercise exits in lp
3003
                            $lp_item_id = $this->get_current_item_id();
3004
                            $lp_view_id = $this->get_view_id();
3005
                            $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
3006
                            
3007
                            if (empty($provided_toc)) {                                
3008
                                if ($this->debug > 0) {
3009
                                    error_log('In learnpath::get_link() Loading get_toc ', 0);
3010
                                }
3011
                                $list = $this->get_toc();
3012
                            } else {
3013
                                if ($this->debug > 0) {
3014
                                    error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
3015
                                }
3016
                                $list = $provided_toc;
3017
                            }
3018
                            
3019
                            $type_quiz = false;
3020

    
3021
                            foreach ($list as $toc) {
3022
                                if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
3023
                                    $type_quiz = true;
3024
                                }
3025
                            }
3026

    
3027
                            if ($type_quiz) {
3028
                                $lp_item_id = Database::escape_string($lp_item_id);
3029
                                $lp_view_id = Database::escape_string($lp_view_id);
3030
                                $sql = "SELECT count(*) FROM $lp_item_view_table
3031
                                        WHERE c_id = $course_id AND lp_item_id='" . (int) $lp_item_id . "' AND lp_view_id ='" . (int) $lp_view_id . "' AND status='completed'";
3032
                                $result = Database::query($sql);
3033
                                $row_count = Database :: fetch_row($result);
3034
                                $count_item_view = (int) $row_count[0];
3035
                                $not_multiple_attempt = 0;
3036
                                if ($prevent_reinit === 1 && $count_item_view > 0) {
3037
                                    $not_multiple_attempt = 1;
3038
                                }
3039
                                $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
3040
                            }
3041

    
3042
                            $tmp_array = explode('/', $file);
3043
                            $document_name = $tmp_array[count($tmp_array) - 1];
3044
                            if (strpos($document_name, '_DELETED_')) {
3045
                                $file = 'blank.php?error=document_deleted';
3046
                            }
3047
                        }
3048
                    }
3049
                    break;
3050
                case 2 :
3051
                    if ($this->debug > 2) {
3052
                        error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
3053
                    }
3054

    
3055
                    if ($lp_item_type != 'dir') {
3056
                        // Quite complex here:
3057
                        // We want to make sure 'http://' (and similar) links can
3058
                        // be loaded as is (withouth the Chamilo path in front) but
3059
                        // some contents use this form: resource.htm?resource=http://blablabla
3060
                        // which means we have to find a protocol at the path's start, otherwise
3061
                        // it should not be considered as an external URL.
3062

    
3063
                        //if ($this->prerequisites_match($item_id)) {
3064
                        if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
3065
                            if ($this->debug > 2) {
3066
                                error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
3067
                            }
3068
                            // Distant url, return as is.
3069
                            $file = $lp_item_path;
3070
                        } else {
3071
                            if ($this->debug > 2) {
3072
                                error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
3073
                            }
3074
                            // Prevent getting untranslatable urls.
3075
                            $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
3076
                            $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
3077
                            // Prepare the path.
3078
                            $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
3079
                            // TODO: Fix this for urls with protocol header.
3080
                            $file = str_replace('//', '/', $file);
3081
                            $file = str_replace(':/', '://', $file);
3082
                            if (substr($lp_path, -1) == '/') {
3083
                                $lp_path = substr($lp_path, 0, -1);
3084
                            }
3085

    
3086
                            if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
3087
                                // if file not found.
3088
                                $decoded = html_entity_decode($lp_item_path);
3089
                                list ($decoded) = explode('?', $decoded);
3090
                                if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
3091
                                    require_once 'resourcelinker.inc.php';
3092
                                    $file = rl_get_resource_link_for_learnpath(api_get_course_id(), $this->get_id(), $item_id);
3093
                                    if (empty($file)) {
3094
                                        $file = 'blank.php?error=document_not_found';
3095
                                    } else {
3096
                                        $tmp_array = explode('/', $file);
3097
                                        $document_name = $tmp_array[count($tmp_array) - 1];
3098
                                        if (strpos($document_name, '_DELETED_')) {
3099
                                            $file = 'blank.php?error=document_deleted';
3100
                                        } else {
3101
                                            $file = 'blank.php?error=document_not_found';
3102
                                        }
3103
                                    }
3104
                                } else {
3105
                                    $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
3106
                                }
3107
                            }
3108
                        }
3109
                        //}else{
3110
                        //prerequisites did not match
3111
                        //$file = 'blank.php';
3112
                        //}
3113
                        // We want to use parameters if they were defined in the imsmanifest                        
3114
                        if (strpos($file, 'blank.php') === false) {
3115
                            $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
3116
                        }
3117
                    } else {
3118
                        $file = 'lp_content.php?type=dir';
3119
                    }
3120
                    break;
3121
                case 3 :
3122
                    if ($this->debug > 2) {
3123
                        error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
3124
                    }
3125
                    // Formatting AICC HACP append URL.
3126
                    $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
3127
                    if ($lp_item_type != 'dir') {
3128
                        // Quite complex here:
3129
                        // We want to make sure 'http://' (and similar) links can
3130
                        // be loaded as is (withouth the Chamilo path in front) but
3131
                        // some contents use this form: resource.htm?resource=http://blablabla
3132
                        // which means we have to find a protocol at the path's start, otherwise
3133
                        // it should not be considered as an external URL.
3134

    
3135
                        if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
3136
                            if ($this->debug > 2) {
3137
                                error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
3138
                            }
3139
                            // Distant url, return as is.
3140
                            $file = $lp_item_path;
3141
                            // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
3142
                            /*
3143
                            if (stristr($file,'<servername>') !== false) {
3144
                                $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
3145
                            }
3146
                            */
3147
                            if (stripos($file, '<servername>') !== false) {
3148
                                //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
3149
                                $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
3150
                                $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
3151
                            }
3152
                            //
3153
                            $file .= $aicc_append;
3154
                        } else {
3155
                            if ($this->debug > 2) {
3156
                                error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
3157
                            }
3158
                            // Prevent getting untranslatable urls.
3159
                            $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
3160
                            $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
3161
                            // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
3162
                            $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
3163
                            // TODO: Fix this for urls with protocol header.
3164
                            $file = str_replace('//', '/', $file);
3165
                            $file = str_replace(':/', '://', $file);
3166
                            $file .= $aicc_append;
3167
                        }
3168
                    } else {
3169
                        $file = 'lp_content.php?type=dir';
3170
                    }
3171
                    break;
3172
                case 4 :
3173
                    break;
3174
                default :
3175
                    break;
3176
            }
3177
        }
3178
        if ($this->debug > 2) {
3179
            error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
3180
        }
3181
        return $file;
3182
    }
3183

    
3184
    /**
3185
     * Gets the latest usable view or generate a new one
3186
     * @param	integer	Optional attempt number. If none given, takes the highest from the lp_view table
3187
     * @return	integer	DB lp_view id
3188
     */
3189
    public function get_view($attempt_num = 0) {
3190
        if ($this->debug > 0) {
3191
            error_log('New LP - In learnpath::get_view()', 0);
3192
        }
3193
        $search = '';
3194
        // Use $attempt_num to enable multi-views management (disabled so far).
3195
        if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
3196
            $search = 'AND view_count = ' . $attempt_num;
3197
        }
3198
        // When missing $attempt_num, search for a unique lp_view record for this lp and user.
3199
        $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
3200

    
3201
        $course_id = api_get_course_int_id();
3202

    
3203
        $sql = "SELECT id, view_count FROM $lp_view_table
3204
        		WHERE c_id = ".$course_id." AND lp_id = " . $this->get_id() ." AND user_id = " . $this->get_user_id() . " " .$search .
3205
        		" ORDER BY view_count DESC";
3206
        $res = Database::query($sql);
3207
        if (Database :: num_rows($res) > 0) {
3208
            $row = Database :: fetch_array($res);
3209
            $this->lp_view_id = $row['id'];
3210
        } else {
3211
            // There is no database record, create one.
3212
            $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id,view_count) VALUES
3213
            		($course_id, " . $this->get_id() . "," . $this->get_user_id() . ",1)";
3214
            $res = Database::query($sql);
3215
            $id = Database :: insert_id();
3216
            $this->lp_view_id = $id;
3217
        }
3218
        return $this->lp_view_id;
3219
    }
3220

    
3221
    /**
3222
     * Gets the current view id
3223
     * @return	integer	View ID (from lp_view)
3224
     */
3225
    public function get_view_id() {
3226
        if ($this->debug > 0) {
3227
            error_log('New LP - In learnpath::get_view_id()', 0);
3228
        }
3229
        if (!empty ($this->lp_view_id)) {
3230
            return $this->lp_view_id;
3231
        } else {
3232
            return 0;
3233
        }
3234
    }
3235

    
3236
    /**
3237
     * Gets the update queue
3238
     * @return	array	Array containing IDs of items to be updated by JavaScript
3239
     */
3240
    public function get_update_queue() {
3241
        if ($this->debug > 0) {
3242
            error_log('New LP - In learnpath::get_update_queue()', 0);
3243
        }
3244
        return $this->update_queue;
3245
    }
3246

    
3247
    /**
3248
     * Gets the user ID
3249
     * @return	integer	User ID
3250
     */
3251
    public function get_user_id() {
3252
        if ($this->debug > 0) {
3253
            error_log('New LP - In learnpath::get_user_id()', 0);
3254
        }
3255
        if (!empty ($this->user_id)) {
3256
            return $this->user_id;
3257
        } else {
3258
            return false;
3259
        }
3260
    }
3261

    
3262
    /**
3263
     * Checks if any of the items has an audio element attached
3264
     * @return  bool    True or false
3265
     */
3266
    public function has_audio() {
3267
        if ($this->debug > 1) {
3268
            error_log('New LP - In learnpath::has_audio()', 0);
3269
        }
3270
        $has = false;
3271
        foreach ($this->items as $i => $item) {
3272
            if (!empty ($this->items[$i]->audio)) {
3273
                $has = true;
3274
                break;
3275
            }
3276
        }
3277
        return $has;
3278
    }
3279

    
3280
    /**
3281
     * Logs a message into a file
3282
     * @param	string 	Message to log
3283
     * @return	boolean	True on success, false on error or if msg empty
3284
     */
3285
    public function log($msg) {
3286
        if ($this->debug > 0) {
3287
            error_log('New LP - In learnpath::log()', 0);
3288
        }
3289
        // TODO
3290
        $this->error .= $msg;
3291
        return true;
3292
    }
3293

    
3294
    /**
3295
     * Moves an item up and down at its level
3296
     * @param	integer	Item to move up and down
3297
     * @param	string	Direction 'up' or 'down'
3298
     * @return	integer	New display order, or false on error
3299
     */
3300
    public function move_item($id, $direction) {
3301
        $course_id = api_get_course_int_id();
3302
        if ($this->debug > 0) {
3303
            error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
3304
        }
3305
        if (empty ($id) or empty ($direction)) {
3306
            return false;
3307
        }
3308
        $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
3309
        $sql_sel = "SELECT *
3310
                    FROM " . $tbl_lp_item . "
3311
                    WHERE c_id = ".$course_id." AND id = " . $id;
3312
        $res_sel = Database::query($sql_sel);
3313
        // Check if elem exists.
3314
        if (Database :: num_rows($res_sel) < 1) {
3315
            return false;
3316
        }
3317
        // Gather data.
3318
        $row = Database :: fetch_array($res_sel);
3319
        $previous = $row['previous_item_id'];
3320
        $next = $row['next_item_id'];
3321
        $display = $row['display_order'];
3322
        $parent = $row['parent_item_id'];
3323
        $lp = $row['lp_id'];
3324
        // Update the item (switch with previous/next one).
3325
        switch ($direction) {
3326
            case 'up' :
3327
                if ($this->debug > 2) {
3328
                    error_log('Movement up detected', 0);
3329
                }
3330
                if ($display <= 1) { /*do nothing*/
3331
                } else {
3332
                    $sql_sel2 = "SELECT * FROM $tbl_lp_item
3333
                                 WHERE c_id = ".$course_id." AND id = $previous";
3334

    
3335
                    if ($this->debug > 2) {
3336
                        error_log('Selecting previous: ' . $sql_sel2, 0);
3337
                    }
3338
                    $res_sel2 = Database::query($sql_sel2);
3339
                    if (Database :: num_rows($res_sel2) < 1) {
3340
                        $previous_previous = 0;
3341
                    }
3342
                    // Gather data.
3343
                    $row2 = Database :: fetch_array($res_sel2);
3344
                    $previous_previous = $row2['previous_item_id'];
3345
                    // Update previous_previous item (switch "next" with current).
3346
                    if ($previous_previous != 0) {
3347
                        $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $id WHERE c_id = ".$course_id." AND id = $previous_previous";
3348
                        if ($this->debug > 2) {
3349
                            error_log($sql_upd2, 0);
3350
                        }
3351
                        $res_upd2 = Database::query($sql_upd2);
3352
                    }
3353
                    // Update previous item (switch with current).
3354
                    if ($previous != 0) {
3355
                        $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next, previous_item_id = $id, display_order = display_order +1
3356
                                    WHERE c_id = ".$course_id." AND id = $previous";
3357
                        if ($this->debug > 2) {
3358
                            error_log($sql_upd2, 0);
3359
                        }
3360
                        $res_upd2 = Database::query($sql_upd2);
3361
                    }
3362

    
3363
                    // Update current item (switch with previous).
3364
                    if ($id != 0) {
3365
                        $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $previous, previous_item_id = $previous_previous, display_order = display_order-1
3366
                                    WHERE c_id = ".$course_id." AND id = $id";
3367
                        if ($this->debug > 2) {
3368
                            error_log($sql_upd2, 0);
3369
                        }
3370
                        $res_upd2 = Database::query($sql_upd2);
3371
                    }
3372
                    // Update next item (new previous item).
3373
                    if ($next != 0) {
3374
                        $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
3375
                                     WHERE c_id = ".$course_id." AND id = $next";
3376
                        if ($this->debug > 2) {
3377
                            error_log($sql_upd2, 0);
3378
                        }
3379
                        $res_upd2 = Database::query($sql_upd2);
3380
                    }
3381
                    $display = $display -1;
3382
                }
3383
                break;
3384

    
3385
            case 'down' :
3386
                if ($this->debug > 2) {
3387
                    error_log('Movement down detected', 0);
3388
                }
3389
                if ($next == 0) { /* Do nothing. */
3390
                } else {
3391
                    $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
3392
                    if ($this->debug > 2) {
3393
                        error_log('Selecting next: ' . $sql_sel2, 0);
3394
                    }
3395
                    $res_sel2 = Database::query($sql_sel2);
3396
                    if (Database :: num_rows($res_sel2) < 1) {
3397
                        $next_next = 0;
3398
                    }
3399
                    // Gather data.
3400
                    $row2 = Database :: fetch_array($res_sel2);
3401
                    $next_next = $row2['next_item_id'];
3402
                    // Update previous item (switch with current).
3403
                    if ($previous != 0) {
3404
                        $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
3405
                                     WHERE c_id = ".$course_id." AND id = $previous";
3406
                        $res_upd2 = Database::query($sql_upd2);
3407
                    }
3408
                    // Update current item (switch with previous).
3409
                    if ($id != 0) {
3410
                        $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
3411
                                     WHERE c_id = ".$course_id." AND id = $id";
3412
                        $res_upd2 = Database::query($sql_upd2);
3413
                    }
3414

    
3415
                    // Update next item (new previous item).
3416
                    if ($next != 0) {
3417
                        $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
3418
                                     WHERE c_id = ".$course_id." AND id = $next";
3419
                        $res_upd2 = Database::query($sql_upd2);
3420
                    }
3421

    
3422
                    // Update next_next item (switch "previous" with current).
3423
                    if ($next_next != 0) {
3424
                        $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $id
3425
                                     WHERE c_id = ".$course_id." AND id = $next_next";
3426
                        $res_upd2 = Database::query($sql_upd2);
3427
                    }
3428
                    $display = $display +1;
3429
                }
3430
                break;
3431
            default :
3432
                return false;
3433
        }
3434
        return $display;
3435
    }
3436

    
3437
    /**
3438
     * Move a learnpath up (display_order)
3439
     * @param	integer	Learnpath ID
3440
     */
3441
    public function move_up($lp_id) {
3442
        $course_id = api_get_course_int_id();
3443
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
3444
        $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
3445
        $res = Database::query($sql);
3446
        if ($res === false)
3447
            return false;
3448
        $lps = array ();
3449
        $lp_order = array ();
3450
        $num = Database :: num_rows($res);
3451
        // First check the order is correct, globally (might be wrong because
3452
        // of versions < 1.8.4)
3453
        if ($num > 0) {
3454
            $i = 1;
3455
            while ($row = Database :: fetch_array($res)) {
3456
                if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
3457
                    $need_fix = true;
3458
                    $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = " . $row['id'];
3459
                    $res_u = Database::query($sql_u);
3460
                }
3461
                $row['display_order'] = $i;
3462
                $lps[$row['id']] = $row;
3463
                $lp_order[$i] = $row['id'];
3464
                $i++;
3465
            }
3466
        }
3467
        if ($num > 1) { // If there's only one element, no need to sort.
3468
            $order = $lps[$lp_id]['display_order'];
3469
            if ($order > 1) { // If it's the first element, no need to move up.
3470
                $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
3471
                $res_u1 = Database::query($sql_u1);
3472
                $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . " WHERE c_id = ".$course_id." AND id = " . $lp_id;
3473
                $res_u2 = Database::query($sql_u2);
3474
            }
3475
        }
3476
    }
3477

    
3478
    /**
3479
     * Move a learnpath down (display_order)
3480
     * @param	integer	Learnpath ID
3481
     */
3482
    public function move_down($lp_id) {
3483
        $course_id = api_get_course_int_id();
3484
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
3485
        $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
3486
        $res = Database::query($sql);
3487
        if ($res === false)
3488
            return false;
3489
        $lps = array ();
3490
        $lp_order = array ();
3491
        $num = Database :: num_rows($res);
3492
        $max = 0;
3493
        // First check the order is correct, globally (might be wrong because
3494
        // of versions < 1.8.4).
3495
        if ($num > 0) {
3496
            $i = 1;
3497
            while ($row = Database :: fetch_array($res)) {
3498
                $max = $i;
3499
                if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
3500
                    $need_fix = true;
3501
                    $sql_u = "UPDATE $lp_table SET display_order = $i
3502
                              WHERE c_id = ".$course_id." AND id = " . $row['id'];
3503
                    $res_u = Database::query($sql_u);
3504
                }
3505
                $row['display_order'] = $i;
3506
                $lps[$row['id']] = $row;
3507
                $lp_order[$i] = $row['id'];
3508
                $i++;
3509
            }
3510
        }
3511
        if ($num > 1) { // If there's only one element, no need to sort.
3512
            $order = $lps[$lp_id]['display_order'];
3513
            if ($order < $max) { // If it's the first element, no need to move up.
3514
                $sql_u1 = "UPDATE $lp_table SET display_order = $order
3515
                           WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
3516
                $res_u1 = Database::query($sql_u1);
3517
                $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
3518
                           WHERE c_id = ".$course_id." AND id = " . $lp_id;
3519
                $res_u2 = Database::query($sql_u2);
3520
            }
3521
        }
3522
    }
3523

    
3524
    /**
3525
     * Updates learnpath attributes to point to the next element
3526
     * The last part is similar to set_current_item but processing the other way around
3527
     */
3528
    public function next() {
3529
        if ($this->debug > 0) {
3530
            error_log('New LP - In learnpath::next()', 0);
3531
        }
3532
        $this->last = $this->get_current_item_id();
3533
        $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
3534
        $this->autocomplete_parents($this->last);
3535
        $new_index = $this->get_next_index();
3536
        if ($this->debug > 2) {
3537
            error_log('New LP - New index: ' . $new_index, 0);
3538
        }
3539
        $this->index = $new_index;
3540
        if ($this->debug > 2) {
3541
            error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
3542
        }
3543
        $this->current = $this->ordered_items[$new_index];
3544
        if ($this->debug > 2) {
3545
            error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
3546
        }
3547
    }
3548

    
3549
    /**
3550
     * Open a resource = initialise all local variables relative to this resource. Depending on the child
3551
     * class, this might be redefined to allow several behaviours depending on the document type.
3552
     * @param integer Resource ID
3553
     * @return boolean True on success, false otherwise
3554
     */
3555
    public function open($id) {
3556
        if ($this->debug > 0) {
3557
            error_log('New LP - In learnpath::open()', 0);
3558
        }
3559
        // TODO:
3560
        // set the current resource attribute to this resource
3561
        // switch on element type (redefine in child class?)
3562
        // set status for this item to "opened"
3563
        // start timer
3564
        // initialise score
3565
        $this->index = 0; //or = the last item seen (see $this->last)
3566
    }
3567

    
3568
    /**
3569
     * Check that all prerequisites are fulfilled. Returns true and an empty string on succes, returns false
3570
     * and the prerequisite string on error.
3571
     * This function is based on the rules for aicc_script language as described in the SCORM 1.2 CAM documentation page 108.
3572
     * @param	integer	Optional item ID. If none given, uses the current open item.
3573
     * @return	boolean	True if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites string otherwise.
3574
     */
3575
    public function prerequisites_match($item = null) {
3576
        if ($this->debug > 0) {
3577
            error_log('New LP - In learnpath::prerequisites_match()', 0);
3578
        }
3579
        if (empty ($item)) {
3580
            $item = $this->current;
3581
        }
3582
        if (is_object($this->items[$item])) {
3583
            $prereq_string = $this->items[$item]->get_prereq_string();
3584
            if (empty ($prereq_string)) {
3585
                return true;
3586
            }
3587
            // Clean spaces.
3588
            $prereq_string = str_replace(' ', '', $prereq_string);
3589
            if ($this->debug > 0) {
3590
                error_log('Found prereq_string: ' . $prereq_string, 0);
3591
            }
3592
            // Now send to the parse_prereq() function that will check this component's prerequisites.
3593
            $result = $this->items[$item]->parse_prereq($prereq_string, $this->items, $this->refs_list, $this->get_user_id());
3594

    
3595
            if ($result === false) {
3596
                $this->set_error_msg($this->items[$item]->prereq_alert);
3597
            }
3598
        } else {
3599
            $result = true;
3600
            if ($this->debug > 1) {
3601
                error_log('New LP - $this->items[' . $item . '] was not an object', 0);
3602
            }
3603
        }
3604

    
3605
        if ($this->debug > 1) {
3606
            error_log('New LP - End of prerequisites_match(). Error message is now ' . $this->error, 0);
3607
        }
3608
        return $result;
3609
    }
3610

    
3611
    /**
3612
     * Updates learnpath attributes to point to the previous element
3613
     * The last part is similar to set_current_item but processing the other way around
3614
     */
3615
    public function previous() {
3616
        if ($this->debug > 0) {
3617
            error_log('New LP - In learnpath::previous()', 0);
3618
        }
3619
        $this->last = $this->get_current_item_id();
3620
        $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
3621
        $this->autocomplete_parents($this->last);
3622
        $new_index = $this->get_previous_index();
3623
        $this->index = $new_index;
3624
        $this->current = $this->ordered_items[$new_index];
3625
    }
3626

    
3627
    /**
3628
     * Publishes a learnpath. This basically means show or hide the learnpath
3629
     * to normal users.
3630
     * Can be used as abstract
3631
     * @param	integer	Learnpath ID
3632
     * @param	string	New visibility
3633
     */
3634
    public function toggle_visibility($lp_id, $set_visibility = 1) {
3635
        //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_visibility()', 0); }
3636
        $action = 'visible';
3637
        if ($set_visibility != 1) {
3638
            $action = 'invisible';
3639
        }
3640
        return api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $lp_id, $action, api_get_user_id());
3641
    }
3642

    
3643
    /**
3644
     * Publishes a learnpath. This basically means show or hide the learnpath
3645
     * on the course homepage
3646
     * Can be used as abstract
3647
     * @param	integer	Learnpath id
3648
     * @param	string	New visibility (v/s - visible/invisible)
3649
     */
3650
    public static function toggle_publish($lp_id, $set_visibility = 'v') {
3651
        //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_publish()', 0); }
3652
        $course_id = api_get_course_int_id();
3653
        $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
3654
        $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
3655
        $result = Database::query($sql);
3656
        $row = Database :: fetch_array($result);
3657
        $name = domesticate($row['name']);
3658
        if ($set_visibility == 'i') {
3659
            $s = $name . " " . get_lang('_no_published');
3660
            $dialogBox = $s;
3661
            $v = 0;
3662
        }
3663
        if ($set_visibility == 'v') {
3664
            $s = $name . " " . get_lang('_published');
3665
            $dialogBox = $s;
3666
            $v = 1;
3667
        }
3668

    
3669
        $session_id = api_get_session_id();
3670
        $session_condition = api_get_session_condition($session_id);
3671

    
3672
        $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
3673

    
3674
        $course_id = api_get_course_int_id();
3675

    
3676
        $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
3677
        $sql = "SELECT * FROM $tbl_tool WHERE c_id = ".$course_id." AND name='$name' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition";
3678
        $result = Database::query($sql);
3679
        $num = Database :: num_rows($result);
3680
        $row2 = Database :: fetch_array($result);
3681
        //if ($this->debug > 2) { error_log('New LP - '.$sql.' - '.$num, 0); }
3682
        if (($set_visibility == 'i') && ($num > 0)) {
3683
            $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (name='$name' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition)";
3684
        } elseif (($set_visibility == 'v') && ($num == 0)) {
3685
            $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
3686
            	    ($course_id, '$name','$link','scormbuilder.gif','$v','0','pastillegris.gif',0, $session_id)";
3687
        } else {
3688
            // Parameter and database incompatible, do nothing.
3689
        }
3690
        $result = Database::query($sql);
3691
        //if ($this->debug > 2) { error_log('New LP - Leaving learnpath::toggle_visibility: '.$sql, 0); }
3692
    }
3693

    
3694
    /**
3695
     * Restart the whole learnpath. Return the URL of the first element.
3696
     * Make sure the results are saved with anoter method. This method should probably be
3697
     * redefined in children classes.
3698
     * To use a similar method  statically, use the create_new_attempt() method
3699
     * @return string URL to load in the viewer
3700
     */
3701
    public function restart() {
3702
        if ($this->debug > 0) {
3703
            error_log('New LP - In learnpath::restart()', 0);
3704
        }
3705
        // TODO
3706
        // Call autosave method to save the current progress.
3707
        //$this->index = 0;
3708
        $session_id = api_get_session_id();
3709
        $course_id = api_get_course_int_id();
3710
        $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
3711
        $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) " .
3712
        	   "VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
3713
        if ($this->debug > 2) {
3714
            error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
3715
        }
3716
        $res = Database::query($sql);
3717
        if ($view_id = Database :: insert_id($res)) {
3718
            $this->lp_view_id = $view_id;
3719
            $this->attempt = $this->attempt + 1;
3720
        } else {
3721
            $this->error = 'Could not insert into item_view table...';
3722
            return false;
3723
        }
3724
        $this->autocomplete_parents($this->current);
3725
        foreach ($this->items as $index => $dummy) {
3726
            $this->items[$index]->restart();
3727
            $this->items[$index]->set_lp_view($this->lp_view_id);
3728
        }
3729
        $this->first();
3730
        return true;
3731
    }
3732

    
3733
    /**
3734
     * Saves the current item
3735
     * @return	boolean
3736
     */
3737
    public function save_current() {
3738
        if ($this->debug > 0) {
3739
            error_log('New LP - In learnpath::save_current()', 0);
3740
        }
3741
        // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
3742
        // on $ordered_items[] but not sure it's always safe to use with $items[]).
3743
        if ($this->debug > 2) {
3744
            error_log('New LP - save_current() saving item ' . $this->current, 0);
3745
        }
3746
        if ($this->debug > 2) {
3747
            error_log('' . print_r($this->items, true), 0);
3748
        }
3749
        if (is_object($this->items[$this->current])) {
3750
            //$res = $this->items[$this->current]->save(false);
3751
            $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
3752
            $this->autocomplete_parents($this->current);
3753
            $status = $this->items[$this->current]->get_status();
3754
            $this->append_message('new_item_status: ' . $status);
3755
            $this->update_queue[$this->current] = $status;
3756
            return $res;
3757
        }
3758
        return false;
3759
    }
3760

    
3761
    /**
3762
     * Saves the given item
3763
     * @param	integer	Item ID. Optional (will take from $_REQUEST if null)
3764
     * @param	boolean	Save from url params (true) or from current attributes (false). Optional. Defaults to true
3765
     * @return	boolean
3766
     */
3767
    public function save_item($item_id = null, $from_outside = true) {
3768
        $course_id = api_get_course_int_id();
3769
        if ($this->debug > 0) {
3770
            error_log('New LP - In learnpath::save_item(' . $item_id . ',' . $from_outside . ')', 0);
3771
        }
3772
        // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
3773
        // on $ordered_items[] but not sure it's always safe to use with $items[]).
3774
        if (empty ($item_id)) {
3775
            $item_id = Database::escape_string($_REQUEST['id']);
3776
        }
3777
        if (empty ($item_id)) {
3778
            $item_id = $this->get_current_item_id();
3779
        }
3780
        if ($this->debug > 2) {
3781
            error_log('New LP - save_current() saving item ' . $item_id, 0);
3782
        }
3783
        if (is_object($this->items[$item_id])) {
3784
            $res = $this->items[$item_id]->save($from_outside, $this->prerequisites_match($item_id));
3785
            //$res = $this->items[$item_id]->save($from_outside);
3786
            $this->autocomplete_parents($item_id);
3787
            $status = $this->items[$item_id]->get_status();
3788
            $this->append_message('new_item_status: ' . $status);
3789
            $this->update_queue[$item_id] = $status;
3790
            return $res;
3791
        }
3792
        return false;
3793
    }
3794

    
3795
    /**
3796
     * Saves the last item seen's ID only in case
3797
     */
3798
    public function save_last() {
3799
        $course_id = api_get_course_int_id();
3800
        if ($this->debug > 0) {
3801
            error_log('New LP - In learnpath::save_last()', 0);
3802
        }
3803
        $session_condition = api_get_session_condition(api_get_session_id(), true, false);
3804
        $table = Database :: get_course_table(TABLE_LP_VIEW);
3805
        if (isset ($this->current)) {
3806
            if ($this->debug > 2) {
3807
                error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
3808
            }
3809
            $sql = "UPDATE $table SET last_item = " . Database::escape_string($this->get_current_item_id()). " " .
3810
                    "WHERE c_id = ".$course_id." AND lp_id = " . $this->get_id() . " AND user_id = " . $this->get_user_id().' '.$session_condition;
3811

    
3812
            if ($this->debug > 2) {
3813
                error_log('New LP - Saving last item seen : ' . $sql, 0);
3814
            }
3815
            $res = Database::query($sql);
3816
        }
3817

    
3818
        // Save progress.
3819
        list($progress, $text) = $this->get_progress_bar_text('%');
3820
        if ($progress >= 0 && $progress <= 100) {
3821
            $progress = (int) $progress;
3822
            $sql = "UPDATE $table SET progress = $progress " .
3823
                    "WHERE c_id = ".$course_id." AND lp_id = " . $this->get_id() . " AND " .
3824
                            "user_id = " . $this->get_user_id().' '.$session_condition;
3825
            $res = Database::query($sql); // Ignore errors as some tables might not have the progress field just yet.
3826
            $this->progress_db = $progress;
3827
        }
3828
    }
3829

    
3830
    /**
3831
     * Sets the current item ID (checks if valid and authorized first)
3832
     * @param	integer	New item ID. If not given or not authorized, defaults to current
3833
     */
3834
    public function set_current_item($item_id = null) {
3835
        if ($this->debug > 0) {
3836
            error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
3837
        }
3838
        if (empty ($item_id)) {
3839
            if ($this->debug > 2) {
3840
                error_log('New LP - No new current item given, ignore...', 0);
3841
            }
3842
            // Do nothing.
3843
        } else {
3844
            if ($this->debug > 2) {
3845
                error_log('New LP - New current item given is ' . $item_id . '...', 0);
3846
            }
3847
            if (is_numeric($item_id)) {
3848
                $item_id = Database::escape_string($item_id);
3849
                // TODO: Check in database here.
3850
                $this->last = $this->current;
3851
                $this->current = $item_id;
3852
                // TODO: Update $this->index as well.
3853
                foreach ($this->ordered_items as $index => $item) {
3854
                    if ($item == $this->current) {
3855
                        $this->index = $index;
3856
                        break;
3857
                    }
3858
                }
3859
                if ($this->debug > 2) {
3860
                    error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
3861
                }
3862
            } else {
3863
                error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
3864
            }
3865
        }
3866
    }
3867

    
3868
    /**
3869
     * Sets the encoding
3870
     * @param	string	New encoding
3871
     * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
3872
     */
3873
    public function set_encoding($enc = 'UTF-8') {
3874
        if ($this->debug > 0) {
3875
            error_log('New LP - In learnpath::set_encoding()', 0);
3876
        }
3877

    
3878
        $course_id = api_get_course_int_id();
3879

    
3880
        /* // Deprecated code (Chamilo 1.8.8).
3881
        $enc = strtoupper($enc);
3882
        $encodings = array (
3883
            'UTF-8',
3884
            'ISO-8859-1',
3885
            'ISO-8859-15',
3886
            'cp1251',
3887
            'cp1252',
3888
            'KOI8-R',
3889
            'BIG5',
3890
            'GB2312',
3891
            'Shift_JIS',
3892
            'EUC-JP',
3893
            ''
3894
        );
3895
        if (in_array($enc, $encodings)) { // TODO: Incorrect comparison, fix it.
3896
            $lp = $this->get_id();
3897
            if ($lp != 0) {
3898
                $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
3899
                $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE id = " . $lp;
3900
                $res = Database::query($sql);
3901
                return $res;
3902
            }
3903
        }
3904
        return false;
3905
        */
3906

    
3907
        $enc = api_refine_encoding_id($enc);
3908
        if (empty($enc)) {
3909
            $enc = api_get_system_encoding();
3910
        }
3911
        if (api_is_encoding_supported($enc)) {
3912
            $lp = $this->get_id();
3913
            if ($lp != 0) {
3914
                $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
3915
                $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
3916
                $res = Database::query($sql);
3917
                return $res;
3918
            }
3919
        }
3920
        return false;
3921
    }
3922

    
3923
    /**
3924
     * Sets the JS lib setting in the database directly.
3925
     * This is the JavaScript library file this lp needs to load on startup
3926
     * @param	string	Proximity setting
3927
     * @return  boolean True on update success. False otherwise.
3928
     */
3929
    public function set_jslib($lib = '') {
3930
        if ($this->debug > 0) {
3931
            error_log('New LP - In learnpath::set_jslib()', 0);
3932
        }
3933
        $lp = $this->get_id();
3934
        $course_id = api_get_course_int_id();
3935

    
3936
        if ($lp != 0) {
3937
            $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
3938
            $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
3939
            $res = Database::query($sql);
3940
            return $res;
3941
        } else {
3942
            return false;
3943
        }
3944
    }
3945

    
3946
    /**
3947
     * Sets the name of the LP maker (publisher) (and save)
3948
     * @param	string	Optional string giving the new content_maker of this learnpath
3949
     * @return  boolean True
3950
     */
3951
    public function set_maker($name = '') {
3952
        if ($this->debug > 0) {
3953
            error_log('New LP - In learnpath::set_maker()', 0);
3954
        }
3955
        if (empty ($name))
3956
            return false;
3957
        $this->maker = Database::escape_string($name);
3958
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
3959
        $course_id = api_get_course_int_id();
3960
        $lp_id = $this->get_id();
3961
        $sql = "UPDATE $lp_table SET content_maker = '" . $this->maker . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
3962
        if ($this->debug > 2) {
3963
            error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
3964
        }
3965
        $res = Database::query($sql);
3966
        return true;
3967
    }
3968

    
3969
    /**
3970
     * Sets the name of the current learnpath (and save)
3971
     * @param	string	Optional string giving the new name of this learnpath
3972
     * @return  boolean True/False
3973
     */
3974
    public function set_name($name = '') {
3975
        if ($this->debug > 0) {
3976
            error_log('New LP - In learnpath::set_name()', 0);
3977
        }
3978
        if (empty ($name))
3979
            return false;
3980

    
3981
        $this->name = Database::escape_string($name);
3982
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
3983
        $lp_id = $this->get_id();
3984
        $course_id = api_get_course_int_id();
3985
        $sql = "UPDATE $lp_table SET name = '" . $this->name . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
3986
        if ($this->debug > 2) {
3987
            error_log('New LP - lp updated with new name : ' . $this->name, 0);
3988
        }
3989
        $res = Database::query($sql);
3990
        // If the lp is visible on the homepage, change his name there.
3991
        if (Database::affected_rows()) {
3992
            $table = Database :: get_course_table(TABLE_TOOL_LIST);
3993
            $sql = 'UPDATE ' . $table . ' SET
3994
                        name = "' . $this->name . '"
3995
                    WHERE c_id = '.$course_id.' AND link = "newscorm/lp_controller.php?action=view&lp_id=' . $lp_id . '"';
3996
            Database::query($sql);
3997
        }
3998
        return true;
3999
    }
4000

    
4001
    /**
4002
     * Set index specified prefix terms for all items in this path
4003
     * @param   string  Comma-separated list of terms
4004
     * @param   char Xapian term prefix
4005
     * @return  boolean False on error, true otherwise
4006
     */
4007
    public function set_terms_by_prefix($terms_string, $prefix) {
4008
        $course_id = api_get_course_int_id();
4009
        if (api_get_setting('search_enabled') !== 'true')
4010
            return false;
4011

    
4012
        if (!extension_loaded('xapian')) {
4013
            return false;
4014
        }
4015

    
4016
        $terms_string = trim($terms_string);
4017
        $terms = explode(',', $terms_string);
4018
        array_walk($terms, 'trim_value');
4019

    
4020
        $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
4021

    
4022
        // Don't do anything if no change, verify only at DB, not the search engine.
4023
        if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
4024
            return false;
4025

    
4026
        require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
4027
        require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
4028
        require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
4029
        require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
4030

    
4031
        $items_table = Database :: get_course_table(TABLE_LP_ITEM);
4032
        // TODO: Make query secure agains XSS : use member attr instead of post var.
4033
        $lp_id = intval($_POST['lp_id']);
4034
        $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
4035
        $result = Database::query($sql);
4036
        $di = new ChamiloIndexer();
4037

    
4038
        while ($lp_item = Database :: fetch_array($result)) {
4039
            // Get search_did.
4040
            $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
4041
            $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
4042
            $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
4043

    
4044
            //echo $sql; echo '<br>';
4045
            $res = Database::query($sql);
4046
            if (Database::num_rows($res) > 0) {
4047

    
4048
                $se_ref = Database :: fetch_array($res);
4049

    
4050
                // Compare terms.
4051
                $doc = $di->get_document($se_ref['search_did']);
4052

    
4053
                $xapian_terms = xapian_get_doc_terms($doc, $prefix);
4054

    
4055
                $xterms = array ();
4056
                foreach ($xapian_terms as $xapian_term) {
4057
                    $xterms[] = substr($xapian_term['name'], 1);
4058
                }
4059

    
4060
                $dterms = $terms;
4061

    
4062
                $missing_terms = array_diff($dterms, $xterms);
4063
                $deprecated_terms = array_diff($xterms, $dterms);
4064

    
4065
                // Save it to search engine.
4066
                foreach ($missing_terms as $term) {
4067
                    $doc->add_term($prefix . $term, 1);
4068
                }
4069
                foreach ($deprecated_terms as $term) {
4070
                    $doc->remove_term($prefix . $term);
4071
                }
4072
                $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
4073
                $di->getDb()->flush();
4074
            } else {
4075
                //@todo What we should do here?
4076
            }
4077
        }
4078
        return true;
4079
    }
4080

    
4081
    /**
4082
     * Sets the theme of the LP (local/remote) (and save)
4083
     * @param	string	Optional string giving the new theme of this learnpath
4084
     * @return   bool    Returns true if theme name is not empty
4085
     */
4086
    public function set_theme($name = '') {
4087
        $course_id = api_get_course_int_id();
4088
        if ($this->debug > 0) {
4089
            error_log('New LP - In learnpath::set_theme()', 0);
4090
        }
4091
        $this->theme = Database::escape_string($name);
4092
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4093
        $lp_id = $this->get_id();
4094
        $sql = "UPDATE $lp_table SET theme = '" . $this->theme . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
4095
        if ($this->debug > 2) {
4096
            error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
4097
        }
4098
        //$res = Database::query($sql);
4099
        $res = Database::query($sql);
4100
        return true;
4101
    }
4102

    
4103
    /**
4104
     * Sets the image of an LP (and save)
4105
     * @param	 string	Optional string giving the new image of this learnpath
4106
     * @return bool   Returns true if theme name is not empty
4107
     */
4108
    public function set_preview_image($name = '') {
4109
        $course_id = api_get_course_int_id();
4110
        if ($this->debug > 0) {
4111
            error_log('New LP - In learnpath::set_preview_image()', 0);
4112
        }
4113

    
4114
        $this->preview_image = Database::escape_string($name);
4115
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4116
        $lp_id = $this->get_id();
4117
        $sql = "UPDATE $lp_table SET preview_image = '" . $this->preview_image . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
4118
        if ($this->debug > 2) {
4119
            error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
4120
        }
4121
        $res = Database::query($sql);
4122
        return true;
4123
    }
4124

    
4125
    /**
4126
     * Sets the author of a LP (and save)
4127
     * @param	string	Optional string giving the new author of this learnpath
4128
     * @return   bool    Returns true if author's name is not empty
4129
     */
4130
    public function set_author($name = '') {
4131
        $course_id = api_get_course_int_id();
4132
        if ($this->debug > 0) {
4133
            error_log('New LP - In learnpath::set_author()', 0);
4134
        }
4135
        $this->author = Database::escape_string($name);
4136
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4137
        $lp_id = $this->get_id();
4138
        $sql = "UPDATE $lp_table SET author = '" . $this->author . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
4139
        if ($this->debug > 2) {
4140
            error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
4141
        }
4142
        $res = Database::query($sql);
4143
        return true;
4144
	}
4145
	/**
4146
	* Sets the hide_toc_frame parameter of a LP (and save)
4147
	* @param	int	1 if frame is hiddent 0 thenelse
4148
	* @return   bool    Returns true if author's name is not empty
4149
	*/
4150
	public function set_hide_toc_frame($hide) {
4151
	    $course_id = api_get_course_int_id();
4152
		if ($this->debug > 0) {
4153
			error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
4154
		}
4155
        if (intval($hide) == $hide){
4156
            $this->hide_toc_frame = $hide;
4157
            $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4158
            $lp_id = $this->get_id();
4159
            $sql = "UPDATE $lp_table SET hide_toc_frame = '" . $this->hide_toc_frame . "'
4160
            WHERE c_id = ".$course_id." AND id = '$lp_id'";
4161
            if ($this->debug > 2) {
4162
                error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
4163
            }
4164
            $res = Database::query($sql);
4165
            return true;
4166
        } else {
4167
            return false;
4168
        }
4169
    }
4170

    
4171
    /**
4172
     * Sets the prerequisite of a LP (and save)
4173
     * @param	int		integer giving the new prerequisite of this learnpath
4174
     * @return 	bool 	returns true if prerequisite is not empty
4175
     */
4176
    public function set_prerequisite($prerequisite) {
4177
        $course_id = api_get_course_int_id();
4178
        if ($this->debug > 0) {
4179
            error_log('New LP - In learnpath::set_prerequisite()', 0);
4180
        }
4181
        $this->prerequisite = intval($prerequisite);
4182
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4183
        $lp_id = $this->get_id();
4184
        $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
4185
                WHERE c_id = ".$course_id." AND id = '$lp_id'";
4186
        if ($this->debug > 2) {
4187
            error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
4188
        }
4189
        $res = Database::query($sql);
4190
        return true;
4191
    }
4192

    
4193
    /**
4194
     * Sets the location/proximity of the LP (local/remote) (and save)
4195
     * @param	string	Optional string giving the new location of this learnpath
4196
     * @return  boolean True on success / False on error
4197
     */
4198
    public function set_proximity($name = '') {
4199
        $course_id = api_get_course_int_id();
4200
        if ($this->debug > 0) {
4201
            error_log('New LP - In learnpath::set_proximity()', 0);
4202
        }
4203
        if (empty ($name))
4204
            return false;
4205

    
4206
        $this->proximity = Database::escape_string($name);
4207
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4208
        $lp_id = $this->get_id();
4209
        $sql = "UPDATE $lp_table SET content_local = '" . $this->proximity . "'
4210
                WHERE c_id = ".$course_id." AND id = '$lp_id'";
4211
        if ($this->debug > 2) {
4212
            error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
4213
        }
4214
        $res = Database::query($sql);
4215
        return true;
4216
    }
4217

    
4218
    /**
4219
     * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
4220
     * @param	integer	DB ID of the item
4221
     */
4222
    public function set_previous_item($id) {
4223
        if ($this->debug > 0) {
4224
            error_log('New LP - In learnpath::set_previous_item()', 0);
4225
        }
4226
        $this->last = $id;
4227
    }
4228

    
4229

    
4230
     /**
4231
     * Sets use_max_score
4232
     * @param   string  Optional string giving the new location of this learnpath
4233
     * @return  boolean True on success / False on error
4234
     */
4235
    public function set_use_max_score($use_max_score = 1) {
4236
        $course_id = api_get_course_int_id();
4237
        if ($this->debug > 0) {
4238
            error_log('New LP - In learnpath::set_use_max_score()', 0);
4239
        }
4240
        $use_max_score = intval($use_max_score);
4241
        $this->use_max_score = $use_max_score;
4242
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4243
        $lp_id = $this->get_id();
4244
        $sql = "UPDATE $lp_table SET use_max_score = '" . $this->use_max_score . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
4245

    
4246
        if ($this->debug > 2) {
4247
            error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
4248
        }
4249
        $res = Database::query($sql);
4250
        return true;
4251
    }
4252

    
4253
     /**
4254
     * Sets and saves the expired_on date
4255
     * @param   string  Optional string giving the new author of this learnpath
4256
     * @return   bool    Returns true if author's name is not empty
4257
     */
4258
    public function set_expired_on($expired_on) {
4259
        $course_id = api_get_course_int_id();
4260
        if ($this->debug > 0) {
4261
            error_log('New LP - In learnpath::set_expired_on()', 0);
4262
        }
4263

    
4264
        if (!empty($expired_on)) {
4265
            $this->expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
4266
        } else {
4267
            $this->expired_on = '';
4268
        }
4269
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4270
        $lp_id = $this->get_id();
4271
        $sql = "UPDATE $lp_table SET expired_on = '" . $this->expired_on . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
4272
        if ($this->debug > 2) {
4273
            error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
4274
        }
4275
        $res = Database::query($sql);
4276
        return true;
4277
    }
4278

    
4279
    /**
4280
     * Sets and saves the publicated_on date
4281
     * @param   string  Optional string giving the new author of this learnpath
4282
     * @return   bool    Returns true if author's name is not empty
4283
     */
4284
    public function set_publicated_on($publicated_on) {
4285
        $course_id = api_get_course_int_id();
4286
        if ($this->debug > 0) {
4287
            error_log('New LP - In learnpath::set_expired_on()', 0);
4288
        }
4289
        if (!empty($publicated_on)) {
4290
            $this->publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
4291
        } else {
4292
            $this->publicated_on = '';
4293
        }
4294
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4295
        $lp_id = $this->get_id();
4296
        $sql = "UPDATE $lp_table SET publicated_on = '" . $this->publicated_on . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
4297
        if ($this->debug > 2) {
4298
            error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
4299
        }
4300
        $res = Database::query($sql);
4301
        return true;
4302
    }
4303

    
4304

    
4305

    
4306
    /**
4307
     * Sets and saves the expired_on date
4308
     * @param   string  Optional string giving the new author of this learnpath
4309
     * @return   bool    Returns true if author's name is not empty
4310
     */
4311
    public function set_modified_on() {
4312
        $course_id = api_get_course_int_id();
4313
        if ($this->debug > 0) {
4314
            error_log('New LP - In learnpath::set_expired_on()', 0);
4315
        }
4316
        $this->modified_on = api_get_utc_datetime();
4317
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4318
        $lp_id = $this->get_id();
4319
        $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
4320
        if ($this->debug > 2) {
4321
            error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
4322
        }
4323
        $res = Database::query($sql);
4324
        return true;
4325
    }
4326

    
4327
    /**
4328
     * Sets the object's error message
4329
     * @param	string	Error message. If empty, reinits the error string
4330
     * @return 	void
4331
     */
4332
    public function set_error_msg($error = '') {
4333
        if ($this->debug > 0) {
4334
            error_log('New LP - In learnpath::set_error_msg()', 0);
4335
        }
4336
        if (empty ($error)) {
4337
            $this->error = '';
4338
        } else {
4339
            $this->error .= $error;
4340
        }
4341
    }
4342

    
4343
    /**
4344
     * Launches the current item if not 'sco' (starts timer and make sure there is a record ready in the DB)
4345
     * @param  boolean     Whether to allow a new attempt or not
4346
     * @return boolean     True
4347
     */
4348
    public function start_current_item($allow_new_attempt = false) {
4349
        if ($this->debug > 0) {
4350
            error_log('New LP - In learnpath::start_current_item()', 0);
4351
        }
4352
        if ($this->current != 0 AND is_object($this->items[$this->current])) {
4353
            $type = $this->get_type();
4354
            $item_type = $this->items[$this->current]->get_type();
4355
            if (($type == 2 && $item_type != 'sco') OR ($type == 3 && $item_type != 'au') OR ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)) {
4356
                $this->items[$this->current]->open($allow_new_attempt);
4357

    
4358
                $this->autocomplete_parents($this->current);
4359
                $prereq_check = $this->prerequisites_match($this->current);
4360
                $this->items[$this->current]->save(false, $prereq_check);
4361
                //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
4362
            } else {
4363
                // If sco, then it is supposed to have been updated by some other call.
4364
            }
4365
            if ($item_type == 'sco') {
4366
                $this->items[$this->current]->restart();
4367
            }
4368
        }
4369
        if ($this->debug > 0) {
4370
            error_log('New LP - End of learnpath::start_current_item()', 0);
4371
        }
4372
        return true;
4373
    }
4374

    
4375
    /**
4376
     * Stops the processing and counters for the old item (as held in $this->last)
4377
     * @return boolean  True/False
4378
     */
4379
    public function stop_previous_item() {
4380
        if ($this->debug > 0) {
4381
            error_log('New LP - In learnpath::stop_previous_item()', 0);
4382
        }
4383

    
4384
        if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
4385
            if ($this->debug > 2) {
4386
                error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
4387
            }
4388
            switch ($this->get_type()) {
4389
                case '3' :
4390
                    if ($this->items[$this->last]->get_type() != 'au') {
4391
                        if ($this->debug > 2) {
4392
                            error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
4393
                        }
4394
                        $this->items[$this->last]->close();
4395
                        //$this->autocomplete_parents($this->last);
4396
                        //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
4397
                    } else {
4398
                        if ($this->debug > 2) {
4399
                            error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
4400
                        }
4401
                    }
4402
                case '2' :
4403
                    if ($this->items[$this->last]->get_type() != 'sco') {
4404
                        if ($this->debug > 2) {
4405
                            error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
4406
                        }
4407
                        $this->items[$this->last]->close();
4408
                        //$this->autocomplete_parents($this->last);
4409
                        //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
4410
                    } else {
4411
                        if ($this->debug > 2) {
4412
                            error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
4413
                        }
4414
                    }
4415
                    break;
4416
                case '1' :
4417
                default :
4418
                    if ($this->debug > 2) {
4419
                        error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
4420
                    }
4421
                    $this->items[$this->last]->close();
4422
                    break;
4423
            }
4424
        } else {
4425
            if ($this->debug > 2) {
4426
                error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
4427
            }
4428
            return false;
4429
        }
4430
        return true;
4431
    }
4432

    
4433
    /**
4434
     * Updates the default view mode from fullscreen to embedded and inversely
4435
     * @return	string The current default view mode ('fullscreen' or 'embedded')
4436
     */
4437
    public function update_default_view_mode() {
4438
        $course_id = api_get_course_int_id();
4439
        if ($this->debug > 0) {
4440
            error_log('New LP - In learnpath::update_default_view_mode()', 0);
4441
        }
4442
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4443
        $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4444
        $res = Database::query($sql);
4445
        if (Database :: num_rows($res) > 0) {
4446
            $row = Database :: fetch_array($res);
4447
            $view_mode = $row['default_view_mod'];
4448
            if ($view_mode == 'fullscreen') {
4449
                $view_mode = 'embedded';
4450
            } elseif ($view_mode == 'embedded') {
4451
                $view_mode = 'embedframe';
4452
            } elseif ($view_mode == 'embedframe') {
4453
                $view_mode = 'fullscreen';
4454
            }
4455
            $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode' WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4456
            $res = Database::query($sql);
4457
            $this->mode = $view_mode;
4458
            return $view_mode;
4459
        } else {
4460
            if ($this->debug > 2) {
4461
                error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
4462
            }
4463
        }
4464
        return -1;
4465
    }
4466

    
4467
    /**
4468
     * Updates the default behaviour about auto-commiting SCORM updates
4469
     * @return	boolean	True if auto-commit has been set to 'on', false otherwise
4470
     */
4471
    public function update_default_scorm_commit() {
4472
        $course_id = api_get_course_int_id();
4473
        if ($this->debug > 0) {
4474
            error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
4475
        }
4476
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4477
        $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4478
        $res = Database::query($sql);
4479
        if (Database :: num_rows($res) > 0) {
4480
            $row = Database :: fetch_array($res);
4481
            $force = $row['force_commit'];
4482
            if ($force == 1) {
4483
                $force = 0;
4484
                $force_return = false;
4485
            } elseif ($force == 0) {
4486
                $force = 1;
4487
                $force_return = true;
4488
            }
4489
            $sql = "UPDATE $lp_table SET force_commit = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4490
            $res = Database::query($sql);
4491
            $this->force_commit = $force_return;
4492
            return $force_return;
4493
        } else {
4494
            if ($this->debug > 2) {
4495
                error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
4496
            }
4497
        }
4498
        return -1;
4499
    }
4500

    
4501
    /**
4502
     * Updates the order of learning paths (goes through all of them by order and fills the gaps)
4503
     * @return	bool	True on success, false on failure
4504
     */
4505
    public function update_display_order() {
4506
        $course_id = api_get_course_int_id();
4507
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4508

    
4509
        $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
4510
        $res = Database::query($sql);
4511
        if ($res === false)
4512
            return false;
4513
        $lps = array ();
4514
        $lp_order = array ();
4515
        $num = Database :: num_rows($res);
4516
        // First check the order is correct, globally (might be wrong because
4517
        // of versions < 1.8.4).
4518
        if ($num > 0) {
4519
            $i = 1;
4520
            while ($row = Database :: fetch_array($res)) {
4521
                if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
4522
                    $need_fix = true;
4523
                    $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = " . $row['id'];
4524
                    $res_u = Database::query($sql_u);
4525
                }
4526
                $i++;
4527
            }
4528
        }
4529
        return true;
4530
    }
4531

    
4532
    /**
4533
     * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
4534
     * @return	boolean	True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
4535
     */
4536
    public function update_reinit() {
4537
        $course_id = api_get_course_int_id();
4538
        if ($this->debug > 0) {
4539
            error_log('New LP - In learnpath::update_reinit()', 0);
4540
        }
4541
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4542
        $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4543
        $res = Database::query($sql);
4544
        if (Database :: num_rows($res) > 0) {
4545
            $row = Database :: fetch_array($res);
4546
            $force = $row['prevent_reinit'];
4547
            if ($force == 1) {
4548
                $force = 0;
4549
            } elseif ($force == 0) {
4550
                $force = 1;
4551
            }
4552
            $sql = "UPDATE $lp_table SET prevent_reinit = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4553
            $res = Database::query($sql);
4554
            $this->prevent_reinit = $force;
4555
            return $force;
4556
        } else {
4557
            if ($this->debug > 2) {
4558
                error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
4559
            }
4560
        }
4561
        return -1;
4562
    }
4563

    
4564
    /**
4565
     * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
4566
     *
4567
     * @return string 'single', 'multi' or 'seriousgame'
4568
     * @author ndiechburg <noel@cblue.be>
4569
     **/
4570
    public function get_attempt_mode() {
4571
        if (!isset($this->seriousgame_mode)) { //Set default value for seriousgame_mode
4572
            $this->seriousgame_mode=0;
4573
        }
4574
        if (!isset($this->prevent_reinit)) { // Set default value for prevent_reinit
4575
            $this->prevent_reinit =1;
4576
        }
4577
        if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
4578
            return 'seriousgame';
4579
        }
4580
        if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
4581
            return 'single';
4582
        }
4583
        if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
4584
            return 'multiple';
4585
        }
4586
        return 'single';
4587
    }
4588

    
4589
    /**
4590
     * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
4591
     *
4592
     * @param string 'seriousgame', 'single' or 'multiple'
4593
     * @return boolean
4594
     * @author ndiechburg <noel@cblue.be>
4595
     **/
4596
    public function set_attempt_mode($mode) {
4597
        $course_id = api_get_course_int_id();
4598
        switch ($mode) {
4599
            case 'seriousgame' :
4600
              $sg_mode = 1;
4601
              $prevent_reinit = 1;
4602
              break;
4603
            case 'single' :
4604
              $sg_mode = 0;
4605
              $prevent_reinit = 1;
4606
              break;
4607
            case 'multiple' :
4608
              $sg_mode = 0;
4609
              $prevent_reinit = 0;
4610
              break;
4611
            default :
4612
              $sg_mode = 0;
4613
              $prevent_reinit = 0;
4614
              break;
4615
        }
4616
        $this->prevent_reinit = $prevent_reinit;
4617
        $this->seriousgame_mode = $sg_mode;
4618
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4619
        $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4620
        $res = Database::query($sql);
4621
        if ($res) {
4622
            return true;
4623
        } else {
4624
          return false;
4625
        }
4626
    }
4627

    
4628
  /**
4629
   * switch between multiple attempt, single attempt or serious_game mode (only for scorm)
4630
   *
4631
   * @return boolean
4632
   * @author ndiechburg <noel@cblue.be>
4633
   **/
4634
    public function switch_attempt_mode() {
4635
        if ($this->debug > 0) {
4636
          error_log('New LP - In learnpath::switch_attempt_mode()', 0);
4637
        }
4638
        $mode = $this->get_attempt_mode();
4639
        switch ($mode) {
4640
            case 'single' :
4641
              $next_mode = 'multiple';
4642
              break;
4643
            case 'multiple' :
4644
              $next_mode = 'seriousgame';
4645
              break;
4646
            case 'seriousgame' :
4647
              $next_mode = 'single';
4648
              break;
4649
            default :
4650
              $next_mode = 'single';
4651
              break;
4652
        }
4653
        $this->set_attempt_mode($next_mode);
4654
    }
4655

    
4656
  /**
4657
   * Swithc the lp in ktm mode. This is a special scorm mode with unique attempt but possibility to do again a completed item.
4658
   *
4659
   * @return boolean true if seriousgame_mode has been set to 1, false otherwise
4660
   * @author ndiechburg <noel@cblue.be>
4661
   **/
4662
    public function set_seriousgame_mode() {
4663
        $course_id = api_get_course_int_id();
4664
		if ($this->debug > 0) {
4665
			error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
4666
		}
4667
		$lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4668
		$sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4669
		$res = Database::query($sql);
4670
		if (Database :: num_rows($res) > 0) {
4671
			$row = Database :: fetch_array($res);
4672
			$force = $row['seriousgame_mode'];
4673
			if ($force == 1) {
4674
				$force = 0;
4675
			} elseif ($force == 0) {
4676
				$force = 1;
4677
			}
4678
			$sql = "UPDATE $lp_table SET seriousgame_mode = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4679
			$res = Database::query($sql);
4680
			$this->seriousgame_mode = $force;
4681
			return $force;
4682
		} else {
4683
			if ($this->debug > 2) {
4684
				error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
4685
			}
4686
		}
4687
		return -1;
4688
    }
4689
    
4690
    /**
4691
     * Updates the "scorm_debug" value that shows or hide the debug window
4692
     * @return	boolean	True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
4693
     */
4694
    public function update_scorm_debug() {
4695
        $course_id = api_get_course_int_id();
4696
        if ($this->debug > 0) {
4697
            error_log('New LP - In learnpath::update_scorm_debug()', 0);
4698
        }
4699
        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
4700
        $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4701
        $res = Database::query($sql);
4702
        if (Database :: num_rows($res) > 0) {
4703
            $row = Database :: fetch_array($res);
4704
            $force = $row['debug'];
4705
            if ($force == 1) {
4706
                $force = 0;
4707
            } elseif ($force == 0) {
4708
                $force = 1;
4709
            }
4710
            $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
4711
            $res = Database::query($sql);
4712
            $this->scorm_debug = $force;
4713
            return $force;
4714
        } else {
4715
            if ($this->debug > 2) {
4716
                error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
4717
            }
4718
        }
4719
        return -1;
4720
    }
4721

    
4722
    /**
4723
     * Function that makes a call to the function sort_tree_array and create_tree_array
4724
     * @author Kevin Van Den Haute
4725
     * @param  array
4726
     */
4727
    public function tree_array($array) {
4728
        if ($this->debug > 1) {
4729
            error_log('New LP - In learnpath::tree_array()', 0);
4730
        }
4731
        $array = $this->sort_tree_array($array);
4732
        $this->create_tree_array($array);
4733
    }
4734

    
4735
    /**
4736
     * Creates an array with the elements of the learning path tree in it
4737
     *
4738
     * @author Kevin Van Den Haute
4739
     * @param array $array
4740
     * @param int $parent
4741
     * @param int $depth
4742
     * @param array $tmp
4743
     */
4744
    public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ()) {
4745
        if ($this->debug > 1) {
4746
            error_log('New LP - In learnpath::create_tree_array())', 0);
4747
        }
4748
        if (is_array($array)) {
4749
            for ($i = 0; $i < count($array); $i++) {
4750
                if ($array[$i]['parent_item_id'] == $parent) {
4751
                    if (!in_array($array[$i]['parent_item_id'], $tmp)) {
4752
                        $tmp[] = $array[$i]['parent_item_id'];
4753
                        $depth++;
4754
                    }
4755
                    $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
4756
                    $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
4757
                    $this->arrMenu[] = array (
4758
                        'id' => $array[$i]['id'],
4759
                        'item_type' => $array[$i]['item_type'],
4760
                        'title' => $array[$i]['title'],
4761
                        'path' => $array[$i]['path'],
4762
                        'description' => $array[$i]['description'],
4763
                        'parent_item_id' => $array[$i]['parent_item_id'],
4764
                        'previous_item_id' => $array[$i]['previous_item_id'],
4765
                        'next_item_id' => $array[$i]['next_item_id'],
4766
                        'min_score' => $array[$i]['min_score'],
4767
                        'max_score' => $array[$i]['max_score'],
4768
                        'mastery_score' => $array[$i]['mastery_score'],
4769
                        'display_order' => $array[$i]['display_order'],
4770
                        'prerequisite' => $preq,
4771
                        'depth' => $depth,
4772
                        'audio' => $audio
4773
                    );
4774

    
4775
                    $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
4776
                }
4777
            }
4778
        }
4779
    }
4780

    
4781
    /**
4782
     * Sorts a multi dimensional array by parent id and display order
4783
     * @author Kevin Van Den Haute
4784
     *
4785
     * @param array $array (array with al the learning path items in it)
4786
     *
4787
     * @return array
4788
     */
4789
    public function sort_tree_array($array) {
4790
        foreach ($array as $key => $row) {
4791
            $parent[$key] = $row['parent_item_id'];
4792
            $position[$key] = $row['display_order'];
4793
        }
4794

    
4795
        if (count($array) > 0)
4796
            array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
4797

    
4798
        return $array;
4799
    }
4800

    
4801
    /**
4802
     * Function that creates a table structure with a learning path his modules, chapters and documents.
4803
     * Also the actions for the modules, chapters and documents are in this table.
4804
     * @author Kevin Van Den Haute
4805
     * @param int $lp_id
4806
     * @return string
4807
     */
4808
    public function overview() {
4809
        $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
4810

    
4811
        if ($this->debug > 0) {
4812
            error_log('New LP - In learnpath::overview()', 0);
4813
        }
4814
        global $_course;
4815
        $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
4816
        $return = '';
4817

    
4818
        $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
4819

    
4820
        // we need to start a form when we want to update all the mp3 files
4821
        if ($update_audio == 'true') {
4822
            $return .= '<form action="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;updateaudio=' . Security :: remove_XSS($_GET['updateaudio']) .'&amp;action=' . Security :: remove_XSS($_GET['action']) . '&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
4823
        }
4824
        $return .= '<div id="message"></div>';
4825

    
4826
        $return_audio = '<table class="data_table">';
4827
        $return_audio .= '<tr>';
4828
        $return_audio .= '<th width="60%">' . get_lang('Title') . '</th>';
4829
        $return_audio .= '<th>' . get_lang('Audio') . '</th>';
4830
   		$return_audio .= '</tr>';
4831

    
4832
        if ($update_audio != 'true') {
4833
        	$return .= '<div class="span12">';
4834
            $return .= self::return_new_tree($update_audio);
4835
        	$return .='</div>';
4836
        	$return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn')), array('style'=>'float:left; margin-top:15px;width:100%'));
4837
        } else {
4838
            $return_audio .= self::return_new_tree($update_audio);
4839
        	$return .= $return_audio.'</table>';
4840
        }
4841

    
4842
        // We need to close the form when we are updating the mp3 files.
4843
        if ($update_audio == 'true') {
4844
            $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
4845
        }
4846

    
4847
        // We need to close the form when we are updating the mp3 files.
4848
        if ($update_audio == 'true' && count($arrLP) != 0) {
4849
            $return .= '</form>';
4850
        }
4851
        return $return;
4852
    }
4853

    
4854
    public function return_new_tree($update_audio = 'false', $drop_element_here = false) {
4855
        $ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
4856
        echo '<script>
4857
        var newOrderData= "";
4858
        function processChildren(parentId) {
4859
            //Loop through the children of the UL element defined by the parentId
4860
            var ulParentID= "UL_" + parentId;
4861
            $("#" + ulParentID).children().each(function () {
4862

    
4863
                /*Only process elements with an id attribute (in order to skip the blank,
4864
                    unmovable <li> elements.*/
4865

    
4866
                if ($(this).attr("id")) {
4867
                    /*Build a string of data with the childs ID and parent ID,
4868
                        using the "|" as a delimiter between the two IDs and the "^"
4869
                        as a record delimiter (these delimiters were chosen in case the data
4870
                        involved includes more common delimiters like commas within the content)
4871
                    */
4872
                    newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
4873

    
4874
                    //Determine if this child is a containter
4875
                    if ($(this).is(".container")) {
4876
                        //Process the child elements of the container
4877
                        processChildren($(this).attr("id"));
4878
                    }
4879
                }
4880
            });  //end of children loop
4881
        } //end of processChildren function
4882

    
4883
        $(function() {
4884

    
4885
            $(".item_data").live("mouseover", function(event) {
4886
                $(".button_actions", this).show();
4887
            });
4888

    
4889
            $(".item_data").live("mouseout", function() {
4890
                $(".button_actions",this).hide();
4891
            });
4892

    
4893
            $(".button_actions").hide();
4894

    
4895
            $( ".lp_resource" ).sortable({
4896
                items: ".lp_resource_element ",
4897
                handle: ".moved", //only the class "moved"
4898
                cursor: "move",
4899
                connectWith: "#lp_item_list",
4900
                placeholder: "ui-state-highlight", //defines the yellow highlight
4901

    
4902
                start: function(event, ui) {
4903
                    $(ui.item).css("width", "160px");
4904
                    $(ui.item).find(".item_data").attr("style", "");
4905

    
4906
                },
4907
                stop: function(event, ui) {
4908
                    $(ui.item).css("width", "100%");
4909
                },
4910
            });
4911

    
4912
            $("#lp_item_list").sortable({
4913
                items: "li",
4914
                handle: ".moved", //only the class "moved"
4915
                cursor: "move",
4916
                placeholder: "ui-state-highlight", //defines the yellow highlight
4917

    
4918
                update: function(event, ui) {
4919

    
4920
                    //Walk through the direct descendants of the lp_item_list <ul>
4921
                    $("#lp_item_list").children().each(function () {
4922

    
4923
                        /*Only process elements with an id attribute (in order to skip the blank,
4924
                        unmovable <li> elements.*/
4925

    
4926
                        if ($(this).attr("id")) {
4927
                                /*Build a string of data with the child s ID and parent ID,
4928
                                using the "|" as a delimiter between the two IDs and the "^"
4929
                                as a record delimiter (these delimiters were chosen in case the data
4930
                                involved includes more common delimiters like commas within the content)
4931
                                */
4932
                                newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
4933

    
4934
                                //Determine if this child is a containter
4935
                                if ($(this).is(".li_container")) {
4936
                                    //Process the child elements of the container
4937
                                    processChildren($(this).attr("id"));
4938
                                }
4939
                            }
4940
                    }); //end of lp_item_list children loop
4941

    
4942
                    var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
4943
                    $.post("'.$ajax_url.'", order, function(reponse){
4944
                        $("#message").html(reponse);
4945
                    });
4946
                },
4947
                receive: function(event, ui) {
4948

    
4949
                    var id = $(ui.item).attr("data_id");
4950
                    var type = $(ui.item).attr("data_type");
4951
                    var title = $(ui.item).attr("title");
4952

    
4953
                    if (ui.item.parent()[0]) {
4954
                        var parent_id = $(ui.item.parent()[0]).attr("id");
4955
                        var previous_id = $(ui.item.prev()).attr("id");
4956

    
4957
                        if (parent_id) {
4958
                            parent_id = parent_id.split("_")[1];
4959
                            var params = {
4960
                                    "a": "add_lp_item",
4961
                                    "id": id,
4962
                                    "parent_id": parent_id,
4963
                                    "previous_id": previous_id,
4964
                                    "type": type,
4965
                                    "title" : title
4966
                                };
4967
                            $.ajax({
4968
                                type: "GET",
4969
                                url: "'.$ajax_url.'",
4970
                                data: params,
4971
                                async: false,
4972
                                success: function(data) {
4973
                                    if (data == -1) {
4974
                                    } else {
4975

    
4976
                                        $(".normal-message").hide();
4977
                                        $(ui.item).attr("id", data);
4978
                                        $(ui.item).addClass("lp_resource_element_new");
4979
                                        $(ui.item).find(".item_data").attr("style", "");
4980
                                        $(ui.item).addClass("record li_container");
4981
                                        $(ui.item).removeClass("lp_resource_element");
4982
                                        $(ui.item).removeClass("doc_resource");
4983
                                    }
4984
                                }
4985
                            });
4986
                        }
4987
                    }//
4988
                }//end receive
4989
            });
4990
        });
4991
        </script>';
4992

    
4993
        $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
4994

    
4995
        $course_id = api_get_course_int_id();
4996
        $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
4997

    
4998
        $sql = "SELECT * FROM $tbl_lp_item
4999
                WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
5000

    
5001
        $result = Database::query($sql);
5002
        $arrLP = array();
5003
        while ($row = Database :: fetch_array($result)) {
5004
            $row['title'] = Security :: remove_XSS($row['title']);
5005
            $row['description'] = Security :: remove_XSS($row['description']);
5006
            $arrLP[] = array (
5007
                'id' => $row['id'],
5008
                'item_type' => $row['item_type'],
5009
                'title' => $row['title'],
5010
                'path' => $row['path'],
5011
                'description' => $row['description'],
5012
                'parent_item_id' => $row['parent_item_id'],
5013
                'previous_item_id' => $row['previous_item_id'],
5014
                'next_item_id' => $row['next_item_id'],
5015
                'max_score' => $row['max_score'],
5016
                'min_score' => $row['min_score'],
5017
                'mastery_score' => $row['mastery_score'],
5018
                'prerequisite' => $row['prerequisite'],
5019
                'display_order' => $row['display_order'],
5020
                'audio' => $row['audio']
5021
            );
5022
        }
5023

    
5024

    
5025
        $this->tree_array($arrLP);
5026
        $arrLP = $this->arrMenu;
5027
        unset ($this->arrMenu);
5028

    
5029
        $elements = array();
5030
        for ($i = 0; $i < count($arrLP); $i++) {
5031
            $title = $arrLP[$i]['title'];
5032
            $title_cut = cut($arrLP[$i]['title'], 25);
5033
            if (($i % 2) == 0) {
5034
                $oddclass = 'row_odd';
5035
            } else {
5036
                $oddclass = 'row_even';
5037
            }
5038
            $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddclass . '">';
5039

    
5040
            $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
5041

    
5042
            $icon = '';
5043
            if (file_exists('../img/lp_' . $icon_name . '.png')) {
5044
            	$icon = '<img src="../img/lp_' . $icon_name . '.png" />';
5045
            } else {
5046
            	if (file_exists('../img/lp_' . $icon_name . '.gif')) {
5047
            		$icon = '<img src="../img/lp_' . $icon_name . '.gif"  />';
5048
            	} else {
5049
            		$icon = '<img src="../img/folder_document.gif" />';
5050
            	}
5051
            }
5052

    
5053
            // The audio column.
5054
            $return_audio  .= '<td align="center">';
5055

    
5056
            $audio = '';
5057
            
5058
            if (!$update_audio OR $update_audio <> 'true') {
5059
                if (!empty($arrLP[$i]['audio'])) {
5060
                    /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
5061
                    $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
5062
                    $audio .= '<script type="text/javascript">
5063
                                    var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
5064
                                    s1.addParam("allowscriptaccess","always");
5065
                                    s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
5066
                                    s1.write("container' . $i . '");
5067
                                </script>';*/
5068
                } else {
5069
                    $audio .= '';
5070
                }
5071
            } else {
5072
                if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'dokeos_module' && $arrLP[$i]['item_type'] != 'dir') {
5073
                    $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
5074
                    if (!empty ($arrLP[$i]['audio'])) {
5075
                        $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br /><input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
5076
                    }
5077
                }
5078
            }
5079
            $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
5080
            $return_audio .= '</td>';
5081
			$move_icon = '';
5082
            $move_item_icon = '';
5083
			$edit_icon = '';
5084
			$delete_icon = '';
5085
            $audio_icon = '';$prerequisities_icon = '';
5086

    
5087
            if ($is_allowed_to_edit) {
5088
                if (!$update_audio OR $update_audio <> 'true') {
5089
                    $move_icon .= '<a class="moved" href="#">';
5090
					$move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
5091
                    $move_icon .= '</a>';
5092
                }
5093

    
5094
                // No edit for this item types
5095
                if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
5096
                    if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
5097
                        $edit_icon .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=edit_item&amp;view=build&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id . '&amp;path_item=' . $arrLP[$i]['path'] . '">';
5098
                        $edit_icon .= Display::return_icon('edit.png', get_lang('_edit_learnpath_module'), array(), ICON_SIZE_TINY);
5099
                        $edit_icon .= '</a>';
5100
                    } else {
5101
                        $edit_icon .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=edit_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id . '&amp;path_item=' . $arrLP[$i]['path'] . '">';
5102
                        $edit_icon .= Display::return_icon('edit.png', get_lang('_edit_learnpath_module'), array(), ICON_SIZE_TINY);
5103
                        $edit_icon .= '</a>';
5104
                    }
5105
                }
5106

    
5107
                $delete_icon .= ' <a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=delete_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id . '" onClick="return confirmation(\'' . addslashes($title) . '\');">';
5108
                $delete_icon .= Display::return_icon('delete.png', get_lang('_delete_learnpath_module'), array(), ICON_SIZE_TINY);
5109
                $delete_icon .= '</a>';
5110

    
5111
                $url = api_get_self() . '?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
5112
                    
5113
                if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
5114
                    $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('Prerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
5115
                    $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');                
5116
                    $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
5117
                }
5118
            }
5119
            if ($update_audio != 'true') {
5120
            	$row = $move_icon.' '.$icon.Display::span($title_cut).Display::span($audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon, array('class'=>'button_actions'));
5121
            } else {
5122
            	$row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
5123
            }
5124
            $parent_id = $arrLP[$i]['parent_item_id'];
5125

    
5126
            $default_data[$arrLP[$i]['id']] = $row;
5127
            $default_content[$arrLP[$i]['id']] = $arrLP[$i];
5128

    
5129
            if (empty($parent_id)) {
5130
            	$elements[$arrLP[$i]['id']]['data'] = $row;
5131
            	$elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
5132
            } else {
5133
            	$parent_arrays = array();
5134
            	if ($arrLP[$i]['depth'] > 1) {
5135
            		//Getting list of parents
5136
            		for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
5137
            			foreach($arrLP as $item) {
5138
            				if ($item['id'] == $parent_id) {
5139
            					if ($item['parent_item_id'] == 0) {
5140
            						$parent_id = $item['id'];
5141
            						break;
5142
            					} else {
5143
            						$parent_id = $item['parent_item_id'];
5144
            						if (empty($parent_arrays)) {
5145
            							$parent_arrays[] = intval($item['id']);
5146
            						}
5147
            						$parent_arrays[] = $parent_id;
5148
            						break;
5149
            					}
5150
            				}
5151
            			}
5152
            		}
5153
            	}
5154

    
5155
            	if (!empty($parent_arrays)) {
5156
	            	$parent_arrays = array_reverse($parent_arrays);
5157
	            	$val = '$elements';
5158
	            	$x = 0;
5159
	            	foreach($parent_arrays as $item) {
5160
	            		if ($x != count($parent_arrays) -1) {
5161
	            			$val .= '["'.$item.'"]["children"]';
5162
	            		} else {
5163
	            			$val .= '["'.$item.'"]["children"]';
5164
	            		}
5165
	            		$x++;
5166
	            	}
5167
	            	$val .= "";
5168
	            	$code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
5169
	            	eval($code_str);
5170
            	} else {
5171
	            	$elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
5172
	            	$elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
5173
            	}
5174
            }
5175
        }
5176

    
5177
        $return = '<div class="lp_tree well">';
5178

    
5179
        $return .= '<ul id="lp_item_list">';
5180
        $return .='<h4>'.$this->name.'</h4><br>';
5181

    
5182
        $tree = self::print_recursive($elements, $default_data, $default_content);
5183

    
5184
        if (!empty($tree)) {
5185
            $return .= $tree;
5186
        } else {
5187
            if ($drop_element_here) {
5188
                $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
5189
            }
5190
        }
5191
        $return .= '</ul>';
5192
        if ($update_audio == 'true') {
5193
            $return = $return_audio;
5194
        } else {
5195
            $return .= '</div>';
5196
        }
5197
        return $return;
5198
    }
5199

    
5200
    function print_recursive($elements, $default_data, $default_content) {
5201
        $return = '';
5202
        foreach ($elements as $key => $item) {
5203
            if (isset($item['load_data']) || empty($item['data'])) {
5204
                $item['data'] = $default_data[$item['load_data']];
5205
                $item['type'] = $default_content[$item['load_data']]['item_type'];
5206
            }
5207
            $sub_list = '';
5208
            if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
5209
                $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
5210
            }
5211
            if (empty($item['children'])) {
5212
                $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
5213
                $active = null;
5214
                if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
5215
                    $active = 'active';
5216
                }
5217
                $return  .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
5218
            } else {
5219
                //sections
5220
                if (isset($item['children'])) {
5221
                    $data = self::print_recursive($item['children'], $default_data, $default_content);
5222
                }
5223
                $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
5224
                $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
5225
            }
5226
        }
5227
        return $return;
5228
    }
5229

    
5230
    /**
5231
     * This function builds the action menu
5232
     * @return void
5233
     */
5234
    public function build_action_menu() {
5235
        $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
5236

    
5237
        $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
5238
        $return = '<div class="actions">';
5239

    
5240
        $return .= '<a href="lp_controller.php?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&action=build&lp_id=' . $this->lp_id . '">' . Display :: return_icon('home.png', get_lang('Build'),'',ICON_SIZE_MEDIUM).'</a>';
5241

    
5242
        if ($is_allowed_to_edit) {
5243
        }
5244
        //$return .=  '<a href="' . api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('BasicOverview') . '">' . Display :: return_icon('move_learnpath.png', get_lang('BasicOverview'),'',ICON_SIZE_MEDIUM).'</a>';
5245
        $return .=  '<a href="lp_controller.php?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&action=view&lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('view_left_right.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</a> ';
5246

    
5247
        $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=add_item&amp;type=step&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('NewStep') . '">' . Display :: return_icon('new_learnigpath_object.png', get_lang('NewStep'),'',ICON_SIZE_MEDIUM).'</a>';
5248
//		echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=add_item&amp;type=chapter&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('NewChapter') . '">' . Display :: return_icon('add_learnpath_section.png', get_lang('NewChapter'),'',ICON_SIZE_MEDIUM).'</a>';
5249
        
5250
        $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '&amp;updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'