PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/php/main/inc/lib/timeline.lib.php

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