promotion.lib.php 7.7 KB

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