Skip to content

Commit bce6c43

Browse files
authored
Merge pull request #94 from topcoder-platform/issues-521
Issues-521: Added caching Challenge data(ChallengeID,IsNDA), Issues-524
2 parents bd4e4f4 + b25da62 commit bce6c43

File tree

2 files changed

+125
-9
lines changed

2 files changed

+125
-9
lines changed

Topcoder/class.topcoder.plugin.php

Lines changed: 122 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TopcoderPlugin extends Gdn_Plugin {
3333
const CACHE_KEY_TOPCODER_PROFILE = 'topcoder.{UserID}';
3434
const CACHE_TOPCODER_KEY_TOPCODER_PROFILE = 'topcoder.{Handle}';
3535
const CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES = 'topcoder.roleresources';
36+
const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE = 'topcoder.challenge.{ChallengeID}';
3637
const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES = 'topcoder.challenge.{ChallengeID}.resources';
3738

3839
const CACHE_DEFAULT_EXPIRY_TIME = 60*60*3; //The default expiration time in Memcached is in seconds, 10800 = 3 hours
@@ -1446,11 +1447,11 @@ public function getChallengeResources($challengeId) {
14461447
}
14471448

14481449
$expirationTime = self::CACHE_DEFAULT_EXPIRY_TIME;
1449-
$challenge = self::loadChallenge($challengeId);
1450-
if($challenge && count($challenge) > 0) {
1450+
$challenge = self::getChallenge($challengeId);
1451+
if($challenge) {
14511452
// Set expiration time for Challenge roles
1452-
$endDate = strtotime($challenge[0]->endDate);
1453-
$startDate = strtotime($challenge[0]->startDate);
1453+
$endDate = $challenge['EndDate'];
1454+
$startDate =$challenge['StartDate'];
14541455
// $duration = $endDate > -1 && $startDate > -1 ? $endDate - $startDate: 0;
14551456
// archived
14561457
$isEnded = $endDate > -1 && now() - $endDate > 0;
@@ -1520,6 +1521,43 @@ private static function loadChallengeResources($challengeId) {
15201521
return null;
15211522
}
15221523

1524+
/**
1525+
* Get Topcoder Challenge by ChallengeId
1526+
* @param $challengeId
1527+
* @return mixed|null
1528+
*/
1529+
public function getChallenge($challengeId) {
1530+
$challenge = self::getChallengeFromCache($challengeId);
1531+
if ($challenge) {
1532+
return $challenge;
1533+
}
1534+
1535+
$cachedChallenge = ['ChallengeID' => $challengeId];
1536+
$challenge = self::loadChallenge($challengeId);
1537+
1538+
$expirationTime = self::CACHE_DEFAULT_EXPIRY_TIME;
1539+
if($challenge) {
1540+
// Set expiration time for Challenge roles
1541+
$startDate = strtotime($challenge->startDate);
1542+
$endDate = strtotime($challenge->endDate);
1543+
// archived
1544+
$isEnded = $endDate > -1 && now() - $endDate > 0;
1545+
if(!$isEnded) {
1546+
$expirationTime = self::CACHE_ONE_DAY_EXPIRY_TIME;
1547+
}
1548+
$cachedChallenge['StartDate'] = $startDate;
1549+
$cachedChallenge['EndDate'] = $endDate;
1550+
$termIDs = array_column($challenge->terms, 'id');
1551+
$NDA_UUID = c('Plugins.Topcoder.NDA_UUID');
1552+
$cachedChallenge['IsNDA'] = in_array($NDA_UUID, $termIDs);
1553+
}
1554+
if (Gdn_Cache::activeEnabled()) {
1555+
self::topcoderChallengeCache($challengeId, $cachedChallenge, $expirationTime);
1556+
}
1557+
return $cachedChallenge;
1558+
}
1559+
1560+
15231561
/**
15241562
* Load Topcoder Challenge by Challenge ID
15251563
* @param $challengeId
@@ -1535,7 +1573,7 @@ private static function loadChallenge($challengeId) {
15351573
'header' => 'Authorization: Bearer ' .$token
15361574
));
15371575
$context = stream_context_create($options);
1538-
$data = file_get_contents($topcoderChallengeApiUrl . '?challengeId=' . $challengeId, false, $context);
1576+
$data = file_get_contents($topcoderChallengeApiUrl . $challengeId, false, $context);
15391577
if ($data === false) {
15401578
// Handle errors (e.g. 404 and others)
15411579
self::log('Couldn\'t get challenge: no token', ['headers'=> json_encode($http_response_header)]);
@@ -1549,6 +1587,35 @@ private static function loadChallenge($challengeId) {
15491587
return null;
15501588
}
15511589

1590+
/**
1591+
* Load challenge from cache
1592+
* @param $challengeID
1593+
* @return false|mixed
1594+
*/
1595+
private static function getChallengeFromCache($challengeID) {
1596+
if(!Gdn_Cache::activeEnabled()) {
1597+
return false;
1598+
}
1599+
1600+
$handleKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE, ['ChallengeID' => $challengeID]);
1601+
if(!Gdn::cache()->exists($handleKey)) {
1602+
return false;
1603+
}
1604+
$challenge = Gdn::cache()->get($handleKey);
1605+
if ($challenge === Gdn_Cache::CACHEOP_FAILURE) {
1606+
return false;
1607+
}
1608+
return $challenge;
1609+
}
1610+
1611+
private static function topcoderChallengeCache($challengeID, $challenge, $expirationTime = self::CACHE_DEFAULT_EXPIRY_TIME) {
1612+
$challengeKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE, ['ChallengeID' => $challengeID]);
1613+
return Gdn::cache()->store($challengeKey , $challenge, [
1614+
Gdn_Cache::FEATURE_EXPIRY => $expirationTime
1615+
]);
1616+
}
1617+
1618+
15521619
/**
15531620
* Get a Topcoder Roles
15541621
*
@@ -1761,13 +1828,15 @@ public static function getUserPhotoUrl($user) {
17611828
// Set Topcoder Project Roles Data for a challenge
17621829
private function setTopcoderProjectData($sender, $challengeID) {
17631830
if($challengeID) {
1831+
$challenge = $this->getChallenge($challengeID);
17641832
$resources = $this->getChallengeResources($challengeID);
17651833
$roleResources = $this->getRoleResources();
17661834
$currentProjectRoles = $this->getTopcoderProjectRoles(Gdn::session()->User, $resources, $roleResources);
17671835
if($currentProjectRoles) {
17681836
$currentProjectRoles = array_map('strtolower',$currentProjectRoles);
17691837
}
17701838

1839+
$sender->Data['Challenge'] = $challenge;
17711840
$sender->Data['ChallengeResources'] = $resources;
17721841
$sender->Data['ChallengeRoleResources'] = $roleResources;
17731842
$sender->Data['ChallengeCurrentUserProjectRoles'] = $currentProjectRoles;
@@ -1777,7 +1846,7 @@ private function setTopcoderProjectData($sender, $challengeID) {
17771846
// }
17781847
self::log('setTopcoderProjectData', ['ChallengeID' => $challengeID, 'currentUser' => $currentProjectRoles,
17791848
'Topcoder Resources' => $resources , 'Topcoder RoleResources'
1780-
=> $roleResources,]);
1849+
=> $roleResources, 'challenge' =>$challenge]);
17811850
}
17821851
}
17831852

@@ -2483,3 +2552,50 @@ function topcoderMentionAnchor($mention, $cssClass = null, $options = null) {
24832552
}
24842553
}
24852554

2555+
if (!function_exists('watchingSorts')) {
2556+
/**
2557+
* Returns watching sorting.
2558+
*
2559+
* @param string $extraClasses any extra classes you add to the drop down
2560+
* @return string
2561+
*/
2562+
function watchingSorts($extraClasses = '') {
2563+
if (!Gdn::session()->isValid()) {
2564+
return;
2565+
}
2566+
2567+
$baseUrl = preg_replace('/\?.*/', '', Gdn::request()->getFullPath());
2568+
$transientKey = Gdn::session()->transientKey();
2569+
$filters = [
2570+
[
2571+
'name' => t('New'),
2572+
'param' => 'sort',
2573+
'value' => 'new',
2574+
'extra' => ['TransientKey' => $transientKey, 'save' => 1]
2575+
],
2576+
2577+
[
2578+
'name' => t('Old'),
2579+
'param' => 'sort',
2580+
'value' => 'old',
2581+
'extra' => ['TransientKey' => $transientKey, 'save' => 1]
2582+
]
2583+
];
2584+
2585+
$defaultParams = [];
2586+
if (!empty($defaultParams)) {
2587+
$defaultUrl = $baseUrl.'?'.http_build_query($defaultParams);
2588+
} else {
2589+
$defaultUrl = $baseUrl;
2590+
}
2591+
2592+
return sortsDropDown('WatchingSort',
2593+
$baseUrl,
2594+
$filters,
2595+
$extraClasses,
2596+
null,
2597+
$defaultUrl,
2598+
'Sort'
2599+
);
2600+
}
2601+
}

Topcoder/controllers/class.watchingcontroller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public function index($cp = '', $dp = '') {
5656
$sort = Gdn::request()->get('sort', null);
5757
$saveSorting = $sort !== null && Gdn::request()->get('save') && Gdn::session()->validateTransientKey(Gdn::request()->get('TransientKey', ''));
5858
if($saveSorting) {
59-
Gdn::session()->setPreference('CategorySort', $sort);
59+
Gdn::session()->setPreference('WatchingSort', $sort);
6060
}
61-
$sort = Gdn::session()->getPreference('CategorySort', false);
62-
$this->setData('CategorySort', $sort);
61+
$sort = Gdn::session()->getPreference('WatchingSort', false);
62+
$this->setData('WatchingSort', $sort);
6363

6464
$userMetaModel = new UserMetaModel();
6565
list($cp, $categoryLimit) = offsetLimit($cp, 30);

0 commit comments

Comments
 (0)