ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilExcRandomAssignmentManager.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
13{
14 const DENIED_SUBMISSIONS = "has_submissions";
15 const DENIED_PEER_REVIEWS = "has_peer_reviews";
16 const DENIED_TEAM_ASSIGNMENTS = "has_team_assignments";
17
21 protected $exc;
22
26 protected $exc_id;
27
31 protected $user;
32
36 protected $rand_ass_repo;
37
41 protected $lng;
42
47
51 public function __construct(
54 ilObjUser $user = null,
55 ilLanguage $lng = null
56 ) {
57 global $DIC;
58
59 $this->exc = $exc;
60 $this->exc_id = $this->exc->getId();
61 $this->user = (is_null($user))
62 ? $DIC->user()
63 : $user;
64 $this->rand_ass_repo = $rand_ass_repo;
65 $this->lng = (is_null($lng))
66 ? $DIC->language()
67 : $lng;
68
69 $this->submission_repo = new ilExcSubmissionRepository($DIC->database());
70 }
71
77 public function canBeActivated()
78 {
80 foreach (ilExAssignment::getInstancesByExercise($this->exc_id) as $ass) {
81 if ($ass->getPeerReview() || $ass->getAssignmentType()->usesTeams()) {
82 return false;
83 }
84 }
85 return !$this->hasAnySubmission();
86 }
87
93 public function getDeniedActivationReasons()
94 {
96 $lng->loadLanguageModule("exc");
97 $has_peer_reviews = false;
98 $has_teams = false;
100 foreach (ilExAssignment::getInstancesByExercise($this->exc_id) as $ass) {
101 if ($ass->getPeerReview()) {
102 $has_peer_reviews = true;
103 }
104 if ($ass->getAssignmentType()->usesTeams()) {
105 $has_teams = true;
106 }
107 }
108 $reasons = [];
109 if ($this->hasAnySubmission()) {
110 $reasons[self::DENIED_SUBMISSIONS] = $lng->txt("exc_denied_has_submissions");
111 }
112 if ($has_peer_reviews) {
113 $reasons[self::DENIED_PEER_REVIEWS] = $lng->txt("exc_denied_has_peer_reviews");
114 }
115 if ($has_teams) {
116 $reasons[self::DENIED_TEAM_ASSIGNMENTS] = $lng->txt("exc_denied_has_team_assignments");
117 }
118 return $reasons;
119 }
120
126 public function canBeDeactivated()
127 {
128 return !$this->hasAnySubmission();
129 }
130
137 {
139 $lng->loadLanguageModule("exc");
140 $reasons = [];
141 if ($this->hasAnySubmission()) {
142 $reasons[self::DENIED_SUBMISSIONS] = $lng->txt("exc_denied_has_submissions");
143 }
144 return $reasons;
145 }
146
152 public function isActivated()
153 {
154 return ($this->exc->getPassMode() == ilObjExercise::PASS_MODE_RANDOM);
155 }
156
163 {
164 return count(ilExAssignment::getInstancesByExercise($this->exc_id));
165 }
166
173 {
174 return $this->exc->getNrMandatoryRandom();
175 }
176
180 protected function hasAnySubmission()
181 {
182
184 foreach (ilExAssignment::getInstancesByExercise($this->exc_id) as $ass) {
185 if ($this->submission_repo->hasSubmissions($ass->getId())) {
186 return true;
187 }
188 }
189 return false;
190 }
191
196 public function needsStart()
197 {
198 if ($this->isActivated()) {
199 $ass_of_user = $this->rand_ass_repo->getAssignmentsOfUser($this->user->getId(), $this->exc_id);
200 if (count($ass_of_user) == 0) {
201 return true;
202 }
203 }
204 return false;
205 }
206
213 public function getMandatoryAssignmentsOfUser($user_id)
214 {
215 return $this->rand_ass_repo->getAssignmentsOfUser($user_id, $this->exc_id);
216 }
217
218
222 public function startExercise()
223 {
224 if ($this->needsStart()) {
225 $this->rand_ass_repo->saveAssignmentsOfUser(
226 $this->user->getId(),
227 $this->exc_id,
228 $this->getAssignmentSelection()
229 );
230 }
231 }
232
236 protected function getAssignmentSelection()
237 {
238 $ass_ids = array_map(function ($i) {
239 return $i->getId();
241
242 $selected = [];
243 for ($i = 0; $i < $this->getNumberOfMandatoryAssignments(); $i++) {
244 $j = rand(0, count($ass_ids) - 1);
245 $selected[] = current(array_splice($ass_ids, $j, 1));
246 }
247
248 return $selected;
249 }
250
258 public function isAssignmentVisible(int $ass_id, int $user_id)
259 {
260 if ($this->isActivated() && !in_array($ass_id, $this->getMandatoryAssignmentsOfUser($user_id))) {
261 return false;
262 }
263 return true;
264 }
265}
user()
Definition: user.php:4
An exception for terminatinating execution or to throw for unit testing.
static getInstancesByExercise($a_exc_id)
Stores info about random assignments for users in exercises.
Manages random mandatory assignments of an exercise.
getDeniedDeactivationReasons()
Get reasons for denied deactivation.
canBeDeactivated()
Checks if the random assignment can be activated (if no learner has already submitted stuff)
__construct(ilObjExercise $exc, ilExcRandomAssignmentDBRepository $rand_ass_repo, ilObjUser $user=null, ilLanguage $lng=null)
Constructor.
isActivated()
Is random assignment activated?
getAssignmentSelection()
Get random assignment selection.
getTotalNumberOfAssignments()
Get total number of assignments.
needsStart()
Needs current user to start the exercise (by selecting the random assignments)?
getMandatoryAssignmentsOfUser($user_id)
Get mandatory assignments of user.
getNumberOfMandatoryAssignments()
Get total number of mandatory assignments.
isAssignmentVisible(int $ass_id, int $user_id)
Is assignment visible for user.
language handling
Class ilObjExercise.
$i
Definition: metadata.php:24
$DIC
Definition: xapitoken.php:46