<?php
namespace local_treestudyplan;
require_once($CFG->libdir.'/externallib.php');

use \local_treestudyplan\local\helpers\webservicehelper;

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

class studentstudyplanservice extends \external_api 
{
    const CAP_VIEWOTHER = "local/treestudyplan:viewuserreports";
    /************************
     *                      *
     * list_user_studyplans *
     *                      *
     ************************/

    public static function list_user_studyplans_parameters() 
	{
        return new \external_function_parameters([
            "userid" => new \external_value(PARAM_INT, 'id of student', VALUE_DEFAULT),
        ]);
    }

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

    private static function list_user_studyplans($userid){
        global $CFG, $DB;

        $list = [];
        $studyplans = studyplan::find_for_user($userid);
        foreach($studyplans as $studyplan)
        {
            // only include studyplans in the context the user has permissions for
            if(webservicehelper::has_capabilities(self::CAP_VIEWOTHER,$studyplan->context(),false)){
                $list[] =$studyplan->simple_model();
            }
        }
        return $list;
    }

    /************************
     *                      *
     * get_user_studyplans  *
     *                      *
     ************************/

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

	public static function get_user_studyplans_returns() 
	{
        return new \external_multiple_structure(
            studyplan::user_structure()
        );
    }
    public static function get_user_studyplans($userid)
    {
        global $CFG, $DB;

        $studyplans = studyplan::find_for_user($userid);

        $map = [];            
        foreach($studyplans as $studyplan)
        {
            // only include studyplans in the context the user has permissions for
            if(webservicehelper::has_capabilities(self::CAP_VIEWOTHER,$studyplan->context(),false)){
                $map[] = $studyplan->user_model($userid);
            }
        }

        return $map;
    }

    /************************
     *                      *
     * get_user_studyplan   *
     *                      *
     ************************/

    public static function get_user_studyplan_parameters() 
	{
        return new \external_function_parameters( [
            "userid" => new \external_value(PARAM_INT, 'id of user'),
            "studyplanid" => new \external_value(PARAM_INT, 'id of specific studyplan to provide'),
        ] );
    }

	public static function get_user_studyplan_returns() 
	{
        return studyplan::user_structure();
    }
    public static function get_user_studyplan($userid,$studyplanid)
    {
        global $CFG, $DB;

        $studyplan = studyplan::findById($studyplanid);
        webservicehelper::require_capabilities(self::CAP_VIEWOTHER,$studyplan->context());

        if($studyplan->has_linked_user($userid)){
            return $studyplan->user_model($userid);
        }
        else {
            return null;
        }
    }

    /****************************
     *                          *
     * get_invited_studyplan    *
     *                          *
     ****************************/

    public static function get_invited_studyplan_parameters() 
	{
        return new \external_function_parameters( [
            "invitekey" => new \external_value(PARAM_RAW, 'invite key'),
        ] );
    }

	public static function get_invited_studyplan_returns() 
	{
        return new \external_multiple_structure(
            studyplan::user_structure()
        );
    }
    public static function get_invited_studyplan($invitekey)
    {
        global $CFG, $DB;

        $invite = $DB->get_record_select("local_treestudyplan_invit", $DB->sql_compare_text("invitekey"). " = " . $DB->sql_compare_text(":invitekey"), ['invitekey' => $invitekey]);

        if(empty($invite)){
            return [];
        }

        // Validate context now
        \external_api::validate_context(\context_system::instance());

        $userid = $invite->user_id;

        $map = [];
        $studyplans = studyplan::find_for_user($userid);
        foreach($studyplans as $studyplan)
        {
            $map[] = $studyplan->user_model($userid);
        }
        return $map;
    }

    /************************
     *                      *
     * list_own_studyplans *
     *                      *
     ************************/

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

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

    private static function list_own_studyplans(){
        global $CFG, $DB, $USER;
        $userid = $USER->id;

        $list = [];
        $studyplans = studyplan::find_for_user($userid);
        foreach($studyplans as $studyplan)
        {
            $list[] =$studyplan->simple_model();
        }
        return $list;
    }

    /************************
     *                      *
     * get_own_studyplan   *
     *                      *
     ************************/

    public static function get_own_studyplan_parameters() 
	{
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of specific studyplan to provide', VALUE_DEFAULT),
        ] );
    }

	public static function get_own_studyplan_returns() 
	{
        return new \external_multiple_structure(
            studyplan::user_structure()
        );
    }

    public static function get_own_studyplan($id=null)
    {
        global $USER;

        // Validate this call in the system context.
        \external_api::validate_context(\context_system::instance());

        $userid = $USER->id;

        $studyplans = studyplan::find_for_user($userid);

        if(isset($id) && $id > 0){
            if(isset($studyplans[$id])){
                $studyplan = $studyplans[$id];
                return [$studyplan->user_model($userid)];
            } else {
                return [];
            }
        }
        else {
            $map = [];
            foreach($studyplans as $studyplan){
                $map[] = $studyplan->user_model($userid);
            }
            return $map;
        }
    }

    /***************************
     *                         *
     * get_teaching_studyplans *
     *                         *
     ***************************/

    public static function get_teaching_studyplans_parameters() 
	{
        return new \external_function_parameters( [
            "id" => new \external_value(PARAM_INT, 'id of specific studyplan to provide', VALUE_DEFAULT),
        ] );
    }

	public static function get_teaching_studyplans_returns() 
	{
        return new \external_multiple_structure(
            studyplan::editor_structure()
        );
    }

    public static function get_teaching_studyplans($id=null)
    {
        global $CFG, $DB, $USER;
        $userid = $USER->id;
        
        \external_api::validate_context(\context_system::instance()); 
        $studyplans = teachingfinder::list_my_plans();

        if(isset($id) && $id > 0){
            if(isset($studyplans[$id])){
                $studyplan = $studyplans[$id];
                return [$studyplan->editor_model($userid)];
            } else {
                return [];
            }
        }
        else {
            $map = [];
            foreach($studyplans as $studyplan){
                $map[] = $studyplan->editor_model($userid);
            }
            return $map;
        }
    }
}