promotion.lib.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides methods for the promotion 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 'career.lib.php';
  10. require_once 'fckeditor/fckeditor.php';
  11. define ('PROMOTION_STATUS_ACTIVE', 1);
  12. define ('PROMOTION_STATUS_INACTIVE',0);
  13. class Promotion extends Model {
  14. var $table;
  15. var $columns = array('id','name','description','career_id','status','created_at','updated_at');
  16. public function __construct() {
  17. $this->table = Database::get_main_table(TABLE_PROMOTION);
  18. }
  19. /**
  20. * Gets all promotions by career id
  21. * @param int career id
  22. * @return array results
  23. */
  24. public function get_all_promotions_by_career_id($career_id, $order = false) {
  25. return Database::select('*', $this->table, array('where'=>array('career_id = ?'=>$career_id),'order' =>$order));
  26. }
  27. public function get_status_list() {
  28. return array(PROMOTION_STATUS_ACTIVE => get_lang('Active'), PROMOTION_STATUS_INACTIVE => get_lang('Inactive'));
  29. }
  30. /**
  31. * Displays the title + grid
  32. * @return string html code
  33. */
  34. function display() {
  35. // action links
  36. echo '<div class="actions" style="margin-bottom:20px">';
  37. echo '<a href="career_dashboard.php">'.Display::return_icon('back.png',get_lang('Back'),'','32').'</a>';
  38. echo '<a href="'.api_get_self().'?action=add">'.Display::return_icon('new_promotion.png',get_lang('Add'),'','32').'</a>';
  39. echo '<a href="'.api_get_path(WEB_CODE_PATH).'admin/session_add.php">'.Display::return_icon('new_session.png',get_lang('AddSession'),'','32').'</a>';
  40. echo '</div>';
  41. echo Display::grid_html('promotions');
  42. }
  43. /**
  44. * Update all session status by promotion
  45. * @param int promotion id
  46. * @param int status (1, 0)
  47. */
  48. public function update_all_sessions_status_by_promotion_id($promotion_id, $status) {
  49. require_once api_get_path(LIBRARY_PATH).'sessionmanager.lib.php';
  50. $session_list = SessionManager::get_all_sessions_by_promotion($promotion_id);
  51. if (!empty($session_list)) {
  52. foreach($session_list as $item) {
  53. SessionManager::set_session_status($item['id'], $status);
  54. }
  55. }
  56. }
  57. /**
  58. * Returns a Form validator Obj
  59. * @todo the form should be auto generated
  60. * @param string url
  61. * @param string header name
  62. * @return obj form validator obj
  63. */
  64. function return_form($url, $action = 'add') {
  65. $oFCKeditor = new FCKeditor('description') ;
  66. $oFCKeditor->ToolbarSet = 'careers';
  67. $oFCKeditor->Width = '100%';
  68. $oFCKeditor->Height = '200';
  69. $oFCKeditor->Value = $message;
  70. $oFCKeditor->CreateHtml();
  71. $form = new FormValidator('promotion', 'post', $url);
  72. // Settting the form elements
  73. $header = get_lang('Add');
  74. if ($action == 'edit') {
  75. $header = get_lang('Modify');
  76. }
  77. $form->addElement('header', '', $header);
  78. $form->addElement('hidden', 'id', intval($_GET['id']));
  79. $form->addElement('text', 'name', get_lang('Name'), array('size' => '70','id' => 'name'));
  80. $form->add_html_editor('description', get_lang('Description'), false, false, array('ToolbarSet' => 'careers','Width' => '100%', 'Height' => '250'));
  81. $career = new Career();
  82. $careers = $career->get_all();
  83. $career_list = array();
  84. foreach($careers as $item) {
  85. $career_list[$item['id']] = $item['name'];
  86. }
  87. $form->addElement('select', 'career_id', get_lang('Career'), $career_list);
  88. $status_list = $this->get_status_list();
  89. $form->addElement('select', 'status', get_lang('Status'), $status_list);
  90. if ($action == 'edit') {
  91. $form->addElement('text', 'created_at', get_lang('CreatedAt'));
  92. $form->freeze('created_at');
  93. }
  94. $form->addElement('style_submit_button', 'submit', get_lang('Modify'), 'class="save"');
  95. // Setting the defaults
  96. $defaults = $this->get($_GET['id']);
  97. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  98. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  99. $form->setDefaults($defaults);
  100. // Setting the rules
  101. $form->addRule('name', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  102. return $form;
  103. }
  104. /**
  105. * Copies the promotion to a new one
  106. * @param integer Promotion ID
  107. * @param integer Career ID, in case we want to change it
  108. * @param boolean Whether or not to copy the sessions inside
  109. * @return integer New promotion ID on success, false on failure
  110. */
  111. public function copy($id, $career_id = null, $copy_sessions = false) {
  112. $promotion = $this->get($id);
  113. $new = array();
  114. foreach ($promotion as $key => $val) {
  115. switch ($key) {
  116. case 'id':
  117. case 'updated_at':
  118. break;
  119. case 'name':
  120. $val .= ' '.get_lang('Copy');
  121. $new[$key] = $val;
  122. break;
  123. case 'created_at':
  124. $val = api_get_utc_datetime();
  125. $new[$key] = $val;
  126. break;
  127. case 'career_id':
  128. if (!empty($career_id)) {
  129. $val = (int)$career_id;
  130. }
  131. $new[$key] = $val;
  132. default:
  133. $new[$key] = $val;
  134. break;
  135. }
  136. }
  137. $pid = false;
  138. if ($copy_sessions) {
  139. /**
  140. * When copying a session we do:
  141. * 1. Copy a new session from the source
  142. * 2. Copy all courses from the session (no user data, no user list)
  143. * 3. Create the promotion
  144. */
  145. require_once api_get_path(LIBRARY_PATH).'sessionmanager.lib.php';
  146. $session_list = SessionManager::get_all_sessions_by_promotion($id);
  147. if (!empty($session_list)) {
  148. $pid = $this->save($new);
  149. if (!empty($pid)) {
  150. foreach($session_list as $item) {
  151. $sid = SessionManager::copy_session($item['id'], true, false, true, true);
  152. if ($sid != 0) {
  153. SessionManager::suscribe_sessions_to_promotion($pid, array($sid));
  154. }
  155. }
  156. }
  157. }
  158. } else {
  159. $pid = $this->save($new);
  160. }
  161. return $pid;
  162. }
  163. }