career.lib.php 5.3 KB

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