Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 403] [Versions 39 and 311]

   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          if (!$this->uistage->is_first_stage()) {
 131              $buttonarray[] = $this->_form->createElement('submit', 'previous', get_string('previousstage', 'backup'));
 132          } else if ($this->uistage instanceof backup_ui_stage) {
 133              // Only display the button on the first stage of backup, they only place where it has an effect.
 134              $buttonarray[] = $this->_form->createElement('submit', 'oneclickbackup', get_string('jumptofinalstep', 'backup'),
 135                  array('class' => 'oneclickbackup'));
 136          }
 137          $buttonarray[] = $this->_form->createElement('cancel', 'cancel', get_string('cancel'), array('class' => 'confirmcancel'));
 138          $buttonarray[] = $this->_form->createElement(
 139              'submit',
 140              'submitbutton',
 141              get_string($this->uistage->get_ui()->get_name().'stage'.$this->uistage->get_stage().'action', 'backup'),
 142              array('class' => 'proceedbutton')
 143          );
 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          // Determine highest setting level, which is displayed in this stage. This is relevant for considering only
 187          // locks of dependency settings for parent settings, which are not displayed in this stage.
 188          $highestlevel = backup_setting::ACTIVITY_LEVEL;
 189          foreach ($settingstasks as $st) {
 190              list($setting, $task) = $st;
 191              if ($setting->get_level() < $highestlevel) {
 192                  $highestlevel = $setting->get_level();
 193              }
 194          }
 195  
 196          $defaults = array();
 197          foreach ($settingstasks as $st) {
 198              list($setting, $task) = $st;
 199              // If the setting cant be changed or isn't visible then add it as a fixed setting.
 200              if (!$setting->get_ui()->is_changeable($highestlevel) ||
 201                  $setting->get_visibility() != backup_setting::VISIBLE) {
 202                  $this->add_fixed_setting($setting, $task);
 203                  continue;
 204              }
 205  
 206              // First add the formatting for this setting.
 207              $this->add_html_formatting($setting);
 208  
 209              // Then call the add method with the get_element_properties array.
 210              call_user_func_array(array($this->_form, 'addElement'),
 211                  array_values($setting->get_ui()->get_element_properties($task, $OUTPUT)));
 212              $this->_form->setType($setting->get_ui_name(), $setting->get_param_validation());
 213              $defaults[$setting->get_ui_name()] = $setting->get_value();
 214              if ($setting->has_help()) {
 215                  list($identifier, $component) = $setting->get_help();
 216                  $this->_form->addHelpButton($setting->get_ui_name(), $identifier, $component);
 217              }
 218              $this->_form->addElement('html', html_writer::end_tag('div'));
 219          }
 220          $this->_form->setDefaults($defaults);
 221          return true;
 222      }
 223  
 224      /**
 225       * Adds a heading to the form
 226       * @param string $name
 227       * @param string $text
 228       */
 229      public function add_heading($name , $text) {
 230          $this->_form->addElement('header', $name, $text);
 231      }
 232  
 233      /**
 234       * Adds HTML formatting for the given backup setting, needed to group/segment
 235       * correctly.
 236       * @param backup_setting $setting
 237       */
 238      protected function add_html_formatting(backup_setting $setting) {
 239          $mform = $this->_form;
 240          $isincludesetting = (strpos($setting->get_name(), '_include') !== false);
 241          if ($isincludesetting && $setting->get_level() != backup_setting::ROOT_LEVEL) {
 242              switch ($setting->get_level()) {
 243                  case backup_setting::COURSE_LEVEL:
 244                      if ($this->activitydiv) {
 245                          $this->_form->addElement('html', html_writer::end_tag('div'));
 246                          $this->activitydiv = false;
 247                      }
 248                      if ($this->sectiondiv) {
 249                          $this->_form->addElement('html', html_writer::end_tag('div'));
 250                          $this->sectiondiv = false;
 251                      }
 252                      if ($this->coursediv) {
 253                          $this->_form->addElement('html', html_writer::end_tag('div'));
 254                      }
 255                      $mform->addElement('html', html_writer::start_tag('div', array('class' => 'grouped_settings course_level')));
 256                      $mform->addElement('html', html_writer::start_tag('div', array('class' => 'include_setting course_level')));
 257                      $this->coursediv = true;
 258                      break;
 259                  case backup_setting::SECTION_LEVEL:
 260                      if ($this->activitydiv) {
 261                          $this->_form->addElement('html', html_writer::end_tag('div'));
 262                          $this->activitydiv = false;
 263                      }
 264                      if ($this->sectiondiv) {
 265                          $this->_form->addElement('html', html_writer::end_tag('div'));
 266                      }
 267                      $mform->addElement('html', html_writer::start_tag('div', array('class' => 'grouped_settings section_level')));
 268                      $mform->addElement('html', html_writer::start_tag('div', array('class' => 'include_setting section_level')));
 269                      $this->sectiondiv = true;
 270                      break;
 271                  case backup_setting::ACTIVITY_LEVEL:
 272                      if ($this->activitydiv) {
 273                          $this->_form->addElement('html', html_writer::end_tag('div'));
 274                      }
 275                      $mform->addElement('html', html_writer::start_tag('div', array('class' => 'grouped_settings activity_level')));
 276                      $mform->addElement('html', html_writer::start_tag('div', array('class' => 'include_setting activity_level')));
 277                      $this->activitydiv = true;
 278                      break;
 279                  default:
 280                      $mform->addElement('html', html_writer::start_tag('div', array('class' => 'normal_setting')));
 281                      break;
 282              }
 283          } else if ($setting->get_level() == backup_setting::ROOT_LEVEL) {
 284              $mform->addElement('html', html_writer::start_tag('div', array('class' => 'root_setting')));
 285          } else {
 286              $mform->addElement('html', html_writer::start_tag('div', array('class' => 'normal_setting')));
 287          }
 288      }
 289  
 290      /**
 291       * Adds a fixed or static setting to the form
 292       * @param backup_setting $setting
 293       * @param base_task $task
 294       */
 295      public function add_fixed_setting(backup_setting $setting, base_task $task) {
 296          global $OUTPUT;
 297          $settingui = $setting->get_ui();
 298          if ($setting->get_visibility() == backup_setting::VISIBLE) {
 299              $this->add_html_formatting($setting);
 300              switch ($setting->get_status()) {
 301                  case backup_setting::LOCKED_BY_PERMISSION:
 302                      $icon = ' '.$OUTPUT->pix_icon('i/permissionlock', get_string('lockedbypermission', 'backup'), 'moodle',
 303                              array('class' => 'smallicon lockedicon permissionlock'));
 304                      break;
 305                  case backup_setting::LOCKED_BY_CONFIG:
 306                      $icon = ' '.$OUTPUT->pix_icon('i/configlock', get_string('lockedbyconfig', 'backup'), 'moodle',
 307                              array('class' => 'smallicon lockedicon configlock'));
 308                      break;
 309                  case backup_setting::LOCKED_BY_HIERARCHY:
 310                      $icon = ' '.$OUTPUT->pix_icon('i/hierarchylock', get_string('lockedbyhierarchy', 'backup'), 'moodle',
 311                              array('class' => 'smallicon lockedicon configlock'));
 312                      break;
 313                  default:
 314                      $icon = '';
 315                      break;
 316              }
 317              $context = context_course::instance($task->get_courseid());
 318              $label = format_string($settingui->get_label($task), true, array('context' => $context));
 319              $labelicon = $settingui->get_icon();
 320              if (!empty($labelicon)) {
 321                  $label .= $OUTPUT->render($labelicon);
 322              }
 323              $this->_form->addElement('static', 'static_'.$settingui->get_name(), $label, $settingui->get_static_value().$icon);
 324              $this->_form->addElement('html', html_writer::end_tag('div'));
 325          }
 326          $this->_form->addElement('hidden', $settingui->get_name(), $settingui->get_value());
 327          $this->_form->setType($settingui->get_name(), $settingui->get_param_validation());
 328      }
 329  
 330      /**
 331       * Adds dependencies to the form recursively
 332       *
 333       * @param backup_setting $setting
 334       */
 335      public function add_dependencies(backup_setting $setting) {
 336          $mform = $this->_form;
 337          // Apply all dependencies for backup.
 338          foreach ($setting->get_my_dependency_properties() as $key => $dependency) {
 339              call_user_func_array(array($this->_form, 'disabledIf'), array_values($dependency));
 340          }
 341      }
 342  
 343      /**
 344       * Returns true if the form was cancelled, false otherwise
 345       * @return bool
 346       */
 347      public function is_cancelled() {
 348          return (optional_param('cancel', false, PARAM_BOOL) || parent::is_cancelled());
 349      }
 350  
 351      /**
 352       * Removes an element from the form if it exists
 353       * @param string $elementname
 354       * @return bool
 355       */
 356      public function remove_element($elementname) {
 357          if ($this->_form->elementExists($elementname)) {
 358              return $this->_form->removeElement($elementname);
 359          } else {
 360              return false;
 361          }
 362      }
 363  
 364      /**
 365       * Gets an element from the form if it exists
 366       *
 367       * @param string $elementname
 368       * @return HTML_QuickForm_input|MoodleQuickForm_group
 369       */
 370      public function get_element($elementname) {
 371          if ($this->_form->elementExists($elementname)) {
 372              return $this->_form->getElement($elementname);
 373          } else {
 374              return false;
 375          }
 376      }
 377  
 378      /**
 379       * Displays the form
 380       */
 381      public function display() {
 382          global $PAGE, $COURSE;
 383  
 384          $this->require_definition_after_data();
 385  
 386          $config = new stdClass;
 387          if ($this->uistage->get_ui() instanceof import_ui) {
 388              $config->title = get_string('confirmcancelimport', 'backup');
 389          } else if ($this->uistage->get_ui() instanceof restore_ui) {
 390              $config->title = get_string('confirmcancelrestore', 'backup');
 391          } else {
 392              $config->title = get_string('confirmcancel', 'backup');
 393          }
 394          $config->question = get_string('confirmcancelquestion', 'backup');
 395          $config->yesLabel = $config->title;
 396          $config->noLabel = get_string('confirmcancelno', 'backup');
 397          $config->closeButtonTitle = get_string('close', 'editor');
 398          $PAGE->requires->yui_module(
 399              'moodle-backup-confirmcancel',
 400              'M.core_backup.confirmcancel.watch_cancel_buttons',
 401              array($config)
 402          );
 403  
 404          // Get list of module types on course.
 405          $modinfo = get_fast_modinfo($COURSE);
 406          $modnames = array_map('strval', $modinfo->get_used_module_names(true));
 407          core_collator::asort($modnames);
 408          $PAGE->requires->yui_module('moodle-backup-backupselectall', 'M.core_backup.backupselectall',
 409                  array($modnames));
 410          $PAGE->requires->strings_for_js(array('select', 'all', 'none'), 'moodle');
 411          $PAGE->requires->strings_for_js(array('showtypes', 'hidetypes'), 'backup');
 412  
 413          parent::display();
 414      }
 415  
 416      /**
 417       * Ensures the the definition after data is loaded
 418       */
 419      public function require_definition_after_data() {
 420          if (!$this->_definition_finalized) {
 421              $this->definition_after_data();
 422          }
 423      }
 424  }