ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Rating.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\Data\Factory as DataFactory;
28
29class Rating extends FormInput implements C\Input\Field\Rating
30{
31 protected ?string $text = null;
32 protected ?float $current_average = null;
33
34 public function __construct(
35 DataFactory $data_factory,
37 string $label,
38 ?string $byline
39 ) {
40 parent::__construct($data_factory, $refinery, $label, $byline);
41 $this->setAdditionalTransformation($this->getFiveStarRatingScaleTransformation());
42 }
43
45 {
46 return $this->refinery->custom()->transformation(
47 static function ($v): ?FiveStarRatingScale {
48 if(is_null($v) || $v instanceof FiveStarRatingScale) {
49 return $v;
50 }
51 return FiveStarRatingScale::from((int)$v);
52 }
53 );
54 }
55
56 public function withAdditionalText(?string $text): static
57 {
58 $clone = clone $this;
59 $clone->text = $text;
60 return $clone;
61 }
62
63 public function getAdditionalText(): ?string
64 {
65 return $this->text;
66 }
67
68 public function withValue($value): self
69 {
70 if(!$value instanceof FiveStarRatingScale) {
71 $value = $this->getFiveStarRatingScaleTransformation()->transform($value);
72 }
73 return parent::withValue($value);
74 }
75
76 public function isClientSideValueOk($value): bool
77 {
78 return is_null($value) || is_numeric($value) || $value instanceof FiveStarRatingScale;
79 }
80
82 {
83 if ($this->requirement_constraint !== null) {
84 return $this->requirement_constraint;
85 }
86 return $this->refinery->custom()->constraint(
87 static fn($v) => $v instanceof FiveStarRatingScale && $v->value > 0,
88 'no rating given'
89 );
90 }
91
92 public function getUpdateOnLoadCode(): \Closure
93 {
94 return fn($id) => "$('#$id').on('input', function(event) {
95 il.UI.input.onFieldUpdate(event, '$id', $('#$id').val());
96 });
97 il.UI.input.onFieldUpdate(event, '$id', $('#$id').val());";
98 }
99
100 public function withCurrentAverage(?float $current_average): static
101 {
102 $max = count(FiveStarRatingScale::cases()) - 1;
103 if($current_average < 0 || $current_average > $max) {
104 throw new \InvalidArgumentException('current_average must be between 0 and ' . $max);
105 }
106 $clone = clone $this;
107 $clone->current_average = $current_average;
108 return $clone;
109 }
110
111 public function getCurrentAverage(): ?float
112 {
114 }
115
116}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Factory for Date Formats.
Definition: Factory.php:27
Builds data types.
Definition: Factory.php:36
FiveStarRatingScale
This is the scale for the Rating Input.
Transform values according to custom configuration.
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, string $label, ?string $byline)
Definition: Rating.php:34
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Rating.php:68
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
This describes inputs that can be used in forms.
Definition: FormInput.php:33
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.