Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make training depends and can_award arrays #908

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion schema/data-auth.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]
]
11 changes: 11 additions & 0 deletions schema/patches/4.sql
Original file line number Diff line number Diff line change
@@ -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;
64 changes: 46 additions & 18 deletions src/Classes/ServiceAPI/MyRadio_TrainingStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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)
{
Expand Down