/**
  * Executes the task
  *
  * @return void
  */
 public function execute()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/blocks/coupon/classes/settings.php';
     // Call coupons.
     $coupons = helper::get_coupons_to_send();
     if (!$coupons || empty($coupons)) {
         return;
     }
     // Omdat we geen koppeltabel hebben...
     $sentcoupons = array();
     $couponsend = time();
     // Dit moet even om ervoor te zorgen dat dingen per owner gegroepeerd worden.
     // Let op: dit verkloot meerdere batches per owner - Sebastian dd 2014-03-19.
     foreach ($coupons as $coupon) {
         // Check if we have an owner.
         if (!is_null($coupon->ownerid)) {
             // And add to sentCoupons so we can check if all of them have been sent.
             if (!isset($sentcoupons[$coupon->ownerid])) {
                 $sentcoupons[$coupon->ownerid] = array();
             }
             if (!in_array($coupon->timecreated, $sentcoupons[$coupon->ownerid])) {
                 $sentcoupons[$coupon->ownerid][] = $couponsend;
             }
         }
         helper::mail_coupons(array($coupon), $coupon->for_user_email, null, $coupon->email_body, true);
         $coupon->issend = true;
         $coupon->timemodified = time();
         $DB->update_record('block_coupon', $coupon);
     }
     // Check if all coupons have been send.
     if (!empty($sentcoupons)) {
         foreach ($sentcoupons as $ownerid => $coupons) {
             foreach ($coupons as $coupontimecreated) {
                 if (helper::has_sent_all_coupons($ownerid, $coupontimecreated)) {
                     // Mail confirmation.
                     helper::confirm_coupons_sent($ownerid, $coupontimecreated);
                 }
             }
         }
     }
 }