career.lib.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. require_once 'fckeditor/fckeditor.php';
  13. define ('CAREER_STATUS_ACTIVE', 1);
  14. define ('CAREER_STATUS_INACTIVE',0);
  15. /**
  16. * @package chamilo.library
  17. */
  18. /**
  19. * Class Career
  20. */
  21. class Career extends Model
  22. {
  23. public $table;
  24. public $columns = array('id', 'name','description','status','created_at','updated_at');
  25. public function __construct()
  26. {
  27. $this->table = Database::get_main_table(TABLE_CAREER);
  28. }
  29. /**
  30. * Get the count of elements
  31. */
  32. public function get_count()
  33. {
  34. $row = Database::select('count(*) as count', $this->table, array(),'first');
  35. return $row['count'];
  36. }
  37. /**
  38. * @param array $where_conditions
  39. * @return array
  40. */
  41. public function get_all($where_conditions = array())
  42. {
  43. return Database::select('*',$this->table, array('where'=>$where_conditions,'order' =>'name ASC'));
  44. }
  45. /**
  46. * Update all promotion status by career
  47. * @param int career id
  48. * @param int status (1 or 0)
  49. */
  50. public function update_all_promotion_status_by_career_id($career_id, $status)
  51. {
  52. $promotion = new Promotion();
  53. $promotion_list = $promotion->get_all_promotions_by_career_id($career_id);
  54. if (!empty($promotion_list)) {
  55. foreach($promotion_list as $item) {
  56. $params['id'] = $item['id'];
  57. $params['status'] = $status;
  58. $promotion->update($params);
  59. $promotion->update_all_sessions_status_by_promotion_id($params['id'], $status);
  60. }
  61. }
  62. }
  63. /**
  64. * Displays the title + grid
  65. */
  66. public function display()
  67. {
  68. echo '<div class="actions" style="margin-bottom:20px">';
  69. echo '<a href="career_dashboard.php">'.Display::return_icon('back.png',get_lang('Back'),'','32').'</a>';
  70. echo '<a href="'.api_get_self().'?action=add">'.Display::return_icon('new_career.png',get_lang('Add'),'','32').'</a>';
  71. echo '</div>';
  72. echo Display::grid_html('careers');
  73. }
  74. /**
  75. * @return array
  76. */
  77. public function get_status_list()
  78. {
  79. return array(CAREER_STATUS_ACTIVE => get_lang('Unarchived'), CAREER_STATUS_INACTIVE => get_lang('Archived'));
  80. }
  81. /**
  82. * Returns a Form validator Obj
  83. * @todo the form should be auto generated
  84. * @param string url
  85. * @param string action add, edit
  86. * @return obj form validator obj
  87. */
  88. public function return_form($url, $action)
  89. {
  90. $oFCKeditor = new FCKeditor('description');
  91. $oFCKeditor->ToolbarSet = 'careers';
  92. $oFCKeditor->Width = '100%';
  93. $oFCKeditor->Height = '200';
  94. $oFCKeditor->Value = '';
  95. $oFCKeditor->CreateHtml();
  96. $form = new FormValidator('career', 'post', $url);
  97. // Setting the form elements
  98. $header = get_lang('Add');
  99. if ($action == 'edit') {
  100. $header = get_lang('Modify');
  101. }
  102. $form->addElement('header', $header);
  103. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  104. $form->addElement('hidden', 'id', $id);
  105. $form->addElement('text', 'name', get_lang('Name'), array('size' => '70'));
  106. $form->add_html_editor('description', get_lang('Description'), false, false, array('ToolbarSet' => 'careers','Width' => '100%', 'Height' => '250'));
  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. }