| Conditions | 12 |
| Paths | 54 |
| Total Lines | 74 |
| Code Lines | 54 |
| 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 |
||
| 97 | public function calc_score($stud_id = null, $type = null) |
||
| 98 | { |
||
| 99 | $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT); |
||
| 100 | $session_id = api_get_session_id(); |
||
| 101 | |||
| 102 | // get attendance qualify max |
||
| 103 | $sql = 'SELECT att.attendance_qualify_max |
||
| 104 | FROM '.$this->get_attendance_table().' att |
||
| 105 | WHERE |
||
| 106 | att.c_id = '.$this->course_id.' AND |
||
| 107 | att.id = '.intval($this->get_ref_id()).' AND |
||
| 108 | att.session_id='.intval($session_id).''; |
||
| 109 | $query = Database::query($sql); |
||
| 110 | $attendance = Database::fetch_array($query, 'ASSOC'); |
||
| 111 | |||
| 112 | // Get results |
||
| 113 | $sql = 'SELECT * |
||
| 114 | FROM '.$tbl_attendance_result.' |
||
| 115 | WHERE c_id = '.$this->course_id.' AND attendance_id = '.intval($this->get_ref_id()); |
||
| 116 | if (isset($stud_id)) { |
||
| 117 | $sql .= ' AND user_id = '.intval($stud_id); |
||
| 118 | } |
||
| 119 | $scores = Database::query($sql); |
||
| 120 | // for 1 student |
||
| 121 | if (isset($stud_id)) { |
||
| 122 | if ($data = Database::fetch_array($scores, 'ASSOC')) { |
||
| 123 | return [ |
||
| 124 | $data['score'], |
||
| 125 | $attendance['attendance_qualify_max'], |
||
| 126 | ]; |
||
| 127 | } else { |
||
| 128 | //We sent the 0/attendance_qualify_max instead of null for correct calculations |
||
| 129 | return [0, $attendance['attendance_qualify_max']]; |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | // all students -> get average |
||
| 133 | $students = []; // user list, needed to make sure we only |
||
| 134 | // take first attempts into account |
||
| 135 | $rescount = 0; |
||
| 136 | $sum = 0; |
||
| 137 | $sumResult = 0; |
||
| 138 | $bestResult = 0; |
||
| 139 | |||
| 140 | while ($data = Database::fetch_array($scores)) { |
||
| 141 | if (!(array_key_exists($data['user_id'], $students))) { |
||
| 142 | if ($attendance['attendance_qualify_max'] != 0) { |
||
| 143 | $students[$data['user_id']] = $data['score']; |
||
| 144 | $rescount++; |
||
| 145 | $sum += $data['score'] / $attendance['attendance_qualify_max']; |
||
| 146 | $sumResult += $data['score']; |
||
| 147 | if ($data['score'] > $bestResult) { |
||
| 148 | $bestResult = $data['score']; |
||
| 149 | } |
||
| 150 | $weight = $attendance['attendance_qualify_max']; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | if ($rescount == 0) { |
||
| 156 | return null; |
||
| 157 | } else { |
||
| 158 | switch ($type) { |
||
| 159 | case 'best': |
||
| 160 | return [$bestResult, $weight]; |
||
| 161 | break; |
||
| 162 | case 'average': |
||
| 163 | return [$sumResult / $rescount, $weight]; |
||
| 164 | break; |
||
| 165 | case 'ranking': |
||
| 166 | return AbstractLink::getCurrentUserRanking($stud_id, $students); |
||
| 167 | break; |
||
| 168 | default: |
||
| 169 | return [$sum, $rescount]; |
||
| 170 | break; |
||
| 171 | } |
||
| 279 |