protected function combine_correlated_tags($tags) {
        global $DB;
        $ids = array_map(function($t) {
            return $t->id;
        }, $tags);

        // Retrieve the correlated tags of this tag and correlated tags of all tags to be merged in one query
        // but store them separately. Calculate the list of correlated tags that need to be added to the current.
        list($sql, $params) = $DB->get_in_or_equal($ids);
        $params[] = $this->id;
        $records = $DB->get_records_select('tag_correlation', 'tagid '.$sql.' OR tagid = ?',
            $params, '', 'tagid, id, correlatedtags');
        $correlated = array();
        $mycorrelated = array();
        foreach ($records as $record) {
            $taglist = preg_split('/\s*,\s*/', trim($record->correlatedtags), -1, PREG_SPLIT_NO_EMPTY);
            if ($record->tagid == $this->id) {
                $mycorrelated = $taglist;
            } else {
                $correlated = array_merge($correlated, $taglist);
            }
        }
        array_unique($correlated);
        // Strip out from $correlated the ids of the tags that are already in $mycorrelated
        // or are one of the tags that are going to be combined.
        $correlated = array_diff($correlated, [$this->id], $ids, $mycorrelated);

        if (empty($correlated)) {
            // Nothing to do, ignore situation when current tag is correlated to one of the merged tags - they will
            // be deleted later and get_tag_correlation() will not return them. Next cron will clean everything up.
            return;
        }

        // Update correlated tags of this tag.
        $newcorrelatedlist = join(',', array_merge($mycorrelated, $correlated));
        if (isset($records[$this->id])) {
            $DB->update_record('tag_correlation', array('id' => $records[$this->id]->id, 'correlatedtags' => $newcorrelatedlist));
        } else {
            $DB->insert_record('tag_correlation', array('tagid' => $this->id, 'correlatedtags' => $newcorrelatedlist));
        }

        // Add this tag to the list of correlated tags of each tag in $correlated.
        list($sql, $params) = $DB->get_in_or_equal($correlated);
        $records = $DB->get_records_select('tag_correlation', 'tagid '.$sql, $params, '', 'tagid, id, correlatedtags');
        foreach ($correlated as $tagid) {
            if (isset($records[$tagid])) {
                $newcorrelatedlist = $records[$tagid]->correlatedtags . ',' . $this->id;
                $DB->update_record('tag_correlation', array('id' => $records[$tagid]->id, 'correlatedtags' => $newcorrelatedlist));
            } else {
                $DB->insert_record('tag_correlation', array('tagid' => $tagid, 'correlatedtags' => '' . $this->id));
            }
        }
    }
protected function combine_correlated_tags($tags) {
        global $DB;
        $ids = array_map(function($t) {
            return $t->id;
        }, $tags);

        // Retrieve the correlated tags of this tag and correlated tags of all tags to be merged in one query
        // but store them separately. Calculate the list of correlated tags that need to be added to the current.
        list($sql, $params) = $DB->get_in_or_equal($ids);
        $params[] = $this->id;
        $records = $DB->get_records_select('tag_correlation', 'tagid '.$sql.' OR tagid = ?',
            $params, '', 'tagid, id, correlatedtags');
        $correlated = array();
        $mycorrelated = array();
        foreach ($records as $record) {
            $taglist = preg_split('/\s*,\s*/', trim($record->correlatedtags), -1, PREG_SPLIT_NO_EMPTY);
            if ($record->tagid == $this->id) {
                $mycorrelated = $taglist;
            } else {
                $correlated = array_merge($correlated, $taglist);
            }
        }
        array_unique($correlated);
        // Strip out from $correlated the ids of the tags that are already in $mycorrelated
        // or are one of the tags that are going to be combined.
        $correlated = array_diff($correlated, [$this->id], $ids, $mycorrelated);

        if (empty($correlated)) {
            // Nothing to do, ignore situation when current tag is correlated to one of the merged tags - they will
            // be deleted later and get_tag_correlation() will not return them. Next cron will clean everything up.
            return;
        }

        // Update correlated tags of this tag.
        $newcorrelatedlist = join(',', array_merge($mycorrelated, $correlated));
        if (isset($records[$this->id])) {
            $DB->update_record('tag_correlation', array('id' => $records[$this->id]->id, 'correlatedtags' => $newcorrelatedlist));
        } else {
            $DB->insert_record('tag_correlation', array('tagid' => $this->id, 'correlatedtags' => $newcorrelatedlist));
        }

        // Add this tag to the list of correlated tags of each tag in $correlated.
        list($sql, $params) = $DB->get_in_or_equal($correlated);
        $records = $DB->get_records_select('tag_correlation', 'tagid '.$sql, $params, '', 'tagid, id, correlatedtags');
        foreach ($correlated as $tagid) {
            if (isset($records[$tagid])) {
                $newcorrelatedlist = $records[$tagid]->correlatedtags . ',' . $this->id;
                $DB->update_record('tag_correlation', array('id' => $records[$tagid]->id, 'correlatedtags' => $newcorrelatedlist));
            } else {
                $DB->insert_record('tag_correlation', array('tagid' => $tagid, 'correlatedtags' => '' . $this->id));
            }
        }
    }
protected function combine_correlated_tags ( $ tags ) { global $ DB ; $ ids = array_map ( function ( $ t ) { return $ t -> id ; } , $ tags ) ; // Retrieve the correlated tags of this tag and correlated tags of all tags to be merged in one query // but store them separately. Calculate the list of correlated tags that need to be added to the current. list ( $ sql , $ params ) = $ DB -> get_in_or_equal ( $ ids ) ; $ params [ ] = $ this -> id ; $ records = $ DB -> get_records_select ( 'tag_correlation' , 'tagid ' . $ sql . ' OR tagid = ?' , $ params , '' , 'tagid, id, correlatedtags' ) ; $ correlated = array ( ) ; $ mycorrelated = array ( ) ; foreach ( $ records as $ record ) { $ taglist = preg_split ( '/\s*,\s*/' , trim ( $ record -> correlatedtags ) , - 1 , PREG_SPLIT_NO_EMPTY ) ; if ( $ record -> tagid == $ this -> id ) { $ mycorrelated = $ taglist ; } else { $ correlated = array_merge ( $ correlated , $ taglist ) ; } } array_unique ( $ correlated ) ; // Strip out from $correlated the ids of the tags that are already in $mycorrelated // or are one of the tags that are going to be combined. $ correlated = array_diff ( $ correlated , [ $ this -> id ] , $ ids , $ mycorrelated ) ; if ( empty ( $ correlated ) ) { // Nothing to do, ignore situation when current tag is correlated to one of the merged tags - they will // be deleted later and get_tag_correlation() will not return them. Next cron will clean everything up. return ; } // Update correlated tags of this tag. $ newcorrelatedlist = join ( ',' , array_merge ( $ mycorrelated , $ correlated ) ) ; if ( isset ( $ records [ $ this -> id ] ) ) { $ DB -> update_record ( 'tag_correlation' , array ( 'id' => $ records [ $ this -> id ] -> id , 'correlatedtags' => $ newcorrelatedlist ) ) ; } else { $ DB -> insert_record ( 'tag_correlation' , array ( 'tagid' => $ this -> id , 'correlatedtags' => $ newcorrelatedlist ) ) ; } // Add this tag to the list of correlated tags of each tag in $correlated. list ( $ sql , $ params ) = $ DB -> get_in_or_equal ( $ correlated ) ; $ records = $ DB -> get_records_select ( 'tag_correlation' , 'tagid ' . $ sql , $ params , '' , 'tagid, id, correlatedtags' ) ; foreach ( $ correlated as $ tagid ) { if ( isset ( $ records [ $ tagid ] ) ) { $ newcorrelatedlist = $ records [ $ tagid ] -> correlatedtags . ',' . $ this -> id ; $ DB -> update_record ( 'tag_correlation' , array ( 'id' => $ records [ $ tagid ] -> id , 'correlatedtags' => $ newcorrelatedlist ) ) ; } else { $ DB -> insert_record ( 'tag_correlation' , array ( 'tagid' => $ tagid , 'correlatedtags' => '' . $ this -> id ) ) ; } } }
Combine together correlated tags of several tags

This is a help method for method combine_tags()

@param core_tag_tag[] $tags
Combine together correlated tags of several tags