Completed
Push — master ( 4fc9f8...d0e06e )
by Julito
12:04
created

Timeline::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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