Browse Source

Survey: Avoid generate auto invitation code when invitation already exists - refs BT#15392

Angel Fernando Quiroz Campos 5 years ago
parent
commit
76c8c5e75a
2 changed files with 45 additions and 2 deletions
  1. 14 2
      main/survey/fillsurvey.php
  2. 31 0
      main/survey/survey.lib.php

+ 14 - 2
main/survey/fillsurvey.php

@@ -99,8 +99,20 @@ if ($invitationcode == 'auto' && isset($_GET['scode'])) {
     if ($isAnonymous) {
         $autoInvitationcode = 'auto-ANONY_'.md5(time())."-$surveyCode";
     } else {
-        // New invitation code from userid
-        $autoInvitationcode = "auto-$userid-$surveyCode";
+        $invitations = SurveyManager::getUserInvitationsForSurveyInCourse(
+            $userid,
+            $surveyCode,
+            $courseInfo['real_id'],
+            $sessionId
+        );
+        $lastInvitation = current($invitations);
+
+        if (!$lastInvitation) {
+            // New invitation code from userid
+            $autoInvitationcode = "auto-$userid-$surveyCode";
+        } else {
+            $autoInvitationcode = $lastInvitation->getInvitationCode();
+        }
     }
 
     // The survey code must exist in this course, or the URL is invalid

+ 31 - 0
main/survey/survey.lib.php

@@ -2356,4 +2356,35 @@ class SurveyManager
             );
         }
     }
+
+    /**
+     * @param int    $userId
+     * @param string $surveyCode
+     * @param int    $courseId
+     * @param int    $sessionId
+     * @param int    $groupId
+     *
+     * @return array|CSurveyInvitation[]
+     */
+    public static function getUserInvitationsForSurveyInCourse(
+        $userId,
+        $surveyCode,
+        $courseId,
+        $sessionId = 0,
+        $groupId = 0
+    ) {
+        $invitationRepo = Database::getManager()->getRepository('ChamiloCourseBundle:CSurveyInvitation');
+        $invitations = $invitationRepo->findBy(
+            [
+                'user' => $userId,
+                'cId' => $courseId,
+                'sessionId' => $sessionId,
+                'groupId' => $groupId,
+                'surveyCode' => $surveyCode,
+            ],
+            ['invitationDate' => 'DESC']
+        );
+
+        return $invitations;
+    }
 }