PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/uploadcourse/classes/step2_form.php

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