<commit_before><?php
/**
 * Interact with Moodle
 * @author <NAME> <<EMAIL>>
 */

namespace App\Classes;

use App\User;
use Illuminate\Support\Facades\DB;
use MoodleRest;
use ReflectionClass;

class VATUSAMoodle extends MoodleRest
{

    /**
     * List of Cohorts
     * @var array|mixed
     */
    protected $cohorts = [];

    /**
     * List of all Categories
     * @var array|mixed
     */
    protected $categories = [];

    /** @var int[] Role Mappings */
    protected $roleIds = [
        'TA'  => 1,
        'INS' => 4,
        'STU' => 5,
        'MTR' => 9,
        'CBT' => 10,
        'FACCBT' => 11
    ];

    /** @var int Category Contexts */
    public const CATEGORY_CONTEXT_VATUSA = 43;
    public const CATEGORY_CONTEXT_OBS = 44;
    public const CATEGORY_CONTEXT_S1 = 45;
    public const CATEGORY_CONTEXT_S2 = 115;
    public const CATEGORY_CONTEXT_S3 = 47;
    public const CATEGORY_CONTEXT_C1 = 48;

    /** @var int Category IDs */
    public const CATEGORY_ID_VATUSA = 2;
    public const CATEGORY_ID_OBS = 3;
    public const CATEGORY_ID_S1 = 4;
    public const CATEGORY_ID_S2 = 72;
    public const CATEGORY_ID_S3 = 6;
    public const CATEGORY_ID_C1 = 7;

    /** @var int Context Levels */
    public const CONTEXT_SYSTEM = 10;
    public const CONTEXT_USER = 30;
    public const CONTEXT_COURSECAT = 40;
    public const CONTEXT_COURSE = 50;
    public const CONTEXT_MODULE = 70;
    public const CONTEXT_BLOCK = 80;

    /**
     * VATUSAMoodle constructor.
     *
     * @param bool $isSSO
     */
    public function __construct(bool $isSSO = false)
    {
        parent::__construct(config('services.moodle.url') . '/webservice/rest/server.php',
            $isSSO ? config('services.moodle.token_sso') : config('services.moodle.token'));

        $this->categories = $this->getCategories();
    }

    /**
     * Set token type
     * @param bool $isSSO
     */
    public function setSSO(bool $isSSO = true)
    {
        $this->setToken($isSSO ? config('services.moodle.token_sso') : config('services.moodle.token'));
    }

    /**
     * Get all Cohorts
     * @return mixed
     */
    public function getCohorts()
    {
        return $this->request("core_cohort_get_cohorts");
    }

    /**
     * Get members of all Cohorts.
     * @return array|mixed
     */
    public function getCohortMembers(): array
    {
        $members = [];
        foreach ($this->getCohorts() as $cohort) {
            $id = $cohort["id"];
            $members[] = $this->request("core_cohort_get_cohort_members", ["cohortids" => [0 => $id]])[0];
        }

        return $members;
    }

    /**
     * Get an array of all categories.
     * @return mixed
     */
    public function getCategories()
    {
        return $this->request("core_course_get_categories");
    }

    /**
     * Get single category
     *
     * @param int $id Category ID
     *
     * @return mixed
     */
    public function getCategory(int $id): array
    {
        return $this->request("core_course_get_categories",
            ["criteria" => [0 => ["key" => "id", "value" => $id]]]);
    }

    /**
     * Get Category Context or ID
     *
     * @param string|null $short   IDNumber
     * @param bool        $context Short is Context
     * @param bool        $full    Return full array
     *
     * @return mixed|null
     */
    public function getCategoryFromShort(?string $short, bool $context = false, bool $full = false)
    {
        if (is_null($short)) {
            return null;
        }

        foreach ($this->categories as $category) {
            if ($category["idnumber"] === $short) {
                if ($full) {
                    return $context ? array_merge($category,
                        ["context" => $this->getContext($category["id"], "coursecat")]) : $category;
                }

                return $context ? $this->getContext($category["id"], "coursecat") : $category['id'];
            }
        }

        return null;
    }

    /**
     * Get All Subcategories of Parent
     *
     * @param int|null $parent        ID or Context
     * @param bool     $includeParent Include parent in return
     * @param bool     $context       Parent is Context
     * @param bool     $full          Return full array
     *
     * @return array
     */
    public function getAllSubcategories(
        ?int $parent,
        bool $includeParent = false,
        bool $context = false,
        bool $full = false<commit_msg>Don't preload course categories<commit_after><?php
/**
 * Interact with Moodle
 * @author <NAME> <<EMAIL>>
 */

namespace App\Classes;

use App\User;
use Illuminate\Support\Facades\DB;
use MoodleRest;
use ReflectionClass;

class VATUSAMoodle extends MoodleRest
{

    /**
     * List of Cohorts
     * @var array|mixed
     */
    protected $cohorts = [];

    /**
     * List of all Categories
     * @var array|mixed
     */
    protected $categories = [];

    /** @var int[] Role Mappings */
    protected $roleIds = [
        'TA'  => 1,
        'INS' => 4,
        'STU' => 5,
        'MTR' => 9,
        'CBT' => 10,
        'FACCBT' => 11
    ];

    /** @var int Category Contexts */
    public const CATEGORY_CONTEXT_VATUSA = 43;
    public const CATEGORY_CONTEXT_OBS = 44;
    public const CATEGORY_CONTEXT_S1 = 45;
    public const CATEGORY_CONTEXT_S2 = 115;
    public const CATEGORY_CONTEXT_S3 = 47;
    public const CATEGORY_CONTEXT_C1 = 48;

    /** @var int Category IDs */
    public const CATEGORY_ID_VATUSA = 2;
    public const CATEGORY_ID_OBS = 3;
    public const CATEGORY_ID_S1 = 4;
    public const CATEGORY_ID_S2 = 72;
    public const CATEGORY_ID_S3 = 6;
    public const CATEGORY_ID_C1 = 7;

    /** @var int Context Levels */
    public const CONTEXT_SYSTEM = 10;
    public const CONTEXT_USER = 30;
    public const CONTEXT_COURSECAT = 40;
    public const CONTEXT_COURSE = 50;
    public const CONTEXT_MODULE = 70;
    public const CONTEXT_BLOCK = 80;

    /**
     * VATUSAMoodle constructor.
     *
     * @param bool $isSSO
     */
    public function __construct(bool $isSSO = false)
    {
        parent::__construct(config('services.moodle.url') . '/webservice/rest/server.php',
            $isSSO ? config('services.moodle.token_sso') : config('services.moodle.token'));
    }

    /**
     * Set token type
     * @param bool $isSSO
     */
    public function setSSO(bool $isSSO = true)
    {
        $this->setToken($isSSO ? config('services.moodle.token_sso') : config('services.moodle.token'));
    }

    /**
     * Get all Cohorts
     * @return mixed
     */
    public function getCohorts()
    {
        return $this->request("core_cohort_get_cohorts");
    }

    /**
     * Get members of all Cohorts.
     * @return array|mixed
     */
    public function getCohortMembers(): array
    {
        $members = [];
        foreach ($this->getCohorts() as $cohort) {
            $id = $cohort["id"];
            $members[] = $this->request("core_cohort_get_cohort_members", ["cohortids" => [0 => $id]])[0];
        }

        return $members;
    }

    /**
     * Get an array of all categories.
     * @return mixed
     */
    public function getCategories()
    {
        return $this->request("core_course_get_categories");
    }

    /**
     * Get single category
     *
     * @param int $id Category ID
     *
     * @return mixed
     */
    public function getCategory(int $id): array
    {
        return $this->request("core_course_get_categories",
            ["criteria" => [0 => ["key" => "id", "value" => $id]]]);
    }

    /**
     * Get Category Context or ID
     *
     * @param string|null $short   IDNumber
     * @param bool        $context Short is Context
     * @param bool        $full    Return full array
     *
     * @return mixed|null
     */
    public function getCategoryFromShort(?string $short, bool $context = false, bool $full = false)
    {
        if (is_null($short)) {
            return null;
        }

        foreach ($this->getCategories() as $category) {
            if ($category["idnumber"] === $short) {
                if ($full) {
                    return $context ? array_merge($category,
                        ["context" => $this->getContext($category["id"], "coursecat")]) : $category;
                }

                return $context ? $this->getContext($category["id"], "coursecat") : $category['id'];
            }
        }

        return null;
    }

    /**
     * Get All Subcategories of Parent
     *
     * @param int|null $parent        ID or Context
     * @param bool     $includeParent Include parent in return
     * @param bool     $context       Parent is Context
     * @param bool     $full          Return full array
     *
     * @return array
     */
    public function getAllSubcategories(
        ?int $parent,
        bool $includeParent = false,
        bool $context = false,
        bool $full = false
{'fraction_non_alphanumeric': 0.1255328696432578, 'fraction_numerical': 0.01144267444469374, 'mean_word_length': 2.0364441416893735, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5497071', 'n_tokens_mistral': 2925, 'n_tokens_neox': 2772, 'n_words': 692}