Skip to content

Commit

Permalink
MDL-24806 new time/date profile field
Browse files Browse the repository at this point in the history
Credit goes to Mark Nelson - Pukunui Technology
  • Loading branch information
skodak committed Nov 11, 2010
1 parent 69a00d0 commit 68f94e2
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
80 changes: 80 additions & 0 deletions user/profile/field/datetime/define.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* Define datetime fields
*
* @author Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version 20101022
*/

class profile_define_datetime extends profile_define_base {

/**
* Define the setting for a datetime custom field
*
* @param object $form the user form
*/
function define_form_specific($form) {
// Create variables to store start and end
$currentyear = date('Y');
$startyear = $currentyear - 100;
$endyear = $currentyear + 20;

// Create array for the years
$arryears = array();
for ($i = $startyear; $i <= $endyear; $i++) {
$arryears[$i] = $i;
}

// Add elements
$form->addElement('select', 'param1', get_string('startyear', 'profilefield_datetime'), $arryears);
$form->setType('param1', PARAM_INT);
$form->setDefault('param1', $currentyear);

$form->addElement('select', 'param2', get_string('endyear', 'profilefield_datetime'), $arryears);
$form->setType('param2', PARAM_INT);
$form->setDefault('param2', $currentyear + 20);

$form->addElement('checkbox', 'param3', get_string('wanttime', 'profilefield_datetime'));
$form->setType('param4', PARAM_INT);

$form->addElement('hidden', 'defaultdata', '0');
$form->setType('defaultdata', PARAM_INT);
}

/**
* Validate the data from the profile field form
*
* @param object data from the add/edit profile field form
* @return array associative array of error messages
*/
function define_validate_specific($data) {
$errors = array();

// Make sure the start year is not greater than the end year
if ($data->param1 > $data->param2) {
$errors['param1'] = get_string('startyearafterend', 'profilefield_datetime');
}

return $errors;
}

/**
* Preprocess data from the profile field form before
* it is saved.
*
* @param object data from the add/edit profile field form
* @return object processed data object
*/
function define_save_preprocess($data) {
if (empty($data->param3)) {
$data->param3 = NULL;
}

// No valid value in the default data column needed
$data->defaultdata = '0';

return $data;
}
}
63 changes: 63 additions & 0 deletions user/profile/field/datetime/field.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Handles displaying and editing the datetime field
*
* @author Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version 20101022
*/

class profile_field_datetime extends profile_field_base {

/**
* Handles editing datetime fields
*
* @param object moodleform instance
*/
function edit_field_add($mform) {
// Check if the field is required
if ($this->field->required) {
$optional = false;
} else {
$optional = true;
}

$attributes = array(
'startyear' => $this->field->param1,
'stopyear' => $this->field->param2,
'timezone' => 99,
'applydst' => true,
'optional' => $optional
);

// Check if they wanted to include time as well
if (!empty($this->field->param3)) {
$mform->addElement('date_time_selector', $this->inputname, format_string($this->field->name), $attributes);
} else {
$mform->addElement('date_selector', $this->inputname, format_string($this->field->name), $attributes);
}

$mform->setType($this->inputname, PARAM_INT);
$mform->setDefault($this->inputname, time());
}

/**
* Display the data for this field
*/
function display_data() {
// Check if time was specified
if (!empty($this->field->param3)) {
$format = get_string('strftimedaydatetime', 'langconfig');
} else {
$format = get_string('strftimedate', 'langconfig');
}

// Check if a date has been specified
if (empty($this->data)) {
return get_string('notset', 'profilefield_datetime');
} else {
return userdate($this->data, $format);
}
}
}
19 changes: 19 additions & 0 deletions user/profile/field/datetime/lang/en/profilefield_datetime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* The english language pack used in this profile field type
*
* @author Mark Nelson - Pukunui Technology
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version 20100611
*/

$string['currentdatedefault'] = 'Check to use current date as default';
$string['defaultdate'] = 'Default date';
$string['endyear'] = 'End year';
$string['notset'] = 'Not set';
$string['pluginname'] = 'Date/Time';
$string['specifydatedefault'] = 'or specify a date';
$string['startyearafterend'] = 'The start year can\'t occur after the end year';
$string['startyear'] = 'Start year';
$string['wanttime'] = 'Include time?';

0 comments on commit 68f94e2

Please sign in to comment.