Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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