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