ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.RandomAssignmentsManager.php
Go to the documentation of this file.
1<?php
2
20
23
30{
31 public const DENIED_SUBMISSIONS = "has_submissions";
32 public const DENIED_PEER_REVIEWS = "has_peer_reviews";
33 public const DENIED_TEAM_ASSIGNMENTS = "has_team_assignments";
34
35 protected \ilObjExercise $exc;
36 protected int $exc_id;
37 protected \ilObjUser $user;
39 protected \ilLanguage $lng;
41
42 public function __construct(
46 ?\ilObjUser $user = null,
47 ?\ilLanguage $lng = null
48 ) {
49 global $DIC;
50
51 $this->exc = $exc;
52 $this->exc_id = $this->exc->getId();
53 $this->user = (is_null($user))
54 ? $DIC->user()
55 : $user;
56 $this->rand_ass_repo = $rand_ass_repo;
57 $this->lng = (is_null($lng))
58 ? $DIC->language()
59 : $lng;
60
61 $this->submission_repo = $submission_repo;
62 }
63
64 // Checks if the random assignment can be activated (if no learner has already submitted stuff)
65 public function canBeActivated(): bool
66 {
68 foreach (\ilExAssignment::getInstancesByExercise($this->exc_id) as $ass) {
69 if ($ass->getPeerReview() || $ass->getAssignmentType()->usesTeams()) {
70 return false;
71 }
72 }
73 return !$this->hasAnySubmission();
74 }
75
81 public function getDeniedActivationReasons(): array
82 {
85 $has_peer_reviews = false;
86 $has_teams = false;
88 foreach (\ilExAssignment::getInstancesByExercise($this->exc_id) as $ass) {
89 if ($ass->getPeerReview()) {
90 $has_peer_reviews = true;
91 }
92 if ($ass->getAssignmentType()->usesTeams()) {
93 $has_teams = true;
94 }
95 }
96 $reasons = [];
97 if ($this->hasAnySubmission()) {
98 $reasons[self::DENIED_SUBMISSIONS] = $lng->txt("exc_denied_has_submissions");
99 }
100 if ($has_peer_reviews) {
101 $reasons[self::DENIED_PEER_REVIEWS] = $lng->txt("exc_denied_has_peer_reviews");
102 }
103 if ($has_teams) {
104 $reasons[self::DENIED_TEAM_ASSIGNMENTS] = $lng->txt("exc_denied_has_team_assignments");
105 }
106 return $reasons;
107 }
108
109 // Checks if the random assignment can be activated (if no learner has already submitted stuff)
110 public function canBeDeactivated(): bool
111 {
112 return !$this->hasAnySubmission();
113 }
114
119 public function getDeniedDeactivationReasons(): array
120 {
122 $lng->loadLanguageModule("exc");
123 $reasons = [];
124 if ($this->hasAnySubmission()) {
125 $reasons[self::DENIED_SUBMISSIONS] = $lng->txt("exc_denied_has_submissions");
126 }
127 return $reasons;
128 }
129
130 // Is random assignment activated?
131 public function isActivated(): bool
132 {
133 return ($this->exc->getPassMode() == \ilObjExercise::PASS_MODE_RANDOM);
134 }
135
137 {
138 return count(\ilExAssignment::getInstancesByExercise($this->exc_id));
139 }
140
142 {
143 return $this->exc->getNrMandatoryRandom();
144 }
145
146 protected function hasAnySubmission(): bool
147 {
149 foreach (\ilExAssignment::getInstancesByExercise($this->exc_id) as $ass) {
150 if ($this->submission_repo->hasSubmissions($ass->getId())) {
151 return true;
152 }
153 }
154 return false;
155 }
156
160 public function needsStart(): bool
161 {
162 if ($this->isActivated()) {
163 $ass_of_user = $this->rand_ass_repo->getAssignmentsOfUser($this->user->getId(), $this->exc_id);
164 if (count($ass_of_user) == 0) {
165 return true;
166 }
167 }
168 return false;
169 }
170
177 public function getMandatoryAssignmentsOfUser(int $user_id): array
178 {
179 return $this->rand_ass_repo->getAssignmentsOfUser($user_id, $this->exc_id);
180 }
181
182
183 // Start exercise
184 public function startExercise(): void
185 {
186 if ($this->needsStart()) {
187 $this->rand_ass_repo->saveAssignmentsOfUser(
188 $this->user->getId(),
189 $this->exc_id,
190 $this->getAssignmentSelection()
191 );
192 }
193 }
194
200 protected function getAssignmentSelection(): array
201 {
202 $ass_ids = array_map(function ($i) {
203 return $i->getId();
205
206 $selected = [];
207 for ($i = 0; $i < $this->getNumberOfMandatoryAssignments(); $i++) {
208 $j = rand(0, count($ass_ids) - 1);
209 $selected[] = current(array_splice($ass_ids, $j, 1));
210 }
211
212 return $selected;
213 }
214
222 public function isAssignmentVisible(
223 int $ass_id,
224 int $user_id
225 ): bool {
226 if ($this->isActivated() && !in_array($ass_id, $this->getMandatoryAssignmentsOfUser($user_id))) {
227 return false;
228 }
229 return true;
230 }
231}
Stores info about random assignments for users in exercises.
Manages random mandatory assignments of an exercise (business logic)
isAssignmentVisible(int $ass_id, int $user_id)
Is assignment visible for user.
getMandatoryAssignmentsOfUser(int $user_id)
Get mandatory assignments of user.
needsStart()
Needs current user to start the exercise (by selecting the random assignments)?
__construct(\ilObjExercise $exc, RandomAssignmentsDBRepository $rand_ass_repo, Submission\SubmissionRepositoryInterface $submission_repo, ?\ilObjUser $user=null, ?\ilLanguage $lng=null)
static getInstancesByExercise(int $a_exc_id)
language handling
loadLanguageModule(string $a_module)
Load language module.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
Class ilObjExercise.
User class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26