private function get_category_from_group($categories) {
        global $DB;

        if (empty($categories)) {
            $catid = $this->get_default_category_id();
        } else {
            $createnewcategories = $this->get_config('createnewcategories');
            $categoryseparator = trim($this->get_config('categoryseparator'));
            $nestedcategories = trim($this->get_config('nestedcategories'));
            $searchbyidnumber = trim($this->get_config('categoryidnumber'));

            if (!empty($categoryseparator)) {
                $sep = '{\\'.$categoryseparator.'}';
            }

            $catid = 0;
            $fullnestedcatname = '';

            foreach ($categories as $categoryinfo) {
                if ($searchbyidnumber) {
                    $values = preg_split($sep, $categoryinfo, -1, PREG_SPLIT_NO_EMPTY);
                    if (count($values) < 2) {
                        $this->log_line('Category ' . $categoryinfo . ' missing name or idnumber. Using default category instead.');
                        $catid = $this->get_default_category_id();
                        break;
                    }
                    $categoryname = $values[0];
                    $categoryidnumber = $values[1];
                } else {
                    $categoryname = $categoryinfo;
                    $categoryidnumber = null;
                    if (empty($categoryname)) {
                        $this->log_line('Category ' . $categoryinfo . ' missing name. Using default category instead.');
                        $catid = $this->get_default_category_id();
                        break;
                    }
                }

                if (!empty($fullnestedcatname)) {
                    $fullnestedcatname .= ' / ';
                }

                $fullnestedcatname .= $categoryname;
                $parentid = $catid;

                // Check if category exist.
                $params = array();
                if ($searchbyidnumber) {
                    $params['idnumber'] = $categoryidnumber;
                } else {
                    $params['name'] = $categoryname;
                }
                if ($nestedcategories) {
                    $params['parent'] = $parentid;
                }

                if ($catid = $DB->get_field('course_categories', 'id', $params)) {
                    continue; // This category already exists.
                }

                // If we're allowed to create new categories, let's create this one.
                if ($createnewcategories) {
                    $newcat = new stdClass();
                    $newcat->name = $categoryname;
                    $newcat->visible = 0;
                    $newcat->parent = $parentid;
                    $newcat->idnumber = $categoryidnumber;
                    $newcat = core_course_category::create($newcat);
                    $catid = $newcat->id;
                    $this->log_line("Created new (hidden) category '$fullnestedcatname'");
                } else {
                    // If not found and not allowed to create, stick with default.
                    $this->log_line('Category ' . $categoryinfo . ' not found in Moodle database. Using default category instead.');
                    $catid = $this->get_default_category_id();
                    break;
                }
            }
        }

        return $catid;
    }