2007-01-11 19:47:08 +00:00
< ? php
2009-08-13 20:45:50 +00:00
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Resource configuration form
*
* @ package mod - resource
* @ copyright 2009 Petr Skoda ( http :// skodak . org )
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
*/
require_once ( $CFG -> dirroot . '/course/moodleform_mod.php' );
require_once ( $CFG -> dirroot . '/mod/resource/locallib.php' );
require_once ( $CFG -> libdir . '/filelib.php' );
2007-01-11 19:47:08 +00:00
2009-08-13 20:45:50 +00:00
class mod_resource_mod_form extends moodleform_mod {
2007-01-11 19:47:08 +00:00
function definition () {
2008-06-01 21:36:11 +00:00
global $CFG , $DB ;
2007-01-11 19:47:08 +00:00
$mform =& $this -> _form ;
2009-08-13 20:45:50 +00:00
$config = get_config ( 'resource' );
2007-01-11 19:47:08 +00:00
2009-08-13 20:45:50 +00:00
if ( $this -> current -> instance and $this -> current -> tobemigrated ) {
// resource not migrated yet
$resoruce_old = $DB -> get_record ( 'resource_old' , array ( 'oldid' => $this -> current -> instance ));
$mform -> addElement ( 'static' , 'warning' , '' , get_string ( 'notmigrated' , 'resource' , $resoruce_old -> type ));
$mform -> addElement ( 'cancel' );
$this -> standard_hidden_coursemodule_elements ();
return ;
}
2007-01-11 19:47:08 +00:00
2009-08-13 20:45:50 +00:00
//-------------------------------------------------------
2007-01-11 19:47:08 +00:00
$mform -> addElement ( 'header' , 'general' , get_string ( 'general' , 'form' ));
2007-03-13 07:38:54 +00:00
$mform -> addElement ( 'text' , 'name' , get_string ( 'name' ), array ( 'size' => '48' ));
2008-07-26 15:15:25 +00:00
if ( ! empty ( $CFG -> formatstringstriptags )) {
$mform -> setType ( 'name' , PARAM_TEXT );
} else {
$mform -> setType ( 'name' , PARAM_CLEAN );
}
2007-01-11 19:47:08 +00:00
$mform -> addRule ( 'name' , null , 'required' , null , 'client' );
2009-08-13 20:45:50 +00:00
$this -> add_intro_editor ( $config -> requiremodintro );
2007-01-11 19:47:08 +00:00
2009-08-13 20:45:50 +00:00
//-------------------------------------------------------
$mform -> addElement ( 'header' , 'contentsection' , get_string ( 'contentheader' , 'resource' ));
$mform -> addElement ( 'static' , 'note' , '' , '<i>(TODO: This filemanager + textfield is temporary until we create new file manager with main file support.)</i>' );
$mform -> addElement ( 'filemanager' , 'files' , get_string ( 'file' ));
$mform -> addElement ( 'static' , 'notex' , '' , '<i>(TODO: Ignore main file when uploading only one file, if multiple files present use "/myfile.ext" to specify the main file.)</i>' );
$mform -> addElement ( 'text' , 'mainfile' , 'Main file' );
2007-01-11 19:47:08 +00:00
2009-08-13 20:45:50 +00:00
//-------------------------------------------------------
$mform -> addElement ( 'header' , 'optionssection' , get_string ( 'optionsheader' , 'resource' ));
if ( $this -> current -> instance ) {
$options = resourcelib_get_displayoptions ( explode ( ',' , $config -> displayoptions ), $this -> current -> display );
} else {
$options = resourcelib_get_displayoptions ( explode ( ',' , $config -> displayoptions ));
}
if ( count ( $options ) == 1 ) {
$mform -> addElement ( 'hidden' , 'display' );
reset ( $options );
$mform -> setDefault ( 'display' , key ( $options ));
} else {
$mform -> addElement ( 'select' , 'display' , get_string ( 'displayselect' , 'resource' ), $options );
$mform -> setDefault ( 'display' , $config -> display );
$mform -> setAdvanced ( 'display' , $config -> display_adv );
}
if ( array_key_exists ( RESOURCELIB_DISPLAY_POPUP , $options )) {
$mform -> addElement ( 'text' , 'popupwidth' , get_string ( 'popupwidth' , 'resource' ), array ( 'size' => 3 ));
if ( count ( $options ) > 1 ) {
$mform -> disabledIf ( 'popupwidth' , 'display' , 'noteq' , RESOURCELIB_DISPLAY_POPUP );
}
$mform -> setType ( 'popupwidth' , PARAM_INT );
$mform -> setDefault ( 'popupwidth' , $config -> popupwidth );
$mform -> setAdvanced ( 'popupwidth' , $config -> popupwidth_adv );
$mform -> addElement ( 'text' , 'popupheight' , get_string ( 'popupheight' , 'resource' ), array ( 'size' => 3 ));
if ( count ( $options ) > 1 ) {
$mform -> disabledIf ( 'popupheight' , 'display' , 'noteq' , RESOURCELIB_DISPLAY_POPUP );
}
$mform -> setType ( 'popupheight' , PARAM_INT );
$mform -> setDefault ( 'popupheight' , $config -> popupheight );
$mform -> setAdvanced ( 'popupheight' , $config -> popupheight_adv );
}
if ( array_key_exists ( RESOURCELIB_DISPLAY_AUTO , $options ) or
array_key_exists ( RESOURCELIB_DISPLAY_EMBED , $options ) or
array_key_exists ( RESOURCELIB_DISPLAY_FRAME , $options )) {
$mform -> addElement ( 'checkbox' , 'printheading' , get_string ( 'printheading' , 'resource' ));
$mform -> disabledIf ( 'printheading' , 'display' , 'eq' , RESOURCELIB_DISPLAY_POPUP );
$mform -> disabledIf ( 'printheading' , 'display' , 'eq' , RESOURCELIB_DISPLAY_DOWNLOAD );
$mform -> disabledIf ( 'printheading' , 'display' , 'eq' , RESOURCELIB_DISPLAY_OPEN );
$mform -> disabledIf ( 'printheading' , 'display' , 'eq' , RESOURCELIB_DISPLAY_NEW );
$mform -> setDefault ( 'printheading' , $config -> printheading );
$mform -> setAdvanced ( 'printheading' , $config -> printheading_adv );
$mform -> addElement ( 'checkbox' , 'printintro' , get_string ( 'printintro' , 'resource' ));
$mform -> disabledIf ( 'printintro' , 'display' , 'eq' , RESOURCELIB_DISPLAY_POPUP );
$mform -> disabledIf ( 'printintro' , 'display' , 'eq' , RESOURCELIB_DISPLAY_DOWNLOAD );
$mform -> disabledIf ( 'printintro' , 'display' , 'eq' , RESOURCELIB_DISPLAY_OPEN );
$mform -> disabledIf ( 'printintro' , 'display' , 'eq' , RESOURCELIB_DISPLAY_NEW );
$mform -> setDefault ( 'printintro' , $config -> printintro );
$mform -> setAdvanced ( 'printintro' , $config -> printintro_adv );
}
2007-01-11 19:47:08 +00:00
2009-08-13 20:45:50 +00:00
$options = array ( '0' => get_string ( 'none' ), '1' => get_string ( 'allfiles' ), '2' => get_string ( 'htmlfilesonly' ));
$mform -> addElement ( 'select' , 'filterfiles' , get_string ( 'filterfiles' , 'resource' ), $options );
$mform -> setDefault ( 'filterfiles' , $config -> filterfiles );
$mform -> setAdvanced ( 'filterfiles' , $config -> filterfiles_adv );
// add legacy files flag only if used
if ( isset ( $this -> current -> legacyfiles ) and $this -> current -> legacyfiles != RESOURCELIB_LEGACYFILES_NO ) {
$options = array ( RESOURCE_LEGACYYFILES_DONE => get_string ( 'legacyfilesdone' , 'resource' ),
RESOURCELIB_LEGACYFILES_ACTIVE => get_string ( 'legacyfilesactive' , 'resource' ));
$mform -> addElement ( 'select' , 'legacyfiles' , get_string ( 'legacyfiles' , 'resource' ), $options );
$mform -> setAdvanced ( 'legacyfiles' , 1 );
}
//-------------------------------------------------------
2009-04-21 08:57:20 +00:00
$this -> standard_coursemodule_elements ();
2007-01-11 19:47:08 +00:00
2009-08-13 20:45:50 +00:00
//-------------------------------------------------------
2007-01-11 19:47:08 +00:00
$this -> add_action_buttons ();
2009-08-13 20:45:50 +00:00
//-------------------------------------------------------
$mform -> addElement ( 'hidden' , 'revision' );
$mform -> setType ( 'revision' , PARAM_INT );
$mform -> setDefault ( 'revision' , 1 );
}
function data_preprocessing ( & $default_values ) {
if ( $this -> current -> instance and ! $this -> current -> tobemigrated ) {
$draftitemid = file_get_submitted_draft_itemid ( 'files' );
file_prepare_draft_area ( $draftitemid , $this -> context -> id , 'resource_content' , 0 , array ( 'subdirs' => true ));
$default_values [ 'files' ] = $draftitemid ;
}
if ( ! empty ( $default_values [ 'displayoptions' ])) {
$displayoptions = unserialize ( $default_values [ 'displayoptions' ]);
if ( isset ( $displayoptions [ 'printintro' ])) {
$default_values [ 'printintro' ] = $displayoptions [ 'printintro' ];
}
if ( isset ( $displayoptions [ 'printheading' ])) {
$default_values [ 'printheading' ] = $displayoptions [ 'printheading' ];
}
if ( ! empty ( $displayoptions [ 'popupwidth' ])) {
$default_values [ 'popupwidth' ] = $displayoptions [ 'popupwidth' ];
}
if ( ! empty ( $displayoptions [ 'popupheight' ])) {
$default_values [ 'popupheight' ] = $displayoptions [ 'popupheight' ];
}
}
2007-01-11 19:47:08 +00:00
}
2009-08-13 20:45:50 +00:00
function definition_after_data () {
if ( $this -> current -> instance and $this -> current -> tobemigrated ) {
// resource not migrated yet
return ;
}
parent :: definition_after_data ();
2007-01-11 19:47:08 +00:00
}
2009-08-13 20:45:50 +00:00
function validation ( $data , $files ) {
global $USER ;
$errors = parent :: validation ( $data , $files );
$usercontext = get_context_instance ( CONTEXT_USER , $USER -> id );
$fs = get_file_storage ();
if ( ! $files = $fs -> get_area_files ( $usercontext -> id , 'user_draft' , $data [ 'files' ], 'id' , false )) {
$errors [ 'files' ] = get_string ( 'required' );
return $errors ;
}
if ( count ( $files ) == 1 ) {
// no need to select main file if only one picked
return $errors ;
}
$filepaths = array ();
foreach ( $files as $file ) {
$filepaths [] = $file -> get_filepath () . $file -> get_filename ();
}
if ( ! in_array ( $data [ 'mainfile' ], $filepaths )) {
$errors [ 'mainfile' ] = 'Please type correct main file path' ; //TODO: will not be needed, do NOT localize!
}
return $errors ;
}
2007-01-11 19:47:08 +00:00
}
2009-08-13 20:45:50 +00:00