Completed
Push — master ( 0adf57...cd9ffe )
by Julito
10:56
created

Course::to_system_encoding()   D

Complexity

Conditions 28
Paths 26

Size

Total Lines 92
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 28
eloc 82
c 0
b 0
f 0
nop 0
dl 0
loc 92
rs 4.1666
nc 26

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Component\CourseCopy;
5
6
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Resource;
7
8
/**
9
 * A course-object to use in Export/Import/Backup/Copy.
10
 *
11
 * @author Bart Mollet <[email protected]>
12
 *
13
 * @package chamilo.backup
14
 */
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 = '';
0 ignored issues
show
Bug Best Practice introduced by
The property backup_path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $type of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
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()
92
    {
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()
103
    {
104
        $sample_text = '';
105
        foreach ($this->resources as $type => &$resources) {
106
            if (count($resources) > 0) {
107
                foreach ($resources as $id => &$resource) {
108
                    $title = '';
109
                    $description = '';
110
                    switch ($type) {
111
                        case RESOURCE_ANNOUNCEMENT:
112
                        case RESOURCE_EVENT:
113
                        case RESOURCE_THEMATIC:
114
                        case RESOURCE_WIKI:
115
                            $title = $resource->title;
116
                            $description = $resource->content;
117
                            break;
118
                        case RESOURCE_DOCUMENT:
119
                            $title = $resource->title;
120
                            $description = $resource->comment;
121
                            break;
122
                        case RESOURCE_FORUM:
123
                        case RESOURCE_FORUMCATEGORY:
124
                        case RESOURCE_LINK:
125
                        case RESOURCE_LINKCATEGORY:
126
                        case RESOURCE_QUIZ:
127
                        case RESOURCE_TEST_CATEGORY:
128
                        case RESOURCE_WORK:
129
                            $title = $resource->title;
130
                            $description = $resource->description;
131
                            break;
132
                        case RESOURCE_FORUMPOST:
133
                            $title = $resource->title;
134
                            $description = $resource->text;
135
                            break;
136
                        case RESOURCE_SCORM:
137
                        case RESOURCE_FORUMTOPIC:
138
                            $title = $resource->title;
139
                            break;
140
                        case RESOURCE_GLOSSARY:
141
                        case RESOURCE_LEARNPATH:
142
                            $title = $resource->name;
143
                            $description = $resource->description;
144
                            break;
145
                        case RESOURCE_LEARNPATH_CATEGORY:
146
                            $title = $resource->name;
147
                            break;
148
                        case RESOURCE_QUIZQUESTION:
149
                            $title = $resource->question;
150
                            $description = $resource->description;
151
                            break;
152
                        case RESOURCE_SURVEY:
153
                            $title = $resource->title;
154
                            $description = $resource->subtitle;
155
                            break;
156
                        case RESOURCE_SURVEYQUESTION:
157
                            $title = $resource->survey_question;
158
                            $description = $resource->survey_question_comment;
159
                            break;
160
                        case RESOURCE_TOOL_INTRO:
161
                            $description = $resource->intro_text;
162
                            break;
163
                        case RESOURCE_ATTENDANCE:
164
                            $title = $resource->params['name'];
165
                            $description = $resource->params['description'];
166
                            break;
167
                        default:
168
                            break;
169
                    }
170
171
                    $title = api_html_to_text($title);
172
                    $description = api_html_to_text($description);
173
174
                    if (!empty($title)) {
175
                        $sample_text .= $title."\n";
176
                    }
177
                    if (!empty($description)) {
178
                        $sample_text .= $description."\n";
179
                    }
180
                    if (!empty($title) || !empty($description)) {
181
                        $sample_text .= "\n";
182
                    }
183
                }
184
            }
185
        }
186
187
        return $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)
319
    {
320
        // Uncompress
321
        if (function_exists('gzdeflate')) {
322
            $inflated = @gzinflate($course);
323
            if ($inflated !== false) {
324
                $course = $inflated;
325
            }
326
        }
327
328
        if (extension_loaded('igbinary')) {
329
            $unserialized = igbinary_unserialize($course);
330
        } else {
331
            $unserialized = \UnserializeApi::unserialize(
332
                'course',
333
                $course
334
            );
335
        }
336
337
        return $unserialized;
338
    }
339
}
340