timeline.lib.php 8.6 KB

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