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