| Conditions | 12 |
| Paths | 88 |
| Total Lines | 66 |
| Code Lines | 47 |
| 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 |
||
| 81 | public function calc_score($stud_id = null, $type = null) |
||
| 82 | { |
||
| 83 | $tbl_stats = Database::get_course_table(TABLE_LP_VIEW); |
||
| 84 | $session_id = $this->get_session_id(); |
||
| 85 | if (empty($session_id)) { |
||
| 86 | $session_id = api_get_session_id(); |
||
| 87 | } |
||
| 88 | |||
| 89 | $sql = "SELECT * FROM $tbl_stats |
||
| 90 | WHERE |
||
| 91 | c_id = ".$this->course_id." AND |
||
| 92 | lp_id = ".$this->get_ref_id()." AND |
||
| 93 | session_id = $session_id "; |
||
| 94 | |||
| 95 | if (isset($stud_id)) { |
||
| 96 | $sql .= ' AND user_id = '.intval($stud_id); |
||
| 97 | } |
||
| 98 | |||
| 99 | // order by id, that way the student's first attempt is accessed first |
||
| 100 | $sql .= ' ORDER BY view_count DESC'; |
||
| 101 | |||
| 102 | $scores = Database::query($sql); |
||
| 103 | // for 1 student |
||
| 104 | if (isset($stud_id)) { |
||
| 105 | if ($data = Database::fetch_assoc($scores)) { |
||
| 106 | return [$data['progress'], 100]; |
||
| 107 | } else { |
||
| 108 | return null; |
||
| 109 | } |
||
| 110 | } else { |
||
| 111 | // all students -> get average |
||
| 112 | $students = []; // user list, needed to make sure we only |
||
| 113 | // take first attempts into account |
||
| 114 | $rescount = 0; |
||
| 115 | $sum = 0; |
||
| 116 | $bestResult = 0; |
||
| 117 | $sumResult = 0; |
||
| 118 | while ($data = Database::fetch_array($scores)) { |
||
| 119 | if (!(array_key_exists($data['user_id'], $students))) { |
||
| 120 | $students[$data['user_id']] = $data['progress']; |
||
| 121 | $rescount++; |
||
| 122 | $sum += $data['progress'] / 100; |
||
| 123 | $sumResult += $data['progress']; |
||
| 124 | |||
| 125 | if ($data['progress'] > $bestResult) { |
||
| 126 | $bestResult = $data['progress']; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($rescount == 0) { |
||
| 132 | return null; |
||
| 133 | } else { |
||
| 134 | switch ($type) { |
||
| 135 | case 'best': |
||
| 136 | return [$bestResult, 100]; |
||
| 137 | break; |
||
| 138 | case 'average': |
||
| 139 | return [$sumResult / $rescount, 100]; |
||
| 140 | break; |
||
| 141 | case 'ranking': |
||
| 142 | return AbstractLink::getCurrentUserRanking($stud_id, $students); |
||
| 143 | break; |
||
| 144 | default: |
||
| 145 | return [$sum, $rescount]; |
||
| 146 | break; |
||
| 147 | } |
||
| 260 |