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