/** * 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); } } } } }
/** * Render coupon generator page 4 (including header / footer). * * @return string */ public function page_coupon_generator_step4() { global $DB, $USER; // Make sure sessions are still alive. generatoroptions::validate_session(); // Load options. $generatoroptions = generatoroptions::from_session(); // Depending on our data we'll get the right form. if ($generatoroptions->type == generatoroptions::COURSE) { $mform = new \block_coupon\forms\coupon\generator\confirmcourse($this->page->url); } else { $mform = new \block_coupon\forms\coupon\generator\confirmcohorts($this->page->url); } if ($mform->is_cancelled()) { generatoroptions::clean_session(); redirect(new moodle_url('/course/view.php', array('id' => $this->page->course->id))); } else { if ($data = $mform->get_data()) { // These settings are always the same. $generatoroptions->redirecturl = empty($data->redirect_url) ? null : $data->redirect_url; $generatoroptions->enrolperiod = empty($data->enrolment_period) ? null : $data->enrolment_period; // If we're generating based on csv we'll redirect first to confirm the csv input. if ($data->showform == 'csv') { $generatoroptions->senddate = $data->date_send_coupons; $generatoroptions->csvrecipients = $mform->get_file_content('coupon_recipients'); $generatoroptions->emailbody = $data->email_body['text']; // Serialize generatoroptions to session. $generatoroptions->to_session(); // To the extra step. $params = array('id' => $this->page->url->param('id')); $url = new moodle_url('/blocks/coupon/view/generate_coupon_step_five.php', $params); redirect($url); } // If we're generating based on manual csv input. if ($data->showform == 'manual') { $generatoroptions->senddate = $data->date_send_coupons_manual; $generatoroptions->emailbody = $data->email_body_manual['text']; // We'll get users right away. $generatoroptions->recipients = helper::get_recipients_from_csv($data->coupon_recipients_manual); } // If we're generating based on 'amount' of coupons. if ($data->showform == 'amount') { // Save last settings in sessions. $generatoroptions->amount = $data->coupon_amount; $generatoroptions->emailto = !empty($data->use_alternative_email) ? $data->alternative_email : $USER->email; $generatoroptions->generatesinglepdfs = isset($data->generate_pdf) && $data->generate_pdf ? true : false; } // Now that we've got all the coupons. $generator = new generator(); $result = $generator->generate_coupons($generatoroptions); if ($result !== true) { // Means we've got an error. // Don't know yet what we're gonne do in this situation. Maybe mail to supportuser? echo "<p>An error occured while trying to generate the coupons. Please contact support.</p>"; echo "<pre>" . implode("\n", $result) . "</pre>"; die; } if ($data->showform == 'amount') { // Generate and send off. $coupons = $DB->get_records_list('block_coupon', 'id', $generator->get_generated_couponids()); helper::mail_coupons($coupons, $generatoroptions->emailto, $generatoroptions->generatesinglepdfs); generatoroptions::clean_session(); redirect(new moodle_url('/my'), get_string('coupons_sent', 'block_coupon')); } else { redirect(new moodle_url('/my'), get_string('coupons_ready_to_send', 'block_coupon')); } } } $out = ''; $out .= $this->get_coupon_form_page($mform); return $out; }