// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
/**
 * Display the bulk email directoy.
 *
 * @package    local_bulk_email_directory
 * @copyright  Anthony Kuske <www.anthonykuske.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require dirname(dirname(dirname(__FILE__))) . '/config.php';
$directory = new local_bulk_email_directory\local\directory();
/**
 * Setup page
 */
$PAGE->set_context(context_system::instance());
$PAGE->set_url('/local/bulk_email_directory/');
$PAGE->set_title(get_string('pagetitle', 'local_bulk_email_directory'));
$PAGE->set_heading(get_string('pageheading', 'local_bulk_email_directory'));
$PAGE->requires->jquery();
$PAGE->requires->jquery_plugin('ui');
$PAGE->requires->jquery_plugin('ui-css');
$PAGE->requires->js('/local/bulk_email_directory/assets/js/js.js');
$PAGE->requires->css('/local/bulk_email_directory/assets/css/style.css?v=2015092300');
echo $OUTPUT->header();
$list = optional_param('list', false, PARAM_RAW);
$email = optional_param('email', false, PARAM_RAW);
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
/**
 * Provides data for the jQuery UI autocomplete dropdowns.
 *
 * @package    local_bulk_email_directory
 * @copyright  Anthony Kuske <www.anthonykuske.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php';
$directory = new local_bulk_email_directory\local\directory();
$type = required_param('type', PARAM_RAW);
$term = required_param('term', PARAM_RAW);
header('Content-Type: application/json');
if ($type === 'list') {
    $data = $directory->search_lists($term, true);
    echo json_encode($data);
} else {
    if ($type === 'email') {
        $data = $directory->search_emails($term);
        echo json_encode($data);
    }
}