promotion.lib.php 9.0 KB

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