career.lib.php 6.9 KB

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