promotion.lib.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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">'.
  151. Display::return_icon(
  152. 'back.png',
  153. get_lang('Back'),
  154. '',
  155. '32'
  156. )
  157. .'</a>';
  158. echo '<a href="'.api_get_self().'?action=add">'.
  159. Display::return_icon(
  160. 'new_promotion.png',
  161. get_lang('Add'),
  162. '',
  163. '32'
  164. ).'</a>';
  165. echo '<a href="'.api_get_path(WEB_CODE_PATH).'session/session_add.php">'.
  166. Display::return_icon(
  167. 'new_session.png',
  168. get_lang('AddSession'),
  169. '',
  170. '32'
  171. ).'</a>';
  172. echo '</div>';
  173. echo Display::grid_html('promotions');
  174. }
  175. /**
  176. * Update all session status by promotion
  177. * @param int $promotion_id
  178. * @param int $status (1, 0)
  179. */
  180. public function update_all_sessions_status_by_promotion_id(
  181. $promotion_id,
  182. $status
  183. ) {
  184. $sessionList = SessionManager::get_all_sessions_by_promotion($promotion_id);
  185. if (!empty($sessionList)) {
  186. foreach ($sessionList as $item) {
  187. SessionManager::set_session_status($item['id'], $status);
  188. }
  189. }
  190. }
  191. /**
  192. * Returns a Form validator Obj
  193. * @param string $url
  194. * @param string $action
  195. *
  196. * @return FormValidator
  197. */
  198. public function return_form($url, $action = 'add')
  199. {
  200. $form = new FormValidator('promotion', 'post', $url);
  201. // Setting the form elements
  202. $header = get_lang('Add');
  203. if ($action == 'edit') {
  204. $header = get_lang('Modify');
  205. }
  206. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  207. $form->addElement('header', '', $header);
  208. $form->addElement('hidden', 'id', $id);
  209. $form->addElement(
  210. 'text',
  211. 'name',
  212. get_lang('Name'),
  213. array('size' => '70', 'id' => 'name')
  214. );
  215. $form->addHtmlEditor(
  216. 'description',
  217. get_lang('Description'),
  218. false,
  219. false,
  220. array(
  221. 'ToolbarSet' => 'Careers',
  222. 'Width' => '100%',
  223. 'Height' => '250'
  224. )
  225. );
  226. $career = new Career();
  227. $careers = $career->get_all();
  228. $career_list = array();
  229. foreach ($careers as $item) {
  230. $career_list[$item['id']] = $item['name'];
  231. }
  232. $form->addSelect(
  233. 'career_id',
  234. get_lang('Career'),
  235. $career_list,
  236. ['id' => 'career_id']
  237. );
  238. $status_list = $this->get_status_list();
  239. $form->addElement('select', 'status', get_lang('Status'), $status_list);
  240. if ($action == 'edit') {
  241. $form->addElement('text', 'created_at', get_lang('CreatedAt'));
  242. $form->freeze('created_at');
  243. }
  244. if ($action == 'edit') {
  245. $form->addButtonSave(get_lang('Modify'), 'submit');
  246. } else {
  247. $form->addButtonCreate(get_lang('Add'), 'submit');
  248. }
  249. // Setting the defaults
  250. $defaults = $this->get($id);
  251. if (!empty($defaults['created_at'])) {
  252. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  253. }
  254. if (!empty($defaults['updated_at'])) {
  255. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  256. }
  257. $form->setDefaults($defaults);
  258. // Setting the rules
  259. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  260. return $form;
  261. }
  262. /**
  263. * @param array $params
  264. *
  265. * @param bool $show_query
  266. *
  267. * @return bool
  268. */
  269. public function save($params, $show_query = false)
  270. {
  271. $id = parent::save($params, $show_query);
  272. if (!empty($id)) {
  273. Event::addEvent(
  274. LOG_PROMOTION_CREATE,
  275. LOG_PROMOTION_ID,
  276. $id,
  277. api_get_utc_datetime(),
  278. api_get_user_id()
  279. );
  280. }
  281. return $id;
  282. }
  283. /**
  284. * @param int $id
  285. *
  286. * @return bool
  287. */
  288. public function delete($id)
  289. {
  290. if (parent::delete($id)) {
  291. SessionManager::clear_session_ref_promotion($id);
  292. Event::addEvent(
  293. LOG_PROMOTION_DELETE,
  294. LOG_PROMOTION_ID,
  295. $id,
  296. api_get_utc_datetime(),
  297. api_get_user_id()
  298. );
  299. } else {
  300. return false;
  301. }
  302. }
  303. }