setValue doesn't work properly for HTML-Editor?

Re: setValue doesn't work properly for HTML-Editor?

by Süreç Özcan -
Number of replies: 0
alright - I managed a workaround by using javascript instead.

In edit_sosourcecode_form.php I did the following:

//-------------------------- SOURCE CODE ARRANGEMENT - select menu
// check which table_structure to set
$scarrangement = optional_param('scarrangement', 'column2');
$this->_questiontext = get_string($scarrangement == 'column2' ? 'table_structure_2' : 'table_structure_1', 'qtype_sosourcecode');

//call onChange customizeTableLayout-js-function defined in questiontype.php
$arrangement_attributes = array("onChange" => "customizeTableLayout('questiontext')");

$menu = array( //prepare selection options of menu
'column1' => get_string('scone', 'qtype_sosourcecode'),
'column2' => get_string('sctwo', 'qtype_sosourcecode')
);

$mform->addElement('select', 'scarrangement', get_string('scarrangement', 'qtype_sosourcecode'), $menu, $arrangement_attributes);
$mform->getElement('scarrangement')->setSelected($scarrangement); //when creating a new source code initially select 'two columns'
$mform->setHelpButton('scarrangement', array('sosourcecodearrangementhint', get_string('scarrangement', 'qtype_sosourcecode'),
'qtype_sosourcecode'));

//--------------------------- Source Code - header --------------------------------
//---------------------------------------------------------------------------------
$mform->addElement('header', 'scheader', get_string('sosourcecode', 'qtype_sosourcecode'));

//-------------------------- SOURCE CODE - HTML-Editor
$mform->addElement('htmleditor', 'questiontext', get_string('sosourcecode', 'qtype_sosourcecode'),
array('rows' => 15, 'course' => $COURSE->id));
$mform->getElement('questiontext')->setValue($this->_questiontext); // leave this for initial display
$mform->setHelpButton('questiontext', array('sosourcecodehint', get_string('sosourcecode', 'qtype_sosourcecode'),
'qtype_sosourcecode'));

//--------------------------------------------------------------------------------------------


In questiontype.php in display_question_editing_page-function I added this javascript-function:

//--------------------------------------------------------------------------------------------
parent:: display_question_editing_page($mform, $question, $wizardnow);
$formid = $mform->_form->_attributes['id']; // get form id
$col1 = get_string('table_structure_1_js', 'qtype_sosourcecode');
$col2 = get_string('table_structure_2_js', 'qtype_sosourcecode');

//This function is a work around for setting the proper table into the Source Code
//questiontext-html-editor in dependence which source code arrangement drop-down-element //has been selected.

echo <<<JS
<script type="text/javascript">
document.forms['$formid'].reloaded.value = 0;

function customizeTableLayout(id){
// which table layout is selected??
var layout_select = document.getElementById('id_scarrangement');
var layout = null;

//which source-code-arrangement drop-down-element is selected?
for(var o in layout_select.options)
{
var opt = layout_select.options[o];
if(opt.selected) layout = opt.value;
}

if(layout == null)
{
alert("FATAL ERROR!");
return null;
}

var iframes = window.frames;
var ifr = iframes[ (iframes.length == 1 ? 0 : 1) ]; //need this because I may have two html-editors
if(layout == 'column1')
{
ifr.document.body.innerHTML = '$col1';
}
else
{
ifr.document.body.innerHTML = '$col2';
}
}

</script>
JS;

//--------------------------------------------------------------------------------------------