Passed
Push — master ( 4791a8...56927d )
by Julito
09:52
created

initializeItem()   F

Complexity

Conditions 14
Paths 2048

Size

Total Lines 154
Code Lines 104

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 104
nc 2048
nop 5
dl 0
loc 154
rs 1.68
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
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Framework\Container;
6
use Chamilo\CourseBundle\Entity\CLp;
7
8
/**
9
 * This script contains the server part of the xajax interaction process.
10
 * This script, in particular, enables the process of SCO's initialization. It
11
 * resets the JavaScript values for each SCO to the current LMS status.
12
 *
13
 * @author Yannick Warnier <[email protected]>
14
 */
15
16
// Flag to allow for anonymous user - needs to be set before global.inc.php
17
$use_anonymous = true;
18
require_once __DIR__.'/../inc/global.inc.php';
19
api_protect_course_script();
20
21
/**
22
 * Get one item's details.
23
 *
24
 * @param   int LP ID
25
 * @param   int user ID
26
 * @param   int View ID
27
 * @param   int Current item ID
28
 * @param   int New item ID
29
 *
30
 * @return string
31
 */
32
function initializeItem($lpId, $user_id, $view_id, $next_item, $startTime = 0)
33
{
34
    $debug = 0;
35
    $return = '';
36
    if ($debug) {
37
        error_log('In initialize_item('.$lpId.','.$user_id.','.$view_id.','.$next_item.')');
38
    }
39
    /*$item_id may be one of:
40
     * -'next'
41
     * -'previous'
42
     * -'first'
43
     * -'last'
44
     * - a real item ID
45
     */
46
    $mylp = learnpath::getLpFromSession(api_get_course_id(), $lpId, $user_id);
47
    $mylp->set_current_item($next_item);
48
    if ($debug) {
49
        error_log('In initialize_item() - new item is '.$next_item);
50
    }
51
    $mylp->start_current_item(true);
52
    if (is_object($mylp->items[$next_item])) {
53
        if ($debug) {
54
            error_log('In initialize_item - recovering existing item object '.$next_item, 0);
55
        }
56
        $mylpi = $mylp->items[$next_item];
57
    } else {
58
        if ($debug) {
59
            error_log('In initialize_item - generating new item object '.$next_item, 0);
60
        }
61
        $mylpi = new learnpathItem($next_item, $user_id);
62
    }
63
64
    if ($mylpi) {
0 ignored issues
show
introduced by
$mylpi is of type learnpathItem, thus it always evaluated to true.
Loading history...
65
        $mylpi->set_lp_view($view_id);
66
    }
67
68
    /*
69
     * now get what's needed by the SCORM API:
70
     * -score
71
     * -max
72
     * -min
73
     * -lesson_status
74
     * -session_time
75
     * -suspend_data
76
     */
77
    $myscore = $mylpi->get_score();
78
    $mymax = $mylpi->get_max();
79
    if ('' === $mymax) {
80
        $mymax = "''";
81
    }
82
    $mymin = $mylpi->get_min();
83
    $mylesson_status = $mylpi->get_status();
84
    $mytotal_time = $mylpi->get_scorm_time('js', null, true);
85
    $mymastery_score = $mylpi->get_mastery_score();
86
    $mymax_time_allowed = $mylpi->get_max_time_allowed();
87
    $mylaunch_data = $mylpi->get_launch_data();
88
    $mysession_time = $mylpi->get_total_time();
89
    $mysuspend_data = $mylpi->get_suspend_data();
90
    $mylesson_location = $mylpi->get_lesson_location();
91
    $myic = $mylpi->get_interactions_count();
92
    $myistring = '';
93
    for ($i = 0; $i < $myic; $i++) {
94
        $myistring .= ",[".$i.",'','','','','','','']";
95
    }
96
    if (!empty($myistring)) {
97
        $myistring = substr($myistring, 1);
98
    }
99
100
    $mycoursedb = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
101
    $course_id = api_get_course_int_id();
102
    $mylp_iv_id = $mylpi->db_item_view_id;
103
    $phpobjectives = [];
104
    if (!empty($mylp_iv_id)) {
105
        $sql = "SELECT objective_id, status, score_raw, score_max, score_min
106
                FROM $mycoursedb
107
                WHERE lp_iv_id = $mylp_iv_id AND c_id = $course_id
108
                ORDER BY iid ASC;";
109
        $res = Database::query($sql);
110
        while ($row = Database::fetch_row($res)) {
111
            $phpobjectives[] = $row;
112
        }
113
    }
114
    $myobjectives = json_encode($phpobjectives);
115
    $return .=
116
            "olms.score=".$myscore.";".
117
            "olms.max=".$mymax.";".
118
            "olms.min=".$mymin.";".
119
            "olms.lesson_status='".$mylesson_status."';".
120
            "olms.lesson_location='".$mylesson_location."';".
121
            "olms.session_time='".$mysession_time."';".
122
            "olms.suspend_data='".$mysuspend_data."';".
123
            "olms.total_time = '".$mytotal_time."';".
124
            "olms.mastery_score = '".$mymastery_score."';".
125
            "olms.max_time_allowed = '".$mymax_time_allowed."';".
126
            "olms.launch_data = '".$mylaunch_data."';".
127
            "olms.interactions = new Array(".$myistring.");".
128
            //"olms.item_objectives = new Array();" .
129
            "olms.item_objectives = ".$myobjectives.";".
130
            "olms.G_lastError = 0;".
131
            "olms.G_LastErrorMessage = 'No error';".
132
            "olms.finishSignalReceived = 0;";
133
    /*
134
     * and re-initialise the rest (proper to the LMS)
135
     * -lms_lp_id
136
     * -lms_item_id
137
     * -lms_old_item_id
138
     * -lms_new_item_id
139
     * -lms_initialized
140
     * -lms_progress_bar_mode
141
     * -lms_view_id
142
     * -lms_user_id
143
     */
144
    $mynext = $mylp->get_next_item_id();
145
    $myprevious = $mylp->get_previous_item_id();
146
    $myitemtype = $mylpi->get_type();
147
    $mylesson_mode = $mylpi->get_lesson_mode();
148
    $mycredit = $mylpi->get_credit();
149
    $mylaunch_data = $mylpi->get_launch_data();
150
    $myinteractions_count = $mylpi->get_interactions_count();
151
    $mycore_exit = $mylpi->get_core_exit();
152
    $return .=
153
            "olms.lms_lp_id=".$lpId.";".
154
            "olms.lms_item_id=".$next_item.";".
155
            "olms.lms_old_item_id=0;".
156
            "olms.lms_initialized=0;".
157
            "olms.lms_view_id=".$view_id.";".
158
            "olms.lms_user_id=".$user_id.";".
159
            "olms.next_item=".$next_item.";".// This one is very important to replace possible literal strings.
160
            "olms.lms_next_item=".$mynext.";".
161
            "olms.lms_previous_item=".$myprevious.";".
162
            "olms.lms_item_type = '".$myitemtype."';".
163
            "olms.lms_item_credit = '".$mycredit."';".
164
            "olms.lms_item_lesson_mode = '".$mylesson_mode."';".
165
            "olms.lms_item_launch_data = '".$mylaunch_data."';".
166
            "olms.lms_item_interactions_count = '".$myinteractions_count."';".
167
            "olms.lms_item_objectives_count = '".$myinteractions_count."';".
168
            "olms.lms_item_core_exit = '".$mycore_exit."';".
169
            "olms.asset_timer = 0;";
170
171
    $mylp->set_error_msg('');
172
    $mylp->prerequisites_match(); // Check the prerequisites are all complete.
173
    $startTime = (int) $startTime;
174
    if (1 === $startTime) {
175
        $now = time();
176
        $return .= "updateTimer($now);";
177
    }
178
179
    if ($debug) {
180
        error_log('Prereq_match() returned '.htmlentities($mylp->error), 0);
181
        error_log("return = $return ");
182
        error_log("mylp->lp_view_session_id: ".$mylp->lp_view_session_id);
183
    }
184
185
    return $return;
186
}
187
188
echo initializeItem(
189
    $_REQUEST['lid'],
190
    $_REQUEST['uid'],
191
    $_REQUEST['vid'],
192
    $_REQUEST['iid'],
193
    $_REQUEST['start_time'] ?? 0
194
);
195