[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1