Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
mahara
mahara
Commits
b0bf555b
Commit
b0bf555b
authored
Nov 14, 2006
by
Richard Mansfield
Browse files
Expiry element added
parent
b4c67c4b
Changes
1
Hide whitespace changes
Inline
Side-by-side
htdocs/lib/form/elements/expiry.php
0 → 100644
View file @
b0bf555b
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage form-element
* @author Nigel McNie <nigel@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined
(
'INTERNAL'
)
||
die
();
/**
* Provides a duration chooser, with a text box for a number and a
* select box to choose the units, in days, weeks, months, years, or 'no end date'.
*
* @param array $element The element to render
* @param Form $form The form to render the element for
* @return string The HTML for the element
*/
function
form_render_expiry
(
$element
,
Form
$form
)
{
$result
=
''
;
$name
=
$element
[
'name'
];
if
(
!
isset
(
$element
[
'defaultvalue'
]))
{
$element
[
'defaultvalue'
]
=
null
;
}
$global
=
(
$form
->
get_method
()
==
'get'
)
?
$_GET
:
$_POST
;
// Get the value of the element for rendering. The values of the
// two inputs are rendered, and the total time in seconds is
// stored in a hidden input.
if
(
isset
(
$element
[
'value'
]))
{
$seconds
=
$element
[
'value'
];
$values
=
get_expiry_from_seconds
(
$element
[
'value'
]);
}
else
if
(
isset
(
$global
[
$element
[
'name'
]
.
'_number'
])
&&
isset
(
$global
[
$element
[
'name'
]
.
'_units'
]))
{
$values
=
array
(
'number'
=>
$global
[
$element
[
'name'
]
.
'_number'
],
'units'
=>
$global
[
$element
[
'name'
]
.
'_units'
]);
$seconds
=
$values
[
'number'
]
*
seconds_in
(
$values
[
'units'
]);
}
else
if
(
isset
(
$element
[
'defaultvalue'
]))
{
$seconds
=
$element
[
'defaultvalue'
];
$values
=
get_expiry_from_seconds
(
$seconds
);
}
else
{
$values
=
array
(
'number'
=>
''
,
'units'
=>
'noenddate'
);
$seconds
=
null
;
}
$numberinput
=
'<input '
.
(
$form
->
get_ajaxpost
()
?
'onchange="'
.
$name
.
'_change()" '
:
''
);
$numberinput
.
=
'type="text" size="4" '
.
'name="'
.
$name
.
'_number" '
;
$numberinput
.
=
'id="'
.
$name
.
'_number" value="'
.
$values
[
'number'
]
.
"
\"
>
\n
"
;
$allunits
=
get_expiry_units
();
$uselect
=
'<select '
.
(
$form
->
get_ajaxpost
()
?
'onchange="'
.
$name
.
'_change()" '
:
''
);
$uselect
.
=
'name="'
.
$name
.
'_units" id="'
.
$name
.
'_units"'
.
">
\n
"
;
foreach
(
$allunits
as
$u
)
{
$uselect
.
=
"
\t
<option value=
\"
$u
\"
"
.
((
$values
[
'units'
]
==
$u
)
?
' selected="selected"'
:
''
)
.
'>'
.
get_string
(
$u
)
.
"</option>
\n
"
;
}
$uselect
.
=
"</select>
\n
"
;
// The hidden input contains the value of the expiry in seconds
$hidden
=
'<input type="hidden" name="'
.
$name
.
'" id="'
.
$name
.
'" value="'
.
$seconds
.
"
\"
>
\n
"
;
// Every time one of the two inputs is changed, update the number
// of seconds in the hidden input.
if
(
$form
->
get_ajaxpost
())
{
$script
=
<<<
EOJS
<
script
type
=
"text/javascript"
language
=
"javascript"
>
function
{
$name
}
_change
()
{
var
seconds
=
null
;
if
(
$
(
'{$name}_number'
)
.
value
>
0
)
{
var
mult
=
$
(
'{$name}_number'
)
.
value
*
60
*
60
*
24
;
if
(
$
(
'{$name}_units'
)
.
value
==
'days'
)
{
seconds
=
mult
;
}
else
if
(
$
(
'{$name}_units'
)
.
value
==
'weeks'
)
{
seconds
=
mult
*
7
;
}
else
if
(
$
(
'{$name}_units'
)
.
value
==
'months'
)
{
seconds
=
mult
*
30
;
}
else
if
(
$
(
'{$name}_units'
)
.
value
==
'years'
)
{
seconds
=
mult
*
365
;
}
}
$
(
'{$name}'
)
.
value
=
seconds
;
}
</
script
>
EOJS
;
}
else
{
$script
=
''
;
}
return
$numberinput
.
$uselect
.
$hidden
.
$script
;
}
function
get_expiry_units
()
{
return
array
(
'days'
,
'weeks'
,
'months'
,
'years'
,
'noenddate'
);
}
function
seconds_in
(
$unit
)
{
$dayseconds
=
60
*
60
*
24
;
switch
(
$unit
)
{
case
'days'
:
return
$dayseconds
;
case
'weeks'
:
return
$dayseconds
*
7
;
case
'months'
:
return
$dayseconds
*
30
;
case
'years'
:
return
$dayseconds
*
365
;
default
:
return
null
;
}
}
function
get_expiry_from_seconds
(
$seconds
)
{
if
(
$seconds
==
null
)
{
return
array
(
'number'
=>
''
,
'units'
=>
'noenddate'
);
}
// This needs work to produce sensible values; at the moment it will convert
// 60 days into 2 months; 70 days into 7 weeks, etc.
$yearseconds
=
seconds_in
(
'years'
);
if
(
$seconds
%
$yearseconds
==
0
&&
$seconds
>
0
)
{
return
array
(
'number'
=>
(
int
)
(
$seconds
/
$yearseconds
),
'units'
=>
'years'
);
}
$monthseconds
=
seconds_in
(
'months'
);
if
(
$seconds
%
$monthseconds
==
0
&&
$seconds
>
0
)
{
return
array
(
'number'
=>
(
int
)
(
$seconds
/
$monthseconds
),
'units'
=>
'months'
);
}
$weekseconds
=
seconds_in
(
'weeks'
);
if
(
$seconds
%
$weekseconds
==
0
&&
$seconds
>
0
)
{
return
array
(
'number'
=>
(
int
)
(
$seconds
/
$weekseconds
),
'units'
=>
'weeks'
);
}
$dayseconds
=
seconds_in
(
'days'
);
if
(
$seconds
%
$dayseconds
==
0
)
{
return
array
(
'number'
=>
(
int
)
(
$seconds
/
$dayseconds
),
'units'
=>
'days'
);
}
return
null
;
}
// /** gets the value explicitly from the request */
// function form_get_value_expiry($element, Form $form) {
// $name = $element['name'];
// $global = ($form->get_method() == 'get') ? $_GET : $_POST;
// return $global[$name];
// //$unit = $global[$name . '_units'];
// //if ($unit == 'noenddate') {
// // return null;
// //}
// //$allunits = get_expiry_units();
// //$number = $global[$name . '_number'];
// //if (!in_array($unit,$allunits) || $number < 0) {
// // return null;
// //}
// //return $number * seconds_in($unit);
// }
?>
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment