Esempio n. 1
0
 /**
  * Returns XHTML field(s) as required by choices
  *
  * Relies on data being an array should data ever be another valid vartype with
  * acceptable value this may cause a warning/error
  * if (!is_array($data)) would fix the problem
  *
  * @todo Add vartype handling to ensure $data is an array
  *
  * @param array $data An array of checked values
  * @param string $query
  * @return string XHTML field
  */
 public function output_html($data, $query = '')
 {
     if (!$this->load_choices() or empty($this->choices)) {
         return '';
     }
     $default = $this->get_defaultsetting();
     if (is_null($default)) {
         $default = array();
     }
     if (is_null($data)) {
         $data = array();
     }
     $options = array();
     $defaults = array();
     foreach ($this->choices as $key => $description) {
         if (!empty($data[$key])) {
             $checked = 'checked="checked"';
         } else {
             $checked = '';
         }
         if (!empty($default[$key])) {
             $defaults[] = $description;
         }
         //            $options[] = '<input type="checkbox" id="'.$this->get_id().'_'.$key.'" name="'.$this->get_full_name().'['.$key.']" value="1" '.$checked.' />'
         //                .'<label for="'.$this->get_id().'_'.$key.'">'.highlightfast($query, $description).'</label>';
         $options[] = '<input type="checkbox" id="' . $this->get_id() . '_' . $key . '" name="' . $this->get_full_name() . '[' . $key . ']" value="1" ' . $checked . ' />' . '<label for="' . $this->get_id() . '_' . $key . '">' . $this->icons[$key] . highlightfast($query, $description) . '</label>';
     }
     if (is_null($default)) {
         $defaultinfo = NULL;
     } elseif (!empty($defaults)) {
         $defaultinfo = implode(', ', $defaults);
     } else {
         $defaultinfo = get_string('none');
     }
     $return = '<div class="form-multicheckbox">';
     $return .= '<input type="hidden" name="' . $this->get_full_name() . '[xxxxx]" value="1" />';
     // something must be submitted even if nothing selected
     if ($options) {
         $return .= '<ul>';
         foreach ($options as $option) {
             $return .= '<li>' . $option . '</li>';
         }
         $return .= '</ul>';
     }
     $return .= '</div>';
     return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', $defaultinfo, $query);
 }
Esempio n. 2
0
    /**
     * Returns XHTML field(s) as required by choices
     *
     * Relies on data being an array should data ever be another valid vartype with
     * acceptable value this may cause a warning/error
     * if (!is_array($data)) would fix the problem
     *
     * @todo Add vartype handling to ensure $data is an array
     *
     * @param array $data An array of checked values
     * @param string $query
     * @return string XHTML field
     */
    public function output_html($data, $query='') {
        global $OUTPUT;

        if (!$this->load_choices() or empty($this->choices)) {
            return '';
        }

        $default = $this->get_defaultsetting();
        if (is_null($default)) {
            $default = array();
        }
        if (is_null($data)) {
            $data = array();
        }

        $context = (object) [
            'id' => $this->get_id(),
            'name' => $this->get_full_name(),
        ];

        $options = array();
        $defaults = array();
        foreach ($this->choices as $key => $description) {
            if (!empty($default[$key])) {
                $defaults[] = $description;
            }

            $options[] = [
                'key' => $key,
                'checked' => !empty($data[$key]),
                'label' => highlightfast($query, $description)
            ];
        }

        if (is_null($default)) {
            $defaultinfo = null;
        } else if (!empty($defaults)) {
            $defaultinfo = implode(', ', $defaults);
        } else {
            $defaultinfo = get_string('none');
        }

        $context->options = $options;
        $context->hasoptions = !empty($options);

        $element = $OUTPUT->render_from_template('core_admin/setting_configmulticheckbox', $context);

        return format_admin_setting($this, $this->visiblename, $element, $this->description, false, '', $defaultinfo, $query);

    }