| Total Complexity | 82 |
| Total Lines | 323 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Course 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 Course, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Course |
||
| 16 | { |
||
| 17 | public $resources; |
||
| 18 | public $code; |
||
| 19 | public $path; |
||
| 20 | public $destination_path; |
||
| 21 | public $destination_db; |
||
| 22 | public $encoding; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create a new Course-object. |
||
| 26 | */ |
||
| 27 | public function __construct() |
||
| 28 | { |
||
| 29 | $this->resources = []; |
||
| 30 | $this->code = ''; |
||
| 31 | $this->path = ''; |
||
| 32 | $this->backup_path = ''; |
||
|
|
|||
| 33 | $this->encoding = api_get_system_encoding(); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Check if a resource links to the given resource. |
||
| 38 | */ |
||
| 39 | public function is_linked_resource(&$resource_to_check) |
||
| 40 | { |
||
| 41 | foreach ($this->resources as $type => $resources) { |
||
| 42 | if (is_array($resources)) { |
||
| 43 | foreach ($resources as $resource) { |
||
| 44 | Resource::setClassType($resource); |
||
| 45 | if ($resource->links_to($resource_to_check)) { |
||
| 46 | return true; |
||
| 47 | } |
||
| 48 | if ($type == RESOURCE_LEARNPATH && get_class($resource) == 'CourseCopyLearnpath') { |
||
| 49 | if ($resource->has_item($resource_to_check)) { |
||
| 50 | return true; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Add a resource from a given type to this course. |
||
| 62 | * |
||
| 63 | * @param $resource |
||
| 64 | */ |
||
| 65 | public function add_resource(&$resource) |
||
| 66 | { |
||
| 67 | $this->resources[$resource->get_type()][$resource->get_id()] = $resource; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Does this course has resources? |
||
| 72 | * |
||
| 73 | * @param int $type Check if this course has resources of the |
||
| 74 | * given type. If no type is given, check if course has resources of any |
||
| 75 | * type. |
||
| 76 | * |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | public function has_resources($type = null) |
||
| 80 | { |
||
| 81 | if ($type != null) { |
||
| 82 | return |
||
| 83 | isset($this->resources[$type]) && is_array($this->resources[$type]) && ( |
||
| 84 | count($this->resources[$type]) > 0 |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | return count($this->resources) > 0; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function show() |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns sample text based on the imported course content. |
||
| 97 | * This sample text is to be used for course language or encoding |
||
| 98 | * detection if there is missing (meta)data in the archive. |
||
| 99 | * |
||
| 100 | * @return string the resulting sample text extracted from some common resources' data fields |
||
| 101 | */ |
||
| 102 | public function get_sample_text() |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Converts to the system encoding all the language-sensitive fields in the imported course. |
||
| 192 | */ |
||
| 193 | public function to_system_encoding() |
||
| 194 | { |
||
| 195 | if (api_equal_encodings($this->encoding, api_get_system_encoding())) { |
||
| 196 | return; |
||
| 197 | } |
||
| 198 | |||
| 199 | foreach ($this->resources as $type => &$resources) { |
||
| 200 | if (count($resources) > 0) { |
||
| 201 | foreach ($resources as &$resource) { |
||
| 202 | switch ($type) { |
||
| 203 | case RESOURCE_ANNOUNCEMENT: |
||
| 204 | case RESOURCE_EVENT: |
||
| 205 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 206 | $resource->content = api_to_system_encoding($resource->content, $this->encoding); |
||
| 207 | break; |
||
| 208 | case RESOURCE_DOCUMENT: |
||
| 209 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 210 | $resource->comment = api_to_system_encoding($resource->comment, $this->encoding); |
||
| 211 | break; |
||
| 212 | case RESOURCE_FORUM: |
||
| 213 | case RESOURCE_QUIZ: |
||
| 214 | case RESOURCE_FORUMCATEGORY: |
||
| 215 | case RESOURCE_LINK: |
||
| 216 | case RESOURCE_LINKCATEGORY: |
||
| 217 | case RESOURCE_TEST_CATEGORY: |
||
| 218 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 219 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 220 | break; |
||
| 221 | case RESOURCE_FORUMPOST: |
||
| 222 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 223 | $resource->text = api_to_system_encoding($resource->text, $this->encoding); |
||
| 224 | $resource->poster_name = api_to_system_encoding($resource->poster_name, $this->encoding); |
||
| 225 | break; |
||
| 226 | case RESOURCE_FORUMTOPIC: |
||
| 227 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 228 | $resource->topic_poster_name = api_to_system_encoding($resource->topic_poster_name, $this->encoding); |
||
| 229 | $resource->title_qualify = api_to_system_encoding($resource->title_qualify, $this->encoding); |
||
| 230 | break; |
||
| 231 | case RESOURCE_GLOSSARY: |
||
| 232 | $resource->name = api_to_system_encoding($resource->name, $this->encoding); |
||
| 233 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 234 | break; |
||
| 235 | case RESOURCE_LEARNPATH: |
||
| 236 | $resource->name = api_to_system_encoding($resource->name, $this->encoding); |
||
| 237 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 238 | $resource->content_maker = api_to_system_encoding($resource->content_maker, $this->encoding); |
||
| 239 | $resource->content_license = api_to_system_encoding($resource->content_license, $this->encoding); |
||
| 240 | break; |
||
| 241 | case RESOURCE_QUIZQUESTION: |
||
| 242 | $resource->question = api_to_system_encoding($resource->question, $this->encoding); |
||
| 243 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 244 | if (is_array($resource->answers) && count($resource->answers) > 0) { |
||
| 245 | foreach ($resource->answers as &$answer) { |
||
| 246 | $answer['answer'] = api_to_system_encoding($answer['answer'], $this->encoding); |
||
| 247 | $answer['comment'] = api_to_system_encoding($answer['comment'], $this->encoding); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | break; |
||
| 251 | case RESOURCE_SCORM: |
||
| 252 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 253 | break; |
||
| 254 | case RESOURCE_SURVEY: |
||
| 255 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 256 | $resource->subtitle = api_to_system_encoding($resource->subtitle, $this->encoding); |
||
| 257 | $resource->author = api_to_system_encoding($resource->author, $this->encoding); |
||
| 258 | $resource->intro = api_to_system_encoding($resource->intro, $this->encoding); |
||
| 259 | $resource->surveythanks = api_to_system_encoding($resource->surveythanks, $this->encoding); |
||
| 260 | break; |
||
| 261 | case RESOURCE_SURVEYQUESTION: |
||
| 262 | $resource->survey_question = api_to_system_encoding($resource->survey_question, $this->encoding); |
||
| 263 | $resource->survey_question_comment = api_to_system_encoding($resource->survey_question_comment, $this->encoding); |
||
| 264 | break; |
||
| 265 | case RESOURCE_TOOL_INTRO: |
||
| 266 | $resource->intro_text = api_to_system_encoding($resource->intro_text, $this->encoding); |
||
| 267 | break; |
||
| 268 | case RESOURCE_WIKI: |
||
| 269 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 270 | $resource->content = api_to_system_encoding($resource->content, $this->encoding); |
||
| 271 | $resource->reflink = api_to_system_encoding($resource->reflink, $this->encoding); |
||
| 272 | break; |
||
| 273 | case RESOURCE_WORK: |
||
| 274 | $resource->url = api_to_system_encoding($resource->url, $this->encoding); |
||
| 275 | $resource->title = api_to_system_encoding($resource->title, $this->encoding); |
||
| 276 | $resource->description = api_to_system_encoding($resource->description, $this->encoding); |
||
| 277 | break; |
||
| 278 | default: |
||
| 279 | break; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | $this->encoding = api_get_system_encoding(); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Serialize the course with the best serializer available. |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public static function serialize($course) |
||
| 293 | { |
||
| 294 | if (extension_loaded('igbinary')) { |
||
| 295 | $serialized = igbinary_serialize($course); |
||
| 296 | } else { |
||
| 297 | $serialized = serialize($course); |
||
| 298 | } |
||
| 299 | |||
| 300 | // Compress |
||
| 301 | if (function_exists('gzdeflate')) { |
||
| 302 | $deflated = gzdeflate($serialized, 9); |
||
| 303 | if ($deflated !== false) { |
||
| 304 | $deflated = $serialized; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | return $serialized; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Unserialize the course with the best serializer available. |
||
| 313 | * |
||
| 314 | * @param string $course |
||
| 315 | * |
||
| 316 | * @return Course |
||
| 317 | */ |
||
| 318 | public static function unserialize($course) |
||
| 338 | } |
||
| 339 | } |
||
| 340 |