Session: Add relation to prefill session extra with user extra field - refs BT#20569

pull/4618/head
Christian 3 years ago
parent 63b61c09bb
commit cdba8649b0
  1. 23
      main/inc/lib/sessionmanager.lib.php
  2. 11
      main/install/configuration.dist.php
  3. 9
      main/session/session_add.php
  4. 20
      main/session/session_export.php
  5. 57
      tests/scripts/fill_session_extrafield_from_user.php

@ -8190,8 +8190,29 @@ class SessionManager
);
// Extra fields
$setExtraFieldsMandatory = api_get_configuration_value('session_creation_form_set_extra_fields_mandatory');
$fieldsRequired = [];
if (false !== $setExtraFieldsMandatory && !empty($setExtraFieldsMandatory['fields'])) {
$fieldsRequired = $setExtraFieldsMandatory['fields'];
}
$extra_field = new ExtraFieldModel('session');
$extra = $extra_field->addElements($form, $sessionId);
$extra = $extra_field->addElements(
$form,
$sessionId,
[],
false,
false,
[],
[],
[],
false,
false,
[],
[],
false,
[],
$fieldsRequired
);
$form->addElement('html', '</div>');

@ -2377,6 +2377,17 @@ ALTER TABLE c_wiki_category ADD CONSTRAINT FK_17F1099A727ACA70 FOREIGN KEY (pare
// 3. Add an "@" before "ORM\Entity" in the "CWikiCategory" class definition (in src/Chamilo/CourseBundle/Entity/CWikiCategory.php)
//$_configuration['wiki_categories_enabled'] = false;
// Relation to prefill session extra field with user extra field on session creation on main/session/session_add.php
/*$_configuration['session_creation_user_course_extra_field_relation_to_prefill'] = [
'fields' => [
'client' => 'client',
'region' => 'region',
]
];*/
// Configuration setting to make some extra field required in session creation form on main/session/session_add.php.
// $_configuration['session_creation_form_set_extra_fields_mandatory'] = ['fields' => ['client','region']];
// Ask REST webservices (v2.php) to return another identifier for fields related to user ID.
// This is useful if the external system doesn't really deal with user IDs as they are in Chamilo, as it helps
// the external system match the user data return with some external data that is know to Chamilo. For example, if

@ -362,6 +362,15 @@ if (!$formSent) {
$formDefaults['name'] = api_htmlentities($name, ENT_QUOTES, $charset);
}
// Relation to prefill session extra field with user extra field
$fillExtraField = api_get_configuration_value('session_creation_user_course_extra_field_relation_to_prefill');
if (false !== $fillExtraField && !empty($fillExtraField['fields'])) {
foreach ($fillExtraField['fields'] as $sessionVariable => $userVariable) {
$extraValue = UserManager::get_extra_user_data_by_field(api_get_user_id(), $userVariable);
$formDefaults['extra_'.$sessionVariable] = isset($extraValue[$userVariable]) ? $extraValue[$userVariable] : '';
}
}
$form->setDefaults($formDefaults);
if ($form->validate()) {

@ -75,7 +75,7 @@ if (isset($_POST['formSent'])) {
}
$result = Database::query($sql);
$extraVariables = [];
if (Database::num_rows($result)) {
$sessionListToExport = [];
if (in_array($file_type, ['csv', 'xls'])) {
@ -91,6 +91,13 @@ if (isset($_POST['formSent'])) {
'SessionCategory',
];
$extraField = new ExtraField('session');
$allExtraFields = $extraField->get_all();
foreach ($allExtraFields as $extra) {
$exportHeaders[] = $extra['display_text'];
$extraVariables[] = $extra['variable'];
}
if ($includeUsers) {
$exportHeaders[] = 'Users';
}
@ -119,6 +126,7 @@ if (isset($_POST['formSent'])) {
fputs($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Sessions>\n");
}
$extraFieldValueSession = new ExtraFieldValue('session');
while ($row = Database::fetch_array($result)) {
$row['name'] = str_replace(';', ',', $row['name']);
$row['username'] = str_replace(';', ',', $row['username']);
@ -274,6 +282,16 @@ if (isset($_POST['formSent'])) {
$row['session_category'],
];
if (!empty($extraVariables)) {
foreach ($extraVariables as $variable) {
$extraData = $extraFieldValueSession->get_values_by_handler_and_field_variable(
$row['id'],
$variable
);
$exportContent[] = !empty($extraData['value']) ? $extraData['value'] : '';
}
}
if ($includeUsers) {
$exportContent[] = $users;
}

@ -0,0 +1,57 @@
<?php
/* For license terms, see /license.txt */
/**
* This script allows prefill the session extra fields related with
* the user creator
*/
exit;
if (PHP_SAPI != 'cli') {
die('This script can only be launched from the command line');
}
require_once __DIR__.'/../../main/inc/global.inc.php';
$fillExtraField = api_get_configuration_value('session_creation_user_course_extra_field_relation_to_prefill');
$sessions = SessionManager::get_sessions_list();
if (!empty($sessions)) {
foreach ($sessions as $session) {
$sessionId = $session['id'];
$creatorId = getSessionCreatorId($sessionId);
// Relation to prefill session extra field with user extra field
if (false !== $fillExtraField && !empty($fillExtraField['fields'])) {
foreach ($fillExtraField['fields'] as $sessionVariable => $userVariable) {
$extraValue = UserManager::get_extra_user_data_by_field($creatorId, $userVariable);
if (isset($extraValue[$userVariable])) {
$saved = SessionManager::update_session_extra_field_value($sessionId, $sessionVariable, $extraValue[$userVariable]);
if ($saved) {
echo "Updated $sessionId with creator user_id $creatorId, user_field_variable : $userVariable , user_field_value : {$extraValue[$userVariable]}".PHP_EOL;
}
}
}
}
}
}
/**
* Get the user who creates the session
*
* @param $sessionId
* @return int
*/
function getSessionCreatorId($sessionId):int
{
$tblTrackDefault = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
$sql = "SELECT
default_user_id
FROM $tblTrackDefault
WHERE default_value_type = 'session_id' AND
default_value = '$sessionId'
default_event_type = '".LOG_SESSION_CREATE."'";
$rs = Database::query($sql);
$creatorId = Database::result($rs, 0, 0);
return $creatorId;
}
Loading…
Cancel
Save