General developer forum

external_value() line builder for Moodle webservice return functions

 
Picture of Lawrence Lagerlof
external_value() line builder for Moodle webservice return functions
 

I am going to leave this here if someone needs it in the future.

When creating a function on a Moodle webservice plugin, for each field we need to return a instance of external_value.

The query below build these lines for the table "mdl_grade_categories" but you can generate the lines for any table (just change the table_name):


select concat('\'', column_name, '\' => new external_value(',
case
when data_type = 'varchar' then 'PARAM_RAW'
when data_type = 'longtext' then 'PARAM_RAW'
when data_type = 'bigint' then 'PARAM_INT'
when data_type = 'tinyint' then 'PARAM_INT'
when data_type = 'mediumint' then 'PARAM_INT'
when data_type = 'smallint' then 'PARAM_INT'
when data_type = 'int' then 'PARAM_INT'
when data_type = 'decimal' then 'PARAM_NUMBER'
when data_type = 'double' then 'PARAM_NUMBER'
when data_type = 'float' then 'PARAM_NUMBER'
else '?'
end,
', \'', column_name, '\'',
case
when is_nullable = 'YES' then ', VALUE_OPTIONAL),'
else '),'
end) as built_external_value_return_line
from information_schema.columns
where table_schema='moodle' and table_name='mdl_grade_categories';


The result of this query:


'id' => new external_value(PARAM_INT, 'id'),
'courseid' => new external_value(PARAM_INT, 'courseid'),
'parent' => new external_value(PARAM_INT, 'parent', VALUE_OPTIONAL),
'depth' => new external_value(PARAM_INT, 'depth'),
'path' => new external_value(PARAM_RAW, 'path', VALUE_OPTIONAL),
'fullname' => new external_value(PARAM_RAW, 'fullname'),
'aggregation' => new external_value(PARAM_INT, 'aggregation'),
'keephigh' => new external_value(PARAM_INT, 'keephigh'),
'droplow' => new external_value(PARAM_INT, 'droplow'),
'aggregateonlygraded' => new external_value(PARAM_INT, 'aggregateonlygraded'),
'aggregateoutcomes' => new external_value(PARAM_INT, 'aggregateoutcomes'),
'timecreated' => new external_value(PARAM_INT, 'timecreated'),
'timemodified' => new external_value(PARAM_INT, 'timemodified'),
'hidden' => new external_value(PARAM_INT, 'hidden'),


Why? Well, I don't like repetitive tasks. smile



 
Average of ratings: -