From e24c9e25ba9c003988cf050ebaa064d08d9fc65a Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Mon, 31 Jan 2011 16:26:29 +0100 Subject: [PATCH] Adding created_at and updated_at for careers and promotions --- main/admin/careers.php | 11 ++++------ main/admin/promotions.php | 9 ++++---- main/inc/lib/career.lib.php | 16 +++++++++++--- main/inc/lib/model.lib.php | 24 +++++++++++++-------- main/inc/lib/promotion.lib.php | 20 ++++++++++++----- main/install/db_main.sql | 6 +++++- main/install/migrate-db-1.8.7-1.8.8-pre.sql | 4 ++-- 7 files changed, 59 insertions(+), 31 deletions(-) diff --git a/main/admin/careers.php b/main/admin/careers.php index 3837a94646..289f81b7bf 100644 --- a/main/admin/careers.php +++ b/main/admin/careers.php @@ -45,13 +45,13 @@ if (isset($_GET['action']) && $_GET['action'] == 'editnote') { $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_careers'; //The order is important you need to check the the $column variable in the model.ajax.php file -$columns = array(get_lang('Name'),get_lang('Description'),get_lang('Actions')); +$columns = array(get_lang('Name'), get_lang('Description'), get_lang('Actions')); //Column config $column_model = array( array('name'=>'name', 'index'=>'name', 'width'=>'80', 'align'=>'left'), array('name'=>'description', 'index'=>'description', 'width'=>'500', 'align'=>'left'), - array('name'=>'actions', 'index'=>'actions', 'formatter'=>'action_formatter','width'=>'100', 'align'=>'left'), + array('name'=>'actions', 'index'=>'actions', 'width'=>'100', 'align'=>'left','formatter'=>'action_formatter') ); //Autowidth $extra_params['autowidth'] = 'true'; @@ -86,7 +86,7 @@ if (isset($_GET['action']) && $_GET['action'] == 'add') { $_SESSION['notebook_view'] = 'creation_date'; $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']); - $form = $career->return_form($url, get_lang('Add')); + $form = $career->return_form($url, 'add'); // The validation or display if ($form->validate()) { @@ -112,7 +112,7 @@ if (isset($_GET['action']) && $_GET['action'] == 'add') { } elseif (isset($_GET['action']) && $_GET['action'] == 'edit' && is_numeric($_GET['id'])) { // Action handling: Editing $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.intval($_GET['id']); - $form = $career->return_form($url, get_lang('Modify')); + $form = $career->return_form($url, 'edit'); // The validation or display if ($form->validate()) { @@ -121,9 +121,6 @@ if (isset($_GET['action']) && $_GET['action'] == 'add') { $values = $form->exportValues(); $career->update_all_promotion_status_by_career_id($values['id'],$values['status']); $res = $career->update($values); - if ($res) { - - } if ($values['status']) { Display::display_confirmation_message(sprintf(get_lang('CareerXArchived'), $values['name']), false); } else { diff --git a/main/admin/promotions.php b/main/admin/promotions.php index 865c89074c..2f8f35d60e 100644 --- a/main/admin/promotions.php +++ b/main/admin/promotions.php @@ -41,10 +41,11 @@ if (isset($_GET['action']) && $_GET['action'] == 'edit') { $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_promotions'; //The order is important you need to check the model.ajax.php the $column variable $columns = array(get_lang('Name'),get_lang('Career'),get_lang('Description'),get_lang('Actions')); -$column_model = array(array('name'=>'name', 'index'=>'name', 'width'=>'80', 'align'=>'left'), +$column_model = array( + array('name'=>'name', 'index'=>'name', 'width'=>'80', 'align'=>'left'), array('name'=>'career', 'index'=>'career', 'width'=>'100', 'align'=>'left'), array('name'=>'description', 'index'=>'description', 'width'=>'500', 'align'=>'left'), - array('name'=>'actions', 'index'=>'actions', 'formatter'=>'action_formatter','width'=>'100', 'align'=>'left'), + array('name'=>'actions', 'index'=>'actions', 'width'=>'100', 'align'=>'left','formatter'=>'action_formatter'), ); $extra_params['autowidth'] = 'true'; //use the width of the parent //$extra_params['editurl'] = $url; //use the width of the parent @@ -76,7 +77,7 @@ if (isset($_GET['action']) && $_GET['action'] == 'add') { api_not_allowed(); } $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']); - $form = $promotion->return_form($url,get_lang('Add')); + $form = $promotion->return_form($url, 'add'); // The validation or display if ($form->validate()) { @@ -102,7 +103,7 @@ if (isset($_GET['action']) && $_GET['action'] == 'add') { } elseif (isset($_GET['action']) && $_GET['action'] == 'edit' && is_numeric($_GET['id'])) { //Editing $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.intval($_GET['id']); - $form = $promotion->return_form($url, get_lang('Modify')); + $form = $promotion->return_form($url, 'edit'); // The validation or display if ($form->validate()) { diff --git a/main/inc/lib/career.lib.php b/main/inc/lib/career.lib.php index 2c1518b649..eaa5946380 100644 --- a/main/inc/lib/career.lib.php +++ b/main/inc/lib/career.lib.php @@ -18,7 +18,7 @@ define ('CAREER_STATUS_INACTIVE',0); class Career extends Model { var $table; - var $columns = array('id', 'name','description','status'); + var $columns = array('id', 'name','description','status','created_at','updated_at'); public function __construct() { $this->table = Database::get_main_table(TABLE_CAREER); @@ -66,12 +66,17 @@ class Career extends Model { * Returns a Form validator Obj * @todo the form should be auto generated * @param string url - * @param string header name + * @param string action add, edit * @return obj form validator obj */ - public function return_form($url, $header) { + public function return_form($url, $action) { $form = new FormValidator('career', 'post', $url); // Settting the form elements + $header = get_lang('add'); + if ($action == 'edit') { + $header = get_lang('Modify'); + } + $form->addElement('header', '', $header); $form->addElement('hidden', 'id',intval($_GET['id'])); $form->addElement('text', 'name', get_lang('Name'), array('size' => '100')); @@ -79,10 +84,15 @@ class Career extends Model { $status_list = $this->get_status_list(); $form->addElement('select', 'status', get_lang('Status'), $status_list); + $form->addElement('text', 'created_at', get_lang('CreatedAt')); + $form->freeze('created_at'); + $form->addElement('style_submit_button', 'submit', get_lang('Modify'), 'class="save"'); // Setting the defaults $defaults = $this->get($_GET['id']); + $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']); + $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']); $form->setDefaults($defaults); // Setting the rules diff --git a/main/inc/lib/model.lib.php b/main/inc/lib/model.lib.php index db11e40592..dc7d2f0bb1 100644 --- a/main/inc/lib/model.lib.php +++ b/main/inc/lib/model.lib.php @@ -2,7 +2,7 @@ /* For licensing terms, see /license.txt */ /** -* This class provides methods for the notebook management. +* This class provides basic methods to implement a CRUD for a new table in the database see examples in: career.lib.php and promotion.lib.php * Include/require it in your code to use its features. * @package chamilo.library */ @@ -15,10 +15,11 @@ class Model { public function __construct() { } - public function find() { - - } - + /** + * Useful finder + */ + public function find() { + } /** * Delets an item @@ -75,10 +76,7 @@ class Model { } /** - * a little bit of javascript to display a prettier warning when deleting a note - * - * @return unknown - * + * a little bit of javascript to display */ public function javascript() { @@ -93,6 +91,10 @@ class Model { */ public function save($params) { $params = $this->clean_parameters($params); + + if (in_array('created_at', $this->columns)) { + $params['created_at'] = api_get_utc_datetime(); + } if (!empty($params)) { $id = Database::insert($this->table, $params); if (is_numeric($id)){ @@ -110,6 +112,10 @@ class Model { */ public function update($params) { $params = $this->clean_parameters($params); + if (in_array('updated_at', $this->columns)) { + $params['updated_at'] = api_get_utc_datetime(); + } + if (!empty($params)) { $id = $params['id']; unset($params['id']); //To not overwrite the id diff --git a/main/inc/lib/promotion.lib.php b/main/inc/lib/promotion.lib.php index dd344cf3d2..86a15580fc 100644 --- a/main/inc/lib/promotion.lib.php +++ b/main/inc/lib/promotion.lib.php @@ -16,7 +16,7 @@ define ('PROMOTION_STATUS_INACTIVE',0); class Promotion extends Model { var $table; - var $columns = array('id','name','description','career_id','status'); + var $columns = array('id','name','description','career_id','status','created_at','updated_at'); public function __construct() { $this->table = Database::get_main_table(TABLE_PROMOTION); @@ -72,12 +72,16 @@ class Promotion extends Model { * @return obj form validator obj */ - function return_form($url, $header) { + function return_form($url, $action = 'add') { $form = new FormValidator('promotion', 'post', $url); // Settting the form elements + $header = get_lang('add'); + if ($action == 'edit') { + $header = get_lang('Modify'); + } $form->addElement('header', '', $header); $form->addElement('hidden', 'id', intval($_GET['id'])); - $form->addElement('text', 'name', get_lang('Name'), array('size' => '100','id' => 'name')); + $form->addElement('text', 'name', get_lang('Name'), array('size' => '100','id' => 'name')); $form->addElement('html_editor', 'description', get_lang('description'), null); $career = new Career(); @@ -90,13 +94,19 @@ class Promotion extends Model { $status_list = $this->get_status_list(); $form->addElement('select', 'status', get_lang('Status'), $status_list); - + + $form->addElement('text', 'created_at', get_lang('CreatedAt')); + $form->freeze('created_at'); $form->addElement('style_submit_button', 'submit', get_lang('Modify'), 'class="save"'); // Setting the defaults - $defaults = $this->get($_GET['id']); + $defaults = $this->get($_GET['id']); + $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']); + $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']); $form->setDefaults($defaults); + + // Setting the rules $form->addRule('name', '
'.get_lang('ThisFieldIsRequired'), 'required'); diff --git a/main/install/db_main.sql b/main/install/db_main.sql index 1341d9a100..1e2cebc164 100755 --- a/main/install/db_main.sql +++ b/main/install/db_main.sql @@ -2544,7 +2544,9 @@ CREATE TABLE career ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL , description TEXT NOT NULL, - status INT NOT NULL default '0', + status INT NOT NULL default '0', + created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + updated_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id) ); @@ -2554,6 +2556,8 @@ CREATE TABLE promotion ( description TEXT NOT NULL, career_id INT NOT NULL, status INT NOT NULL default '0', + created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + updated_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY(id) ); diff --git a/main/install/migrate-db-1.8.7-1.8.8-pre.sql b/main/install/migrate-db-1.8.7-1.8.8-pre.sql index d13cab035b..0a4d8d6af0 100755 --- a/main/install/migrate-db-1.8.7-1.8.8-pre.sql +++ b/main/install/migrate-db-1.8.7-1.8.8-pre.sql @@ -174,8 +174,8 @@ INSERT INTO course_setting(variable,value,category) VALUES ('enable_lp_auto_laun INSERT INTO course_setting(variable,value,category) VALUES ('pdf_export_watermark_text','','course'); -CREATE TABLE career (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, description TEXT NOT NULL, status INT NOT NULL default '0' PRIMARY KEY (id)); -CREATE TABLE promotion (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, description TEXT NOT NULL, status INT NOT NULL default '0', career_id INT NOT NULL,PRIMARY KEY(id)); +CREATE TABLE career (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, description TEXT NOT NULL, status INT NOT NULL default '0', created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', updated_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id)); +CREATE TABLE promotion (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, description TEXT NOT NULL, status INT NOT NULL default '0', career_id INT NOT NULL, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', updated_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY(id)); ALTER TABLE session ADD promotion_id INT NOT NULL; CREATE TABLE usergroup ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, description TEXT NOT NULL,PRIMARY KEY (id));