career.lib.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides methods for the notebook management.
  5. * Include/require it in your code to use its features.
  6. * @package chamilo.library
  7. */
  8. /**
  9. * Code
  10. */
  11. require_once 'promotion.lib.php';
  12. require_once 'fckeditor/fckeditor.php';
  13. define ('CAREER_STATUS_ACTIVE', 1);
  14. define ('CAREER_STATUS_INACTIVE',0);
  15. /**
  16. * @package chamilo.library
  17. */
  18. class Career extends Model {
  19. var $table;
  20. var $columns = array('id', 'name','description','status','created_at','updated_at');
  21. public function __construct() {
  22. $this->table = Database::get_main_table(TABLE_CAREER);
  23. }
  24. public function get_all($where_conditions = array()) {
  25. return Database::select('*',$this->table, array('where'=>$where_conditions,'order' =>'name ASC'));
  26. }
  27. /**
  28. * Update all promotion status by career
  29. * @param int career id
  30. * @param int status (1 or 0)
  31. */
  32. public function update_all_promotion_status_by_career_id($career_id, $status) {
  33. $promotion = new Promotion();
  34. $promotion_list = $promotion->get_all_promotions_by_career_id($career_id);
  35. if (!empty($promotion_list)) {
  36. foreach($promotion_list as $item) {
  37. $params['id'] = $item['id'];
  38. $params['status'] = $status;
  39. $promotion->update($params);
  40. $promotion->update_all_sessions_status_by_promotion_id($params['id'], $status);
  41. }
  42. }
  43. }
  44. /**
  45. * Displays the title + grid
  46. */
  47. public function display() {
  48. // action links
  49. echo '<div class="actions" style="margin-bottom:20px">';
  50. echo '<a href="career_dashboard.php">'.Display::return_icon('back.png',get_lang('Back'),'','32').'</a>';
  51. echo '<a href="'.api_get_self().'?action=add">'.Display::return_icon('new_career.png',get_lang('Add'),'','32').'</a>';
  52. echo '</div>';
  53. echo Display::grid_html('careers');
  54. }
  55. public function get_status_list() {
  56. return array(CAREER_STATUS_ACTIVE => get_lang('Unarchived'), CAREER_STATUS_INACTIVE => get_lang('Archived'));
  57. }
  58. /**
  59. * Returns a Form validator Obj
  60. * @todo the form should be auto generated
  61. * @param string url
  62. * @param string action add, edit
  63. * @return obj form validator obj
  64. */
  65. public function return_form($url, $action) {
  66. $oFCKeditor = new FCKeditor('description') ;
  67. $oFCKeditor->ToolbarSet = 'careers';
  68. $oFCKeditor->Width = '100%';
  69. $oFCKeditor->Height = '200';
  70. $oFCKeditor->Value = '';
  71. $oFCKeditor->CreateHtml();
  72. $form = new FormValidator('career', 'post', $url);
  73. // Settting the form elements
  74. $header = get_lang('Add');
  75. if ($action == 'edit') {
  76. $header = get_lang('Modify');
  77. }
  78. $form->addElement('header', '', $header);
  79. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  80. $form->addElement('hidden', 'id', $id);
  81. $form->addElement('text', 'name', get_lang('Name'), array('size' => '70'));
  82. $form->add_html_editor('description', get_lang('Description'), false, false, array('ToolbarSet' => 'careers','Width' => '100%', 'Height' => '250'));
  83. $status_list = $this->get_status_list();
  84. $form->addElement('select', 'status', get_lang('Status'), $status_list);
  85. if ($action == 'edit') {
  86. $form->addElement('text', 'created_at', get_lang('CreatedAt'));
  87. $form->freeze('created_at');
  88. }
  89. if ($action == 'edit') {
  90. $form->addElement('style_submit_button', 'submit', get_lang('Modify'), 'class="save"');
  91. } else {
  92. $form->addElement('style_submit_button', 'submit', get_lang('Add'), 'class="save"');
  93. }
  94. // Setting the defaults
  95. $defaults = $this->get($id);
  96. if (!empty($defaults['created_at'])) {
  97. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  98. }
  99. if (!empty($defaults['updated_at'])) {
  100. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  101. }
  102. $form->setDefaults($defaults);
  103. // Setting the rules
  104. $form->addRule('name', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  105. return $form;
  106. }
  107. /**
  108. * Copies the career to a new one
  109. * @param integer Career ID
  110. * @param boolean Whether or not to copy the promotions inside
  111. * @return integer New career ID on success, false on failure
  112. */
  113. public function copy($id, $copy_promotions = false) {
  114. $career = $this->get($id);
  115. $new = array();
  116. foreach ($career as $key => $val) {
  117. switch ($key) {
  118. case 'id':
  119. case 'updated_at':
  120. break;
  121. case 'name':
  122. $val .= ' '.get_lang('Copy');
  123. $new[$key] = $val;
  124. break;
  125. case 'created_at':
  126. $val = api_get_utc_datetime();
  127. $new[$key] = $val;
  128. break;
  129. default:
  130. $new[$key] = $val;
  131. break;
  132. }
  133. }
  134. $cid = $this->save($new);
  135. if ($copy_promotions) {
  136. //Now also copy each session of the promotion as a new session and register it inside the promotion
  137. $promotion = new Promotion();
  138. $promo_list = $promotion->get_all_promotions_by_career_id($id);
  139. if (!empty($promo_list)) {
  140. foreach($promo_list as $item) {
  141. $pid = $promotion->copy($item['id'], $cid);
  142. }
  143. }
  144. }
  145. return $cid;
  146. }
  147. public function save($params) {
  148. $id = parent::save($params);
  149. if (!empty($id)) {
  150. event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
  151. }
  152. return $id;
  153. }
  154. public function delete($id) {
  155. parent::delete($id);
  156. event_system(LOG_CAREER_DELETE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
  157. }
  158. }