[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/backup/util/ui/ -> base_moodleform.class.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This file contains the generic moodleform bridge for the backup user interface
  20   * as well as the individual forms that relate to the different stages the user
  21   * interface can exist within.
  22   *
  23   * @package   moodlecore
  24   * @copyright 2010 Sam Hemelryk
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once($CFG->libdir . '/formslib.php');
  31  
  32  /**
  33   * Backup moodleform bridge
  34   *
  35   * Ahhh the mighty moodleform bridge! Strong enough to take the weight of 682 full
  36   * grown african swallows all of whom have been carring coconuts for several days.
  37   * EWWWWW!!!!!!!!!!!!!!!!!!!!!!!!
  38   *
  39   * @copyright 2010 Sam Hemelryk
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  abstract class base_moodleform extends moodleform {
  43      /**
  44       * The stage this form belongs to
  45       * @var base_ui_stage
  46       */
  47      protected $uistage = null;
  48      /**
  49       * True if we have a course div open, false otherwise
  50       * @var bool
  51       */
  52      protected $coursediv = false;
  53      /**
  54       * True if we have a section div open, false otherwise
  55       * @var bool
  56       */
  57      protected $sectiondiv = false;
  58      /**
  59       * True if we have an activity div open, false otherwise
  60       * @var bool
  61       */
  62      protected $activitydiv = false;
  63      /**
  64       * Creates the form
  65       *
  66       * @param backup_ui_stage $uistage
  67       * @param moodle_url|string $action
  68       * @param mixed $customdata
  69       * @param string $method get|post
  70       * @param string $target
  71       * @param array $attributes
  72       * @param bool $editable
  73       */
  74      function __construct(base_ui_stage $uistage, $action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true) {
  75          $this->uistage = $uistage;
  76          // Add a class to the attributes to prevent the default collapsible behaviour.
  77          if (!$attributes) {
  78              $attributes = array();
  79          }
  80          $attributes['class'] = 'unresponsive';
  81          if (!isset($attributes['enctype'])) {
  82              $attributes['enctype'] = 'application/x-www-form-urlencoded'; // Enforce compatibility with our max_input_vars hack.
  83          }
  84          parent::__construct($action, $customdata, $method, $target, $attributes, $editable);
  85      }
  86      /**
  87       * The standard form definition... obviously not much here
  88       */
  89      function definition() {
  90          $ui = $this->uistage->get_ui();
  91          $mform = $this->_form;
  92          $mform->setDisableShortforms();
  93          $stage = $mform->addElement('hidden', 'stage', $this->uistage->get_stage());
  94          $mform->setType('stage', PARAM_INT);
  95          $stage = $mform->addElement('hidden', $ui->get_name(), $ui->get_uniqueid());
  96          $mform->setType($ui->get_name(), PARAM_ALPHANUM);
  97          $params = $this->uistage->get_params();
  98          if (is_array($params) && count($params) > 0) {
  99              foreach ($params as $name=>$value) {
 100                  // TODO: Horrible hack, but current backup ui structure does not allow
 101                  // to make this easy (only changing params to objects that would be
 102                  // possible. MDL-38735.
 103                  $intparams = array(
 104                          'contextid', 'importid', 'target');
 105                  $stage = $mform->addElement('hidden', $name, $value);
 106                  if (in_array($name, $intparams)) {
 107                      $mform->setType($name, PARAM_INT);
 108                  } else {
 109                      // Adding setType() to avoid missing setType() warnings.
 110                      // MDL-39126: support $mform->setType() for additional backup parameters.
 111                      $mform->setType($name, PARAM_RAW);
 112                  }
 113              }
 114          }
 115      }
 116      /**
 117       * Definition applied after the data is organised.. why's it here? because I want
 118       * to add elements on the fly.
 119       * @global moodle_page $PAGE
 120       */
 121      function definition_after_data() {
 122          $buttonarray=array();
 123          $buttonarray[] = $this->_form->createElement('submit', 'submitbutton', get_string($this->uistage->get_ui()->get_name().'stage'.$this->uistage->get_stage().'action', 'backup'), array('class'=>'proceedbutton'));
 124          if (!$this->uistage->is_first_stage()) {
 125              $buttonarray[] = $this->_form->createElement('submit', 'previous', get_string('previousstage','backup'));
 126          } else if ($this->uistage instanceof backup_ui_stage) {
 127              // Only display the button on the first stage of backup, they only place where it has an effect.
 128              $buttonarray[] = $this->_form->createElement('submit', 'oneclickbackup', get_string('jumptofinalstep', 'backup'),
 129                  array('class' => 'oneclickbackup'));
 130          }
 131          $buttonarray[] = $this->_form->createElement('cancel', 'cancel', get_string('cancel'), array('class'=>'confirmcancel'));
 132          $this->_form->addGroup($buttonarray, 'buttonar', '', array(' '), false);
 133          $this->_form->closeHeaderBefore('buttonar');
 134  
 135          $this->_definition_finalized = true;
 136      }
 137  
 138      /**
 139       * Closes any open divs
 140       */
 141      function close_task_divs() {
 142          if ($this->activitydiv) {
 143              $this->_form->addElement('html', html_writer::end_tag('div'));
 144              $this->activitydiv = false;
 145          }
 146          if ($this->sectiondiv) {
 147              $this->_form->addElement('html', html_writer::end_tag('div'));
 148              $this->sectiondiv = false;
 149          }
 150          if ($this->coursediv) {
 151              $this->_form->addElement('html', html_writer::end_tag('div'));
 152              $this->coursediv = false;
 153          }
 154      }
 155      /**
 156       * Adds the backup_setting as a element to the form
 157       * @param backup_setting $setting
 158       * @return bool
 159       */
 160      function add_setting(backup_setting $setting, base_task $task=null) {
 161          return $this->add_settings(array(array($setting, $task)));
 162      }
 163      /**
 164       * Adds multiple backup_settings as elements to the form
 165       * @param array $settingstasks Consists of array($setting, $task) elements
 166       * @return bool
 167       */
 168      public function add_settings(array $settingstasks) {
 169          global $OUTPUT;
 170  
 171          $defaults = array();
 172          foreach ($settingstasks as $st) {
 173              list($setting, $task) = $st;
 174              // If the setting cant be changed or isn't visible then add it as a fixed setting.
 175              if (!$setting->get_ui()->is_changeable() || $setting->get_visibility() != backup_setting::VISIBLE) {
 176                  $this->add_fixed_setting($setting, $task);
 177                  continue;
 178              }
 179  
 180              // First add the formatting for this setting
 181              $this->add_html_formatting($setting);
 182  
 183              // Then call the add method with the get_element_properties array
 184              call_user_func_array(array($this->_form, 'addElement'), $setting->get_ui()->get_element_properties($task, $OUTPUT));
 185              $this->_form->setType($setting->get_ui_name(), $setting->get_param_validation());
 186              $defaults[$setting->get_ui_name()] = $setting->get_value();
 187              if ($setting->has_help()) {
 188                  list($identifier, $component) = $setting->get_help();
 189                  $this->_form->addHelpButton($setting->get_ui_name(), $identifier, $component);
 190              }
 191              $this->_form->addElement('html', html_writer::end_tag('div'));
 192          }
 193          $this->_form->setDefaults($defaults);
 194          return true;
 195      }
 196      /**
 197       * Adds a heading to the form
 198       * @param string $name
 199       * @param string $text
 200       */
 201      function add_heading($name , $text) {
 202          $this->_form->addElement('header', $name, $text);
 203      }
 204      /**
 205       * Adds HTML formatting for the given backup setting, needed to group/segment
 206       * correctly.
 207       * @param backup_setting $setting
 208       */
 209      protected function add_html_formatting(backup_setting $setting) {
 210          $mform = $this->_form;
 211          $isincludesetting = (strpos($setting->get_name(), '_include')!==false);
 212          if ($isincludesetting && $setting->get_level() != backup_setting::ROOT_LEVEL)  {
 213              switch ($setting->get_level()) {
 214                  case backup_setting::COURSE_LEVEL:
 215                      if ($this->activitydiv) {
 216                          $this->_form->addElement('html', html_writer::end_tag('div'));
 217                          $this->activitydiv = false;
 218                      }
 219                      if ($this->sectiondiv) {
 220                          $this->_form->addElement('html', html_writer::end_tag('div'));
 221                          $this->sectiondiv = false;
 222                      }
 223                      if ($this->coursediv) {
 224                          $this->_form->addElement('html', html_writer::end_tag('div'));
 225                      }
 226                      $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings course_level')));
 227                      $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting course_level')));
 228                      $this->coursediv = true;
 229                      break;
 230                  case backup_setting::SECTION_LEVEL:
 231                      if ($this->activitydiv) {
 232                          $this->_form->addElement('html', html_writer::end_tag('div'));
 233                          $this->activitydiv = false;
 234                      }
 235                      if ($this->sectiondiv) {
 236                          $this->_form->addElement('html', html_writer::end_tag('div'));
 237                      }
 238                      $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings section_level')));
 239                      $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting section_level')));
 240                      $this->sectiondiv = true;
 241                      break;
 242                  case backup_setting::ACTIVITY_LEVEL:
 243                      if ($this->activitydiv) {
 244                          $this->_form->addElement('html', html_writer::end_tag('div'));
 245                      }
 246                      $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings activity_level')));
 247                      $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting activity_level')));
 248                      $this->activitydiv = true;
 249                      break;
 250                  default:
 251                      $mform->addElement('html', html_writer::start_tag('div', array('class'=>'normal_setting')));
 252                      break;
 253              }
 254          } else if ($setting->get_level() == backup_setting::ROOT_LEVEL) {
 255              $mform->addElement('html', html_writer::start_tag('div', array('class'=>'root_setting')));
 256          } else {
 257              $mform->addElement('html', html_writer::start_tag('div', array('class'=>'normal_setting')));
 258          }
 259      }
 260      /**
 261       * Adds a fixed or static setting to the form
 262       * @param backup_setting $setting
 263       */
 264      function add_fixed_setting(backup_setting $setting, base_task $task) {
 265          global $OUTPUT;
 266          $settingui = $setting->get_ui();
 267          if ($setting->get_visibility() == backup_setting::VISIBLE) {
 268              $this->add_html_formatting($setting);
 269              switch ($setting->get_status()) {
 270                  case backup_setting::LOCKED_BY_PERMISSION:
 271                      $icon = ' '.$OUTPUT->pix_icon('i/permissionlock', get_string('lockedbypermission', 'backup'), 'moodle', array('class'=>'smallicon lockedicon permissionlock'));
 272                      break;
 273                  case backup_setting::LOCKED_BY_CONFIG:
 274                      $icon = ' '.$OUTPUT->pix_icon('i/configlock', get_string('lockedbyconfig', 'backup'), 'moodle', array('class'=>'smallicon lockedicon configlock'));
 275                      break;
 276                  case backup_setting::LOCKED_BY_HIERARCHY:
 277                      $icon = ' '.$OUTPUT->pix_icon('i/hierarchylock', get_string('lockedbyhierarchy', 'backup'), 'moodle', array('class'=>'smallicon lockedicon configlock'));
 278                      break;
 279                  default:
 280                      $icon = '';
 281                      break;
 282              }
 283              $label = $settingui->get_label($task);
 284              $labelicon = $settingui->get_icon();
 285              if (!empty($labelicon)) {
 286                  $label .= '&nbsp;'.$OUTPUT->render($labelicon);
 287              }
 288              $this->_form->addElement('static', 'static_'.$settingui->get_name(), $label, $settingui->get_static_value().$icon);
 289              $this->_form->addElement('html', html_writer::end_tag('div'));
 290          }
 291          $this->_form->addElement('hidden', $settingui->get_name(), $settingui->get_value());
 292          $this->_form->setType($settingui->get_name(), $settingui->get_param_validation());
 293      }
 294      /**
 295       * Adds dependencies to the form recursively
 296       *
 297       * @param backup_setting $setting
 298       */
 299      function add_dependencies(backup_setting $setting) {
 300          $mform = $this->_form;
 301          // Apply all dependencies for backup
 302          foreach ($setting->get_my_dependency_properties() as $key=>$dependency) {
 303              call_user_func_array(array($this->_form, 'disabledIf'), $dependency);
 304          }
 305      }
 306      /**
 307       * Returns true if the form was cancelled, false otherwise
 308       * @return bool
 309       */
 310      public function is_cancelled() {
 311          return (optional_param('cancel', false, PARAM_BOOL) || parent::is_cancelled());
 312      }
 313  
 314      /**
 315       * Removes an element from the form if it exists
 316       * @param string $elementname
 317       * @return bool
 318       */
 319      public function remove_element($elementname) {
 320          if ($this->_form->elementExists($elementname)) {
 321              return $this->_form->removeElement($elementname);
 322          } else {
 323              return false;
 324          }
 325      }
 326  
 327      /**
 328       * Gets an element from the form if it exists
 329       *
 330       * @param string $elementname
 331       * @return HTML_QuickForm_input|MoodleQuickForm_group
 332       */
 333      public function get_element($elementname) {
 334          if ($this->_form->elementExists($elementname)) {
 335              return $this->_form->getElement($elementname);
 336          } else {
 337              return false;
 338          }
 339      }
 340  
 341      /**
 342       * Displays the form
 343       */
 344      public function display() {
 345          global $PAGE, $COURSE;
 346  
 347          $this->require_definition_after_data();
 348  
 349          $config = new stdClass;
 350          $config->title = get_string('confirmcancel', 'backup');
 351          $config->question = get_string('confirmcancelquestion', 'backup');
 352          $config->yesLabel = get_string('confirmcancelyes', 'backup');
 353          $config->noLabel = get_string('confirmcancelno', 'backup');
 354          $config->closeButtonTitle = get_string('close', 'editor');
 355          $PAGE->requires->yui_module('moodle-backup-confirmcancel', 'M.core_backup.confirmcancel.watch_cancel_buttons', array($config));
 356  
 357          // Get list of module types on course.
 358          $modinfo = get_fast_modinfo($COURSE);
 359          $modnames = $modinfo->get_used_module_names(true);
 360          $PAGE->requires->yui_module('moodle-backup-backupselectall', 'M.core_backup.backupselectall',
 361                  array($modnames));
 362          $PAGE->requires->strings_for_js(array('select', 'all', 'none'), 'moodle');
 363          $PAGE->requires->strings_for_js(array('showtypes', 'hidetypes'), 'backup');
 364  
 365          parent::display();
 366      }
 367  
 368      /**
 369       * Ensures the the definition after data is loaded
 370       */
 371      public function require_definition_after_data() {
 372          if (!$this->_definition_finalized) {
 373              $this->definition_after_data();
 374          }
 375      }
 376  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1