Inject more info into moodle_text_filter for creating unique string hashes.

Inject more info into moodle_text_filter for creating unique string hashes.

per Kaleb Heitzman,
Number of replies: 3

I've been working on a derivative of the Content Translation Manager and Content Translation filter for use with DeepL. I'm stuck at creating a unique hash with moodle_text_filter.

This is my hashing code at the moment, but I don't trust it's always going to be unique plus the $text dependency needs stripped out.

        list($context, $course, $cm) = get_context_info_array($this->context->id);
$hashkey = sha1(trim('/' . $cm->added . '/' . $format . $this->context->path . '/' . substr($text, 0, 12)));

I've tried creating unique hashes using context and text format to no avail. I need the ability to inject a unique identifier into the filter that's not based on creating a sha1 hash of the text itself, because if you change the source text you lose the db connection to the translation. If I can find a way to create the a unique hash not based on the text itself then this derivative based on DeepL and a different UI will be done. What I would like to do is inject the table and column name from where the text was originally derived from but I can't figure a universal way to do that without going accounting for activities, section types, and other strings in a course.

I looked all the way back to Moodle 2.5 to see what hashing functions existed for text previously and couldn't find anything. 

I'm open to ideas here...



Average of ratings: -
In reply to Kaleb Heitzman

Re: Inject more info into moodle_text_filter for creating unique string hashes.

per Kaleb Heitzman,

Answering this for myself. I found a solution, but not one I love.

Using get_fast_modinfo I can build unique hashes without relying on the text string itself but it means coding checks for each activity used in a course. It's not idea but it works.

In reply to Kaleb Heitzman

Re: Inject more info into moodle_text_filter for creating unique string hashes.

per Francis Devine,
Avatar Core developers
If you have a context ID associated with the filter and there's only one filter per context you could use the hash of the context path (from the context table in the db) which should be a unique string.

edit: nevermind I see you used it already, what gives you concern it won't be unique?

In reply to Francis Devine

Re: Inject more info into moodle_text_filter for creating unique string hashes.

per Kaleb Heitzman,

For example, if you have an activity printing strings from both the name field and intro field, they share the same context path. I've gotten around it by using returning the array key from each instance and attaching it to a configuration array. I finally have the code I need in order to solve the problem I was trying to solve. The problem mainly being updating an english translation without losing other language translations because the hash changed (because it was based on the text). This is some of the data that's being output now that lets me generate a unique hash.


Array
(
    [instance_id] => 19
    [ctx_instance_id] => 80
    [ctx_path] => /1/3/80
    [modname] => section
    [modcol] => name
    [text] => Welcome
)
Array
(
    [instance_id] => 20
    [ctx_instance_id] => 80
    [ctx_path] => /1/3/80
    [modname] => section
    [modcol] => name
    [text] => First Topic
)
Array
(
    [instance_id] => 9
    [ctx_instance_id] => 83
    [ctx_path] => /1/3/80/83
    [modname] => forum
    [text] => Demo Forum
    [modcol] => name
)
Array
(
    [instance_id] => 9
    [ctx_instance_id] => 85
    [ctx_path] => /1/3/80/85
    [modname] => page
    [text] => Translation Page
    [modcol] => name
)