Skip to content

Version 1.7 #90

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 16 commits into from
Apr 30, 2021
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
173 changes: 167 additions & 6 deletions class.groups.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ public function categoryModel_watch_create(CategoryModel $sender){
public function categoryModel_setCategoryMetaData_create(CategoryModel $sender) {
$categoryIDs = val(0, $sender->EventArguments);
$userID = val(1, $sender->EventArguments);
$watched = val(2, $sender->EventArguments);
$watched = val(2, $sender->EventArguments); // 1, 2- partly watched, null- remove
$userMetaModel = new UserMetaModel();
if(is_numeric($categoryIDs) ) {
$categoryIDs = [$categoryIDs];
Expand All @@ -552,7 +552,13 @@ public function categoryModel_setCategoryMetaData_create(CategoryModel $sender)
$userMetaModel = new UserMetaModel();
$userMetaModel->setWatchedCategoriesCount($userID);
Gdn::cache()->remove("UserMeta_{$userID}");
return;// $sender->hasWatched($categoryIDs,$userID);

$discussionModel = new DiscussionModel();
// Don't change bookmark flag in UserDiscussion
if($watched != 2) {
$discussionModel->bookmarkAll($categoryIDs, $userID, $watched == 1 ? 1 : 0);
}
return;
}

/**
Expand Down Expand Up @@ -601,6 +607,43 @@ public function categoryModel_hasWatched_create(CategoryModel $sender) {
return false;
}

/**
* Get user notification preferences
*
* @param $userID
* @param $categoryIDs array|int
* @return array The list of categories with all preference keys.
*/
public function categoryModel_getCategoryNotificationPreferences_create(CategoryModel $sender) {
$categoryIDs = val(0, $sender->EventArguments);
$userID = val(1, $sender->EventArguments);

if(is_numeric($categoryIDs) ) {
$categoryIDs = [$categoryIDs];
}
$result = [];
$userMetaModel = new UserMetaModel();
foreach ($categoryIDs as $categoryID) {
// need to cast to int, by default all values are strings
$newEmailDiscussionKey = 'Preferences.Email.NewDiscussion.' . $categoryID;
$newEmailDiscussionValue = $userMetaModel->getUserMeta($userID, $newEmailDiscussionKey, null);
$result[$categoryID][$newEmailDiscussionKey] = $newEmailDiscussionValue[$newEmailDiscussionKey];

$newEmailCommentKey = 'Preferences.Email.NewComment.' . $categoryID;
$newEmailCommentValue = $userMetaModel->getUserMeta($userID, $newEmailCommentKey, null);
$result[$categoryID][$newEmailCommentKey] = $newEmailCommentValue[$newEmailCommentKey];

$newPopupDiscussionKey = 'Preferences.Popup.NewDiscussion.' . $categoryID;
$newPopupDiscussionValue = $userMetaModel->getUserMeta($userID, $newPopupDiscussionKey, null);
$result[$categoryID][$newPopupDiscussionKey] = $newPopupDiscussionValue[$newPopupDiscussionKey];

$newPopupCommentKey = 'Preferences.Popup.NewComment.' . $categoryID;
$newPopupCommentValue = $userMetaModel->getUserMeta($userID, $newPopupCommentKey, null);
$result[$categoryID][$newPopupCommentKey] = $newPopupCommentValue[$newPopupCommentKey];
}
return $result;
}

//
// EMAIL TEMPLATES
//
Expand Down Expand Up @@ -892,8 +935,9 @@ public function base_beforeRenderDiscussionFilters_handler($sender, $args){
if ($sender instanceof CategoriesController || $sender instanceof PostController) {
$category = $sender->data('Category');
if($category) {
$groupID = val('GroupID', $category);
if($groupID) {
$categoryID = val('CategoryID', $category);
$isChallengeForums = $sender->checkChallengeForums($categoryID);
if($isChallengeForums) {
$menu['AllCategories']['IsActive'] = false;
return;
}
Expand Down Expand Up @@ -923,8 +967,9 @@ private function getMenuItemCssClassFromQuery($sender, $requestMethod){
if ($sender instanceof CategoriesController || $sender instanceof PostController) {
$category = $sender->data('Category');
if($category) {
$groupID = val('GroupID', $category);
if($groupID) {
$categoryID = val('CategoryID', $category);
$isChallengeForums = $sender->checkChallengeForums($categoryID);
if($isChallengeForums) {
$cssClass = ' Active';
}
}
Expand Down Expand Up @@ -993,4 +1038,120 @@ function wrapCheckOrRadio($fieldName, $labelCode, $listOptions, $attributes = []
return $result;
}
}
}

if (!function_exists('groupSorts')) {
/**
* Returns group sorting.
*
* @param string $extraClasses any extra classes you add to the drop down
* @return string
*/
function groupSorts($extraClasses = '') {
if (!Gdn::session()->isValid()) {
return;
}

$baseUrl = preg_replace('/\?.*/', '', Gdn::request()->getFullPath());
$transientKey = Gdn::session()->transientKey();
$filters = [
[
'name' => t('New'),
'param' => 'sort',
'value' => 'new',
'extra' => ['TransientKey' => $transientKey, 'save' => 1, 'filter'=>'challenge']
],

[
'name' => t('Old'),
'param' => 'sort',
'value' => 'old',
'extra' => ['TransientKey' => $transientKey, 'save' => 1, 'filter'=>'challenge']
]
];

$defaultParams = [];
if (!empty($defaultParams)) {
$defaultUrl = $baseUrl.'?'.http_build_query($defaultParams);
} else {
$defaultUrl = $baseUrl;
}

return groupSortsDropDown(
$baseUrl,
$filters,
$extraClasses,
null,
$defaultUrl,
'Sort'
);
}
}

if (!function_exists('groupSortsDropDown')) {
/**
* Returns a sorting drop-down menu.
*
* @param string $baseUrl Target URL with no query string applied.
* @param array $filters A multidimensional array of rows with the following properties:
* ** 'name': Friendly name for the filter.
* ** 'param': URL parameter associated with the filter.
* ** 'value': A value for the URL parameter.
* @param string $extraClasses any extra classes you add to the drop down
* @param string|null $default The default label for when no filter is active. If `null`, the default label is not added
* @param string|null $defaultURL URL override to return to the default, unfiltered state.
* @param string $label Text for the label to attach to the cont
* @return string
*/
function groupSortsDropDown($baseUrl, array $filters = [], $extraClasses = '', $default = null, $defaultUrl = null, $label = 'Sort') {
$links = [];
$active = Gdn::session()->getPreference('GroupSort', null);
// Translate filters into links.
foreach ($filters as $filter) {
// Make sure we have the bare minimum: a label and a URL parameter.
if (!array_key_exists('name', $filter)) {
throw new InvalidArgumentException('Sort does not have a name field.');
}
if (!array_key_exists('param', $filter)) {
throw new InvalidArgumentException('Sort does not have a param field.');
}

// Prepare for consumption by linkDropDown.
$query = [$filter['param'] => $filter['value']];
if (array_key_exists('extra', $filter) && is_array($filter['extra'])) {
$query += $filter['extra'];
}
$url = url($baseUrl . '?' . http_build_query($query));
$link = [
'name' => $filter['name'],
'url' => $url
];

// If we don't already have an active link, and this parameter and value match, this is the active link.
if ($active === null && Gdn::request()->get($filter['param']) == $filter['value']) {
$active = $filter['value'];
$link['active'] = true;
} else if ($active == $filter['value']){
$link['active'] = true;
$active = $filter['value'];
}

// Queue up another filter link.
$links[] = $link;
}

if ($default !== null) {
$default = t('All');
// Add the default link to the top of the list.
array_unshift($links, [
'active' => $active === null,
'name' => $default,
'url' => $defaultUrl ?: $baseUrl
]);
}

// Generate the markup for the drop down menu.
$output = linkDropDown($links, 'selectBox-following ' . trim($extraClasses), t($label) . ': ');
return $output;
}
}
69 changes: 60 additions & 9 deletions controllers/class.groupcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,47 +86,98 @@ public function index($GroupID = '') {
$this->setData('Group', $Group, true);

$this->title($Group->Name);
$category = $this->CategoryModel->getByCode($Group->ChallengeID);
$categoryID= val('CategoryID', $category);
$this->setData('GroupCategoryID', $categoryID);
$this->setData('Breadcrumbs', $this->buildBreadcrumbs($categoryID));

$this->setData('Breadcrumbs', $this->buildBreadcrumb($Group));
$roleResources = $this->data('ChallengeRoleResources');
$resources = $this->data('ChallengeResources');
$copilots = $this->getCopilots($resources, $roleResources);
$this->setData('Copilots', $copilots);
$groupDiscussions = $this->GroupModel->getGroupDiscussionCategories($Group);
$defaultDiscussionUrl = '/post/discussion/';
$defaultDiscussionUrl= $defaultAnnouncementUrl = '/post/discussion/';
if($Group->Type == GroupModel::TYPE_REGULAR) {
if(count($groupDiscussions) > 0) {
$defaultDiscussionUrl .= $groupDiscussions[0]['UrlCode'];
}
} else if($Group->Type == GroupModel::TYPE_CHALLENGE) {
if(count($groupDiscussions) == 1) {
$defaultDiscussionUrl .= $groupDiscussions[0]['UrlCode'];
$defaultAnnouncementUrl .= $groupDiscussions[0]['UrlCode'];
} else {
foreach ($groupDiscussions as $groupDiscussion) {
if ($groupDiscussion['Name'] == 'Code Questions') {
$defaultDiscussionUrl .= $groupDiscussion['UrlCode'];
$defaultAnnouncementUrl .= $groupDiscussion['UrlCode'];
break;
}
}
}
}

$this->setData('DefaultAnnouncementUrl', $defaultDiscussionUrl.'?announce=1');
$CategoryIDs = $this->GroupModel->getAllGroupCategoryIDs($Group->GroupID);
$Categories = $this->CategoryModel->getFull($CategoryIDs)->resultArray();
$this->setData('Categories', $Categories);

// Get category data and discussions

$this->DiscussionsPerCategory = c('Vanilla.Discussions.PerCategory', 5);
$DiscussionModel = new DiscussionModel();
$DiscussionModel->setSort(Gdn::request()->get());
$DiscussionModel->setFilters(Gdn::request()->get());
$this->setData('Sort', $DiscussionModel->getSort());
$this->setData('Filters', $DiscussionModel->getFilters());

$this->CategoryDiscussionData = [];
$Discussions = [];

foreach ($this->CategoryData->result() as $Category) {
if ($Category->CategoryID > 0) {
$this->CategoryDiscussionData[$Category->CategoryID] = $DiscussionModel->get(0, $this->DiscussionsPerCategory,
['d.CategoryID' => $Category->CategoryID, 'Announce' => '0']);
$Discussions = array_merge(
$Discussions,
$this->CategoryDiscussionData[$Category->CategoryID]->resultObject()
);
}
}
$this->setData('Discussions', $Discussions);

// render dropdown options
$this->ShowOptions = true;

$this->setData('DefaultAnnouncementUrl', $defaultAnnouncementUrl.'?announce=1');
$this->setData('DefaultDiscussionUrl', $defaultDiscussionUrl);

// Find all discussions with content from after DateMarkedRead.
$discussionModel = new DiscussionModel();
$categoryIDs = $this->GroupModel->getAllGroupCategoryIDs($Group->GroupID);
$wheres = ['d.CategoryID' => $categoryIDs];
$announcementsWheres = ['d.CategoryID' => $categoryIDs, 'd.Announce > '=> 0];
//Don't use WhereRecent due to load all data including announce.
$discussions = $discussionModel->getWhere($wheres,'DateInserted', 'asc');
$announcements = $discussionModel->getAnnouncements($announcementsWheres );
$this->setData('Announcements', $announcements);
$this->setData('Discussions', $discussions);

$Path = $this->fetchViewLocation('helper_functions', 'discussions', 'vanilla', false);
if ($Path) {
include_once $Path;
}

// For GetOptions function
$Path2 = $this->fetchViewLocation('helper_functions', 'categories', 'vanilla', false);
if ($Path2) {
include_once $Path2;
}
$this->render();
}

public function __get($name) {
switch ($name) {
case 'CategoryData':
// deprecated('CategoriesController->CategoryData', "CategoriesController->data('Categories')");
$this->CategoryData = new Gdn_DataSet($this->data('Categories'), DATASET_TYPE_ARRAY);
$this->CategoryData->datasetType(DATASET_TYPE_OBJECT);
return $this->CategoryData;
}
}

private function getCopilots($resources = null, $roleResources = null) {
$copilots = [];
$roleName = 'copilot';
Expand Down
Loading