Skip to content

Commit

Permalink
Implement officers UI + edit page
Browse files Browse the repository at this point in the history
  • Loading branch information
markspolakovs committed Dec 16, 2022
1 parent ed88633 commit f0792d0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
32 changes: 22 additions & 10 deletions src/Classes/ServiceAPI/MyRadio_Officer.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,24 @@ protected function __construct($id)
/**
* Create a new Officer position.
*
* @param string $name The position name, e.g. "Station Cat"
* @param string $descr A description of the position "official feline"
* @param string $alias Email alias (may be NULL) e.g. station.cat
* @param int $ordering Weighting when appearing in lists e.g. 0
* @param MyRadio_Team $team The Team the Officer is part of
* @param char $type 'm'ember, 'o'fficer, 'a'ssistant head, 'h'ead
* @param string $name The position name, e.g. "Station Cat"
* @param string $descr A description of the position "official feline"
* @param string $alias Email alias (may be NULL) e.g. station.cat
* @param int $ordering Weighting when appearing in lists e.g. 0
* @param MyRadio_Team $team The Team the Officer is part of
* @param char $type 'm'ember, 'o'fficer, 'a'ssistant head, 'h'ead
* @param int $num_places How many people can have this officership (default 1)
*
* @return MyRadio_Officer The new Officer position
*/
public static function createOfficer($name, $descr, $alias, $ordering, MyRadio_Team $team, $type = 'o')
public static function createOfficer($name, $descr, $alias, $ordering, MyRadio_Team $team, $type = 'o', $num_places = 1)
{
return self::getInstance(
self::$db->fetchColumn(
'INSERT INTO public.officer
(officer_name, officer_alias, teamid, ordering, descr, type)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING officerid',
[$name, $alias, $team->getID(), $ordering, $descr, $type]
(officer_name, officer_alias, teamid, ordering, descr, type, num_places)
VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING officerid',
[$name, $alias, $team->getID(), $ordering, $descr, $type, $num_places]
)[0]
);
}
Expand Down Expand Up @@ -555,6 +556,17 @@ public function getNumPlaces()
return $this->num_places;
}

public function setNumPlaces(int $val)
{
self::$db->query('
UPDATE public.officer
SET num_places = $1
WHERE officerid = $2
', [$val, $this->getID()]);
$this->num_places = $val;
$this->updateCacheObject();
}

/**
* Returns all the officer's active permission flags.
*
Expand Down
37 changes: 36 additions & 1 deletion src/Classes/ServiceAPI/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static function getOfficers()
if (self::$officers === false) {
self::wakeup();
self::$officers = self::$db->fetchAll(
'SELECT team.team_name AS team, officer.type, officer.officer_name AS officership,
'SELECT team.team_name AS team, officer.type, officer.officer_name AS officership, officer.num_places,
fname || \' \' || sname AS name, member.memberid, officer.officerid
FROM team
LEFT JOIN officer ON team.teamid = officer.teamid AND officer.status = \'c\'
Expand All @@ -145,8 +145,43 @@ public static function getOfficers()
WHERE team.status = \'c\' AND officer.type != \'m\'
ORDER BY team.ordering, officer.ordering, sname'
);
// Insert into the list duplicate entries for vacant positions
// For example, if ASM has num_positions=2, and only one is filled,
// we need to insert another ASM entry with null name/memberid

// keyed by officerid
$num_filled_positions = [];
// dto - avoid iterating multiple times
$num_avail_positions = [];
$last_index_of_officer = [];
foreach (self::$officers as $i => $off) {
$num_avail_positions[$off['officerid']] = (int)$off['num_places'];
if ($off['memberid'] !== null) {
$num_filled_positions[$off['officerid']]++;
}
$last_index_of_officer[$off['officerid']] = $i;
}

$offset = 0;
foreach ($num_avail_positions as $officerid => $avail) {
if ($num_filled_positions[$officerid] < $avail && $avail > 1) {
$vacancies = ($avail ?? 1) - $num_filled_positions[$officerid];
if ($num_filled_positions[$officerid] == 0) {
$vacancies--;
}
for ($i = 0; $i < $vacancies; $i++) {
$vacancy = self::$officers[$last_index_of_officer[$officerid]+$offset];
$vacancy['memberid'] = null;
$vacancy['name'] = null;
array_splice(self::$officers, $last_index_of_officer[$officerid]+$offset+1, 0, [$vacancy]);
$offset++;
}
}
}

self::$cache->set('MyRadioProfile_officers', self::$officers);
}
// var_dump(self::$officers);

return self::$officers;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Controllers/Profile/editOfficer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
$data['alias'],
$data['ordering'],
$data['team'],
$data['type']
$data['type'],
(int) $data['num_places']
);
} else {
//submit edit
Expand All @@ -31,7 +32,8 @@
->setOrdering($data['ordering'])
->setTeam($data['team'])
->setType($data['type'])
->setStatus($data['status']);
->setStatus($data['status'])
->setNumPlaces((int) $data['num_places']);

// remove empty permissions values
$data['permissions'] = array_filter($data['permissions']['permission']) ?: [];
Expand Down
4 changes: 4 additions & 0 deletions src/Public/js/myradio.profile.listOfficers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ $(".twig-datatable").dataTable({
{
"sTitle": "Officership",
},
// numPlaces
{
bVisible: false
},
//name
{
"sTitle": "Name"
Expand Down

0 comments on commit f0792d0

Please sign in to comment.