function ldc_parse_str(str){ 'use strict'; var i = 0; var search_object = {}; var search_array = str.replace('?', '').split('&'); for(i = 0; i < search_array.length; i ++){ search_object[search_array[i].split('=')[0]] = search_array[i].split('=')[1]; } return search_object; } function ldc_parse_url(url, component){ 'use strict'; var a = document.createElement('a'), components = {}, keys = ['protocol', 'hostname', 'port', 'pathname', 'search', 'hash']; if(url === ''){ a.href = jQuery(location).attr('href'); } else { a.href = url; } if(typeof component === 'undefined' || component === ''){ jQuery.map(keys, function(c){ components[c] = a[c]; }); return components; } else if(jQuery.inArray(component, keys) !== -1){ return a[component]; } else { return ''; } } function ldc_add_query_args(args, url){ 'use strict'; var a = document.createElement('a'), href = ''; if(url === ''){ a.href = jQuery(location).attr('href'); } else { a.href = url; } if(a.protocol){ href += a.protocol + '//'; } if(a.hostname){ href += a.hostname; } if(a.port){ href += ':' + a.port; } if(a.pathname){ if(a.pathname[0] !== '/'){ href += '/'; } href += a.pathname; } if(a.search){ var search = []; var search_object = ldc_parse_str(a.search); jQuery.each(search_object, function(k, v){ if(!(k in args)){ search.push(k + '=' + v); } }); if(search.length > 0){ href += '?' + search.join('&') + '&'; } else { href += '?'; } } else { href += '?'; } jQuery.each(args, function(k, v){ href += k + '=' + v; }); if(a.hash){ href += a.hash; } return href; }; function ldc_add_query_arg(key, value, url){ 'use strict'; var a = document.createElement('a'), href = ''; if(url === ''){ a.href = jQuery(location).attr('href'); } else { a.href = url; } if(a.protocol){ href += a.protocol + '//'; } if(a.hostname){ href += a.hostname; } if(a.port){ href += ':' + a.port; } if(a.pathname){ if(a.pathname[0] !== '/'){ href += '/'; } href += a.pathname; } if(a.search){ var search = []; var search_object = ldc_parse_str(a.search); jQuery.each(search_object, function(k, v){ if(k != key){ search.push(k + '=' + v); } }); if(search.length > 0){ href += '?' + search.join('&') + '&'; } else { href += '?'; } } else { href += '?'; } href += key + '=' + value; if(a.hash){ href += a.hash; } return href; }; var opencourses = {}; jQuery(function($){ $('.boton-certificacion').removeAttr('target').addClass('disabled'); $.getJSON('https://opencourses.ufm.edu/api/user/v1/accounts', function(data){ if($.isArray(data)){ opencourses.account = data[0]; if($.type(opencourses.account.username) !== 'undefined'){ $.getJSON('https://opencourses.ufm.edu/api/user/v1/preferences/' + opencourses.account.username, function(data){ opencourses.preferences = data; $.getJSON('https://opencourses.ufm.edu/api/enrollment/v1/enrollment', function(data){ opencourses.enrollments = data; opencourses.href = window.location.href; $.post('https://wp.ufm.edu/opencoursesdata/wp-json/opencourses/v1/opencoursesdata', { opencoursesdata: JSON.stringify(opencourses) }, function(data){ if(data.redirect){ $(location).attr('href', data.redirect); } if(data.auth2){ $('.boton-certificacion').removeAttr('target').removeClass('disabled'); $('body').on('click contextmenu', '.boton-certificacion', function(event){ event.preventDefault(); $(this).removeAttr('target'); $(this).removeClass('disabled'); current_search = ldc_parse_url($(this).attr('href'), 'search'); current_components = ldc_parse_str(current_search); current_url = ldc_add_query_args(current_components, data.auth2); $(location).attr('href', current_url); }); } }, 'json'); }); }); } } }); });