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   * 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('autocomplete', 'defaults[category]', get_string('coursecategory'), $displaylist);
  87          $mform->addRule('defaults[category]', null, 'required', null, 'client');
  88          $mform->addHelpButton('defaults[category]', 'coursecategory');
  89  
  90          $choices = array();
  91          $choices['0'] = get_string('hide');
  92          $choices['1'] = get_string('show');
  93          $mform->addElement('select', 'defaults[visible]', get_string('coursevisibility'), $choices);
  94          $mform->addHelpButton('defaults[visible]', 'coursevisibility');
  95          $mform->setDefault('defaults[visible]', $courseconfig->visible);
  96  
  97          if ($CFG->downloadcoursecontentallowed &&
  98                  has_capability('moodle/course:configuredownloadcontent', context::instance_by_id($contextid))) {
  99  
 100              $downloadchoices = [
 101                  DOWNLOAD_COURSE_CONTENT_DISABLED => get_string('no'),
 102                  DOWNLOAD_COURSE_CONTENT_ENABLED => get_string('yes'),
 103              ];
 104  
 105              $sitedefaultstring = $downloadchoices[$courseconfig->downloadcontentsitedefault];
 106              $downloadchoices[DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT] = get_string('sitedefaultspecified', '', $sitedefaultstring);
 107              $downloadselectdefault = $courseconfig->downloadcontent ?? DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT;
 108  
 109              $mform->addElement('select', 'defaults[downloadcontent]', get_string('enabledownloadcoursecontent', 'course'),
 110                  $downloadchoices);
 111              $mform->addHelpButton('defaults[downloadcontent]', 'downloadcoursecontent', 'course');
 112              $mform->setDefault('defaults[downloadcontent]', $downloadselectdefault);
 113          }
 114  
 115          $mform->addElement('date_time_selector', 'defaults[startdate]', get_string('startdate'));
 116          $mform->addHelpButton('defaults[startdate]', 'startdate');
 117          $mform->setDefault('defaults[startdate]', time() + 3600 * 24);
 118  
 119          $mform->addElement('date_time_selector', 'defaults[enddate]', get_string('enddate'), array('optional' => true));
 120          $mform->addHelpButton('defaults[enddate]', 'enddate');
 121  
 122          $courseformats = get_sorted_course_formats(true);
 123          $formcourseformats = array();
 124          foreach ($courseformats as $courseformat) {
 125              $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
 126          }
 127          $mform->addElement('select', 'defaults[format]', get_string('format'), $formcourseformats);
 128          $mform->addHelpButton('defaults[format]', 'format');
 129          $mform->setDefault('defaults[format]', $courseconfig->format);
 130  
 131          if (!empty($CFG->allowcoursethemes)) {
 132              $themeobjects = get_list_of_themes();
 133              $themes=array();
 134              $themes[''] = get_string('forceno');
 135              foreach ($themeobjects as $key => $theme) {
 136                  if (empty($theme->hidefromselector)) {
 137                      $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
 138                  }
 139              }
 140              $mform->addElement('select', 'defaults[theme]', get_string('forcetheme'), $themes);
 141          }
 142  
 143          $languages = array();
 144          $languages[''] = get_string('forceno');
 145          $languages += get_string_manager()->get_list_of_translations();
 146          $mform->addElement('select', 'defaults[lang]', get_string('forcelanguage'), $languages);
 147          $mform->setDefault('defaults[lang]', $courseconfig->lang);
 148  
 149          $options = range(0, 10);
 150          $mform->addElement('select', 'defaults[newsitems]', get_string('newsitemsnumber'), $options);
 151          $mform->addHelpButton('defaults[newsitems]', 'newsitemsnumber');
 152          $mform->setDefault('defaults[newsitems]', $courseconfig->newsitems);
 153  
 154          $mform->addElement('selectyesno', 'defaults[showgrades]', get_string('showgrades'));
 155          $mform->addHelpButton('defaults[showgrades]', 'showgrades');
 156          $mform->setDefault('defaults[showgrades]', $courseconfig->showgrades);
 157  
 158          $mform->addElement('selectyesno', 'defaults[showreports]', get_string('showreports'));
 159          $mform->addHelpButton('defaults[showreports]', 'showreports');
 160          $mform->setDefault('defaults[showreports]', $courseconfig->showreports);
 161  
 162          if (!empty($CFG->legacyfilesinnewcourses)) {
 163              $mform->addElement('select', 'defaults[legacyfiles]', get_string('courselegacyfiles'), $choices);
 164              $mform->addHelpButton('defaults[legacyfiles]', 'courselegacyfiles');
 165              if (!isset($courseconfig->legacyfiles)) {
 166                  $courseconfig->legacyfiles = 0;
 167              }
 168              $mform->setDefault('defaults[legacyfiles]', $courseconfig->legacyfiles);
 169          }
 170  
 171          $choices = get_max_upload_sizes($CFG->maxbytes);
 172          $mform->addElement('select', 'defaults[maxbytes]', get_string('maximumupload'), $choices);
 173          $mform->addHelpButton('defaults[maxbytes]', 'maximumupload');
 174          $mform->setDefault('defaults[maxbytes]', $courseconfig->maxbytes);
 175  
 176          $choices = array();
 177          $choices[NOGROUPS] = get_string('groupsnone', 'group');
 178          $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
 179          $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
 180          $mform->addElement('select', 'defaults[groupmode]', get_string('groupmode', 'group'), $choices);
 181          $mform->addHelpButton('defaults[groupmode]', 'groupmode', 'group');
 182          $mform->setDefault('defaults[groupmode]', $courseconfig->groupmode);
 183  
 184          $mform->addElement('selectyesno', 'defaults[groupmodeforce]', get_string('groupmodeforce', 'group'));
 185          $mform->addHelpButton('defaults[groupmodeforce]', 'groupmodeforce', 'group');
 186          $mform->setDefault('defaults[groupmodeforce]', $courseconfig->groupmodeforce);
 187  
 188          // Completion tracking.
 189          if (!empty($CFG->enablecompletion)) {
 190              $mform->addElement('selectyesno', 'defaults[enablecompletion]', get_string('enablecompletion', 'completion'));
 191              $mform->setDefault('defaults[enablecompletion]', $courseconfig->enablecompletion);
 192              $mform->addHelpButton('defaults[enablecompletion]', 'enablecompletion', 'completion');
 193          }
 194  
 195          $mform->addElement('selectyesno', 'defaults[showactivitydates]', get_string('showactivitydates'));
 196          $mform->addHelpButton('defaults[showactivitydates]', 'showactivitydates');
 197          $mform->setDefault('defaults[showactivitydates]', $courseconfig->showactivitydates);
 198  
 199          // Add custom fields to the form.
 200          $handler = \core_course\customfield\course_handler::create();
 201          $handler->instance_form_definition($mform, 0, 'defaultvaluescustomfieldcategory', 'tool_uploadcourse');
 202  
 203          // Hidden fields.
 204          $mform->addElement('hidden', 'importid');
 205          $mform->setType('importid', PARAM_INT);
 206  
 207          $mform->addElement('hidden', 'previewrows');
 208          $mform->setType('previewrows', PARAM_INT);
 209  
 210          $this->add_action_buttons(true, get_string('uploadcourses', 'tool_uploadcourse'));
 211  
 212          // Prepare custom fields data.
 213          $data = (object) $data;
 214          $handler->instance_form_before_set_data($data);
 215  
 216          $this->set_data($data);
 217      }
 218  
 219      /**
 220       * Add actopm buttons.
 221       *
 222       * @param bool $cancel whether to show cancel button, default true
 223       * @param string $submitlabel label for submit button, defaults to get_string('savechanges')
 224       * @return void
 225       */
 226      public function add_action_buttons($cancel = true, $submitlabel = null) {
 227          $mform =& $this->_form;
 228          $buttonarray = array();
 229          $buttonarray[] = &$mform->createElement('submit', 'showpreview', get_string('preview', 'tool_uploadcourse'));
 230          $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
 231          $buttonarray[] = &$mform->createElement('cancel');
 232          $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
 233          $mform->closeHeaderBefore('buttonar');
 234      }
 235  
 236      /**
 237       * Sets the enddate default after set_data is called.
 238       */
 239      public function definition_after_data() {
 240  
 241          $mform = $this->_form;
 242  
 243          // The default end date depends on the course format.
 244          $format = course_get_format((object)array('format' => get_config('moodlecourse', 'format')));
 245  
 246          // Check if course end date form field should be enabled by default.
 247          // If a default date is provided to the form element, it is magically enabled by default in the
 248          // MoodleQuickForm_date_time_selector class, otherwise it's disabled by default.
 249          if (get_config('moodlecourse', 'courseenddateenabled')) {
 250              $enddate = $format->get_default_course_enddate($mform, array('startdate' => 'defaults[startdate]'));
 251              $mform->setDefault('defaults[enddate]', $enddate);
 252          }
 253  
 254          // Tweak the form with values provided by custom fields in use.
 255          \core_course\customfield\course_handler::create()->instance_form_definition_after_data($mform);
 256      }
 257  
 258      /**
 259       * Validation.
 260       *
 261       * @param array $data
 262       * @param array $files
 263       * @return array the errors that were found
 264       */
 265      public function validation($data, $files) {
 266          global $DB;
 267  
 268          $errors = parent::validation($data, $files);
 269  
 270          if ($errorcode = course_validate_dates($data['defaults'])) {
 271              $errors['defaults[enddate]'] = get_string($errorcode, 'error');
 272          }
 273  
 274          // Custom fields validation.
 275          array_merge($errors, \core_course\customfield\course_handler::create()->instance_form_validation($data, $files));
 276  
 277          return $errors;
 278      }
 279  }