Adding session extra fields, working and tested for text and selects just for now see BT#4872

skala
Julio Montoya 12 years ago
parent 3b7db8615b
commit 7f6f76aa16
  1. 5
      main/admin/resume_session.php
  2. 13
      main/admin/session_add.php
  3. 3
      main/css/base.css
  4. 2
      main/gradebook/gradebook_display_certificate.php
  5. 4
      main/gradebook/lib/gradebook_data_generator.class.php
  6. 2
      main/inc/lib/autoload.class.php
  7. 7
      main/inc/lib/formvalidator/Rule/CompareDateTimeText.php
  8. 54
      main/inc/lib/session_field.lib.php
  9. 108
      main/inc/lib/session_field_value.lib.php
  10. 57
      main/inc/lib/session_model.lib.php
  11. 53
      main/inc/lib/sessionmanager.lib.php
  12. 16
      main/inc/lib/usermanager.lib.php

@ -132,12 +132,15 @@ $coach_info = api_get_user_info($session['id_coach']);
$session_field = new SessionField();
$session_fields = $session_field->get_all();
foreach ($session_fields as $session_field) {
if ($session_field['field_visible'] != '1') {
continue;
}
$obj = new SessionFieldValue();
$result = $obj->get_values_by_session_and_field_id($id_session, $session_field['id']);
$session_value = null;
if ($result) {
$session_value = $result[0]['field_value'];
$session_value = $result['field_value'];
}
echo "<tr>";
echo "<td> {$session_field['field_display_text']} </td>";

@ -100,8 +100,7 @@ $(function() {
newel: true
});
'.$add_coach.'
'.$add_coach.'
$("#display_end_date").datetimepicker({
dateFormat: "yy-mm-dd"
@ -235,7 +234,7 @@ $form->addRule(array('coach_access_start_date', 'coach_access_end_date'), get_la
$session_field = new SessionField();
$session_field->add_elements($form);
$session_field->add_elements($form, $id);
$form->addElement('html','</div>');
@ -254,14 +253,14 @@ if (!empty($session_info)) {
if ($form->validate()) {
$params = $form->getSubmitValues();
if (isset($params['id'])) {
$result = SessionManager::update($params);
SessionManager::update($params);
header('Location: resume_session.php?id_session='.$params['id']);
exit;
} else {
$result = SessionManager::add($params);
if ($result) {
$session_id = SessionManager::add($params);
if ($session_id) {
// integer => no error on session creation
header('Location: add_courses_to_session.php?id_session='.$result.'&add=true&msg=');
header('Location: add_courses_to_session.php?id_session='.$session_id.'&add=true&msg=');
exit;
}
}

@ -31,6 +31,9 @@ legend {
padding:40px 0px 0px 0px;
}
header #logo {
padding: 0px 0px 15px 0px;
}
.subnav .brand {
display:none;
}

@ -30,11 +30,13 @@ function confirmation() {
{return false;}
}
</script>";
api_block_anonymous_users();
if (!api_is_allowed_to_edit()) {
api_not_allowed(true);
}
$interbreadcrumb[] = array ('url' => Security::remove_XSS($_SESSION['gradebook_dest']).'?', 'name' => get_lang('Gradebook'));
$interbreadcrumb[] = array ('url' => '#','name' => get_lang('GradebookListOfStudentsCertificates'));
$this_section = SECTION_COURSES;

@ -189,8 +189,8 @@ class GradebookDataGenerator
// Other functions
private function build_result_column($item, $ignore_score_color) {
$scoredisplay = ScoreDisplay :: instance();
$score = $item->calc_score(api_get_user_id());
$scoredisplay = ScoreDisplay::instance();
$score = $item->calc_score(api_get_user_id());
if (!empty($score)) {
switch ($item->get_item_type()) {

@ -804,6 +804,8 @@ class Autoload
$result['Security'] = '/main/inc/lib/security.lib.php';
$result['SessionHandler'] = '/main/inc/lib/session_handler.class.php';
$result['SessionManager'] = '/main/inc/lib/sessionmanager.lib.php';
$result['SessionModel'] = '/main/inc/lib/session_model.lib.php';
$result['SessionField'] = '/main/inc/lib/session_field.lib.php';
$result['SessionFieldValue'] = '/main/inc/lib/session_field_value.lib.php';
$result['SessionFieldOption'] = '/main/inc/lib/session_field_option.lib.php';

@ -16,11 +16,10 @@ class HTML_QuickForm_Rule_CompareDateTimeText extends HTML_QuickForm_Rule_Compar
$datetime2 = api_strtotime($values[1]);
if (strpos($operator, 'allow_empty') !== false) {
$operator = str_replace('allow_empty', '', $operator);
var_dump($datetime1, $datetime2, $operator);
if (!$datetime2 || empty($datetime2)) {
$operator = str_replace('allow_empty', '', $operator);
if (!$datetime2 || empty($datetime2)) {
return true;
}
}
}
$result = parent::validate(array($datetime1, $datetime2), $operator);
return $result;

@ -12,14 +12,16 @@ class SessionField extends Model {
public function add_elements($form, $session_id = null) {
if (empty($form)) {
return false;
}
}
$extra_data = array();
if (!empty($session_id)) {
$extra_data = array();
$extra_data = self::get_session_extra_data($session_id);
if ($form) {
$form->setDefaults($extra_data);
}
}
$extra_fields = self::get_all();
UserManager::set_extra_fields_in_form($form, $extra_data, 'session_field', false, false, $extra_fields);
UserManager::set_extra_fields_in_form($form, null, 'session_field', false, false, $extra_fields);
}
public function get_count() {
@ -41,10 +43,10 @@ class SessionField extends Model {
public function get_session_field_info_by_field_variable($field) {
$field = Database::escape_string($field);
$sql_field = "SELECT id FROM {$this->table} WHERE field_variable = '$field'";
$sql_field = "SELECT * FROM {$this->table} WHERE field_variable = '$field'";
$result = Database::query($sql_field);
if (Database::num_rows($result)) {
$r_field = Database::fetch_row($result);
$r_field = Database::fetch_array($result, 'ASSOC');
return $r_field;
} else {
return false;
@ -62,6 +64,40 @@ class SessionField extends Model {
return $options;
}
public function get_session_extra_data($session_id) {
if (empty($session_id)) {
return array();
}
$extra_data = array();
$session_fields = self::get_all();
$session_field_values = new SessionFieldValue();
if (!empty($session_fields) > 0) {
foreach ($session_fields as $session_field) {
//if ($session_field['field_type'] == self::USER_FIELD_TYPE_TAG) {
//$tags = self::get_user_tags_to_string($user_id,$row['id'],false);
//$extra_data['extra_'.$row['fvar']] = $tags;
//} else {
$field_value = $session_field_values->get_values_by_session_and_field_id($session_id, $session_field['id']);
if ($field_value) {
$field_value = $field_value['field_value'];
switch ($session_field['field_type']) {
case UserManager::USER_FIELD_TYPE_SELECT_MULTIPLE:
$field_value = split(';', $field_value);
case UserManager::USER_FIELD_TYPE_RADIO:
$extra_data['extra_'.$session_field['field_variable']]['extra_'.$session_field['field_variable']] = $field_value;
break;
default:
$extra_data['extra_'.$session_field['field_variable']] = $field_value;
break;
}
}
//}
}
}
return $extra_data;
}
public function get_field_types() {
$types = UserManager::get_user_field_types();
unset($types[UserManager::USER_FIELD_TYPE_TAG]);
@ -100,10 +136,8 @@ class SessionField extends Model {
}
public function save($params, $show_query = false) {
$session_field_info = self::get_session_field_info_by_field_variable($params['field_variable']);
$params = self::clean_parameters($params);
$session_field_info = self::get_session_field_info_by_field_variable($params['field_variable']);
$params = self::clean_parameters($params);
if ($session_field_info) {
return $session_field_info['id'];
} else {

@ -10,15 +10,97 @@ class SessionFieldValue extends Model {
public function get_count() {
$row = Database::select('count(*) as count', $this->table, array(), 'first');
return $row['count'];
}
}
public function save($params, $show_query = false) {
public function save_session_field_values($params) {
$session_field = new SessionField();
$session_field_info = $session_field->get_session_field_info_by_field_variable($params['field_id']);
if (empty($params['session_id'])) {
return false;
}
//Parse params
foreach ($params as $key => $value) {
if (substr($key, 0, 6) == 'extra_') { //an extra field
$field_variable = substr($key, 6);
$session_field_info = $session_field->get_session_field_info_by_field_variable($field_variable);
if ($session_field_info) {
$new_params = array(
'session_id' => $params['session_id'],
'field_id' => $session_field_info['id'],
'field_value' => $value
);
self::save($new_params);
}
}
}
}
public function save($params, $show_query = false) {
$session_field = new SessionField();
$session_field_option = new SessionFieldOption();
//Setting value to insert
$value = $params['field_value'];
$value_to_insert = null;
if (is_array($value)) {
foreach ($value as $val) {
$value_to_insert .= Database::escape_string($val).';';
}
if (!empty($value)) {
$value_to_insert = substr($value, 0, -1);
}
} else {
$value_to_insert = Database::escape_string($value);
}
$params['field_value'] = $value_to_insert;
//If field id exists
$session_field_info = $session_field->get($params['field_id']);
if ($session_field_info) {
$id = parent::save($params, $show_query);
switch ($session_field_info['field_type']) {
case UserManager::USER_FIELD_TYPE_TAG :
break;
case UserManager::USER_FIELD_TYPE_RADIO:
case UserManager::USER_FIELD_TYPE_SELECT:
case UserManager::USER_FIELD_TYPE_SELECT_MULTIPLE:
$field_options = $session_field_option->get_field_options_by_field($params['field_id']);
$params['field_value'] = split(';', $value_to_insert);
/*
if ($field_options) {
$check = false;
foreach ($field_options as $option) {
if (in_array($option['option_value'], $values)) {
$check = true;
break;
}
}
if (!$check) {
return false; //option value not found
}
} else {
return false; //enumerated type but no option found
}*/
break;
case UserManager::USER_FIELD_TYPE_TEXT:
case UserManager::USER_FIELD_TYPE_TEXTAREA:
default:
break;
}
$session_field_values = self::get_values_by_session_and_field_id($params['session_id'], $params['field_id']);
if ($session_field_values) {
self::delete_values_by_session_and_field_id($params['session_id'], $params['field_id']);
}
$params['field_value'] = $value_to_insert;
$params['tms'] = api_get_utc_datetime();
parent::save($params, $show_query);
}
return $id;
}
public function get_values_by_session_and_field_id($session_id, $field_id) {
@ -28,12 +110,11 @@ class SessionFieldValue extends Model {
$sql = "SELECT * FROM {$this->table} WHERE session_id = '$session_id' AND field_id = '".$field_id."' ORDER BY id";
$result = Database::query($sql);
if (Database::num_rows($result)) {
return Database::store_result($result, 'ASSOC');
return Database::fetch_array($result, 'ASSOC');
} else {
return false;
}
$field = Database::escape_string($field);
}
/*$field = Database::escape_string($field);
$sql_field = "SELECT id FROM {$this->table} WHERE field_variable = '$field'";
$result = Database::query($sql_field);
if (Database::num_rows($result)) {
@ -41,7 +122,14 @@ class SessionFieldValue extends Model {
return $r_field;
} else {
return false;
}
}*/
}
public function delete_values_by_session_and_field_id($session_id, $field_id) {
$field_id = intval($field_id);
$session_id = intval($session_id);
$sql = "DELETE FROM {$this->table} WHERE session_id = '$session_id' AND field_id = '".$field_id."' ";
Database::query($sql);
}
public function update($params) {

@ -0,0 +1,57 @@
<?php
class SessionModel extends model {
public $columns = array(
'id',
'id_coach',
'name',
'nbr_courses',
'nbr_users',
'nbr_classes',
'session_admin_id',
'visibility',
'session_category_id',
'promotion_id',
'display_start_date',
'display_end_date',
'access_start_date',
'access_end_date',
'coach_access_start_date',
'coach_access_end_date',
);
public function __construct() {
$this->table = Database::get_main_table(TABLE_MAIN_SESSION);
}
public function clean_parameters($params) {
//Convert dates
$params['display_start_date'] = api_get_utc_datetime($params['display_start_date'], true);
$params['display_end_date'] = api_get_utc_datetime($params['display_end_date'], true);
$params['access_start_date'] = api_get_utc_datetime($params['access_start_date'], true);
$params['access_end_date'] = api_get_utc_datetime($params['access_end_date'], true);
$params['coach_access_start_date'] = api_get_utc_datetime($params['coach_access_start_date'], true);
$params['coach_access_end_date'] = api_get_utc_datetime($params['coach_access_end_date'], true);
$params['id_coach'] = is_array($params['id_coach']) ? $params['id_coach'][0] : $params['id_coach'];
if (empty($params['access_end_date'])) {
$params['visibility'] = SessionManager::DEFAULT_VISIBILITY;
}
unset($params['submit']);
return $params;
}
function save($params, $show_query = false) {
$params = self::clean_parameters($params);
return parent::save($params, $show_query);
}
function update($params) {
$params = self::clean_parameters($params);
$result = parent::update($params);
return $result;
}
}

@ -21,8 +21,6 @@ class SessionManager {
CONST DEFAULT_VISIBILITY = 4; //SESSION_AVAILABLE
private function __construct() {
}
/**
* Fetches a session from the database
@ -39,10 +37,11 @@ class SessionManager {
}
public static function add($params) {
global $_configuration;
global $_configuration;
//Check portal limits
$access_url_id = 1;
if (api_get_multiple_access_url()) {
$access_url_id = api_get_current_access_url_id();
}
@ -51,12 +50,12 @@ class SessionManager {
if ($num >= $_configuration[$access_url_id]['hosting_limit_sessions']) {
return get_lang('PortalSessionsLimitReached');
}
}
$params = self::clean_parameters($params);
}
$my_session_result = SessionManager::get_session_by_name($params['name']);
if (empty($my_session_result)) {
$session_id = Database::insert(Database::get_main_table(TABLE_MAIN_SESSION), $params);
if (empty($my_session_result)) {
$session_model = new SessionModel();
$session_id = $session_model->save($params);
}
if (!empty($session_id)) {
@ -72,7 +71,9 @@ class SessionManager {
*/
//Saving extra fields
$session_field_value = new SessionFieldValue();
$params['session_id'] = $session_id;
$session_field_value->save_session_field_values($params);
//Adding to the correct URL
$access_url_id = api_get_current_access_url_id();
@ -82,37 +83,19 @@ class SessionManager {
$user_id = api_get_user_id();
event_system(LOG_SESSION_CREATE, LOG_SESSION_ID, $session_id, api_get_utc_datetime(), $user_id);
}
return $session_id;
return $session_id;
}
public static function update($params) {
$id = $params['id'];
if (empty($id)) {
return false;
}
unset($params['id']);
$params = self::clean_parameters($params);
$affected = Database::update(Database::get_main_table(TABLE_MAIN_SESSION), $params, array('id = ?'=>$id));
return $affected;
}
public static function clean_parameters($params) {
//Convert dates
$params['display_start_date'] = api_get_utc_datetime($params['display_start_date'], true);
$params['display_end_date'] = api_get_utc_datetime($params['display_end_date'], true);
$params['access_start_date'] = api_get_utc_datetime($params['access_start_date'], true);
$params['access_end_date'] = api_get_utc_datetime($params['access_end_date'], true);
$params['coach_access_start_date'] = api_get_utc_datetime($params['coach_access_start_date'], true);
$params['coach_access_end_date'] = api_get_utc_datetime($params['coach_access_end_date'], true);
$params['id_coach'] = is_array($params['id_coach']) ? $params['id_coach'][0] : $params['id_coach'];
if (empty($params['access_end_date'])) {
$params['visibility'] = SessionManager::DEFAULT_VISIBILITY;
}
$session_model = new SessionModel();
$session_model->update($params);
unset($params['submit']);
return $params;
if (!empty($params['id'])) {
$session_field_value = new SessionFieldValue();
$params['session_id'] = $params['id'];
unset($params['id']);
$session_field_value->save_session_field_values($params);
}
}
/**

@ -1283,7 +1283,7 @@ class UserManager {
default:
break;
}
$tms = time();
$tms = api_get_utc_datetime();
$sqlufv = "SELECT * FROM $t_ufv WHERE user_id = $user_id AND field_id = ".$rowuf['id']." ORDER BY id";
$resufv = Database::query($sqlufv);
$n = Database::num_rows($resufv);
@ -1297,7 +1297,7 @@ class UserManager {
}
$rowufv = Database::fetch_array($resufv);
if ($rowufv['field_value'] != $fvalues) {
$sqlu = "UPDATE $t_ufv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowufv['id'];
$sqlu = "UPDATE $t_ufv SET field_value = '$fvalues', tms = '$tms' WHERE id = ".$rowufv['id'];
$resu = Database::query($sqlu);
return($resu ? true : false);
}
@ -1312,7 +1312,7 @@ class UserManager {
$sql_query = "DELETE FROM $t_ufv WHERE id = ".$rowufv['id'].";";
} else {
// Otherwise update it
$sql_query = "UPDATE $t_ufv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowufv['id'];
$sql_query = "UPDATE $t_ufv SET field_value = '$fvalues', tms = '$tms' WHERE id = ".$rowufv['id'];
}
$resu = Database::query($sql_query);
@ -1320,8 +1320,8 @@ class UserManager {
}
return true;
} else {
$sqli = "INSERT INTO $t_ufv (user_id,field_id,field_value,tms) " .
"VALUES ($user_id,".$rowuf['id'].",'$fvalues',FROM_UNIXTIME($tms))";
$sqli = "INSERT INTO $t_ufv (user_id,field_id,field_value,tms)
VALUES ($user_id,".$rowuf['id'].",'$fvalues', '$tms')";
//error_log('UM::update_extra_field_value: '.$sqli);
$resi = Database::query($sqli);
return($resi ? true : false);
@ -3538,14 +3538,12 @@ class UserManager {
$jquery_ready_content = null;
foreach ($extra as $field_details) {
foreach ($extra as $field_details) {
if (!$admin_permissions) {
if ($field_details['field_visible'] == 0) {
continue;
}
}
}
switch ($field_details['field_type']) {
case self::USER_FIELD_TYPE_TEXT:
$form->addElement('text', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('class' => 'span4'));

Loading…
Cancel
Save