Source for file mod_form.php
Documentation is available at mod_form.php
* Sloodle module add/edit form.
* This script defines a form used to add/edit instances of the Sloodle module.
* @copyright Copyright (c) 2008 Sloodle (various contributors)
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
* @contributor Peter R. Bloomfield
/** Core Sloodle configuration/functionality */
require_once($CFG->dirroot.
'/mod/sloodle/sl_config.php');
/** The base class for the Moodle module form */
require_once ($CFG->dirroot.
'/course/moodleform_mod.php');
/** General Sloodle functionality */
require_once(SLOODLE_LIBROOT.
'/general.php');
* Used to define the Sloodle module instance add/edit form.
global $CFG, $COURSE, $SLOODLE_TYPES;
//-------------------------------------------------------------------------------
// We need to know which type is being added/edited
// Are we adding a new instance?
if (empty($this->_instance)) {
// Yes - check for a 'type' parameter
$sloodletype =
required_param('type', PARAM_TEXT);
// Fetch the instance data
$rec =
get_record('sloodle', 'id', $this->_instance);
if (!$rec) error(get_string('modulenotfound'));
error(get_string('moduletypeunknown', 'sloodle'));
$sloodletype =
$rec->type;
// Store the fullname of the type
$sloodletypefull =
get_string("moduletype:$sloodletype", 'sloodle');
//-------------------------------------------------------------------------------
$mform->addElement('header', 'general', get_string('general', 'form'));
// The type is not changeable with this form
// However, well present it as a frozen selection box, so it appears to the user with the full name,
// but has the short-name underlying value
$typeelem =
&$mform->addElement('select', 'type', get_string('moduletype','sloodle'), array($sloodletype =>
$sloodletypefull));
$mform->setDefault('type', $sloodletype);
$typeelem->setPersistantFreeze(true);
$mform->setHelpButton('type', array("moduletype_$sloodletype", get_string('moduletype','sloodle'), 'sloodle'));
// Make a text box for the name of the module
$mform->addElement('text', 'name', get_string('name', 'sloodle'), array('size'=>
'64'));
$mform->setType('name', PARAM_TEXT);
// Set a client-size rule that an entry is required
$mform->addRule('name', null, 'required', null, 'client');
// Create an HTML editor for module description (intro text)
$mform->addElement('htmleditor', 'intro', get_string('description'));
// Make it raw type (so the HTML isn't filtered out)
$mform->setType('intro', PARAM_RAW);
$mform->addRule('intro', get_string('required'), 'required', null, 'client');
// Provide an HTML editor help button
$mform->setHelpButton('intro', array('writing', 'questions', 'richtext'), false, 'editorhelpbutton');
//-------------------------------------------------------------------------------
// This section adds form elements which are specific to module types.
// Each type-specific data element will be prefixed with the type name, to avoid confusion.
// The "add_instance" and "update_instance" functions in Sloodle's "lib.php" file will then
// process these data items, and write them to the appropriate tables.
// NOTE: the Moodle framework will NOT automatically put the default/existing values here.
// Instead, they need to be added later, in the functions below.
// Check which type is being added
// Add the type-specific Header
$mform->addElement('header', 'typeheader', $sloodletypefull);
// Add a checkbox for whether or not this module is enabled
$mform->addElement('checkbox', 'controller_enabled', get_string('enabled', 'sloodle'), get_string('controlaccess', 'sloodle'));
$mform->setDefault('controller_enabled', 1);
// Add a text-box for the prim password, with a help button describing it
$mform->addElement('text', 'controller_password', get_string('primpass', 'sloodle'), array('size'=>
'12','maxlength'=>
'9'));
$mform->setHelpButton('controller_password', array('prim_password', get_string('help:primpassword','sloodle'), 'sloodle'));
// Set the field requirements
$mform->setDefault('controller_password', mt_rand(100000000, 999999999));
$mform->addRule('controller_password', null, 'numeric', null, 'client');
// Prim Password can be omitted to disable it now (so don't require it)
//$mform->addRule('controller_password', null, 'required', null, 'client');
// Add the type-specific Header
$mform->addElement('header', 'typeheader', $sloodletypefull);
// Add a note of the current distributor channel (read-only)
$mform->addElement('text', 'distributor_channel', get_string('xmlrpc:channel', 'sloodle').
': ', array('size'=>
'40', 'readonly'=>
'true', 'disabled'=>
'true'));
$mform->setDefault('distributor_channel', '');
// Add a note of the number of objects associated with this Distributor
$mform->addElement('text', 'distributor_numobjects', get_string('numobjects', 'sloodle').
': ', array('size'=>
'4', 'readonly'=>
'true', 'disabled'=>
'true'));
$mform->setDefault('distributor_numobjects', '0');
// Add a checkbox option to reset the Distributor, but only if this is an existing entry being updated
if (!empty($this->_instance)) {
$mform->addElement('checkbox', 'distributor_reset', get_string('reset').
': ', get_string('sloodleobjectdistributor:reset', 'sloodle'));
//-------------------------------------------------------------------------------
// Add the standard course module elements, except the group stuff (as Sloodle doesn't support it)
$this->standard_coursemodule_elements(false);
//-------------------------------------------------------------------------------
$this->add_action_buttons();
* Performs extra processing on the form after existing/default data has been specified.
* Pre-processes form initial values.
* Given an array of default values (element name => value) by reference, this function
* can edit the initial values the user sees.
* Note that this is typically only used for editing existing modules, as the initial defaults
* can be coded into the form definition.
* @param array $default_values Array of element names to values/
// Is this a new instance?
if (empty($this->_instance)) return;
// Check which type this is
switch ($default_values['type']) {
// Fetch the controller record
$controller =
get_record('sloodle_controller', 'sloodleid', $this->_instance);
if (!$controller) error(get_string('secondarytablenotfound', 'sloodle'));
// Add in the 'enabled' value
$default_values['controller_enabled'] =
$controller->enabled;
// Add in the prim password value
$default_values['controller_password'] =
$controller->password;
// Fetch the distributor record
$distributor =
get_record('sloodle_distributor', 'sloodleid', $this->_instance);
if (!$distributor) error(get_string('secondarytablenotfound', 'sloodle'));
// Add in the 'channel' value
$default_values['distributor_channel'] =
$distributor->channel;
// Retrieve all object entries for this Distributor
$objects =
get_records('sloodle_distributor_entry', 'distributorid', $distributor->id);
$default_values['distributor_numobjects'] =
count($objects);
* Validates the form data.
* If there are errors return array of errors ("fieldname"=>"error message"),
* @param array $data array of ("fieldname"=>value) of submitted data
* @return bool,array Array of fieldnames to error messages, or boolean true if OK
// Prepare an array of error messages
// Check which type is being used
// Check that the prim password is OK
if (isset
($data['controller_password'])) $pwd =
$data['controller_password'];
// The password can be left unspecified
// Validate the password we have been given
$errors['controller_password'] =
'';
// Add our password errors
foreach ($pwderrors as $pe) {
$errors['controller_password'] .=
get_string("primpass:$pe", 'sloodle') .
'<br>';
// Nothing to error check
// We don't know the type
$errors['type'] =
get_string('moduletypeunknown', 'sloodle');
// Return the errors if there were any
if (count($errors) >
0) return $errors;
Documentation generated on Mon, 16 Jun 2008 15:56:51 +0100 by phpDocumentor 1.4.0