| Total Complexity | 47 |
| Total Lines | 352 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ForumThreadLink 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 ForumThreadLink, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class ForumThreadLink extends AbstractLink |
||
| 14 | { |
||
| 15 | private $forum_thread_table; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Constructor. |
||
| 19 | */ |
||
| 20 | public function __construct() |
||
| 21 | { |
||
| 22 | parent::__construct(); |
||
| 23 | $this->set_type(LINK_FORUM_THREAD); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @return string |
||
| 28 | */ |
||
| 29 | public function get_type_name() |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return bool |
||
| 36 | */ |
||
| 37 | public function is_allowed_to_change_name() |
||
| 38 | { |
||
| 39 | return false; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Generate an array of all exercises available. |
||
| 44 | * |
||
| 45 | * @return array 2-dimensional array - every element contains 2 subelements (id, name) |
||
| 46 | */ |
||
| 47 | public function get_all_links() |
||
| 48 | { |
||
| 49 | if (empty($this->course_code)) { |
||
| 50 | return []; |
||
| 51 | } |
||
| 52 | |||
| 53 | $tbl_grade_links = Database::get_course_table(TABLE_FORUM_THREAD); |
||
| 54 | $sessionId = $this->get_session_id(); |
||
| 55 | |||
| 56 | if ($sessionId) { |
||
| 57 | $session_condition = 'tl.session_id='.$sessionId; |
||
| 58 | } else { |
||
| 59 | $session_condition = '(tl.session_id = 0 OR tl.session_id IS NULL)'; |
||
| 60 | } |
||
| 61 | |||
| 62 | $repo = Container::getForumThreadRepository(); |
||
| 63 | $course = api_get_course_entity($this->course_id); |
||
| 64 | $session = api_get_session_entity($sessionId); |
||
| 65 | |||
| 66 | $qb = $repo->findAllByCourse($course, $session); |
||
| 67 | /** @var CForumThread[] $threads */ |
||
| 68 | $threads = $qb->getQuery()->getResult(); |
||
| 69 | |||
| 70 | /*$sql = 'SELECT tl.iid as thread_id, tl.thread_title, tl.thread_title_qualify |
||
| 71 | FROM '.$tbl_grade_links.' tl |
||
| 72 | WHERE |
||
| 73 | tl.c_id = '.$this->course_id.' AND |
||
| 74 | '.$session_condition.' |
||
| 75 | '; |
||
| 76 | $result = Database::query($sql);*/ |
||
| 77 | $cats = []; |
||
| 78 | foreach ($threads as $thread) { |
||
| 79 | $title = $thread->getThreadTitle(); |
||
| 80 | $threadQualify = $thread->getThreadTitleQualify(); |
||
| 81 | if (!empty($threadQualify)) { |
||
| 82 | $title = $threadQualify; |
||
| 83 | } |
||
| 84 | $cats[] = [$thread->getIid(), $title]; |
||
| 85 | } |
||
| 86 | |||
| 87 | return $cats; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Has anyone done this exercise yet ? |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function has_results() |
||
| 96 | { |
||
| 97 | $table = Database::get_course_table(TABLE_FORUM_POST); |
||
| 98 | |||
| 99 | $sql = "SELECT count(*) AS number FROM $table |
||
| 100 | WHERE |
||
| 101 | c_id = ".$this->course_id." AND |
||
| 102 | iid = '".$this->get_ref_id()."' |
||
| 103 | "; |
||
| 104 | $result = Database::query($sql); |
||
| 105 | $number = Database::fetch_row($result); |
||
| 106 | |||
| 107 | return 0 != $number[0]; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param int $studentId |
||
| 112 | * @param string $type |
||
| 113 | * |
||
| 114 | * @return array|null |
||
| 115 | */ |
||
| 116 | public function calc_score($studentId = null, $type = null) |
||
| 117 | { |
||
| 118 | $threadInfo = $this->getThreadData(); |
||
| 119 | $thread_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY); |
||
| 120 | $sessionId = $this->get_session_id(); |
||
| 121 | $sessionCondition = api_get_session_condition( |
||
| 122 | $sessionId, |
||
| 123 | true, |
||
| 124 | false, |
||
| 125 | 'session_id' |
||
| 126 | ); |
||
| 127 | |||
| 128 | $sql = 'SELECT thread_qualify_max |
||
| 129 | FROM '.Database::get_course_table(TABLE_FORUM_THREAD)." |
||
| 130 | WHERE |
||
| 131 | c_id = ".$this->course_id." AND |
||
| 132 | iid = '".$this->get_ref_id()."' |
||
| 133 | $sessionCondition |
||
| 134 | "; |
||
| 135 | $query = Database::query($sql); |
||
| 136 | $assignment = Database::fetch_array($query); |
||
| 137 | |||
| 138 | $sql = "SELECT * FROM $thread_qualify |
||
| 139 | WHERE |
||
| 140 | c_id = ".$this->course_id." AND |
||
| 141 | iid = ".$this->get_ref_id()." |
||
| 142 | $sessionCondition |
||
| 143 | "; |
||
| 144 | if (isset($studentId)) { |
||
| 145 | $sql .= ' AND user_id = '.intval($studentId); |
||
| 146 | } |
||
| 147 | |||
| 148 | // order by id, that way the student's first attempt is accessed first |
||
| 149 | $sql .= ' ORDER BY qualify_time DESC'; |
||
| 150 | $scores = Database::query($sql); |
||
| 151 | |||
| 152 | // for 1 student |
||
| 153 | if (isset($studentId)) { |
||
| 154 | if (0 == $threadInfo['thread_peer_qualify']) { |
||
| 155 | // Classic way of calculate score |
||
| 156 | if ($data = Database::fetch_array($scores)) { |
||
| 157 | return [ |
||
| 158 | $data['qualify'], |
||
| 159 | $assignment['thread_qualify_max'], |
||
| 160 | ]; |
||
| 161 | } else { |
||
| 162 | // We sent the 0/thread_qualify_max instead of null for correct calculations |
||
| 163 | return [0, $assignment['thread_qualify_max']]; |
||
| 164 | } |
||
| 165 | } else { |
||
| 166 | // Take average |
||
| 167 | $score = 0; |
||
| 168 | $counter = 0; |
||
| 169 | if (Database::num_rows($scores)) { |
||
| 170 | while ($data = Database::fetch_array($scores, 'ASSOC')) { |
||
| 171 | $score += $data['qualify']; |
||
| 172 | $counter++; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | // If no result |
||
| 176 | if (empty($counter) || $counter <= 2) { |
||
| 177 | return [0, $assignment['thread_qualify_max']]; |
||
| 178 | } |
||
| 179 | |||
| 180 | return [$score / $counter, $assignment['thread_qualify_max']]; |
||
| 181 | } |
||
| 182 | } else { |
||
| 183 | // All students -> get average |
||
| 184 | $students = []; // user list, needed to make sure we only |
||
| 185 | // take first attempts into account |
||
| 186 | $counter = 0; |
||
| 187 | $sum = 0; |
||
| 188 | $bestResult = 0; |
||
| 189 | $weight = 0; |
||
| 190 | $sumResult = 0; |
||
| 191 | |||
| 192 | while ($data = Database::fetch_array($scores)) { |
||
| 193 | if (!(array_key_exists($data['user_id'], $students))) { |
||
| 194 | if (0 != $assignment['thread_qualify_max']) { |
||
| 195 | $students[$data['user_id']] = $data['qualify']; |
||
| 196 | $counter++; |
||
| 197 | $sum += $data['qualify'] / $assignment['thread_qualify_max']; |
||
| 198 | $sumResult += $data['qualify']; |
||
| 199 | if ($data['qualify'] > $bestResult) { |
||
| 200 | $bestResult = $data['qualify']; |
||
| 201 | } |
||
| 202 | $weight = $assignment['thread_qualify_max']; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | if (0 == $counter) { |
||
| 208 | return [null, null]; |
||
| 209 | } else { |
||
| 210 | switch ($type) { |
||
| 211 | case 'best': |
||
| 212 | return [$bestResult, $weight]; |
||
| 213 | break; |
||
|
|
|||
| 214 | case 'average': |
||
| 215 | return [$sumResult / $counter, $weight]; |
||
| 216 | break; |
||
| 217 | case 'ranking': |
||
| 218 | return AbstractLink::getCurrentUserRanking($studentId, $students); |
||
| 219 | break; |
||
| 220 | default: |
||
| 221 | return [$sum, $counter]; |
||
| 222 | break; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | public function needs_name_and_description() |
||
| 229 | { |
||
| 230 | return false; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function needs_max() |
||
| 234 | { |
||
| 235 | return false; |
||
| 236 | } |
||
| 237 | |||
| 238 | public function needs_results() |
||
| 239 | { |
||
| 240 | return false; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function get_name() |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function get_description() |
||
| 262 | { |
||
| 263 | return ''; //$this->exercise_data['description']; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Check if this still links to an exercise. |
||
| 268 | */ |
||
| 269 | public function is_valid_link() |
||
| 270 | { |
||
| 271 | $sessionId = $this->get_session_id(); |
||
| 272 | $sql = 'SELECT count(iid) FROM '.$this->get_forum_thread_table().' |
||
| 273 | WHERE |
||
| 274 | c_id = '.$this->course_id.' AND |
||
| 275 | iid = '.$this->get_ref_id().' AND |
||
| 276 | session_id='.$sessionId; |
||
| 277 | $result = Database::query($sql); |
||
| 278 | $number = Database::fetch_row($result); |
||
| 279 | |||
| 280 | return 0 != $number[0]; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function get_link() |
||
| 284 | { |
||
| 285 | $sessionId = $this->get_session_id(); |
||
| 286 | $sql = 'SELECT * FROM '.$this->get_forum_thread_table()." |
||
| 287 | WHERE |
||
| 288 | c_id = {$this->course_id} AND |
||
| 289 | iid = '".$this->get_ref_id()."' AND |
||
| 290 | session_id = $sessionId "; |
||
| 291 | $result = Database::query($sql); |
||
| 292 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 293 | |||
| 294 | if ($row) { |
||
| 295 | $forum_id = $row['forum_id']; |
||
| 296 | |||
| 297 | return api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'. |
||
| 298 | api_get_cidreq_params($this->getCourseId(), $sessionId). |
||
| 299 | '&thread='.$this->get_ref_id().'&gradebook=view&forum='.$forum_id; |
||
| 300 | } |
||
| 301 | |||
| 302 | return ''; |
||
| 303 | } |
||
| 304 | |||
| 305 | public function get_icon_name() |
||
| 306 | { |
||
| 307 | return 'forum'; |
||
| 308 | } |
||
| 309 | |||
| 310 | public function save_linked_data() |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | public function delete_linked_data() |
||
| 324 | { |
||
| 325 | $ref_id = $this->get_ref_id(); |
||
| 326 | if (!empty($ref_id)) { |
||
| 327 | // Cleans forum |
||
| 328 | $sql = 'UPDATE '.$this->get_forum_thread_table().' SET |
||
| 329 | thread_qualify_max = 0, |
||
| 330 | thread_weight = 0, |
||
| 331 | thread_title_qualify = "" |
||
| 332 | WHERE c_id = '.$this->course_id.' AND iid = '.$ref_id; |
||
| 333 | Database::query($sql); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Lazy load function to get the database table of the student publications. |
||
| 339 | */ |
||
| 340 | private function get_forum_thread_table() |
||
| 343 | } |
||
| 344 | |||
| 345 | private function getThreadData() |
||
| 365 | } |
||
| 366 | } |
||
| 367 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.