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