mdjnelson / moodle-mod_customcert

Enables the creation of dynamically generated certificates with complete customisation via the web browser.
https://moodle.org/plugins/mod_customcert
GNU General Public License v3.0
93 stars 159 forks source link

Error writing in the database #605

Closed Galindo02 closed 7 months ago

Galindo02 commented 7 months ago

Hello to everyone, I am working with this PlugIn and doing some slight changes in the code so I can achieve different things related with my courses and moreover. I have been checking this forum to see other's issues and work from there. Recently I wanted to modify the code generator in the /classes/certificate.php file (generate_code function) so instead of generating a random string, it would generate a HASH. The code is the following:

public static function generate_code() { global $DB;

    $uniquecodefound = false;
    $code = random_bytes(32);
    while (!$uniquecodefound) {
        if (!$DB->record_exists('customcert_issues', array('code' => $code))) {
            $uniquecodefound = true;
        } else {
            $code = random_bytes(32);
        }
    }
    $hash = hash('sha256', $code);

    return $hash;
}

The problem is not the generation of the code, as in the "Edit certificate" part, when adding some elements and later visualizing the final result, the code is appearing in the way I want, which is a sha256 (large random string of 256 bits). The problem appears when saving it and trying to "view certificate" in the main page. I have also tried to view/download through some student account and same error appears: Error writing to database Maybe it isn't something related with the plugin code, but if I don't add the concrete element of the hash to the certificate, it works. I have thought that maybe there's a restriction of string lenght when adding the code to the database or something like that.

If anyone can help me with this I would extremely appreciate it. Thank you

mdjnelson commented 7 months ago

Turn on developer mode on your site to see the debugging information in more detail please. Also, since this is a change to the core code I can't say I will create a patch but can help you on your install. It's most likely because the code column only allows 40 characters (for whatever reason, not sure why it's set to that). Edit your database table to increase the length of this.

mdjnelson commented 7 months ago

Closing as I am sure that is the reason.