admin/tool/moodlenet/amd/src/validator.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Our validator that splits the user's input then fires off to a webservice
  17. *
  18. * @module tool_moodlenet/validator
  19. * @copyright 2020 Mathew May <mathew.solutions>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery', 'core/ajax', 'core/str', 'core/notification'], function($, Ajax, Str, Notification) {
  23. /**
  24. * Handle form validation
  25. *
  26. * @method validation
  27. * @param {HTMLElement} inputElement The element the user entered text into.
  28. * @return {Promise} Was the users' entry a valid profile URL?
  29. */
  30. var validation = function validation(inputElement) {
  31. var inputValue = inputElement.value;
  32. // They didn't submit anything or they gave us a simple string that we can't do anything with.
  33. if (inputValue === "" || !inputValue.includes("@")) {
  34. // Create a promise and immediately reject it.
  35. $.when(Str.get_string('profilevalidationerror', 'tool_moodlenet')).then(function(strings) {
  36. return Promise.reject().catch(function() {
  37. return {result: false, message: strings[0]};
  38. });
  39. }).fail(Notification.exception);
  40. }
  41. return Ajax.call([{
  42. methodname: 'tool_moodlenet_verify_webfinger',
  43. args: {
  44. profileurl: inputValue,
  45. course: inputElement.dataset.courseid,
  46. section: inputElement.dataset.sectionnum
  47. }
  48. }])[0].then(function(result) {
  49. return result;
  50. }).catch();
  51. };
  52. return {
  53. validation: validation,
  54. };
  55. });