PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/resource/mod_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 225 lines | 161 code | 29 blank | 35 comment | 33 complexity | eaf1ee2d4d131a06c9386e9b33acbcf3 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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
  20. * @subpackage resource
  21. * @copyright 2009 Petr Skoda {@link http://skodak.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die;
  25. require_once($CFG->dirroot.'/course/moodleform_mod.php');
  26. require_once($CFG->dirroot.'/mod/resource/locallib.php');
  27. require_once($CFG->libdir.'/filelib.php');
  28. class mod_resource_mod_form extends moodleform_mod {
  29. function definition() {
  30. global $CFG, $DB;
  31. $mform =& $this->_form;
  32. $config = get_config('resource');
  33. if ($this->current->instance and $this->current->tobemigrated) {
  34. // resource not migrated yet
  35. $resource_old = $DB->get_record('resource_old', array('oldid'=>$this->current->instance));
  36. $mform->addElement('static', 'warning', '', get_string('notmigrated', 'resource', $resource_old->type));
  37. $mform->addElement('cancel');
  38. $this->standard_hidden_coursemodule_elements();
  39. return;
  40. }
  41. //-------------------------------------------------------
  42. $mform->addElement('header', 'general', get_string('general', 'form'));
  43. $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
  44. if (!empty($CFG->formatstringstriptags)) {
  45. $mform->setType('name', PARAM_TEXT);
  46. } else {
  47. $mform->setType('name', PARAM_CLEANHTML);
  48. }
  49. $mform->addRule('name', null, 'required', null, 'client');
  50. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  51. $this->add_intro_editor($config->requiremodintro);
  52. //-------------------------------------------------------
  53. $mform->addElement('header', 'contentsection', get_string('contentheader', 'resource'));
  54. $mform->setExpanded('contentsection');
  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. if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
  91. $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
  92. if (count($options) > 1) {
  93. $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  94. }
  95. $mform->setType('popupwidth', PARAM_INT);
  96. $mform->setDefault('popupwidth', $config->popupwidth);
  97. $mform->setAdvanced('popupwidth', true);
  98. $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
  99. if (count($options) > 1) {
  100. $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  101. }
  102. $mform->setType('popupheight', PARAM_INT);
  103. $mform->setDefault('popupheight', $config->popupheight);
  104. $mform->setAdvanced('popupheight', true);
  105. }
  106. if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
  107. array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
  108. array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
  109. $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
  110. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
  111. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
  112. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
  113. $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
  114. $mform->setDefault('printintro', $config->printintro);
  115. }
  116. $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
  117. $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
  118. $mform->setDefault('filterfiles', $config->filterfiles);
  119. $mform->setAdvanced('filterfiles', true);
  120. //-------------------------------------------------------
  121. $this->standard_coursemodule_elements();
  122. //-------------------------------------------------------
  123. $this->add_action_buttons();
  124. //-------------------------------------------------------
  125. $mform->addElement('hidden', 'revision');
  126. $mform->setType('revision', PARAM_INT);
  127. $mform->setDefault('revision', 1);
  128. }
  129. function data_preprocessing(&$default_values) {
  130. if ($this->current->instance and !$this->current->tobemigrated) {
  131. $draftitemid = file_get_submitted_draft_itemid('files');
  132. file_prepare_draft_area($draftitemid, $this->context->id, 'mod_resource', 'content', 0, array('subdirs'=>true));
  133. $default_values['files'] = $draftitemid;
  134. }
  135. if (!empty($default_values['displayoptions'])) {
  136. $displayoptions = unserialize($default_values['displayoptions']);
  137. if (isset($displayoptions['printintro'])) {
  138. $default_values['printintro'] = $displayoptions['printintro'];
  139. }
  140. if (!empty($displayoptions['popupwidth'])) {
  141. $default_values['popupwidth'] = $displayoptions['popupwidth'];
  142. }
  143. if (!empty($displayoptions['popupheight'])) {
  144. $default_values['popupheight'] = $displayoptions['popupheight'];
  145. }
  146. if (!empty($displayoptions['showsize'])) {
  147. $default_values['showsize'] = $displayoptions['showsize'];
  148. } else {
  149. // Must set explicitly to 0 here otherwise it will use system
  150. // default which may be 1.
  151. $default_values['showsize'] = 0;
  152. }
  153. if (!empty($displayoptions['showtype'])) {
  154. $default_values['showtype'] = $displayoptions['showtype'];
  155. } else {
  156. $default_values['showtype'] = 0;
  157. }
  158. }
  159. }
  160. function definition_after_data() {
  161. if ($this->current->instance and $this->current->tobemigrated) {
  162. // resource not migrated yet
  163. return;
  164. }
  165. parent::definition_after_data();
  166. }
  167. function validation($data, $files) {
  168. global $USER;
  169. $errors = parent::validation($data, $files);
  170. $usercontext = context_user::instance($USER->id);
  171. $fs = get_file_storage();
  172. if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
  173. $errors['files'] = get_string('required');
  174. return $errors;
  175. }
  176. if (count($files) == 1) {
  177. // no need to select main file if only one picked
  178. return $errors;
  179. } else if(count($files) > 1) {
  180. $mainfile = false;
  181. foreach($files as $file) {
  182. if ($file->get_sortorder() == 1) {
  183. $mainfile = true;
  184. break;
  185. }
  186. }
  187. // set a default main file
  188. if (!$mainfile) {
  189. $file = reset($files);
  190. file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(),
  191. $file->get_filepath(), $file->get_filename(), 1);
  192. }
  193. }
  194. return $errors;
  195. }
  196. }