observers.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. // This file is part of CorfoWS for Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package local_corfows
  18. * @author Hackware Human <human@hackware.cl>
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. */
  21. namespace local_corfows;
  22. defined('MOODLE_INTERNAL') || die();
  23. use \mod_customcert\certificate;
  24. class observers {
  25. public static function customcert($event) {
  26. // Only when downloading own certificate.
  27. // Other sanity checks are performed previous to event trigger.
  28. $downloadown = optional_param('downloadown', false, PARAM_BOOL);
  29. if (!$downloadown) {
  30. return;
  31. }
  32. global $DB, $USER;
  33. $customcertid = $event->get_data()['objectid'];
  34. $issue = static::get_issue($customcertid, $USER->id);
  35. if (!$issue) {
  36. certificate::issue_certificate($customcertid, $USER->id);
  37. $issue = static::get_issue($customcertid, $USER->id);
  38. $validation = false;
  39. } else {
  40. $validation = $DB->record_exists(
  41. 'local_corfows_validation',
  42. array('issue_id' => $issue->id)
  43. );
  44. }
  45. // If validation exists there's nothing else to do.
  46. if (!$validation) {
  47. $response = http_request::validation($issue->code);
  48. if ($response !== false) {
  49. if ((
  50. property_exists($response, 'Success') && $response->Success
  51. ) || (
  52. property_exists($response, 'Message')
  53. && strpos($response->Message, 'ya tiene cargada') !== false
  54. )) {
  55. // Save validation.
  56. $DB->insert_record(
  57. 'local_corfows_validation',
  58. (object) array('issue_id' => $issue->id)
  59. );
  60. return;
  61. }
  62. }
  63. if (property_exists($response, 'Message')) {
  64. $errormsg = $response->Message;
  65. } elseif (property_exists($response, 'error_description')) {
  66. $errormsg = $response->error_description;
  67. } else {
  68. $errormsg = json_encode($response);
  69. }
  70. \debugging("CORFO Error: $errormsg", DEBUG_DEVELOPER);
  71. }
  72. }
  73. protected static function get_issue($customcertid, $userid) {
  74. global $DB;
  75. return $DB->get_record(
  76. 'customcert_issues',
  77. array('userid' => $userid, 'customcertid' => $customcertid)
  78. );
  79. }
  80. }