public function change_parent($newparentcat) {
        // Make sure parent category exists but do not check capabilities here that it is visible to current user.
        if (is_object($newparentcat)) {
            $newparentcat = self::get($newparentcat->id, MUST_EXIST, true);
        } else {
            $newparentcat = self::get((int)$newparentcat, MUST_EXIST, true);
        }
        if ($newparentcat->id != $this->parent) {
            $this->change_parent_raw($newparentcat);
            fix_course_sortorder();
            cache_helper::purge_by_event('changesincoursecat');
            $this->restore();

            $event = \core\event\course_category_updated::create(array(
                'objectid' => $this->id,
                'context' => $this->get_context()
            ));
            $event->set_legacy_logdata(array(SITEID, 'category', 'move', 'editcategory.php?id=' . $this->id, $this->id));
            $event->trigger();
        }
    }
public function change_parent($newparentcat) {
        // Make sure parent category exists but do not check capabilities here that it is visible to current user.
        if (is_object($newparentcat)) {
            $newparentcat = self::get($newparentcat->id, MUST_EXIST, true);
        } else {
            $newparentcat = self::get((int)$newparentcat, MUST_EXIST, true);
        }
        if ($newparentcat->id != $this->parent) {
            $this->change_parent_raw($newparentcat);
            fix_course_sortorder();
            cache_helper::purge_by_event('changesincoursecat');
            $this->restore();

            $event = \core\event\course_category_updated::create(array(
                'objectid' => $this->id,
                'context' => $this->get_context()
            ));
            $event->set_legacy_logdata(array(SITEID, 'category', 'move', 'editcategory.php?id=' . $this->id, $this->id));
            $event->trigger();
        }
    }
public function change_parent ( $ newparentcat ) { // Make sure parent category exists but do not check capabilities here that it is visible to current user. if ( is_object ( $ newparentcat ) ) { $ newparentcat = self :: get ( $ newparentcat -> id , MUST_EXIST , true ) ; } else { $ newparentcat = self :: get ( ( int ) $ newparentcat , MUST_EXIST , true ) ; } if ( $ newparentcat -> id != $ this -> parent ) { $ this -> change_parent_raw ( $ newparentcat ) ; fix_course_sortorder ( ) ; cache_helper :: purge_by_event ( 'changesincoursecat' ) ; $ this -> restore ( ) ; $ event = \ core \ event \ course_category_updated :: create ( array ( 'objectid' => $ this -> id , 'context' => $ this -> get_context ( ) ) ) ; $ event -> set_legacy_logdata ( array ( SITEID , 'category' , 'move' , 'editcategory.php?id=' . $ this -> id , $ this -> id ) ) ; $ event -> trigger ( ) ; } }
Efficiently moves a category - NOTE that this can have
a huge impact access-control-wise...

Note that this function does not check capabilities.

Example of usage:
$coursecat = core_course_category::get($categoryid);
if ($coursecat->can_change_parent($newparentcatid)) {
$coursecat->change_parent($newparentcatid);
}

This function does not update field course_categories.timemodified
If you want to update timemodified, use
$coursecat->update(array('parent' => $newparentcat));

@param int|stdClass|core_course_category $newparentcat
Efficiently moves a category - NOTE that this can have a huge impact access - control - wise ...