Skip to content

Commit

Permalink
Filter labels if annotation session is active
Browse files Browse the repository at this point in the history
  • Loading branch information
lehecht committed Jan 30, 2025
1 parent cc998d5 commit 0398b6c
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/Http/Controllers/Api/Volumes/VolumeAnnotationLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

namespace Biigle\Modules\Largo\Http\Controllers\Api\Volumes;

use Biigle\Label;
use Biigle\Volume;
use Biigle\ImageAnnotation;
use Biigle\VideoAnnotation;
use Illuminate\Http\Request;
use Biigle\Http\Controllers\Api\Controller;

class VolumeAnnotationLabels extends Controller
{
/**
* Get all annotation labels and annotation count for a given volume
*
*
* @api {get} volumes/:vid/label-count Get annotation labels with a annotation count
* @apiGroup Volumes
* @apiName test
* @apiParam {Number} id The Volume ID
* @apiPermission projectMember
* @apiDescription Returns a collection of annotation labels and their counts in the volume
*
*
* @apiSuccessExample {json} Success response:
* [{"id":1,
* "name":"a",
Expand All @@ -28,26 +30,46 @@ class VolumeAnnotationLabels extends Controller
* @param int $id Volume ID
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getVolumeAnnotationLabels($id)
public function getVolumeAnnotationLabels(Request $request, $id)
{
$volume = Volume::findOrFail($id);
$this->authorize('access', $volume);
$this->validate($request, ['take' => 'integer']);
$take = $request->input('take');
$isImageVolume = $volume->isImageVolume();

$session = $volume->getActiveAnnotationSession($request->user());

if ($session) {
$query = $isImageVolume ? ImageAnnotation::allowedBySession($session, $request->user()) :
VideoAnnotation::allowedBySession($session, $request->user());
} else {
$query = $isImageVolume ? ImageAnnotation::query() : VideoAnnotation::query();
}

if ($volume->isImageVolume()) {
$labelQuery = Label::query()
->join('image_annotation_labels', 'labels.id', '=', 'image_annotation_labels.label_id')
->join('image_annotations', 'image_annotation_labels.annotation_id', '=', 'image_annotations.id')
if ($isImageVolume) {
$labelQuery = $query
->join('image_annotation_labels', 'image_annotations.id', '=', 'image_annotation_labels.annotation_id')
->join('labels', 'image_annotation_labels.label_id', '=', 'labels.id')
->join('images', 'image_annotations.image_id', '=', 'images.id')
->where('images.volume_id', '=', $id);
} else {
$labelQuery = Label::query()
->join('video_annotation_labels', 'labels.id', '=', 'video_annotation_labels.label_id')
->join('video_annotations', 'video_annotation_labels.annotation_id', '=', 'video_annotations.id')
$labelQuery = $query
->join('video_annotation_labels', 'video_annotations.id', '=', 'video_annotation_labels.annotation_id')
->join('labels', 'video_annotation_labels.label_id', '=', 'labels.id')
->join('videos', 'video_annotations.video_id', '=', 'videos.id')
->where('videos.volume_id', '=', $id);
}

return $labelQuery
->when($session, function ($query) use ($session, $request) {
if ($session->hide_other_users_annotations) {
$query->where('image_annotation_labels.user_id', $request->user()->id);
}
})
->when(!is_null($take), function ($query) use ($take) {
return $query->take($take);
})
->selectRaw('labels.id, labels.name, labels.color, labels.label_tree_id, count(labels.id) as count')
->groupBy(['labels.id', 'labels.name', 'labels.color', 'labels.label_tree_id'])
->orderBy('labels.name')
Expand Down

0 comments on commit 0398b6c

Please sign in to comment.