PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/resource/mod_form.php

https://bitbucket.org/moodle/moodle
PHP | 231 lines | 171 code | 27 blank | 33 comment | 35 complexity | a74ef9d191969c9e41aa73e49e8deab0 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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->standard_intro_elements();
  51. $element = $mform->getElement('introeditor');
  52. $attributes = $element->getAttributes();
  53. $attributes['rows'] = 5;
  54. $element->setAttributes($attributes);
  55. $filemanager_options = array();
  56. $filemanager_options['accepted_types'] = '*';
  57. $filemanager_options['maxbytes'] = 0;
  58. $filemanager_options['maxfiles'] = -1;
  59. $filemanager_options['mainfile'] = true;
  60. $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);
  61. // add legacy files flag only if used
  62. if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
  63. $options = array(RESOURCELIB_LEGACYFILES_DONE => get_string('legacyfilesdone', 'resource'),
  64. RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'resource'));
  65. $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
  66. }
  67. //-------------------------------------------------------
  68. $mform->addElement('header', 'optionssection', get_string('appearance'));
  69. if ($this->current->instance) {
  70. $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
  71. } else {
  72. $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
  73. }
  74. if (count($options) == 1) {
  75. $mform->addElement('hidden', 'display');
  76. $mform->setType('display', PARAM_INT);
  77. reset($options);
  78. $mform->setDefault('display', key($options));
  79. } else {
  80. $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
  81. $mform->setDefault('display', $config->display);
  82. $mform->addHelpButton('display', 'displayselect', 'resource');
  83. }
  84. $mform->addElement('checkbox', 'showsize', get_string('showsize', 'resource'));
  85. $mform->setDefault('showsize', $config->showsize);
  86. $mform->addHelpButton('showsize', 'showsize', 'resource');
  87. $mform->addElement('checkbox', 'showtype', get_string('showtype', 'resource'));
  88. $mform->setDefault('showtype', $config->showtype);
  89. $mform->addHelpButton('showtype', 'showtype', 'resource');
  90. $mform->addElement('checkbox', 'showdate', get_string('showdate', 'resource'));
  91. $mform->setDefault('showdate', $config->showdate);
  92. $mform->addHelpButton('showdate', 'showdate', 'resource');
  93. if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
  94. $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
  95. if (count($options) > 1) {
  96. $mform->hideIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  97. }
  98. $mform->setType('popupwidth', PARAM_INT);
  99. $mform->setDefault('popupwidth', $config->popupwidth);
  100. $mform->setAdvanced('popupwidth', true);
  101. $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
  102. if (count($options) > 1) {
  103. $mform->hideIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  104. }
  105. $mform->setType('popupheight', PARAM_INT);
  106. $mform->setDefault('popupheight', $config->popupheight);
  107. $mform->setAdvanced('popupheight', true);
  108. }
  109. if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
  110. array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
  111. array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
  112. $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
  113. $mform->hideIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
  114. $mform->hideIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
  115. $mform->hideIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
  116. $mform->hideIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
  117. $mform->setDefault('printintro', $config->printintro);
  118. }
  119. $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
  120. $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
  121. $mform->setDefault('filterfiles', $config->filterfiles);
  122. $mform->setAdvanced('filterfiles', true);
  123. //-------------------------------------------------------
  124. $this->standard_coursemodule_elements();
  125. //-------------------------------------------------------
  126. $this->add_action_buttons();
  127. //-------------------------------------------------------
  128. $mform->addElement('hidden', 'revision');
  129. $mform->setType('revision', PARAM_INT);
  130. $mform->setDefault('revision', 1);
  131. }
  132. function data_preprocessing(&$default_values) {
  133. if ($this->current->instance and !$this->current->tobemigrated) {
  134. $draftitemid = file_get_submitted_draft_itemid('files');
  135. file_prepare_draft_area($draftitemid, $this->context->id, 'mod_resource', 'content', 0, array('subdirs'=>true));
  136. $default_values['files'] = $draftitemid;
  137. }
  138. if (!empty($default_values['displayoptions'])) {
  139. $displayoptions = (array) unserialize_array($default_values['displayoptions']);
  140. if (isset($displayoptions['printintro'])) {
  141. $default_values['printintro'] = $displayoptions['printintro'];
  142. }
  143. if (!empty($displayoptions['popupwidth'])) {
  144. $default_values['popupwidth'] = $displayoptions['popupwidth'];
  145. }
  146. if (!empty($displayoptions['popupheight'])) {
  147. $default_values['popupheight'] = $displayoptions['popupheight'];
  148. }
  149. if (!empty($displayoptions['showsize'])) {
  150. $default_values['showsize'] = $displayoptions['showsize'];
  151. } else {
  152. // Must set explicitly to 0 here otherwise it will use system
  153. // default which may be 1.
  154. $default_values['showsize'] = 0;
  155. }
  156. if (!empty($displayoptions['showtype'])) {
  157. $default_values['showtype'] = $displayoptions['showtype'];
  158. } else {
  159. $default_values['showtype'] = 0;
  160. }
  161. if (!empty($displayoptions['showdate'])) {
  162. $default_values['showdate'] = $displayoptions['showdate'];
  163. } else {
  164. $default_values['showdate'] = 0;
  165. }
  166. }
  167. }
  168. function definition_after_data() {
  169. if ($this->current->instance and $this->current->tobemigrated) {
  170. // resource not migrated yet
  171. return;
  172. }
  173. parent::definition_after_data();
  174. }
  175. function validation($data, $files) {
  176. global $USER;
  177. $errors = parent::validation($data, $files);
  178. $usercontext = context_user::instance($USER->id);
  179. $fs = get_file_storage();
  180. if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
  181. $errors['files'] = get_string('required');
  182. return $errors;
  183. }
  184. if (count($files) == 1) {
  185. // no need to select main file if only one picked
  186. return $errors;
  187. } else if(count($files) > 1) {
  188. $mainfile = false;
  189. foreach($files as $file) {
  190. if ($file->get_sortorder() == 1) {
  191. $mainfile = true;
  192. break;
  193. }
  194. }
  195. // set a default main file
  196. if (!$mainfile) {
  197. $file = reset($files);
  198. file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(),
  199. $file->get_filepath(), $file->get_filename(), 1);
  200. }
  201. }
  202. return $errors;
  203. }
  204. }