<?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) {

    /**************************************
     *
     * Main studyplan settings
     *
     *************************************/

    // 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'),
            "core",
            $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_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);

    /**************************************
     *
     * Manage plans link (systemwide)
     *
     *************************************/

    $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'));


    /**************************************
     *
     * Settings page: Cohort sync
     *
     *************************************/
    
    $pagecsync = new admin_settingpage('local_treestudyplan_settings_cohortsync',
        get_string('settingspage_csync', 'local_treestudyplan', null, true));


    // Description heading.
    $pagecsync->add(new admin_setting_heading('local_treestudyplan/csync_heading',
        get_string('setting_csync_heading', 'local_treestudyplan'),
        get_string('settingdesc_csync_heading', 'local_treestudyplan')
    ));

    // Enable/disable cohort sync.
    $pagecsync->add(new admin_setting_configcheckbox('local_treestudyplan/csync_enable',
        get_string('setting_csync_enable_field', 'local_treestudyplan'),
        get_string('settingdesc_csync_enable_field', 'local_treestudyplan'),
        false
    ));

    // Enable/disable autoremove.
    $pagecsync->add(new admin_setting_configcheckbox('local_treestudyplan/csync_autoremove',
        get_string('setting_csync_autoremove_field', 'local_treestudyplan'),
        get_string('settingdesc_csync_autoremove_field', 'local_treestudyplan'),
        true
    ));

    // Enable/disable remembering previously added cohort syncs so they're not automatically deleted.
    $pagecsync->add(new admin_setting_configcheckbox('local_treestudyplan/csync_remember_manual_csync',
        get_string('setting_csync_remember_manual_csync_field', 'local_treestudyplan'),
        get_string('settingdesc_csync_remember_manual_csync_field', 'local_treestudyplan'),
        true
    ));

    // Enable/disable group creation.
    $pagecsync->add(new admin_setting_configcheckbox('local_treestudyplan/csync_creategroup',
        get_string('setting_csync_creategroup_field', 'local_treestudyplan'),
        get_string('settingdesc_csync_creategroup_field', 'local_treestudyplan'),
        true
    ));

    // Sync users too yes/no?.
    $pagecsync->add(new admin_setting_configcheckbox('local_treestudyplan/csync_users',
        get_string('setting_csync_users_field', 'local_treestudyplan'),
        get_string('settingdesc_csync_users_field', 'local_treestudyplan'),
        true
    ));

    // Select csync enrol role.
    if (!during_initial_install()) {
        $options = get_default_enrol_roles(context_system::instance());
        $student = get_archetype_roles('student');
        $student = reset($student);
        $pagecsync->add(new admin_setting_configselect('local_treestudyplan/csync_roleid',
        get_string('setting_csync_role_field', 'local_treestudyplan'),
        get_string('settingdesc_csync_role_field', 'local_treestudyplan'),
        $student->id ?? null, $options));
    }

    // Add settings page2 to the admin settings category.
    $ADMIN->add('local_treestudyplan', $pagecsync);

    /**************************************
     *
     * Grade and scale interpretation
     *
     *************************************/
    $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'));

    /**************************************
     *
     * Add the help link (Temporary until a better place is found)
     *
     **************************************/

    $ADMIN->add('local_treestudyplan', new admin_externalpage(
        'local_treestudyplan_doc',
        get_string('cfg_help', 'local_treestudyplan', null, true),
        $CFG->wwwroot . '/local/treestudyplan/doc.php/index.htm'));

}
