ILIAS  trunk Revision v11.0_alpha-1846-g895b5f47236
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Rating.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\UI\Component as C;
28 
29 class 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);
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 
81  protected function getConstraintForRequirement(): ?Constraint
82  {
83  if ($this->requirement_constraint !== null) {
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  {
113  return $this->current_average;
114  }
115 
116 }
Transform values according to custom configuration.
FiveStarRatingScale
This is the scale for the Rating Input.
Interface Observer Contains several chained tasks and infos about them.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
Factory for Date Formats.
Definition: Factory.php:26
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
setAdditionalTransformation(Transformation $trafo)
Apply a transformation to the current or future content.
Definition: Input.php:158
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(Container $dic, ilPlugin $plugin)
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, string $label, ?string $byline)
Definition: Rating.php:34
This describes inputs that can be used in forms.
Definition: FormInput.php:32