MDL-22054 converting block name strings to pluginname
[moodle.git] / blocks / loancalc / block_loancalc.php
CommitLineData
4ca6cfbf 1<?php
3e920f36 2
84c57908 3class block_loancalc extends block_base {
3e920f36 4
5 function init() {
4931465f 6 $this->title = get_string('pluginname','block_loancalc');
3e920f36 7 $this->content_type = BLOCK_TYPE_TEXT;
433c242f 8 $this->version = 2007101509;
3e920f36 9 }
10
11 function get_content() {
6b608f8f 12 global $CFG, $OUTPUT;
3e920f36 13
b5d0cafc 14 $calc = $OUTPUT->pix_url('i/calc');
4ca6cfbf 15
3e920f36 16 $this->content->text = '
f4ba7e1a 17 <script type="text/javascript">
948acd80 18 // <![CDATA[
61240489 19 function Next()
20 {
21 submitScreen("Next");
599475c7 22 document.getElementById("vbankform").submit();
61240489 23 }
24 function Back()
25 {
26 submitScreen("Back");
599475c7 27 document.getElementById("vbankform").submit();
61240489 28 }
3e920f36 29
61240489 30 function num_format(x) { // format numbers with two digits
31 sgn = (x < 0);
32 x = Math.abs(x);
33 x = Math.floor((x * 100) + .5);
34 i = 3;
35 y = "";
36 while(((i--) > 0) || (x > 0)) {
37 y = (x % 10) + y;
38 x = Math.floor(x / 10);
39 if(i == 1) {
40 y = "." + y;
41 }
42 }
43 if(sgn) {
44 y = "-" + y;
45 }
46 return(y);
3e920f36 47}
48function comp(v) { // general entry point for all cases
49
61240489 50 // convert all entry fields into variables
599475c7 51 x = document.getElementById("vbankform");
61240489 52 pv = parseFloat(x.LOANAMOUNT.value);
53 lpp = parseFloat(x.LOANPAYPERIOD[x.LOANPAYPERIOD.selectedIndex].value);
54 if (isNaN(pv) && (v != "pv"))
55 {
56 x.LOANAMOUNT.select();
57 x.LOANAMOUNT.focus();
58 alert("Numbers only to be entered");
59 return;
60 }
61 fv = parseFloat("0");
62 yr = parseFloat(x.LOANTERM.value);
63 if (isNaN(yr) && (v != "np"))
64 {
65 x.LOANTERM.select();
66 x.LOANTERM.focus();
67 alert("Numbers only to be entered");
68 return;
69 }
70 np = lpp * yr;
71 pmt = -parseFloat(x.LOANREPAYMENT.value);
72 if (isNaN(pmt) && (v != "pmt"))
73 {
74 x.LOANREPAYMENT.select();
75 x.LOANREPAYMENT.focus();
76 alert("Numbers only to be entered");
77 return;
78 }
79 if(x.LOANINTRATE.value == "") {
80 alert("You must enter an interest rate (ir).");
81 }
82 else {
83 ir = parseFloat(x.LOANINTRATE.value);
84 if (isNaN(ir))
85 {
86 x.LOANINTRATE.select();
87 x.LOANINTRATE.focus();
88 alert("Numbers only to be entered");
89 return;
90 }
91 ir = ((ir / lpp) / 100);
3e920f36 92
61240489 93 // test and compute all cases
3e920f36 94
61240489 95 if (v == "pv") {
96 if(ir == 0) {
97 pv = -(fv + (pmt * np));
98 }
99 else {
100 q1 = Math.pow(1 + ir,-np);
101 q2 = Math.pow(1 + ir,np);
102 pv = -(q1 * (fv * ir - pmt + q2 * pmt))/ir;
103 }
104 x.LOANAMOUNT.value = num_format(pv);
105 }
3e920f36 106
61240489 107 if (v == "np") {
108 if(ir == 0) {
109 if(pmt != 0) {
110 np = - (fv + pv)/pmt;
111 }
112 else {
113 alert("Divide by zero error.");
114 }
115 }
116 else {
117 np = Math.log((-fv * ir + pmt)/(pmt + ir * pv))/ Math.log(1 + ir);
118 }
119 if(np == 0) {
120 alert("Can\'t compute Number of Periods for the present values.");
121 }
122 else {
123 np = (np / lpp)
124 if (isNaN(np)) {
125 alert("The repayment amount is less than the interest. You must increase your repayments to pay off this loan!");
126 } else {
127 x.LOANTERM.value = num_format(np);
128 }
129 }
130 }
3e920f36 131
61240489 132 if (v == "pmt") {
133 if(ir == 0.0) {
134 if(np != 0) {
135 pmt = (fv + pv)/np;
136 }
137 else {
138 alert("Divide by zero error.");
139 }
140 }
141 else {
142 q = Math.pow(1 + ir,np);
143 pmt = ((ir * (fv + q * pv))/(-1 + q));
144 }
145 x.LOANREPAYMENT.value = num_format(pmt);
146 }
3e920f36 147
148
61240489 149 }
3e920f36 150} // function comp
948acd80 151//]]>
e777bc9a 152</script>
936af097 153<form method="post" id="vbankform" action="">
61240489 154 <table>
3e920f36 155 <tr>
61240489 156 <td colspan="2">'.get_string('amountofloan','block_loancalc').'</td>
3e920f36 157 </tr>
61240489 158 <tr>
e777bc9a 159 <td><input name="LOANAMOUNT" id="LOANAMOUNT" size="17" /></td>
0f13ec8f 160 <td><a href="JavaScript:comp(\'pv\');"><img src="'.$calc.'" alt="calculate" /></a></td>
61240489 161 </tr>
162 <tr>
163 <td colspan="2">'.get_string('repaymentamount','block_loancalc').'</td>
3e920f36 164 </tr>
61240489 165 <tr>
e777bc9a 166 <td><input name="LOANREPAYMENT" id="LOANREPAYMENT" size="17" /></td>
0f13ec8f 167 <td><a href="JavaScript:comp(\'pmt\');"><img src="'.$calc.'" alt="calculate" /></a></td>
61240489 168 </tr>
169 <tr>
170 <td colspan="2">'.get_string('loanterm','block_loancalc').'</td>
171 </tr>
172 <tr>
e777bc9a 173 <td><input name="LOANTERM" id="LOANTERM" size="17" /></td>
0f13ec8f 174 <td><a href="JavaScript:comp(\'np\');"><img src="'.$calc.'" alt="calculate" /></a></td>
4ca6cfbf 175 </tr>
61240489 176 <tr>
177 <td colspan="2">'.get_string('interestrate','block_loancalc').'</td>
178 </tr>
179 <tr>
e777bc9a 180 <td><input name="LOANINTRATE" id="LOANINTRATE" size="17" /></td>
61240489 181 <td></td>
4ca6cfbf 182 </tr>
61240489 183 <tr>
184 <td colspan="2">'.get_string('repaymentfreq','block_loancalc').'</td>
185 </tr>
e777bc9a 186 <tr>
61240489 187 <td>';
84c57908 188 $options[52] = get_string('weekly','block_loancalc');
189 $options[26] = get_string('fortnightly','block_loancalc');
190 $options[12] = get_string('monthly','block_loancalc');
d776d59e 191 $this->content->text .= html_writer::select($options,'LOANPAYPERIOD','12');
3e920f36 192 $this->content->text .= '</td>
61240489 193 <td></td>
194 </tr>
195 </table>
196 </form>';
3984a185 197 $this->content->footer = '';
3e920f36 198
199 return $this->content;
200
201 }
202}
4ca6cfbf 203