Completed
Push — master ( 17e47f...c8c08e )
by Julito
65:46 queued 26:40
created

lp_ajax_switch_item_toc.php ➔ switch_item_toc()   F

Complexity

Conditions 16
Paths 528

Size

Total Lines 138
Code Lines 95

Duplication

Lines 51
Ratio 36.96 %

Importance

Changes 0
Metric Value
cc 16
eloc 95
nc 528
nop 5
dl 51
loc 138
rs 2.848
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use ChamiloSession as Session;
5
6
/**
7
 * This script contains the server part of the ajax interaction process. The client part is located
8
 * in lp_api.php or other api's.
9
 * This script updated the TOC of the SCORM without updating the SCO's attributes
10
 * @package chamilo.learnpath
11
 * @author Yannick Warnier <[email protected]>
12
 */
13
14
// Flag to allow for anonymous user - needs to be set before global.inc.php.
15
$use_anonymous = true;
16
//require_once '../inc/global.inc.php';
17
18
/**
19
 * Get one item's details
20
 * @param   integer $lpId LP ID
21
 * @param   integer $userId user ID
22
 * @param   integer $viewId View ID
23
 * @param   integer $currentItem Current item ID
24
 * @param   integer $nextItem New item ID
25
 * @return  string  JavaScript commands to be executed in scorm_api.php
26
 */
27
function switch_item_toc($lpId, $userId, $viewId, $currentItem, $nextItem)
28
{
29
    $debug = 0;
30
    $return = '';
31
    if ($debug > 0) {
32
        error_log('In switch_item_toc('.$lpId.','.$userId.','.$viewId.','.$currentItem.','.$nextItem.')', 0);
33
    }
34
    $myLP = learnpath::getLpFromSession(api_get_course_id(), $lpId, $userId);
35
    $newItemId = 0;
36
    $oldItemId = 0;
37 View Code Duplication
    switch ($nextItem) {
38
        case 'next':
39
            $myLP->set_current_item($currentItem);
40
            $myLP->next();
41
            $newItemId = $myLP->get_current_item_id();
42
            if ($debug > 1) {
43
                error_log('In {next} - next item is '.$newItemId.'(current: '.$currentItem.')', 0);
44
            }
45
            break;
46
        case 'previous':
47
            $myLP->set_current_item($currentItem);
48
            $myLP->previous();
49
            $newItemId = $myLP->get_current_item_id();
50
            if ($debug > 1) {
51
                error_log('In {previous} - next item is '.$newItemId.'(current: '.$currentItem.')', 0);
52
            }
53
            break;
54
        case 'first':
55
            $myLP->set_current_item($currentItem);
56
            $myLP->first();
57
            $newItemId = $myLP->get_current_item_id();
58
            if ($debug > 1) {
59
                error_log('In {first} - next item is '.$newItemId.'(current: '.$currentItem.')', 0);
60
            }
61
            break;
62
        case 'last':
63
            break;
64
        default:
65
            // Should be filtered to check it's not hacked
66
            if ($nextItem == $currentItem) {
67
                // If we're opening the same item again.
68
                $myLP->items[$currentItem]->restart();
69
            } else {
70
                $oldItemId = $currentItem;
71
            }
72
            $newItemId = $nextItem;
73
            $myLP->set_current_item($newItemId);
74
            if ($debug > 1) {
75
                error_log('In {default} - next item is '.$newItemId.'(current: '.$currentItem.')', 0);
76
            }
77
            break;
78
    }
79
    $myLP->start_current_item(true);
80
    if ($myLP->force_commit) {
81
        $myLP->save_current();
82
    }
83 View Code Duplication
    if (is_object($myLP->items[$newItemId])) {
84
        $myLPI = $myLP->items[$newItemId];
85
    } else {
86
        if ($debug > 1) {
87
            error_log('In switch_item_details - generating new item object', 0);
88
        }
89
        $myLPI = new learnpathItem($newItemId, $userId);
90
        $myLPI->set_lp_view($viewId);
91
    }
92
    /*
93
     * now get what's needed by the SCORM API:
94
     * -score
95
     * -max
96
     * -min
97
     * -lesson_status
98
     * -session_time
99
     * -suspend_data
100
     */
101
    $lessonStatus = $myLPI->get_status();
102
    $interactionsCount = $myLPI->get_interactions_count();
103
    /**
104
     * Interactions are not returned by switch_item at the moment, but please
105
     * leave commented code to allow for the addition of these in the future
106
     */
107
    /*
108
    $interactionsString = '';
109
    for ($i = 0; $i < $interactionsCount; $i++) {
110
        $interactionsString .= ",[".$i.",'','','','','','','']";
111
    }
112
    if (!empty($interactionsString)) {
113
        $interactionsString = substr($interactionsString, 1);
114
    }
115
    */
116
    $totalItems = $myLP->get_total_items_count_without_chapters();
117
    $completedItems = $myLP->get_complete_items_count();
118
    $progressMode = $myLP->get_progress_bar_mode();
119
    $progressMode = ($progressMode == '' ? '%' : $progressMode);
120
    $nextItemId = $myLP->get_next_item_id();
121
    $previousItemId = $myLP->get_previous_item_id();
122
    $itemType = $myLPI->get_type();
123
    $lessonMode = $myLPI->get_lesson_mode();
124
    $credit = $myLPI->get_credit();
125
    $launchData = $myLPI->get_launch_data();
126
    $objectivesCount = $myLPI->get_objectives_count();
127
    $coreExit = $myLPI->get_core_exit();
128
129
    $return .=
130
        "olms.lms_lp_id=".$lpId.";" .
131
        "olms.lms_item_id=".$newItemId.";" .
132
        "olms.lms_old_item_id=".$oldItemId.";" .
133
        "olms.lms_initialized=0;" .
134
        "olms.lms_view_id=".$viewId.";" .
135
        "olms.lms_user_id=".$userId.";" .
136
        "olms.next_item=".$newItemId.";" . // This one is very important to replace possible literal strings.
137
        "olms.lms_next_item=".$nextItemId.";" .
138
        "olms.lms_previous_item=".$previousItemId.";" .
139
        "olms.lms_item_type = '".$itemType."';" .
140
        "olms.lms_item_credit = '".$credit."';" .
141
        "olms.lms_item_lesson_mode = '".$lessonMode."';" .
142
        "olms.lms_item_launch_data = '".$launchData."';" .
143
        "olms.lms_item_interactions_count = '".$interactionsCount."';" .
144
        "olms.lms_item_objectives_count = '".$objectivesCount."';" .
145
        "olms.lms_item_core_exit = '".$coreExit."';" .
146
        "olms.asset_timer = 0;";
147
148
    $return .= "update_toc('unhighlight','".$currentItem."');".
149
        "update_toc('highlight','".$newItemId."');".
150
        "update_toc('$lessonStatus','".$newItemId."');".
151
        "update_progress_bar('$completedItems','$totalItems','$progressMode');";
152
153
    $myLP->set_error_msg('');
154
    $myLP->prerequisites_match(); // Check the prerequisites are all complete.
155
    if ($debug > 1) {
156
        error_log('prerequisites_match() returned '.htmlentities($myLP->error), 0);
157
    }
158
    //$_SESSION['scorm_item_id'] = $newItemId; // Save the new item ID for the exercise tool to use.
159
160
    Session::write('lpobject', serialize($myLP));
161
    Session::write('scorm_item_id', $newItemId);
162
163
    return $return;
164
}
165
166
echo switch_item_toc(
167
    $_POST['lid'],
168
    $_POST['uid'],
169
    $_POST['vid'],
170
    $_POST['iid'],
171
    $_POST['next']
172
);
173