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