promotion.lib.php 9.5 KB

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