Skip to content

Issues-218 #33

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

Merged
merged 2 commits into from
Nov 29, 2020
Merged
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
60 changes: 40 additions & 20 deletions controllers/class.groupcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,25 @@ public function edit($groupID = false) {
$this->setData('Breadcrumbs', $this->buildBreadcrumb($Group));

} else {
$Group->Type = Gdn::request()->get('type');
$Group->Type = GroupModel::TYPE_REGULAR;
$Group->OwnerID = Gdn::session()->UserID;
$Group->LeaderID = Gdn::session()->UserID;
}

$typesData = [GroupModel::TYPE_REGULAR => GroupModel::TYPE_REGULAR, GroupModel::TYPE_CHALLENGE => GroupModel::TYPE_CHALLENGE];
$this->setData('Types', $typesData);

// Set Privacy Types
$type = GroupsPlugin::UI[$Group->Type]['TypeName'];
$privacyTypes = [GroupModel::PRIVACY_PUBLIC => sprintf('Public. Anyone can see the %s and its content. Anyone can join.', $type),
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),
GroupModel::PRIVACY_SECRET => sprintf('Secret. Only members can see the %s and view its content. People must be invited to join.', $type)];
$this->setData('PrivacyTypes', $privacyTypes);

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

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

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

// If the form has been posted back...
$this->Form->formValues();
$this->Form->saveImage('Icon');
$this->Form->saveImage('Banner');
if ($groupID = $this->Form->save()) {
if ($this->deliveryType() === DELIVERY_TYPE_DATA) {
$this->index($groupID);
return;
if ($this->Form->errorCount() == 0) {
// If the form has been posted back...
$isIconUploaded = $this->Form->saveImage('Icon');
$isBannerUploaded = $this->Form->saveImage('Banner');
$data = $this->Form->formValues();
if ($groupID = $this->GroupModel->save($data)) {
$this->Form->setValidationResults($this->GroupModel->validationResults());
if($groupID) {
$this->setRedirectTo('group/' . $groupID);
}
$this->View = 'Edit';
} else {
$this->Form->setValidationResults($this->GroupModel->validationResults());
}
$this->setRedirectTo('group/'.$groupID );

} else {
$this->errorMessage($this->Form->errors());
}
} else {
// Get the group data for the requested $GroupID and put it into the form.
$this->Form->setData($Group);
}

$this->render();
Expand Down Expand Up @@ -775,14 +789,20 @@ public function category($GroupID) {
$this->setData('Group', $Group);
$this->Form->setModel($this->CategoryModel);
$slugify = new Slugify();
$parentCategory = $Group->ChallengeID ? $this->CategoryModel->getByCode($Group->ChallengeID): -1;

$parentCategory = $this->GroupModel->getRootGroupCategory($Group);
$this->Form->addHidden('ParentCategoryID', $parentCategory->CategoryID);
$this->Form->addHidden('DisplayAs', 'Discussions');
$this->Form->addHidden('AllowFileUploads',1);
$this->Form->addHidden('UrlCode','');
$this->Form->addHidden('GroupID',$GroupID);
if ($this->Form->authenticatedPostBack(true)) {
$this->Form->setFormValue('UrlCode', $Group->ChallengeID.'-'.$slugify->slugify($this->Form->getValue('Name'), '-'));
if($Group->Type === GroupModel::TYPE_CHALLENGE) {
$this->Form->setFormValue('UrlCode', $Group->ChallengeID . '-' . $slugify->slugify($this->Form->getValue('Name'), '-'));
}
//else {
// $this->Form->setFormValue('UrlCode', 'group-'.$Group->GroupID.'-'.$slugify->slugify($this->Form->getValue('Name'), '-'));
// }
$newCategoryID = $this->Form->save();
if(!$newCategoryID) {
$this->errorMessage($this->Form->errors());
Expand Down
2 changes: 2 additions & 0 deletions controllers/class.groupscontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function setFilterPageData($filter) {
$this->View = 'index';
$this->title('Challenges');
$this->setData('Title', 'My Challenges');
$this->setData('ShowAddButton', false);
$this->setData('AddButtonTitle', 'Challenge');
$this->setData('AddButtonLink', '/group/add?type=challenge');
$this->setData('AvailableGroupTitle', 'Available Challenges');
Expand All @@ -63,6 +64,7 @@ public function setFilterPageData($filter) {
$this->View = 'index';
$this->title('Groups');
$this->setData('Title', 'My Groups');
$this->setData('ShowAddButton', true);
$this->setData('AddButtonTitle', 'Group');
$this->setData('AddButtonLink', '/group/add?type=regular');
$this->setData('MyGroupButtonTitle', 'All My Groups');
Expand Down
102 changes: 100 additions & 2 deletions models/class.groupmodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,19 +991,22 @@ public function save($formPostValues, $settings = false) {

$groupID = val('GroupID', $formPostValues);
$ownerID = val('OwnerID', $formPostValues);
$type = val('Type', $formPostValues);
$name = val('Name', $formPostValues);
$archived = val('Archived', $formPostValues);
$insert = $groupID > 0 ? false : true;

if ($insert) {
// Figure out the next group ID.
$maxGroupID = $this->SQL->select('g.GroupID', 'MAX')->from('Group g')->get()->value('GroupID', 0);
$groupID = $maxGroupID + 1;

$this->addInsertFields($formPostValues);
$formPostValues['GroupID'] = strval($groupID); // string for validation
} else {
$this->addUpdateFields($formPostValues);
}

// TODO: Move creating Challenge group categories from challenge processor
// Validate the form posted values
if ($this->validate($formPostValues, $insert)) {
$fields = $this->Validation->schemaValidationFields();
Expand All @@ -1013,6 +1016,23 @@ public function save($formPostValues, $settings = false) {
$this->update($fields, ['GroupID' => $groupID]);
} else {
$this->insert($fields);
//Create a category for a regular group
if($type == GroupModel::TYPE_REGULAR) {
$categoryModel = new CategoryModel();
$parentCategory = $categoryModel->getByCode('groups');
$categoryData = [ 'Name' => $name,
'ParentCategoryID' => $parentCategory->CategoryID,
'DisplayAs' => 'Discussions',
'AllowFileUploads' => 1,
'UrlCode' => 'group-'.$groupID,
'GroupID' => $groupID,
'Archived' => $archived];
$categoryID = $categoryModel->save($categoryData);
// TODO
if(!$categoryID) {
// Error
}
}
$this->SQL->insert(
'UserGroup',
[
Expand Down Expand Up @@ -1053,8 +1073,28 @@ public function deleteID($groupID, $options = []) {
* @inheritdoc
*/
public function validate($values, $insert = false) {
$result = true;
// TODO: Move Challenge group validation from challenge processor
$type = val('Type', $values);
$urlCode = val('UrlCode', $values);
if($insert === true) {
$categoryModel = new CategoryModel();
if($type === GroupModel::TYPE_REGULAR) {
$category = $categoryModel->getByCode($urlCode);
if($category) {
$result = false;
$this->Validation->addValidationResult('UrlCode', 'Group UrlCode has existed.');
}
$parentCategory = $categoryModel->getByCode('groups');
if (!$parentCategory) {
$result = false;
$this->Validation->addValidationResult('ParentCategoryID', 'Groups category was not found.');
}

return parent::validate($values, $insert);
}
}
$result = $result && parent::validate($values, $insert);
return $result;
}

/**
Expand Down Expand Up @@ -1304,6 +1344,14 @@ public function canAddGroup() {
*
*/
public function canManageCategories($group) {
if((int)$group->Archived === 1) {
return false;
}

if($group->Type === GroupModel::TYPE_REGULAR) {
return false;
}

return $this->isProjectCopilot() || $this->isProjectManager() || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_CATEGORY_MANAGE_PERMISSION);
}

Expand All @@ -1329,6 +1377,9 @@ private function checkTopcoderProjectRole($topcoderProjectRole){
*
*/
public function canEdit($group) {
if((int)$group->Archived === 1) {
return false;
}
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
$groupRole = val('Role', $result, null);
if($groupRole == GroupModel::ROLE_LEADER ||
Expand All @@ -1347,6 +1398,9 @@ public function canEdit($group) {
*
*/
public function canDelete($group){
if((int)$group->Archived === 1) {
return false;
}
return Gdn::session()->UserID == $group->OwnerID || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_GROUP_DELETE_PERMISSION);
}

Expand All @@ -1355,6 +1409,9 @@ public function canDelete($group){
*
*/
public function canJoin($group) {
if((int)$group->Archived === 1) {
return false;
}
return $group->Privacy == GroupModel::PRIVACY_PUBLIC;
}

Expand All @@ -1363,6 +1420,9 @@ public function canJoin($group) {
*
*/
public function canRemoveMember($group) {
if((int)$group->Archived === 1) {
return false;
}
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
$groupRole = val('Role', $result, null);
if($groupRole == GroupModel::ROLE_LEADER ||
Expand All @@ -1377,6 +1437,9 @@ public function canRemoveMember($group) {
*
*/
public function canChangeGroupRole($group) {
if((int)$group->Archived === 1) {
return false;
}
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
$groupRole = val('Role', $result, null);
if($groupRole == GroupModel::ROLE_LEADER ||
Expand All @@ -1391,6 +1454,9 @@ public function canChangeGroupRole($group) {
*
*/
public function canLeave($group) {
if((int)$group->Archived === 1) {
return false;
}
if(isset($group->ChallengeID)) {
return false;
}
Expand All @@ -1409,6 +1475,9 @@ public function canLeave($group) {
*
*/
public function canManageMembers($group) {
if((int)$group->Archived === 1) {
return false;
}
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
$groupRole = val('Role', $result, null);
if($groupRole == GroupModel::ROLE_LEADER ||
Expand All @@ -1423,6 +1492,9 @@ public function canManageMembers($group) {
*
*/
public function canInviteNewMember($group) {
if((int)$group->Archived === 1) {
return false;
}
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
$groupRole = val('Role', $result, null);
if($groupRole === GroupModel::ROLE_LEADER ||
Expand All @@ -1438,6 +1510,9 @@ public function canInviteNewMember($group) {
*
*/
public function canAddDiscussion($group) {
if((int)$group->Archived === 1) {
return false;
}
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
$groupRole = val('Role', $result, null);
if($groupRole || Gdn::session()->UserID == $group->OwnerID) {
Expand All @@ -1451,6 +1526,9 @@ public function canAddDiscussion($group) {
*
*/
public function canAddAnnouncement($group) {
if((int)$group->Archived === 1) {
return false;
}
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
$groupRole = val('Role', $result, null);
if($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID == $group->OwnerID
Expand Down Expand Up @@ -1764,4 +1842,24 @@ public function archiveGroup($group){
$group->Archived = 1;
$this->save($group);
}

public function getGroupDiscussionCategories($group){
if(is_numeric($group) && $group > 0) {
$group = $this->getByGroupID($group);
}
$categoryModel = new CategoryModel();
return $categoryModel->getWhere(['GroupID' => $group->GroupID, 'DisplayAs' => 'Discussions'])->resultArray();
}

public function getRootGroupCategory($group){
if(is_numeric($group) && $group > 0) {
$group = $this->getByGroupID($group);
}
$categoryModel = new CategoryModel();
if($group->ChallengeID) {
return $categoryModel->getByCode($group->ChallengeID);
}

return -1; //return Vanilla root
}
}
2 changes: 1 addition & 1 deletion views/group/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

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

echo '<div class="P">';
Expand Down
18 changes: 15 additions & 3 deletions views/group/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$groupModel = new GroupModel();
$currentTopcoderProjectRoles = Gdn::controller()->data('ChallengeCurrentUserProjectRoles');
$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles);

$discussionCategories = $groupModel->getGroupDiscussionCategories($Group);
?>
<?php echo writeGroupHeader($Group, true, $Owner, $Leaders, $TotalMembers);?>

Expand All @@ -30,7 +30,13 @@
<?php

if($groupModel->canAddAnnouncement($Group)) {
echo anchor('New Announcement', '/group/announcement/' . $Group->GroupID, 'Button Primary', '');
if(count($discussionCategories) > 0 && $Group->Type == GroupModel::TYPE_REGULAR) {
// The group category is selected automatically
$firstCategory = $discussionCategories[0];
echo anchor('New Announcement', '/post/discussion/'.$firstCategory['UrlCode'], 'Button Primary', '');
} else {
echo anchor('New Announcement', '/post/discussion/', 'Button Primary', '');
}
}
?>
</div>
Expand All @@ -53,7 +59,13 @@
<div class="Button-Controls">
<?php
if($groupModel->canAddDiscussion($Group)) {
echo anchor('New Discussion', '/group/discussion/' . $Group->GroupID, 'Button Primary', '');
// The group category is selected automatically
if (count($discussionCategories) > 0 && $Group->Type == GroupModel::TYPE_REGULAR) {
$firstCategory = $discussionCategories[0];
echo anchor('New Discussion', '/post/discussion/' . $firstCategory['UrlCode'], 'Button Primary', '');
} else {
echo anchor('New Discussion', '/post/discussion/', 'Button Primary', '');
}
}
?>
</div>
Expand Down
2 changes: 1 addition & 1 deletion views/groups/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

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

Expand Down