Skip to content

Commit

Permalink
WebServices: get work users
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Aug 25, 2021
1 parent 475fde3 commit 7e2d42c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
66 changes: 66 additions & 0 deletions main/inc/lib/webservices/Rest.php
Expand Up @@ -62,6 +62,7 @@ class Rest extends WebService

const GET_WORK_LIST = 'get_work_list';
const GET_WORK_STUDENTS_WITHOUT_PUBLICATIONS = 'get_work_students_without_publications';
const GET_WORK_USERS = 'get_work_users';
const PUT_WORK_STUDENT_ITEM_VISIBILITY = 'put_course_work_visibility';
const DELETE_WORK_STUDENT_ITEM = 'delete_work_student_item';
const DELETE_WORK_CORRECTIONS = 'delete_work_corrections';
Expand Down Expand Up @@ -2727,6 +2728,71 @@ public function getWorkStudentsWithoutPublications(int $workId): array
return get_list_users_without_publication($workId);
}

public function getWorkUsers(int $workId): array
{
Event::event_access_tool(TOOL_STUDENTPUBLICATION);

require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';

$courseId = $this->course->getId();
$sessionId = $this->session ? $this->session->getId() : 0;
$courseInfo = api_get_course_info_by_id($courseId);

$items = getAllUserToWork($workId, $courseId);
$usersAdded = [];
$result = [
'users_added' => [],
'users_to_add' => [],
];

if (!empty($items)) {
foreach ($items as $data) {
$usersAdded[] = $data['user_id'];

$userInfo = api_get_user_info($data['user_id']);

$result['users_added'][] = [
'user_id' => (int) $data['user_id'],
'complete_name_with_username' => $userInfo['complete_name_with_username'],
];
}
}

if (empty($sessionId)) {
$status = STUDENT;
} else {
$status = 0;
}

$userList = CourseManager::get_user_list_from_course_code(
$courseInfo['code'],
$sessionId,
null,
null,
$status
);

$userToAddList = [];
foreach ($userList as $user) {
if (!in_array($user['user_id'], $usersAdded)) {
$userToAddList[] = $user;
}
}

if (!empty($userToAddList)) {
foreach ($userToAddList as $user) {
$userName = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].') ';

$result['users_to_add'][] = [
'user_id' => (int) $user['user_id'],
'complete_name_with_username' => $userName,
];
}
}

return $result;
}

public static function isAllowedByRequest(bool $inpersonate = false): bool
{
$username = $_GET['username'] ?? null;
Expand Down
13 changes: 13 additions & 0 deletions main/webservices/api/v2.php
Expand Up @@ -318,6 +318,19 @@
$restApi->getWorkStudentsWithoutPublications((int) $_GET['work'])
);
break;
case Rest::GET_WORK_USERS:
if (!isset($_GET['work'])) {
throw new Exception(get_lang('ActionNotAllowed'));
}

if (!api_is_allowed_to_edit()) {
throw new Exception(get_lang('NotAllowed'));
}

$restResponse->setData(
$restApi->getWorkUsers((int) $_GET['work'])
);
break;
case Rest::PUT_WORK_STUDENT_ITEM_VISIBILITY:
if (!isset($_POST['status'], $_POST['work'])) {
throw new Exception(get_lang('ActionNotAllowed'));
Expand Down

0 comments on commit 7e2d42c

Please sign in to comment.