| 1 | 
           efrain | 
           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 | 
           /**
  | 
        
        
            | 
            | 
           17 | 
            * Our validator that splits the user's input then fires off to a webservice
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module     tool_moodlenet/validator
  | 
        
        
            | 
            | 
           20 | 
            * @copyright  2020 Mathew May <mathew.solutions>
  | 
        
        
            | 
            | 
           21 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           22 | 
            */
  | 
        
        
            | 
            | 
           23 | 
           define(['jquery', 'core/ajax', 'core/str', 'core/notification'], function($, Ajax, Str, Notification) {
  | 
        
        
            | 
            | 
           24 | 
               /**
  | 
        
        
            | 
            | 
           25 | 
                * Handle form validation
  | 
        
        
            | 
            | 
           26 | 
                *
  | 
        
        
            | 
            | 
           27 | 
                * @method validation
  | 
        
        
            | 
            | 
           28 | 
                * @param {HTMLElement} inputElement The element the user entered text into.
  | 
        
        
            | 
            | 
           29 | 
                * @return {Promise} Was the users' entry a valid profile URL?
  | 
        
        
            | 
            | 
           30 | 
                */
  | 
        
        
            | 
            | 
           31 | 
               var validation = function validation(inputElement) {
  | 
        
        
            | 
            | 
           32 | 
                   var inputValue = inputElement.value;
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
                   // They didn't submit anything or they gave us a simple string that we can't do anything with.
  | 
        
        
            | 
            | 
           35 | 
                   if (inputValue === "" || !inputValue.includes("@")) {
  | 
        
        
            | 
            | 
           36 | 
                       // Create a promise and immediately reject it.
  | 
        
        
            | 
            | 
           37 | 
                       $.when(Str.get_string('profilevalidationerror', 'tool_moodlenet')).then(function(strings) {
  | 
        
        
            | 
            | 
           38 | 
                           return Promise.reject().catch(function() {
  | 
        
        
            | 
            | 
           39 | 
                               return {result: false, message: strings[0]};
  | 
        
        
            | 
            | 
           40 | 
                           });
  | 
        
        
            | 
            | 
           41 | 
                       }).fail(Notification.exception);
  | 
        
        
            | 
            | 
           42 | 
                   }
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
                   return Ajax.call([{
  | 
        
        
            | 
            | 
           45 | 
                       methodname: 'tool_moodlenet_verify_webfinger',
  | 
        
        
            | 
            | 
           46 | 
                       args: {
  | 
        
        
            | 
            | 
           47 | 
                           profileurl: inputValue,
  | 
        
        
            | 
            | 
           48 | 
                           course: inputElement.dataset.courseid,
  | 
        
        
           | 1441 | 
           ariadna | 
           49 | 
                           section: inputElement.dataset.sectionnum
  | 
        
        
           | 1 | 
           efrain | 
           50 | 
                       }
  | 
        
        
            | 
            | 
           51 | 
                   }])[0].then(function(result) {
  | 
        
        
            | 
            | 
           52 | 
                       return result;
  | 
        
        
            | 
            | 
           53 | 
                   }).catch();
  | 
        
        
            | 
            | 
           54 | 
               };
  | 
        
        
            | 
            | 
           55 | 
               return {
  | 
        
        
            | 
            | 
           56 | 
                   validation: validation,
  | 
        
        
            | 
            | 
           57 | 
               };
  | 
        
        
            | 
            | 
           58 | 
           });
  |