<?php
namespace local_treestudyplan;
use local_treestudyplan\local\helpers\webservicehelper;

require_once($CFG->libdir.'/externallib.php');
require_once($CFG->libdir.'/badgeslib.php');
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->dirroot.'/course/modlib.php');



class studyplanservice extends \external_api 
{

    const CAP_EDIT = "local/treestudyplan:editstudyplan";
    const CAP_VIEW = "local/treestudyplan:viewuserreports";

    
    /************************
     *                      *
     * list_studyplans      *
     *                      *
     ************************/

    public static function list_studyplans_parameters()
    {
        return new \external_function_parameters([
            "context_id" => new \external_value(PARAM_INT, 'context to search in for studyplans', VALUE_DEFAULT),
        ]);
    }

    public static function list_studyplans_returns() 
    {
        return new \external_multiple_structure( studyplan::simple_structure() );
    }

    public static function list_studyplans($context_id = 0){
        global $CFG, $DB;

        // Check if the user has the correct permissions for this context
        $context = webservicehelper::find_context($context_id);
        webservicehelper::require_capabilities([self::CAP_EDIT,self::CAP_VIEW],$context);

        // Now list all the studyplans in the relevant context
        $list = [];
        $studyplans = studyplan::find_all($context_id);
        foreach($studyplans as $studyplan)
        {
            $list[] = $studyplan->simple_model();
        }
        return $list;

    }

    /************************
     *                      *
     * get_studyplan_map    *
     *                      *
     ************************/

    public static function get_studyplan_map_parameters() 
    {
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of a studyplan to check usage on', VALUE_REQUIRED),
        ] );
    }

    public static function get_studyplan_map_returns() 
    {
        return  studyplan::editor_structure();
    }

    public static function get_studyplan_map($id)
    {
        if(isset($id) && $id > 0){
            $studyplan = studyplan::findById($id);
            webservicehelper::require_capabilities([self::CAP_EDIT,self::CAP_VIEW],$studyplan->context());
            return $studyplan->editor_model();
        }
        else {
            return null;
        }
    }

    /************************
     *                      *
     * get_studyline_map    *
     *                      *
     ************************/

    public static function get_studyline_map_parameters() 
    {
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of a studyline to check usage on', VALUE_DEFAULT),
        ]);
    }

    public static function get_studyline_map_returns() 
    {
        return new \external_multiple_structure( studyline::editor_structure() );
    }

    public static function get_studyline_map($id)
    {
        
        $o = studyline::findById($id);
        webservicehelper::require_capabilities([self::CAP_EDIT,self::CAP_VIEW],$o->context());
        return $o->editor_model();
    }

    /************************
     *                      *
     * add_studyplan        *
     *                      *
     ************************/

    public static function add_studyplan_parameters() 
    {
        return new \external_function_parameters( [
            "name" => new \external_value(PARAM_TEXT, 'name of studyplan'),
            "shortname"=> new \external_value(PARAM_TEXT, 'shortname of studyplan'),
            "description"=> new \external_value(PARAM_TEXT, 'description of studyplan'),
            "slots" => new \external_value(PARAM_INT, 'number of slots in studyplan'),
            "startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan'),
            "enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan'),
            "aggregation" => new \external_value(PARAM_TEXT, 'selected aggregator',VALUE_DEFAULT),
            "aggregation_config" => new \external_value(PARAM_TEXT, 'config string for aggregator',VALUE_DEFAULT),
            "context_id" => new \external_value(PARAM_INT, 'context of the study plan',VALUE_DEFAULT),
        ] );
    }

    public static function add_studyplan_returns() 
    {
        return studyplan::simple_structure();
    }

    public static function add_studyplan($name, $shortname,$description, $slots, $startdate, $enddate, $aggregation="bistate", $aggregation_config='',$context_id=0)
    {
        // Check if we have the proper rights for the requested context
        $context = webservicehelper::find_context($context_id);
        webservicehelper::require_capabilities(self::CAP_EDIT,$context);

        $o = studyplan::add([
            'name' => $name,
            'shortname' => $shortname,
            'description' => $description,
            'slots' => $slots,
            'startdate' => $startdate,
            'enddate' => empty($enddate)?null:$enddate,
            'aggregation' => $aggregation,
            'aggregation_config' => $aggregation_config,
            'context_id' => $context_id,
        ]);
        return $o->simple_model();
    }
    
    /************************
     *                      *
     * edit_studyplan       *
     *                      *
     ************************/

    public static function edit_studyplan_parameters() 
    {
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of studyplan'),
            "name" => new \external_value(PARAM_TEXT, 'name of studyplan'),
            "shortname"=> new \external_value(PARAM_TEXT, 'shortname of studyplan'),
            "description"=> new \external_value(PARAM_TEXT, 'description of studyplan'),
            "slots" => new \external_value(PARAM_INT, 'number of slots in studyplan'),
            "startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan'),
            "enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan'),
            "aggregation" => new \external_value(PARAM_TEXT, 'selected aggregator',VALUE_DEFAULT),
            "aggregation_config" => new \external_value(PARAM_TEXT, 'config string for aggregator',VALUE_DEFAULT),
            "context_id" => new \external_value(PARAM_INT, 'context of the study plan',VALUE_DEFAULT),
        ] );
    }

    public static function edit_studyplan_returns() 
    {
        return studyplan::simple_structure();
    }
 
    public static function edit_studyplan($id, $name, $shortname, $description, $slots, $startdate, $enddate, $aggregation="bistate", $aggregation_config='',$context_id=0)
    {
        // Validate access in the intended context
        $context = webservicehelper::find_context($context_id);
        webservicehelper::require_capabilities(self::CAP_EDIT,$context,false); // do not validate the context in this case, just check the permissions

        $o = studyplan::findById($id);
        webservicehelper::require_capabilities(self::CAP_EDIT,$o->context());

        $o->edit([
            'name' => $name,
            'shortname' => $shortname,
            'description' => $description,
            'slots' => $slots,
            'startdate' => $startdate,
            'enddate' => $enddate,
            'aggregation' => $aggregation,
            'aggregation_config' => $aggregation_config,
            'context_id' => $context_id,
        ]);
        return $o->simple_model();
    }

    /************************
     *                      *
     * delete_studyplan     *
     *                      *
     ************************/

    public static function delete_studyplan_parameters() 
    {
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of studyplan'),
            "force" => new \external_value(PARAM_BOOL, 'id of studyplan', VALUE_DEFAULT),
        ] );
    }

    public static function delete_studyplan_returns() 
    {
        return success::structure();
    }
 
    public static function delete_studyplan($id,$force=false)
    {
        $o = studyplan::findById($id);
        // validate if the requesting user has the right to edit the plan in it's current context
        webservicehelper::require_capabilities(self::CAP_EDIT,$o->context());
        return $o->delete(!!$force)->model();
    }

    /************************
     *                      *
     * add_studyline        *
     *                      *
     ************************/

    public static function add_studyline_parameters() 
    {
        return new \external_function_parameters( [
            "studyplan_id" => new \external_value(PARAM_INT, 'id of studyplan to add line to'),
            "name" => new \external_value(PARAM_TEXT, 'shortname of studyline'),
            "shortname"=> new \external_value(PARAM_TEXT, 'idnumber of studyline'),
            "color"=> new \external_value(PARAM_TEXT, 'description of studyline'),
            "sequence" => new \external_value(PARAM_INT, 'sequence of studyline'),
        ] );
    }

    public static function add_studyline_returns() 
    {
        return studyline::editor_structure();
    }
 
    public static function add_studyline($studyplan_id, $name, $shortname,$color,$sequence)
    {
        // validate if the requesting user has the right to edit the plan in it's current context
        $studyplan = studyplan::findById($studyplan_id);
        webservicehelper::require_capabilities(self::CAP_EDIT,$studyplan->context());

        $o = studyline::add([
            'studyplan_id' => $studyplan_id,
            'name' => $name,
            'shortname' => $shortname,
            'color' => $color,
            'sequence' => $sequence,
        ]);
        return $o->editor_model();
    }

    /************************
     *                      *
     * edit_studyline       *
     *                      *
     ************************/

    public static function edit_studyline_parameters() 
    {
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of studyline'),
            "name" => new \external_value(PARAM_TEXT, 'shortname of studyline'),
            "shortname"=> new \external_value(PARAM_TEXT, 'idnumber of studyline'),
            "color"=> new \external_value(PARAM_TEXT, 'description of studyline'),
        ] );
    }

    public static function edit_studyline_returns() 
    {
        return studyline::editor_structure();
    }
 
    public static function edit_studyline($id, $name, $shortname, $color)
    {
        $o = studyline::findById($id);
        // validate if the requesting user has the right to edit the plan in it's current context
        webservicehelper::require_capabilities(self::CAP_EDIT,$o->context());

        $o->edit([
            'name' => $name,
            'shortname' => $shortname,
            'color' => $color,
        ]);
        return $o->editor_model();
    }

 

    /************************
     *                      *
     * delete_studyline     *
     *                      *
     ************************/

    public static function delete_studyline_parameters() 
    {
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of studyline'),
        ] );
    }

    public static function delete_studyline_returns() 
    {
        return success::structure();
    }
 
    public static function delete_studyline($id)
    {
        $o = studyline::findById($id);
        // validate if the requesting user has the right to edit the plan in it's current context
        webservicehelper::require_capabilities(self::CAP_EDIT,$o->context());
        return $o->delete()->model();

    }

    /************************
     *                      *
     * reorder_studylines   *
     *                      *
     ************************/

    public static function reorder_studylines_parameters() 
    {
        return new \external_function_parameters( [
            "sequence" => new \external_multiple_structure(
                new \external_single_structure([
                    "id" => new \external_value(PARAM_INT, 'id of studyline '),
                    "sequence" => new \external_value(PARAM_INT, 'order of studyline'),
                ]),
            ),
        ] );
    }

    public static function reorder_studylines_returns() 
    {
        return success::structure();
    }
 
    public static function reorder_studylines($resequence)
    {
        // validate if the requesting user has the right to edit the lines in it's current context
        foreach($resequence as $sq){
            $o = studyline::findById(($sq['id']));
            webservicehelper::require_capabilities(self::CAP_EDIT,$o->context());
        }

        return studyline::reorder($resequence)->model();
    }
 
/***********************************
 * STUDYITEM FUNCTIONS
 ***********************************/

    /************************
     *                      *
     * get_studyitem    *
     *                      *
     ************************/

    public static function get_studyitem_parameters() 
    {
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of a study item to retrieve'),
        ] );
    }

    public static function get_studyitem_returns() 
    {
        return studyitem::editor_structure();
    }

     
    public static function get_studyitem($id)
    {
        $o = studyitem::findById($id);
        webservicehelper::require_capabilities([self::CAP_EDIT,self::CAP_VIEW],$o->context());
        return $o->editor_model();

    }
    
    /************************
     *                      *
     * add_studyitem        *
     *                      *
     ************************/

    public static function add_studyitem_parameters() 
    {
        return new \external_function_parameters( [
            "line_id" => new \external_value(PARAM_INT, 'id of related study line'),
            "type" => new \external_value(PARAM_TEXT, 'type of study item'),
            "details" => new \external_single_structure([
                "conditions"=> new \external_value(PARAM_TEXT, 'conditions for completion',VALUE_OPTIONAL),
                "competency_id" => new \external_value(PARAM_INT, 'id of referenced competency',VALUE_OPTIONAL),
                "course_id" => new \external_value(PARAM_INT, 'id of referenced course',VALUE_OPTIONAL),
                "badge_id" => new \external_value(PARAM_INT, 'id of referenced badge',VALUE_OPTIONAL),
                "continuation_id" => new \external_value(PARAM_INT, 'id of continued item',VALUE_OPTIONAL),
            ]),
            "slot" => new \external_value(PARAM_INT, 'slot in the study plan',VALUE_DEFAULT),
            "layer" => new \external_value(PARAM_INT, 'layer in the slot',VALUE_OPTIONAL),
        ] );
    }

    public static function add_studyitem_returns() 
    {
        return studyitem::editor_structure();
    }

    public static function add_studyitem($line_id,$type,$details,$slot=-1,$layer=0)
    {
        webservicehelper::require_capabilities(self::CAP_EDIT,studyline::findById($line_id)->context());

        $o = studyitem::add([
            'line_id' => $line_id,
            'type' => $type,
            //'conditions' => $conditions,
            'slot' => $slot,
            'layer' => $layer,
            'competency_id' => isset($details['competency_id'])?$details['competency_id']:null,
            'course_id' => isset($details['course_id'])?$details['course_id']:null,
            'badge_id' => isset($details['badge_id'])?$details['badge_id']:null,
            'continuation_id' => isset($details['continuation_id'])?$details['continuation_id']:null,
        ]);
        return $o->editor_model();
    }
    
    /************************
     *                      *
     * edit_studyitem       *
     *                      *
     ************************/

    public static function edit_studyitem_parameters() 
    {
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of study item'),
            "conditions"=> new \external_value(PARAM_TEXT, 'conditions for completion'),
            "continuation_id" => new \external_value(PARAM_INT, 'id of continued item',VALUE_DEFAULT),
        ]);
    }

    public static function edit_studyitem_returns() 
    {
        return studyitem::editor_structure();
    }
 
    public static function edit_studyitem($id,$conditions,$continuation_id=false)
    {
      
        $o = studyitem::findById($id);
        webservicehelper::require_capabilities(self::CAP_EDIT,$o->context());

        $config = [
            'conditions' => $conditions,
        ];
        if($continuation_id !== false) {
            $config['continuation_id'] = $continuation_id;
        }

        $o->edit($config);
        return $o->editor_model();

    }

    /************************
     *                      *
     * reorder_studyitems   *
     *                      *
     ************************/

    public static function reorder_studyitems_parameters() 
    {
        return new \external_function_parameters( [
            "items" => new \external_multiple_structure(
                new \external_single_structure([
                    "id" => new \external_value(PARAM_INT, 'id of studyitem '),
                    "line_id" => new \external_value(PARAM_INT, 'id of related study line'),
                    "slot" => new \external_value(PARAM_INT, 'slot in the study plan'),
                    "layer" => new \external_value(PARAM_INT, 'layer in the slot'),
                ]),
            ),
        ] );
    }

    public static function reorder_studyitems_returns() 
    {
        return success::structure();
    }
 
    public static function reorder_studyitems($resequence)
    {
        // Check for permissions to modify the studyplan
        foreach($resequence as $sq){
            webservicehelper::require_capabilities(self::CAP_EDIT,studyitem::findById(($sq['id']))->context());
        }

        return studyitem::reorder($resequence)->model();
    }


    /************************
     *                      *
     * delete_studyitem     *
     *                      *
     ************************/

    public static function delete_studyitem_parameters() 
    {
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of studyitem'),
        ] );
    }

    public static function delete_studyitem_returns() 
    {
        return success::structure();
    }
 
    public static function delete_studyitem($id)
    {
        $o = studyitem::findById($id);
        webservicehelper::require_capabilities(self::CAP_EDIT,$o->context());

        return $o->delete()->model();
    }

    /************************
     *                      *
     * connect_studyitems   *
     *                      *
     ************************/

    public static function connect_studyitems_parameters() 
    {
        return new \external_function_parameters( [
            "from_id" => new \external_value(PARAM_INT, 'id of studyitem connect start '),
            "to_id" => new \external_value(PARAM_INT, 'id ofstudyitem connect end'),
        ] );
    }

    public static function connect_studyitems_returns() 
    {
        return studyitemconnection::structure();
    }
 
    public static function connect_studyitems($from_id,$to_id)
    {
        // Validate permissions
        webservicehelper::require_capabilities(self::CAP_EDIT,studyitem::findById($from_id)->context());
        webservicehelper::require_capabilities(self::CAP_EDIT,studyitem::findById($to_id)->context());

        $o = studyitemconnection::connect($from_id,$to_id);
        return $o->model();
    }

    /****************************
     *                          *
     * disconnect_studyitems    *
     *                          *
     ****************************/

    public static function disconnect_studyitems_parameters() 
    {
        return new \external_function_parameters( [
            "from_id" => new \external_value(PARAM_INT, 'id of studyitem '),
            "to_id" => new \external_value(PARAM_INT, 'id of related study line'),
        ] );
    }

    public static function disconnect_studyitems_returns() 
    {
        return success::structure();
    }
 
    public static function disconnect_studyitems($from_id,$to_id)
    {
        // Validate permissions
        webservicehelper::require_capabilities(self::CAP_EDIT,studyitem::findById($from_id)->context());
        webservicehelper::require_capabilities(self::CAP_EDIT,studyitem::findById($to_id)->context());
        return studyitemconnection::disconnect($from_id,$to_id)->model();
    }    

    /****************************
     *                          *
     * list badges              *
     *                          *
     ****************************/

    public static function list_badges_parameters() 
    {
        return new \external_function_parameters( [] );
    }

    public static function list_badges_returns() 
    {
        return new \external_multiple_structure(badgeinfo::editor_structure());
    }
 
    public static function list_badges()
    {
        $systemcontext = webservicehelper::system_context();

        $result = [];
        $badges = badges_get_badges(BADGE_TYPE_SITE,"timemodified");
        foreach ($badges as $badge) {
// TODO: Add config option to list only active badges
//            if($badge->is_active()){
                $result[] = (new badgeinfo($badge))->editor_model();
//            }

//TODO: Include course badges somehow... Just site badges is not enough

        }        
        return $result;

    }    

    /****************************
     *                          *
     * include/remove grades    *
     *                          *
     ****************************/

    public static function include_grade_parameters() 
    {
        return new \external_function_parameters( [
            "grade_id" => new \external_value(PARAM_INT, 'id of gradeitem '),
            "item_id" => new \external_value(PARAM_INT, 'id of studyitem '),
            "include" => new \external_value(PARAM_BOOL, 'include or not '),
            "required" => new \external_value(PARAM_BOOL, 'required grade or not', VALUE_DEFAULT),
            ] );
    }

    public static function include_grade_returns() 
    {
        return success::structure();
    }
 
    public static function include_grade($grade_id,$item_id,$include,$required=false)
    {
        // TODO: DEPRECATE THIS WHEN IMPLEMENTING THE CORE COMPLETION STRATEGY
        global $USER;

        // find related course and course context
        $coursecontext = gradeinfo::getCourseContextById($grade_id);
        // do sanity checks
        \external_api::validate_context($coursecontext);


        // check correct capabilities
        if(has_capability('local/treestudyplan:editstudyplan', studyitem::findById($item_id)->context()) ||
           is_enrolled($coursecontext, $USER, 'local/treestudyplan:selectowngradables')){
            return gradeinfo::include_grade($grade_id,$item_id,$include,$required)->model();
        }
        else {
            return success::fail("Access denied")->model();
        }

    }    

    /****************************
     *                          *
     * list aggregators         *
     *                          *
     ****************************/

    public static function list_aggregators_parameters() 
    {
        return new \external_function_parameters([]);
    }

    public static function list_aggregators_returns() 
    {
        return aggregator::list_structure();
    }
 
    public static function list_aggregators()
    {
        return aggregator::list_model();

    }    

    /****************************
     *                          *
     * force_studyplan_scale    *
     *                          *
     ****************************/
    
    public static function force_studyplan_scale_parameters() 
    {
        return new \external_function_parameters( [
            "studyplan_id" => new \external_value(PARAM_INT, 'id of studyplan'),
            "scale_id" => new \external_value(PARAM_INT, 'scale_id to set'),
        ] );
    }

    public static function force_studyplan_scale_returns() 
    {
        return new \external_multiple_structure(new \external_single_structure([
            "course" => courseinfo::simple_structure(),
            "grades" => new \external_multiple_structure(new \external_single_structure([
                "name" => new \external_value(PARAM_TEXT, 'grade name'),
                "changed" => new \external_value(PARAM_TEXT, 'changed or not'),
                "debug" => new \external_value(PARAM_TEXT, 'debug',VALUE_OPTIONAL),
            ])),
        ]));
    }
 
    public static function force_studyplan_scale($studyplan_id, $scale_id)
    {
        global $DB;
        $dbman = $DB->get_manager();

        // Validate permissions
        webservicehelper::require_capabilities(self::CAP_EDIT,studyplan::findById($studyplan_id)->context());

        $list = [];
        // check if scaleid is valid
        $scale = \grade_scale::fetch(['id' => $scale_id]);
        $scale->load_items();

        if($scale){
            $gradecfg = $DB->get_record("local_treestudyplan_gradecfg",["scale_id"=>$scale->id]);
            $scalepass = ($gradecfg)?$gradecfg->min_completed:0;
            $scalemax = count($scale->scale_items);

            // find studyline id's
            $studyline_ids = $DB->get_fieldset_select(studyline::TABLE,"id","studyplan_id = :plan_id",['plan_id' => $studyplan_id]);
            foreach($studyline_ids as $studyline_id) {
                // find id's of studyitems of type course
                $records = $DB->get_records(studyitem::TABLE,['line_id' => $studyline_id]);

                foreach($records as $item_r){
                    $studyitem = new studyitem($item_r->id);
                    if($studyitem->isValid() && $studyitem->type() == studyitem::COURSE){
                        $courseinfo = $studyitem->getcourseinfo();
                        $gradables = gradeinfo::list_studyitem_gradables($studyitem);
                        
                        $gradelist = [];
                        foreach($gradables as $g){
                            $gi = $g->getGradeItem();
                            
                            // only change items that do not yet have grades
                            // otherwise we will need to implement grade recalculations and it is not worth the trouble. 
                            // if grades are given, you likely don't want to change it like this anyway.
                            
                            if(!$gi->has_grades()){
                                $gi->gradetype = GRADE_TYPE_SCALE;
                                $gi->scaleid = $scale->id;
                                $gi->grademin = 1;
                                $gi->grademax = $scalemax;
                                $gi->gradepass = $scalepass; 

                                // Update grade_item
                                $result = $gi->update("local/treestudyplan"); // update, signalling with our signature and bulkupdate
                                
                                $debug = "";
                                if($result){ $updated = "converted";}
                                else { $updated = "error";}

                                // next update the activity's table if it has a grade field
                                // grade is generally set to the negative scale id if it is a scale.
                                $tablename = $gi->itemmodule;
                                $fieldname = "grade";
                                if($result && $gi->itemtype == "mod" && $dbman->table_exists($tablename)){
                                    if($dbman->field_exists($tablename,$fieldname)){
                                        $grade_value = intval(0-($scale->id));
                                        try {
                                            $DB->set_field($tablename,$fieldname,$grade_value, ["id" => $gi->iteminstance]);
                                        } 
                                        catch(\dml_exception $x){
                                            $updated = "fail";
                                            $debug = strval($x);
                                        }
                                    }
                                }
                                 
                            }
                            else { 
                                $updated = "skipped";
                            }

                            $gradelist[] = [
                                'name' => $gi->itemname,
                                'changed' => $updated,
                                'debug' => $debug,
                            ];
                        }

                        $list[] = [
                            'course' => $courseinfo->simple_model(),
                            'grades' => $gradelist,
                        ];
                    }
                }
            }
        }

        return $list;

    }    
    
    /****************************
     *                          *
     * list_scales              *
     *                          *
     ****************************/

    public static function list_scales_parameters() 
    {
        return new \external_function_parameters( [] );
    }

    public static function list_scales_returns() 
    {
        return new \external_multiple_structure(new \external_single_structure([
            "id" => new \external_value(PARAM_INT, 'id of scale'),
            "name" => new \external_value(PARAM_TEXT, 'scale name'),
        ]));
    }
 
    public static function list_scales()
    {
        global $DB;
        $list = [];
        $scales = \grade_scale::fetch_all_global();

        foreach($scales as $scale){
            $list[] = [
                "id" => $scale->id,
                "name" => $scale->name,
            ];
        }

        return $list;

    }      

  /****************************
     *                          *
     * disable_autoenddate    *
     *                          *
     ****************************/
    
    public static function disable_autoenddate_parameters() 
    {
        return new \external_function_parameters( [
            "studyplan_id" => new \external_value(PARAM_INT, 'id of studyplan'),
        ] );
    }

    public static function disable_autoenddate_returns() 
    {
        return success::structure();
    }
 
    public static function disable_autoenddate($studyplan_id)
    {
        global $DB;

        // Validate permissions
        webservicehelper::require_capabilities(self::CAP_EDIT,studyplan::findById($studyplan_id)->context());

        // find studyline id's
        $studyline_ids = $DB->get_fieldset_select(studyline::TABLE,"id","studyplan_id = :plan_id",['plan_id' => $studyplan_id]);
        foreach($studyline_ids as $studyline_id) {
            // find id's of studyitems of type course
            $records = $DB->get_records(studyitem::TABLE,['line_id' => $studyline_id]);

            foreach($records as $item_r){
                $studyitem = new studyitem($item_r->id);
                if($studyitem->isValid() && $studyitem->type() == studyitem::COURSE){
                    $record = $DB->get_record("course_format_options",["courseid" => $studyitem->courseid(), "name" => "automaticenddate"]);
                    if($record && $record->value){
                        $record->value = false;
                        $DB->update_record("course_format_options",$record);
                    }
                }
            }
        }

        return success::success()->model();
    }    
    /****************************
     *                          *
     * duplicate studyplan      *
     *                          *
     ****************************/

    public static function duplicate_plan_parameters() 
    {
        return new \external_function_parameters( [
            "plan_id" => new \external_value(PARAM_INT, 'id of plan to copy '),
            "name" => new \external_value(PARAM_TEXT, 'name of copy '),
            "shortname" => new \external_value(PARAM_TEXT, 'shortname of copy '),
            ] );
    }

    public static function duplicate_plan_returns() 
    {
        return studyplan::simple_structure();
    }
 
    public static function duplicate_plan($studyplan_id,$name,$shortname)
    {
        // Validate permissions
        webservicehelper::require_capabilities(self::CAP_EDIT,studyplan::findById($studyplan_id)->context());

        return studyplan::duplicate_plan($studyplan_id,$name,$shortname);
    }   

    /****************************
     *                          *
     * export studyplan         *
     *                          *
     ****************************/

    public static function export_plan_parameters() 
    {
        return new \external_function_parameters( [
            "studyplan_id" => new \external_value(PARAM_INT, 'id of plan to export '),
            "format" => new \external_value(PARAM_TEXT,'export format', VALUE_OPTIONAL),
            ] );
    }

    public static function export_plan_returns() 
    {
        return studyplan::export_structure();
    }
 
    public static function export_plan($studyplan_id, $format="json")
    {
        try{
            // Validate permissions
            webservicehelper::require_capabilities(self::CAP_EDIT,studyplan::findById($studyplan_id)->context());

            $plan = studyplan::findById($studyplan_id);
            if($format == "csv"){
                return $plan->export_plan_csv();
            }
            else{
                return $plan->export_plan();
            }
        }
        catch(\webservice_access_exception $x) {
            return [ "format" => "", "content" => ""];
        }
    }   

    public static function export_studylines_parameters() 
    {
        return new \external_function_parameters( [
            "studyplan_id" => new \external_value(PARAM_INT, 'id of plan to export '),
            ] );
    }

    public static function export_studylines_returns() 
    {
        return studyplan::export_structure();
    }
 
    public static function export_studylines($studyplan_id)
    {
        $systemcontext = webservicehelper::system_context();

        try{
            webservicehelper::require_capabilities(self::CAP_EDIT,studyplan::findById($studyplan_id)->context());
            $plan = studyplan::findById($studyplan_id);
            return $plan->export_studylines();
        }
        catch(\webservice_access_exception $x) {
            return [ "format" => "", "content" => ""];
        }
    }   
    /****************************
     *                          *
     * import studyplan         *
     *                          *
     ****************************/

    public static function import_plan_parameters() 
    {
        return new \external_function_parameters( [
            "content" => new \external_value(PARAM_TEXT, 'import file content'),
            "format" => new \external_value(PARAM_TEXT, 'import format'),
            "context_id" => new \external_value(PARAM_INT, 'context of the study plan',VALUE_DEFAULT),
            ] );
    }

    public static function import_plan_returns() 
    {
        return success::structure();
    }
 
    public static function import_plan($content,$format="application/json",$context_id=1)
    {

        try{
            // Validate import context
            $context = webservicehelper::find_context($context_id);
            webservicehelper::require_capabilities(self::CAP_EDIT,$context);

            $result = studyplan::import_studyplan($content,$format,$context_id);
            return (new success($result, "During study plan import"))->model();
        }
        catch(\webservice_access_exception $x) {
            return success::fail("Access denied")->model();
        }
    }   

    public static function import_studylines_parameters() 
    {
        return new \external_function_parameters( [
            "studyplan_id" => new \external_value(PARAM_INT, 'id of plan to export '),
            "content" => new \external_value(PARAM_TEXT, 'import file content'),
            "format" => new \external_value(PARAM_TEXT, 'import format'),
            ] );
    }

    public static function import_studylines_returns() 
    {
        return success::structure();
    }
 
    public static function import_studylines($studyplan_id, $content,$format="application/json")
    {

        try{
            $plan = studyplan::findById($studyplan_id);
            // Validate import context
            webservicehelper::require_capabilities(self::CAP_EDIT,$plan->context());

            $result = $plan->import_studylines($content,$format);
            return ($result?success::success():success::fail())->model();
        }
        catch(\webservice_access_exception $x) {
            return success::fail("Access denied")->model();
        }
    }  
    
    
    /************************************************
     *                                              *
     * Read and write course module title and desc  *
     * TODO: FINISH OR REMOVE                       *
     *                                              *
     ************************************************/
    public static function read_course_displayname_parameters(){
        return new \external_function_parameters( [
            "cmid" => new \external_value(PARAM_INT, 'id of plan to export '),
            ] );
    }
    public static function read_course_displayname_returns(){
        return new \external_value(PARAM_TEXT, 'title of course module');
    }

    public static function read_course_displayname($cmid){
        //$cm = 
    }

    public static function write_course_displayname_parameters(){
        return new \external_function_parameters( [
            "cmid" => new \external_value(PARAM_INT, 'id of plan to export '),
            "title" => new \external_value(PARAM_TEXT, 'title of course module'),
        ] );
    }

    public static function write_course_displayname_returns(){
        return success::structure();
    }

    public static function write_course_displayname($cmid,$title,$desc){

    }

    /********************************************************
     *                                                      *
     * Read and write course module title and desc          *
     * TODO: DEPRECATE when implementing core completion    * 
     *                                                      *
     ********************************************************/

    public static function submit_cm_editform_parameters(){
        return new \external_function_parameters( [
            "cmid" => new \external_value(PARAM_INT, 'id of course module'),
            "formdata" => new \external_value(PARAM_RAW, 'url encoded form data'),
        ] );
    }

    public static function submit_cm_editform_returns(){
        return success::structure();
    }

    public static function submit_cm_editform($cmid,$formdata){
        global $CFG;
        global $DB;

        // Check the course module exists.
        $cm = \get_coursemodule_from_id('', $cmid, 0, false, MUST_EXIST);
        // Get some context ready
        $context = \context_course::instance($cm->course);
        self::validate_context($context);

        // Check the course exists.
        $course = \get_course($cm->course);
        // require_login
        require_login($course, false, $cm); // needed to setup proper $COURSE

        // get needed info to create the correct form
        list($cm, $context, $module, $data, $cw) = \get_moduleinfo_data($cm, $course);
        $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
        if (file_exists($modmoodleform)) {
            require_once($modmoodleform);
        } else {
            print_error('noformdesc');
        }
        $mformclassname = 'mod_'.$module->name.'_mod_form';

        // now hack the received data into $_POST, so the mform thinks it has been submitted "normally"
        foreach(explode("&",$formdata) as $pair){
            $p = explode("=",$pair,2);
            $k = urldecode($p[0]);
            $v = (count($p)>1)?urldecode($p[1]):"";
            
            if(strpos($k,"[") > 0 && strpos($k,"]") == strlen($k) -1){
                // its a bracketet field, like filename[text] which should be separated and put into a named array
                list($k,$h) = explode("[",$k,2);
                if(strlen($k) > 0 && strlen($h) > 1){
                    $h = rtrim($h,"]");
                    if(!array_key_exists($k,$_POST) || !is_array($_POST[$k])){
                        $_POST[$k] = [];
                    }
                    $_POST[$k][$h] = $v;
                }
            }else {
                $_POST[$k] = $v;
            }
        }
        
        // Now create the mform and update the module...
        // update_moduleinfo() actually needs the mform somehow. Hence the ugly hacks
        $mform = new $mformclassname($data, $cw->section, $cm, $course);
        $mform->set_data($data);

        if ($fromform = $mform->get_data()) {
            update_moduleinfo($cm, $fromform, $course, $mform);
            return success::success()->model();
        } else {
            return success::fail("MForm did not recognize submission data")->model();
        }
    }


}