<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// 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/>.

/**
 * Local plugin "Boost navigation fumbling" - Settings
 *
 * @package    local_chronotable
 * @copyright  2017 Alexander Bias, Ulm University <alexander.bias@uni-ulm.de>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

use local_treestudyplan\aggregator;

if ($hassiteconfig) {

	$taxonomies = core_competency\competency_framework::get_taxonomies_list();

	// Create admin settings category.
	$ADMIN->add('courses', new admin_category('local_treestudyplan',
			get_string('pluginname', 'local_treestudyplan', null, true)));

	// Settings page: Root nodes.
	$page = new admin_settingpage('local_treestudyplan_settings',
			get_string('settingspage', 'local_treestudyplan', null, true));


	// GOAL AGGREGATION SETTINGS
	$page->add(new admin_setting_heading('local_treestudyplan/aggregation_heading',
		get_string('setting_aggregation_heading', 'local_treestudyplan'),
		get_string('settingdesc_aggregation_heading', 'local_treestudyplan')
	));

	$aggregators = [];
	foreach(aggregator::list() as $a){
		$aggregators[$a] = get_string("{$a}_aggregator_title",'local_treestudyplan', null, true);
	}

	$page->add(new admin_setting_configselect('local_treestudyplan/aggregation_mode',
			get_string('setting_aggregation_mode', 'local_treestudyplan'),
			get_string('settingdesc_aggregation_mode', 'local_treestudyplan'),
			"bistate",
			$aggregators
		));

	// DISPLAY COURSE INFO SETTINGS
	$page->add(new admin_setting_heading('local_treestudyplan/display_heading',
		get_string('setting_display_heading', 'local_treestudyplan'),
		get_string('settingdesc_display_heading', 'local_treestudyplan')
	));

	$displayfields = ["shortname" => get_string("shortname"), "idnumber" => get_string("idnumber")];
	$handler = \core_customfield\handler::get_handler('core_course', 'course');

	foreach($handler->get_categories_with_fields() as $cat){
		$catname = $cat->get_formatted_name();
		foreach($cat->get_fields() as $field) {
			$fieldname = $field->get_formatted_name();
			$fieldid = $field->get("shortname");
			$displayfields["customfield_".$fieldid] = $catname.": ".$fieldname;
		}
	}

	$page->add(new admin_setting_configselect('local_treestudyplan/display_field',
			get_string('setting_display_field', 'local_treestudyplan'),
			get_string('settingdesc_display_field', 'local_treestudyplan'),
			"shortname",
			$displayfields
		));

	// BISTATE AGGREGATON DEFAULTS
	$page->add(new admin_setting_heading('local_treestudyplan/bistate_aggregation_heading',
		get_string('setting_bistate_heading', 'local_treestudyplan'),
		get_string('settingdesc_bistate_heading', 'local_treestudyplan')
	));

	$page->add(new admin_setting_configtext('local_treestudyplan/bistate_thresh_excellent',
		get_string('setting_bistate_thresh_excellent', 'local_treestudyplan'),
		get_string('settingdesc_bistate_thresh_excellent', 'local_treestudyplan'),
		"100",
		PARAM_INT
	));

	$page->add(new admin_setting_configtext('local_treestudyplan/bistate_thresh_good',
		get_string('setting_bistate_thresh_good', 'local_treestudyplan'),
		get_string('settingdesc_bistate_thresh_good', 'local_treestudyplan'),
		"80",
		PARAM_INT
	));

	$page->add(new admin_setting_configtext('local_treestudyplan/bistate_thresh_completed',
		get_string('setting_bistate_thresh_completed', 'local_treestudyplan'),
		get_string('settingdesc_bistate_thresh_completed', 'local_treestudyplan'),
		"66",
		PARAM_INT
	));


	$page->add(new admin_setting_configcheckbox('local_treestudyplan/bistate_support_failed',
		get_string('setting_bistate_support_failed', 'local_treestudyplan'),
		get_string('settingdesc_bistate_support_failed', 'local_treestudyplan'),
		True,
	));

	$page->add(new admin_setting_configtext('local_treestudyplan/bistate_thresh_progress',
		get_string('setting_bistate_thresh_progress', 'local_treestudyplan'),
		get_string('settingdesc_bistate_thresh_progress', 'local_treestudyplan'),
		"33",
		PARAM_INT
	));

	$page->add(new admin_setting_configcheckbox('local_treestudyplan/bistate_accept_pending_submitted',
		get_string('setting_bistate_accept_pending_submitted', 'local_treestudyplan'),
		get_string('settingdesc_bistate_accept_pending_submitted', 'local_treestudyplan'),
		False,
	));

	// Add settings page to the admin settings category.
	$ADMIN->add('local_treestudyplan', $page);
	// Cohort config page
	$ADMIN->add('local_treestudyplan', new admin_externalpage(
		'local_treestudyplan_editplans',
		get_string('cfg_plans', 'local_treestudyplan', null, true),
		$CFG->wwwroot . '/local/treestudyplan/edit-plan.php'));
	// Cohort config page
	$ADMIN->add('local_treestudyplan', new admin_externalpage(
		'local_treestudyplan_gradeconfig',
		get_string('cfg_grades', 'local_treestudyplan', null, true),
		$CFG->wwwroot . '/local/treestudyplan/cfg_grades.php'));

}