/** * Perform validation. * * @param array $data array of ("fieldname"=>value) of submitted data * @param array $files array of uploaded files "element_name"=>tmp_file_path * @return array of "element_name"=>"error_description" if there are errors, * or an empty array if everything is OK (true allowed for backwards compatibility too). */ public function validation($data, $files) { $errors = parent::validation($data, $files); $recipientserror = helper::validate_coupon_recipients($data['coupon_recipients']); if ($recipientserror !== true) { $errors['coupon_recipients'] = $recipientserror; } return $errors; }
/** * form definition */ public function definition() { global $CFG, $DB, $SESSION; $mform =& $this->_form; $mform->addElement('header', 'header', get_string('heading:info', 'block_coupon')); if (!($strinfo = get_config('block_coupon', 'info_coupon_cohort_courses'))) { $strinfo = get_string('missing_config_info', 'block_coupon'); } $mform->addElement('static', 'info', '', $strinfo); // Collect cohort records. $cohorts = $DB->get_records_list('cohort', 'id', $SESSION->generatoroptions->cohorts); // Now we'll show the cohorts one by one. foreach ($cohorts as $cohort) { // Header for the cohort. $mform->addElement('header', 'cohortsheader[]', $cohort->name); // Collect courses connected to cohort. $cohortcourses = helper::get_courses_by_cohort($cohort->id); // If we have connected courses we'll display them. if ($cohortcourses) { $headingstr = array(); foreach ($cohortcourses as $course) { $headingstr[] = $course->fullname; } $mform->addElement('static', 'connected_courses', get_string('label:connected_courses', 'block_coupon'), implode('<br/>', $headingstr)); } else { $mform->addElement('static', 'connected_courses[' . $cohort->id . '][]', get_string('label:connected_courses', 'block_coupon'), get_string('label:no_courses_connected', 'block_coupon')); } // Collect not connected courses. $notconnectedcourses = helper::get_unconnected_cohort_courses($cohort->id); // If we have not connected courses we'll display them. if ($notconnectedcourses) { $arrnotconnectedcourses = array(); foreach ($notconnectedcourses as $notconnectedcourse) { $arrnotconnectedcourses[$notconnectedcourse->id] = $notconnectedcourse->fullname; } $attributes = array('size' => min(20, count($arrnotconnectedcourses))); $selectconnectcourses =& $mform->addElement('select', 'connect_courses[' . $cohort->id . ']', get_string('label:coupon_connect_course', 'block_coupon'), $arrnotconnectedcourses, $attributes); $mform->addHelpButton('connect_courses[' . $cohort->id . ']', 'label:coupon_connect_course', 'block_coupon'); $selectconnectcourses->setMultiple(true); } // That's the end of the loop. } // Action buttons. $this->add_action_buttons(true, get_string('button:next', 'block_coupon')); }
/** * form definition */ public function definition() { $mform =& $this->_form; $mform->addElement('header', 'header', get_string('heading:info', 'block_coupon')); if (!($strinfo = get_config('block_coupon', 'info_coupon_cohorts'))) { $strinfo = get_string('missing_config_info', 'block_coupon'); } $mform->addElement('static', 'info', '', $strinfo); $mform->addElement('header', 'header', get_string('heading:input_cohorts', 'block_coupon')); // First we'll get some useful info. $cohorts = helper::get_cohort_menu(); // And create data for multiselect. $arrcohortselect = array(); foreach ($cohorts as $cohort) { $arrcohortselect[$cohort->id] = $cohort->name; } $attributes = array('size' => min(20, count($arrcohortselect))); // Cohort id. $selectcohorts =& $mform->addElement('select', 'coupon_cohorts', get_string('label:coupon_cohorts', 'block_coupon'), $arrcohortselect, $attributes); $selectcohorts->setMultiple(true); $mform->addRule('coupon_cohorts', get_string('error:required', 'block_coupon'), 'required', null, 'client'); $mform->addHelpButton('coupon_cohorts', 'label:coupon_cohorts', 'block_coupon'); $this->add_action_buttons(true, get_string('button:next', 'block_coupon')); }
* @author R.J. van Dongen <*****@*****.**> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once dirname(__FILE__) . '/../../../config.php'; require_once $CFG->dirroot . '/blocks/coupon/classes/settings.php'; use block_coupon\helper; $id = required_param('id', PARAM_INT); $instance = $DB->get_record('block_instances', array('id' => $id), '*', MUST_EXIST); $context = \context_block::instance($instance->id); $coursecontext = $context->get_course_context(false); $course = false; if ($coursecontext !== false) { $course = $DB->get_record("course", array("id" => $coursecontext->instanceid)); } if ($course === false) { $course = get_site(); } require_login($course, true); $PAGE->navbar->add(get_string('page:generate_coupon_step_four.php:title', 'block_coupon')); $url = new moodle_url('/blocks/coupon/view/generate_coupon_step_four.php', array('id' => $id)); $PAGE->set_url($url); $PAGE->set_title(get_string('view:generate_coupon:title', 'block_coupon')); $PAGE->set_heading(get_string('view:generate_coupon:heading', 'block_coupon')); $PAGE->set_context($context); $PAGE->set_course($course); $PAGE->set_pagelayout('standard'); // Make sure the moodle editmode is off. helper::force_no_editing_mode(); require_capability('block/coupon:generatecoupons', $context); $renderer = $PAGE->get_renderer('block_coupon'); echo $renderer->page_coupon_generator_step4();
/** * Render coupon generator page 5 (including header / footer). * * @return string */ public function page_coupon_generator_step5() { // Make sure sessions are still alive. generatoroptions::validate_session(); // Load options. $generatoroptions = generatoroptions::from_session(); // Create form. $mform = new \block_coupon\forms\coupon\generator\extra($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()) { // Get recipients. $generatoroptions->recipients = helper::get_recipients_from_csv($data->coupon_recipients); // Now that we've got all information we'll create the coupon objects. $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; } // Finish. generatoroptions::clean_session(); redirect(new moodle_url('/my'), get_string('coupons_ready_to_send', 'block_coupon')); } } $out = ''; $out .= $this->get_coupon_form_page($mform); return $out; }
/** * Create a new instance * * @param string $titlestring */ public function __construct($titlestring) { global $CFG; $this->namestring = $titlestring; $this->generatordate = date('Y-m-d', time()); parent::__construct('P', 'mm', 'A4', true, 'UTF-8'); $this->SetFont('helvetica', '', 12); $this->SetCreator('PDF Generator build 1.0'); $this->SetAuthor('Sebsoft PDF Generator build 1.0'); $this->SetTitle(get_string('pdf-meta:title', 'block_coupon')); $this->SetSubject(get_string('pdf-meta:subject', 'block_coupon')); $this->SetKeywords(get_string('pdf-meta:keywords', 'block_coupon')); $this->SetHeaderMargin(0); $this->SetFooterMargin(0); $this->SetMargins(0, 0, 0, true); // L-T-R. $this->SetAutoPageBreak(false, 0); $fn = helper::get_coupon_logo(); if (file_exists($fn)) { $this->logo = $fn; } }
if ($course === false) { $course = get_site(); } require_login($course, true); $PAGE->navbar->add(get_string('view:input_coupon:title', 'block_coupon')); $url = new moodle_url('/blocks/coupon/view/input_coupon.php', array('id' => $id)); $PAGE->set_url($url); $PAGE->set_title(get_string('view:input_coupon:title', 'block_coupon')); $PAGE->set_heading(get_string('view:input_coupon:heading', 'block_coupon')); $PAGE->set_context($context); $PAGE->set_pagelayout('standard'); // Make sure the moodle editmode is off. helper::force_no_editing_mode(); require_capability('block/coupon:inputcoupons', $context); // Include the form. $mform = new validator($url); if ($mform->is_cancelled()) { redirect(new moodle_url('/course/view.php', array('id' => $course->id))); } else { if ($data = $mform->get_data()) { $redirecturl = helper::claim_coupon($data->coupon_code); // Redirect to my directly. redirect($redirecturl, get_string('success:coupon_used', 'block_coupon')); } else { echo $OUTPUT->header(); echo '<div class="block-coupon-container">'; $mform->display(); echo '</div>'; echo $OUTPUT->footer(); } }
/** * Perform validation. * * @param array $data array of ("fieldname"=>value) of submitted data * @param array $files array of uploaded files "element_name"=>tmp_file_path * @return array of "element_name"=>"error_description" if there are errors, * or an empty array if everything is OK (true allowed for backwards compatibility too). */ public function validation($data, $files) { // Set which fields to validate, depending on form used. if ($data['showform'] == 'csv' || $data['showform'] == 'manual') { $data2validate = array('email_body' => $data['email_body'], 'date_send_coupons' => $data['date_send_coupons']); } else { $data2validate = array('coupon_amount' => $data['coupon_amount'], 'alternative_email' => $data['alternative_email']); } $data2validate['redirect_url'] = $data['redirect_url']; $data2validate['enrolment_period'] = $data['enrolment_period']; // Validate. $errors = parent::validation($data2validate, $files); // Custom validate. if ($data['showform'] == 'amount') { // Max amount of coupons. $maxcouponsamount = get_config('block_coupon', 'max_coupons'); if ($data['coupon_amount'] > $maxcouponsamount || $data['coupon_amount'] < 1) { $errors['coupon_amount'] = get_string('error:coupon_amount_too_high', 'block_coupon', array('min' => '0', 'max' => $maxcouponsamount)); } // Alternative email required if use_alternative_email is checked. if (isset($data['use_alternative_email']) && empty($data['alternative_email'])) { $errors['alternative_email'] = get_string('error:alternative_email_required', 'block_coupon'); } } else { if ($data['showform'] == 'csv') { $csvcontent = $this->get_file_content('coupon_recipients'); if (!$csvcontent || empty($csvcontent)) { $errors['coupon_recipients'] = get_string('required'); } } else { $validationresult = helper::validate_coupon_recipients($data['coupon_recipients_manual']); if ($validationresult !== true) { $errors['coupon_recipients_manual'] = $validationresult; } } } return $errors; }
/** * Render visual representation of the 'datecomplete' column for use in the table * * @param \stdClass $row * @return string time string */ public function col_datecomplete($row) { return is_numeric($row->datecomplete) ? helper::render_date($row->datecomplete, false) : $row->datecomplete; }
/** * 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); } } } } }