Get a html text from course format options

Get a html text from course format options

by Mateus Rodrigues Fuzetto -
Number of replies: 4
Hi,

i'm developing a course format that gonna have a lot of informations

I managed to create almost all of the inputs that i needed, but when i tried to create an input html(editor).
It worked, but when i tried to save the course, a error apeared.

So i want to confirm, Can i really use an input html(editor) ?

The input coursestart its where i have the problem.

The code :

public function course_format_options($foreditform = false) {
static $courseformatoptions = false;
if ($courseformatoptions === false) {
$courseconfig = get_config('moodlecourse');
$courseformatoptions = array(
'hiddensections' => array(
'default' => $courseconfig->hiddensections,
'type' => PARAM_INT
),
'coursedisplay' => array(
'default' => $courseconfig->coursedisplay,
'type' => PARAM_INT
),
'courseappearinfrontpage' => array (
'default' => get_config('format_ead', 'courseappearinfrontpage'),
'type' => PARAM_BOOL
),
'courseorderinfrontpage' => array (
'default' => get_config('format_ead', 'courseorderinfrontpage'),
'type' => PARAM_INT
),
'coursetype' => array (
'default' => get_config('format_ead', 'coursetype'),
'type' => PARAM_INT
),
'courseshowroomimage' => array(
'default' => get_config('format_ead', 'coursesampleimage'),
'type' => PARAM_FILE,
),
'coursetargetaudience' => array (
'default' => get_config('format_ead', 'coursetargetaudience'),
'type' => PARAM_TEXT
),
'courseobjective' => array (
'default' => get_config('format_ead', 'courseobjective'),
'type' => PARAM_TEXT
),
'courseworkload' => array (
'default' => get_config('format_ead', 'courseworkload'),
'type' => PARAM_TEXT
),
'coursestart' => array (
'default' => get_config('format_ead', 'coursestart'),
'type' => PARAM_CLEANHTML
),

'coursecertification' => array (
'default' => get_config('format_ead', 'coursecertification'),
'type' => PARAM_TEXT
),
);
}
if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
$courseformatoptionsedit = array(
'hiddensections' => array(
'label' => new lang_string('hiddensections'),
'help' => 'hiddensections',
'help_component' => 'moodle',
'element_type' => 'select',
'element_attributes' => array(
array(
0 => new lang_string('hiddensectionscollapsed'),
1 => new lang_string('hiddensectionsinvisible')
)
)
),
'coursedisplay' => array(
'label' => new lang_string('coursedisplay'),
'element_type' => 'select',
'element_attributes' => array(
array(
COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi')
)
)
),
'courseappearinfrontpage' => array (
'label' => new lang_string('courseappearinfrontpage', 'format_ead'),
'element_type' => 'checkbox',
),
'courseorderinfrontpage' => array (
'label' => new lang_string('courseorderinfrontpage', 'format_ead'),
'element_type' => 'select',
'element_attributes' => array (
array (
0 => '1º',
1 => '2º',
2 => '3º',
3 => '4º',
4 => '5º',
5 => '6º',
6 => '7º',
7 => '8º',
8 => '9º',
9 => '10º'
)
)
),
'coursetype' => array (
'label' => new lang_string('coursetype', 'format_ead'),
'element_type' => 'select',
'element_attributes' => array(
array(
0 => new lang_string('coursetypegraduation', 'format_ead'),
1 => new lang_string('coursetypeposgraduation', 'format_ead'),
2 => new lang_string('coursetypeextension', 'format_ead')
)
)
),
'courseshowroomimage' => array(
'label' => new lang_string('courseshowroomimage', 'format_ead'),
'element_type' => 'filemanager',
'element_attributes' => array(null,
array(
'subdirs' => 0,
'maxfiles' => 1,
'accepted_types' => array('.jpg', '.gif', '.png')
)
)
),
'coursetargetaudience' => array (
'label' => new lang_string('coursetargetaudience', 'format_ead'),
'element_type' => 'text'
),
'courseobjective' => array (
'label' => new lang_string('courseobjective', 'format_ead'),
'element_type' => 'text'
),
'courseworkload' => array (
'label' => new lang_string('courseworkload', 'format_ead'),
'element_type' => 'text'
),
'coursestart' => array (
'label' => new lang_string('coursestart', 'format_ead'),
'element_type' => 'editor'
),

'coursecertification' => array (
'label' => new lang_string('coursecertification', 'format_ead'),
'element_type' => 'text'
),
);
$courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
}
return $courseformatoptions;
}

When I try to save the following error appears:

Coding error detected, it must be fixed by a programmer: clean_param() can not process arrays, please use clean_param_array() instead.

I have done a var dump, and i discovered that only the input html(editor) coursestart pass an array not a string or int.

Output buffer: array(50) { ["returnto"]=> string(1) "0" ["returnurl"]=> object(moodle_url)#86 (9) {
["scheme":protected]=> string(5) "https" ["host":protected]=> string(31) "ava.alpha.redeinterna.toledo.br"
["port":protected]=> string(0) "" ["user":protected]=> string(0) "" ["pass":protected]=> string(0) ""
["path":protected]=> string(16) "/course/view.php" ["slashargument":protected]=> string(0) ""
["anchor":protected]=> NULL ["params":protected]=> array(1) { ["id"]=> string(1) "6" } }
["fullname"]=> string(3) "asd" ["shortname"]=> string(2) "as" ["category"]=> string(1) "1"
["visible"]=> string(1) "1" ["startdate"]=> int(1558494000) ["enddate"]=> int(1590030000)
["idnumber"]=> string(0) "" ["mform_isexpanded_id_descriptionhdr"]=> int(1) ["summary_editor"]=> array(3) {
["text"]=> string(0) "" ["format"]=> string(1) "1" ["itemid"]=> int(600770070) }
["overviewfiles_filemanager"]=> int(374904918) ["format"]=> string(3) "ead" ["hiddensections"]=> string(1) "0"
["coursedisplay"]=> string(1) "0" ["courseorderinfrontpage"]=> string(1) "0"
["coursetype"]=> string(1) "0" ["courseshowroomimage"]=> string(9) "130528568" ["coursetargetaudience"]=> string(8) "Audience"
["courseobjective"]=> string(9) "Objective" ["courseworkload"]=> string(8) "Workload"
["coursestart"]=> array(2) { ["text"]=> string(12) "<p>Start</p>" ["format"]=> string(1) "1" }
["coursecertification"]=> string(13) "Certification" ["addcourseformatoptionshere"]=> int(0) ["lang"]=> string(0) ""
["newsitems"]=> string(1) "5" ["showgrades"]=> string(1) "1" ["showreports"]=> string(1) "0" ["maxbytes"]=> string(1) "0"
["enablecompletion"]=> string(1) "1" ["groupmode"]=> string(1) "0" ["groupmodeforce"]=> string(1) "0" ["defaultgroupingid"]=> string(1) "0"
["role_1"]=> string(0) "" ["role_2"]=> string(0) "" ["role_3"]=> string(0) "" ["role_4"]=> string(0) "" ["role_5"]=> string(0) ""
["role_6"]=> string(0) "" ["role_7"]=> string(0) "" ["role_8"]=> string(0) "" ["tags"]=> array(0) { }
["saveandreturn"]=> string(15) "Save and return" ["id"]=> int(6) ["timemodified"]=> int(1558465610)
["summarytrust"]=> int(0) ["summary"]=> string(0) "" ["summaryformat"]=> string(1) "1" ["overviewfiles"]=> string(0) ""
["courseappearinfrontpage"]=> int(0) }

Can someone help ?


Average of ratings: -
In reply to Mateus Rodrigues Fuzetto

Re: Get a html text from course format options

by Mateus Rodrigues Fuzetto -

I managed to save, but I can't recover the data
In reply to Mateus Rodrigues Fuzetto

Re: Get a html text from course format options

by Đầm Váy Đẹp -

Me too !

In reply to Đầm Váy Đẹp

Re: Get a html text from course format options

by Mateus Rodrigues Fuzetto -
I finally did it, but I don't know if it was the best solution

In my course format, I had to override some other functions
To save, I subscribed two function, from course/format/lib.php in my course option lib ;

 //The function that update the values 

protected function update_format_options($data, $sectionid = null) {
global $DB;

$data = $this->validate_format_options((array)$data, $sectionid);
if (!$sectionid) {
$allformatoptions = $this->course_format_options();
$sectionid = 0;
} else {
$allformatoptions = $this->section_format_options();
}
if (empty($allformatoptions)) {
// nothing to update anyway
return false;
}
$defaultoptions = array();
$cached = array();
foreach ($allformatoptions as $key => $option) {
$defaultoptions[$key] = null;
if (array_key_exists('default', $option)) {
$defaultoptions[$key] = $option['default'];
}
$cached[$key] = ($sectionid === 0 || !empty($option['cache']));
}
$records = $DB->get_records('course_format_options',
array('courseid' => $this->courseid,
'format' => $this->format,
'sectionid' => $sectionid
), '', 'name,id,value');
$changed = $needrebuild = false;
foreach ($defaultoptions as $key => $value) {
if (isset($records[$key])) {
if (array_key_exists($key, $data) && $records[$key]->value !== $data[$key]) {
$DB->set_field('course_format_options', 'value',
is_array($data[$key]) ? $data[$key]['text'] : $data[$key] , array('id' => $records[$key]->id));
$changed = true;
$needrebuild = $needrebuild || $cached[$key];
}
} else {
if (array_key_exists($key, $data) && $data[$key] !== $value) {
$newvalue = is_array($data[$key]) ? $data[$key]['text'] : $data[$key];
$changed = true;
$needrebuild = $needrebuild || $cached[$key];
} else {
$newvalue = $value;
// we still insert entry in DB but there are no changes from user point of
// view and no need to call rebuild_course_cache()
}
$DB->insert_record('course_format_options', array(
'courseid' => $this->courseid,
'format' => $this->format,
'sectionid' => $sectionid,
'name' => $key,
'value' => $newvalue
));
}
}
if ($needrebuild) {
rebuild_course_cache($this->courseid, true);
}
if ($changed) {
// reset internal caches
if (!$sectionid) {
$this->course = false;
}
unset($this->formatoptions[$sectionid]);
}
return $changed;
}
//The function that validate the values 
protected function validate_format_options(array $rawdata, int $sectionid = null) : array {
if (!$sectionid) {
$allformatoptions = $this->course_format_options(true);
} else {
$allformatoptions = $this->section_format_options(true);
}
$data = array_intersect_key($rawdata, $allformatoptions);
foreach ($data as $key => $value) {
$option = $allformatoptions[$key] + ['type' => PARAM_RAW, 'element_type' => null, 'element_attributes' => [[]]];
if(!is_array($data[$key])) {
$data[$key] = clean_param($value, $option['type']);
}
else {
$data[$key] = clean_param_array($value, $option['type']);
}
if ($option['element_type'] === 'select' && !array_key_exists($data[$key], $option['element_attributes'][0])) {
// Value invalid for select element, skip.
unset($data[$key]);
}
}
return $data;
}

Doing that I saved the values ​​from the editor as text. And to get the values ​​I edited the function create_edit_form_elements, and i added the follow code :
 //Get the values the values from the editor coursestart that are loaded in the form and transform then in a array (
 
if(isset($mform->_defaultValues['coursestart'])) {
$mform->_defaultValues['coursestart'] = array (
'text' => $mform->_defaultValues['coursecurriculum'],
'format' => 1,
'itemid' => null
);
}
I changed the param type from the editor to
'coursestart' => array (
   'default' => get_config('format_ead', 'coursestart'),
   'type' => PARAM_CLEANHTML CHANGED THIS TO PARAM_RAW
),
With this you can retrieve the text.
The code still needs some improvements, but it works.
Average of ratings: Useful (2)
In reply to Mateus Rodrigues Fuzetto

Re: Get a html text from course format options

by stefan weber -
Picture of Plugin developers
just wanted to say thanks for posting the solution to your own problem, i just ran into the same issue smile