Kaynağa Gözat

Ticket #4575 - Events: Enhancements. Point 3.

AntonLV 2 yıl önce
ebeveyn
işleme
36a3222559

+ 38 - 1
modules/base/groups/classes/BxBaseModGroupsConnectionFans.php

@@ -29,8 +29,26 @@ class BxBaseModGroupsConnectionFans extends BxTemplConnection
     {
         return $this->_sModule;
     }
+    
+    public function actionRemove($iContent = 0, $iInitiator = false)
+    {
+        $CNF = &$this->_oModule->_oConfig->CNF;
+
+        $aResult = parent::actionRemove($iContent, $iInitiator);
+
+        if($aResult['err'] == false) {
+            if(!$iContent)
+                $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
+
+            $aContentInfo = $this->_oModule->_oDb->getContentInfoByProfileId($iContent);
+            if(!empty($aContentInfo) && is_array($aContentInfo))
+                $this->_oModule->_oDb->deleteAnswersProfileId($aContentInfo[$CNF['FIELD_ID']], $iInitiator ? $iInitiator : bx_get_logged_profile_id());
+        }
 
-    public function getActionTitle ($sAction, $iInitiatorId, $iContentId, $bFlip = false)
+        return $aResult;
+    }
+
+    public function getActionTitle($sAction, $iInitiatorId, $iContentId, $bFlip = false)
     {
         $aResult = [];
         if($this->isConnectedNotMutual($iInitiatorId, $iContentId))
@@ -74,6 +92,25 @@ class BxBaseModGroupsConnectionFans extends BxTemplConnection
 
         return (int)$aContentInfo[$CNF['FIELD_JOIN_CONFIRMATION']] != 0 && $this->_oModule->_oDb->hasQuestions($aContentInfo[$CNF['FIELD_ID']]);
     }
+    
+    public function isQuestionnaireAnswered($iContentProfileId, $iProfileId = 0)
+    {
+        $CNF = &$this->_oModule->_oConfig->CNF;
+
+        if(!$iProfileId)
+            $iProfileId = bx_get_logged_profile_id();
+        if(!$iProfileId)
+            return false;
+
+        if(!$this->_bQuestionnaire || empty($CNF['FIELD_JOIN_CONFIRMATION']))
+            return false;
+
+        $aContentInfo = $this->_oModule->_oDb->getContentInfoByProfileId($iContentProfileId);
+        if(empty($aContentInfo) || !is_array($aContentInfo))
+            return false;
+
+        return (int)$aContentInfo[$CNF['FIELD_JOIN_CONFIRMATION']] != 0 && $this->_oModule->_oDb->areQuestionsAnswered($aContentInfo[$CNF['FIELD_ID']], $iProfileId);
+    }
 
     public function getQuestionnaireForm($sAction, $iContentProfileId, $aParams = [])
     {

+ 21 - 0
modules/base/groups/classes/BxBaseModGroupsDb.php

@@ -533,6 +533,17 @@ class BxBaseModGroupsDb extends BxBaseModProfileDb
 
         return !empty($aQuestions) && is_array($aQuestions);
     }
+    
+    public function areQuestionsAnswered($iContentId, $iProfileId)
+    {
+        $aQuestions = $this->getQuestions([
+            'sample' => 'answers', 
+            'content_id' => $iContentId,
+            'profile_id' => $iProfileId
+        ]);
+
+        return !empty($aQuestions) && is_array($aQuestions);
+    }
 
     public function getQuestionOrderMax($iContentId)
     {
@@ -558,6 +569,16 @@ class BxBaseModGroupsDb extends BxBaseModProfileDb
         ]) !== false ? (int)$this->lastId() : false;
     }
 
+    public function deleteAnswersProfileId($iContentId, $iProfileId)
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        return $this->query("DELETE FROM `ta` USING `" . $CNF['TABLE_QUESTIONS'] . "` AS `tq` LEFT JOIN `" . $CNF['TABLE_ANSWERS'] . "` AS `ta` ON `tq`.`id`=`ta`.`question_id` WHERE `tq`.`content_id`=:content_id AND `ta`.`profile_id`=:profile_id", [
+            'content_id' => $iContentId,
+            'profile_id' => $iProfileId
+        ]) !== false;
+    }
+
     public function deleteQuestionnaires($iContentId) 
     {
         $CNF = &$this->_oConfig->CNF;

+ 11 - 4
modules/base/groups/classes/BxBaseModGroupsModule.php

@@ -1154,10 +1154,11 @@ class BxBaseModGroupsModule extends BxBaseModProfileModule
     public function serviceGetQuestionnaire($sSource, $sObject, $sAction, $iContentProfileId)
     {
         $CNF = &$this->_oConfig->CNF;
+        $sMsg = _t('_sys_txt_not_found');
 
         $aContentInfo = $this->_oDb->getContentInfoByProfileId($iContentProfileId);
         if(empty($aContentInfo) || !is_array($aContentInfo))
-            return ['code' => 1];
+            return $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['code' => 1, 'msg' => $sMsg];
         
         $oForm = null;
         if(($oConnection = BxDolConnection::getObjectInstance($sObject)) !== false) {
@@ -1166,10 +1167,10 @@ class BxBaseModGroupsModule extends BxBaseModProfileModule
             ]]);
 
             if($oForm === false)
-                return ['code' => 3];
+                return $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['code' => 3, 'msg' => $sMsg];
         }
         else
-            return ['code' => 2];
+            return $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['code' => 2, 'msg' => $sMsg];
 
         $oForm->initChecker();
         if($oForm->isSubmittedAndValid()) {
@@ -1179,9 +1180,15 @@ class BxBaseModGroupsModule extends BxBaseModProfileModule
             foreach($aQuestions as $aQuestion)
                 $this->_oDb->insertAnswer((int)$aQuestion['id'], $iProfileId, $oForm->getCleanValue('question_' . $aQuestion['id']));
 
-            return ['code' => 0, 'o' => $sObject, 'a' => $sAction, 'cpi' => $iContentProfileId, 'ci' => $aContentInfo[$CNF['FIELD_ID']], 'eval' => $this->_oConfig->getJsObject($sSource) . '.connActionPerformed(oData)'];
+            return $this->_bIsApi ? [] : ['code' => 0, 'o' => $sObject, 'a' => $sAction, 'cpi' => $iContentProfileId, 'ci' => $aContentInfo[$CNF['FIELD_ID']], 'eval' => $this->_oConfig->getJsObject($sSource) . '.connActionPerformed(oData)'];
         }
 
+        if($this->_bIsApi)
+            return [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => [
+                'name' => $this->_oModule->getName(), 
+                'request' => ['url' => $oForm->aFormAttrs['action'], 'immutable' => true]]
+            ])];
+
         bx_import('BxTemplFunctions');
         $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oConfig->getHtmlIds('popup_questionnaire'), _t($CNF['T']['popup_title_questionnaire']), $this->_oTemplate->parseHtmlByName('popup_qnr_questionnaire.html', [
             'form_id' => $oForm->getId(),

+ 3 - 2
template/scripts/BxBaseServiceConnections.php

@@ -47,8 +47,9 @@ class BxBaseServiceConnections extends BxDol
         if(!$oConnection)
             return ['code' => 2];
 
-        $sMethod = 'hasQuestionnaire';
-        if($aParams['a'] == 'add' && method_exists($oConnection, $sMethod) && $oConnection->$sMethod($aParams['cid'])) {
+        $sMethodHas = 'hasQuestionnaire';
+        $sMethodIsAnswered = 'isQuestionnaireAnswered';
+        if($aParams['a'] == 'add' && method_exists($oConnection, $sMethodHas) && $oConnection->$sMethodHas($aParams['cid']) && method_exists($oConnection, $sMethodIsAnswered) && !$oConnection->$sMethodIsAnswered($aParams['cid'], $aParams['iid'])) {
             $sModule = $oConnection->getModule();
 
             return [