Project

General

Profile

Feature #5253 ยป learnpath.class.php

Yoselyn Castillo, 15/08/2012 22:56

 
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 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

    
653
        while (Database :: num_rows($res_name)) {
654
            // There is already one such name, update the current one a bit.
655
            $i++;
656
            $name = $name . ' - ' . $i;
657
            $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
658
            //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
659
            $res_name = Database::query($check_name);
660
        }
661
        // New name does not exist yet; keep it.
662
        // Escape description.
663
        $description = learnpath :: escape_string(api_htmlentities($description, ENT_QUOTES, $charset)); // Kevin: added htmlentities().
664
        $type = 1;
665
        switch ($learnpath) {
666
            case 'guess':
667
                break;
668
            case 'dokeos':
669
            case 'chamilo':
670
                $type = 1;
671
                break;
672
            case 'aicc':
673
                break;
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
                $res_insert = Database::query($sql_insert);
694
                $id = Database :: insert_id();
695
                if ($id > 0) {
696
                    // Insert into item_property.
697
                    api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
698
                    return $id;
699
                }
700
                break;
701
        }
702
    }
703

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

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

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

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

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

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

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

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

    
870

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1644
        } else {
1645

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2127
        return $output;
2128
    }
2129

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2612

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3202
        $course_id = api_get_course_int_id();
3203

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3675
        $course_id = api_get_course_int_id();
3676

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

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

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

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

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

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

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

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

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

    
3879
        $course_id = api_get_course_int_id();
3880

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
4061
                $dterms = $terms;
4062

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

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

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

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

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

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

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

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

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

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

    
4230

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

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

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

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

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

    
4305

    
4306

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
4799
        return $array;
4800
    }
4801

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

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

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

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

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

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

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

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

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

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

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

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

    
4884
        $(function() {
4885

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

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

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

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

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

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

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

    
4919
                update: function(event, ui) {
4920

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

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

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

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

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

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

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

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

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

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

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

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

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

    
5025

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
5241
        $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>';
5242

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

    
5248
        $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>';
5249
//		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>';
5250
        
5251
        $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '&amp;updateaudio=true&qu