career.lib.php 6.4 KB

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