/**
  * 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()
 {
     global $DB, $SESSION;
     $mform =& $this->_form;
     $mform->addElement('header', 'header', get_string('heading:info', 'block_coupon'));
     if (!($strinfo = get_config('block_coupon', 'info_coupon_confirm'))) {
         $strinfo = get_string('missing_config_info', 'block_coupon');
     }
     $mform->addElement('static', 'info', '', $strinfo);
     // Determine which type of settings we'll use.
     $radioarray = array();
     $radioarray[] =& $mform->createElement('radio', 'showform', '', get_string('showform-amount', 'block_coupon'), 'amount', array('onchange' => 'showHide(this.value)'));
     $radioarray[] =& $mform->createElement('radio', 'showform', '', get_string('showform-csv', 'block_coupon'), 'csv', array('onchange' => 'showHide(this.value)'));
     $radioarray[] =& $mform->createElement('radio', 'showform', '', get_string('showform-manual', 'block_coupon'), 'manual', array('onchange' => 'showHide(this.value)'));
     $mform->addGroup($radioarray, 'radioar', get_string('label:showform', 'block_coupon'), array('<br/>'), false);
     $mform->setDefault('showform', 'amount');
     // Send coupons based on CSV upload.
     $mform->addElement('header', 'csvForm', get_string('heading:csvForm', 'block_coupon'));
     // Filepicker.
     $urldownloadcsv = new \moodle_url('/blocks/coupon/sample.csv');
     $mform->addElement('filepicker', 'coupon_recipients', get_string('label:coupon_recipients', 'block_coupon'), null, array('accepted_types' => 'csv'));
     $mform->addHelpButton('coupon_recipients', 'label:coupon_recipients', 'block_coupon');
     $mform->addElement('static', 'coupon_recipients_desc', '', get_string('coupon_recipients_desc', 'block_coupon'));
     $mform->addElement('static', 'sample_csv', '', '<a href="' . $urldownloadcsv . '" target="_blank">' . get_string('download-sample-csv', 'block_coupon') . '</a>');
     // Editable email message.
     $mform->addElement('editor', 'email_body', get_string('label:email_body', 'block_coupon'), array('noclean' => 1));
     $mform->setType('email_body', PARAM_RAW);
     $mform->setDefault('email_body', array('text' => get_string('coupon_mail_csv_content_cohorts', 'block_coupon')));
     $mform->addRule('email_body', get_string('required'), 'required');
     $mform->addHelpButton('email_body', 'label:email_body', 'block_coupon');
     // Configurable enrolment time.
     $mform->addElement('date_selector', 'date_send_coupons', get_string('label:date_send_coupons', 'block_coupon'));
     $mform->addRule('date_send_coupons', get_string('required'), 'required');
     $mform->addHelpButton('date_send_coupons', 'label:date_send_coupons', 'block_coupon');
     // Send coupons based on CSV upload.
     $mform->addElement('header', 'manualForm', get_string('heading:manualForm', 'block_coupon'));
     // Textarea recipients.
     $mform->addElement('textarea', 'coupon_recipients_manual', get_string("label:coupon_recipients", 'block_coupon'), 'rows="20" cols="50"');
     $mform->addRule('coupon_recipients_manual', get_string('required'), 'required', null, 'client');
     $mform->addHelpButton('coupon_recipients_manual', 'label:coupon_recipients_txt', 'block_coupon');
     $mform->setDefault('coupon_recipients_manual', 'E-mail,Gender,Name');
     $mform->addElement('static', 'coupon_recipients_desc', '', get_string('coupon_recipients_desc', 'block_coupon'));
     // Editable email message.
     $mform->addElement('editor', 'email_body_manual', get_string('label:email_body', 'block_coupon'), array('noclean' => 1));
     $mform->setType('email_body_manual', PARAM_RAW);
     $mform->setDefault('email_body_manual', array('text' => get_string('coupon_mail_csv_content_cohorts', 'block_coupon')));
     $mform->addRule('email_body_manual', get_string('required'), 'required');
     $mform->addHelpButton('email_body_manual', 'label:email_body', 'block_coupon');
     // Configurable enrolment time.
     $mform->addElement('date_selector', 'date_send_coupons_manual', get_string('label:date_send_coupons', 'block_coupon'));
     $mform->addRule('date_send_coupons_manual', get_string('required'), 'required');
     $mform->addHelpButton('date_send_coupons_manual', 'label:date_send_coupons', 'block_coupon');
     // Send coupons based on Amount field.
     $mform->addElement('header', 'amountForm', get_string('heading:amountForm', 'block_coupon'));
     // Set email_to variable.
     $usealternativeemail = get_config('block_coupon', 'use_alternative_email');
     $alternativeemail = get_config('block_coupon', 'alternative_email');
     // Amount of coupons.
     $mform->addElement('text', 'coupon_amount', get_string('label:coupon_amount', 'block_coupon'));
     $mform->setType('coupon_amount', PARAM_INT);
     $mform->addRule('coupon_amount', get_string('error:numeric_only', 'block_coupon'), 'numeric');
     $mform->addRule('coupon_amount', get_string('required'), 'required');
     $mform->addHelpButton('coupon_amount', 'label:coupon_amount', 'block_coupon');
     // Use alternative email address.
     $mform->addElement('checkbox', 'use_alternative_email', get_string('label:use_alternative_email', 'block_coupon'));
     $mform->setType('use_alternative_email', PARAM_BOOL);
     $mform->setDefault('use_alternative_email', $usealternativeemail);
     // Email address to mail to.
     $mform->addElement('text', 'alternative_email', get_string('label:alternative_email', 'block_coupon'));
     $mform->setType('alternative_email', PARAM_EMAIL);
     $mform->setDefault('alternative_email', $alternativeemail);
     $mform->addRule('alternative_email', get_string('error:invalid_email', 'block_coupon'), 'email', null);
     $mform->addHelpButton('alternative_email', 'label:alternative_email', 'block_coupon');
     $mform->disabledIf('alternative_email', 'use_alternative_email', 'notchecked');
     // Generate_pdf checkbox.
     $mform->addElement('checkbox', 'generate_pdf', get_string('label:generate_pdfs', 'block_coupon'));
     $mform->addHelpButton('generate_pdf', 'label:generate_pdfs', 'block_coupon');
     // Collect cohort records.
     $cohorts = $DB->get_records_list('cohort', 'id', $SESSION->generatoroptions->cohorts);
     // Cohorts to add.
     foreach ($cohorts as $cohort) {
         $mform->addElement('header', 'cohortsheader[]', $cohort->name);
         // Fetch the courses that are connected to this cohort.
         if ($cohortcourses = helper::get_courses_by_cohort($cohort->id)) {
             $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'));
         }
     }
     // All elements added, add the custom js function and submit buttons.
     $mform->addElement('html', "\n            <script type='text/javascript'>\n            window.onload=function(){\n                if (document.getElementById('id_showform_csv').checked == true) {\n                    showHide('csv');\n                } else if (document.getElementById('id_showform_amount').checked == true) {\n                    showHide('amount');\n                } else {\n                    showHide('manual');\n                }\n            }\n\n            function showHide(fieldValue) {\n                switch(fieldValue) {\n                    case 'csv':\n                        document.getElementById('id_amountForm').style.display='none';\n                        document.getElementById('id_manualForm').style.display='none';\n                        break;\n                    case 'amount':\n                        document.getElementById('id_csvForm').style.display='none';\n                        document.getElementById('id_manualForm').style.display='none';\n                        break;\n                    case 'manual':\n                        document.getElementById('id_csvForm').style.display='none';\n                        document.getElementById('id_amountForm').style.display='none';\n                        break;\n                }\n\n                document.getElementById('id_' + fieldValue + 'Form').style.display='block';\n            }\n            </script>\n        ");
     // Submit button.
     $this->add_action_buttons(true, get_string('button:save', 'block_coupon'));
 }