career.lib.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. require_once 'model.lib.php';
  9. require_once 'promotion.lib.php';
  10. require_once 'fckeditor/fckeditor.php';
  11. define ('CAREER_STATUS_ACTIVE', 1);
  12. define ('CAREER_STATUS_INACTIVE',0);
  13. class Career extends Model {
  14. var $table;
  15. var $columns = array('id', 'name','description','status','created_at','updated_at');
  16. public function __construct() {
  17. $this->table = Database::get_main_table(TABLE_CAREER);
  18. }
  19. public function get_all($where_conditions = array()) {
  20. return Database::select('*',$this->table, array('where'=>$where_conditions,'order' =>'name ASC'));
  21. }
  22. /**
  23. * Update all promotion status by career
  24. * @param int career id
  25. * @param int status (1 or 0)
  26. */
  27. public function update_all_promotion_status_by_career_id($career_id, $status) {
  28. $promotion = new Promotion();
  29. $promotion_list = $promotion->get_all_promotions_by_career_id($career_id);
  30. if (!empty($promotion_list)) {
  31. foreach($promotion_list as $item) {
  32. $params['id'] = $item['id'];
  33. $params['status'] = $status;
  34. $promotion->update($params);
  35. $promotion->update_all_sessions_status_by_promotion_id($params['id'], $status);
  36. }
  37. }
  38. }
  39. /**
  40. * Displays the title + grid
  41. */
  42. public function display() {
  43. // action links
  44. echo '<div class="actions" style="margin-bottom:20px">';
  45. echo '<a href="career_dashboard.php">'.Display::return_icon('back.png',get_lang('Back'),'','32').'</a>';
  46. echo '<a href="'.api_get_self().'?action=add">'.Display::return_icon('new_career.png',get_lang('Add'),'','32').'</a>';
  47. echo '</div>';
  48. echo Display::grid_html('careers');
  49. }
  50. public function get_status_list() {
  51. return array(CAREER_STATUS_ACTIVE => get_lang('Unarchived'), CAREER_STATUS_INACTIVE => get_lang('Archived'));
  52. }
  53. /**
  54. * Returns a Form validator Obj
  55. * @todo the form should be auto generated
  56. * @param string url
  57. * @param string action add, edit
  58. * @return obj form validator obj
  59. */
  60. public function return_form($url, $action) {
  61. $oFCKeditor = new FCKeditor('description') ;
  62. $oFCKeditor->ToolbarSet = 'careers';
  63. $oFCKeditor->Width = '100%';
  64. $oFCKeditor->Height = '200';
  65. $oFCKeditor->Value = $message;
  66. $oFCKeditor->CreateHtml();
  67. $form = new FormValidator('career', 'post', $url);
  68. // Settting the form elements
  69. $header = get_lang('Add');
  70. if ($action == 'edit') {
  71. $header = get_lang('Modify');
  72. }
  73. $form->addElement('header', '', $header);
  74. $form->addElement('hidden', 'id',intval($_GET['id']));
  75. $form->addElement('text', 'name', get_lang('Name'), array('size' => '70'));
  76. $form->add_html_editor('description', get_lang('Description'), false, false, array('ToolbarSet' => 'careers','Width' => '100%', 'Height' => '250'));
  77. $status_list = $this->get_status_list();
  78. $form->addElement('select', 'status', get_lang('Status'), $status_list);
  79. if ($action == 'edit') {
  80. $form->addElement('text', 'created_at', get_lang('CreatedAt'));
  81. $form->freeze('created_at');
  82. }
  83. $form->addElement('style_submit_button', 'submit', get_lang('Modify'), 'class="save"');
  84. // Setting the defaults
  85. $defaults = $this->get($_GET['id']);
  86. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  87. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  88. $form->setDefaults($defaults);
  89. // Setting the rules
  90. $form->addRule('name', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  91. return $form;
  92. }
  93. /**
  94. * Copies the career to a new one
  95. * @param integer Career ID
  96. * @param boolean Whether or not to copy the promotions inside
  97. * @return integer New career ID on success, false on failure
  98. */
  99. public function copy($id, $copy_promotions = false) {
  100. $career = $this->get($id);
  101. $new = array();
  102. foreach ($career as $key => $val) {
  103. switch ($key) {
  104. case 'id':
  105. case 'updated_at':
  106. break;
  107. case 'name':
  108. $val .= ' '.get_lang('Copy');
  109. $new[$key] = $val;
  110. break;
  111. case 'created_at':
  112. $val = api_get_utc_datetime();
  113. $new[$key] = $val;
  114. break;
  115. default:
  116. $new[$key] = $val;
  117. break;
  118. }
  119. }
  120. $cid = $this->save($new);
  121. if ($copy_promotions) {
  122. //Now also copy each session of the promotion as a new session and register it inside the promotion
  123. $promotion = new Promotion();
  124. $promo_list = $promotion->get_all_promotions_by_career_id($id);
  125. if (!empty($promo_list)) {
  126. foreach($promo_list as $item) {
  127. $pid = $promotion->copy($item['id'], $cid);
  128. }
  129. }
  130. }
  131. return $cid;
  132. }
  133. }