Source for file mod_form.php

Documentation is available at mod_form.php

  1. <?php
  2.     /**
  3.     * Sloodle module add/edit form.
  4.     * This script defines a form used to add/edit instances of the Sloodle module.
  5.     *
  6.     * @package sloodle
  7.     * @copyright Copyright (c) 2008 Sloodle (various contributors)
  8.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9.     *
  10.     * @contributor Peter R. Bloomfield
  11.     *
  12.     */
  13.  
  14.  
  15. /** Core Sloodle configuration/functionality */
  16. require_once($CFG->dirroot.'/mod/sloodle/sl_config.php');
  17. /** The base class for the Moodle module form */
  18. require_once ($CFG->dirroot.'/course/moodleform_mod.php');
  19.  
  20.  
  21. /** General Sloodle functionality */
  22. require_once(SLOODLE_LIBROOT.'/general.php');
  23.  
  24.  
  25. /**
  26. * Used to define the Sloodle module instance add/edit form.
  27. @package sloodle
  28. */
  29. class mod_sloodle_mod_form extends moodleform_mod {
  30.  
  31.     /**
  32.     * Defines the form
  33.     * @return void 
  34.     * @uses $CFG
  35.     * @uses $COURSE
  36.     * @uses $SLOODLE_TYPES
  37.     */                         
  38.     function definition({
  39.  
  40.         global $CFG$COURSE$SLOODLE_TYPES;
  41.         $mform    =$this->_form;
  42.  
  43. //-------------------------------------------------------------------------------
  44.  
  45.         // We need to know which type is being added/edited
  46.         $sloodletype SLOODLE_TYPE_CTRL// default
  47.         
  48.         // Are we adding a new instance?
  49.         if (empty($this->_instance)) {
  50.             // Yes - check for a 'type' parameter
  51.             $sloodletype required_param('type'PARAM_TEXT);
  52.         else {
  53.             // Fetch the instance data
  54.             $rec get_record('sloodle''id'$this->_instance);
  55.             if (!$recerror(get_string('modulenotfound'));
  56.             // Get the module type
  57.             if (empty($rec->type)) {
  58.                 error(get_string('moduletypeunknown''sloodle'));
  59.             }
  60.             $sloodletype $rec->type;
  61.         }
  62.         
  63.         // Store the fullname of the type
  64.         $sloodletypefull get_string("moduletype:$sloodletype"'sloodle');
  65.         
  66. //-------------------------------------------------------------------------------
  67.         
  68.         // General info section
  69.         $mform->addElement('header''general'get_string('general''form'));
  70.         
  71.         // The type is not changeable with this form
  72.         // However, well present it as a frozen selection box, so it appears to the user with the full name,
  73.         //  but has the short-name underlying value
  74.         $typeelem &$mform->addElement('select''type'get_string('moduletype','sloodle')array($sloodletype => $sloodletypefull));
  75.         $mform->setDefault('type'$sloodletype);
  76.         $typeelem->freeze();
  77.         $typeelem->setPersistantFreeze(true);
  78.         $mform->setHelpButton('type'array("moduletype_$sloodletype"get_string('moduletype','sloodle')'sloodle'));
  79.                 
  80.         // Make a text box for the name of the module
  81.         $mform->addElement('text''name'get_string('name''sloodle')array('size'=>'64'));
  82.         // Make it text type
  83.         $mform->setType('name'PARAM_TEXT);
  84.         // Set a client-size rule that an entry is required
  85.         $mform->addRule('name'null'required'null'client');
  86.  
  87.         // Create an HTML editor for module description (intro text)
  88.         $mform->addElement('htmleditor''intro'get_string('description'));
  89.         // Make it raw type (so the HTML isn't filtered out)
  90.         $mform->setType('intro'PARAM_RAW);
  91.         // Make it required
  92.         //$mform->addRule('intro', get_string('required'), 'required', null, 'client'); // Don't require description - PRB
  93.         // Provide an HTML editor help button
  94.         $mform->setHelpButton('intro'array('writing''questions''richtext')false'editorhelpbutton');
  95.         
  96.         
  97. //-------------------------------------------------------------------------------
  98.         
  99.         // This section adds form elements which are specific to module types.
  100.         // Each type-specific data element will be prefixed with the type name, to avoid confusion.
  101.         // The "add_instance" and "update_instance" functions in Sloodle's "lib.php" file will then
  102.         //  process these data items, and write them to the appropriate tables.
  103.         
  104.         // NOTE: the Moodle framework will NOT automatically put the default/existing values here.
  105.         // Instead, they need to be added later, in the functions below.
  106.  
  107.         // Check which type is being added
  108.         switch ($sloodletype{
  109.         
  110.         // // CONTROLLER // //
  111.         
  112.         case SLOODLE_TYPE_CTRL:
  113.             // Add the type-specific Header
  114.             $mform->addElement('header''typeheader'$sloodletypefull);
  115.             
  116.             // Add a checkbox for whether or not this module is enabled
  117.             $mform->addElement('checkbox''controller_enabled'get_string('enabled''sloodle')get_string('controlaccess''sloodle'));
  118.             $mform->setDefault('controller_enabled'1);
  119.             
  120.             // Add a text-box for the prim password, with a help button describing it
  121.             $mform->addElement('text''controller_password'get_string('primpass''sloodle')array('size'=>'12','maxlength'=>'9'));
  122.             $mform->setHelpButton('controller_password'array('prim_password'get_string('help:primpassword','sloodle')'sloodle'));
  123.             // Set the field requirements
  124.             $mform->setDefault('controller_password'mt_rand(100000000999999999));
  125.             $mform->addRule('controller_password'null'numeric'null'client');
  126.             
  127.             // Prim Password can be omitted to disable it now (so don't require it)
  128.             //$mform->addRule('controller_password', null, 'required', null, 'client');
  129.             
  130.             break;
  131.             
  132.             
  133.             
  134.         // // DISTRIBUTOR // //
  135.         
  136.         case SLOODLE_TYPE_DISTRIB:
  137.         
  138.             // Add the type-specific Header
  139.             $mform->addElement('header''typeheader'$sloodletypefull);
  140.             
  141.             // Add a note of the current distributor channel (read-only)
  142.             $mform->addElement('text''distributor_channel'get_string('xmlrpc:channel''sloodle').': 'array('size'=>'40''readonly'=>'true''disabled'=>'true'));
  143.             $mform->setDefault('distributor_channel''');
  144.             
  145.             // Add a note of the number of objects associated with this Distributor
  146.             $mform->addElement('text''distributor_numobjects'get_string('numobjects''sloodle').': 'array('size'=>'4''readonly'=>'true''disabled'=>'true'));
  147.             $mform->setDefault('distributor_numobjects''0');
  148.             
  149.             // Add a checkbox option to reset the Distributor, but only if this is an existing entry being updated
  150.             if (!empty($this->_instance)) {
  151.                 $mform->addElement('checkbox''distributor_reset'get_string('reset').': 'get_string('sloodleobjectdistributor:reset''sloodle'));
  152.             }
  153.             
  154.             break;
  155.             
  156.             
  157.         // // SLIDESHOW // //
  158.         
  159.         case SLOODLE_TYPE_PRESENTER:
  160.         
  161.             // Add the type-specific Header
  162.             $mform->addElement('header''typeheader'$sloodletypefull);
  163.  
  164.             // Add boxes to enter the size of the frame
  165.             $mform->addElement('text''presenter_framewidth'get_string('framewidth''sloodle').': 'array('size'=>'4'));
  166.             $mform->addRule('presenter_framewidth'null'numeric'null'client');
  167.             $mform->setDefault('presenter_framewidth'512);
  168.             
  169.             $mform->addElement('text''presenter_frameheight'get_string('frameheight''sloodle').': 'array('size'=>'4'));
  170.             $mform->addRule('presenter_frameheight'null'numeric'null'client');
  171.             $mform->setDefault('presenter_frameheight'512);
  172.  
  173.             break;
  174.  
  175.  
  176.         // // MAP // //
  177.  
  178.         case SLOODLE_TYPE_MAP:
  179.  
  180.             // Add the type-specific header
  181.             $mform->addElement('header''typeheader'$sloodletypefull);
  182.             
  183.             // Add boxes for the initial coordinates of the map
  184.             $mform->addElement('text''map_initialx''Initial position (X): 'array('size'=>'10'))$mform->setDefault('map_initialx''1000.0');
  185.             $mform->addElement('text''map_initialy''Initial position (Y): 'array('size'=>'10'))$mform->setDefault('map_initialy''1000.0');
  186.             
  187.             // Add the initial zoom factor
  188.             $mform->addElement('text''map_initialzoom''Initial zoom level (1-6): 'array('size'=>'3'))$mform->setDefault('map_initialzoom''2');
  189.             $mform->addRule('map_initialzoom'null'numeric'null'client');
  190.             
  191.             // Add a checkbox for showing pan controls
  192.             $mform->addElement('checkbox''map_showpan''Pan controls: ''If checked, pan controls will be visible on the map.');
  193.             $mform->setDefault('map_showpan'1);
  194.             
  195.             // Add a checkbox for showing zoom controls
  196.             $mform->addElement('checkbox''map_showzoom''Zoom controls: ''If checked, zoom controls will be visible on the map.');
  197.             $mform->setDefault('map_showzoom'1);
  198.             
  199.             // Add a checkbox for allowing dragging of the map
  200.             $mform->addElement('checkbox''map_allowdrag''Allow dragging: ''If checked, users will be able to click-and-drag the map to pan it.');
  201.             $mform->setDefault('map_allowdrag'1);
  202.  
  203.             break;
  204.         
  205.         case SLOODLE_TYPE_IBANK:
  206.  
  207.             // Add the type-specific header
  208.             $mform->addElement('header''typeheader'$sloodletypefull);
  209.             
  210.             // The stipend needs an amount and a purpose
  211.             $mform->addElement('text''stipendgiver_amount'get_string('stipendgiver:amount','sloodle')array('size'=>'10'))
  212.             $mform->setDefault('stipendgiver_amount''0');
  213.             $pTypes=array('Lindens'=>'Lindens','iPoints'=>'iPoints');
  214.             $mform->addElement('select''icurrency',get_string('stipendgiver:typeofcurrency','sloodle'),$pTypes);
  215.             $mform->addRule('stipendgiver_amount'null'numeric'null'client');
  216.             break;
  217.         
  218.                                                                            
  219.        
  220.         
  221.          
  222. //-------------------------------------------------------------------------------
  223.         // Add the standard course module elements, except the group stuff (as Sloodle doesn't support it)
  224.         $this->standard_coursemodule_elements(false);
  225.         
  226. //-------------------------------------------------------------------------------
  227.         // Form buttons
  228.         $this->add_action_buttons();
  229.     }
  230.  
  231.     /**
  232.     * Performs extra processing on the form after existing/default data has been specified.
  233.     * @return void 
  234.     */
  235.     function definition_after_data({
  236.     }
  237.     
  238.     /**
  239.     * Pre-processes form initial values.
  240.     * Given an array of default values (element name => value) by reference, this function
  241.     *  can edit the initial values the user sees.
  242.     * Note that this is typically only used for editing existing modules, as the initial defaults
  243.     *  can be coded into the form definition.
  244.     * @param array $default_values Array of element names to values/
  245.     * @return void 
  246.     */
  247.     function data_preprocessing(&$default_values{
  248.         // Get the form
  249.         $mform =$this->_form;
  250.         
  251.         // Is this a new instance?
  252.         if (empty($this->_instance)) return;
  253.         // Check which type this is
  254.         switch ($default_values['type']{
  255.         case SLOODLE_TYPE_CTRL:
  256.             // Fetch the controller record
  257.             $controller get_record('sloodle_controller''sloodleid'$this->_instance);
  258.             if (!$controllererror(get_string('secondarytablenotfound''sloodle'));
  259.             
  260.             // Add in the 'enabled' value
  261.             $default_values['controller_enabled'$controller->enabled;
  262.             // Add in the prim password value
  263.             $default_values['controller_password'$controller->password;
  264.             
  265.             break;
  266.             
  267.         case SLOODLE_TYPE_DISTRIB:
  268.             // Fetch the distributor record
  269.             $distributor get_record('sloodle_distributor''sloodleid'$this->_instance);
  270.             if (!$distributorerror(get_string('secondarytablenotfound''sloodle'));
  271.             
  272.             // Add in the 'channel' value
  273.             $default_values['distributor_channel'$distributor->channel;
  274.             
  275.             // Retrieve all object entries for this Distributor
  276.             $objects get_records('sloodle_distributor_entry''distributorid'$distributor->id);
  277.             if (is_array($objects)) {
  278.                 $default_values['distributor_numobjects'count($objects);
  279.             }
  280.         
  281.             break;
  282.                 
  283.         case SLOODLE_TYPE_IBANK:
  284.             // Fetch the stipend giver record
  285.             $stipendgiver get_record('sloodle_stipendgiver''sloodleid'$this->_instance);
  286.             if (!$stipendgivererror(get_string('secondarytablenotfound''sloodle'));
  287.             
  288.             // Add in the amount and currency of the ibank
  289.             $default_values['stipendgiver_amount'$stipendgiver->amount;
  290.             $default_values['icurrency'$stipendgiver->icurrency;
  291.             
  292.             
  293.             break;
  294.   
  295.         case SLOODLE_TYPE_PRESENTER:
  296.             // Fetch the Presenter record.
  297.             $presenter get_record('sloodle_presenter''sloodleid'$this->_instance);
  298.             if (!$presentererror(get_string('secondarytablenotfound''sloodle'));
  299.  
  300.             // Add in the dimensions of the frame
  301.             $default_values['presenter_framewidth'= (int)$presenter->framewidth;
  302.             $default_values['presenter_frameheight'= (int)$presenter->frameheight;
  303.  
  304.             break;
  305.  
  306.         case SLOODLE_TYPE_MAP:
  307.             // Fetch the map record
  308.             $map get_record('sloodle_map''sloodleid'$this->_instance);
  309.             if (!$maperror(get_string('secondarytablenotfound''sloodle'));
  310.             
  311.             // Add in all the values from the database
  312.             $default_values['map_initialx'$map->initialx;
  313.             $default_values['map_initialy'$map->initialy;
  314.             $default_values['map_initialzoom'$map->initialzoom;
  315.             $default_values['map_showpan'$map->showpan;
  316.             $default_values['map_showzoom'$map->showzoom;
  317.             $default_values['map_allowdrag'$map->allowdrag;
  318.             
  319.             break;
  320.             
  321.         default:
  322.             // Nothing to do?
  323.             break;
  324.         }
  325.     }
  326.  
  327.  
  328.     /**
  329.      * Validates the form data.
  330.      * If there are errors return array of errors ("fieldname"=>"error message"),
  331.      * otherwise true if ok.
  332.      *
  333.      * @param array $data array of ("fieldname"=>value) of submitted data
  334.      * @return bool,array Array of fieldnames to error messages, or boolean true if OK
  335.      */
  336.     function validation($data{
  337.         global $SLOODLE_TYPES;
  338.         // Prepare an array of error messages
  339.         $errors array();
  340.     
  341.         // Check which type is being used
  342. switch ($data['type']{
  343.         
  344.         case SLOODLE_TYPE_CTRL:
  345.             // Check that the prim password is OK
  346.             $pwd '';
  347.             if (isset($data['controller_password'])) $pwd $data['controller_password'];
  348.             // The password can be left unspecified
  349.             if (empty($pwd)) break;
  350.             
  351.             // Validate the password we have been given
  352.             $pwderrors array();
  353.             if (!sloodle_validate_prim_password_verbose($pwd$pwderrors)) {
  354.                 $errors['controller_password''';
  355.                 // Add our password errors
  356.                 foreach ($pwderrors as $pe{
  357.                     $errors['controller_password'.= get_string("primpass:$pe"'sloodle''<br>';
  358.                 }
  359.             }
  360.             
  361.             break;
  362.             
  363.             
  364.         case SLOODLE_TYPE_DISTRIB:
  365.             // Nothing to error check
  366.             break;
  367.          
  368.         
  369.         case SLOODLE_TYPE_PRESENTER:
  370.             // Nothing to error check
  371.             break;
  372.         
  373.         case SLOODLE_TYPE_MAP:
  374.             // Nothing to error check
  375.             break
  376.  
  377.         // ADD FUTURE TYPES HERE
  378.            case SLOODLE_TYPE_IBANK:           //MOVED TO 0.41
  379.             // Nothing to error check
  380.            break
  381.  
  382.         // ADD FUTURE TYPES HERE
  383.         
  384.             
  385.         default:
  386.             // We don't know the type
  387.             $errors['type'get_string('moduletypeunknown''sloodle');
  388.             break;
  389.         }
  390.         
  391.         // Return the errors if there were any
  392.         if (count($errors0return $errors;
  393.         // Everything seems OK
  394.         return true;
  395.     }
  396.  
  397. }
  398. ?>

Documentation generated on Fri, 17 Jul 2009 11:02:09 +0100 by phpDocumentor 1.4.0