timeline.lib.php 8.8 KB

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