Passed
Push — webservicelpcreate ( d8cb35 )
by
unknown
13:48
created

Timeline::get_status_list()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class Timeline
6
 * Timeline model class definition.
7
 *
8
 * @package chamilo.library
9
 */
10
class Timeline extends Model
11
{
12
    public $table;
13
    public $columns = [
14
        'headline',
15
        'type',
16
        'start_date',
17
        'end_date',
18
        'text',
19
        'media',
20
        'media_credit',
21
        'media_caption',
22
        'title_slide',
23
        'parent_id',
24
        'status',
25
        'c_id',
26
    ];
27
    public $is_course_model = true;
28
29
    /**
30
     * Timeline constructor.
31
     */
32
    public function __construct()
33
    {
34
        $this->table = Database::get_course_table(TABLE_TIMELINE);
35
    }
36
37
    /**
38
     * Get the count of elements.
39
     *
40
     * @return int
41
     */
42
    public function get_count()
43
    {
44
        $course_id = api_get_course_int_id();
45
        $row = Database::select(
46
            'count(*) as count',
47
            $this->table,
48
            [
49
                'where' => [
50
                    'parent_id = ? AND c_id = ?' => [
51
                        '0',
52
                        $course_id,
53
                    ],
54
                ],
55
            ],
56
            'first'
57
        );
58
59
        return $row['count'];
60
    }
61
62
    /**
63
     * @param array $where_conditions
64
     *
65
     * @return array
66
     */
67
    public function get_all($where_conditions = [])
68
    {
69
        return Database::select(
70
            '*',
71
            $this->table,
72
            ['where' => $where_conditions, 'order' => 'headline ASC']
73
        );
74
    }
75
76
    /**
77
     * Displays the title + grid.
78
     */
79
    public function listing()
80
    {
81
        // action links
82
        $html = '<div class="actions">';
83
        $html .= '<a href="'.api_get_self().'?action=add">'.
84
            Display::return_icon('add.png', get_lang('Add'), '', '32').'</a>';
85
        $html .= '</div>';
86
        $html .= Display::grid_html('timelines');
87
88
        return $html;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function get_status_list()
95
    {
96
        return [
97
            TIMELINE_STATUS_ACTIVE => get_lang('Active'),
98
            TIMELINE_STATUS_INACTIVE => get_lang('Inactive'),
99
        ];
100
    }
101
102
    /**
103
     * Returns a Form validator Obj.
104
     *
105
     * @todo the form should be auto generated
106
     *
107
     * @param   string  url
108
     * @param   string  action add, edit
109
     *
110
     * @return obj form validator obj
111
     */
112
    public function return_form($url, $action)
113
    {
114
        $form = new FormValidator('timeline', 'post', $url);
115
        // Setting the form elements
116
        $header = get_lang('Add');
117
        if ($action == 'edit') {
118
            $header = get_lang('Modify');
119
        }
120
        $form->addElement('header', $header);
121
        $id = isset($_GET['id']) ? intval($_GET['id']) : '';
122
        $form->addElement('hidden', 'id', $id);
123
124
        $form->addElement('text', 'headline', get_lang('Name'), ['size' => '70']);
125
        $status_list = $this->get_status_list();
126
        $form->addElement('select', 'status', get_lang('Status'), $status_list);
127
        if ($action == 'edit') {
128
            //$form->addElement('text', 'created_at', get_lang('CreatedAt'));
129
            //$form->freeze('created_at');
130
        }
131
        if ($action == 'edit') {
132
            $form->addButtonSave(get_lang('Modify'), 'submit');
133
        } else {
134
            $form->addButtonCreate(get_lang('Add'), 'submit');
135
        }
136
137
        $form->addRule('headline', get_lang('ThisFieldIsRequired'), 'required');
138
139
        // Setting the defaults
140
        $defaults = $this->get($id);
141
142
        /*if (!empty($defaults['created_at'])) {
143
            $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
144
        }
145
        if (!empty($defaults['updated_at'])) {
146
            $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
147
        }*/
148
        $form->setDefaults($defaults);
149
150
        // Setting the rules
151
        $form->addRule('headline', get_lang('ThisFieldIsRequired'), 'required');
152
153
        return $form;
154
    }
155
156
    /**
157
     * @param $url
158
     * @param $action
159
     *
160
     * @return FormValidator
161
     */
162
    public function return_item_form($url, $action)
163
    {
164
        $form = new FormValidator('item_form', 'post', $url);
165
        // Setting the form elements
166
        $header = get_lang('Add');
167
        if ($action == 'edit') {
168
            $header = get_lang('Modify');
169
        }
170
        $form->addElement('header', $header);
171
        $id = isset($_GET['id']) ? intval($_GET['id']) : '';
172
        $parent_id = isset($_GET['parent_id']) ? intval($_GET['parent_id']) : '';
173
        $form->addElement('hidden', 'parent_id', $parent_id);
174
        $form->addElement('hidden', 'id', $id);
175
        $form->addElement('text', 'headline', get_lang('Name'));
176
177
        //@todo fix this
178
        $form->addElement('text', 'start_date', get_lang('StartDate'), ['size' => '70']);
179
        $form->addElement('text', 'end_date', get_lang('EndDate'), ['size' => '70']);
180
        $form->addElement('textarea', 'text', get_lang('TimelineItemText'));
181
        $form->addElement('text', 'media', get_lang('TimelineItemMedia'), ['size' => '70']);
182
        $form->addElement('text', 'media_caption', get_lang('TimelineItemMediaCaption'), ['size' => '70']);
183
        $form->addElement('text', 'media_credit', get_lang('TimelineItemMediaCredit'), ['size' => '70']);
184
        $form->addElement('text', 'title_slide', get_lang('TimelineItemTitleSlide'), ['size' => '70']);
185
186
        $form->addRule('headline', get_lang('ThisFieldIsRequired'), 'required');
187
        $form->addRule('start_date', get_lang('ThisFieldIsRequired'), 'required');
188
189
        if ($action == 'edit') {
190
            // Setting the defaults
191
            $defaults = $this->get($id);
192
            $form->addButtonSave(get_lang('Modify'), 'submit');
193
        } else {
194
            $form->addButtonCreate(get_lang('Add'), 'submit');
195
        }
196
        $form->setDefaults($defaults);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaults does not seem to be defined for all execution paths leading up to this point.
Loading history...
197
198
        // Setting the rules
199
        $form->addRule('headline', get_lang('ThisFieldIsRequired'), 'required');
200
201
        return $form;
202
    }
203
204
    /**
205
     * @param array $params
206
     *
207
     * @return bool
208
     */
209
    public function save_item($params)
210
    {
211
        $params['c_id'] = api_get_course_int_id();
212
        $id = parent::save($params);
213
        if (!empty($id)) {
214
            //event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
215
        }
216
217
        return $id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $id also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
218
    }
219
220
    /**
221
     * @param array $params
222
     * @param bool  $showQuery
223
     *
224
     * @return bool
225
     */
226
    public function save($params, $showQuery = false)
227
    {
228
        $params['c_id'] = api_get_course_int_id();
229
        $params['parent_id'] = '0';
230
        $params['type'] = 'default';
231
        $id = parent::save($params, $showQuery);
232
        if (!empty($id)) {
233
            //event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
234
        }
235
236
        return $id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $id also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
237
    }
238
239
    /**
240
     * @param int $id
241
     */
242
    public function delete($id)
243
    {
244
        parent::delete($id);
245
        //event_system(LOG_CAREER_DELETE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
246
    }
247
248
    /**
249
     * @param int $id
250
     *
251
     * @return string
252
     */
253
    public function get_url($id)
254
    {
255
        return api_get_path(WEB_AJAX_PATH).'timeline.ajax.php?a=get_timeline_content&id='.intval($id);
256
    }
257
258
    /**
259
     * @param int $id
260
     *
261
     * @return array
262
     */
263
    public function get_timeline_content($id)
264
    {
265
        $timeline = [];
266
        $course_id = api_get_course_int_id();
267
        $timeline['timeline'] = $this->process_item($this->get($id));
268
        $items = $this->process_items($this->get_all(['parent_id = ? AND c_id = ? ' => [$id, $course_id]]));
269
        $timeline['timeline']['date'] = $items;
270
271
        return $timeline;
272
    }
273
274
    public function process_items($items)
275
    {
276
        foreach ($items as &$item) {
277
            $item = $this->process_item($item);
278
        }
279
        $new_array = [];
280
        foreach ($items as $item) {
281
            $new_array[] = $item;
282
        }
283
284
        return $new_array;
285
    }
286
287
    public function process_item($item)
288
    {
289
        $item['startDate'] = $item['start_date'];
290
        unset($item['start_date']);
291
        if (!empty($item['end_date'])) {
292
            $item['endDate'] = $item['end_date'];
293
        } else {
294
            unset($item['endDate']);
295
        }
296
        unset($item['end_date']);
297
        // Assets
298
        $item['asset'] = [
299
            'media' => $item['media'],
300
            'credit' => $item['media_credit'],
301
            'caption' => $item['media_caption'],
302
        ];
303
304
        //Cleaning items
305
        unset($item['id']);
306
        if (empty($item['type'])) {
307
            unset($item['type']);
308
        }
309
        unset($item['media']);
310
        unset($item['media_credit']);
311
        unset($item['media_caption']);
312
        unset($item['status']);
313
        unset($item['title_slide']);
314
        unset($item['parent_id']);
315
        unset($item['c_id']);
316
317
        return $item;
318
    }
319
}
320