Skip to content

Commit 54d0a8e

Browse files
authored
Merge pull request #66 from topcoder-platform/issues-469
Issues-469: archive/unarchive groups
2 parents e458a94 + 5c5692d commit 54d0a8e

File tree

3 files changed

+68
-23
lines changed

3 files changed

+68
-23
lines changed

controllers/api/GroupsApiController.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function get($id) {
112112
if (!$group) {
113113
throw new NotFoundException('Group');
114114
}
115-
return $group;
115+
return ApiUtils::convertOutputKeys($group);
116116
}
117117

118118
/**
@@ -254,7 +254,7 @@ public function get_members($id, array $query) {
254254
* @throws ServerException If the group could not be archived.
255255
* @return
256256
*/
257-
public function put_archive($id, array $body) {
257+
public function put_archive($id) {
258258
$this->permission('Groups.Group.Archive');
259259
$this->idParamSchema();
260260

@@ -266,10 +266,31 @@ public function put_archive($id, array $body) {
266266
if(!$this->groupModel->canArchiveGroup($group)) {
267267
throw new ClientException('Don\'t have permissions to archive this group.');
268268
}
269-
270-
$this->groupModel->archiveGroup($group->GroupID);
269+
$this->groupModel->archiveGroup($group->GroupID, 1);
271270
}
272271

272+
/**
273+
* Unarchive a group.
274+
*
275+
* @param int $id The ID of the group.
276+
* @throws NotFoundException if the group could not be found.
277+
* @throws ServerException If the group could not be archived.
278+
* @return
279+
*/
280+
public function put_unarchive($id) {
281+
$this->permission('Groups.Group.Archive');
282+
$this->idParamSchema();
283+
284+
$group = $this->groupModel->getByGroupID($id);
285+
if(!$group) {
286+
throw new NotFoundException('Group');
287+
}
288+
289+
if(!$this->groupModel->canArchiveGroup($group)) {
290+
throw new ClientException('Don\'t have permissions to unarchive this group.');
291+
}
292+
$this->groupModel->archiveGroup($group->GroupID, 0);
293+
}
273294

274295
/**
275296
* Remove a member from a group
@@ -415,6 +436,17 @@ public function groupMemberPatchSchema() {
415436
['watch:b' => 'Watch status'], 'in');
416437
}
417438

439+
/**
440+
* Get Group Archive schema.
441+
*
442+
* @param string $type The type of schema.
443+
* @return Schema Returns a schema object.
444+
*/
445+
public function groupArchiveSchema() {
446+
return $this->schema(
447+
['archived:b' => 'Archived status'], 'in');
448+
}
449+
418450
/**
419451
* Get a GroupID/UserID - only conversation record schema.
420452
*

models/class.groupmodel.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,39 +1707,35 @@ public function notifyJoinGroup($groupID, $userID) {
17071707
$activityModel->save($data);
17081708
}
17091709

1710-
17111710
public function canArchiveGroup($group){
17121711
return Gdn::session()->UserID == $group->OwnerID || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_GROUP_ARCHIVE_PERMISSION);
17131712
}
17141713

1714+
public function canUnarchiveGroup($group){
1715+
return Gdn::session()->UserID == $group->OwnerID || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_GROUP_ARCHIVE_PERMISSION);
1716+
}
1717+
17151718
/**
1716-
* Archive a group and its categories
1719+
* Archive/Unarchive a group and its categories
17171720
*
17181721
* @param $group
1722+
* @param $archived boolean
17191723
*/
1720-
public function archiveGroup($group){
1724+
public function archiveGroup($group, $archived){
17211725
if(is_numeric($group) && $group > 0) {
17221726
$group = $this->getByGroupID($group);
17231727
}
17241728

1725-
if($group->ChallengeID) {
1726-
$categoryModel = new CategoryModel();
1727-
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
1728-
if($groupCategory->DisplayAs !== 'Discussions') {
1729-
$categories = CategoryModel::getSubtree($groupCategory->CategoryID, true);
1730-
$categoryIDs = array_column($categories, 'CategoryID');
1731-
} else {
1732-
$categoryIDs = [$groupCategory->CategoryID];
1733-
}
1734-
1735-
foreach($categoryIDs as $categoryID) {
1736-
$category = $categoryModel->getID($categoryID, DATASET_TYPE_ARRAY);
1737-
$category['Archived'] = 1;
1738-
$categoryModel->save($category);
1739-
}
1729+
$categories = Gdn::sql()->getWhere('Category', ['GroupID' => $group->GroupID])->resultArray();
1730+
$categoryIDs = array_column($categories, 'CategoryID');
1731+
$categoryModel = new CategoryModel();
1732+
foreach($categoryIDs as $categoryID) {
1733+
$category = $categoryModel->getID($categoryID, DATASET_TYPE_ARRAY);
1734+
$category['Archived'] = $archived;
1735+
$categoryModel->save($category);
17401736
}
17411737

1742-
$group->Archived = 1;
1738+
$group->Archived = $archived;
17431739
$this->save($group);
17441740
}
17451741

openapi/groups.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,23 @@ paths:
261261
requestBody:
262262
required: false
263263
summary: Archive a group.
264+
'/groups/{id}/unarchive':
265+
put:
266+
parameters:
267+
- description: The group ID.
268+
in: path
269+
name: id
270+
required: true
271+
schema:
272+
type: integer
273+
responses:
274+
'204':
275+
description: Success
276+
tags:
277+
- Groups
278+
requestBody:
279+
required: false
280+
summary: Unarchive a group.
264281
components:
265282
requestBodies:
266283
GroupPost:

0 commit comments

Comments
 (0)