/**
  * Form definition: create the color editing controls and then create a checkbox for
  * each section to enable/disable the collapsed label display.
  *
  */
 public function definition()
 {
     global $DB;
     $mform =& $this->_form;
     $mform->addElement('header', 'colorheader', get_string('editcolorsheader', 'format_collblct'));
     $mform->registerRule('color', 'regex', '/^#([a-fA-F0-9]{6})$/');
     $mform->addElement('text', 'foregroundcolor', get_string('foregroundcolor', 'format_collblct'), null);
     $mform->setType('foregroundcolor', PARAM_TEXT);
     $mform->addRule('foregroundcolor', get_string('invalidcolor', FORMAT_CTWCL_LANG_TABLE), 'color');
     $mform->addElement('text', 'backgroundcolor', get_string('backgroundcolor', 'format_collblct'), null);
     $mform->setType('backgroundcolor', PARAM_TEXT);
     $mform->addRule('backgroundcolor', get_string('invalidcolor', FORMAT_CTWCL_LANG_TABLE), 'color');
     // Add button to return to defaults.
     $mform->addElement('checkbox', 'returndefault', get_string('returndefault', FORMAT_CTWCL_LANG_TABLE));
     $mform->addElement('header', 'courseheader', 'Enable collapsed labels by section');
     $sectionheaders = array();
     $numberofsections = ctwcl_get_section_titles($this->courseid, $sectionheaders);
     for ($i = 0; $i < $numberofsections; $i++) {
         $sectionfield = 'sectionfield_' . "{$i}";
         $sectioncheckbox = 'sectioncheckbox_' . "{$i}";
         $pagegroup = array();
         $pagegroup[] = $mform->createElement('advcheckbox', $sectioncheckbox, '', null, null, array(0, 1));
         $pagegroup[] = $mform->createElement('static', $sectionfield, null, $sectionheaders[$i]);
         $mform->addGroup($pagegroup, 'newlistbar' . "{$i}", null, null, false);
     }
     $mform->addElement('hidden', 'courseid');
     $mform->setType('courseid', PARAM_INT);
     $this->add_action_buttons();
 }
/**
 * This function processes the submitted form for the two requested colors.  Earlier
 * in this file, the default values are placed into the form, so I only handle updates
 * if the values have changed from the default.
 *
 */
function process_form($courseid, &$submittedform)
{
    global $DB;
    if (isset($submittedform->returndefault)) {
        $colorrecord = new course_color_record($courseid);
        $colorrecord->delete_record();
        return;
    }
    /* NOTE: I've written these as separate cases to try to only write new colors and preserve
     * the code falling back to the defaults as best as I can. */
    if ($submittedform->foregroundcolor != DEFAULT_FOREGROUND) {
        $colorrecord = new course_color_record($courseid);
        $colorrecord->set_foreground_color($submittedform->foregroundcolor);
    }
    if ($submittedform->backgroundcolor != DEFAULT_BACKGROUND) {
        $colorrecord = new course_color_record($courseid);
        $colorrecord->set_background_color($submittedform->backgroundcolor);
    }
    // Now check each of the sections.
    $sectionheaders = array();
    $numberofsections = ctwcl_get_section_titles($courseid, $sectionheaders);
    $csr = new course_section_record($courseid);
    for ($i = 0; $i < $numberofsections; $i++) {
        $sectionfield = 'sectionfield_' . "{$i}";
        $sectioncheckbox = 'sectioncheckbox_' . "{$i}";
        // Moodle sections count 1->N, section 0 is the summary area at the top.
        $csr->update_section_record($i + 1, $submittedform->{$sectioncheckbox});
    }
}