Convenience tables for mapping integer constants

Convenience tables for mapping integer constants

by Peter West -
Number of replies: 2

I mentioned this on the general developer list, without drawing any comment, so I'll try here, even though it is not a MAJOR feature.

There are many sets of constants defined against small sets of integers.  For example, completion/criteria/completion_criteria.php defines such a set.  File cc1 excerpts the relevant definitions.

These constants are ubiquitous in the database tables, and they obscure the meanings. To determine the contents of such records requires manual cross-referencing with the code, as in this example from the contributed ad-hoc queries.

CASE WHEN p.criteriatype = 1 THEN "Self" WHEN p.criteriatype = 2 THEN "By Date" WHEN p.criteriatype = 3 THEN "Unenrol Status" WHEN p.criteriatype = 4 THEN "Activity" WHEN p.criteriatype = 5 THEN "Duration" WHEN p.criteriatype = 6 THEN "Course Grade" WHEN p.criteriatype = 7 THEN "Approve by Role" WHEN p.criteriatype = 8 THEN "Previous Course" END AS criteriatype,

If these constants change, the query will no longer work as expected, and may not work at all.

I noticed this when I was trying to grasp some of  the intricacies of Moodle by looking at the install.xml files and making my own ad hoc db queries.

It seems to me that this situation can be relieved, without impacting the integrity of the code by creating an ancillary table or tables that provide a mapping from constants to meaningful text:- the reverse of the definitions in the code.

File cc2 shows an outline of the changes to the previous file.  The procedure as outlined would create an in-memory mapping as well as the database mapping, but that can be dispensed wth.

Such changes can be made incrementally, gradually increasing the number of mappings.  I, for example, am looking for a way to automatically assign a grade to the simple completion or non-completion of a particular activity, so I could map the constants that are of interest to me.  Others, with other areas of interest, could extend the mappings.

Is this of any interest to the developers?

Average of ratings: -
In reply to Peter West

Re: Convenience tables for mapping integer constants

by Tomasz Muras -
Picture of Core developers Picture of Plugin developers Picture of Plugins guardians Picture of Translators

Hi Peter,

I see your point and I agree in general - it would be nice to have something like this sometimes. But at least for me personally, those constants were never a big problem enough to justify the need for new arrays and DB table. There are just not that many of them at the moment - I think they are manageable as they are.

cheers,
Tomek

In reply to Tomasz Muras

Re: Convenience tables for mapping integer constants

by Peter West -

Although I am not familiar with the code base, my sense was that there are many of these constant sets.  For instance, if I search *.php files for the regex 'define *\(' I get 2124 matches.  If I then search for 'define *\(.*(true|false)' I get 334 matches.  That leaves quite a few, many of which are likely to be in constant sets.  I'll look a bit more closely at the DB schema for integers that look as though they might be defined in sets.

At first blush, there seems to be a need for only two tables; one to name the set, and the other for the actual names and values.