/**
  * To autofocus on first form element or first element with error.
  *
  * @param string $name if this is set then the focus is forced to a field with this name
  *
  * @return string  javascript to select form element with first error or
  *                  first element if no errors. Use this as a parameter
  *                  when calling print_header
  */
 function focus($name = NULL)
 {
     $form =& $this->_form;
     $elkeys = array_keys($form->_elementIndex);
     if (isset($form->_errors) && 0 != count($form->_errors)) {
         $errorkeys = array_keys($form->_errors);
         $elkeys = array_intersect($elkeys, $errorkeys);
     }
     $names = null;
     while (!$names) {
         $el = array_shift($elkeys);
         $names = $form->_getElNamesRecursive($el);
     }
     if (empty($name)) {
         $name = array_shift($names);
     }
     $focus = 'forms[\'' . $this->_form->getAttribute('id') . '\'].elements[\'' . $name . '\']';
     return $focus;
 }
Exemple #2
0
 /**
  * What to do when starting the form
  *
  * @param MoodleQuickForm $form reference of the form
  */
 function startForm(&$form)
 {
     global $PAGE;
     $this->_reqHTML = $form->getReqHTML();
     $this->_elementTemplates = str_replace('{req}', $this->_reqHTML, $this->_elementTemplates);
     $this->_advancedHTML = $form->getAdvancedHTML();
     $this->_collapseButtons = '';
     $formid = $form->getAttribute('id');
     parent::startForm($form);
     if ($form->isFrozen()) {
         $this->_formTemplate = "\n<div class=\"mform frozen\">\n{content}\n</div>";
     } else {
         $this->_formTemplate = "\n<form{attributes}>\n\t<div style=\"display: none;\">{hidden}</div>\n{collapsebtns}\n{content}\n</form>";
         $this->_hiddenHtml .= $form->_pageparams;
     }
     if ($form->is_form_change_checker_enabled()) {
         $PAGE->requires->yui_module('moodle-core-formchangechecker', 'M.core_formchangechecker.init', array(array('formid' => $formid)));
         $PAGE->requires->string_for_js('changesmadereallygoaway', 'moodle');
     }
     if (!empty($this->_collapsibleElements)) {
         if (count($this->_collapsibleElements) > 1) {
             $this->_collapseButtons = $this->_collapseButtonsTemplate;
             $this->_collapseButtons = str_replace('{strexpandall}', get_string('expandall'), $this->_collapseButtons);
             $PAGE->requires->strings_for_js(array('collapseall', 'expandall'), 'moodle');
         }
         $PAGE->requires->yui_module('moodle-form-shortforms', 'M.form.shortforms', array(array('formid' => $formid)));
     }
     if (!empty($this->_advancedElements)) {
         $PAGE->requires->strings_for_js(array('showmore', 'showless'), 'form');
         $PAGE->requires->yui_module('moodle-form-showadvanced', 'M.form.showadvanced', array(array('formid' => $formid)));
     }
 }