diff --git a/schema/data-auth.json b/schema/data-auth.json index f2ca85f70..5035928ef 100644 --- a/schema/data-auth.json +++ b/schema/data-auth.json @@ -81,5 +81,6 @@ ["Tracklist own/current show","AUTH_TRACKLIST_OWN", true], ["Tracklist for any show/timeslot","AUTH_TRACKLIST_ALL", true], ["Access WebStudio", "AUTH_ACCESS_WEBSTUDIO", true], - ["View Messages for Any Show", "AUTH_ANY_SHOW_MESSAGES", true] + ["View Messages for Any Show", "AUTH_ANY_SHOW_MESSAGES", true], + ["Give Anyone Any Training Status", "AUTH_AWARDANYTRAINING", true] ] diff --git a/schema/patches/4.sql b/schema/patches/4.sql new file mode 100644 index 000000000..de6756f62 --- /dev/null +++ b/schema/patches/4.sql @@ -0,0 +1,11 @@ +BEGIN; + +ALTER TABLE public.l_presenterstatus + ALTER COLUMN can_award + TYPE INTEGER[] + USING ARRAY[can_award], + ALTER COLUMN depends + TYPE INTEGER[] + USING ARRAY[depends]; + +COMMIT; \ No newline at end of file diff --git a/src/Classes/ServiceAPI/MyRadio_TrainingStatus.php b/src/Classes/ServiceAPI/MyRadio_TrainingStatus.php index e4b3055f8..d1188ff57 100644 --- a/src/Classes/ServiceAPI/MyRadio_TrainingStatus.php +++ b/src/Classes/ServiceAPI/MyRadio_TrainingStatus.php @@ -42,16 +42,16 @@ class MyRadio_TrainingStatus extends ServiceAPI private $ordering; /** - * The Training Status a member must have before achieving this one. + * The Training Status-es a member must have before achieving this one. Uses AND logic. * - * @var MyRadio_TrainingStatus + * @var int[]|null */ private $depends; /** - * The Training Status a member must have in order to award this one. + * The Training Status-es a member must have in order to award this one. Uses AND logic. * - * @var MyRadio_TrainingStatus + * @var int[]|null */ private $can_award; @@ -89,20 +89,22 @@ protected function __construct($statusid) { $this->presenterstatusid = (int) $statusid; - $result = self::$db->fetchOne('SELECT * FROM public.l_presenterstatus WHERE presenterstatusid=$1', [$statusid]); + $result = self::$db->fetchOne( + 'SELECT descr, ordering, detail, array_to_json(depends), array_to_json(can_award) + FROM public.l_presenterstatus WHERE presenterstatusid=$1', + [$statusid] + ); if (empty($result)) { throw new MyRadioException('The specified Training Status ('.$statusid.') does not seem to exist', 404); - - return; } $this->descr = $result['descr']; $this->ordering = (int) $result['ordering']; $this->detail = $result['detail']; - $this->depends = empty($result['depends']) ? null : $result['depends']; - $this->can_award = empty($result['can_award']) ? null : $result['can_award']; + $this->depends = empty($result['depends']) ? null : json_decode($result['depends']); + $this->can_award = empty($result['can_award']) ? null : json_decode($result['can_award']); $this->permissions = array_map( 'intval', @@ -160,15 +162,17 @@ public function getPermissions() * * Returns null if there is no dependency. * - * @return MyRadio_TrainingStatus + * @return MyRadio_TrainingStatus[] */ public function getDepends() { - return empty($this->depends) ? null : self::getInstance($this->depends); + return empty($this->depends) + ? [] + : array_map(['MyRadio\ServiceAPI\MyRadio_TrainingStatus', 'getInstance'], $this->depends); } /** - * Checks if the user has the Training Status the one depends on. + * Checks if the user has all the Training Status-es the one depends on. * * @param MyRadio_User $user Default current User. * @@ -180,17 +184,27 @@ public function hasDependency(MyRadio_User $user = null) $user = MyRadio_User::getInstance(); } - return $this->getDepends() == null or $this->getDepends()->isAwardedTo($user); + if (empty($this->depends)) { + return true; + } + foreach($this->getDepends() as $dep) { + if (!$dep->isAwardedTo($user)) { + return false; + } + } + return true; } /** - * Gets the TrainingStatus a member must have before awarding this one. + * Gets all the TrainingStatus-es a member must have before awarding this one. * - * @return MyRadio_TrainingStatus + * @return MyRadio_TrainingStatus[] */ public function getAwarder() { - return self::getInstance($this->can_award); + return empty($this->canAward()) + ? [] + : array_map(['MyRadio\ServiceAPI\MyRadio_TrainingStatus', 'getInstance'], $this->can_award); } /** @@ -206,7 +220,21 @@ public function canAward(MyRadio_User $user = null) $user = MyRadio_User::getInstance(); } - return $this->getAwarder()->isAwardedTo($user); + if ($user->hasAuth(AUTH_AWARDANYTRAINING)) { + // I am become trainer, doer of trainings + return true; + } + + if (empty($this->can_award)) { + return false; + } + + foreach ($this->getAwarder() as $awarder) { + if (!$awarder->isAwardedTo($user)) { + return false; + } + } + return true; } /** @@ -216,7 +244,7 @@ public function canAward(MyRadio_User $user = null) * @param int $ids If true, just returns User Training Status IDs instead of * UserTrainingStatuses. * - * @return MyRadio_User[]|int + * @return MyRadio_UserTrainingStatus[]|int */ public function getAwardedTo($ids = false) {