Skip to content

Commit

Permalink
Admin: Add configuration setting 'webservice_return_user_field' to en…
Browse files Browse the repository at this point in the history
…able returning a specific extra field instead of the username in webservices that implement it - refs BT#20478
  • Loading branch information
ywarnier committed Feb 17, 2023
1 parent fd1eca0 commit b0823cc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
43 changes: 41 additions & 2 deletions main/inc/lib/webservices/Rest.php
Expand Up @@ -1199,6 +1199,27 @@ public function getUserProfile(): array
return $result;
}

/**
* Get user's username or another field if so configured through $_configuration['webservice_return_user_field']
* @param int $userId
* @return string
*/
private function __getConfiguredUsernameById(int $userId): string
{
$userField = api_get_configuration_value('webservice_return_user_field');
if (empty($userField)) {
return api_get_user_info($userId)['username'];
}

$fieldValue = new ExtraFieldValue('user');
$extraInfo = $fieldValue->get_values_by_handler_and_field_variable($userId, $userField);
if (!empty($extraInfo)) {
return $extraInfo['value'];
} else {
return api_get_user_info($userId)['username'];
}
}

/**
* Get one's own (avg) progress in learning paths.
*/
Expand Down Expand Up @@ -2876,7 +2897,7 @@ public function getTestUpdatesList($fields = []): array
if ($fieldDetails['item_type'] == 'course') {
$itemId = $row['c_id'];
} else {
$itemId = $row['iid'];
$itemId = $row['id'];
}
$fieldResult = Database::query(sprintf($fieldsSearchString, $itemId, $fieldDetails['id']));
if (Database::num_rows($fieldResult) > 0) {
Expand All @@ -2887,6 +2908,14 @@ public function getTestUpdatesList($fields = []): array
}
}
}
// Get item authoring data
$itemProps = api_get_last_item_property_info($row['c_id'], 'quiz', $row['id']);
$row['created_by'] = $this->__getConfiguredUsernameById($itemProps['insert_user_id']);
if ($itemProps['insert_user_id'] == $itemProps['lastedit_user_id']) {
$row['updated_by'] = $row['created_by'];
} else {
$row['updated_by'] = $this->__getConfiguredUsernameById($itemProps['lastedit_user_id']);
}
$resultArray[] = $row;
}
}
Expand Down Expand Up @@ -2961,6 +2990,15 @@ public function getTestAverageResultsList(array $ids = [], ?array $fields = []):
$type = Exercise::getFeedbackTypeLiteral($row['feedback_type']);
$passPercentage = empty($row['pass_percentage']) ? 0.5 : $row['pass_percentage'];

// Get item authoring data
$itemProps = api_get_last_item_property_info($row['c_id'], 'quiz', $item);
$createdBy = $this->__getConfiguredUsernameById($itemProps['insert_user_id']);
if ($itemProps['insert_user_id'] == $itemProps['lastedit_user_id']) {
$updatedBy = $createdBy;
} else {
$updatedBy = $this->__getConfiguredUsernameById($itemProps['lastedit_user_id']);
}

$sql = "
SELECT a.exe_exo_id AS id,
a.exe_user_id,
Expand Down Expand Up @@ -3009,7 +3047,8 @@ public function getTestAverageResultsList(array $ids = [], ?array $fields = []):
$resultArray[] = [
'id' => $item,
'title' => $title,
'updated_by' => '',
'created_by' => $createdBy,
'updated_by' => $updatedBy,
'type' => $type,
'completion' => $completion,
'completion_method' => $completionMethod,
Expand Down
8 changes: 8 additions & 0 deletions main/install/configuration.dist.php
Expand Up @@ -2377,6 +2377,14 @@
// 3. Add an "@" before "ORM\Entity" in the "CWikiCategory" class definition (in src/Chamilo/CourseBundle/Entity/CWikiCategory.php)
//$_configuration['wiki_categories_enabled'] = false;

// 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
// you use an external authentication system, you can return the extra field used to match the user with the
// external authentication system rather than user.id.
// $_configuration['webservice_return_user_field'] = 'oauth2_id';


// KEEP THIS AT THE END
// -------- Custom DB changes
// Add user activation by confirmation email
Expand Down

0 comments on commit b0823cc

Please sign in to comment.