Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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   * Bulk course upload step 2.
  19   *
  20   * @package    tool_uploadcourse
  21   * @copyright  2011 Piers Harding
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->dirroot . '/course/lib.php');
  28  
  29  /**
  30   * Specify course upload details.
  31   *
  32   * @package    tool_uploadcourse
  33   * @copyright  2011 Piers Harding
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class tool_uploadcourse_step2_form extends tool_uploadcourse_base_form {
  37  
  38      /**
  39       * The standard form definiton.
  40       * @return void.
  41       */
  42      public function definition () {
  43          global $CFG;
  44  
  45          $mform   = $this->_form;
  46          $data    = $this->_customdata['data'];
  47          $courseconfig = get_config('moodlecourse');
  48  
  49          // Import options.
  50          $this->add_import_options();
  51  
  52          // Course options.
  53          $mform->addElement('header', 'courseoptionshdr', get_string('courseprocess', 'tool_uploadcourse'));
  54          $mform->setExpanded('courseoptionshdr', true);
  55  
  56          $mform->addElement('text', 'options[shortnametemplate]', get_string('shortnametemplate', 'tool_uploadcourse'),
  57              'maxlength="100" size="20"');
  58          $mform->setType('options[shortnametemplate]', PARAM_RAW);
  59          $mform->addHelpButton('options[shortnametemplate]', 'shortnametemplate', 'tool_uploadcourse');
  60          $mform->hideIf('options[shortnametemplate]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE);
  61          $mform->hideIf('options[shortnametemplate]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_UPDATE_ONLY);
  62  
  63          // Restore file is not in the array options on purpose, because formslib can't handle it!
  64          $contextid = $this->_customdata['contextid'];
  65          $mform->addElement('hidden', 'contextid', $contextid);
  66          $mform->setType('contextid', PARAM_INT);
  67          $mform->addElement('filepicker', 'restorefile', get_string('templatefile', 'tool_uploadcourse'));
  68          $mform->addHelpButton('restorefile', 'templatefile', 'tool_uploadcourse');
  69  
  70          $mform->addElement('text', 'options[templatecourse]', get_string('coursetemplatename', 'tool_uploadcourse'));
  71          $mform->setType('options[templatecourse]', PARAM_TEXT);
  72          $mform->addHelpButton('options[templatecourse]', 'coursetemplatename', 'tool_uploadcourse');
  73  
  74          $mform->addElement('selectyesno', 'options[reset]', get_string('reset', 'tool_uploadcourse'));
  75          $mform->setDefault('options[reset]', 0);
  76          $mform->hideIf('options[reset]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_CREATE_NEW);
  77          $mform->hideIf('options[reset]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_CREATE_ALL);
  78          $mform->disabledIf('options[reset]', 'options[allowresets]', 'eq', 0);
  79          $mform->addHelpButton('options[reset]', 'reset', 'tool_uploadcourse');
  80  
  81          // Default values.
  82          $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploadcourse'));
  83          $mform->setExpanded('defaultheader', true);
  84  
  85          $displaylist = core_course_category::make_categories_list('moodle/course:create');
  86          $mform->addElement('select', 'defaults[category]', get_string('coursecategory'), $displaylist);
  87          $mform->addHelpButton('defaults[category]', 'coursecategory');
  88  
  89          $choices = array();
  90          $choices['0'] = get_string('hide');
  91          $choices['1'] = get_string('show');
  92          $mform->addElement('select', 'defaults[visible]', get_string('coursevisibility'), $choices);
  93          $mform->addHelpButton('defaults[visible]', 'coursevisibility');
  94          $mform->setDefault('defaults[visible]', $courseconfig->visible);
  95  
  96          $mform->addElement('date_time_selector', 'defaults[startdate]', get_string('startdate'));
  97          $mform->addHelpButton('defaults[startdate]', 'startdate');
  98          $mform->setDefault('defaults[startdate]', time() + 3600 * 24);
  99  
 100          $mform->addElement('date_time_selector', 'defaults[enddate]', get_string('enddate'), array('optional' => true));
 101          $mform->addHelpButton('defaults[enddate]', 'enddate');
 102  
 103          $courseformats = get_sorted_course_formats(true);
 104          $formcourseformats = array();
 105          foreach ($courseformats as $courseformat) {
 106              $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
 107          }
 108          $mform->addElement('select', 'defaults[format]', get_string('format'), $formcourseformats);
 109          $mform->addHelpButton('defaults[format]', 'format');
 110          $mform->setDefault('defaults[format]', $courseconfig->format);
 111  
 112          if (!empty($CFG->allowcoursethemes)) {
 113              $themeobjects = get_list_of_themes();
 114              $themes=array();
 115              $themes[''] = get_string('forceno');
 116              foreach ($themeobjects as $key => $theme) {
 117                  if (empty($theme->hidefromselector)) {
 118                      $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
 119                  }
 120              }
 121              $mform->addElement('select', 'defaults[theme]', get_string('forcetheme'), $themes);
 122          }
 123  
 124          $languages = array();
 125          $languages[''] = get_string('forceno');
 126          $languages += get_string_manager()->get_list_of_translations();
 127          $mform->addElement('select', 'defaults[lang]', get_string('forcelanguage'), $languages);
 128          $mform->setDefault('defaults[lang]', $courseconfig->lang);
 129  
 130          $options = range(0, 10);
 131          $mform->addElement('select', 'defaults[newsitems]', get_string('newsitemsnumber'), $options);
 132          $mform->addHelpButton('defaults[newsitems]', 'newsitemsnumber');
 133          $mform->setDefault('defaults[newsitems]', $courseconfig->newsitems);
 134  
 135          $mform->addElement('selectyesno', 'defaults[showgrades]', get_string('showgrades'));
 136          $mform->addHelpButton('defaults[showgrades]', 'showgrades');
 137          $mform->setDefault('defaults[showgrades]', $courseconfig->showgrades);
 138  
 139          $mform->addElement('selectyesno', 'defaults[showreports]', get_string('showreports'));
 140          $mform->addHelpButton('defaults[showreports]', 'showreports');
 141          $mform->setDefault('defaults[showreports]', $courseconfig->showreports);
 142  
 143          if (!empty($CFG->legacyfilesinnewcourses)) {
 144              $mform->addElement('select', 'defaults[legacyfiles]', get_string('courselegacyfiles'), $choices);
 145              $mform->addHelpButton('defaults[legacyfiles]', 'courselegacyfiles');
 146              if (!isset($courseconfig->legacyfiles)) {
 147                  $courseconfig->legacyfiles = 0;
 148              }
 149              $mform->setDefault('defaults[legacyfiles]', $courseconfig->legacyfiles);
 150          }
 151  
 152          $choices = get_max_upload_sizes($CFG->maxbytes);
 153          $mform->addElement('select', 'defaults[maxbytes]', get_string('maximumupload'), $choices);
 154          $mform->addHelpButton('defaults[maxbytes]', 'maximumupload');
 155          $mform->setDefault('defaults[maxbytes]', $courseconfig->maxbytes);
 156  
 157          $choices = array();
 158          $choices[NOGROUPS] = get_string('groupsnone', 'group');
 159          $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
 160          $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
 161          $mform->addElement('select', 'defaults[groupmode]', get_string('groupmode', 'group'), $choices);
 162          $mform->addHelpButton('defaults[groupmode]', 'groupmode', 'group');
 163          $mform->setDefault('defaults[groupmode]', $courseconfig->groupmode);
 164  
 165          $mform->addElement('selectyesno', 'defaults[groupmodeforce]', get_string('groupmodeforce', 'group'));
 166          $mform->addHelpButton('defaults[groupmodeforce]', 'groupmodeforce', 'group');
 167          $mform->setDefault('defaults[groupmodeforce]', $courseconfig->groupmodeforce);
 168  
 169          // Completion tracking.
 170          if (!empty($CFG->enablecompletion)) {
 171              $mform->addElement('selectyesno', 'defaults[enablecompletion]', get_string('enablecompletion', 'completion'));
 172              $mform->setDefault('defaults[enablecompletion]', $courseconfig->enablecompletion);
 173              $mform->addHelpButton('defaults[enablecompletion]', 'enablecompletion', 'completion');
 174          }
 175  
 176          // Add custom fields to the form.
 177          $handler = \core_course\customfield\course_handler::create();
 178          $handler->instance_form_definition($mform, 0, 'defaultvaluescustomfieldcategory', 'tool_uploadcourse');
 179  
 180          // Hidden fields.
 181          $mform->addElement('hidden', 'importid');
 182          $mform->setType('importid', PARAM_INT);
 183  
 184          $mform->addElement('hidden', 'previewrows');
 185          $mform->setType('previewrows', PARAM_INT);
 186  
 187          $this->add_action_buttons(true, get_string('uploadcourses', 'tool_uploadcourse'));
 188  
 189          // Prepare custom fields data.
 190          $data = (object) $data;
 191          $handler->instance_form_before_set_data($data);
 192  
 193          $this->set_data($data);
 194      }
 195  
 196      /**
 197       * Add actopm buttons.
 198       *
 199       * @param bool $cancel whether to show cancel button, default true
 200       * @param string $submitlabel label for submit button, defaults to get_string('savechanges')
 201       * @return void
 202       */
 203      public function add_action_buttons($cancel = true, $submitlabel = null) {
 204          $mform =& $this->_form;
 205          $buttonarray = array();
 206          $buttonarray[] = &$mform->createElement('submit', 'showpreview', get_string('preview', 'tool_uploadcourse'));
 207          $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
 208          $buttonarray[] = &$mform->createElement('cancel');
 209          $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
 210          $mform->closeHeaderBefore('buttonar');
 211      }
 212  
 213      /**
 214       * Sets the enddate default after set_data is called.
 215       */
 216      public function definition_after_data() {
 217  
 218          $mform = $this->_form;
 219  
 220          // The default end date depends on the course format.
 221          $format = course_get_format((object)array('format' => get_config('moodlecourse', 'format')));
 222  
 223          // Check if course end date form field should be enabled by default.
 224          // If a default date is provided to the form element, it is magically enabled by default in the
 225          // MoodleQuickForm_date_time_selector class, otherwise it's disabled by default.
 226          if (get_config('moodlecourse', 'courseenddateenabled')) {
 227              $enddate = $format->get_default_course_enddate($mform, array('startdate' => 'defaults[startdate]'));
 228              $mform->setDefault('defaults[enddate]', $enddate);
 229          }
 230  
 231          // Tweak the form with values provided by custom fields in use.
 232          \core_course\customfield\course_handler::create()->instance_form_definition_after_data($mform);
 233      }
 234  
 235      /**
 236       * Validation.
 237       *
 238       * @param array $data
 239       * @param array $files
 240       * @return array the errors that were found
 241       */
 242      public function validation($data, $files) {
 243          global $DB;
 244  
 245          $errors = parent::validation($data, $files);
 246  
 247          if ($errorcode = course_validate_dates($data['defaults'])) {
 248              $errors['defaults[enddate]'] = get_string($errorcode, 'error');
 249          }
 250  
 251          // Custom fields validation.
 252          array_merge($errors, \core_course\customfield\course_handler::create()->instance_form_validation($data, $files));
 253  
 254          return $errors;
 255      }
 256  }