Merge branch 'MDL-34853' of local stronk7 repo.

(originally w34_MDL-34853_m24_durationsetting @ git://github.com/skodak/moodle.git)
This commit is contained in:
Eloy Lafuente (stronk7)
2012-08-22 03:41:14 +02:00
9 changed files with 175 additions and 11 deletions
+1 -1
View File
@@ -30,7 +30,7 @@ $string['altertimestart'] = 'Alter start time';
$string['assignrole'] = 'Assign role';
$string['confirmbulkdeleteenrolment'] = 'Are you sure you want to delete these users enrolments?';
$string['defaultperiod'] = 'Default enrolment duration';
$string['defaultperiod_desc'] = 'Default length of time that the enrolment is valid (in seconds). If set to zero, the enrolment duration will be unlimited by default.';
$string['defaultperiod_desc'] = 'Default length of time that the enrolment is valid. If set to zero, the enrolment duration will be unlimited by default.';
$string['defaultperiod_help'] = 'Default length of time that the enrolment is valid, starting with the moment the user is enrolled. If disabled, the enrolment duration will be unlimited by default.';
$string['deleteselectedusers'] = 'Delete selected user enrolments';
$string['editenrolment'] = 'Edit enrolment';
+2 -2
View File
@@ -44,8 +44,8 @@ if ($ADMIN->fulltree) {
$settings->add(new admin_setting_configselect('enrol_manual/status',
get_string('status', 'enrol_manual'), get_string('status_desc', 'enrol_manual'), ENROL_INSTANCE_ENABLED, $options));
$settings->add(new admin_setting_configtext('enrol_manual/enrolperiod',
get_string('defaultperiod', 'enrol_manual'), get_string('defaultperiod_desc', 'enrol_manual'), 0, PARAM_INT));
$settings->add(new admin_setting_configduration('enrol_manual/enrolperiod',
get_string('defaultperiod', 'enrol_manual'), get_string('defaultperiod_desc', 'enrol_manual'), 0));
if (!during_initial_install()) {
$options = get_default_enrol_roles(context_system::instance());
+1 -1
View File
@@ -37,7 +37,7 @@ $string['enrolenddate'] = 'End date';
$string['enrolenddate_help'] = 'If enabled, users can be enrolled until this date only.';
$string['enrolenddaterror'] = 'Enrolment end date cannot be earlier than start date';
$string['enrolperiod'] = 'Enrolment duration';
$string['enrolperiod_desc'] = 'Default length of time that the enrolment is valid (in seconds). If set to zero, the enrolment duration will be unlimited by default.';
$string['enrolperiod_desc'] = 'Default length of time that the enrolment is valid. If set to zero, the enrolment duration will be unlimited by default.';
$string['enrolperiod_help'] = 'Length of time that the enrolment is valid, starting with the moment the user is enrolled. If disabled, the enrolment duration will be unlimited.';
$string['enrolstartdate'] = 'Start date';
$string['enrolstartdate_help'] = 'If enabled, users can be enrolled from this date onward only.';
+2 -2
View File
@@ -68,6 +68,6 @@ if ($ADMIN->fulltree) {
get_string('defaultrole', 'enrol_paypal'), get_string('defaultrole_desc', 'enrol_paypal'), $student->id, $options));
}
$settings->add(new admin_setting_configtext('enrol_paypal/enrolperiod',
get_string('enrolperiod', 'enrol_paypal'), get_string('enrolperiod_desc', 'enrol_paypal'), 0, PARAM_INT));
$settings->add(new admin_setting_configduration('enrol_paypal/enrolperiod',
get_string('enrolperiod', 'enrol_paypal'), get_string('enrolperiod_desc', 'enrol_paypal'), 0));
}
+1 -1
View File
@@ -39,7 +39,7 @@ $string['enrolenddate_help'] = 'If enabled, users can enrol themselves until thi
$string['enrolenddaterror'] = 'Enrolment end date cannot be earlier than start date';
$string['enrolme'] = 'Enrol me';
$string['enrolperiod'] = 'Enrolment duration';
$string['enrolperiod_desc'] = 'Default length of time that the enrolment is valid (in seconds). If set to zero, the enrolment duration will be unlimited by default.';
$string['enrolperiod_desc'] = 'Default length of time that the enrolment is valid. If set to zero, the enrolment duration will be unlimited by default.';
$string['enrolperiod_help'] = 'Length of time that the enrolment is valid, starting with the moment the user enrols themselves. If disabled, the enrolment duration will be unlimited.';
$string['enrolstartdate'] = 'Start date';
$string['enrolstartdate_help'] = 'If enabled, users can enrol themselves from this date onward only.';
+2 -2
View File
@@ -65,8 +65,8 @@ if ($ADMIN->fulltree) {
get_string('defaultrole', 'enrol_self'), get_string('defaultrole_desc', 'enrol_self'), $student->id, $options));
}
$settings->add(new admin_setting_configtext('enrol_self/enrolperiod',
get_string('enrolperiod', 'enrol_self'), get_string('enrolperiod_desc', 'enrol_self'), 0, PARAM_INT));
$settings->add(new admin_setting_configduration('enrol_self/enrolperiod',
get_string('enrolperiod', 'enrol_self'), get_string('enrolperiod_desc', 'enrol_self'), 0));
$options = array(0 => get_string('never'),
1800 * 3600 * 24 => get_string('numdays', '', 1800),
+1
View File
@@ -1805,6 +1805,7 @@ $string['visibletostudents'] = 'Visible to {$a}';
$string['warningdeleteresource'] = 'Warning: {$a} is referred in a resource. Would you like to update the resource?';
$string['webpage'] = 'Web page';
$string['week'] = 'Week';
$string['weeks'] = 'weeks';
$string['weekhide'] = 'Hide this week from {$a}';
$string['weeklyoutline'] = 'Weekly outline';
$string['weekshow'] = 'Show this week to {$a}';
+162
View File
@@ -2768,6 +2768,168 @@ class admin_setting_configtime extends admin_setting {
}
/**
* Seconds duration setting.
*
* @copyright 2012 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_setting_configduration extends admin_setting {
/** @var int default duration unit */
protected $defaultunit;
/**
* Constructor
* @param string $name unique ascii name, either 'mysetting' for settings that in config,
* or 'myplugin/mysetting' for ones in config_plugins.
* @param string $visiblename localised name
* @param string $description localised long description
* @param mixed $defaultsetting string or array depending on implementation
* @param int $defaultunit - day, week, etc. (in seconds)
*/
public function __construct($name, $visiblename, $description, $defaultsetting, $defaultunit = 86400) {
if (is_number($defaultsetting)) {
$defaultsetting = self::parse_seconds($defaultsetting);
}
$units = self::get_units();
if (isset($units[$defaultunit])) {
$this->defaultunit = $defaultunit;
} else {
$this->defaultunit = 86400;
}
parent::__construct($name, $visiblename, $description, $defaultsetting);
}
/**
* Returns selectable units.
* @static
* @return array
*/
protected static function get_units() {
return array(
604800 => get_string('weeks'),
86400 => get_string('days'),
3600 => get_string('hours'),
60 => get_string('minutes'),
1 => get_string('seconds'),
);
}
/**
* Converts seconds to some more user friendly string.
* @static
* @param int $seconds
* @return string
*/
protected static function get_duration_text($seconds) {
if (empty($seconds)) {
return get_string('none');
}
$data = self::parse_seconds($seconds);
switch ($data['u']) {
case (60*60*24*7):
return get_string('numweeks', '', $data['v']);
case (60*60*24):
return get_string('numdays', '', $data['v']);
case (60*60):
return get_string('numhours', '', $data['v']);
case (60):
return get_string('numminutes', '', $data['v']);
default:
return get_string('numseconds', '', $data['v']*$data['u']);
}
}
/**
* Finds suitable units for given duration.
* @static
* @param int $seconds
* @return array
*/
protected static function parse_seconds($seconds) {
foreach (self::get_units() as $unit => $unused) {
if ($seconds % $unit === 0) {
return array('v'=>(int)($seconds/$unit), 'u'=>$unit);
}
}
return array('v'=>(int)$seconds, 'u'=>1);
}
/**
* Get the selected duration as array.
*
* @return mixed An array containing 'v'=>xx, 'u'=>xx, or null if not set
*/
public function get_setting() {
$seconds = $this->config_read($this->name);
if (is_null($seconds)) {
return null;
}
return self::parse_seconds($seconds);
}
/**
* Store the duration as seconds.
*
* @param array $data Must be form 'h'=>xx, 'm'=>xx
* @return bool true if success, false if not
*/
public function write_setting($data) {
if (!is_array($data)) {
return '';
}
$seconds = (int)($data['v']*$data['u']);
if ($seconds < 0) {
return get_string('errorsetting', 'admin');
}
$result = $this->config_write($this->name, $seconds);
return ($result ? '' : get_string('errorsetting', 'admin'));
}
/**
* Returns duration text+select fields.
*
* @param array $data Must be form 'v'=>xx, 'u'=>xx
* @param string $query
* @return string duration text+select fields and wrapping div(s)
*/
public function output_html($data, $query='') {
$default = $this->get_defaultsetting();
if (is_number($default)) {
$defaultinfo = self::get_duration_text($default);
} else if (is_array($default)) {
$defaultinfo = self::get_duration_text($default['v']*$default['u']);
} else {
$defaultinfo = null;
}
$units = self::get_units();
$return = '<div class="form-duration defaultsnext">';
$return .= '<input type="text" size="5" id="'.$this->get_id().'v" name="'.$this->get_full_name().'[v]" value="'.s($data['v']).'" />';
$return .= '<select id="'.$this->get_id().'u" name="'.$this->get_full_name().'[u]">';
foreach ($units as $val => $text) {
$selected = '';
if ($data['v'] == 0) {
if ($val == $this->defaultunit) {
$selected = ' selected="selected"';
}
} else if ($val == $data['u']) {
$selected = ' selected="selected"';
}
$return .= '<option value="'.$val.'"'.$selected.'>'.$text.'</option>';
}
$return .= '</select></div>';
return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', $defaultinfo, $query);
}
}
/**
* Used to validate a textarea used for ip addresses
*
+3 -2
View File
@@ -58,8 +58,8 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group {
* @param string $elementName Element's name
* @param mixed $elementLabel Label(s) for an element
* @param array $options Options to control the element's display. Recognised values are
'optional' => true/false - whether to display an 'enabled' checkbox next to the element.
'defaultunit' => 1|60|3600|86400 - the default unit to display when the time is blank.
* 'optional' => true/false - whether to display an 'enabled' checkbox next to the element.
* 'defaultunit' => 1|60|3600|86400|604800 - the default unit to display when the time is blank.
* If not specified, minutes is used.
* @param mixed $attributes Either a typical HTML attribute string or an associative array
*/
@@ -91,6 +91,7 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group {
public function get_units() {
if (is_null($this->_units)) {
$this->_units = array(
604800 => get_string('weeks'),
86400 => get_string('days'),
3600 => get_string('hours'),
60 => get_string('minutes'),