123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Class Timeline
- * Timeline model class definition.
- */
- class Timeline extends Model
- {
- public $table;
- public $columns = [
- 'headline',
- 'type',
- 'start_date',
- 'end_date',
- 'text',
- 'media',
- 'media_credit',
- 'media_caption',
- 'title_slide',
- 'parent_id',
- 'status',
- 'c_id',
- ];
- public $is_course_model = true;
- /**
- * Timeline constructor.
- */
- public function __construct()
- {
- parent::__construct();
- $this->table = Database::get_course_table(TABLE_TIMELINE);
- }
- /**
- * Get the count of elements.
- *
- * @return int
- */
- public function get_count()
- {
- $course_id = api_get_course_int_id();
- $row = Database::select(
- 'count(*) as count',
- $this->table,
- [
- 'where' => [
- 'parent_id = ? AND c_id = ?' => [
- '0',
- $course_id,
- ],
- ],
- ],
- 'first'
- );
- return $row['count'];
- }
- /**
- * @param array $where_conditions
- *
- * @return array
- */
- public function get_all($where_conditions = [])
- {
- return Database::select(
- '*',
- $this->table,
- ['where' => $where_conditions, 'order' => 'headline ASC']
- );
- }
- /**
- * Displays the title + grid.
- */
- public function listing()
- {
- // action links
- $html = '<div class="actions">';
- $html .= '<a href="'.api_get_self().'?action=add">'.
- Display::return_icon('add.png', get_lang('Add'), '', '32').'</a>';
- $html .= '</div>';
- $html .= Display::grid_html('timelines');
- return $html;
- }
- /**
- * @return array
- */
- public function get_status_list()
- {
- return [
- TIMELINE_STATUS_ACTIVE => get_lang('active'),
- TIMELINE_STATUS_INACTIVE => get_lang('inactive'),
- ];
- }
- /**
- * Returns a Form validator Obj.
- *
- * @todo the form should be auto generated
- *
- * @param string url
- * @param string action add, edit
- *
- * @return obj form validator obj
- */
- public function return_form($url, $action)
- {
- $form = new FormValidator('timeline', 'post', $url);
- // Setting the form elements
- $header = get_lang('Add');
- if ($action == 'edit') {
- $header = get_lang('Edit');
- }
- $form->addElement('header', $header);
- $id = isset($_GET['id']) ? intval($_GET['id']) : '';
- $form->addElement('hidden', 'id', $id);
- $form->addElement('text', 'headline', get_lang('Name'), ['size' => '70']);
- $status_list = $this->get_status_list();
- $form->addElement('select', 'status', get_lang('Status'), $status_list);
- if ($action == 'edit') {
- //$form->addElement('text', 'created_at', get_lang('Created at'));
- //$form->freeze('created_at');
- }
- if ($action == 'edit') {
- $form->addButtonSave(get_lang('Edit'), 'submit');
- } else {
- $form->addButtonCreate(get_lang('Add'), 'submit');
- }
- $form->addRule('headline', get_lang('Required field'), 'required');
- // Setting the defaults
- $defaults = $this->get($id);
- /*if (!empty($defaults['created_at'])) {
- $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
- }
- if (!empty($defaults['updated_at'])) {
- $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
- }*/
- $form->setDefaults($defaults);
- // Setting the rules
- $form->addRule('headline', get_lang('Required field'), 'required');
- return $form;
- }
- /**
- * @param $url
- * @param $action
- *
- * @return FormValidator
- */
- public function return_item_form($url, $action)
- {
- $form = new FormValidator('item_form', 'post', $url);
- // Setting the form elements
- $header = get_lang('Add');
- if ($action == 'edit') {
- $header = get_lang('Edit');
- }
- $form->addElement('header', $header);
- $id = isset($_GET['id']) ? intval($_GET['id']) : '';
- $parent_id = isset($_GET['parent_id']) ? intval($_GET['parent_id']) : '';
- $form->addElement('hidden', 'parent_id', $parent_id);
- $form->addElement('hidden', 'id', $id);
- $form->addElement('text', 'headline', get_lang('Name'));
- //@todo fix this
- $form->addElement('text', 'start_date', get_lang('Start Date'), ['size' => '70']);
- $form->addElement('text', 'end_date', get_lang('End Date'), ['size' => '70']);
- $form->addElement('textarea', 'text', get_lang('Text'));
- $form->addElement('text', 'media', get_lang('Media'), ['size' => '70']);
- $form->addElement('text', 'media_caption', get_lang('MediaCaption'), ['size' => '70']);
- $form->addElement('text', 'media_credit', get_lang('MediaCredit'), ['size' => '70']);
- $form->addElement('text', 'title_slide', get_lang('Slider title'), ['size' => '70']);
- $form->addRule('headline', get_lang('Required field'), 'required');
- $form->addRule('start_date', get_lang('Required field'), 'required');
- if ($action == 'edit') {
- // Setting the defaults
- $defaults = $this->get($id);
- $form->addButtonSave(get_lang('Edit'), 'submit');
- } else {
- $form->addButtonCreate(get_lang('Add'), 'submit');
- }
- $form->setDefaults($defaults);
- // Setting the rules
- $form->addRule('headline', get_lang('Required field'), 'required');
- return $form;
- }
- /**
- * @param array $params
- *
- * @return bool
- */
- public function save_item($params)
- {
- $params['c_id'] = api_get_course_int_id();
- $id = parent::save($params);
- if (!empty($id)) {
- //event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
- }
- return $id;
- }
- /**
- * @param array $params
- * @param bool $showQuery
- *
- * @return bool
- */
- public function save($params, $showQuery = false)
- {
- $params['c_id'] = api_get_course_int_id();
- $params['parent_id'] = '0';
- $params['type'] = 'default';
- $id = parent::save($params, $showQuery);
- if (!empty($id)) {
- //event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
- }
- return $id;
- }
- /**
- * @param int $id
- */
- public function delete($id)
- {
- parent::delete($id);
- //event_system(LOG_CAREER_DELETE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
- }
- /**
- * @param int $id
- *
- * @return string
- */
- public function get_url($id)
- {
- return api_get_path(WEB_AJAX_PATH).'timeline.ajax.php?a=get_timeline_content&id='.intval($id);
- }
- /**
- * @param int $id
- *
- * @return array
- */
- public function get_timeline_content($id)
- {
- $timeline = [];
- $course_id = api_get_course_int_id();
- $timeline['timeline'] = $this->process_item($this->get($id));
- $items = $this->process_items($this->get_all(['parent_id = ? AND c_id = ? ' => [$id, $course_id]]));
- $timeline['timeline']['date'] = $items;
- return $timeline;
- }
- public function process_items($items)
- {
- foreach ($items as &$item) {
- $item = $this->process_item($item);
- }
- $new_array = [];
- foreach ($items as $item) {
- $new_array[] = $item;
- }
- return $new_array;
- }
- public function process_item($item)
- {
- $item['startDate'] = $item['start_date'];
- unset($item['start_date']);
- if (!empty($item['end_date'])) {
- $item['endDate'] = $item['end_date'];
- } else {
- unset($item['endDate']);
- }
- unset($item['end_date']);
- // Assets
- $item['asset'] = [
- 'media' => $item['media'],
- 'credit' => $item['media_credit'],
- 'caption' => $item['media_caption'],
- ];
- //Cleaning items
- unset($item['id']);
- if (empty($item['type'])) {
- unset($item['type']);
- }
- unset($item['media']);
- unset($item['media_credit']);
- unset($item['media_caption']);
- unset($item['status']);
- unset($item['title_slide']);
- unset($item['parent_id']);
- unset($item['c_id']);
- return $item;
- }
- }
|