timeline.lib.php 9.1 KB

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