timeline.lib.php 8.7 KB

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