@@ -33,6 +33,7 @@ class TopcoderPlugin extends Gdn_Plugin {
33
33
const CACHE_KEY_TOPCODER_PROFILE = 'topcoder.{UserID} ' ;
34
34
const CACHE_TOPCODER_KEY_TOPCODER_PROFILE = 'topcoder.{Handle} ' ;
35
35
const CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES = 'topcoder.roleresources ' ;
36
+ const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE = 'topcoder.challenge.{ChallengeID} ' ;
36
37
const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES = 'topcoder.challenge.{ChallengeID}.resources ' ;
37
38
38
39
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) {
1446
1447
}
1447
1448
1448
1449
$ 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 ) {
1451
1452
// 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 ' ] ;
1454
1455
// $duration = $endDate > -1 && $startDate > -1 ? $endDate - $startDate: 0;
1455
1456
// archived
1456
1457
$ isEnded = $ endDate > -1 && now () - $ endDate > 0 ;
@@ -1520,6 +1521,43 @@ private static function loadChallengeResources($challengeId) {
1520
1521
return null ;
1521
1522
}
1522
1523
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
+
1523
1561
/**
1524
1562
* Load Topcoder Challenge by Challenge ID
1525
1563
* @param $challengeId
@@ -1535,7 +1573,7 @@ private static function loadChallenge($challengeId) {
1535
1573
'header ' => 'Authorization: Bearer ' .$ token
1536
1574
));
1537
1575
$ context = stream_context_create ($ options );
1538
- $ data = file_get_contents ($ topcoderChallengeApiUrl . ' ?challengeId= ' . $ challengeId , false , $ context );
1576
+ $ data = file_get_contents ($ topcoderChallengeApiUrl . $ challengeId , false , $ context );
1539
1577
if ($ data === false ) {
1540
1578
// Handle errors (e.g. 404 and others)
1541
1579
self ::log ('Couldn \'t get challenge: no token ' , ['headers ' => json_encode ($ http_response_header )]);
@@ -1549,6 +1587,35 @@ private static function loadChallenge($challengeId) {
1549
1587
return null ;
1550
1588
}
1551
1589
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
+
1552
1619
/**
1553
1620
* Get a Topcoder Roles
1554
1621
*
@@ -1761,13 +1828,15 @@ public static function getUserPhotoUrl($user) {
1761
1828
// Set Topcoder Project Roles Data for a challenge
1762
1829
private function setTopcoderProjectData ($ sender , $ challengeID ) {
1763
1830
if ($ challengeID ) {
1831
+ $ challenge = $ this ->getChallenge ($ challengeID );
1764
1832
$ resources = $ this ->getChallengeResources ($ challengeID );
1765
1833
$ roleResources = $ this ->getRoleResources ();
1766
1834
$ currentProjectRoles = $ this ->getTopcoderProjectRoles (Gdn::session ()->User , $ resources , $ roleResources );
1767
1835
if ($ currentProjectRoles ) {
1768
1836
$ currentProjectRoles = array_map ('strtolower ' ,$ currentProjectRoles );
1769
1837
}
1770
1838
1839
+ $ sender ->Data ['Challenge ' ] = $ challenge ;
1771
1840
$ sender ->Data ['ChallengeResources ' ] = $ resources ;
1772
1841
$ sender ->Data ['ChallengeRoleResources ' ] = $ roleResources ;
1773
1842
$ sender ->Data ['ChallengeCurrentUserProjectRoles ' ] = $ currentProjectRoles ;
@@ -1777,7 +1846,7 @@ private function setTopcoderProjectData($sender, $challengeID) {
1777
1846
// }
1778
1847
self ::log ('setTopcoderProjectData ' , ['ChallengeID ' => $ challengeID , 'currentUser ' => $ currentProjectRoles ,
1779
1848
'Topcoder Resources ' => $ resources , 'Topcoder RoleResources '
1780
- => $ roleResources ,]);
1849
+ => $ roleResources , $ challenge => $ challenge ]);
1781
1850
}
1782
1851
}
1783
1852
0 commit comments