career.lib.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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">'.
  75. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
  76. echo '<a href="'.api_get_self().'?action=add">'.
  77. Display::return_icon('new_career.png', get_lang('Add'), '', ICON_SIZE_MEDIUM).'</a>';
  78. echo '</div>';
  79. echo Display::grid_html('careers');
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function get_status_list()
  85. {
  86. return array(
  87. CAREER_STATUS_ACTIVE => get_lang('Unarchived'),
  88. CAREER_STATUS_INACTIVE => get_lang('Archived')
  89. );
  90. }
  91. /**
  92. * Returns a Form validator Obj
  93. * @todo the form should be auto generated
  94. * @param string $url
  95. * @param string $action add, edit
  96. * @return FormValidator
  97. */
  98. public function return_form($url, $action)
  99. {
  100. $form = new FormValidator('career', 'post', $url);
  101. // Setting the form elements
  102. $header = get_lang('Add');
  103. if ($action == 'edit') {
  104. $header = get_lang('Modify');
  105. }
  106. $form->addElement('header', $header);
  107. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  108. $form->addElement('hidden', 'id', $id);
  109. $form->addElement('text', 'name', get_lang('Name'), array('size' => '70'));
  110. $form->addHtmlEditor(
  111. 'description',
  112. get_lang('Description'),
  113. false,
  114. false,
  115. array(
  116. 'ToolbarSet' => 'Careers',
  117. 'Width' => '100%',
  118. 'Height' => '250'
  119. )
  120. );
  121. $status_list = $this->get_status_list();
  122. $form->addElement('select', 'status', get_lang('Status'), $status_list);
  123. if ($action == 'edit') {
  124. $form->addElement('text', 'created_at', get_lang('CreatedAt'));
  125. $form->freeze('created_at');
  126. }
  127. if ($action == 'edit') {
  128. $form->addButtonSave(get_lang('Modify'), 'submit');
  129. } else {
  130. $form->addButtonCreate(get_lang('Add'), 'submit');
  131. }
  132. // Setting the defaults
  133. $defaults = $this->get($id);
  134. if (!empty($defaults['created_at'])) {
  135. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  136. }
  137. if (!empty($defaults['updated_at'])) {
  138. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  139. }
  140. $form->setDefaults($defaults);
  141. // Setting the rules
  142. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  143. return $form;
  144. }
  145. /**
  146. * Copies the career to a new one
  147. * @param integer Career ID
  148. * @param boolean Whether or not to copy the promotions inside
  149. * @return integer New career ID on success, false on failure
  150. */
  151. public function copy($id, $copy_promotions = false)
  152. {
  153. $career = $this->get($id);
  154. $new = array();
  155. foreach ($career as $key => $val) {
  156. switch ($key) {
  157. case 'id':
  158. case 'updated_at':
  159. break;
  160. case 'name':
  161. $val .= ' '.get_lang('CopyLabelSuffix');
  162. $new[$key] = $val;
  163. break;
  164. case 'created_at':
  165. $val = api_get_utc_datetime();
  166. $new[$key] = $val;
  167. break;
  168. default:
  169. $new[$key] = $val;
  170. break;
  171. }
  172. }
  173. $cid = $this->save($new);
  174. if ($copy_promotions) {
  175. //Now also copy each session of the promotion as a new session and register it inside the promotion
  176. $promotion = new Promotion();
  177. $promo_list = $promotion->get_all_promotions_by_career_id($id);
  178. if (!empty($promo_list)) {
  179. foreach ($promo_list as $item) {
  180. $promotion->copy($item['id'], $cid, true);
  181. }
  182. }
  183. }
  184. return $cid;
  185. }
  186. /**
  187. * @param int $career_id
  188. * @return bool
  189. */
  190. public function get_status($career_id)
  191. {
  192. $TBL_CAREER = Database::get_main_table(TABLE_CAREER);
  193. $career_id = intval($career_id);
  194. $sql = "SELECT status FROM $TBL_CAREER WHERE id = '$career_id'";
  195. $result = Database::query($sql);
  196. if (Database::num_rows($result) > 0) {
  197. $data = Database::fetch_array($result);
  198. return $data['status'];
  199. } else {
  200. return false;
  201. }
  202. }
  203. /**
  204. * @param array $params
  205. * @param bool $show_query
  206. * @return bool
  207. */
  208. public function save($params, $show_query = false)
  209. {
  210. if (isset($params['description'])) {
  211. $params['description'] = Security::remove_XSS($params['description']);
  212. }
  213. $id = parent::save($params);
  214. if (!empty($id)) {
  215. Event::addEvent(
  216. LOG_CAREER_CREATE,
  217. LOG_CAREER_ID,
  218. $id,
  219. api_get_utc_datetime(),
  220. api_get_user_id()
  221. );
  222. }
  223. return $id;
  224. }
  225. /**
  226. * Delete a record from the career table and report in the default events log table
  227. * @param int $id The ID of the career to delete
  228. * @return bool True if the career could be deleted, false otherwise
  229. */
  230. public function delete($id)
  231. {
  232. $res = parent::delete($id);
  233. if ($res) {
  234. Event::addEvent(
  235. LOG_CAREER_DELETE,
  236. LOG_CAREER_ID,
  237. $id,
  238. api_get_utc_datetime(),
  239. api_get_user_id()
  240. );
  241. }
  242. return $res;
  243. }
  244. /**
  245. * Update the career table with the given params
  246. * @param array $params The field values to be set
  247. * @return bool Returns true if the record could be updated, false otherwise
  248. */
  249. public function update($params)
  250. {
  251. if (isset($params['description'])) {
  252. $params['description'] = Security::remove_XSS($params['description']);
  253. }
  254. return parent::update($params);
  255. }
  256. }