Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/resource/mod_form.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die;
00028 
00029 require_once($CFG->dirroot.'/course/moodleform_mod.php');
00030 require_once($CFG->dirroot.'/mod/resource/locallib.php');
00031 require_once($CFG->libdir.'/filelib.php');
00032 
00033 class mod_resource_mod_form extends moodleform_mod {
00034     function definition() {
00035         global $CFG, $DB;
00036         $mform =& $this->_form;
00037 
00038         $config = get_config('resource');
00039 
00040         if ($this->current->instance and $this->current->tobemigrated) {
00041             // resource not migrated yet
00042             $resource_old = $DB->get_record('resource_old', array('oldid'=>$this->current->instance));
00043             $mform->addElement('static', 'warning', '', get_string('notmigrated', 'resource', $resource_old->type));
00044             $mform->addElement('cancel');
00045             $this->standard_hidden_coursemodule_elements();
00046             return;
00047         }
00048 
00049         //-------------------------------------------------------
00050         $mform->addElement('header', 'general', get_string('general', 'form'));
00051         $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
00052         if (!empty($CFG->formatstringstriptags)) {
00053             $mform->setType('name', PARAM_TEXT);
00054         } else {
00055             $mform->setType('name', PARAM_CLEANHTML);
00056         }
00057         $mform->addRule('name', null, 'required', null, 'client');
00058         $this->add_intro_editor($config->requiremodintro);
00059 
00060         //-------------------------------------------------------
00061         $mform->addElement('header', 'contentsection', get_string('contentheader', 'resource'));
00062 
00063         $filemanager_options = array();
00064         // 3 == FILE_EXTERNAL & FILE_INTERNAL
00065         // These two constant names are defined in repository/lib.php
00066         $filemanager_options['return_types'] = 3;
00067         $filemanager_options['accepted_types'] = '*';
00068         $filemanager_options['maxbytes'] = 0;
00069         $filemanager_options['maxfiles'] = -1;
00070         $filemanager_options['mainfile'] = true;
00071 
00072         $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);
00073 
00074         //-------------------------------------------------------
00075         $mform->addElement('header', 'optionssection', get_string('optionsheader', 'resource'));
00076 
00077         if ($this->current->instance) {
00078             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
00079         } else {
00080             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
00081         }
00082 
00083         if (count($options) == 1) {
00084             $mform->addElement('hidden', 'display');
00085             $mform->setType('display', PARAM_INT);
00086             reset($options);
00087             $mform->setDefault('display', key($options));
00088         } else {
00089             $mform->addElement('select', 'display', get_string('displayselect', 'resource'), $options);
00090             $mform->setDefault('display', $config->display);
00091             $mform->setAdvanced('display', $config->display_adv);
00092             $mform->addHelpButton('display', 'displayselect', 'resource');
00093         }
00094 
00095         if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
00096             $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
00097             if (count($options) > 1) {
00098                 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
00099             }
00100             $mform->setType('popupwidth', PARAM_INT);
00101             $mform->setDefault('popupwidth', $config->popupwidth);
00102             $mform->setAdvanced('popupwidth', $config->popupwidth_adv);
00103 
00104             $mform->addElement('text', 'popupheight', get_string('popupheight', 'resource'), array('size'=>3));
00105             if (count($options) > 1) {
00106                 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
00107             }
00108             $mform->setType('popupheight', PARAM_INT);
00109             $mform->setDefault('popupheight', $config->popupheight);
00110             $mform->setAdvanced('popupheight', $config->popupheight_adv);
00111         }
00112 
00113         if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
00114           array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
00115           array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
00116             $mform->addElement('checkbox', 'printheading', get_string('printheading', 'resource'));
00117             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
00118             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
00119             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
00120             $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
00121             $mform->setDefault('printheading', $config->printheading);
00122             $mform->setAdvanced('printheading', $config->printheading_adv);
00123 
00124             $mform->addElement('checkbox', 'printintro', get_string('printintro', 'resource'));
00125             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
00126             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_DOWNLOAD);
00127             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
00128             $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
00129             $mform->setDefault('printintro', $config->printintro);
00130             $mform->setAdvanced('printintro', $config->printintro_adv);
00131         }
00132 
00133         $options = array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly'));
00134         $mform->addElement('select', 'filterfiles', get_string('filterfiles', 'resource'), $options);
00135         $mform->setDefault('filterfiles', $config->filterfiles);
00136         $mform->setAdvanced('filterfiles', $config->filterfiles_adv);
00137 
00138         // add legacy files flag only if used
00139         if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
00140             $options = array(RESOURCELIB_LEGACYFILES_DONE   => get_string('legacyfilesdone', 'resource'),
00141                              RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'resource'));
00142             $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'resource'), $options);
00143             $mform->setAdvanced('legacyfiles', 1);
00144         }
00145 
00146         //-------------------------------------------------------
00147         $this->standard_coursemodule_elements();
00148 
00149         //-------------------------------------------------------
00150         $this->add_action_buttons();
00151 
00152         //-------------------------------------------------------
00153         $mform->addElement('hidden', 'revision');
00154         $mform->setType('revision', PARAM_INT);
00155         $mform->setDefault('revision', 1);
00156     }
00157 
00158     function data_preprocessing(&$default_values) {
00159         if ($this->current->instance and !$this->current->tobemigrated) {
00160             $draftitemid = file_get_submitted_draft_itemid('files');
00161             file_prepare_draft_area($draftitemid, $this->context->id, 'mod_resource', 'content', 0, array('subdirs'=>true));
00162             $default_values['files'] = $draftitemid;
00163         }
00164         if (!empty($default_values['displayoptions'])) {
00165             $displayoptions = unserialize($default_values['displayoptions']);
00166             if (isset($displayoptions['printintro'])) {
00167                 $default_values['printintro'] = $displayoptions['printintro'];
00168             }
00169             if (isset($displayoptions['printheading'])) {
00170                 $default_values['printheading'] = $displayoptions['printheading'];
00171             }
00172             if (!empty($displayoptions['popupwidth'])) {
00173                 $default_values['popupwidth'] = $displayoptions['popupwidth'];
00174             }
00175             if (!empty($displayoptions['popupheight'])) {
00176                 $default_values['popupheight'] = $displayoptions['popupheight'];
00177             }
00178         }
00179     }
00180 
00181     function definition_after_data() {
00182         if ($this->current->instance and $this->current->tobemigrated) {
00183             // resource not migrated yet
00184             return;
00185         }
00186 
00187         parent::definition_after_data();
00188     }
00189 
00190     function validation($data, $files) {
00191         global $USER;
00192 
00193         $errors = parent::validation($data, $files);
00194 
00195         $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
00196         $fs = get_file_storage();
00197         if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
00198             $errors['files'] = get_string('required');
00199             return $errors;
00200         }
00201         if (count($files) == 1) {
00202             // no need to select main file if only one picked
00203             return $errors;
00204         } else if(count($files) > 1) {
00205             $mainfile = false;
00206             foreach($files as $file) {
00207                 if ($file->get_sortorder() == 1) {
00208                     $mainfile = true;
00209                     break;
00210                 }
00211             }
00212             // set a default main file
00213             if (!$mainfile) {
00214                 $file = reset($files);
00215                 file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(),
00216                                    $file->get_filepath(), $file->get_filename(), 1);
00217             }
00218         }
00219         return $errors;
00220     }
00221 }
 All Data Structures Namespaces Files Functions Variables Enumerations