<?
class frmDragDrop2 extends MForm
{
    private $correct = "a thing of beauty is a joy forever by John Keats";

    public function __construct()
    {
        parent::__construct("Drag&Drop - Sample 2");
        $this->eventHandler();
    }

    public function createFields()
    { 
        $ui = $this->manager->getUI();

        $this->page->addStyleCode(".dropHover {background-color: #EEE}");
        $phrase = array('a','thing','a','Keats','by','joy','of','is','beauty','forever','John');

        $dd = new MDragDrop('dd2');

        $i = 0;
        foreach($phrase as $p)
        {
           $d = new MDiv("{$p}_" . $i++,$p,'word');
           $d->margin = '5px';
           $d->backgroundColor = '#ffA';
           $drag[] = $d;
        }

        $box = new MBaseGroup('dragBox','Drag a word',$drag);
        $dd->addDraggable($box);

        $dropOn = new MDiv('drop1','&nbsp;');
//        $dropOn->height = '30px';
        $dropOn->width = '100%';

        $box1 = new MBaseGroup('dropBox','Build the phrase: drop words here, in order', array($dropOn));

        $dd->addDropZone($box1);

        $status = new MDiv('divText');

        $fields = array(
            $box,
            $box1,
            $dd,
            new MButton('btnTest','Test'),
            new MSpacer('10px')
        );
        $this->setFields($fields);
        $this->defaultButton = false;
    }

    public function btnTest_click()
    {
        $in = $this->dd2->getValue();
        foreach($in as $word=>$box)
        {
            $s .= substr($word,0,strpos($word,'_')) . " ";
        }         
        if (trim($s) == $this->correct)
        {
            $a = new MLabel($s . " [Congratulations!] ", 'blue', true);
        }
        else
        {
            $a = new MLabel($s . " [Wrong! Try again!]", 'red', true);
        }
        $this->addField($a);
    }

}
?>
