promotion.lib.php 8.6 KB

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