| Total Complexity | 40 |
| Total Lines | 343 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like StudentPublicationLink often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StudentPublicationLink, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class StudentPublicationLink extends AbstractLink |
||
| 14 | { |
||
| 15 | private $studpub_table; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Constructor. |
||
| 19 | */ |
||
| 20 | public function __construct() |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @return string |
||
| 28 | */ |
||
| 29 | public function get_type_name() |
||
| 30 | { |
||
| 31 | return get_lang('Assignments'); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function is_allowed_to_change_name() |
||
| 35 | { |
||
| 36 | return false; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Generate an array of all exercises available. |
||
| 41 | * |
||
| 42 | * @return array 2-dimensional array - every element contains 2 subelements (id, name) |
||
| 43 | */ |
||
| 44 | public function get_all_links() |
||
| 45 | { |
||
| 46 | if (empty($this->course_code)) { |
||
| 47 | return []; |
||
| 48 | } |
||
| 49 | |||
| 50 | $sessionId = $this->get_session_id(); |
||
| 51 | $session = api_get_session_entity($sessionId); |
||
| 52 | /* |
||
| 53 | if (empty($session_id)) { |
||
| 54 | $session_condition = api_get_session_condition(0, true); |
||
| 55 | } else { |
||
| 56 | $session_condition = api_get_session_condition($session_id, true, true); |
||
| 57 | } |
||
| 58 | $sql = "SELECT id, url, title FROM $tbl_grade_links |
||
| 59 | WHERE c_id = {$this->course_id} AND filetype='folder' AND active = 1 $session_condition ";*/ |
||
| 60 | |||
| 61 | //Only show works from the session |
||
| 62 | //AND has_properties != '' |
||
| 63 | $repo = Container::getStudentPublicationRepository(); |
||
| 64 | $qb = $repo->getResourcesByCourse(api_get_course_entity($this->course_id), $session); |
||
| 65 | $qb->andWhere("resource.filetype = 'folder' AND resource.active = true"); |
||
| 66 | $links = $qb->getQuery()->getResult(); |
||
| 67 | |||
| 68 | /*$links = Container::getStudentPublicationRepository() |
||
| 69 | ->findBy([ |
||
| 70 | 'cId' => $this->course_id, |
||
| 71 | 'active' => true, |
||
| 72 | 'filetype' => 'folder', |
||
| 73 | 'session' => $session, |
||
| 74 | ]);*/ |
||
| 75 | $cats = []; |
||
| 76 | foreach ($links as $data) { |
||
| 77 | $work_name = $data->getTitle(); |
||
| 78 | if (empty($work_name)) { |
||
| 79 | $work_name = basename($data->getUrl()); |
||
| 80 | } |
||
| 81 | $cats[] = [$data->getIid(), $work_name]; |
||
| 82 | } |
||
| 83 | |||
| 84 | return $cats; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Has anyone done this exercise yet ? |
||
| 89 | */ |
||
| 90 | public function has_results() |
||
| 91 | { |
||
| 92 | $studentPublication = $this->getStudentPublication(); |
||
| 93 | |||
| 94 | if (empty($studentPublication)) { |
||
| 95 | return ''; |
||
|
|
|||
| 96 | } |
||
| 97 | $id = $studentPublication->getIid(); |
||
| 98 | $session = api_get_session_entity($this->get_session_id()); |
||
| 99 | $results = Container::getStudentPublicationRepository() |
||
| 100 | ->findBy([ |
||
| 101 | 'cId' => $this->course_id, |
||
| 102 | 'parentId' => $id, |
||
| 103 | 'session' => $session, |
||
| 104 | ]); |
||
| 105 | |||
| 106 | return 0 != count($results); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param null $studentId |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 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 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | public function needs_name_and_description() |
||
| 254 | { |
||
| 255 | return false; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function get_name() |
||
| 259 | { |
||
| 260 | $studentPublication = $this->getStudentPublication(); |
||
| 261 | $title = $studentPublication->getTitle(); |
||
| 262 | |||
| 263 | return empty($title) ? get_lang('Untitled') : $title; |
||
| 264 | } |
||
| 265 | |||
| 266 | public function get_description() |
||
| 267 | { |
||
| 268 | $studentPublication = $this->getStudentPublication(); |
||
| 269 | |||
| 270 | return $studentPublication->getDescription(); |
||
| 271 | } |
||
| 272 | |||
| 273 | public function get_link() |
||
| 274 | { |
||
| 275 | $studentPublication = $this->getStudentPublication(); |
||
| 276 | $sessionId = $this->get_session_id(); |
||
| 277 | $url = api_get_path(WEB_PATH).'main/work/work.php?'. |
||
| 278 | api_get_cidreq_params($this->getCourseId(), $sessionId). |
||
| 279 | '&id='.$studentPublication->getIid().'&gradebook=view'; |
||
| 280 | |||
| 281 | return $url; |
||
| 282 | } |
||
| 283 | |||
| 284 | public function needs_max() |
||
| 285 | { |
||
| 286 | return false; |
||
| 287 | } |
||
| 288 | |||
| 289 | public function needs_results() |
||
| 290 | { |
||
| 291 | return false; |
||
| 292 | } |
||
| 293 | |||
| 294 | public function is_valid_link() |
||
| 295 | { |
||
| 296 | $studentPublication = $this->getStudentPublication(); |
||
| 297 | |||
| 298 | return null !== $studentPublication; |
||
| 299 | } |
||
| 300 | |||
| 301 | public function get_icon_name() |
||
| 302 | { |
||
| 303 | return 'studentpublication'; |
||
| 304 | } |
||
| 305 | |||
| 306 | public function save_linked_data() |
||
| 307 | { |
||
| 308 | $studentPublication = $this->getStudentPublication(); |
||
| 309 | |||
| 310 | if (empty($studentPublication)) { |
||
| 311 | return ''; |
||
| 312 | } |
||
| 313 | |||
| 314 | $weight = api_float_val($this->get_weight()); |
||
| 315 | $studentPublication->setWeight($weight); |
||
| 316 | |||
| 317 | $repo = Container::getStudentPublicationRepository(); |
||
| 318 | $repo->update($studentPublication); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function delete_linked_data() |
||
| 325 | { |
||
| 326 | /*$data = $this->get_exercise_data(); |
||
| 327 | if (empty($data)) { |
||
| 328 | return ''; |
||
| 329 | }*/ |
||
| 330 | |||
| 331 | /*if (!empty($id)) { |
||
| 332 | //Cleans works |
||
| 333 | $sql = 'UPDATE '.$this->get_studpub_table().' |
||
| 334 | SET weight = 0 |
||
| 335 | WHERE c_id = '.$this->course_id.' AND id ='.$id; |
||
| 336 | Database::query($sql); |
||
| 337 | }*/ |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Lazy load function to get the database table of the student publications. |
||
| 342 | */ |
||
| 343 | private function get_studpub_table() |
||
| 344 | { |
||
| 345 | return $this->studpub_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION); |
||
| 346 | } |
||
| 347 | |||
| 348 | private function getStudentPublication(): ?CStudentPublication |
||
| 356 | } |
||
| 357 | } |
||
| 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: