Project

General

Profile

Bug #5395 ยป learnpath.class.php

Yoselyn Castillo, 14/09/2012 15:40

 
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
        
675
        switch ($origin) {
676
            case 'zip':
677
                // Check zipname string. If empty, we are currently creating a new Chamilo learnpath.
678
                break;
679
            case 'manual':
680
            default:
681
                $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
682
                $res_max = Database::query($get_max);
683
                if (Database :: num_rows($res_max) < 1) {
684
                    $dsp = 1;
685
                } else {
686
                    $row = Database :: fetch_array($res_max);
687
                    $dsp = $row[0] + 1;
688
                }
689

    
690
                $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) " .
691
                              "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."')";
692

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

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

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

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

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

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

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

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

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

    
872

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

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

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

    
927
        require_once '../gradebook/lib/be.inc.php';
928

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

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

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

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

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

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

    
1034
        // Remove from search engine if enabled.
1035
        if (api_get_setting('search_enabled') == 'true') {
1036
            $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
1037
            $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';
1038
            $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
1039
            $res = Database::query($sql);
1040
            if (Database :: num_rows($res) > 0) {
1041
                $row2 = Database :: fetch_array($res);
1042
                require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
1043
                $di = new ChamiloIndexer();
1044
                $di->remove_document((int) $row2['search_did']);
1045
            }
1046
            $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';
1047
            $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
1048
            Database::query($sql);
1049
        }
1050
    }
1051

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

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

    
1104
        $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
1105
        $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
1106

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

    
1126
            /* BEGIN -- virtually remove the current item id */
1127
            /* for the next and previous item it is like the current item doesn't exist anymore */
1128

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

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

    
1145
                //echo '<p>' . $sql_update_previous . '</p>';
1146
            }
1147

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

    
1158
            //echo '<p>' . $sql_update_order . '</p>';
1159

    
1160
            /* END -- virtually remove the current item id */
1161

    
1162
            /* BEGIN -- update the current item id to his new location */
1163

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

    
1176
                //echo '<p>' . $sql_select_old . '</p>';
1177

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

    
1187
                //echo 'New next_item_id of current item: ' . $new_next . '<br />';
1188
                //echo 'New previous_item_id of current item: ' . $previous . '<br />';
1189
                //echo 'New display_order of current item: ' . $new_order . '<br />';
1190

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

    
1199
                //echo '<p>' . $sql_select_old . '</p>';
1200

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

    
1205
                $new_next = $row_select_old['next_item_id'];
1206
                $new_order = $row_select_old['display_order'] + 1;
1207
            }
1208

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

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

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

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

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

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

    
1271
            $res_update_next = Database::query($sql_update_order);
1272
        }
1273
    }
1274

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

    
1290
        if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
1291
            return false;
1292
        }
1293

    
1294
        $prerequisite_id = Database::escape_string($prerequisite_id);
1295

    
1296
        $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
1297

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

    
1301
        if (!is_numeric($max_score) || $max_score < 0)
1302
            $max_score = 100;
1303

    
1304
        if ($mastery_score > $max_score)
1305
            $max_score = $mastery_score;
1306

    
1307
        if (!is_numeric($prerequisite_id))
1308
            $prerequisite_id = 'NULL';
1309

    
1310
        $sql_upd = " UPDATE " . $tbl_lp_item . "
1311
                     SET prerequisite = " . $prerequisite_id . " WHERE c_id = ".$course_id." AND id = " . $id;
1312
        $res_upd = Database::query($sql_upd);
1313

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

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

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

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

    
1374
        if (empty ($id) OR $id != strval(intval($id))) {
1375
            return array ();
1376
        }
1377

    
1378
        $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
1379
        $sql_parent = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
1380
        $res_parent = Database::query($sql_parent);
1381
        if (Database :: num_rows($res_parent) > 0) {
1382
            $row_parent = Database :: fetch_array($res_parent);
1383
            $parent = $row_parent['parent_item_id'];
1384
            $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";
1385
            $res_bros = Database::query($sql_bros);
1386
            $list = array ();
1387
            while ($row_bro = Database :: fetch_array($res_bros)) {
1388
                $list[] = $row_bro;
1389
            }
1390
            return $list;
1391
        }
1392
        return array ();
1393
    }
1394

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

    
1407
        if (empty ($id) OR $id != strval(intval($id))) {
1408
            return array ();
1409
        }
1410

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

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

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

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

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

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

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

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

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

    
1625
        $info = '';
1626
        $item_id = Database::escape_string($item_id);
1627

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

    
1646
        } else {
1647

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

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

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

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

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

    
1712
        // TODO: Find a good value for the following variables.
1713
        $file = '';
1714
        $openDir = '';
1715
        $edoceo = '';
1716
        $time = 0;
1717
        $navbar = '';
1718
        $RequestUri = '';
1719
        $mycurrentitemid = $this->get_current_item_id();
1720
        if ($this->mode == 'fullscreen') {
1721
            $navbar = '
1722
                  <div class="buttons">
1723
                    <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>
1724
                    <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="' . get_lang('ScormPrevious') . '"></a>
1725
                    <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next"  ><img border="0" src="../img/lp_rightarrow.gif" title="' . get_lang('ScormNext') . '"></a>.
1726
                    <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>
1727
                  </div>';
1728

    
1729
        } else {
1730
            $navbar = '
1731
                  <div class="buttons">
1732
                    <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>
1733
                    <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="' . get_lang('ScormPrevious') . '"></a>
1734
                    <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next"  ><img border="0" src="../img/lp_rightarrow.gif" title="' . get_lang('ScormNext') . '"></a>
1735
                  </div>';
1736
        }
1737
        return $navbar;
1738
    }
1739

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1940
        if (empty ($progress)) {
1941
            $progress = '0';
1942
        }
1943

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

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

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

    
1994
        // Getting all the information about the item.
1995
        $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 " .
1996
                "WHERE  lp.id = '" . $_SESSION['oLP']->current . "' AND
1997
                        lp.c_id = $course_id AND
1998
                        lp_view.c_id = $course_id";
1999
        $result = Database::query($sql);
2000
        $row 	= Database::fetch_assoc($result);
2001
        $output = '';
2002

    
2003
        if (!empty ($row['audio'])) {
2004

    
2005
            $list = $_SESSION['oLP']->get_toc();
2006
            $type_quiz = false;
2007

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

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

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

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

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

    
2067
            // Also check the time availability of the LP
2068

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

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

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

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

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

    
2129
        return $output;
2130
    }
2131

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2614

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

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

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

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

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

    
2717
        $charset = api_get_system_encoding();
2718
        $display_action_links_with_icons = false;
2719

    
2720
        if ($this->debug > 0) {
2721
            error_log('New LP - In learnpath::get_html_toc()', 0);
2722
        }
2723
        if (empty($toc_list)) {
2724
            $toc_list = $this->get_toc();
2725
        }
2726
        //echo $this->current;
2727
        //$parent = $this->items[$this->current]->get_parent();
2728
        //if (empty($parent)) { $parent = $this->ordered_items[$this->items[$this->current]->get_previous_index()]; }
2729
        $html = '<div id="scorm_title" class="scorm_title">' . Security::remove_XSS($this->get_name()) . '</div>';
2730
        // Build, display.
2731
        if ($is_allowed_to_edit) {
2732
            $gradebook = Security :: remove_XSS($_GET['gradebook']);
2733
            if ($this->get_lp_session_id() == api_get_session_id()) {
2734
                $html .= '<div id="actions_lp" class="actions_lp">';
2735
                if ($display_action_links_with_icons) {
2736
                    $html .= '<div class = "btn-group">';
2737
                    $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>";
2738
                    //$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>";
2739
                    //$html .= '<span>' . Display :: return_icon('view_remove_na.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</span><br />';
2740
                    $html .= '<a href="lp_controller.php?' . api_get_cidreq() . '">'. get_lang('ReturnToLPList') . '</a>';
2741
                    $html .= '</div>';
2742
                } else {
2743
                    $html .= '<div class="btn-group">';
2744
                    $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>";
2745
                    $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>";
2746
                    //$html .= '<span><b>' . get_lang('Display') . '</b></span><br />';
2747
                    $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>';
2748
                    $html .= '</div>';
2749
                }
2750
                $html .= '</div>';
2751
            }
2752

    
2753
        }
2754
        $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
2755
        require_once 'resourcelinker.inc.php';
2756

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

    
2777
            $style = 'scorm_item';
2778
            $scorm_color_background = 'scorm_item';
2779
            $style_item = 'scorm_item';
2780
            $current = false;
2781

    
2782
            if ($item['id'] == $this->current) {
2783
                $style = 'scorm_item_highlight';
2784
                $scorm_color_background = 'scorm_item_highlight';
2785
            } else {
2786

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

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

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

    
2813
            $title = Security::remove_XSS($title);
2814
            if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
2815
                //$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>" ;
2816
                $url = $this->get_link('http', $item['id'], $toc_list);
2817
                //$html .= '<a href="'.$url.'" target="content_name" onClick="top.load_item('.$item['id'].',\''.$url.'\');">'.$title.'</a>' ;
2818
                //$html .= '<a href="" onClick="top.load_item('.$item['id'].',\''.$url.'\');return false;">'.$title.'</a>' ;
2819

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

    
2828
            /*$tbl_track_e_exercises = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
2829
            $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
2830
            $user_id = api_get_user_id();
2831

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

    
2851
            $html .= "</div>";
2852

    
2853
            if ($scorm_color_background != '') {
2854
                $html .= '</div>';
2855
            }
2856

    
2857
            $color_counter++;
2858
        }
2859
        $html .= "</div>";
2860
        return $html;
2861
    }
2862

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

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

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

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

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

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

    
2933
        $file = '';
2934
        $lp_table 			= Database::get_course_table(TABLE_LP_MAIN);
2935
        $lp_item_table 		= Database::get_course_table(TABLE_LP_ITEM);
2936
        $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
2937
        $item_id 			= Database::escape_string($item_id);
2938

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

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

    
2972
            // 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.
2973
            if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
2974
                $lp_type = 1;
2975
            }
2976
            
2977
            if ($this->debug > 2) {
2978
                error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
2979
                error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
2980
            }
2981
                        
2982
            // Now go through the specific cases to get the end of the path.
2983

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

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

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

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

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

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

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

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

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

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

    
3204
        $course_id = api_get_course_int_id();
3205

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3672
        $session_id = api_get_session_id();
3673
        $session_condition = api_get_session_condition($session_id);
3674

    
3675
        $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
3676

    
3677
        $course_id = api_get_course_int_id();
3678

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

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

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

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

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

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

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

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

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

    
3881
        $course_id = api_get_course_int_id();
3882

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

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

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

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

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

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

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

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

    
4015
        if (!extension_loaded('xapian')) {
4016
            return false;
4017
        }
4018

    
4019
        $terms_string = trim($terms_string);
4020
        $terms = explode(',', $terms_string);
4021
        array_walk($terms, 'trim_value');
4022

    
4023
        $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
4024

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

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

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

    
4041
        while ($lp_item = Database :: fetch_array($result)) {
4042
            // Get search_did.
4043
            $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
4044
            $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';
4045
            $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
4046

    
4047
            //echo $sql; echo '<br>';
4048
            $res = Database::query($sql);
4049
            if (Database::num_rows($res) > 0) {
4050

    
4051
                $se_ref = Database :: fetch_array($res);
4052

    
4053
                // Compare terms.
4054
                $doc = $di->get_document($se_ref['search_did']);
4055

    
4056
                $xapian_terms = xapian_get_doc_terms($doc, $prefix);
4057

    
4058
                $xterms = array ();
4059
                foreach ($xapian_terms as $xapian_term) {
4060
                    $xterms[] = substr($xapian_term['name'], 1);
4061
                }
4062

    
4063
                $dterms = $terms;
4064

    
4065
                $missing_terms = array_diff($dterms, $xterms);
4066
                $deprecated_terms = array_diff($xterms, $dterms);
4067

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

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

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

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

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

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

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

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

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

    
4232

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

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

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

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

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

    
4307

    
4308

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
4778
                    $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
4779
                }
4780
            }
4781
        }
4782
    }
4783

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

    
4798
        if (count($array) > 0)
4799
            array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
4800

    
4801
        return $array;
4802
    }
4803

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

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

    
4821
        $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
4822

    
4823
        // we need to start a form when we want to update all the mp3 files
4824
        if ($update_audio == 'true') {
4825
            $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">';
4826
        }
4827
        $return .= '<div id="message"></div>';
4828

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

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

    
4845
        // We need to close the form when we are updating the mp3 files.
4846
        if ($update_audio == 'true') {
4847
            $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?
4848
        }
4849

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

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

    
4866
                /*Only process elements with an id attribute (in order to skip the blank,
4867
                    unmovable <li> elements.*/
4868

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

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

    
4886
        $(function() {
4887

    
4888
            $(".item_data").live("mouseover", function(event) {
4889
                $(".button_actions", this).show();
4890
            });
4891

    
4892
            $(".item_data").live("mouseout", function() {
4893
                $(".button_actions",this).hide();
4894
            });
4895

    
4896
            $(".button_actions").hide();
4897

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

    
4905
                start: function(event, ui) {
4906
                    $(ui.item).css("width", "160px");
4907
                    $(ui.item).find(".item_data").attr("style", "");
4908

    
4909
                },
4910
                stop: function(event, ui) {
4911
                    $(ui.item).css("width", "100%");
4912
                },
4913
            });
4914

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

    
4921
                update: function(event, ui) {
4922

    
4923
                    //Walk through the direct descendants of the lp_item_list <ul>
4924
                    $("#lp_item_list").children().each(function () {
4925

    
4926
                        /*Only process elements with an id attribute (in order to skip the blank,
4927
                        unmovable <li> elements.*/
4928

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

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

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

    
4952
                    var id = $(ui.item).attr("data_id");
4953
                    var type = $(ui.item).attr("data_type");
4954
                    var title = $(ui.item).attr("title");
4955

    
4956
                    if (ui.item.parent()[0]) {
4957
                        var parent_id = $(ui.item.parent()[0]).attr("id");
4958
                        var previous_id = $(ui.item.prev()).attr("id");
4959

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

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

    
4996
        $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
4997

    
4998
        $course_id = api_get_course_int_id();
4999
        $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
5000

    
5001
        $sql = "SELECT * FROM $tbl_lp_item
5002
                WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
5003

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

    
5027

    
5028
        $this->tree_array($arrLP);
5029
        $arrLP = $this->arrMenu;
5030
        unset ($this->arrMenu);
5031

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

    
5043
            $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
5044

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

    
5056
            // The audio column.
5057
            $return_audio  .= '<td align="center">';
5058

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

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

    
5097
                // No edit for this item types
5098
                if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
5099
                    if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
5100
                        $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'] . '">';
5101
                        $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
5102
                        $edit_icon .= '</a>';
5103
                    } else {
5104
                        $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'] . '">';
5105
                        $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
5106
                        $edit_icon .= '</a>';
5107
                    }
5108
                }
5109

    
5110
                $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) . '\');">';
5111
                $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
5112
                $delete_icon .= '</a>';
5113

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

    
5129
            $default_data[$arrLP[$i]['id']] = $row;
5130
            $default_content[$arrLP[$i]['id']] = $arrLP[$i];
5131

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

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

    
5180
        $return = '<div class="lp_tree well">';
5181

    
5182
        $return .= '<ul id="lp_item_list">';
5183
        $return .='<h4>'.$this->name.'</h4><br>';
5184

    
5185
        $tree = self::print_recursive($elements, $default_data, $default_content);
5186

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

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

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

    
5240
        $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
5241
        $return = '<div class="actions">';
5242

    
5243
        $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>';
5244

    
5245
        if ($is_allowed_to_edit) {
5246
        }
5247
        //$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>';
5248
        $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> ';
5249

    
5250
        $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>';
5251
//		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>';