career.lib.php 6.8 KB

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