career.lib.php 7.0 KB

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