Skip to content

Commit deaf0bf

Browse files
authored
Merge pull request #33 from topcoder-platform/issues-229
Issues-218
2 parents 2ea6440 + 70da020 commit deaf0bf

File tree

6 files changed

+159
-27
lines changed

6 files changed

+159
-27
lines changed

controllers/class.groupcontroller.php

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,25 @@ public function edit($groupID = false) {
165165
$this->setData('Breadcrumbs', $this->buildBreadcrumb($Group));
166166

167167
} else {
168-
$Group->Type = Gdn::request()->get('type');
168+
$Group->Type = GroupModel::TYPE_REGULAR;
169169
$Group->OwnerID = Gdn::session()->UserID;
170170
$Group->LeaderID = Gdn::session()->UserID;
171171
}
172172

173-
$typesData = [GroupModel::TYPE_REGULAR => GroupModel::TYPE_REGULAR, GroupModel::TYPE_CHALLENGE => GroupModel::TYPE_CHALLENGE];
174-
$this->setData('Types', $typesData);
175-
173+
// Set Privacy Types
176174
$type = GroupsPlugin::UI[$Group->Type]['TypeName'];
177175
$privacyTypes = [GroupModel::PRIVACY_PUBLIC => sprintf('Public. Anyone can see the %s and its content. Anyone can join.', $type),
178176
GroupModel::PRIVACY_PRIVATE => sprintf('Private. Anyone can see the %s, but only members can see its content. People must apply or be invited to join.', $type),
179177
GroupModel::PRIVACY_SECRET => sprintf('Secret. Only members can see the %s and view its content. People must be invited to join.', $type)];
180178
$this->setData('PrivacyTypes', $privacyTypes);
181179

180+
// Set Type dropbox
181+
if($Group->Type == GroupModel::TYPE_REGULAR || $groupID === false) { // Regular Groups can be created from UI only
182+
$typesData = [GroupModel::TYPE_REGULAR => GroupModel::TYPE_REGULAR, GroupModel::TYPE_CHALLENGE => GroupModel::TYPE_CHALLENGE];
183+
} else if ($Group->Type == GroupModel::TYPE_CHALLENGE){
184+
$typesData = [GroupModel::TYPE_CHALLENGE => GroupModel::TYPE_CHALLENGE];
185+
}
186+
$this->setData('Types', $typesData);
182187

183188
// Set the model on the form.
184189
$this->Form->setModel($this->GroupModel);
@@ -187,23 +192,32 @@ public function edit($groupID = false) {
187192
$this->Form->addHidden('GroupID', $groupID);
188193
$this->Form->addHidden('OwnerID', $Group->OwnerID);
189194

190-
// If seeing the form for the first time...
191-
if ($this->Form->authenticatedPostBack() === false) {
192-
// Get the group data for the requested $GroupID and put it into the form.
193-
$this->Form->setData($Group);
194-
} else {
195+
if ($this->Form->authenticatedPostBack(true)) { //The form was submitted
196+
if (!Gdn::session()->checkPermission(GroupsPlugin::GROUPS_GROUP_ADD_PERMISSION)) {
197+
$this->Form->addError('You do not have permission to add a group');
198+
}
195199

196-
// If the form has been posted back...
197-
$this->Form->formValues();
198-
$this->Form->saveImage('Icon');
199-
$this->Form->saveImage('Banner');
200-
if ($groupID = $this->Form->save()) {
201-
if ($this->deliveryType() === DELIVERY_TYPE_DATA) {
202-
$this->index($groupID);
203-
return;
200+
if ($this->Form->errorCount() == 0) {
201+
// If the form has been posted back...
202+
$isIconUploaded = $this->Form->saveImage('Icon');
203+
$isBannerUploaded = $this->Form->saveImage('Banner');
204+
$data = $this->Form->formValues();
205+
if ($groupID = $this->GroupModel->save($data)) {
206+
$this->Form->setValidationResults($this->GroupModel->validationResults());
207+
if($groupID) {
208+
$this->setRedirectTo('group/' . $groupID);
209+
}
210+
$this->View = 'Edit';
211+
} else {
212+
$this->Form->setValidationResults($this->GroupModel->validationResults());
204213
}
205-
$this->setRedirectTo('group/'.$groupID );
214+
215+
} else {
216+
$this->errorMessage($this->Form->errors());
206217
}
218+
} else {
219+
// Get the group data for the requested $GroupID and put it into the form.
220+
$this->Form->setData($Group);
207221
}
208222

209223
$this->render();
@@ -775,14 +789,20 @@ public function category($GroupID) {
775789
$this->setData('Group', $Group);
776790
$this->Form->setModel($this->CategoryModel);
777791
$slugify = new Slugify();
778-
$parentCategory = $Group->ChallengeID ? $this->CategoryModel->getByCode($Group->ChallengeID): -1;
792+
793+
$parentCategory = $this->GroupModel->getRootGroupCategory($Group);
779794
$this->Form->addHidden('ParentCategoryID', $parentCategory->CategoryID);
780795
$this->Form->addHidden('DisplayAs', 'Discussions');
781796
$this->Form->addHidden('AllowFileUploads',1);
782797
$this->Form->addHidden('UrlCode','');
783798
$this->Form->addHidden('GroupID',$GroupID);
784799
if ($this->Form->authenticatedPostBack(true)) {
785-
$this->Form->setFormValue('UrlCode', $Group->ChallengeID.'-'.$slugify->slugify($this->Form->getValue('Name'), '-'));
800+
if($Group->Type === GroupModel::TYPE_CHALLENGE) {
801+
$this->Form->setFormValue('UrlCode', $Group->ChallengeID . '-' . $slugify->slugify($this->Form->getValue('Name'), '-'));
802+
}
803+
//else {
804+
// $this->Form->setFormValue('UrlCode', 'group-'.$Group->GroupID.'-'.$slugify->slugify($this->Form->getValue('Name'), '-'));
805+
// }
786806
$newCategoryID = $this->Form->save();
787807
if(!$newCategoryID) {
788808
$this->errorMessage($this->Form->errors());

controllers/class.groupscontroller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function setFilterPageData($filter) {
4949
$this->View = 'index';
5050
$this->title('Challenges');
5151
$this->setData('Title', 'My Challenges');
52+
$this->setData('ShowAddButton', false);
5253
$this->setData('AddButtonTitle', 'Challenge');
5354
$this->setData('AddButtonLink', '/group/add?type=challenge');
5455
$this->setData('AvailableGroupTitle', 'Available Challenges');
@@ -63,6 +64,7 @@ public function setFilterPageData($filter) {
6364
$this->View = 'index';
6465
$this->title('Groups');
6566
$this->setData('Title', 'My Groups');
67+
$this->setData('ShowAddButton', true);
6668
$this->setData('AddButtonTitle', 'Group');
6769
$this->setData('AddButtonLink', '/group/add?type=regular');
6870
$this->setData('MyGroupButtonTitle', 'All My Groups');

models/class.groupmodel.php

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,19 +991,22 @@ public function save($formPostValues, $settings = false) {
991991

992992
$groupID = val('GroupID', $formPostValues);
993993
$ownerID = val('OwnerID', $formPostValues);
994+
$type = val('Type', $formPostValues);
995+
$name = val('Name', $formPostValues);
996+
$archived = val('Archived', $formPostValues);
994997
$insert = $groupID > 0 ? false : true;
995998

996999
if ($insert) {
9971000
// Figure out the next group ID.
9981001
$maxGroupID = $this->SQL->select('g.GroupID', 'MAX')->from('Group g')->get()->value('GroupID', 0);
9991002
$groupID = $maxGroupID + 1;
1000-
10011003
$this->addInsertFields($formPostValues);
10021004
$formPostValues['GroupID'] = strval($groupID); // string for validation
10031005
} else {
10041006
$this->addUpdateFields($formPostValues);
10051007
}
10061008

1009+
// TODO: Move creating Challenge group categories from challenge processor
10071010
// Validate the form posted values
10081011
if ($this->validate($formPostValues, $insert)) {
10091012
$fields = $this->Validation->schemaValidationFields();
@@ -1013,6 +1016,23 @@ public function save($formPostValues, $settings = false) {
10131016
$this->update($fields, ['GroupID' => $groupID]);
10141017
} else {
10151018
$this->insert($fields);
1019+
//Create a category for a regular group
1020+
if($type == GroupModel::TYPE_REGULAR) {
1021+
$categoryModel = new CategoryModel();
1022+
$parentCategory = $categoryModel->getByCode('groups');
1023+
$categoryData = [ 'Name' => $name,
1024+
'ParentCategoryID' => $parentCategory->CategoryID,
1025+
'DisplayAs' => 'Discussions',
1026+
'AllowFileUploads' => 1,
1027+
'UrlCode' => 'group-'.$groupID,
1028+
'GroupID' => $groupID,
1029+
'Archived' => $archived];
1030+
$categoryID = $categoryModel->save($categoryData);
1031+
// TODO
1032+
if(!$categoryID) {
1033+
// Error
1034+
}
1035+
}
10161036
$this->SQL->insert(
10171037
'UserGroup',
10181038
[
@@ -1053,8 +1073,28 @@ public function deleteID($groupID, $options = []) {
10531073
* @inheritdoc
10541074
*/
10551075
public function validate($values, $insert = false) {
1076+
$result = true;
1077+
// TODO: Move Challenge group validation from challenge processor
1078+
$type = val('Type', $values);
1079+
$urlCode = val('UrlCode', $values);
1080+
if($insert === true) {
1081+
$categoryModel = new CategoryModel();
1082+
if($type === GroupModel::TYPE_REGULAR) {
1083+
$category = $categoryModel->getByCode($urlCode);
1084+
if($category) {
1085+
$result = false;
1086+
$this->Validation->addValidationResult('UrlCode', 'Group UrlCode has existed.');
1087+
}
1088+
$parentCategory = $categoryModel->getByCode('groups');
1089+
if (!$parentCategory) {
1090+
$result = false;
1091+
$this->Validation->addValidationResult('ParentCategoryID', 'Groups category was not found.');
1092+
}
10561093

1057-
return parent::validate($values, $insert);
1094+
}
1095+
}
1096+
$result = $result && parent::validate($values, $insert);
1097+
return $result;
10581098
}
10591099

10601100
/**
@@ -1304,6 +1344,14 @@ public function canAddGroup() {
13041344
*
13051345
*/
13061346
public function canManageCategories($group) {
1347+
if((int)$group->Archived === 1) {
1348+
return false;
1349+
}
1350+
1351+
if($group->Type === GroupModel::TYPE_REGULAR) {
1352+
return false;
1353+
}
1354+
13071355
return $this->isProjectCopilot() || $this->isProjectManager() || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_CATEGORY_MANAGE_PERMISSION);
13081356
}
13091357

@@ -1329,6 +1377,9 @@ private function checkTopcoderProjectRole($topcoderProjectRole){
13291377
*
13301378
*/
13311379
public function canEdit($group) {
1380+
if((int)$group->Archived === 1) {
1381+
return false;
1382+
}
13321383
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
13331384
$groupRole = val('Role', $result, null);
13341385
if($groupRole == GroupModel::ROLE_LEADER ||
@@ -1347,6 +1398,9 @@ public function canEdit($group) {
13471398
*
13481399
*/
13491400
public function canDelete($group){
1401+
if((int)$group->Archived === 1) {
1402+
return false;
1403+
}
13501404
return Gdn::session()->UserID == $group->OwnerID || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_GROUP_DELETE_PERMISSION);
13511405
}
13521406

@@ -1355,6 +1409,9 @@ public function canDelete($group){
13551409
*
13561410
*/
13571411
public function canJoin($group) {
1412+
if((int)$group->Archived === 1) {
1413+
return false;
1414+
}
13581415
return $group->Privacy == GroupModel::PRIVACY_PUBLIC;
13591416
}
13601417

@@ -1363,6 +1420,9 @@ public function canJoin($group) {
13631420
*
13641421
*/
13651422
public function canRemoveMember($group) {
1423+
if((int)$group->Archived === 1) {
1424+
return false;
1425+
}
13661426
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
13671427
$groupRole = val('Role', $result, null);
13681428
if($groupRole == GroupModel::ROLE_LEADER ||
@@ -1377,6 +1437,9 @@ public function canRemoveMember($group) {
13771437
*
13781438
*/
13791439
public function canChangeGroupRole($group) {
1440+
if((int)$group->Archived === 1) {
1441+
return false;
1442+
}
13801443
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
13811444
$groupRole = val('Role', $result, null);
13821445
if($groupRole == GroupModel::ROLE_LEADER ||
@@ -1391,6 +1454,9 @@ public function canChangeGroupRole($group) {
13911454
*
13921455
*/
13931456
public function canLeave($group) {
1457+
if((int)$group->Archived === 1) {
1458+
return false;
1459+
}
13941460
if(isset($group->ChallengeID)) {
13951461
return false;
13961462
}
@@ -1409,6 +1475,9 @@ public function canLeave($group) {
14091475
*
14101476
*/
14111477
public function canManageMembers($group) {
1478+
if((int)$group->Archived === 1) {
1479+
return false;
1480+
}
14121481
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
14131482
$groupRole = val('Role', $result, null);
14141483
if($groupRole == GroupModel::ROLE_LEADER ||
@@ -1423,6 +1492,9 @@ public function canManageMembers($group) {
14231492
*
14241493
*/
14251494
public function canInviteNewMember($group) {
1495+
if((int)$group->Archived === 1) {
1496+
return false;
1497+
}
14261498
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
14271499
$groupRole = val('Role', $result, null);
14281500
if($groupRole === GroupModel::ROLE_LEADER ||
@@ -1438,6 +1510,9 @@ public function canInviteNewMember($group) {
14381510
*
14391511
*/
14401512
public function canAddDiscussion($group) {
1513+
if((int)$group->Archived === 1) {
1514+
return false;
1515+
}
14411516
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
14421517
$groupRole = val('Role', $result, null);
14431518
if($groupRole || Gdn::session()->UserID == $group->OwnerID) {
@@ -1451,6 +1526,9 @@ public function canAddDiscussion($group) {
14511526
*
14521527
*/
14531528
public function canAddAnnouncement($group) {
1529+
if((int)$group->Archived === 1) {
1530+
return false;
1531+
}
14541532
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
14551533
$groupRole = val('Role', $result, null);
14561534
if($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID == $group->OwnerID
@@ -1764,4 +1842,24 @@ public function archiveGroup($group){
17641842
$group->Archived = 1;
17651843
$this->save($group);
17661844
}
1845+
1846+
public function getGroupDiscussionCategories($group){
1847+
if(is_numeric($group) && $group > 0) {
1848+
$group = $this->getByGroupID($group);
1849+
}
1850+
$categoryModel = new CategoryModel();
1851+
return $categoryModel->getWhere(['GroupID' => $group->GroupID, 'DisplayAs' => 'Discussions'])->resultArray();
1852+
}
1853+
1854+
public function getRootGroupCategory($group){
1855+
if(is_numeric($group) && $group > 0) {
1856+
$group = $this->getByGroupID($group);
1857+
}
1858+
$categoryModel = new CategoryModel();
1859+
if($group->ChallengeID) {
1860+
return $categoryModel->getByCode($group->ChallengeID);
1861+
}
1862+
1863+
return -1; //return Vanilla root
1864+
}
17671865
}

views/group/edit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
echo '<div class="P">';
2929
echo $this->Form->label('Type', 'Type');
30-
echo $this->Form->dropDown('Type', $this->data('Types'), ['IncludeNull' => t('All Types')]);
30+
echo $this->Form->dropDown('Type', $this->data('Types'));
3131
echo '</div>';
3232

3333
echo '<div class="P">';

views/group/index.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
$groupModel = new GroupModel();
1919
$currentTopcoderProjectRoles = Gdn::controller()->data('ChallengeCurrentUserProjectRoles');
2020
$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles);
21-
21+
$discussionCategories = $groupModel->getGroupDiscussionCategories($Group);
2222
?>
2323
<?php echo writeGroupHeader($Group, true, $Owner, $Leaders, $TotalMembers);?>
2424

@@ -30,7 +30,13 @@
3030
<?php
3131

3232
if($groupModel->canAddAnnouncement($Group)) {
33-
echo anchor('New Announcement', '/group/announcement/' . $Group->GroupID, 'Button Primary', '');
33+
if(count($discussionCategories) > 0 && $Group->Type == GroupModel::TYPE_REGULAR) {
34+
// The group category is selected automatically
35+
$firstCategory = $discussionCategories[0];
36+
echo anchor('New Announcement', '/post/discussion/'.$firstCategory['UrlCode'], 'Button Primary', '');
37+
} else {
38+
echo anchor('New Announcement', '/post/discussion/', 'Button Primary', '');
39+
}
3440
}
3541
?>
3642
</div>
@@ -53,7 +59,13 @@
5359
<div class="Button-Controls">
5460
<?php
5561
if($groupModel->canAddDiscussion($Group)) {
56-
echo anchor('New Discussion', '/group/discussion/' . $Group->GroupID, 'Button Primary', '');
62+
// The group category is selected automatically
63+
if (count($discussionCategories) > 0 && $Group->Type == GroupModel::TYPE_REGULAR) {
64+
$firstCategory = $discussionCategories[0];
65+
echo anchor('New Discussion', '/post/discussion/' . $firstCategory['UrlCode'], 'Button Primary', '');
66+
} else {
67+
echo anchor('New Discussion', '/post/discussion/', 'Button Primary', '');
68+
}
5769
}
5870
?>
5971
</div>

views/groups/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
include_once $this->fetchViewLocation('helper_functions');
1010

11-
if($canAddGroup === true) {
11+
if($canAddGroup === true && $this->data('ShowAddButton') === true) {
1212
echo '<div class="groupToolbar"><a href="'.$this->data('AddButtonLink').'" class="Button Primary groupToolbar-newGroup">'. $this->data('AddButtonTitle').'</a></div>';
1313
}
1414

0 commit comments

Comments
 (0)