PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/resource/mod_form.php

https://gitlab.com/JrLucena/moodle
PHP | 232 lines | 169 code | 29 blank | 34 comment | 35 complexity | 50c18bcdb9bbd7a24baafa18179921ee 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->standard_intro_elements();
  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. $mform->addElement('checkbox', 'showdate', get_string('showdate', 'resource'));
  90. $mform->setDefault('showdate', $config->showdate);
  91. $mform->addHelpButton('showdate', 'showdate', 'resource');
  92. if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
  93. $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
  94. if (count($options) > 1) {
  95. $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  96. }
  97. $mform->setType('popupwidth', PARAM_INT);
  98. $mform->setDefault('popupwidth', $config->popupwidth);
  99. $mform->setAdvanced('popupwidth', true);
  100. $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
  101. if (count($options) > 1) {
  102. $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  103. }
  104. $mform->setType('popupheight', PARAM_INT);
  105. $mform->setDefault('popupheight', $config->popupheight);
  106. $mform->setAdvanced('popupheight', true);
  107. }
  108. if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
  109. array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
  110. array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
  111. $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
  112. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
  113. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
  114. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
  115. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
  116. $mform->setDefault('printintro', $config->printintro);
  117. }
  118. $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
  119. $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
  120. $mform->setDefault('filterfiles', $config->filterfiles);
  121. $mform->setAdvanced('filterfiles', true);
  122. //-------------------------------------------------------
  123. $this->standard_coursemodule_elements();
  124. //-------------------------------------------------------
  125. $this->add_action_buttons();
  126. //-------------------------------------------------------
  127. $mform->addElement('hidden', 'revision');
  128. $mform->setType('revision', PARAM_INT);
  129. $mform->setDefault('revision', 1);
  130. }
  131. function data_preprocessing(&$default_values) {
  132. if ($this->current->instance and !$this->current->tobemigrated) {
  133. $draftitemid = file_get_submitted_draft_itemid('files');
  134. file_prepare_draft_area($draftitemid, $this->context->id, 'mod_resource', 'content', 0, array('subdirs'=>true));
  135. $default_values['files'] = $draftitemid;
  136. }
  137. if (!empty($default_values['displayoptions'])) {
  138. $displayoptions = unserialize($default_values['displayoptions']);
  139. if (isset($displayoptions['printintro'])) {
  140. $default_values['printintro'] = $displayoptions['printintro'];
  141. }
  142. if (!empty($displayoptions['popupwidth'])) {
  143. $default_values['popupwidth'] = $displayoptions['popupwidth'];
  144. }
  145. if (!empty($displayoptions['popupheight'])) {
  146. $default_values['popupheight'] = $displayoptions['popupheight'];
  147. }
  148. if (!empty($displayoptions['showsize'])) {
  149. $default_values['showsize'] = $displayoptions['showsize'];
  150. } else {
  151. // Must set explicitly to 0 here otherwise it will use system
  152. // default which may be 1.
  153. $default_values['showsize'] = 0;
  154. }
  155. if (!empty($displayoptions['showtype'])) {
  156. $default_values['showtype'] = $displayoptions['showtype'];
  157. } else {
  158. $default_values['showtype'] = 0;
  159. }
  160. if (!empty($displayoptions['showdate'])) {
  161. $default_values['showdate'] = $displayoptions['showdate'];
  162. } else {
  163. $default_values['showdate'] = 0;
  164. }
  165. }
  166. }
  167. function definition_after_data() {
  168. if ($this->current->instance and $this->current->tobemigrated) {
  169. // resource not migrated yet
  170. return;
  171. }
  172. parent::definition_after_data();
  173. }
  174. function validation($data, $files) {
  175. global $USER;
  176. $errors = parent::validation($data, $files);
  177. $usercontext = context_user::instance($USER->id);
  178. $fs = get_file_storage();
  179. if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
  180. $errors['files'] = get_string('required');
  181. return $errors;
  182. }
  183. if (count($files) == 1) {
  184. // no need to select main file if only one picked
  185. return $errors;
  186. } else if(count($files) > 1) {
  187. $mainfile = false;
  188. foreach($files as $file) {
  189. if ($file->get_sortorder() == 1) {
  190. $mainfile = true;
  191. break;
  192. }
  193. }
  194. // set a default main file
  195. if (!$mainfile) {
  196. $file = reset($files);
  197. file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(),
  198. $file->get_filepath(), $file->get_filename(), 1);
  199. }
  200. }
  201. return $errors;
  202. }
  203. }