PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/enrol/mpay/forms.php

https://bitbucket.org/sebastianberm/moodle-payments-moodle2
PHP | 323 lines | 249 code | 52 blank | 22 comment | 18 complexity | 33ae274e9bd270476a6c078810713c82 MD5 | raw file
  1. <?php
  2. /**
  3. * @desc MoodlePayments - Pay for a course enrolment
  4. * @since 1.8
  5. * @version 1.3
  6. * @author Luuk Verhoeven [luuk@sebsoft.nl] / Sebastian Berm [sebastian@sebsoft.nl]
  7. * @package enrol/mpay
  8. * @copyright Sebsoft.nl
  9. */
  10. defined('MOODLE_INTERNAL') || die();
  11. require_once($CFG->libdir . '/formslib.php');
  12. //Settings for course enrolment
  13. class enrol_mpay_edit_form extends moodleform
  14. {
  15. function definition()
  16. {
  17. global $CFG;
  18. $mform = $this->_form;
  19. list($instance, $plugin, $context) = $this->_customdata;
  20. $mform->addElement('header', 'header', get_string('pluginname', 'enrol_mpay'));
  21. $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
  22. $mform->setType('name', PARAM_RAW_TRIMMED);
  23. $mform->setDefault('name',get_string('pluginname', 'enrol_mpay'));
  24. $mform->addElement('duration', 'enrolperiod', get_string('defaultperiod', 'enrol_manual'), array('optional' => true, 'defaultunit' => 86400));
  25. $mform->setType('duration',PARAM_RAW_TRIMMED);
  26. $mform->setDefault('enrolperiod', $plugin->get_config('enrolperiod'));
  27. if ($instance->id)
  28. {
  29. $roles = get_default_enrol_roles($context, $instance->roleid);
  30. }
  31. else
  32. {
  33. $roles = get_default_enrol_roles($context, $plugin->get_config('roleid'));
  34. }
  35. $mform->addElement('select', 'roleid', get_string('defaultrole', 'role'), $roles);
  36. $mform->setDefault('roleid', $plugin->get_config('roleid'));
  37. $mform->setType('roleid',PARAM_INT);
  38. /* CURRENCIE */
  39. $arrayCurrencies = get_string_manager()->get_list_of_currencies();
  40. $default = empty($CFG->enrol_mpay_currency) ? 'EUR' : $CFG->enrol_mpay_currency;
  41. $mform->addElement('hidden', 'currency', $default);
  42. $mform->setType('currency',PARAM_RAW_TRIMMED);
  43. $currency = $arrayCurrencies[$default];
  44. $mform->addElement('html', '<div class="fitem"><div class="fitemtitle"></div><div class="felement ftext">' . $currency . '</div></div>');
  45. /* AMOUT SETTINGS */
  46. $mform->addElement('text', 'cost', get_string('amount', 'enrol_mpay') . " ($default)");
  47. $default = empty($CFG->enrol_mpay_amount) ? 10.99 : $CFG->enrol_mpay_amount;
  48. $mform->setDefault('cost', $default);
  49. $mform->setType('cost', PARAM_FLOAT);
  50. $mform->addElement('hidden', 'id');
  51. $mform->setType('id',PARAM_INT);
  52. $mform->addElement('hidden', 'courseid');
  53. $mform->setType('courseid',PARAM_INT);
  54. $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));
  55. $this->set_data($instance);
  56. }
  57. }
  58. //User pay values sending to pay.nl
  59. class payForm extends moodleform
  60. {
  61. function definition()
  62. {
  63. global $USER, $CFG, $PAGE, $DB, $SITE;
  64. //get amount
  65. $mform = & $this->_form;
  66. $id = $this->_customdata['id'];
  67. $COURSE = $this->_customdata['course'];
  68. $enrolSettings = Mpay::getEnrolSettings($COURSE->id);
  69. //var_dump($enrolSettings);
  70. if ((float) $enrolSettings->cost < 0)
  71. {
  72. $cost = (float) $CFG->mpay_enrol_amount;
  73. }
  74. else
  75. {
  76. $cost = (float) $enrolSettings->cost;
  77. }
  78. $mform->addElement('header', 'heading', get_string('pluginname', 'enrol_mpay'));
  79. // Calculate displaycost
  80. $lang = current_language();
  81. $displaycost = "";
  82. switch ($lang)
  83. {
  84. case 'en': $displaycost = number_format($cost, 2, '.', ',');
  85. break;
  86. case 'nl': $displaycost = number_format($cost, 2, ',', ' ');
  87. break;
  88. case 'be': $displaycost = number_format($cost, 2, ',', ' ');
  89. break;
  90. default : $displaycost = number_format($cost, 2, '.', ',');
  91. break;
  92. }
  93. //TODO this is a value thats not used
  94. $prencent = !empty($CFG->enrol_mpay_coupon_percent) ? $CFG->enrol_mpay_coupon_percent : '';
  95. $paymentValidationString = base64_encode(str_rot13($enrolSettings->password)) . "-" .
  96. base64_encode($COURSE->fullname) . "-" .
  97. base64_encode($cost) . "-" .
  98. base64_encode($prencent) . "-" .
  99. base64_encode(str_rot13($CFG->enrol_mpay_websitecode));
  100. $vs = $paymentValidationString . "-" . md5($paymentValidationString); // Payment validation
  101. $mform->addElement('hidden', 'id', $id);
  102. $mform->setType('id',PARAM_INT);
  103. $mform->addElement('hidden', 'cmd', "initpayment");
  104. $mform->setType('cmd',PARAM_ALPHAEXT);
  105. $mform->addElement('hidden', 'charset', "utf-8");
  106. $mform->setType('charset',PARAM_RAW_TRIMMED);
  107. $mform->addElement('hidden', 'websitecode', $CFG->enrol_mpay_websitecode);
  108. $mform->setType('websitecode',PARAM_RAW_TRIMMED);
  109. $mform->addElement('hidden', 'course_fullname', $COURSE->fullname);
  110. $mform->setType('course_fullname',PARAM_RAW_TRIMMED);
  111. $mform->addElement('hidden', 'course_shortname', $COURSE->shortname);
  112. $mform->setType('course_shortname',PARAM_RAW_TRIMMED);
  113. $mform->addElement('hidden', 'studentname', fullname($USER));
  114. $mform->setType('studentname',PARAM_RAW_TRIMMED);
  115. $mform->addElement('hidden', 'user_id', $USER->id);
  116. $mform->setType('user_id',PARAM_INT);
  117. $mform->addElement('hidden', 'course_id', $COURSE->id);
  118. $mform->setType('course_id',PARAM_INT);
  119. $mform->addElement('hidden', 'currency', $enrolSettings->currency);
  120. $mform->setType('currency',PARAM_RAW_TRIMMED);
  121. $mform->addElement('hidden', 'amount', $cost);
  122. $mform->setType('amount',PARAM_RAW_TRIMMED);
  123. $mform->addElement('hidden', 'return_notify', "$CFG->wwwroot/enrol/mpay/mpsup.php");
  124. $mform->setType('return_notify',PARAM_RAW_TRIMMED);
  125. $mform->addElement('hidden', 'return_url', "$CFG->wwwroot/enrol/mpay/return.php?id=$COURSE->id");
  126. $mform->setType('return_url',PARAM_RAW_TRIMMED);
  127. $mform->addElement('hidden', 'return_cancel', $CFG->wwwroot);
  128. $mform->setType('return_cancel',PARAM_RAW_TRIMMED);
  129. $mform->addElement('hidden', 'continuemessage', get_string('continuetocourse', 'enrol_mpay'));
  130. $mform->setType('continuemessage',PARAM_RAW_TRIMMED);
  131. $mform->addElement('hidden', 'first_name', $USER->firstname);
  132. $mform->setType('first_name',PARAM_RAW_TRIMMED);
  133. $mform->addElement('hidden', 'last_name', $USER->lastname);
  134. $mform->setType('last_name',PARAM_RAW_TRIMMED);
  135. $mform->addElement('hidden', 'address', $USER->address);
  136. $mform->setType('address',PARAM_RAW_TRIMMED);
  137. $mform->addElement('hidden', 'city', $USER->city);
  138. $mform->setType('city',PARAM_RAW_TRIMMED);
  139. $mform->addElement('hidden', 'email', $USER->email);
  140. $mform->setType('email',PARAM_EMAIL);
  141. $country = $USER->country;
  142. if (strlen($country) == 0)
  143. {
  144. $country = "NL";
  145. }
  146. $mform->addElement('hidden', 'country', $country);
  147. $mform->setType('country',PARAM_RAW_TRIMMED);
  148. $mform->addElement('hidden', 'studentidnumber', $USER->idnumber);
  149. $mform->setType('studentidnumber',PARAM_ALPHANUMEXT);
  150. $mform->addElement('hidden', 'clientversion', '2011081002');
  151. $mform->setType('clientversion',PARAM_INT);
  152. $mform->addElement('hidden', 'moodleversion', $CFG->release);
  153. $mform->setType('moodleversion',PARAM_RAW_TRIMMED);
  154. $mform->addElement('hidden', 'language', current_language());
  155. $mform->setType('language',PARAM_RAW_TRIMMED);
  156. $mform->addElement('hidden', 'site_fullname', $SITE->fullname);
  157. $mform->setType('site_fullname',PARAM_RAW_TRIMMED);
  158. $mform->addElement('hidden', 'site_shortname', $SITE->shortname);
  159. $mform->setType('site_shortname',PARAM_RAW_TRIMMED);
  160. $mform->addElement('hidden', 'vs', $vs, array('id' => 'vsString'));
  161. $mform->setType('vs',PARAM_RAW);
  162. if (isset($CFG->enrol_mpay_enable_couponcode) && $CFG->enrol_mpay_enable_couponcode == 1)
  163. {
  164. //global is active now check if there is a coupon code avialable
  165. $result = Mpay::getAvailableCoupon($COURSE->id); //Workarround popup not working if its in formtags
  166. if ($result)
  167. {
  168. $mform->addElement('hidden', 'couponcode', '', array('id' => 'couponK'));
  169. $mform->setType('couponcode',PARAM_RAW);
  170. Mpay::setPopup($COURSE->id);
  171. }
  172. }
  173. //check if sandbox is on
  174. if (isset($CFG->enrol_mpay_sandbox) && $CFG->enrol_mpay_sandbox == 1)
  175. {
  176. $mform->addElement('hidden', 'sandbox', 1);
  177. $mform->setType('sandbox',PARAM_INT);
  178. }
  179. $list = Mpay::getmPayPaymentMethodList();
  180. if (count($list) == 0)
  181. {
  182. # Just show the default method
  183. $record = $DB->get_record('enrol_mpay_methods', array('systemname' => $CFG->enrol_mpay_default_payment_methode));
  184. Mpay::show_payment_method($record, true, false, $extra, $realtext);
  185. }
  186. else
  187. {
  188. foreach ($list as $method)
  189. {
  190. Mpay::show_payment_method($method, false, true, $extra, $realtext);
  191. }
  192. }
  193. $currency = Currencies::getCurrency($enrolSettings->currency);
  194. if ($enrolSettings->currency == $currency)
  195. {
  196. //shit we dont find symbol
  197. $currency = '<small>' . $currency . '</small>';
  198. }
  199. //var_dump(get_course_cost($enrolSettings));
  200. $mform->addElement('html', '<div id="mpayfinder"></div><div><h4>' . get_string('price-to-pay', 'enrol_mpay') . '<font style="text-decoration:underline;"> ' . $currency . ' ' . $displaycost . '</font><span style="font-size:13px;color:red" rel="' . $cost . '" id="discount"></span></h4>
  201. <div>' . $extra . '</div><div id="paymentselect">' . $realtext . '</div>');
  202. $mform->addElement('html', Mpay::displayPopup());
  203. $mform->addElement('submit', 'mpay_submit', get_string('startpaymentbutton', 'enrol_mpay'));
  204. }
  205. }
  206. class couponedit_form extends moodleform
  207. {
  208. function definition()
  209. {
  210. $mform = & $this->_form;
  211. $mform->addElement('hidden', 'id');
  212. $courselist = get_courses();
  213. $courses = array();
  214. $courses['0'] = get_string('entiresite', 'enrol_mpay');
  215. foreach ($courselist as $course)
  216. {
  217. $courses[$course->id] = $course->fullname;
  218. }
  219. $mform->addElement('select', 'courseid', get_string('course'), $courses);
  220. $mform->addElement('hidden', 'nooutput', 1);
  221. $mform->addElement('text', 'code', get_string('couponcode', 'enrol_mpay'), 'maxlength="15" size="15"');
  222. $mform->addRule('code', get_string('couponcodemissing', 'enrol_mpay'), 'required', null, 'client');
  223. $mform->setType('code', PARAM_TEXT);
  224. $mform->addElement('date_selector', 'validfrom', get_string('validfromform', 'enrol_mpay'));
  225. $mform->addRule('validfrom', get_string('validfrommissing', 'enrol_mpay'), 'required', null, 'client');
  226. $mform->addElement('date_selector', 'validto', get_string('validtoform', 'enrol_mpay'));
  227. $mform->addRule('validto', get_string('validtomissing', 'enrol_mpay'), 'required', null, 'client');
  228. $mform->addElement('text', 'percentage', get_string('percentage', 'enrol_mpay'));
  229. $mform->addRule('percentage', get_string('percentagemissing', 'enrol_mpay'), 'required', null, 'client');
  230. // Calender
  231. $this->add_action_buttons();
  232. }
  233. function validation($data, $files)
  234. {
  235. $errors = parent::validation($data, $files);
  236. if ($data['validfrom'] > $data['validto'])
  237. {
  238. $errors['validfrom'] = get_string('validfromhigherthanvalidto', 'enrol_mpay');
  239. }
  240. return $errors;
  241. }
  242. }
  243. class vatedit_form extends moodleform
  244. {
  245. function definition()
  246. {
  247. $mform = & $this->_form;
  248. $mform->addElement('hidden', 'id');
  249. $mform->setType('id', PARAM_INT);
  250. $courselist = get_courses();
  251. $courses = array();
  252. $courses['0'] = get_string('entiresite', 'enrol_mpay');
  253. foreach ($courselist as $course)
  254. {
  255. $courses[$course->id] = $course->fullname;
  256. }
  257. $mform->addElement('select', 'course', get_string('course'), $courses);
  258. $mform->setType('course', PARAM_INT);
  259. $mform->addElement('hidden', 'nooutput', 1);
  260. $mform->setType('nooutput', PARAM_INT);
  261. $mform->addElement('text', 'percentage', get_string('percentage', 'enrol_mpay'));
  262. $mform->setType('percentage', PARAM_FLOAT);
  263. $mform->addRule('percentage', get_string('percentagemissing', 'enrol_mpay'), 'required', null, 'client');
  264. $this->add_action_buttons();
  265. }
  266. function validation($data, $files)
  267. {
  268. $errors = parent::validation($data, $files);
  269. return $errors;
  270. }
  271. }