PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/resource/mod_form.php

https://github.com/raymanuk/moodle
PHP | 224 lines | 161 code | 29 blank | 34 comment | 33 complexity | 1500b1e91285f06b3211f0f7296bf8ec MD5 | raw file
  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. * Resource configuration form
  18. *
  19. * @package mod_resource
  20. * @copyright 2009 Petr Skoda {@link http://skodak.org}
  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/moodleform_mod.php');
  25. require_once($CFG->dirroot.'/mod/resource/locallib.php');
  26. require_once($CFG->libdir.'/filelib.php');
  27. class mod_resource_mod_form extends moodleform_mod {
  28. function definition() {
  29. global $CFG, $DB;
  30. $mform =& $this->_form;
  31. $config = get_config('resource');
  32. if ($this->current->instance and $this->current->tobemigrated) {
  33. // resource not migrated yet
  34. $resource_old = $DB->get_record('resource_old', array('oldid'=>$this->current->instance));
  35. $mform->addElement('static', 'warning', '', get_string('notmigrated', 'resource', $resource_old->type));
  36. $mform->addElement('cancel');
  37. $this->standard_hidden_coursemodule_elements();
  38. return;
  39. }
  40. //-------------------------------------------------------
  41. $mform->addElement('header', 'general', get_string('general', 'form'));
  42. $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
  43. if (!empty($CFG->formatstringstriptags)) {
  44. $mform->setType('name', PARAM_TEXT);
  45. } else {
  46. $mform->setType('name', PARAM_CLEANHTML);
  47. }
  48. $mform->addRule('name', null, 'required', null, 'client');
  49. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  50. $this->add_intro_editor($config->requiremodintro);
  51. //-------------------------------------------------------
  52. $mform->addElement('header', 'contentsection', get_string('contentheader', 'resource'));
  53. $mform->setExpanded('contentsection');
  54. $filemanager_options = array();
  55. $filemanager_options['accepted_types'] = '*';
  56. $filemanager_options['maxbytes'] = 0;
  57. $filemanager_options['maxfiles'] = -1;
  58. $filemanager_options['mainfile'] = true;
  59. $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);
  60. // add legacy files flag only if used
  61. if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
  62. $options = array(RESOURCELIB_LEGACYFILES_DONE => get_string('legacyfilesdone', 'resource'),
  63. RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'resource'));
  64. $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
  65. }
  66. //-------------------------------------------------------
  67. $mform->addElement('header', 'optionssection', get_string('appearance'));
  68. if ($this->current->instance) {
  69. $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
  70. } else {
  71. $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
  72. }
  73. if (count($options) == 1) {
  74. $mform->addElement('hidden', 'display');
  75. $mform->setType('display', PARAM_INT);
  76. reset($options);
  77. $mform->setDefault('display', key($options));
  78. } else {
  79. $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
  80. $mform->setDefault('display', $config->display);
  81. $mform->addHelpButton('display', 'displayselect', 'resource');
  82. }
  83. $mform->addElement('checkbox', 'showsize', get_string('showsize', 'resource'));
  84. $mform->setDefault('showsize', $config->showsize);
  85. $mform->addHelpButton('showsize', 'showsize', 'resource');
  86. $mform->addElement('checkbox', 'showtype', get_string('showtype', 'resource'));
  87. $mform->setDefault('showtype', $config->showtype);
  88. $mform->addHelpButton('showtype', 'showtype', 'resource');
  89. if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
  90. $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
  91. if (count($options) > 1) {
  92. $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  93. }
  94. $mform->setType('popupwidth', PARAM_INT);
  95. $mform->setDefault('popupwidth', $config->popupwidth);
  96. $mform->setAdvanced('popupwidth', true);
  97. $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
  98. if (count($options) > 1) {
  99. $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  100. }
  101. $mform->setType('popupheight', PARAM_INT);
  102. $mform->setDefault('popupheight', $config->popupheight);
  103. $mform->setAdvanced('popupheight', true);
  104. }
  105. if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
  106. array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
  107. array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
  108. $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
  109. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
  110. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
  111. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
  112. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
  113. $mform->setDefault('printintro', $config->printintro);
  114. }
  115. $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
  116. $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
  117. $mform->setDefault('filterfiles', $config->filterfiles);
  118. $mform->setAdvanced('filterfiles', true);
  119. //-------------------------------------------------------
  120. $this->standard_coursemodule_elements();
  121. //-------------------------------------------------------
  122. $this->add_action_buttons();
  123. //-------------------------------------------------------
  124. $mform->addElement('hidden', 'revision');
  125. $mform->setType('revision', PARAM_INT);
  126. $mform->setDefault('revision', 1);
  127. }
  128. function data_preprocessing(&$default_values) {
  129. if ($this->current->instance and !$this->current->tobemigrated) {
  130. $draftitemid = file_get_submitted_draft_itemid('files');
  131. file_prepare_draft_area($draftitemid, $this->context->id, 'mod_resource', 'content', 0, array('subdirs'=>true));
  132. $default_values['files'] = $draftitemid;
  133. }
  134. if (!empty($default_values['displayoptions'])) {
  135. $displayoptions = unserialize($default_values['displayoptions']);
  136. if (isset($displayoptions['printintro'])) {
  137. $default_values['printintro'] = $displayoptions['printintro'];
  138. }
  139. if (!empty($displayoptions['popupwidth'])) {
  140. $default_values['popupwidth'] = $displayoptions['popupwidth'];
  141. }
  142. if (!empty($displayoptions['popupheight'])) {
  143. $default_values['popupheight'] = $displayoptions['popupheight'];
  144. }
  145. if (!empty($displayoptions['showsize'])) {
  146. $default_values['showsize'] = $displayoptions['showsize'];
  147. } else {
  148. // Must set explicitly to 0 here otherwise it will use system
  149. // default which may be 1.
  150. $default_values['showsize'] = 0;
  151. }
  152. if (!empty($displayoptions['showtype'])) {
  153. $default_values['showtype'] = $displayoptions['showtype'];
  154. } else {
  155. $default_values['showtype'] = 0;
  156. }
  157. }
  158. }
  159. function definition_after_data() {
  160. if ($this->current->instance and $this->current->tobemigrated) {
  161. // resource not migrated yet
  162. return;
  163. }
  164. parent::definition_after_data();
  165. }
  166. function validation($data, $files) {
  167. global $USER;
  168. $errors = parent::validation($data, $files);
  169. $usercontext = context_user::instance($USER->id);
  170. $fs = get_file_storage();
  171. if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
  172. $errors['files'] = get_string('required');
  173. return $errors;
  174. }
  175. if (count($files) == 1) {
  176. // no need to select main file if only one picked
  177. return $errors;
  178. } else if(count($files) > 1) {
  179. $mainfile = false;
  180. foreach($files as $file) {
  181. if ($file->get_sortorder() == 1) {
  182. $mainfile = true;
  183. break;
  184. }
  185. }
  186. // set a default main file
  187. if (!$mainfile) {
  188. $file = reset($files);
  189. file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(),
  190. $file->get_filepath(), $file->get_filename(), 1);
  191. }
  192. }
  193. return $errors;
  194. }
  195. }