Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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 all the form definitions used by the portfolio code.
  19   *
  20   * @package core_portfolio
  21   * @copyright 2008 Penny Leach <penny@catalyst.net.nz>,
  22   *            Martin Dougiamas (http://dougiamas.com)
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  // make sure we include moodleform first!
  29  require_once ($CFG->libdir.'/formslib.php');
  30  
  31  /**
  32   * During-export config form.
  33   *
  34   * This is the form that is actually used while exporting.
  35   * Plugins and callers don't get to define their own class
  36   * as we have to handle form elements from both places
  37   *
  38   * @package core_portfolio
  39   * @category portfolio
  40   * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
  41   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  final class portfolio_export_form extends moodleform {
  44  
  45      /**
  46       * prepare form
  47       */
  48      public function definition() {
  49  
  50          $mform =& $this->_form;
  51          $mform->addElement('hidden', 'stage', PORTFOLIO_STAGE_CONFIG);
  52          $mform->addElement('hidden', 'id', $this->_customdata['id']);
  53          $mform->addElement('hidden', 'instance', $this->_customdata['instance']->get('id'));
  54          $mform->setType('instance', PARAM_INT);
  55          $mform->setType('stage', PARAM_INT);
  56          $mform->setType('id', PARAM_INT);
  57  
  58          if (array_key_exists('formats', $this->_customdata) && is_array($this->_customdata['formats'])) {
  59              if (count($this->_customdata['formats']) > 1) {
  60                  $options = array();
  61                  foreach ($this->_customdata['formats'] as $key) {
  62                      $options[$key] = get_string('format_' . $key, 'portfolio');
  63                  }
  64                  $mform->addElement('select', 'format', get_string('availableformats', 'portfolio'), $options);
  65              } else {
  66                  $f = array_shift($this->_customdata['formats']);
  67                  $mform->addElement('hidden', 'format', $f);
  68                  $mform->setType('format', PARAM_RAW);
  69              }
  70          }
  71  
  72          // only display the option to wait or not if it's applicable
  73          if (array_key_exists('expectedtime', $this->_customdata)
  74              && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_LOW
  75              && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_FORCEQUEUE) {
  76              $radioarray = array();
  77              $radioarray[] = $mform->createElement('radio', 'wait', '', get_string('wait', 'portfolio'), 1);
  78              $radioarray[] = $mform->createElement('radio', 'wait', '', get_string('dontwait', 'portfolio'),  0);
  79              $mform->addGroup($radioarray, 'radioar', get_string('wanttowait_' . $this->_customdata['expectedtime'], 'portfolio') , array(' '), false);
  80              $mform->setDefault('wait', 0);
  81          } else {
  82              if ($this->_customdata['expectedtime'] == PORTFOLIO_TIME_LOW) {
  83                  $mform->addElement('hidden', 'wait', 1);
  84              } else {
  85                  $mform->addElement('hidden', 'wait', 0);
  86              }
  87              $mform->setType('wait', PARAM_INT);
  88          }
  89  
  90          if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
  91              $this->_customdata['plugin']->export_config_form($mform, $this->_customdata['userid']);
  92          }
  93  
  94          if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
  95              $this->_customdata['caller']->export_config_form($mform, $this->_customdata['instance'], $this->_customdata['userid']);
  96          }
  97  
  98          $this->add_action_buttons(true, get_string('next'));
  99      }
 100  
 101      /**
 102       * Validate portfolio export form
 103       *
 104       * @param stdClass $data portfolio information from form data
 105       * @return array
 106       */
 107      public function validation($data, $files) {
 108  
 109          $errors = array();
 110  
 111          if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
 112              $pluginerrors = $this->_customdata['plugin']->export_config_validation($data);
 113              if (is_array($pluginerrors)) {
 114                  $errors = $pluginerrors;
 115              }
 116          }
 117          if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
 118              $callererrors = $this->_customdata['caller']->export_config_validation($data);
 119              if (is_array($callererrors)) {
 120                  $errors = array_merge($errors, $callererrors);
 121              }
 122          }
 123          return $errors;
 124      }
 125  }
 126  
 127  /**
 128   * Admin config form.
 129   *
 130   * This form is extendable by plugins who want the admin to be able to configure more than just the name of the instance.
 131   * This is NOT done by subclassing this class.
 132   *
 133   * @package core_portfolio
 134   * @category portfolio
 135   * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
 136   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 137   */
 138  final class portfolio_admin_form extends moodleform {
 139  
 140      /** @var object to hold porfolio instance configuration */
 141      protected $instance;
 142  
 143      /** @var string plugin name*/
 144      protected $plugin;
 145  
 146      /** @var string portfolio plugin name*/
 147      protected $portfolio;
 148  
 149      /** @var string plugin availability*/
 150      protected $action;
 151  
 152      /** @var int portfolio plugin visibility*/
 153      protected $visible;
 154  
 155      /**
 156       * prepare form
 157       */
 158      public function definition() {
 159          global $CFG;
 160          $this->plugin = $this->_customdata['plugin'];
 161          $this->instance = (isset($this->_customdata['instance'])
 162                  && is_subclass_of($this->_customdata['instance'], 'portfolio_plugin_base'))
 163              ? $this->_customdata['instance'] : null;
 164          $this->portfolio = $this->_customdata['portfolio'];
 165          $this->action = $this->_customdata['action'];
 166          $this->visible = $this->_customdata['visible'];
 167  
 168          $mform =& $this->_form;
 169          $strrequired = get_string('required');
 170  
 171          $mform->addElement('hidden', 'pf', $this->portfolio);
 172          $mform->setType('pf', PARAM_ALPHA);
 173          $mform->addElement('hidden', 'action', $this->action);
 174          $mform->setType('action', PARAM_ALPHA);
 175          $mform->addElement('hidden', 'visible', $this->visible);
 176          $mform->setType('visible', PARAM_INT);
 177          $mform->addElement('hidden', 'plugin', $this->plugin);
 178          $mform->setType('plugin', PARAM_PLUGIN);
 179  
 180          if (!$this->instance) {
 181              $insane = portfolio_instance_sanity_check($this->instance);
 182          } else {
 183              $insane = portfolio_plugin_sanity_check($this->plugin);
 184          }
 185  
 186          if (isset($insane) && is_array($insane)) {
 187              $insane = array_shift($insane);
 188          }
 189          if (isset($insane) && is_string($insane)) { // something went wrong, warn...
 190              $mform->addElement('warning', 'insane', null, get_string($insane, 'portfolio_' . $this->plugin));
 191          }
 192  
 193          $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
 194          $mform->addRule('name', $strrequired, 'required', null, 'client');
 195          $mform->setType('name', PARAM_TEXT);
 196  
 197          // let the plugin add the fields they want (either statically or not)
 198          if (portfolio_static_function($this->plugin, 'has_admin_config')) {
 199              require_once($CFG->libdir . '/portfolio/plugin.php');
 200              require_once($CFG->dirroot . '/portfolio/' . $this->plugin .  '/lib.php');
 201              $classname = 'portfolio_plugin_' . $this->plugin;
 202              $classname::admin_config_form($mform);
 203          }
 204  
 205          // and set the data if we have some.
 206          if ($this->instance) {
 207              $data = array('name' => $this->instance->get('name'));
 208              foreach ($this->instance->get_allowed_config() as $config) {
 209                  $data[$config] = $this->instance->get_config($config);
 210              }
 211              $this->set_data($data);
 212          } else {
 213              $this->set_data(array('name' => portfolio_static_function($this->plugin, 'get_name')));
 214          }
 215  
 216          $this->add_action_buttons(true, get_string('save', 'portfolio'));
 217      }
 218  
 219      /**
 220       * Validate admin config form
 221       *
 222       * @param stdObject $data form data
 223       * @return array
 224       */
 225      public function validation($data, $files) {
 226          global $DB;
 227  
 228          $errors = array();
 229          if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
 230              $errors = array('name' => get_string('err_uniquename', 'portfolio'));
 231          }
 232  
 233          $pluginerrors = array();
 234          $pluginerrors = portfolio_static_function($this->plugin, 'admin_config_validation', $data);
 235          if (is_array($pluginerrors)) {
 236              $errors = array_merge($errors, $pluginerrors);
 237          }
 238          return $errors;
 239      }
 240  }
 241  
 242  /**
 243   * User config form.
 244   *
 245   * This is the form for letting the user configure an instance of a plugin.
 246   * In order to extend this, you don't subclass this in the plugin.
 247   *
 248   * @package core_portfolio
 249   * @category portfolio
 250   * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
 251   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 252   */
 253  final class portfolio_user_form extends moodleform {
 254  
 255      /** @var object  user porfolio instance */
 256      protected $instance;
 257  
 258      /** @var int hold user id */
 259      protected $userid;
 260  
 261      /**
 262       * prepare form
 263       */
 264      public function definition() {
 265          $this->instance = $this->_customdata['instance'];
 266          $this->userid = $this->_customdata['userid'];
 267  
 268          $this->_form->addElement('hidden', 'config', $this->instance->get('id'));
 269          $this->_form->setType('config', PARAM_INT);
 270  
 271          $this->instance->user_config_form($this->_form, $this->userid);
 272  
 273          $data = array();
 274          foreach ($this->instance->get_allowed_user_config() as $config) {
 275              $data[$config] = $this->instance->get_user_config($config, $this->userid);
 276          }
 277          $this->set_data($data);
 278          $this->add_action_buttons(true, get_string('save', 'portfolio'));
 279      }
 280  
 281      /**
 282       * User user config form.
 283       *
 284       * @param stdClass $data form data
 285       */
 286      public function validation($data, $files) {
 287  
 288          $errors = $this->instance->user_config_validation($data);
 289  
 290      }
 291  }
 292  
 293  
 294  /**
 295   * Form that just contains the dropdown menu of available instances.
 296   *
 297   * This is not used by portfolio_add_button, but on the first step of the export,
 298   * if the plugin instance has not yet been selected.
 299   *
 300   * @package core_portfolio
 301   * @category portfolio
 302   * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
 303   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 304   */
 305  class portfolio_instance_select extends moodleform {
 306  
 307      /** @var portfolio_caller_base plugin instance */
 308      private $caller;
 309  
 310      /**
 311       * The required basic elements to the form.
 312       */
 313      function definition() {
 314          $this->caller = $this->_customdata['caller'];
 315          $options = $this->_customdata['options'];
 316          $mform =& $this->_form;
 317          $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options);
 318          $mform->addElement('hidden', 'id', $this->_customdata['id']);
 319          $mform->setType('id', PARAM_INT);
 320          $this->add_action_buttons(true, get_string('next'));
 321      }
 322  }