| Conditions | 16 |
| Paths | 325 |
| Total Lines | 136 |
| Code Lines | 72 |
| 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 |
||
| 114 | public function calc_score($studentId = null, $type = null) |
||
| 115 | { |
||
| 116 | $studentId = (int) $studentId; |
||
| 117 | $em = Database::getManager(); |
||
| 118 | $assignment = $this->getStudentPublication(); |
||
| 119 | |||
| 120 | if (empty($assignment)) { |
||
| 121 | return []; |
||
| 122 | } |
||
| 123 | $session = api_get_session_entity($this->get_session_id()); |
||
| 124 | |||
| 125 | // @todo check session id / course id access |
||
| 126 | /*$id = $studentPublication->getIid(); |
||
| 127 | $assignment = Container::getStudentPublicationRepository() |
||
| 128 | ->findOneBy([ |
||
| 129 | 'cId' => $this->course_id, |
||
| 130 | 'iid' => $id, |
||
| 131 | 'session' => $session, |
||
| 132 | ]) |
||
| 133 | ; |
||
| 134 | $parentId = !$assignment ? 0 : $assignment->getId(); |
||
| 135 | */ |
||
| 136 | |||
| 137 | $parentId = $assignment->getIid(); |
||
| 138 | |||
| 139 | if (empty($session)) { |
||
| 140 | $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a |
||
| 141 | WHERE |
||
| 142 | a.cId = :course AND |
||
| 143 | a.active = :active AND |
||
| 144 | a.parentId = :parent AND |
||
| 145 | a.session is null AND |
||
| 146 | a.qualificatorId <> 0 |
||
| 147 | '; |
||
| 148 | |||
| 149 | $params = [ |
||
| 150 | 'course' => $this->course_id, |
||
| 151 | 'parent' => $parentId, |
||
| 152 | 'active' => true, |
||
| 153 | ]; |
||
| 154 | } else { |
||
| 155 | $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a |
||
| 156 | WHERE |
||
| 157 | a.cId = :course AND |
||
| 158 | a.active = :active AND |
||
| 159 | a.parentId = :parent AND |
||
| 160 | a.session = :session AND |
||
| 161 | a.qualificatorId <> 0 |
||
| 162 | '; |
||
| 163 | |||
| 164 | $params = [ |
||
| 165 | 'course' => $this->course_id, |
||
| 166 | 'parent' => $parentId, |
||
| 167 | 'session' => $session, |
||
| 168 | 'active' => true, |
||
| 169 | ]; |
||
| 170 | } |
||
| 171 | |||
| 172 | if (!empty($studentId)) { |
||
| 173 | $dql .= ' AND a.userId = :student '; |
||
| 174 | $params['student'] = $studentId; |
||
| 175 | } |
||
| 176 | |||
| 177 | $order = api_get_setting('student_publication_to_take_in_gradebook'); |
||
| 178 | |||
| 179 | switch ($order) { |
||
| 180 | case 'last': |
||
| 181 | // latest attempt |
||
| 182 | $dql .= ' ORDER BY a.sentDate DESC'; |
||
| 183 | break; |
||
| 184 | case 'first': |
||
| 185 | default: |
||
| 186 | // first attempt |
||
| 187 | $dql .= ' ORDER BY a.iid'; |
||
| 188 | break; |
||
| 189 | } |
||
| 190 | |||
| 191 | $scores = $em->createQuery($dql)->execute($params); |
||
| 192 | |||
| 193 | // for 1 student |
||
| 194 | if (!empty($studentId)) { |
||
| 195 | if (!count($scores)) { |
||
| 196 | return [null, null]; |
||
| 197 | } |
||
| 198 | |||
| 199 | $data = $scores[0]; |
||
| 200 | |||
| 201 | return [ |
||
| 202 | $data->getQualification(), |
||
| 203 | $assignment->getQualification(), |
||
| 204 | api_get_local_time($assignment->getDateOfQualification()), |
||
| 205 | 1, |
||
| 206 | ]; |
||
| 207 | } |
||
| 208 | |||
| 209 | $students = []; // user list, needed to make sure we only |
||
| 210 | // take first attempts into account |
||
| 211 | $rescount = 0; |
||
| 212 | $sum = 0; |
||
| 213 | $bestResult = 0; |
||
| 214 | $weight = 0; |
||
| 215 | $sumResult = 0; |
||
| 216 | |||
| 217 | foreach ($scores as $data) { |
||
| 218 | if (!(array_key_exists($data->getUserId(), $students))) { |
||
| 219 | if (0 != $assignment->getQualification()) { |
||
| 220 | $students[$data->getUserId()] = $data->getQualification(); |
||
| 221 | $rescount++; |
||
| 222 | $sum += $data->getQualification() / $assignment->getQualification(); |
||
| 223 | $sumResult += $data->getQualification(); |
||
| 224 | |||
| 225 | if ($data->getQualification() > $bestResult) { |
||
| 226 | $bestResult = $data->getQualification(); |
||
| 227 | } |
||
| 228 | $weight = $assignment->getQualification(); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | if (0 == $rescount) { |
||
| 234 | return [null, null]; |
||
| 235 | } |
||
| 236 | |||
| 237 | switch ($type) { |
||
| 238 | case 'best': |
||
| 239 | return [$bestResult, $weight]; |
||
| 240 | break; |
||
| 241 | case 'average': |
||
| 242 | return [$sumResult / $rescount, $weight]; |
||
| 243 | break; |
||
| 244 | case 'ranking': |
||
| 245 | return AbstractLink::getCurrentUserRanking($studentId, $students); |
||
| 246 | break; |
||
| 247 | default: |
||
| 248 | return [$sum, $rescount]; |
||
| 249 | break; |
||
| 250 | } |
||
| 358 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: