promotion.lib.php 7.7 KB

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