You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
347 lines
9.5 KiB
347 lines
9.5 KiB
![]()
15 years ago
|
<?php
|
||
|
/* For licensing terms, see /license.txt */
|
||
![]()
14 years ago
|
|
||
![]()
14 years ago
|
/**
|
||
![]()
11 years ago
|
* Class Promotion
|
||
|
* This class provides methods for the promotion management.
|
||
|
* Include/require it in your code to use its features.
|
||
![]()
8 years ago
|
*
|
||
![]()
14 years ago
|
* @package chamilo.library
|
||
|
*/
|
||
![]()
12 years ago
|
class Promotion extends Model
|
||
|
{
|
||
|
public $table;
|
||
![]()
8 years ago
|
public $columns = [
|
||
![]()
11 years ago
|
'id',
|
||
|
'name',
|
||
|
'description',
|
||
|
'career_id',
|
||
|
'status',
|
||
|
'created_at',
|
||
|
'updated_at',
|
||
![]()
8 years ago
|
];
|
||
![]()
12 years ago
|
|
||
![]()
11 years ago
|
/**
|
||
![]()
8 years ago
|
* Constructor.
|
||
![]()
11 years ago
|
*/
|
||
![]()
10 years ago
|
public function __construct()
|
||
![]()
12 years ago
|
{
|
||
![]()
9 years ago
|
parent::__construct();
|
||
![]()
10 years ago
|
$this->table = Database::get_main_table(TABLE_PROMOTION);
|
||
|
}
|
||
![]()
12 years ago
|
|
||
![]()
10 years ago
|
/**
|
||
![]()
8 years ago
|
* Get the count of elements.
|
||
![]()
13 years ago
|
*/
|
||
![]()
12 years ago
|
public function get_count()
|
||
|
{
|
||
![]()
9 years ago
|
$row = Database::select(
|
||
|
'count(*) as count',
|
||
|
$this->table,
|
||
![]()
8 years ago
|
[],
|
||
![]()
9 years ago
|
'first'
|
||
|
);
|
||
|
|
||
![]()
13 years ago
|
return $row['count'];
|
||
|
}
|
||
![]()
12 years ago
|
|
||
![]()
10 years ago
|
/**
|
||
![]()
8 years ago
|
* Copies the promotion to a new one.
|
||
|
*
|
||
|
* @param int Promotion ID
|
||
|
* @param int Career ID, in case we want to change it
|
||
|
* @param bool Whether or not to copy the sessions inside
|
||
|
*
|
||
|
* @return int New promotion ID on success, false on failure
|
||
![]()
10 years ago
|
*/
|
||
|
public function copy($id, $career_id = null, $copy_sessions = false)
|
||
![]()
12 years ago
|
{
|
||
![]()
10 years ago
|
$pid = false;
|
||
|
$promotion = $this->get($id);
|
||
![]()
12 years ago
|
if (!empty($promotion)) {
|
||
![]()
8 years ago
|
$new = [];
|
||
![]()
12 years ago
|
foreach ($promotion as $key => $val) {
|
||
|
switch ($key) {
|
||
|
case 'id':
|
||
|
case 'updated_at':
|
||
|
break;
|
||
|
case 'name':
|
||
![]()
6 years ago
|
$val .= ' '.get_lang('Copy');
|
||
![]()
12 years ago
|
$new[$key] = $val;
|
||
|
break;
|
||
|
case 'created_at':
|
||
|
$val = api_get_utc_datetime();
|
||
|
$new[$key] = $val;
|
||
|
break;
|
||
|
case 'career_id':
|
||
|
if (!empty($career_id)) {
|
||
![]()
9 years ago
|
$val = (int) $career_id;
|
||
![]()
12 years ago
|
}
|
||
|
$new[$key] = $val;
|
||
![]()
11 years ago
|
break;
|
||
![]()
12 years ago
|
default:
|
||
|
$new[$key] = $val;
|
||
![]()
11 years ago
|
break;
|
||
![]()
12 years ago
|
}
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
if ($copy_sessions) {
|
||
|
/**
|
||
|
* When copying a session we do:
|
||
|
* 1. Copy a new session from the source
|
||
|
* 2. Copy all courses from the session (no user data, no user list)
|
||
![]()
8 years ago
|
* 3. Create the promotion.
|
||
![]()
10 years ago
|
*/
|
||
|
$session_list = SessionManager::get_all_sessions_by_promotion($id);
|
||
![]()
12 years ago
|
|
||
![]()
10 years ago
|
if (!empty($session_list)) {
|
||
|
$pid = $this->save($new);
|
||
|
if (!empty($pid)) {
|
||
![]()
8 years ago
|
$new_session_list = [];
|
||
![]()
12 years ago
|
|
||
![]()
10 years ago
|
foreach ($session_list as $item) {
|
||
![]()
9 years ago
|
$sid = SessionManager::copy(
|
||
|
$item['id'],
|
||
|
true,
|
||
|
false,
|
||
|
false,
|
||
|
true
|
||
|
);
|
||
![]()
13 years ago
|
$new_session_list[] = $sid;
|
||
![]()
10 years ago
|
}
|
||
![]()
12 years ago
|
|
||
![]()
13 years ago
|
if (!empty($new_session_list)) {
|
||
![]()
9 years ago
|
SessionManager::subscribe_sessions_to_promotion(
|
||
![]()
9 years ago
|
$pid,
|
||
|
$new_session_list
|
||
|
);
|
||
![]()
13 years ago
|
}
|
||
![]()
10 years ago
|
}
|
||
|
}
|
||
|
} else {
|
||
|
$pid = $this->save($new);
|
||
|
}
|
||
|
}
|
||
![]()
11 years ago
|
|
||
![]()
10 years ago
|
return $pid;
|
||
|
}
|
||
![]()
12 years ago
|
|
||
![]()
15 years ago
|
/**
|
||
![]()
8 years ago
|
* Gets all promotions by career id.
|
||
|
*
|
||
![]()
15 years ago
|
* @param int career id
|
||
![]()
12 years ago
|
* @param bool $order
|
||
![]()
8 years ago
|
*
|
||
|
* @return array results
|
||
![]()
15 years ago
|
*/
|
||
![]()
12 years ago
|
public function get_all_promotions_by_career_id($career_id, $order = false)
|
||
|
{
|
||
![]()
11 years ago
|
return Database::select(
|
||
|
'*',
|
||
|
$this->table,
|
||
![]()
8 years ago
|
[
|
||
|
'where' => ['career_id = ?' => $career_id],
|
||
![]()
11 years ago
|
'order' => $order,
|
||
![]()
8 years ago
|
]
|
||
![]()
11 years ago
|
);
|
||
![]()
15 years ago
|
}
|
||
![]()
12 years ago
|
|
||
|
/**
|
||
|
* @return array
|
||
|
*/
|
||
|
public function get_status_list()
|
||
|
{
|
||
![]()
8 years ago
|
return [
|
||
![]()
6 years ago
|
PROMOTION_STATUS_ACTIVE => get_lang('active'),
|
||
|
PROMOTION_STATUS_INACTIVE => get_lang('inactive'),
|
||
![]()
8 years ago
|
];
|
||
![]()
12 years ago
|
}
|
||
|
|
||
![]()
15 years ago
|
/**
|
||
![]()
8 years ago
|
* Displays the title + grid.
|
||
|
*
|
||
|
* @return string html code
|
||
![]()
15 years ago
|
*/
|
||
![]()
10 years ago
|
public function display()
|
||
![]()
11 years ago
|
{
|
||
![]()
10 years ago
|
// Action links
|
||
|
echo '<div class="actions" style="margin-bottom:20px">';
|
||
![]()
8 years ago
|
echo '<a href="career_dashboard.php">'.
|
||
|
Display::return_icon(
|
||
|
'back.png',
|
||
|
get_lang('Back'),
|
||
|
'',
|
||
|
'32'
|
||
|
)
|
||
|
.'</a>';
|
||
![]()
9 years ago
|
echo '<a href="'.api_get_self().'?action=add">'.
|
||
![]()
9 years ago
|
Display::return_icon(
|
||
|
'new_promotion.png',
|
||
|
get_lang('Add'),
|
||
|
'',
|
||
|
'32'
|
||
![]()
9 years ago
|
).'</a>';
|
||
|
echo '<a href="'.api_get_path(WEB_CODE_PATH).'session/session_add.php">'.
|
||
![]()
9 years ago
|
Display::return_icon(
|
||
|
'new_session.png',
|
||
![]()
6 years ago
|
get_lang('Add a training session'),
|
||
![]()
9 years ago
|
'',
|
||
|
'32'
|
||
![]()
9 years ago
|
).'</a>';
|
||
![]()
10 years ago
|
echo '</div>';
|
||
![]()
12 years ago
|
echo Display::grid_html('promotions');
|
||
![]()
10 years ago
|
}
|
||
![]()
12 years ago
|
|
||
![]()
15 years ago
|
/**
|
||
![]()
8 years ago
|
* Update all session status by promotion.
|
||
|
*
|
||
|
* @param int $promotion_id
|
||
|
* @param int $status (1, 0)
|
||
![]()
10 years ago
|
*/
|
||
|
public function update_all_sessions_status_by_promotion_id(
|
||
|
$promotion_id,
|
||
|
$status
|
||
|
) {
|
||
![]()
9 years ago
|
$sessionList = SessionManager::get_all_sessions_by_promotion($promotion_id);
|
||
|
if (!empty($sessionList)) {
|
||
|
foreach ($sessionList as $item) {
|
||
![]()
12 years ago
|
SessionManager::set_session_status($item['id'], $status);
|
||
![]()
15 years ago
|
}
|
||
|
}
|
||
|
}
|
||
![]()
12 years ago
|
|
||
![]()
15 years ago
|
/**
|
||
![]()
8 years ago
|
* Returns a Form validator Obj.
|
||
![]()
11 years ago
|
*
|
||
![]()
8 years ago
|
* @param string $url
|
||
|
* @param string $action
|
||
|
*
|
||
|
* @return FormValidator
|
||
![]()
15 years ago
|
*/
|
||
![]()
11 years ago
|
public function return_form($url, $action = 'add')
|
||
![]()
12 years ago
|
{
|
||
![]()
10 years ago
|
$form = new FormValidator('promotion', 'post', $url);
|
||
![]()
12 years ago
|
// Setting the form elements
|
||
![]()
15 years ago
|
$header = get_lang('Add');
|
||
![]()
15 years ago
|
if ($action == 'edit') {
|
||
![]()
6 years ago
|
$header = get_lang('Edit');
|
||
![]()
15 years ago
|
}
|
||
![]()
6 years ago
|
|
||
|
$id = isset($_GET['id']) ? (int) $_GET['id'] : '';
|
||
![]()
12 years ago
|
|
||
![]()
15 years ago
|
$form->addElement('header', '', $header);
|
||
![]()
14 years ago
|
$form->addElement('hidden', 'id', $id);
|
||
![]()
11 years ago
|
$form->addElement(
|
||
|
'text',
|
||
|
'name',
|
||
|
get_lang('Name'),
|
||
![]()
8 years ago
|
['size' => '70', 'id' => 'name']
|
||
![]()
11 years ago
|
);
|
||
![]()
11 years ago
|
$form->addHtmlEditor(
|
||
![]()
11 years ago
|
'description',
|
||
|
get_lang('Description'),
|
||
|
false,
|
||
|
false,
|
||
![]()
8 years ago
|
[
|
||
![]()
11 years ago
|
'ToolbarSet' => 'Careers',
|
||
![]()
11 years ago
|
'Width' => '100%',
|
||
![]()
8 years ago
|
'Height' => '250',
|
||
![]()
8 years ago
|
]
|
||
![]()
11 years ago
|
);
|
||
![]()
15 years ago
|
$career = new Career();
|
||
|
$careers = $career->get_all();
|
||
![]()
8 years ago
|
$career_list = [];
|
||
![]()
10 years ago
|
foreach ($careers as $item) {
|
||
![]()
15 years ago
|
$career_list[$item['id']] = $item['name'];
|
||
|
}
|
||
![]()
9 years ago
|
$form->addSelect(
|
||
![]()
9 years ago
|
'career_id',
|
||
|
get_lang('Career'),
|
||
![]()
9 years ago
|
$career_list,
|
||
|
['id' => 'career_id']
|
||
![]()
9 years ago
|
);
|
||
![]()
12 years ago
|
$status_list = $this->get_status_list();
|
||
![]()
15 years ago
|
$form->addElement('select', 'status', get_lang('Status'), $status_list);
|
||
![]()
15 years ago
|
if ($action == 'edit') {
|
||
![]()
6 years ago
|
$form->addElement('text', 'created_at', get_lang('Created at'));
|
||
![]()
15 years ago
|
$form->freeze('created_at');
|
||
![]()
12 years ago
|
}
|
||
![]()
11 years ago
|
if ($action == 'edit') {
|
||
![]()
6 years ago
|
$form->addButtonSave(get_lang('Edit'), 'submit');
|
||
![]()
14 years ago
|
} else {
|
||
![]()
10 years ago
|
$form->addButtonCreate(get_lang('Add'), 'submit');
|
||
![]()
14 years ago
|
}
|
||
![]()
12 years ago
|
|
||
![]()
15 years ago
|
// Setting the defaults
|
||
![]()
14 years ago
|
$defaults = $this->get($id);
|
||
|
if (!empty($defaults['created_at'])) {
|
||
![]()
10 years ago
|
$defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
|
||
![]()
14 years ago
|
}
|
||
|
if (!empty($defaults['updated_at'])) {
|
||
![]()
10 years ago
|
$defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
|
||
![]()
12 years ago
|
}
|
||
![]()
15 years ago
|
$form->setDefaults($defaults);
|
||
![]()
12 years ago
|
|
||
![]()
15 years ago
|
// Setting the rules
|
||
![]()
6 years ago
|
$form->addRule('name', get_lang('Required field'), 'required');
|
||
![]()
12 years ago
|
|
||
![]()
15 years ago
|
return $form;
|
||
|
}
|
||
![]()
12 years ago
|
|
||
![]()
11 years ago
|
/**
|
||
![]()
11 years ago
|
* @param array $params
|
||
![]()
8 years ago
|
* @param bool $show_query
|
||
![]()
10 years ago
|
*
|
||
![]()
11 years ago
|
* @return bool
|
||
|
*/
|
||
![]()
12 years ago
|
public function save($params, $show_query = false)
|
||
|
{
|
||
![]()
6 years ago
|
$promotion = new \Chamilo\CoreBundle\Entity\Promotion();
|
||
|
|
||
|
$em = Database::getManager();
|
||
|
$repo = $em->getRepository('ChamiloCoreBundle:Career');
|
||
|
$promotion
|
||
|
->setName($params['name'])
|
||
|
->setStatus($params['status'])
|
||
|
->setDescription($params['description'])
|
||
|
->setCareer($repo->find($params['career_id']))
|
||
|
;
|
||
|
|
||
|
$em->persist($promotion);
|
||
|
$em->flush();
|
||
|
|
||
|
if (!empty($promotion->getId())) {
|
||
![]()
11 years ago
|
Event::addEvent(
|
||
|
LOG_PROMOTION_CREATE,
|
||
|
LOG_PROMOTION_ID,
|
||
![]()
6 years ago
|
$promotion->getId(),
|
||
![]()
11 years ago
|
api_get_utc_datetime(),
|
||
|
api_get_user_id()
|
||
|
);
|
||
![]()
10 years ago
|
}
|
||
![]()
10 years ago
|
|
||
![]()
6 years ago
|
return $promotion->getId();
|
||
![]()
15 years ago
|
}
|
||
![]()
12 years ago
|
|
||
![]()
11 years ago
|
/**
|
||
![]()
11 years ago
|
* @param int $id
|
||
![]()
10 years ago
|
*
|
||
![]()
11 years ago
|
* @return bool
|
||
|
*/
|
||
![]()
12 years ago
|
public function delete($id)
|
||
|
{
|
||
![]()
10 years ago
|
if (parent::delete($id)) {
|
||
|
SessionManager::clear_session_ref_promotion($id);
|
||
![]()
11 years ago
|
Event::addEvent(
|
||
|
LOG_PROMOTION_DELETE,
|
||
|
LOG_PROMOTION_ID,
|
||
|
$id,
|
||
|
api_get_utc_datetime(),
|
||
|
api_get_user_id()
|
||
|
);
|
||
![]()
12 years ago
|
} else {
|
||
|
return false;
|
||
![]()
12 years ago
|
}
|
||
![]()
14 years ago
|
}
|
||
![]()
14 years ago
|
}
|