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

lp_ajax_initialize.php ➔ initialize_item()   F

Complexity

Conditions 16
Paths 4096

Size

Total Lines 148
Code Lines 100

Duplication

Lines 4
Ratio 2.7 %

Importance

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