timeline.lib.php 8.6 KB

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