| Total Complexity | 49 |
| Total Lines | 342 |
| 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 |
||
| 10 | class ForumThreadLink extends AbstractLink |
||
| 11 | { |
||
| 12 | private $forum_thread_table; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Constructor. |
||
| 16 | */ |
||
| 17 | public function __construct() |
||
| 18 | { |
||
| 19 | parent::__construct(); |
||
| 20 | $this->set_type(LINK_FORUM_THREAD); |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @return string |
||
| 25 | */ |
||
| 26 | public function get_type_name() |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @return bool |
||
| 33 | */ |
||
| 34 | public function is_allowed_to_change_name() |
||
| 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 | $tbl_grade_links = Database::get_course_table(TABLE_FORUM_THREAD); |
||
| 51 | $sessionId = $this->get_session_id(); |
||
| 52 | |||
| 53 | if ($sessionId) { |
||
| 54 | $session_condition = 'tl.session_id='.$sessionId; |
||
| 55 | } else { |
||
| 56 | $session_condition = '(tl.session_id = 0 OR tl.session_id IS NULL)'; |
||
| 57 | } |
||
| 58 | |||
| 59 | $sql = 'SELECT tl.iid as thread_id, tl.thread_title, tl.thread_title_qualify |
||
| 60 | FROM '.$tbl_grade_links.' tl |
||
| 61 | WHERE |
||
| 62 | tl.c_id = '.$this->course_id.' AND |
||
| 63 | '.$session_condition.' |
||
| 64 | '; |
||
| 65 | |||
| 66 | $result = Database::query($sql); |
||
| 67 | while ($data = Database::fetch_array($result)) { |
||
| 68 | if (isset($data['thread_title_qualify']) && '' != $data['thread_title_qualify']) { |
||
| 69 | $cats[] = [$data['thread_id'], $data['thread_title_qualify']]; |
||
| 70 | } else { |
||
| 71 | $cats[] = [$data['thread_id'], $data['thread_title']]; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | return isset($cats) ? $cats : []; |
||
|
|
|||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Has anyone done this exercise yet ? |
||
| 80 | * |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public function has_results() |
||
| 84 | { |
||
| 85 | $table = Database::get_course_table(TABLE_FORUM_POST); |
||
| 86 | |||
| 87 | $sql = "SELECT count(*) AS number FROM $table |
||
| 88 | WHERE |
||
| 89 | c_id = ".$this->course_id." AND |
||
| 90 | iid = '".$this->get_ref_id()."' |
||
| 91 | "; |
||
| 92 | $result = Database::query($sql); |
||
| 93 | $number = Database::fetch_row($result); |
||
| 94 | |||
| 95 | return 0 != $number[0]; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param int $studentId |
||
| 100 | * @param string $type |
||
| 101 | * |
||
| 102 | * @return array|null |
||
| 103 | */ |
||
| 104 | public function calc_score($studentId = null, $type = null) |
||
| 105 | { |
||
| 106 | $threadInfo = $this->getThreadData(); |
||
| 107 | $thread_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY); |
||
| 108 | $sessionId = $this->get_session_id(); |
||
| 109 | $sessionCondition = api_get_session_condition( |
||
| 110 | $sessionId, |
||
| 111 | true, |
||
| 112 | false, |
||
| 113 | 'session_id' |
||
| 114 | ); |
||
| 115 | |||
| 116 | $sql = 'SELECT thread_qualify_max |
||
| 117 | FROM '.Database::get_course_table(TABLE_FORUM_THREAD)." |
||
| 118 | WHERE |
||
| 119 | c_id = ".$this->course_id." AND |
||
| 120 | iid = '".$this->get_ref_id()."' |
||
| 121 | $sessionCondition |
||
| 122 | "; |
||
| 123 | $query = Database::query($sql); |
||
| 124 | $assignment = Database::fetch_array($query); |
||
| 125 | |||
| 126 | $sql = "SELECT * FROM $thread_qualify |
||
| 127 | WHERE |
||
| 128 | c_id = ".$this->course_id." AND |
||
| 129 | iid = ".$this->get_ref_id()." |
||
| 130 | $sessionCondition |
||
| 131 | "; |
||
| 132 | if (isset($studentId)) { |
||
| 133 | $sql .= ' AND user_id = '.intval($studentId); |
||
| 134 | } |
||
| 135 | |||
| 136 | // order by id, that way the student's first attempt is accessed first |
||
| 137 | $sql .= ' ORDER BY qualify_time DESC'; |
||
| 138 | $scores = Database::query($sql); |
||
| 139 | |||
| 140 | // for 1 student |
||
| 141 | if (isset($studentId)) { |
||
| 142 | if (0 == $threadInfo['thread_peer_qualify']) { |
||
| 143 | // Classic way of calculate score |
||
| 144 | if ($data = Database::fetch_array($scores)) { |
||
| 145 | return [ |
||
| 146 | $data['qualify'], |
||
| 147 | $assignment['thread_qualify_max'], |
||
| 148 | ]; |
||
| 149 | } else { |
||
| 150 | // We sent the 0/thread_qualify_max instead of null for correct calculations |
||
| 151 | return [0, $assignment['thread_qualify_max']]; |
||
| 152 | } |
||
| 153 | } else { |
||
| 154 | // Take average |
||
| 155 | $score = 0; |
||
| 156 | $counter = 0; |
||
| 157 | if (Database::num_rows($scores)) { |
||
| 158 | while ($data = Database::fetch_array($scores, 'ASSOC')) { |
||
| 159 | $score += $data['qualify']; |
||
| 160 | $counter++; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | // If no result |
||
| 164 | if (empty($counter) || $counter <= 2) { |
||
| 165 | return [0, $assignment['thread_qualify_max']]; |
||
| 166 | } |
||
| 167 | |||
| 168 | return [$score / $counter, $assignment['thread_qualify_max']]; |
||
| 169 | } |
||
| 170 | } else { |
||
| 171 | // All students -> get average |
||
| 172 | $students = []; // user list, needed to make sure we only |
||
| 173 | // take first attempts into account |
||
| 174 | $counter = 0; |
||
| 175 | $sum = 0; |
||
| 176 | $bestResult = 0; |
||
| 177 | $weight = 0; |
||
| 178 | $sumResult = 0; |
||
| 179 | |||
| 180 | while ($data = Database::fetch_array($scores)) { |
||
| 181 | if (!(array_key_exists($data['user_id'], $students))) { |
||
| 182 | if (0 != $assignment['thread_qualify_max']) { |
||
| 183 | $students[$data['user_id']] = $data['qualify']; |
||
| 184 | $counter++; |
||
| 185 | $sum += $data['qualify'] / $assignment['thread_qualify_max']; |
||
| 186 | $sumResult += $data['qualify']; |
||
| 187 | if ($data['qualify'] > $bestResult) { |
||
| 188 | $bestResult = $data['qualify']; |
||
| 189 | } |
||
| 190 | $weight = $assignment['thread_qualify_max']; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | if (0 == $counter) { |
||
| 196 | return [null, null]; |
||
| 197 | } else { |
||
| 198 | switch ($type) { |
||
| 199 | case 'best': |
||
| 200 | return [$bestResult, $weight]; |
||
| 201 | break; |
||
| 202 | case 'average': |
||
| 203 | return [$sumResult / $counter, $weight]; |
||
| 204 | break; |
||
| 205 | case 'ranking': |
||
| 206 | return AbstractLink::getCurrentUserRanking($studentId, $students); |
||
| 207 | break; |
||
| 208 | default: |
||
| 209 | return [$sum, $counter]; |
||
| 210 | break; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | public function needs_name_and_description() |
||
| 217 | { |
||
| 218 | return false; |
||
| 219 | } |
||
| 220 | |||
| 221 | public function needs_max() |
||
| 222 | { |
||
| 223 | return false; |
||
| 224 | } |
||
| 225 | |||
| 226 | public function needs_results() |
||
| 227 | { |
||
| 228 | return false; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function get_name() |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function get_description() |
||
| 250 | { |
||
| 251 | return ''; //$this->exercise_data['description']; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Check if this still links to an exercise. |
||
| 256 | */ |
||
| 257 | public function is_valid_link() |
||
| 258 | { |
||
| 259 | $sessionId = $this->get_session_id(); |
||
| 260 | $sql = 'SELECT count(iid) FROM '.$this->get_forum_thread_table().' |
||
| 261 | WHERE |
||
| 262 | c_id = '.$this->course_id.' AND |
||
| 263 | iid = '.$this->get_ref_id().' AND |
||
| 264 | session_id='.$sessionId; |
||
| 265 | $result = Database::query($sql); |
||
| 266 | $number = Database::fetch_row($result); |
||
| 267 | |||
| 268 | return 0 != $number[0]; |
||
| 269 | } |
||
| 270 | |||
| 271 | public function get_link() |
||
| 272 | { |
||
| 273 | $sessionId = $this->get_session_id(); |
||
| 274 | $sql = 'SELECT * FROM '.$this->get_forum_thread_table()." |
||
| 275 | WHERE |
||
| 276 | c_id = {$this->course_id} AND |
||
| 277 | iid = '".$this->get_ref_id()."' AND |
||
| 278 | session_id = $sessionId "; |
||
| 279 | $result = Database::query($sql); |
||
| 280 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 281 | |||
| 282 | if ($row) { |
||
| 283 | $forum_id = $row['forum_id']; |
||
| 284 | return api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'. |
||
| 285 | api_get_cidreq_params($this->getCourseId(), $sessionId). |
||
| 286 | '&thread='.$this->get_ref_id().'&gradebook=view&forum='.$forum_id; |
||
| 287 | } |
||
| 288 | |||
| 289 | return ''; |
||
| 290 | } |
||
| 291 | |||
| 292 | public function get_icon_name() |
||
| 293 | { |
||
| 294 | return 'forum'; |
||
| 295 | } |
||
| 296 | |||
| 297 | public function save_linked_data() |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | public function delete_linked_data() |
||
| 311 | { |
||
| 312 | $ref_id = $this->get_ref_id(); |
||
| 313 | if (!empty($ref_id)) { |
||
| 314 | // Cleans forum |
||
| 315 | $sql = 'UPDATE '.$this->get_forum_thread_table().' SET |
||
| 316 | thread_qualify_max = 0, |
||
| 317 | thread_weight = 0, |
||
| 318 | thread_title_qualify = "" |
||
| 319 | WHERE c_id = '.$this->course_id.' AND iid = '.$ref_id; |
||
| 320 | Database::query($sql); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Lazy load function to get the database table of the student publications. |
||
| 326 | */ |
||
| 327 | private function get_forum_thread_table() |
||
| 330 | } |
||
| 331 | |||
| 332 | private function getThreadData() |
||
| 352 | } |
||
| 353 | } |
||
| 354 |