| Conditions | 27 |
| Paths | > 20000 |
| Total Lines | 174 |
| Code Lines | 109 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 129 | function generateHtmlForLearningPaths(User $student, array $courseInfo, int $sessionId): string |
||
| 130 | { |
||
| 131 | $html = Display::page_subheader2(get_lang('ToolLearnpath')); |
||
| 132 | |||
| 133 | $columnHeaders = []; |
||
| 134 | $columnHeaders['lp'] = get_lang('LearningPath'); |
||
| 135 | $columnHeaders['time'] = get_lang('Time'); |
||
| 136 | $columnHeaders['best_score'] = get_lang('BestScore'); |
||
| 137 | $columnHeaders['latest_attempt_avg_score'] = get_lang('LatestAttemptAverageScore'); |
||
| 138 | $columnHeaders['progress'] = get_lang('Progress'); |
||
| 139 | $columnHeaders['last_connection'] = get_lang('LastConnexion'); |
||
| 140 | |||
| 141 | $trackingColumns = api_get_configuration_value('tracking_columns'); |
||
| 142 | |||
| 143 | if (isset($trackingColumns['my_students_lp'])) { |
||
| 144 | foreach ($columnHeaders as $key => $value) { |
||
| 145 | if (!isset($trackingColumns['my_progress_lp'][$key]) || $trackingColumns['my_students_lp'][$key] == false) { |
||
| 146 | unset($columnHeaders[$key]); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | if (true === api_get_configuration_value('student_follow_page_add_LP_subscription_info')) { |
||
| 152 | $columnHeaders['student_follow_page_add_LP_subscription_info'] = get_lang('Unlock'); |
||
| 153 | } |
||
| 154 | |||
| 155 | if (true === api_get_configuration_value('student_follow_page_add_LP_acquisition_info')) { |
||
| 156 | $columnHeaders['student_follow_page_add_LP_acquisition_info'] = get_lang('Acquisition'); |
||
| 157 | } |
||
| 158 | |||
| 159 | $columnHeadersKeys = array_keys($columnHeaders); |
||
| 160 | |||
| 161 | $timeCourse = Tracking::minimumTimeAvailable($sessionId, $courseInfo['real_id']) |
||
| 162 | ? Tracking::getCalculateTime($student->getId(), $courseInfo['real_id'], $sessionId) |
||
| 163 | : null; |
||
| 164 | |||
| 165 | $lpCategories = learnpath::getCategories($courseInfo['real_id'], true); |
||
| 166 | |||
| 167 | /** @var CLpCategory $item */ |
||
| 168 | foreach ($lpCategories as $item) { |
||
| 169 | $categoryId = $item->getId(); |
||
| 170 | |||
| 171 | if (!learnpath::categoryIsVisibleForStudent($item, $student, $courseInfo['real_id'], $sessionId)) { |
||
| 172 | continue; |
||
| 173 | } |
||
| 174 | |||
| 175 | $lpList = new LearnpathList( |
||
| 176 | api_get_user_id(), |
||
| 177 | $courseInfo, |
||
| 178 | $sessionId, |
||
| 179 | null, |
||
| 180 | false, |
||
| 181 | $categoryId, |
||
| 182 | false, |
||
| 183 | true |
||
| 184 | ); |
||
| 185 | |||
| 186 | $flat_list = $lpList->get_flat_list(); |
||
| 187 | |||
| 188 | if (count($lpCategories) > 1) { |
||
| 189 | $html .= Display::page_subheader3($item->getName()); |
||
| 190 | } |
||
| 191 | |||
| 192 | $lpTable = [$columnHeaders]; |
||
| 193 | |||
| 194 | foreach ($flat_list as $learnpath) { |
||
| 195 | $lp_id = $learnpath['lp_old_id']; |
||
| 196 | |||
| 197 | $contentToExport = []; |
||
| 198 | |||
| 199 | if (in_array('lp', $columnHeadersKeys)) { |
||
| 200 | $contentToExport[] = api_html_entity_decode(stripslashes($learnpath['lp_name']), ENT_QUOTES); |
||
| 201 | } |
||
| 202 | |||
| 203 | if (in_array('time', $columnHeadersKeys)) { |
||
| 204 | // Get time in lp |
||
| 205 | if (!empty($timeCourse)) { |
||
| 206 | $lpTime = $timeCourse[TOOL_LEARNPATH] ?? 0; |
||
| 207 | $total_time = isset($lpTime[$lp_id]) ? (int) $lpTime[$lp_id] : 0; |
||
| 208 | } else { |
||
| 209 | $total_time = Tracking::get_time_spent_in_lp( |
||
| 210 | $student->getId(), |
||
| 211 | $courseInfo['code'], |
||
| 212 | [$lp_id], |
||
| 213 | $sessionId |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | $contentToExport[] = api_time_to_hms($total_time); |
||
| 218 | } |
||
| 219 | |||
| 220 | if (in_array('best_score', $columnHeadersKeys)) { |
||
| 221 | $bestScore = Tracking::get_avg_student_score( |
||
| 222 | $student->getId(), |
||
| 223 | $courseInfo['code'], |
||
| 224 | [$lp_id], |
||
| 225 | $sessionId, |
||
| 226 | false, |
||
| 227 | false, |
||
| 228 | true |
||
| 229 | ); |
||
| 230 | |||
| 231 | $contentToExport[] = empty($bestScore) ? '' : "$bestScore %"; |
||
| 232 | } |
||
| 233 | |||
| 234 | if (in_array('latest_attempt_avg_score', $columnHeadersKeys)) { |
||
| 235 | $score_latest = Tracking::get_avg_student_score( |
||
| 236 | $student->getId(), |
||
| 237 | $courseInfo['code'], |
||
| 238 | [$lp_id], |
||
| 239 | $sessionId, |
||
| 240 | false, |
||
| 241 | true |
||
| 242 | ); |
||
| 243 | |||
| 244 | if (isset($score_latest) && is_numeric($score_latest)) { |
||
| 245 | $score_latest = "$score_latest %"; |
||
| 246 | } |
||
| 247 | |||
| 248 | $contentToExport[] = $score_latest; |
||
| 249 | } |
||
| 250 | |||
| 251 | if (in_array('progress', $columnHeadersKeys)) { |
||
| 252 | $progress = Tracking::get_avg_student_progress( |
||
| 253 | $student->getId(), |
||
| 254 | $courseInfo['code'], |
||
| 255 | [$lp_id], |
||
| 256 | $sessionId |
||
| 257 | ); |
||
| 258 | |||
| 259 | $contentToExport[] = is_numeric($progress) ? "$progress %" : '0 %'; |
||
| 260 | } |
||
| 261 | |||
| 262 | if (in_array('last_connection', $columnHeadersKeys)) { |
||
| 263 | // Get last connection time in lp |
||
| 264 | $start_time = Tracking::get_last_connection_time_in_lp( |
||
| 265 | $student->getId(), |
||
| 266 | $courseInfo['code'], |
||
| 267 | $lp_id, |
||
| 268 | $sessionId |
||
| 269 | ); |
||
| 270 | |||
| 271 | $contentToExport[] = empty($start_time) |
||
| 272 | ? '-' |
||
| 273 | : api_convert_and_format_date($start_time, DATE_TIME_FORMAT_LONG); |
||
| 274 | } |
||
| 275 | |||
| 276 | if (in_array('student_follow_page_add_LP_subscription_info', $columnHeadersKeys)) { |
||
| 277 | $lpSubscription = StudentFollowPage::getLpSubscription( |
||
| 278 | $learnpath, |
||
| 279 | $student->getId(), |
||
| 280 | $courseInfo['real_id'], |
||
| 281 | $sessionId |
||
| 282 | ); |
||
| 283 | $contentToExport[] = strip_tags(str_replace('<br>', "\n", $lpSubscription)); |
||
| 284 | } |
||
| 285 | |||
| 286 | if (in_array('student_follow_page_add_LP_acquisition_info', $columnHeadersKeys)) { |
||
| 287 | $lpAcquisition = StudentFollowPage::getLpAcquisition( |
||
| 288 | $learnpath, |
||
| 289 | $student->getId(), |
||
| 290 | $courseInfo['real_id'], |
||
| 291 | $sessionId |
||
| 292 | ); |
||
| 293 | $contentToExport[] = strip_tags(str_replace('<br>', "\n", $lpAcquisition)); |
||
| 294 | } |
||
| 295 | |||
| 296 | $lpTable[] = $contentToExport; |
||
| 297 | } |
||
| 298 | |||
| 299 | $html .= Export::convert_array_to_html($lpTable); |
||
| 300 | } |
||
| 301 | |||
| 302 | return $html; |
||
| 303 | } |
||
| 495 |