promotion.lib.php 7.5 KB

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