career.lib.php 6.6 KB

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