Simple math in database? Can this be done?

Simple math in database? Can this be done?

by John Patten -
Number of replies: 7
Hi All!

I would like to take a field in my database and divide it by 10 and report that info out in another database field. Can this be done?

Thank you!
Average of ratings: -
In reply to John Patten

Re: Simple math in database? Can this be done?

by Bill Mounce -
I am trying to do the same kind of thing in that I want to use php to access the value of a field, change it, and display it.So far no one has responded to my post. But my guess is that instead of php using javascript might be a better choice. But just a guess.

Bill
In reply to Bill Mounce

Re: Simple math in database? Can this be done?

by Itamar Tzadok -
Yes, that's a good guess. Something I do in the add entry page is open a dialog window for writing some formatted stuff (special characters and other things). Closing that window copies the formatted text into a textarea field (Moodle editor) and also sets a radio button field. All that (and more) with javascript. smile
In reply to Itamar Tzadok

Re: Simple math in database? Can this be done?

by Fabian Glagovsky -
Picture of Particularly helpful Moodlers Picture of Testers

Hi Itamar and Bill:

Could you help me? I am not a programmer, but I have learned a bit of JavaScript.

I would like to validate the fields of the database while the person is adding entries, so he does not leave some specific fields empty (for example, his lastname). Could you help me with the code? Is this possible to do in the DB module of Moodle?

Thanks in advance!

Fabian

In reply to Fabian Glagovsky

Re: Simple math in database? Can this be done?

by Itamar Tzadok -
Here is a quick and not necessarily the most elegant solution. This is a Firefox solution and won't work on IE. With some googling you should be able to fill in the blanks and make it cross-browser. First the code:

<script type="text/javascript">
function validate(e){
var fld=document.getElementById('XXXXX#id');
if (fld.value==''){
e.preventDefault();
alert('You must provide a value');
fld.focus();
return false;
}
}
var frm = document.getElementById('divForm').parentNode.parentNode.parentNode;
if (frm.addEventListener) frm.addEventListener('submit',validate,false);
</script>

Guidelines:
Enclose your current template code in a div tag with id 'divForm'.
Put the code at the end of the template (right after the close tag of 'divForm' you just added).
Change XXXXX to the desired field name.
Add other validations.
Try.
With some luck it will work.

Explanation:
The path '.parentNode.parentNode.parentNode' should return the form element which performs the submit when you save the entry. It is this submit that we want to capture and stop if the validation fails. Then we add a listener to capture the submit event. Upon save attempt the submit event is fired and the validate function is called. In the function the field to validate is retrieved, its value is checked and if not valid (empty in this example) the submit is canceled, a message is displayed and the focus is returned to that field.

I hope this helps. smile


In reply to Itamar Tzadok

Re: Simple math in database? Can this be done?

by Fabian Glagovsky -
Picture of Particularly helpful Moodlers Picture of Testers

Thank you Itamar, it worked on Firefox.

Any idea why it doesn't work on IE? Where should I start looking? I am really lost here.

Could you mark the line in the code you provided that it is not interpreted correctly in IE?

I  have tried adding another "If" in the function with

 e.cancelBubble = true;
e.returnValue = false;

an idea I took from here

http://www.openjs.com/articles/prevent_default_action/

But it doesn't work.

Toda!

Fabian

In reply to Fabian Glagovsky

Re: Simple math in database? Can this be done?

by Itamar Tzadok -
Here is some cross-browser code I found but haven't tried. Note that for IE you need to call window.attachEvent. Further explanation should be available in the following link: http://www.digital-web.com/articles/seven_javascript_techniques/

Hope that helps. smile

  1. var addListener = function() {
  2. if ( window.addEventListener ) {
  3. return function(el, type, fn) {
  4. el.addEventListener(type, fn, false);
  5. };
  6. } else if ( window.attachEvent ) {
  7. return function(el, type, fn) {
  8. var f = function() {
  9. fn.call(el, window.event);
  10. };
  11. el.attachEvent('on'+type, f);
  12. };
  13. } else {
  14. return function(el, type, fn) {
  15. element['on'+type] = fn;
  16. }
  17. }
  18. }();

In reply to Itamar Tzadok

Re: Simple math in database? Can this be done?

by Fabian Glagovsky -
Picture of Particularly helpful Moodlers Picture of Testers

Itamar: sorry for not replying sooner. Thank you very, very much for your help. I will try it.

Fabian