Skip to content

Commit

Permalink
WebServices: get work list (details for works)
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Aug 25, 2021
1 parent 8f79a6f commit 5ab7fe0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
64 changes: 64 additions & 0 deletions main/inc/lib/webservices/Rest.php
Expand Up @@ -60,6 +60,7 @@ class Rest extends WebService
const SAVE_FORUM_THREAD = 'save_forum_thread';
const SET_THREAD_NOTIFY = 'set_thread_notify';

const GET_WORK_LIST = 'get_work_list';
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 @@ -2653,6 +2654,69 @@ public function deleteWorkCorrections(int $workId): string
return get_lang('Deleted');
}

public function getWorkList(int $workId): array
{
$isAllowedToEdit = api_is_allowed_to_edit();

Event::event_access_tool(TOOL_STUDENTPUBLICATION);

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

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

$courseInfo = api_get_course_info_by_id($courseId);
$webPath = api_get_path(WEB_PATH);

$whereCondition = !$isAllowedToEdit ? " AND u.id = $userId" : '';

$works = get_work_user_list(
0,
0,
'title',
'asc',
$workId,
$whereCondition,
null,
false,
$courseId,
$sessionId
);

return array_map(
function (array $work) use ($courseInfo, $webPath) {
$itemId = $work['id'];
$count = getWorkCommentCount($itemId, $courseInfo);

$work['feedback'] = $count.' '.Display::returnFontAwesomeIcon('comments-o');
$work['feedback_clean'] = $count;

$workInfo = get_work_data_by_id($itemId);
$commentsTmp = getWorkComments($workInfo);
$comments = [];

foreach ($commentsTmp as $comment) {
$comment['comment'] = str_replace('src="/', 'src="'.$webPath.'app/', $comment['comment']);
$comments[] = $comment;
}

$work['comments'] = $comments;

if (empty($workInfo['qualificator_id'])) {
$qualificator_id = Display::label(get_lang('NotRevised'), 'warning');
} else {
$qualificator_id = Display::label(get_lang('Revised'), 'success');
}

$work['qualificator_id'] = $qualificator_id;

return $work;
},
$works
);
}

public static function isAllowedByRequest(bool $inpersonate = false): bool
{
$username = $_GET['username'] ?? null;
Expand Down
9 changes: 9 additions & 0 deletions main/webservices/api/v2.php
Expand Up @@ -296,6 +296,15 @@
);
break;

case Rest::GET_WORK_LIST:
if (!isset($_GET['work'])) {
throw new Exception(get_lang('ActionNotAllowed'));
}

$restResponse->setData(
$restApi->getWorkList((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 5ab7fe0

Please sign in to comment.