| Conditions | 17 |
| Paths | 62 |
| Total Lines | 107 |
| Code Lines | 74 |
| 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 |
||
| 116 | public function calc_score($studentId = null, $type = null) |
||
| 117 | { |
||
| 118 | $threadInfo = $this->getThreadData(); |
||
| 119 | $thread_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY); |
||
| 120 | $sessionId = $this->get_session_id(); |
||
| 121 | $sessionCondition = api_get_session_condition( |
||
| 122 | $sessionId, |
||
| 123 | true, |
||
| 124 | false, |
||
| 125 | 'session_id' |
||
| 126 | ); |
||
| 127 | |||
| 128 | $sql = 'SELECT thread_qualify_max |
||
| 129 | FROM '.Database::get_course_table(TABLE_FORUM_THREAD)." |
||
| 130 | WHERE |
||
| 131 | c_id = ".$this->course_id." AND |
||
| 132 | iid = '".$this->get_ref_id()."' |
||
| 133 | $sessionCondition |
||
| 134 | "; |
||
| 135 | $query = Database::query($sql); |
||
| 136 | $assignment = Database::fetch_array($query); |
||
| 137 | |||
| 138 | $sql = "SELECT * FROM $thread_qualify |
||
| 139 | WHERE |
||
| 140 | c_id = ".$this->course_id." AND |
||
| 141 | iid = ".$this->get_ref_id()." |
||
| 142 | $sessionCondition |
||
| 143 | "; |
||
| 144 | if (isset($studentId)) { |
||
| 145 | $sql .= ' AND user_id = '.intval($studentId); |
||
| 146 | } |
||
| 147 | |||
| 148 | // order by id, that way the student's first attempt is accessed first |
||
| 149 | $sql .= ' ORDER BY qualify_time DESC'; |
||
| 150 | $scores = Database::query($sql); |
||
| 151 | |||
| 152 | // for 1 student |
||
| 153 | if (isset($studentId)) { |
||
| 154 | if (0 == $threadInfo['thread_peer_qualify']) { |
||
| 155 | // Classic way of calculate score |
||
| 156 | if ($data = Database::fetch_array($scores)) { |
||
| 157 | return [ |
||
| 158 | $data['qualify'], |
||
| 159 | $assignment['thread_qualify_max'], |
||
| 160 | ]; |
||
| 161 | } else { |
||
| 162 | // We sent the 0/thread_qualify_max instead of null for correct calculations |
||
| 163 | return [0, $assignment['thread_qualify_max']]; |
||
| 164 | } |
||
| 165 | } else { |
||
| 166 | // Take average |
||
| 167 | $score = 0; |
||
| 168 | $counter = 0; |
||
| 169 | if (Database::num_rows($scores)) { |
||
| 170 | while ($data = Database::fetch_array($scores, 'ASSOC')) { |
||
| 171 | $score += $data['qualify']; |
||
| 172 | $counter++; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | // If no result |
||
| 176 | if (empty($counter) || $counter <= 2) { |
||
| 177 | return [0, $assignment['thread_qualify_max']]; |
||
| 178 | } |
||
| 179 | |||
| 180 | return [$score / $counter, $assignment['thread_qualify_max']]; |
||
| 181 | } |
||
| 182 | } else { |
||
| 183 | // All students -> get average |
||
| 184 | $students = []; // user list, needed to make sure we only |
||
| 185 | // take first attempts into account |
||
| 186 | $counter = 0; |
||
| 187 | $sum = 0; |
||
| 188 | $bestResult = 0; |
||
| 189 | $weight = 0; |
||
| 190 | $sumResult = 0; |
||
| 191 | |||
| 192 | while ($data = Database::fetch_array($scores)) { |
||
| 193 | if (!(array_key_exists($data['user_id'], $students))) { |
||
| 194 | if (0 != $assignment['thread_qualify_max']) { |
||
| 195 | $students[$data['user_id']] = $data['qualify']; |
||
| 196 | $counter++; |
||
| 197 | $sum += $data['qualify'] / $assignment['thread_qualify_max']; |
||
| 198 | $sumResult += $data['qualify']; |
||
| 199 | if ($data['qualify'] > $bestResult) { |
||
| 200 | $bestResult = $data['qualify']; |
||
| 201 | } |
||
| 202 | $weight = $assignment['thread_qualify_max']; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | if (0 == $counter) { |
||
| 208 | return [null, null]; |
||
| 209 | } else { |
||
| 210 | switch ($type) { |
||
| 211 | case 'best': |
||
| 212 | return [$bestResult, $weight]; |
||
| 213 | break; |
||
|
|
|||
| 214 | case 'average': |
||
| 215 | return [$sumResult / $counter, $weight]; |
||
| 216 | break; |
||
| 217 | case 'ranking': |
||
| 218 | return AbstractLink::getCurrentUserRanking($studentId, $students); |
||
| 219 | break; |
||
| 220 | default: |
||
| 221 | return [$sum, $counter]; |
||
| 222 | break; |
||
| 223 | } |
||
| 367 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.