Completed
Push — 1.10.x ( c0da86...0328a9 )
by
unknown
125:03 queued 74:59
created

Resource::get_tool()   C

Complexity

Conditions 20
Paths 20

Size

Total Lines 58
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 42
nc 20
nop 1
dl 0
loc 58
rs 6.3621
c 0
b 0
f 0

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 Coursecopy;
5
6
define('RESOURCE_DOCUMENT', 'document');
7
define('RESOURCE_GLOSSARY', 'glossary');
8
define('RESOURCE_EVENT', 'calendar_event');
9
define('RESOURCE_LINK', 'link');
10
define('RESOURCE_COURSEDESCRIPTION', 'course_description');
11
define('RESOURCE_LEARNPATH', 'learnpath');
12
define('RESOURCE_ANNOUNCEMENT', 'announcement');
13
define('RESOURCE_FORUM', 'forum');
14
define('RESOURCE_FORUMTOPIC', 'thread');
15
define('RESOURCE_FORUMPOST', 'post');
16
define('RESOURCE_QUIZ', 'quiz');
17
define('RESOURCE_TEST_CATEGORY', 'test_category');
18
define('RESOURCE_QUIZQUESTION', 'Exercise_Question');
19
define('RESOURCE_TOOL_INTRO', 'Tool introduction');
20
define('RESOURCE_LINKCATEGORY', 'Link_Category');
21
define('RESOURCE_FORUMCATEGORY', 'Forum_Category');
22
define('RESOURCE_SCORM', 'Scorm');
23
define('RESOURCE_SURVEY', 'survey');
24
define('RESOURCE_SURVEYQUESTION', 'survey_question');
25
define('RESOURCE_SURVEYINVITATION', 'survey_invitation');
26
define('RESOURCE_WIKI', 'wiki');
27
define('RESOURCE_THEMATIC', 'thematic');
28
define('RESOURCE_ATTENDANCE', 'attendance');
29
define('RESOURCE_WORK', 'work');
30
define('RESOURCE_SESSION_COURSE', 'session_course');
31
define('RESOURCE_GRADEBOOK', 'gradebook');
32
33
/**
34
 * Class Resource
35
 * Representation of a resource in a Chamilo-course.
36
 * This is a base class of which real resource-classes (for Links,
37
 * Documents,...) should be derived.
38
 * @author Bart Mollet <[email protected]>s
39
 * @package  chamilo.backup
40
 * @todo Use the globally defined constants voor tools and remove the RESOURCE_*
41
 * constants
42
 */
43
class Resource
44
{
45
    /**
46
     * The id from this resource in the source course
47
     */
48
    public $source_id;
49
50
    /**
51
     * The id from this resource in the destination course
52
     */
53
    public $destination_id;
54
55
    /**
56
     * The type of this resource
57
     */
58
    public $type;
59
60
    /**
61
     * Linked resources
62
     */
63
    public $linked_resources;
64
65
    /**
66
     * The properties of this resource
67
     */
68
    public $item_properties;
69
70
    public $obj = null;
71
72
    /**
73
     * Create a new Resource
74
     * @param int $id The id of this resource in the source course.
75
     * @param integer $type The type of this resource.
76
     */
77
    public function __construct($id, $type)
78
    {
79
        $this->source_id = $id;
80
        $this->type = $type;
81
        $this->destination_id = -1;
82
        $this->linked_resources = array();
83
        $this->item_properties = array();
84
    }
85
86
    /**
87
     * Add linked resource
88
     */
89
    public function add_linked_resource($type, $id)
90
    {
91
        $this->linked_resources[$type][] = $id;
92
    }
93
94
    /**
95
     * Get linked resources
96
     */
97
    public function get_linked_resources()
98
    {
99
        return $this->linked_resources;
100
    }
101
102
    /**
103
     * Checks if this resource links to a given resource
104
     */
105
    public function links_to(& $resource)
106
    {
107
        self::setClassType($resource);
108
        $type = $resource->get_type();
0 ignored issues
show
Bug introduced by
The method get_type cannot be called on $resource (of type resource).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
109
        if (isset($this->linked_resources[$type]) &&
110
            is_array($this->linked_resources[$type])
111
        ) {
112
            return in_array(
113
                $resource->get_id(),
0 ignored issues
show
Bug introduced by
The method get_id cannot be called on $resource (of type resource).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
114
                $this->linked_resources[$type]
115
            );
116
        }
117
        return false;
118
    }
119
120
    /**
121
     * Returns the id of this resource.
122
     * @return int The id of this resource in the source course.
123
     */
124
    public function get_id()
125
    {
126
        return $this->source_id;
127
    }
128
129
    /**
130
     * Resturns the type of this resource
131
     * @return integer The type.
132
     */
133
    public function get_type()
134
    {
135
        return $this->type;
136
    }
137
138
    /**
139
     * Get the constant which defines the tool of this resource. This is
140
     * used in the item_properties table.
141
     * @param bool $for_item_property_table (optional)	Added by Ivan,
142
     * 29-AUG-2009: A parameter for resolving differencies between defined TOOL_*
143
     * constants and hardcoded strings that are stored in the database.
144
     * Example: The constant TOOL_THREAD is defined in the main_api.lib.php
145
     * with the value 'thread', but the "Forums" tool records in the field 'tool'
146
     * in the item property table the hardcoded value 'forum_thread'.
147
     * @todo once the RESOURCE_* constants are replaced by the globally
148
     * defined TOOL_* constants, this function will be replaced by get_type()
149
     */
150
    public function get_tool($for_item_property_table = true)
151
    {
152
        switch ($this->get_type()) {
153
            case RESOURCE_DOCUMENT:
154
                return TOOL_DOCUMENT;
155
            case RESOURCE_LINK:
156
                return TOOL_LINK;
157
            case RESOURCE_EVENT:
158
                return TOOL_CALENDAR_EVENT;
159
            case RESOURCE_COURSEDESCRIPTION:
160
                return TOOL_COURSE_DESCRIPTION;
161
            case RESOURCE_LEARNPATH:
162
                return TOOL_LEARNPATH;
163
            case RESOURCE_ANNOUNCEMENT:
164
                return TOOL_ANNOUNCEMENT;
165
            case RESOURCE_FORUMCATEGORY:
166
                return 'forum_category'; // Ivan, 29-AUG-2009: A constant like TOOL_FORUM_CATEGORY is missing in main_api.lib.php. Such a constant has been defined in the forum tool for local needs.
167
            case RESOURCE_FORUM:
168
                return TOOL_FORUM;
169
            case RESOURCE_FORUMTOPIC:
170
                if ($for_item_property_table) {
171
                    return 'forum_thread'; // Ivan, 29-AUG-2009: A hardcoded value that the "Forums" tool stores in the item property table.
172
                }
173
                return TOOL_THREAD;
174
            case RESOURCE_FORUMPOST:
175
                return TOOL_POST;
176
            case RESOURCE_QUIZ:
177
                return TOOL_QUIZ;
178
            case RESOURCE_TEST_CATEGORY:
179
                return TOOL_TEST_CATEGORY;
180
            //case RESOURCE_QUIZQUESTION: //no corresponding global constant
181
            //	return TOOL_QUIZ_QUESTION;
182
            //case RESOURCE_TOOL_INTRO:
183
            //	return TOOL_INTRO;
184
            //case RESOURCE_LINKCATEGORY:
185
            //	return TOOL_LINK_CATEGORY;
186
            //case RESOURCE_SCORM:
187
            //	return TOOL_SCORM_DOCUMENT;
188
            case RESOURCE_SURVEY:
189
                return TOOL_SURVEY;
190
            //case RESOURCE_SURVEYQUESTION:
191
            //	return TOOL_SURVEY_QUESTION;
192
            //case RESOURCE_SURVEYINVITATION:
193
            //	return TOOL_SURVEY_INVITATION;
194
            case RESOURCE_GLOSSARY:
195
                return TOOL_GLOSSARY;
196
            case RESOURCE_WIKI:
197
                return TOOL_WIKI;
198
            case RESOURCE_THEMATIC:
199
                return TOOL_COURSE_PROGRESS;
200
            case RESOURCE_ATTENDANCE:
201
                return TOOL_ATTENDANCE;
202
            case RESOURCE_WORK:
203
                return TOOL_STUDENTPUBLICATION;
204
            default:
205
                return null;
206
        }
207
    }
208
209
    /**
210
     * Set the destination id
211
     * @param int $id The id of this resource in the destination course.
212
     */
213
    public function set_new_id($id)
214
    {
215
        $this->destination_id = $id;
216
    }
217
218
    /**
219
     * Check if this resource is allready restored in the destination course.
220
     * @return bool true if allready restored (i.e. destination_id is set).
221
     */
222
    public function is_restored()
223
    {
224
        return $this->destination_id > -1;
225
    }
226
227
    /**
228
     * Show this resource
229
     */
230
    public function show()
231
    {
232
        //echo 'RESOURCE: '.$this->get_id().' '.$type[$this->get_type()].' ';
233
    }
234
235
    /**
236
     * Fix objects coming from 1.9.x to 1.10.x
237
     * Example class Event to CalendarEvent
238
     *
239
     * @param Resource $resource
240
     */
241
    public static function setClassType(&$resource)
242
    {
243
        $class = get_class($resource);
244
        switch ($class) {
245
            case 'Event':
246
                /** @var $resource \CalendarEvent */
247
                $newResource = new \CalendarEvent(
248
                    $resource->source_id,
249
                    $resource->title,
250
                    $resource->content,
251
                    $resource->start_date,
252
                    $resource->end_date,
253
                    $resource->attachment_path,
254
                    $resource->attachment_filename,
255
                    $resource->attachment_size,
256
                    $resource->attachment_comment,
257
                    $resource->all_day
258
                );
259
                $resource = $newResource;
260
                break;
261
            case 'CourseDescription':
262
                if (!method_exists($resource, 'show')) {
263
                    $resource = (array) $resource;
264
                    $newResource = new CourseDescription(
265
                        isset($resource['id']) ? $resource['id'] : '',
266
                        $resource['title'],
267
                        $resource['content'],
268
                        $resource['description_type']
269
                    );
270
                    $newResource->source_id = $resource['source_id'];
271
                    $newResource->destination_id = $resource['source_id'];
272
                    $newResource->linked_resources = $resource['source_id'];
273
                    $newResource->item_properties = $resource['source_id'];
274
                    $newResource->obj = $resource['obj'];
275
                    $resource = $newResource;
276
                }
277
278
                break;
279
        }
280
    }
281
}
282