Browse Source

Block survey if not available see BT#14957

Julio Montoya 6 years ago
parent
commit
a45e1b8615
3 changed files with 102 additions and 44 deletions
  1. 7 44
      main/survey/fillsurvey.php
  2. 2 0
      main/survey/meeting.php
  3. 93 0
      main/survey/survey.lib.php

+ 7 - 44
main/survey/fillsurvey.php

@@ -109,9 +109,9 @@ if ($invitationcode == 'auto' && isset($_GET['scode'])) {
     $result = Database::query($sql);
     if (Database :: num_rows($result) > 0) {
         // Check availability
-        $row = Database :: fetch_array($result, 'ASSOC');
-        $tempdata = SurveyManager :: get_survey($row['survey_id']);
-        check_time_availability($tempdata);
+        $row = Database::fetch_array($result, 'ASSOC');
+        $tempdata = SurveyManager::get_survey($row['survey_id']);
+        SurveyManager::checkTimeAvailability($tempdata);
         // Check for double invitation records (insert should be done once)
         $sql = "SELECT user
                 FROM $table_survey_invitation
@@ -202,6 +202,10 @@ $survey_data = SurveyManager::get_survey($survey_invitation['survey_id']);
 if (empty($survey_data)) {
     api_not_allowed(true);
 }
+
+// Checking time availability
+SurveyManager::checkTimeAvailability($survey_data);
+
 $survey_data['survey_id'] = $survey_invitation['survey_id'];
 
 if ($survey_data['survey_type'] == '3') {
@@ -524,9 +528,6 @@ if ($survey_data['form_fields'] != '' &&
     $form->setDefaults($user_data);
 }
 
-// Checking time availability
-check_time_availability($survey_data);
-
 // Header
 Display::display_header(get_lang('ToolSurvey'));
 
@@ -1408,41 +1409,3 @@ if ($survey_data['survey_type'] === '0') {
 $form->addHtml('</div>');
 $form->display();
 Display::display_footer();
-
-/**
- * Check whether this survey has ended. If so, display message and exit rhis script.
- *
- * @param array $surveyData Survey data
- */
-function check_time_availability($surveyData)
-{
-    $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
-    $utcZone = new DateTimeZone('UTC');
-    $startDate = new DateTime($surveyData['start_date'], $utcZone);
-    $endDate = new DateTime($surveyData['end_date'], $utcZone);
-    $currentDate = new DateTime('now', $utcZone);
-    if (!$allowSurveyAvailabilityDatetime) {
-        $currentDate->modify('today');
-    }
-    if ($currentDate < $startDate) {
-        api_not_allowed(
-            true,
-            Display:: return_message(
-                get_lang('SurveyNotAvailableYet'),
-                'warning',
-                false
-            )
-        );
-    }
-
-    if ($currentDate > $endDate) {
-        api_not_allowed(
-            true,
-            Display:: return_message(
-                get_lang('SurveyNotAvailableAnymore'),
-                'warning',
-                false
-            )
-        );
-    }
-}

+ 2 - 0
main/survey/meeting.php

@@ -64,6 +64,8 @@ if (empty($surveyData)) {
     api_not_allowed(true);
 }
 
+SurveyManager::checkTimeAvailability($surveyData);
+
 $invitations = SurveyUtil::get_invited_users($surveyData['code']);
 $students = isset($invitations['course_users']) ? $invitations['course_users'] : [];
 

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

@@ -2212,4 +2212,97 @@ class SurveyManager
             }
         }
     }
+
+    /**
+     * @param array $survey
+     *
+     * @return int
+     */
+    public static function getCountPages($survey)
+    {
+        if (empty($survey) || !isset($survey['iid'])) {
+            return 0;
+        }
+
+        $courseId = $survey['c_id'];
+        $surveyId = $survey['survey_id'];
+
+        $table = Database::get_course_table(TABLE_SURVEY_QUESTION);
+
+        // One question per page
+        $sql = "SELECT * FROM $table
+                WHERE
+                    survey_question NOT LIKE '%{{%' AND
+                    type = 'pagebreak' AND
+                    c_id = $courseId AND
+                    survey_id = '".$surveyId."'";
+        $result = Database::query($sql);
+        $numberPageBreaks = Database::num_rows($result);
+
+        // One question per page
+        $sql = "SELECT * FROM $table
+                    WHERE
+                        survey_question NOT LIKE '%{{%' AND
+                        type != 'pagebreak' AND
+                        c_id = $courseId AND
+                        survey_id = '".$surveyId."'";
+        $result = Database::query($sql);
+        $countOfQuestions = Database::num_rows($result);
+
+        $count = 1;
+        if (!empty($numberPageBreaks) && !empty($countOfQuestions)) {
+            // One question per page
+            $count = $countOfQuestions;
+        }
+
+        if ($survey['one_question_per_page'] == 1) {
+            $count = 1;
+            if (!empty($countOfQuestions)) {
+                $count = $countOfQuestions;
+            }
+        }
+
+        return $count;
+    }
+
+    /**
+     * Check whether this survey has ended. If so, display message and exit rhis script.
+     *
+     * @param array $surveyData Survey data
+     */
+    public static function checkTimeAvailability($surveyData)
+    {
+        if (empty($surveyData)) {
+            api_not_allowed(true);
+        }
+        $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
+        $utcZone = new DateTimeZone('UTC');
+        $startDate = new DateTime($surveyData['start_date'], $utcZone);
+        $endDate = new DateTime($surveyData['end_date'], $utcZone);
+        $currentDate = new DateTime('now', $utcZone);
+        if (!$allowSurveyAvailabilityDatetime) {
+            $currentDate->modify('today');
+        }
+        if ($currentDate < $startDate) {
+            api_not_allowed(
+                true,
+                Display:: return_message(
+                    get_lang('SurveyNotAvailableYet'),
+                    'warning',
+                    false
+                )
+            );
+        }
+
+        if ($currentDate > $endDate) {
+            api_not_allowed(
+                true,
+                Display:: return_message(
+                    get_lang('SurveyNotAvailableAnymore'),
+                    'warning',
+                    false
+                )
+            );
+        }
+    }
 }