promotion.lib.php 6.9 KB

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