| Conditions | 43 |
| Paths | 7 |
| Total Lines | 244 |
| Code Lines | 156 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 177 | public function calc_score($stud_id = null, $type = null) |
||
| 178 | { |
||
| 179 | $allowStats = api_get_configuration_value('allow_gradebook_stats'); |
||
| 180 | |||
| 181 | if ($allowStats) { |
||
| 182 | $link = $this->entity; |
||
| 183 | if (!empty($link)) { |
||
| 184 | $weight = $link->getScoreWeight(); |
||
| 185 | |||
| 186 | switch ($type) { |
||
| 187 | case 'best': |
||
| 188 | $bestResult = $link->getBestScore(); |
||
| 189 | $result = [$bestResult, $weight]; |
||
| 190 | |||
| 191 | return $result; |
||
| 192 | break; |
||
| 193 | case 'average': |
||
| 194 | $count = count($this->getStudentList()); |
||
| 195 | if (empty($count)) { |
||
| 196 | $result = [0, $weight]; |
||
| 197 | |||
| 198 | return $result; |
||
| 199 | } |
||
| 200 | $sumResult = array_sum($link->getUserScoreList()); |
||
| 201 | $result = [$sumResult / $count, $weight]; |
||
| 202 | |||
| 203 | return $result; |
||
| 204 | break; |
||
| 205 | case 'ranking': |
||
| 206 | return ''; |
||
| 207 | /* |
||
| 208 | $newList = []; |
||
| 209 | $ranking = AbstractLink::getCurrentUserRanking($stud_id, $link->getUserScoreList()); |
||
| 210 | return $ranking;*/ |
||
| 211 | break; |
||
| 212 | default: |
||
| 213 | if (!empty($stud_id)) { |
||
| 214 | $scoreList = $link->getUserScoreList(); |
||
| 215 | $result = [0, $weight]; |
||
| 216 | if (isset($scoreList[$stud_id])) { |
||
| 217 | $result = [$scoreList[$stud_id], $weight]; |
||
| 218 | } |
||
| 219 | |||
| 220 | return $result; |
||
| 221 | } else { |
||
| 222 | $studentCount = count($this->getStudentList()); |
||
| 223 | $sumResult = array_sum($link->getUserScoreList()); |
||
| 224 | $result = [$sumResult, $studentCount]; |
||
| 225 | } |
||
| 226 | |||
| 227 | return $result; |
||
| 228 | break; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | $tblStats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 234 | $tblHp = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES); |
||
| 235 | $tblDoc = Database::get_course_table(TABLE_DOCUMENT); |
||
| 236 | |||
| 237 | /* the following query should be similar (in conditions) to the one used |
||
| 238 | in exercise/exercise.php, look for note-query-exe-results marker*/ |
||
| 239 | $sessionId = $this->get_session_id(); |
||
| 240 | $courseId = $this->getCourseId(); |
||
| 241 | $exerciseData = $this->get_exercise_data(); |
||
| 242 | |||
| 243 | $exerciseId = isset($exerciseData['id']) ? $exerciseData['id'] : 0; |
||
| 244 | $stud_id = (int) $stud_id; |
||
| 245 | |||
| 246 | if (empty($exerciseId)) { |
||
| 247 | return null; |
||
| 248 | } |
||
| 249 | |||
| 250 | $key = 'exercise_link_id:'. |
||
| 251 | $this->get_id(). |
||
| 252 | 'exerciseId:'.$exerciseId.'student:'.$stud_id.'session:'.$sessionId.'courseId:'.$courseId.'type:'.$type; |
||
| 253 | |||
| 254 | $useCache = api_get_configuration_value('gradebook_use_apcu_cache'); |
||
| 255 | $cacheAvailable = api_get_configuration_value('apc') && $useCache; |
||
| 256 | $cacheDriver = null; |
||
| 257 | if ($cacheAvailable) { |
||
| 258 | $cacheDriver = new \Doctrine\Common\Cache\ApcuCache(); |
||
| 259 | if ($cacheDriver->contains($key)) { |
||
| 260 | return $cacheDriver->fetch($key); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | $exercise = new Exercise($courseId); |
||
| 265 | $exercise->read($exerciseData['id']); |
||
| 266 | |||
| 267 | if (!$this->is_hp) { |
||
| 268 | if ($exercise->exercise_was_added_in_lp == false) { |
||
| 269 | $sql = "SELECT * FROM $tblStats |
||
| 270 | WHERE |
||
| 271 | exe_exo_id = $exerciseId AND |
||
| 272 | orig_lp_id = 0 AND |
||
| 273 | orig_lp_item_id = 0 AND |
||
| 274 | status <> 'incomplete' AND |
||
| 275 | session_id = $sessionId AND |
||
| 276 | c_id = $courseId |
||
| 277 | "; |
||
| 278 | } else { |
||
| 279 | $lpId = null; |
||
| 280 | if (!empty($exercise->lpList)) { |
||
| 281 | // Taking only the first LP |
||
| 282 | $lpId = current($exercise->lpList); |
||
| 283 | $lpId = $lpId['lp_id']; |
||
| 284 | } |
||
| 285 | |||
| 286 | $sql = "SELECT * |
||
| 287 | FROM $tblStats |
||
| 288 | WHERE |
||
| 289 | exe_exo_id = $exerciseId AND |
||
| 290 | orig_lp_id = $lpId AND |
||
| 291 | status <> 'incomplete' AND |
||
| 292 | session_id = $sessionId AND |
||
| 293 | c_id = $courseId "; |
||
| 294 | } |
||
| 295 | |||
| 296 | if (!empty($stud_id) && $type != 'ranking') { |
||
| 297 | $sql .= " AND exe_user_id = $stud_id "; |
||
| 298 | } |
||
| 299 | $sql .= ' ORDER BY exe_id DESC'; |
||
| 300 | } else { |
||
| 301 | $sql = "SELECT * FROM $tblHp hp |
||
| 302 | INNER JOIN $tblDoc doc |
||
| 303 | ON (hp.exe_name = doc.path AND doc.c_id = hp.c_id) |
||
| 304 | WHERE |
||
| 305 | hp.c_id = $courseId AND |
||
| 306 | doc.id = ".$exerciseId; |
||
| 307 | |||
| 308 | if (!empty($stud_id)) { |
||
| 309 | $sql .= " AND hp.exe_user_id = $stud_id "; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | $scores = Database::query($sql); |
||
| 314 | |||
| 315 | if (isset($stud_id) && empty($type)) { |
||
| 316 | // for 1 student |
||
| 317 | if ($data = Database::fetch_array($scores)) { |
||
| 318 | $result = [$data['score'], $data['max_score']]; |
||
| 319 | if ($cacheAvailable) { |
||
| 320 | $cacheDriver->save($key, $result); |
||
| 321 | } |
||
| 322 | |||
| 323 | return $result; |
||
| 324 | } else { |
||
| 325 | if ($cacheAvailable) { |
||
| 326 | $cacheDriver->save($key, null); |
||
| 327 | } |
||
| 328 | |||
| 329 | return null; |
||
| 330 | } |
||
| 331 | } else { |
||
| 332 | // all students -> get average |
||
| 333 | // normal way of getting the info |
||
| 334 | $students = []; // user list, needed to make sure we only |
||
| 335 | // take first attempts into account |
||
| 336 | $student_count = 0; |
||
| 337 | $sum = 0; |
||
| 338 | $bestResult = 0; |
||
| 339 | $weight = 0; |
||
| 340 | $sumResult = 0; |
||
| 341 | |||
| 342 | $studentList = $this->getStudentList(); |
||
| 343 | $studentIdList = []; |
||
| 344 | if (!empty($studentList)) { |
||
| 345 | $studentIdList = array_column($studentList, 'user_id'); |
||
| 346 | } |
||
| 347 | |||
| 348 | while ($data = Database::fetch_array($scores, 'ASSOC')) { |
||
| 349 | // Only take into account users in the current student list. |
||
| 350 | if (!empty($studentIdList)) { |
||
| 351 | if (!in_array($data['exe_user_id'], $studentIdList)) { |
||
| 352 | continue; |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | if (!isset($students[$data['exe_user_id']])) { |
||
| 357 | if ($data['max_score'] != 0) { |
||
| 358 | $students[$data['exe_user_id']] = $data['score']; |
||
| 359 | $student_count++; |
||
| 360 | if ($data['score'] > $bestResult) { |
||
| 361 | $bestResult = $data['score']; |
||
| 362 | } |
||
| 363 | $sum += $data['score'] / $data['max_score']; |
||
| 364 | $sumResult += $data['score']; |
||
| 365 | $weight = $data['max_score']; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | if ($student_count == 0) { |
||
| 371 | if ($cacheAvailable) { |
||
| 372 | $cacheDriver->save($key, null); |
||
| 373 | } |
||
| 374 | |||
| 375 | return null; |
||
| 376 | } else { |
||
| 377 | switch ($type) { |
||
| 378 | case 'best': |
||
| 379 | $result = [$bestResult, $weight]; |
||
| 380 | if ($cacheAvailable) { |
||
| 381 | $cacheDriver->save($key, $result); |
||
| 382 | } |
||
| 383 | |||
| 384 | return $result; |
||
| 385 | break; |
||
| 386 | case 'average': |
||
| 387 | $count = count($this->getStudentList()); |
||
| 388 | if (empty($count)) { |
||
| 389 | $result = [0, $weight]; |
||
| 390 | if ($cacheAvailable) { |
||
| 391 | $cacheDriver->save($key, $result); |
||
| 392 | } |
||
| 393 | |||
| 394 | return $result; |
||
| 395 | } |
||
| 396 | |||
| 397 | $result = [$sumResult / $count, $weight]; |
||
| 398 | |||
| 399 | if ($cacheAvailable) { |
||
| 400 | $cacheDriver->save($key, $result); |
||
| 401 | } |
||
| 402 | |||
| 403 | return $result; |
||
| 404 | break; |
||
| 405 | case 'ranking': |
||
| 406 | $ranking = AbstractLink::getCurrentUserRanking($stud_id, $students); |
||
| 407 | if ($cacheAvailable) { |
||
| 408 | $cacheDriver->save($key, $ranking); |
||
| 409 | } |
||
| 410 | |||
| 411 | return $ranking; |
||
| 412 | break; |
||
| 413 | default: |
||
| 414 | $result = [$sum, $student_count]; |
||
| 415 | if ($cacheAvailable) { |
||
| 416 | $cacheDriver->save($key, $result); |
||
| 417 | } |
||
| 418 | |||
| 419 | return $result; |
||
| 420 | break; |
||
| 421 | } |
||
| 632 |