| Conditions | 46 |
| Paths | 7 |
| Total Lines | 248 |
| Code Lines | 163 |
| 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 |
||
| 127 | public function calc_score($stud_id = null, $type = null) |
||
| 128 | { |
||
| 129 | $allowStats = api_get_configuration_value('allow_gradebook_stats'); |
||
| 130 | |||
| 131 | if ($allowStats) { |
||
| 132 | $link = $this->entity; |
||
| 133 | if (!empty($link)) { |
||
| 134 | $weight = $link->getScoreWeight(); |
||
| 135 | |||
| 136 | switch ($type) { |
||
| 137 | case 'best': |
||
| 138 | $bestResult = $link->getBestScore(); |
||
| 139 | $result = [$bestResult, $weight]; |
||
| 140 | |||
| 141 | return $result; |
||
| 142 | break; |
||
| 143 | case 'average': |
||
| 144 | $count = count($this->getStudentList()); |
||
| 145 | if (empty($count)) { |
||
| 146 | $result = [0, $weight]; |
||
| 147 | |||
| 148 | return $result; |
||
| 149 | } |
||
| 150 | $sumResult = array_sum($link->getUserScoreList()); |
||
| 151 | $result = [$sumResult / $count, $weight]; |
||
| 152 | |||
| 153 | return $result; |
||
| 154 | break; |
||
| 155 | case 'ranking': |
||
| 156 | return [null, null]; |
||
| 157 | break; |
||
| 158 | default: |
||
| 159 | if (!empty($stud_id)) { |
||
| 160 | $scoreList = $link->getUserScoreList(); |
||
| 161 | $result = [0, $weight]; |
||
| 162 | if (isset($scoreList[$stud_id])) { |
||
| 163 | $result = [$scoreList[$stud_id], $weight]; |
||
| 164 | } |
||
| 165 | |||
| 166 | return $result; |
||
| 167 | } else { |
||
| 168 | $studentCount = count($this->getStudentList()); |
||
| 169 | $sumResult = array_sum($link->getUserScoreList()); |
||
| 170 | $result = [$sumResult, $studentCount]; |
||
| 171 | } |
||
| 172 | |||
| 173 | return $result; |
||
| 174 | break; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | $tblStats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 180 | $tblHp = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES); |
||
| 181 | $tblDoc = Database::get_course_table(TABLE_DOCUMENT); |
||
| 182 | |||
| 183 | /* the following query should be similar (in conditions) to the one used |
||
| 184 | in exercise/exercise.php, look for note-query-exe-results marker*/ |
||
| 185 | $sessionId = $this->get_session_id(); |
||
| 186 | $courseId = $this->getCourseId(); |
||
| 187 | $exerciseData = $this->get_exercise_data(); |
||
| 188 | $exerciseId = isset($exerciseData['id']) ? (int) $exerciseData['id'] : 0; |
||
| 189 | $stud_id = (int) $stud_id; |
||
| 190 | |||
| 191 | if (empty($exerciseId)) { |
||
| 192 | return null; |
||
| 193 | } |
||
| 194 | |||
| 195 | $key = 'exercise_link_id:'. |
||
| 196 | $this->get_id(). |
||
| 197 | 'exerciseId:'.$exerciseId.'student:'.$stud_id.'session:'.$sessionId.'courseId:'.$courseId.'type:'.$type; |
||
| 198 | |||
| 199 | $useCache = api_get_configuration_value('gradebook_use_apcu_cache'); |
||
| 200 | $cacheAvailable = api_get_configuration_value('apc') && $useCache; |
||
| 201 | $cacheDriver = null; |
||
| 202 | if ($cacheAvailable) { |
||
| 203 | $cacheDriver = new \Doctrine\Common\Cache\ApcuCache(); |
||
| 204 | if ($cacheDriver->contains($key)) { |
||
| 205 | return $cacheDriver->fetch($key); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | $exercise = new Exercise($courseId); |
||
| 210 | $exercise->read($exerciseId); |
||
| 211 | |||
| 212 | if (!$this->is_hp) { |
||
| 213 | if (false == $exercise->exercise_was_added_in_lp) { |
||
| 214 | $sql = "SELECT * FROM $tblStats |
||
| 215 | WHERE |
||
| 216 | exe_exo_id = $exerciseId AND |
||
| 217 | orig_lp_id = 0 AND |
||
| 218 | orig_lp_item_id = 0 AND |
||
| 219 | status <> 'incomplete' AND |
||
| 220 | session_id = $sessionId AND |
||
| 221 | c_id = $courseId |
||
| 222 | "; |
||
| 223 | } else { |
||
| 224 | $lpCondition = null; |
||
| 225 | if (!empty($exercise->lpList)) { |
||
| 226 | //$lpId = $exercise->getLpBySession($sessionId); |
||
| 227 | $lpList = []; |
||
| 228 | foreach ($exercise->lpList as $lpData) { |
||
| 229 | if ((int) $lpData['session_id'] == $sessionId) { |
||
| 230 | $lpList[] = $lpData['lp_id']; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | $lpCondition = ' orig_lp_id = 0 OR (orig_lp_id IN ("'.implode('", "', $lpList).'")) AND '; |
||
| 234 | } |
||
| 235 | |||
| 236 | $sql = "SELECT * |
||
| 237 | FROM $tblStats |
||
| 238 | WHERE |
||
| 239 | exe_exo_id = $exerciseId AND |
||
| 240 | $lpCondition |
||
| 241 | status <> 'incomplete' AND |
||
| 242 | session_id = $sessionId AND |
||
| 243 | c_id = $courseId "; |
||
| 244 | } |
||
| 245 | |||
| 246 | if (!empty($stud_id) && 'ranking' !== $type) { |
||
| 247 | $sql .= " AND exe_user_id = $stud_id "; |
||
| 248 | } |
||
| 249 | $sql .= ' ORDER BY exe_id DESC'; |
||
| 250 | } else { |
||
| 251 | $sql = "SELECT * FROM $tblHp hp |
||
| 252 | INNER JOIN $tblDoc doc |
||
| 253 | ON (hp.exe_name = doc.path AND doc.c_id = hp.c_id) |
||
| 254 | WHERE |
||
| 255 | hp.c_id = $courseId AND |
||
| 256 | doc.id = $exerciseId"; |
||
| 257 | |||
| 258 | if (!empty($stud_id)) { |
||
| 259 | $sql .= " AND hp.exe_user_id = $stud_id "; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | $scores = Database::query($sql); |
||
| 264 | |||
| 265 | if (isset($stud_id) && empty($type)) { |
||
| 266 | // for 1 student |
||
| 267 | if ($data = Database::fetch_array($scores, 'ASSOC')) { |
||
| 268 | $attempts = Database::query($sql); |
||
| 269 | $counter = 0; |
||
| 270 | while ($attempt = Database::fetch_array($attempts)) { |
||
| 271 | $counter++; |
||
| 272 | } |
||
| 273 | $result = [$data['score'], $data['max_score'], $data['exe_date'], $counter]; |
||
| 274 | if ($cacheAvailable) { |
||
| 275 | $cacheDriver->save($key, $result); |
||
| 276 | } |
||
| 277 | |||
| 278 | return $result; |
||
| 279 | } else { |
||
| 280 | if ($cacheAvailable) { |
||
| 281 | $cacheDriver->save($key, null); |
||
| 282 | } |
||
| 283 | |||
| 284 | return null; |
||
| 285 | } |
||
| 286 | } else { |
||
| 287 | // all students -> get average |
||
| 288 | // normal way of getting the info |
||
| 289 | $students = []; // user list, needed to make sure we only |
||
| 290 | // take first attempts into account |
||
| 291 | $student_count = 0; |
||
| 292 | $sum = 0; |
||
| 293 | $bestResult = 0; |
||
| 294 | $weight = 0; |
||
| 295 | $sumResult = 0; |
||
| 296 | $studentList = $this->getStudentList(); |
||
| 297 | $studentIdList = []; |
||
| 298 | if (!empty($studentList)) { |
||
| 299 | $studentIdList = array_column($studentList, 'user_id'); |
||
| 300 | } |
||
| 301 | |||
| 302 | while ($data = Database::fetch_array($scores, 'ASSOC')) { |
||
| 303 | // Only take into account users in the current student list. |
||
| 304 | if (!empty($studentIdList)) { |
||
| 305 | if (!in_array($data['exe_user_id'], $studentIdList)) { |
||
| 306 | continue; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | if (!isset($students[$data['exe_user_id']])) { |
||
| 311 | if (0 != $data['max_score']) { |
||
| 312 | $students[$data['exe_user_id']] = $data['score']; |
||
| 313 | $student_count++; |
||
| 314 | if ($data['score'] > $bestResult) { |
||
| 315 | $bestResult = $data['score']; |
||
| 316 | } |
||
| 317 | $sum += $data['score'] / $data['max_score']; |
||
| 318 | $sumResult += $data['score']; |
||
| 319 | $weight = $data['max_score']; |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | if (0 == $student_count) { |
||
| 325 | if ($cacheAvailable) { |
||
| 326 | $cacheDriver->save($key, null); |
||
| 327 | } |
||
| 328 | |||
| 329 | return null; |
||
| 330 | } else { |
||
| 331 | switch ($type) { |
||
| 332 | case 'best': |
||
| 333 | $result = [$bestResult, $weight]; |
||
| 334 | if ($cacheAvailable) { |
||
| 335 | $cacheDriver->save($key, $result); |
||
| 336 | } |
||
| 337 | |||
| 338 | return $result; |
||
| 339 | break; |
||
| 340 | case 'average': |
||
| 341 | $count = count($this->getStudentList()); |
||
| 342 | if (empty($count)) { |
||
| 343 | $result = [0, $weight]; |
||
| 344 | if ($cacheAvailable) { |
||
| 345 | $cacheDriver->save($key, $result); |
||
| 346 | } |
||
| 347 | |||
| 348 | return $result; |
||
| 349 | } |
||
| 350 | |||
| 351 | $result = [$sumResult / $count, $weight]; |
||
| 352 | |||
| 353 | if ($cacheAvailable) { |
||
| 354 | $cacheDriver->save($key, $result); |
||
| 355 | } |
||
| 356 | |||
| 357 | return $result; |
||
| 358 | break; |
||
| 359 | case 'ranking': |
||
| 360 | $ranking = AbstractLink::getCurrentUserRanking($stud_id, $students); |
||
| 361 | if ($cacheAvailable) { |
||
| 362 | $cacheDriver->save($key, $ranking); |
||
| 363 | } |
||
| 364 | |||
| 365 | return $ranking; |
||
| 366 | break; |
||
| 367 | default: |
||
| 368 | $result = [$sum, $student_count]; |
||
| 369 | if ($cacheAvailable) { |
||
| 370 | $cacheDriver->save($key, $result); |
||
| 371 | } |
||
| 372 | |||
| 373 | return $result; |
||
| 374 | break; |
||
| 375 | } |
||
| 580 |