<?php //$Id: user_filter_forms.php,v 1.1.2.3 2009/09/26 16:28:24 skodak Exp $

require_once($CFG->libdir.'/formslib.php');

class user_add_filter_form extends moodleform {

    function definition() {
// (Shirai131): ユーザ一覧表示におけるフィルタの追加時にアクティブなフィルタをすべて削除してから追加するチェックボックスを追加する改良 (2009/04/23) (2009/07/23)
// (Shirai131): ここから追加
        global $SESSION;

// (Shirai131): ここまで追加
        $mform       =& $this->_form;
        $fields      = $this->_customdata['fields'];
        $extraparams = $this->_customdata['extraparams'];

        $mform->addElement('header', 'newfilter', get_string('newfilter','filters'));

        foreach($fields as $ft) {
            $ft->setupForm($mform);
        }

        // in case we wasnt to track some page params
        if ($extraparams) {
            foreach ($extraparams as $key=>$value) {
                $mform->addElement('hidden', $key, $value);
                $mform->setType($key, PARAM_RAW);
            }
        }

        // Add button
// (Shirai131): ここからコメントアウト
//      $mform->addElement('submit', 'addfilter', get_string('addfilter','filters'));
// (Shirai131): ここから追加
        $objs = array();
//      $objs[] = &$mform->createElement('submit', 'searchuser',     get_string('searchuser',      'filters'));
        $objs[] = &$mform->createElement('submit', 'searchuser',     get_string('searchuser',      'fs_moodle'));
//      $objs[] = &$mform->createElement('submit', 'searchadd',      get_string('searchadd',       'filters'));
        $objs[] = &$mform->createElement('submit', 'searchadd',      get_string('searchadd',       'fs_moodle'));
//      $objs[] = &$mform->createElement('submit', 'searchremoveall',get_string('searchremoveall', 'filters'));
        if (empty($SESSION->user_filtering) or count($SESSION->user_filtering) == 0) $attrib = 'disabled'; else $attrib = '';
        $objs[] = &$mform->createElement('submit', 'searchremoveall',get_string('searchremoveall', 'fs_moodle'), $attrib);
        $mform->addElement('group', 'searchusergrp', '', $objs, ' ', false);

        // Display latest filter
        if (!empty($SESSION->latest_filter)) {
            $mform->addElement('html', '<hr size="1pix" width="80%" />');
//          $latestfilterlabel = get_string('latestfilter', 'filters').'&nbsp;:&nbsp;';
            $latestfilterlabel = get_string('latestfilter', 'fs_moodle').'&nbsp;:&nbsp;';
            $latestfilterhtml  = '<table align="center">';
            foreach ($SESSION->latest_filter as $fname => $data) {
                if (!array_key_exists($fname, $fields)) {
                    continue; // filter not used
                }
                $field = $fields[$fname];
                $description = $field->get_label($data);
                $latestfilterhtml .= '<tr><td>'.$latestfilterlabel.'</td><th align="left">'.$description.'</th></tr>';
                $latestfilterlabel = ' ';
            }
            $latestfilterhtml .= '</table>';
            $mform->addElement('html', $latestfilterhtml);
        }
// (Shirai131): ここまで追加

        // Don't use last advanced state
        $mform->setShowAdvanced(false);
    }
}

class user_active_filter_form extends moodleform {

    function definition() {
        global $SESSION; // this is very hacky :-(

        $mform       =& $this->_form;
        $fields      = $this->_customdata['fields'];
        $extraparams = $this->_customdata['extraparams'];

        if (!empty($SESSION->user_filtering)) {
            // add controls for each active filter in the active filters group
            $mform->addElement('header', 'actfilterhdr', get_string('actfilterhdr','filters'));

            foreach ($SESSION->user_filtering as $fname=>$datas) {
                if (!array_key_exists($fname, $fields)) {
                    continue; // filter not used
                }
                $field = $fields[$fname];
                foreach($datas as $i=>$data) {
                    $description = $field->get_label($data);
                    $mform->addElement('checkbox', 'filter['.$fname.']['.$i.']', null, $description);
                }
            }

            if ($extraparams) {
                foreach ($extraparams as $key=>$value) {
                    $mform->addElement('hidden', $key, $value);
                    $mform->setType($key, PARAM_RAW);
                }
            }

// (Shirai131): ここからコメントアウト
/*
            $objs = array();
            $objs[] = &$mform->createElement('submit', 'removeselected', get_string('removeselected','filters'));
            $objs[] = &$mform->createElement('submit', 'removeall', get_string('removeall','filters'));
            $mform->addElement('group', 'actfiltergrp', '', $objs, ' ', false);
*/
// (Shirai131): ここから追加
            $mform->addElement('submit', 'removeselected', get_string('removeselected','filters'));
// (Shirai131): ここまで追加
        }
    }
}
