promotion.lib.php 7.6 KB

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