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');
  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.  
  158. //-------------------------------------------------------------------------------
  159.         // Add the standard course module elements, except the group stuff (as Sloodle doesn't support it)
  160.         $this->standard_coursemodule_elements(false);
  161.         
  162. //-------------------------------------------------------------------------------
  163.         // Form buttons
  164.         $this->add_action_buttons();
  165.     }
  166.  
  167.     /**
  168.     * Performs extra processing on the form after existing/default data has been specified.
  169.     * @return void 
  170.     */
  171.     function definition_after_data({
  172.     }
  173.     
  174.     /**
  175.     * Pre-processes form initial values.
  176.     * Given an array of default values (element name => value) by reference, this function
  177.     *  can edit the initial values the user sees.
  178.     * Note that this is typically only used for editing existing modules, as the initial defaults
  179.     *  can be coded into the form definition.
  180.     * @param array $default_values Array of element names to values/
  181.     * @return void 
  182.     */
  183.     function data_preprocessing(&$default_values{
  184.         // Get the form
  185.         $mform =$this->_form;
  186.         
  187.         // Is this a new instance?
  188.         if (empty($this->_instance)) return;
  189.         // Check which type this is
  190.         switch ($default_values['type']{
  191.         case SLOODLE_TYPE_CTRL:
  192.             // Fetch the controller record
  193.             $controller get_record('sloodle_controller''sloodleid'$this->_instance);
  194.             if (!$controllererror(get_string('secondarytablenotfound''sloodle'));
  195.             
  196.             // Add in the 'enabled' value
  197.             $default_values['controller_enabled'$controller->enabled;
  198.             // Add in the prim password value
  199.             $default_values['controller_password'$controller->password;
  200.             
  201.             break;
  202.             
  203.         case SLOODLE_TYPE_DISTRIB:
  204.             // Fetch the distributor record
  205.             $distributor get_record('sloodle_distributor''sloodleid'$this->_instance);
  206.             if (!$distributorerror(get_string('secondarytablenotfound''sloodle'));
  207.             
  208.             // Add in the 'channel' value
  209.             $default_values['distributor_channel'$distributor->channel;
  210.             
  211.             // Retrieve all object entries for this Distributor
  212.             $objects get_records('sloodle_distributor_entry''distributorid'$distributor->id);
  213.             if (is_array($objects)) {
  214.                 $default_values['distributor_numobjects'count($objects);
  215.             }
  216.         
  217.             break;
  218.             
  219.         default:
  220.             // Nothing to do?
  221.             break;
  222.         }
  223.     }
  224.  
  225.  
  226.     /**
  227.      * Validates the form data.
  228.      * If there are errors return array of errors ("fieldname"=>"error message"),
  229.      * otherwise true if ok.
  230.      *
  231.      * @param array $data array of ("fieldname"=>value) of submitted data
  232.      * @return bool,array Array of fieldnames to error messages, or boolean true if OK
  233.      */
  234.     function validation($data{
  235.         global $SLOODLE_TYPES;
  236.         // Prepare an array of error messages
  237.         $errors array();
  238.     
  239.         // Check which type is being used
  240.         switch ($data['type']{
  241.         
  242.         case SLOODLE_TYPE_CTRL:
  243.             // Check that the prim password is OK
  244.             $pwd '';
  245.             if (isset($data['controller_password'])) $pwd $data['controller_password'];
  246.             // The password can be left unspecified
  247.             if (empty($pwd)) break;
  248.             
  249.             // Validate the password we have been given
  250.             $pwderrors array();
  251.             if (!sloodle_validate_prim_password_verbose($pwd$pwderrors)) {
  252.                 $errors['controller_password''';
  253.                 // Add our password errors
  254.                 foreach ($pwderrors as $pe{
  255.                     $errors['controller_password'.= get_string("primpass:$pe"'sloodle''<br>';
  256.                 }
  257.             }
  258.             
  259.             break;
  260.             
  261.             
  262.         case SLOODLE_TYPE_DISTRIB:
  263.             // Nothing to error check
  264.             break;
  265.          
  266.  
  267.         // ADD FUTURE TYPES HERE
  268.         
  269.             
  270.         default:
  271.             // We don't know the type
  272.             $errors['type'get_string('moduletypeunknown''sloodle');
  273.             break;
  274.         }
  275.         
  276.         // Return the errors if there were any
  277.         if (count($errors0return $errors;
  278.         // Everything seems OK
  279.         return true;
  280.     }
  281.  
  282. }
  283. ?>

Documentation generated on Mon, 07 Jul 2008 12:33:02 +0100 by phpDocumentor 1.4.0