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. * @package chamilo.library
  8. */
  9. class Timeline extends Model
  10. {
  11. public $table;
  12. public $columns = [
  13. 'headline',
  14. 'type',
  15. 'start_date',
  16. 'end_date',
  17. 'text',
  18. 'media',
  19. 'media_credit',
  20. 'media_caption',
  21. 'title_slide',
  22. 'parent_id',
  23. 'status',
  24. 'c_id',
  25. ];
  26. public $is_course_model = true;
  27. /**
  28. * Timeline constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->table = Database::get_course_table(TABLE_TIMELINE);
  33. }
  34. /**
  35. * Get the count of elements.
  36. *
  37. * @return int
  38. */
  39. public function get_count()
  40. {
  41. $course_id = api_get_course_int_id();
  42. $row = Database::select(
  43. 'count(*) as count',
  44. $this->table,
  45. [
  46. 'where' => [
  47. 'parent_id = ? AND c_id = ?' => [
  48. '0',
  49. $course_id,
  50. ],
  51. ],
  52. ],
  53. 'first'
  54. );
  55. return $row['count'];
  56. }
  57. /**
  58. * @param array $where_conditions
  59. *
  60. * @return array
  61. */
  62. public function get_all($where_conditions = [])
  63. {
  64. return Database::select(
  65. '*',
  66. $this->table,
  67. ['where' => $where_conditions, 'order' => 'headline ASC']
  68. );
  69. }
  70. /**
  71. * Displays the title + grid.
  72. */
  73. public function listing()
  74. {
  75. // action links
  76. $html = '<div class="actions">';
  77. $html .= '<a href="'.api_get_self().'?action=add">'.
  78. Display::return_icon('add.png', get_lang('Add'), '', '32').'</a>';
  79. $html .= '</div>';
  80. $html .= Display::grid_html('timelines');
  81. return $html;
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function get_status_list()
  87. {
  88. return [
  89. TIMELINE_STATUS_ACTIVE => get_lang('Active'),
  90. TIMELINE_STATUS_INACTIVE => get_lang('Inactive'),
  91. ];
  92. }
  93. /**
  94. * Returns a Form validator Obj.
  95. *
  96. * @todo the form should be auto generated
  97. *
  98. * @param string url
  99. * @param string action add, edit
  100. *
  101. * @return obj form validator obj
  102. */
  103. public function return_form($url, $action)
  104. {
  105. $form = new FormValidator('timeline', 'post', $url);
  106. // Setting the form elements
  107. $header = get_lang('Add');
  108. if ($action == 'edit') {
  109. $header = get_lang('Modify');
  110. }
  111. $form->addElement('header', $header);
  112. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  113. $form->addElement('hidden', 'id', $id);
  114. $form->addElement('text', 'headline', get_lang('Name'), ['size' => '70']);
  115. $status_list = $this->get_status_list();
  116. $form->addElement('select', 'status', get_lang('Status'), $status_list);
  117. if ($action == 'edit') {
  118. //$form->addElement('text', 'created_at', get_lang('CreatedAt'));
  119. //$form->freeze('created_at');
  120. }
  121. if ($action == 'edit') {
  122. $form->addButtonSave(get_lang('Modify'), 'submit');
  123. } else {
  124. $form->addButtonCreate(get_lang('Add'), 'submit');
  125. }
  126. $form->addRule('headline', get_lang('ThisFieldIsRequired'), 'required');
  127. // Setting the defaults
  128. $defaults = $this->get($id);
  129. /*if (!empty($defaults['created_at'])) {
  130. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  131. }
  132. if (!empty($defaults['updated_at'])) {
  133. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  134. }*/
  135. $form->setDefaults($defaults);
  136. // Setting the rules
  137. $form->addRule('headline', get_lang('ThisFieldIsRequired'), 'required');
  138. return $form;
  139. }
  140. /**
  141. * @param $url
  142. * @param $action
  143. *
  144. * @return FormValidator
  145. */
  146. public function return_item_form($url, $action)
  147. {
  148. $form = new FormValidator('item_form', 'post', $url);
  149. // Setting the form elements
  150. $header = get_lang('Add');
  151. if ($action == 'edit') {
  152. $header = get_lang('Modify');
  153. }
  154. $form->addElement('header', $header);
  155. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  156. $parent_id = isset($_GET['parent_id']) ? intval($_GET['parent_id']) : '';
  157. $form->addElement('hidden', 'parent_id', $parent_id);
  158. $form->addElement('hidden', 'id', $id);
  159. $form->addElement('text', 'headline', get_lang('Name'));
  160. //@todo fix this
  161. $form->addElement('text', 'start_date', get_lang('StartDate'), ['size' => '70']);
  162. $form->addElement('text', 'end_date', get_lang('EndDate'), ['size' => '70']);
  163. $form->addElement('textarea', 'text', get_lang('TimelineItemText'));
  164. $form->addElement('text', 'media', get_lang('TimelineItemMedia'), ['size' => '70']);
  165. $form->addElement('text', 'media_caption', get_lang('TimelineItemMediaCaption'), ['size' => '70']);
  166. $form->addElement('text', 'media_credit', get_lang('TimelineItemMediaCredit'), ['size' => '70']);
  167. $form->addElement('text', 'title_slide', get_lang('TimelineItemTitleSlide'), ['size' => '70']);
  168. $form->addRule('headline', get_lang('ThisFieldIsRequired'), 'required');
  169. $form->addRule('start_date', get_lang('ThisFieldIsRequired'), 'required');
  170. if ($action == 'edit') {
  171. // Setting the defaults
  172. $defaults = $this->get($id);
  173. $form->addButtonSave(get_lang('Modify'), 'submit');
  174. } else {
  175. $form->addButtonCreate(get_lang('Add'), 'submit');
  176. }
  177. $form->setDefaults($defaults);
  178. // Setting the rules
  179. $form->addRule('headline', get_lang('ThisFieldIsRequired'), 'required');
  180. return $form;
  181. }
  182. /**
  183. * @param array $params
  184. *
  185. * @return bool
  186. */
  187. public function save_item($params)
  188. {
  189. $params['c_id'] = api_get_course_int_id();
  190. $id = parent::save($params);
  191. if (!empty($id)) {
  192. //event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
  193. }
  194. return $id;
  195. }
  196. /**
  197. * @param array $params
  198. *
  199. * @return bool
  200. */
  201. public function save($params)
  202. {
  203. $params['c_id'] = api_get_course_int_id();
  204. $params['parent_id'] = '0';
  205. $params['type'] = 'default';
  206. $id = parent::save($params);
  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. }