643 lines
20 KiB
HTML
643 lines
20 KiB
HTML
<section class="py-md-4 py-2">
|
|
<div class="container-fluid">
|
|
<div class="row">
|
|
<div class="col-12">
|
|
<h1 class="text-secondary font-weight-bolder">Gamification</h1>
|
|
<p>Sistema de gamification, se pueden crear insignias por logros en los recursos o por categorias.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<section class="py-md-4 py-2 controller">
|
|
<div class="container-fluid">
|
|
<div class="row align-items-center align-self-center">
|
|
<div class="col-12">
|
|
<table id="datatable" class="display table table-striped table-bordered" cellspacing="0" width="100%">
|
|
<thead>
|
|
<tr></tr>
|
|
</thead>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<section class="py-md-4 py-2 editor" style="display: none;">
|
|
<div class="container-fluid">
|
|
<div class="row align-items-center justify-content-start">
|
|
<div class="col-12 text-left my-4">
|
|
<h3 class="title-cat edit" style="display: none;">Editar Insignia</h3>
|
|
<h3 class="title-cat crear" style="display: none;">Crear Insignia</h3>
|
|
</div>
|
|
<div class="col-12 col-md-10">
|
|
<form class="formcontroller">
|
|
<input type="hidden" name="idobjetivo" id="idobjetivo" value="">
|
|
<input type="hidden" name="rowid" id="rowid" value="">
|
|
<input type="hidden" name="urlimginsignia" id="urlimginsignia" value="">
|
|
<div class="form-group row">
|
|
<label for="tipo" class="col-md-2 col-12 col-form-label">Tipo</label>
|
|
<div class="col-8">
|
|
<select id="tipo" name="tipo" class="col-12 selectpicker" data-live-search="true" required="required">
|
|
<option value="categoria" selected>Categoría</option>
|
|
<option value="scormpkg">Recurso</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="form-group row">
|
|
<label for="catsuperior" class="col-md-2 col-12 col-form-label">Objetivo</label>
|
|
<div class="col-8">
|
|
<div class="wcats"><select id="catsuperior" name="catsuperior" class="col-12 selectpicker" data-live-search="true" required="required"></select></div>
|
|
<div class="wrecurso"><select id="recurso" name="recurso" class="col-12 selectpicker" data-live-search="true" required="required"></select></div>
|
|
</div>
|
|
</div>
|
|
<div class="form-group row">
|
|
<label class="col-md-2 col-12 col-form-label" for="nombre">Nombre</label>
|
|
<div class="col-8">
|
|
<input id="nombre" name="nombre" placeholder="Nombre" type="text" class="form-control" required="required">
|
|
</div>
|
|
</div>
|
|
<div class="form-group row">
|
|
<label for="desc" class="col-md-2 col-12 col-form-label">Descripción</label>
|
|
<div class="col-8">
|
|
<textarea id="desc" name="desc" cols="40" rows="5" class="form-control"></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="form-group row">
|
|
<label for="habilitado" class="col-md-2 col-12 col-form-label">Habilitado</label>
|
|
<div class="col-8">
|
|
<select id="habilitado" name="habilitado" class="custom-select" required="required">
|
|
<option value="1" selected>SI</option>
|
|
<option value="0">No</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="form-group row">
|
|
<label class="col-md-2 col-12 col-form-label">Imagen</label>
|
|
<div class="col-8">
|
|
<div class="row">
|
|
<div class="col-12 col-md-6 py-2 py-md-0">
|
|
<div class="UppyDragDrop"></div>
|
|
<div class="w-100 my-3">
|
|
<div class="for-ProgressBar"></div>
|
|
</div>
|
|
</div>
|
|
<div class="col-12 col-md-6 py-2 py-md-0 preview" style="display: none;">
|
|
<div class="card h-100">
|
|
<div class="card-body text-center">
|
|
<div class="ThumbnailGenerator"></div>
|
|
<div class="uploaded-files text-secondary">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="form-group row justify-content-center">
|
|
<div class="col-12 col-md-6 text-right">
|
|
<div class="btn btn-warning btn_cancelar">Cancelar</div>
|
|
<button name="Enviar" type="submit" value="Enviar" class="btn btn-primary">Enviar</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<script type="text/javascript">
|
|
$(".layer-load").show();
|
|
var table;
|
|
jQuery(document).ready(function ($) {
|
|
function parseJSON(resp) {
|
|
if (resp == null) return null;
|
|
if (typeof resp === 'object') return resp;
|
|
try { return JSON.parse(resp); }
|
|
catch (e) { console.error('Respuesta no-JSON del servidor:', String(resp).substring(0, 300)); return null; }
|
|
}
|
|
|
|
|
|
/**********************************************************************/
|
|
/****************** UPLOAD ********************************************/
|
|
/**********************************************************************/
|
|
var uppy = Uppy.Core({
|
|
debug: false,
|
|
autoProceed: true,
|
|
allowMultipleUploads: true,
|
|
locale: Uppy.locales.Spanish,
|
|
restrictions: {
|
|
allowedFileTypes: ['image/*']
|
|
}
|
|
});
|
|
uppy.use(Uppy.DragDrop, {
|
|
target: '.UppyDragDrop',
|
|
locale: {
|
|
strings: {
|
|
browse: 'Buscar',
|
|
dropHereOr: "Arrastra el archivo aqu\xed o %{browse}",
|
|
dropHint: "Arrastra el archivo aqu\xed",
|
|
dropPaste: "Arrastra el archivo aqu\xed, pegar o %{browse}",
|
|
dropPasteImport: "Arrastra el archivo aqu\xed, pegar, %{browse} o importar desde",
|
|
}
|
|
}
|
|
}).use(Uppy.XHRUpload, {
|
|
headers: { 'X-CSRF-Token': window.CSRF_TOKEN },
|
|
endpoint: "gamification_upload.php",
|
|
formData: true,
|
|
method: 'post',
|
|
fieldName: 'files[]'
|
|
}).use(Uppy.ProgressBar, { target: '.for-ProgressBar', hideAfterFinish: false }).on('upload-success', function (file, response) { onUploadSuccess(file, response) });
|
|
|
|
function onUploadSuccess(file, response) {
|
|
if (response.uploadURL) {
|
|
var url = response.uploadURL;
|
|
$("#urlimginsignia").val(url);
|
|
var fileName = file.name;
|
|
$(".preview").show();
|
|
$(".uploaded-files").show().addClass('newimg');
|
|
$(".uploaded-files").html("");
|
|
$(".uploaded-files").html('<a class="btn-link" href="' + response.uploadURL + '" target="_blank">' +
|
|
'<img class="img-fluid" src="' + response.uploadURL + '"><br>' + fileName + '</a>');
|
|
} else {
|
|
Swal.fire(
|
|
'',
|
|
response.body.txt,
|
|
'error'
|
|
)
|
|
uppy.reset();
|
|
}
|
|
}
|
|
/**********************************************************************/
|
|
/****************** DATATABLE *****************************************/
|
|
/**********************************************************************/
|
|
function getData() {
|
|
listadoDeCategorias();
|
|
|
|
$("#datatable").html('<thead><tr></tr></thead>');
|
|
$.ajax({
|
|
url: 'gamification.php'
|
|
}).done(function (data) {
|
|
var _data = JSON.parse(data);
|
|
var tableName = "#datatable";
|
|
var str;
|
|
$.each(_data.columns, function (k, colObj) {
|
|
str = '<th>' + colObj + '</th>';
|
|
$(str).appendTo(tableName + '>thead>tr');
|
|
});
|
|
|
|
table = $('#datatable').DataTable({
|
|
dom: "Bfrtip",
|
|
responsive: true,
|
|
"drawCallback": function (settings) {
|
|
$("#btn_edit, #btn_habilitar").addClass('disabled');
|
|
addEventsCheck();
|
|
},
|
|
buttons: [{
|
|
extend: 'excel',
|
|
text: 'Exportar a Excel'
|
|
},
|
|
{
|
|
text: 'Editar',
|
|
action: function (e, dt, node, config) {
|
|
editar(e, dt, node, config);
|
|
},
|
|
attr: {
|
|
id: 'btn_edit'
|
|
}
|
|
},
|
|
{
|
|
text: 'Alternar habilitar',
|
|
action: function (e, dt, node, config) {
|
|
deshabilitar(e, dt, node, config)
|
|
},
|
|
attr: {
|
|
id: 'btn_habilitar'
|
|
}
|
|
},
|
|
{
|
|
text: 'Crear',
|
|
action: function (e, dt, node, config) {
|
|
crear();
|
|
},
|
|
attr: {
|
|
id: 'btn_crear'
|
|
}
|
|
}
|
|
|
|
],
|
|
'columnDefs': [{
|
|
'targets': 0,
|
|
'checkboxes': { 'selectRow': true }
|
|
}],
|
|
|
|
'order': [
|
|
[1, 'asc']
|
|
],
|
|
data: _data.datos,
|
|
"language": {
|
|
"url": "../json/Spanish.json"
|
|
}
|
|
|
|
});
|
|
|
|
$(".layer-load").hide();
|
|
})
|
|
.fail(function () {
|
|
|
|
})
|
|
.always(function () {
|
|
table.columns.adjust().draw();
|
|
$(window).resize(function (event) {
|
|
table.columns.adjust().draw();
|
|
});
|
|
});
|
|
}
|
|
|
|
function reloadData() {
|
|
//listadoDeCategorias();
|
|
$.ajax({
|
|
url: 'gamification.php'
|
|
})
|
|
.done(function (data) {
|
|
var NewlyCreatedData = JSON.parse(data);
|
|
table.clear();
|
|
table.rows.add(NewlyCreatedData.datos); // Add new data
|
|
table.columns.adjust().draw(); // Redraw the DataTable
|
|
table.column(0).checkboxes.deselectAll();
|
|
})
|
|
.fail(function () {
|
|
//console.log("error");
|
|
})
|
|
.always(function () {
|
|
//console.log("complete");
|
|
});
|
|
}
|
|
|
|
function addEventsCheck() {
|
|
$("#datatable_wrapper input[type='checkbox']").on('change', function (e) {
|
|
setTimeout(function (){
|
|
console.log($("#datatable_wrapper input[type='checkbox']:checked").length);
|
|
if (table.column(0).checkboxes.selected().length == 0) {
|
|
$("#btn_edit, #btn_habilitar").addClass('disabled');
|
|
} else if (table.column(0).checkboxes.selected().length == 1) {
|
|
$("#btn_edit, #btn_habilitar").removeClass('disabled');
|
|
} else if (table.column(0).checkboxes.selected().length > 1) {
|
|
$("#btn_edit").addClass('disabled');
|
|
$("#btn_habilitar").removeClass('disabled');
|
|
}
|
|
}, 150);
|
|
});
|
|
}
|
|
|
|
|
|
/**********************************************************************/
|
|
/****************** Editar, Deshabilitar, Crear ***********************/
|
|
/**********************************************************************/
|
|
function initTipo() {
|
|
|
|
$("#tipo").selectpicker('render');
|
|
$("#tipo").change(function (event) {
|
|
if ($("#catsuperior").find('option').length == 0 && $("#recurso").find('option').length == 0) {
|
|
Swal.fire({
|
|
html: "Sin categorias y sin recursos",
|
|
icon: "error",
|
|
onBeforeOpen: function () {},
|
|
onOpen: function () {},
|
|
onRender: function () {},
|
|
onClose: function () {
|
|
$(".btn_cancelar").click();
|
|
},
|
|
onAfterClose: function () {}
|
|
});
|
|
}
|
|
if ($(this).val() == "categoria") {
|
|
$(".wrecurso").hide();
|
|
$(".wcats").show();
|
|
if ($("#catsuperior").find('option').length == 0) {
|
|
Swal.fire({
|
|
html: "Sin categorias",
|
|
icon: "error",
|
|
onBeforeOpen: function () {},
|
|
onOpen: function () {},
|
|
onRender: function () {},
|
|
onClose: function () {
|
|
$("#tipo").val('categoria');
|
|
$("#tipo").selectpicker('render');
|
|
$("#tipo").trigger('change');
|
|
|
|
},
|
|
onAfterClose: function () {}
|
|
});
|
|
}
|
|
} else {
|
|
$(".wrecurso").show();
|
|
$(".wcats").hide();
|
|
if ($("#recurso").find('option').length == 0) {
|
|
Swal.fire({
|
|
html: "Sin recursos",
|
|
icon: "error",
|
|
onBeforeOpen: function () {},
|
|
onOpen: function () {},
|
|
onRender: function () {},
|
|
onClose: function () {
|
|
$("#tipo").val('categoria');
|
|
$("#tipo").selectpicker('render');
|
|
$("#tipo").trigger('change');
|
|
|
|
},
|
|
onAfterClose: function () {}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
function listadoDeRecursos() {
|
|
$(".layer-load").show();
|
|
$.ajax({
|
|
url: 'recursos_list.php',
|
|
type: 'POST'
|
|
})
|
|
.done(function (response) {
|
|
$(".layer-load").hide();
|
|
var _data = parseJSON(response);
|
|
if (_data.code == "1") {
|
|
$("#recurso").html("");
|
|
var _padres = _.groupBy(_data.datos, function (n) {
|
|
return n.catname;
|
|
});
|
|
$.each(_padres, function (index, val) {
|
|
$("#recurso").append('<optgroup label="' + index + '"></optgroup>');
|
|
$.each(val, function (indexs, val2) {
|
|
$("[label='" + index + "']").append('<option value="' + val2.id + '">' + val2.nombre + '</option>');
|
|
});
|
|
});
|
|
$('#recurso').selectpicker('render');
|
|
|
|
}
|
|
})
|
|
.fail(function () {
|
|
|
|
})
|
|
.always(function () {
|
|
initTipo();
|
|
});
|
|
|
|
}
|
|
|
|
function listadoDeCategorias() {
|
|
$(".layer-load").show();
|
|
$.ajax({
|
|
url: 'categorias_list.php',
|
|
type: 'POST'
|
|
})
|
|
.done(function (response) {
|
|
var _data = parseJSON(response);
|
|
if (_data.code == "1") {
|
|
$("#catsuperior").html("");
|
|
var _padres = _.filter(_data.datos, function (n) {
|
|
return n.padre == 0;
|
|
});
|
|
var _datos = _.groupBy(_data.datos, function (n) {
|
|
return n.padre;
|
|
});
|
|
$.each(_padres, function (index, val1) {
|
|
$("#catsuperior").append('<optgroup label="' + val1.nombre + '"></optgroup>');
|
|
$.each(_datos, function (index, cats) {
|
|
$.each(cats, function (index, val) {
|
|
if (val.padre == val1.id || val.nombre == val1.nombre) {
|
|
if (val.nombre == val1.nombre) {
|
|
//$("[label='" + val1.nombre + "']").append('<option class="font-weight-bold" value="' + val.id + '">' + val.nombre + '</option>');
|
|
} else {
|
|
$("[label='" + val1.nombre + "']").append('<option value="' + val.id + '">' + val.nombre + '</option>');
|
|
}
|
|
}
|
|
});
|
|
});
|
|
});
|
|
$('#catsuperior').selectpicker('render');
|
|
} else {
|
|
$(".controller").html('<h1>No existen categorias</h1>');
|
|
}
|
|
|
|
$(".layer-load").hide();
|
|
|
|
})
|
|
.fail(function () {
|
|
|
|
})
|
|
.always(function () {
|
|
listadoDeRecursos();
|
|
});
|
|
}
|
|
|
|
function editar(e, dt, node, config) {
|
|
$(".uploaded-files").removeClass("newimg");
|
|
var $table = table.table().node();
|
|
var $chkbox_checked = $('tbody input[type="checkbox"]:checked', $table);
|
|
$($chkbox_checked).closest('tr')
|
|
var data = table.row($($chkbox_checked).closest('tr')).data();
|
|
$("#rowid").val(data[0]);
|
|
$("#idobjetivo").val(data[1]);
|
|
if (data[3] == "Categoría") {
|
|
$(".wcats").show();
|
|
$(".wrecurso").hide();
|
|
$("#tipo").val('categoria');
|
|
$("#catsuperior").find('option').each(function (index, el) {
|
|
if ($(this).text() == data[2]) {
|
|
$("#catsuperior").val($(this).attr('value'));
|
|
$('#catsuperior').selectpicker('render');
|
|
}
|
|
});
|
|
} else {
|
|
$(".wcats").show();
|
|
$(".wrecurso").hide();
|
|
$("#tipo").val('scormpkg');
|
|
$("#recurso").find('option').each(function (index, el) {
|
|
if ($(this).text() == data[2]) {
|
|
$("#recurso").val($(this).attr('value'));
|
|
$('#recurso').selectpicker('render');
|
|
}
|
|
});
|
|
}
|
|
$('#tipo').selectpicker('render');
|
|
$("#nombre").val(data[4]);
|
|
$("#desc").val(data[5]);
|
|
if (data[6] != null) {
|
|
var name = String(data[6]).split('/');
|
|
$(".preview").show();
|
|
$(".uploaded-files").html('<a class="btn-link" href="' + data[6] + '" target="_blank">' +
|
|
'<img class="img-fluid" src="' + data[6] + '"><br>' + name[name.length - 1] + '</a>');
|
|
$("#urlimginsignia").val(data[6]);
|
|
$(".uploaded-files").addClass('updimg');
|
|
}
|
|
$("#habilitado").val(data[7]);
|
|
$(".controller").hide();
|
|
$(".editor").show();
|
|
$(".edit").show();
|
|
$(".crear").hide();
|
|
}
|
|
|
|
function deshabilitar(e, dt, node, config) {
|
|
var rows_selected = table.column(0).checkboxes.selected();
|
|
var ids = new Array();
|
|
$.each(rows_selected, function (index, rowId) {
|
|
ids.push(rowId);
|
|
});
|
|
$(".layer-load").show();
|
|
$.ajax({
|
|
url: 'gamification_delete.php',
|
|
type: 'POST',
|
|
data: { ids: ids },
|
|
})
|
|
.done(function (response) {
|
|
Swal.fire({
|
|
html: response,
|
|
icon: "success",
|
|
onBeforeOpen: function () {},
|
|
onOpen: function () {},
|
|
onRender: function () {},
|
|
onClose: function () {
|
|
reloadData();
|
|
},
|
|
onAfterClose: function () {}
|
|
});
|
|
})
|
|
.fail(function () {
|
|
|
|
})
|
|
.always(function () {
|
|
$(".layer-load").hide();
|
|
});
|
|
}
|
|
|
|
function crear() {
|
|
$("#idobjetivo").val("");
|
|
$("#nombre").val("");
|
|
$("#desc").val("");
|
|
$("#urlimginsignia").val("");
|
|
$(".wrecurso").hide();
|
|
$(".wcats").show();
|
|
$("#clave").val("");
|
|
$("#habilitado").val(1);
|
|
$(".controller").hide();
|
|
$(".editor").show();
|
|
$(".edit").hide();
|
|
$(".crear").show();
|
|
}
|
|
|
|
|
|
$(".btn_cancelar").click(function (event) {
|
|
if ($(".uploaded-files").hasClass('newimg')) {
|
|
$(".layer-load").show();
|
|
$.ajax({
|
|
url: 'gamification_deletefile.php',
|
|
type: 'POST',
|
|
data: { file: $(".uploaded-files a").attr('href') },
|
|
})
|
|
.done(function (response) {
|
|
|
|
})
|
|
.fail(function (error) {
|
|
|
|
})
|
|
.always(function () {
|
|
|
|
$(".layer-load").hide();
|
|
$(".preview").hide();
|
|
$(".ThumbnailGenerator, .uploaded-files").html("");
|
|
uppy.reset();
|
|
$(".controller").show();
|
|
$(".editor").hide();
|
|
$(".uploaded-files").removeClass('newimg');
|
|
});
|
|
} else {
|
|
$(".preview").hide();
|
|
$(".ThumbnailGenerator, .uploaded-files").html("");
|
|
uppy.reset();
|
|
$(".preview").hide();
|
|
$(".controller").show();
|
|
$(".editor").hide();
|
|
}
|
|
});
|
|
|
|
|
|
$(".formcontroller").validate({
|
|
submitHandler: function (form) {
|
|
if ($(".uploaded-files").hasClass('newimg')) {
|
|
var controller = 'gamification_upadate.php';
|
|
if ($(".crear").css("display") == "block") {
|
|
controller = 'gamification_add.php';
|
|
}
|
|
if ($("#tipo").val() == "categoria") {
|
|
$("#idobjetivo").val($("#catsuperior").val());
|
|
} else {
|
|
$("#idobjetivo").val($("#recurso").val());
|
|
}
|
|
$.ajax({
|
|
url: controller,
|
|
type: 'POST',
|
|
data: $(".formcontroller").serializeArray(),
|
|
})
|
|
.done(function (response) {
|
|
Swal.fire({
|
|
html: response,
|
|
icon: 'success',
|
|
onBeforeOpen: function () {},
|
|
onOpen: function () {},
|
|
onRender: function () {},
|
|
onClose: function () {
|
|
$(".preview").hide();
|
|
$(".ThumbnailGenerator, .uploaded-files").html("");
|
|
uppy.reset();
|
|
$(".preview").hide();
|
|
$(".controller").show();
|
|
$(".editor").hide();
|
|
reloadData();
|
|
|
|
$(".mainseccion").load("gamification.html", function () {
|
|
$(".layer-load").hide();
|
|
});
|
|
|
|
},
|
|
onAfterClose: function () {}
|
|
});
|
|
}).fail(function () {}).always(function () {});
|
|
} else {
|
|
|
|
Swal.fire({
|
|
html: "La imagen de la insignia es obligatoria",
|
|
icon: "error",
|
|
onBeforeOpen: function () {},
|
|
onOpen: function () {},
|
|
onRender: function () {},
|
|
onClose: function () {},
|
|
onAfterClose: function () {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
rules: {
|
|
nombre: {
|
|
required: true,
|
|
maxlength: 254,
|
|
minlength: 2,
|
|
normalizer: function (value) {
|
|
return $.trim(value);
|
|
}
|
|
}
|
|
},
|
|
messages: {
|
|
nombre: {
|
|
required: "El nombre de la Insignia es obligatorio",
|
|
minlength: jQuery.validator.format("El nombre de la Insignia es de al menos {0} caracteres"),
|
|
maxlength: jQuery.validator.format("El nombre de la Insignia es de máximo {0} caracteres")
|
|
}
|
|
}
|
|
});
|
|
|
|
getData();
|
|
|
|
});
|
|
</script> |