0

I need to limit one webform submission per quarter. For each quarter, the user can only make one submission. I want to use one webform. Here are some examples of the date period and the due date. How can I achieve the desired outcome with Webforms?

  • Q1 (Oct. 1, 2019 – Dec. 30, 2019): due by Jan. 15, 2020
  • Q2 (Jan. 1, 2020 – Mar. 30, 2020): due by Apr. 15, 2020
  • Q3 (Apr. 1, 2020 – Jun. 30, 2020): due by Jul. 15, 2020

1 Answer 1

0

ok so here is how to do that.. you need to create a module and set Drupal cron to run once a day on your hosting provider then you need your webform to have email unique then do this ....

/**
 * Implements hook_cron().
 */
function YOURMODULE_cron() {
  $current_month = date('m');
  // Get first month of quarter
  $new_month = (3 * floor(($current_month - 1 ) / 3)) + 1;
  $new_month = substr('0' . $new_month, -2);
  $first_quarter_day_date = date('Y') . '-' . $new_month . '-01';
  $today = date('Y-m-d');
  // So this is the first day of the quarter.
  if ($first_quarter_day_date == $today) {
    // Use cache to make sure this runs once.
    $cid = 'clear_out_webform_date';
    $cached = \Drupal::cache()->get($cid);
    $run = TRUE;
    if (!empty($cached)) {
      if (!empty($cached->data['date'])) {
        if ($cached->data['date'] == $today) {
          $run = FALSE;
        }
      }
    }

    if ($run == TRUE) {
      // change this to your webform id
      $webform_id = 'contact';

      // Export all submissions to file.
      $webform = \Drupal\webform\Entity\Webform::load($webform_id);
      $submission_exporter = \Drupal::service('webform_submission.exporter');
      $export_options = $submission_exporter->getDefaultExportOptions();
      $export_options['delimiter'] = ';';
      $export_options['multiple_delimiter'] = ',';
      $submission_exporter->setWebform($webform);
      $submission_exporter->setExporter($export_options);
      $submission_exporter->generate();
      $file_path = $submission_exporter->getExportFilePath();
      // rename the file to something good.
      $filename = $today.'-export.csv';
      $uri = 'public://' . $filename;
      $fileContent = file_get_contents($file_path);
      $destination = $uri;
      // Save the file to be a file entity.
      $file = file_save_data(
        $fileContent,
        $destination,
        FILE_EXISTS_RENAME
      );
      if (is_object($file)) {
        // Probably email your self a reminder here.
        // ^^ file is now an entity so you can do something like.
        // load a know entity and append the file to a document field.
        // or just download it as in the public file dir.

        // Delete previous quarter submissions.
        $query = \Drupal::entityQuery('webform_submission')
          ->condition('webform_id', $webform_id);
        $result = $query->execute();
        if (!empty($result)) {
          $webform_submission_storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
          $submissions = $webform_submission_storage->loadMultiple($result);
          foreach ($submissions as $submission) {
            $submission->delete();
          }
        }
      }
      // set the cache to today so it wont run again.
      \Drupal::cache()->set($cid, ['date' => $today]);
    }
  }
}

have fun and good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.