career.lib.php 6.3 KB

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