Skip to content

Commit

Permalink
Merge pull request thephpleague#7 from BryanGuapulema/surveysView
Browse files Browse the repository at this point in the history
Surveys view
  • Loading branch information
BryanGuapulema authored Nov 27, 2024
2 parents 8e41010 + eb4fe72 commit 0e3edfb
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 57 deletions.
36 changes: 32 additions & 4 deletions app/Http/Controllers/SurveyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Redirect;


class SurveyController extends Controller
Expand Down Expand Up @@ -41,20 +43,46 @@ public function store(Request $request)
'questions' => $questions,
'created_by' => Auth::id(),
]);
return redirect()->route('surveys.show', $survey->uuid)
return redirect()->route('surveys.generated', $survey->uuid)
->with('success', 'Encuesta creada exitosamente. Usa el enlace único para compartirla.');
}

public function index()
{
$surveys = Survey::where('created_by', Auth::id())->get();
return view('docente.index', compact('surveys'));
return view('surveys.index', compact('surveys'));
}

public function show($uuid)
public function generated($uuid)
{
$survey = Survey::where('uuid', $uuid)->firstOrFail();

return view('surveys.show', compact('survey'));
return view('surveys.generated', compact('survey'));
}

public function show($id)
{
$survey = Survey::with('responses')->findOrFail($id);

// Asegúrate de que las preguntas sean un array
$questions = $survey->questions;

// Procesa las respuestas y decodifica el JSON
$responses = $survey->responses->map(function ($response) {
return json_decode($response->answers, true); // Decodifica como array
});

return view('surveys.show', compact('survey', 'questions', 'responses'));
}




public function destroy($id): RedirectResponse
{
Survey::find($id)->delete();

return Redirect::route('surveys.index')
->with('success', 'Encuesta eliminada con éxito');
}
}
25 changes: 19 additions & 6 deletions app/Http/Controllers/UserRoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Models\User;
use Illuminate\Http\Request;
use App\Models\Docente;


class UserRoleController extends Controller
{
Expand All @@ -18,12 +20,23 @@ public function index()
public function update(Request $request, User $user)
{
// Validar que el rol enviado existe
$request->validate([
'role' => 'required|in:admin,docente,estudiante', // Roles permitidos
]);

// Asignar el nuevo rol al usuario
$user->syncRoles([$request->role]);
$validated = $request->validate([
'role' => 'required|in:estudiante,docente,admin',
]);

// Cambiar el rol del usuario
$user->syncRoles($validated['role']);

// Si el nuevo rol es "docente", agregar el usuario a la tabla docentes
if ($validated['role'] === 'docente') {
// Verificar si el usuario ya está en la tabla docentes
if (!Docente::where('id_usuario', $user->id)->exists()) {
Docente::create(['id_usuario' => $user->id]);
}
} else {
// Si el rol no es "docente", eliminar al usuario de la tabla docentes
Docente::where('id_usuario', $user->id)->delete();
}

// Redirigir con un mensaje de éxito
return redirect()->route('admin.users.index')->with('success', 'Rol actualizado correctamente.');
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class Survey extends Model
*/
public function responses()
{
return $this->hasMany(Response::class);
return $this->hasMany(Response::class, 'survey_id');
}

// Convierte las preguntas de JSON a array cuando las obtienes

protected $casts = [
'questions' => 'array',
];
Expand Down
2 changes: 1 addition & 1 deletion config/fortify.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
|
*/

'home' => '/dashboard',
'home' => '/estudiante/dashboard',

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions resources/views/docente/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class="mt-4 inline-block px-6 py-2 bg-indigo-600 text-white text-center rounded-
<div class="bg-white rounded-lg shadow-md overflow-hidden">
<div class="p-6">
<h3 class="text-xl font-semibold text-gray-800">Ver Mis Encuestas</h3>
<p class="mt-2 text-gray-600">Accede a las encuestas que ya has creado y realiza modificaciones si es necesario.</p>
<a href="#"
<p class="mt-2 text-gray-600">Accede a las encuestas que ya has creado.</p>
<a href="{{ route('surveys.index') }}"
class="mt-4 inline-block px-6 py-2 bg-green-600 text-white text-center rounded-md hover:bg-green-500 focus:outline-none focus:ring-2 focus:ring-green-600 focus:ring-offset-2 transition duration-200">
Ver Encuestas
</a>
Expand All @@ -42,7 +42,7 @@ class="mt-4 inline-block px-6 py-2 bg-green-600 text-white text-center rounded-m
<div class="p-6">
<h3 class="text-xl font-semibold text-gray-800">Ver Resultados</h3>
<p class="mt-2 text-gray-600">Accede a los resultados de las encuestas que ya has creado.</p>
<a href="#"
<a href="{{ route('surveys.index') }}"
class="mt-4 inline-block px-6 py-2 bg-cyan-600 text-white text-center rounded-md hover:bg-cyan-500 focus:outline-none focus:ring-2 focus:ring-cyan-600 focus:ring-offset-2 transition duration-200">
Ver Resultados
</a>
Expand Down
20 changes: 12 additions & 8 deletions resources/views/docente/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,39 @@

<div class="flow-root">
<div class="mt-8 overflow-x-auto">
<div class="inline-block min-w-full py-2 align-middle">
<div class=" min-w-full py-2 align-middle">
<table class="w-full divide-y divide-gray-300">
<thead>
<tr>
<th scope="col" class="py-3 pl-4 pr-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500">No</th>

<th scope="col" class="py-3 pl-4 pr-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500"></th>
<!--
<th scope="col" class="py-3 pl-4 pr-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500">Id Curso Paralelo</th>
<th scope="col" class="py-3 pl-4 pr-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500">Id Periodo</th>
-->
<th scope="col" class="py-3 pl-4 pr-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500">Id Usuario</th>
<th scope="col" class="py-3 pl-4 pr-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500">Nombre</th>

<th scope="col" class="px-3 py-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500"></th>
<th scope="col" class="px-3 py-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500">Acción</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200 bg-white">
@foreach ($docentes as $docente)
<tr class="even:bg-gray-50">
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-semibold text-gray-900">{{ ++$i }}</td>

<!--
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{{ $docente->id_curso_paralelo }}</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{{ $docente->id_periodo }}</td>
-->
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{{ $docente->id_usuario }}</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{{ $docente->user->name }}</td>

<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900">
<form action="{{ route('docentes.destroy', $docente->id) }}" method="POST">
<a href="{{ route('docentes.show', $docente->id) }}" class="text-gray-600 font-bold hover:text-gray-900 mr-2">{{ __('Mostrar') }}</a>
<a href="{{ route('docentes.edit', $docente->id) }}" class="text-indigo-600 font-bold hover:text-indigo-900 mr-2">{{ __('Editar') }}</a>
<a href="{{ route('docentes.show', $docente->id) }}" class=" rounded-md bg-cyan-600 px-3 py-2 text-center text-sm font-semibold text-white shadow-sm hover:bg-cyan-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-cyan-600">{{ __('Mostrar') }}</a>
<a href="{{ route('docentes.edit', $docente->id) }}" class=" rounded-md bg-orange-600 px-3 py-2 text-center text-sm font-semibold text-white shadow-sm hover:bg-orange-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-orange-600">{{ __('Editar') }}</a>
@csrf
@method('DELETE')
<a href="{{ route('docentes.destroy', $docente->id) }}" class="text-red-600 font-bold hover:text-red-900" onclick="event.preventDefault(); confirm('Are you sure to delete?') ? this.closest('form').submit() : false;">{{ __('Eliminar') }}</a>
<a href="{{ route('docentes.destroy', $docente->id) }}" class=" rounded-md bg-red-600 px-3 py-2 text-center text-sm font-semibold text-white shadow-sm hover:bg-red-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-red-600" onclick="event.preventDefault(); confirm('Are you sure to delete?') ? this.closest('form').submit() : false;">{{ __('Eliminar') }}</a>
</form>
</td>
</tr>
Expand Down
11 changes: 10 additions & 1 deletion resources/views/estudiante/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
<x-welcome/>
<x-welcome/>

</div>

<div class="mt-6 lg:p-8 bg-white border-b border-gray-200">
<!--<x-application-logo class="block h-12 w-auto" />-->

<h1 class="text-2xl text-center font-semibold text-gray-800">
En caso de ser docente, solicite al administrador la asignación de dicho rol.
</h1>
</div>
</div>
</div>
Expand Down
41 changes: 41 additions & 0 deletions resources/views/surveys/generated.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
Encuesta Creada
</h2>
</x-slot>

<div class="py-6">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900">
<h3 class="text-lg font-semibold mb-4">¡Tu encuesta ha sido creada!</h3>
<p>Comparte este enlace con tus estudiantes para que respondan la encuesta:</p>
<div class="mt-4 bg-gray-100 p-4 rounded-md flex justify-between items-center">
<a href="{{ url('/survey/' . $survey->uuid) }}" id="survey-link"
class="text-blue-600 underline flex-1 mr-4">
{{ url('/survey/' . $survey->uuid) }}
</a>
<!-- Botón para copiar la URL -->
<button id="copy-button"
class="ml-4 text-white bg-indigo-600 px-4 py-2 rounded-md hover:bg-indigo-500 focus:outline-none">
Copiar
</button>

</div>
</div>
</div>
</div>
</div>

<script>
document.getElementById('copy-button').addEventListener('click', function() {
const url = document.getElementById('survey-link').textContent;
navigator.clipboard.writeText(url).then(function() {
alert('URL copiada al portapapeles');
}).catch(function(error) {
alert('Error al copiar la URL: ' + error);
});
});
</script>
</x-app-layout>
54 changes: 54 additions & 0 deletions resources/views/surveys/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Mis Encuestas') }}
</h2>
</x-slot>

<div class="py-6">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">Encuestas Creadas</h3>
<a href="{{ route('surveys.create') }}" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded">
Crear Nueva Encuesta
</a>
</div>

<table class="min-w-full border border-gray-300">
<thead>
<tr class="bg-gray-100">
<th class="px-4 py-2 border-b text-left">Título</th>
<th class="px-4 py-2 border-b text-left">Link</th>
<th class="px-4 py-2 border-b text-center">Acciones</th>
</tr>
</thead>
<tbody>
@forelse ($surveys as $survey)
<tr>
<td class="px-4 py-2 border-b">{{ $survey->title }}</td>
<td class="px-4 py-2 border-b">{{ url('/survey/' . $survey->uuid) }}</td>
<td class="px-4 py-2 border-b text-center">
<a href="{{ route('surveys.show', $survey->id) }}" class="rounded-md bg-cyan-600 px-3 py-2 text-center text-sm font-semibold text-white shadow-sm hover:bg-cyan-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-cyan-600">Ver</a>
<form action="{{ route('surveys.destroy', $survey->id) }}" method="POST" class="inline">
@csrf
@method('DELETE')
<button type="submit" class=" rounded-md bg-red-600 px-3 py-2 text-center text-sm font-semibold text-white shadow-sm hover:bg-red-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-red-600" onclick="return confirm('¿Estás seguro de que deseas eliminar esta encuesta?')">Eliminar</button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="3" class="text-center py-4 text-gray-500">
No has creado encuestas aún.
</td>
</tr>
@endforelse
</tbody>
</table>
</div>
</div>
</div>
</div>
</x-app-layout>
50 changes: 22 additions & 28 deletions resources/views/surveys/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
<x-app-layout>

<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
Encuesta Creada
Respuestas de la Encuesta
</h2>
</x-slot>

<div class="py-6">
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900">
<h3 class="text-lg font-semibold mb-4">¡Tu encuesta ha sido creada!</h3>
<p>Comparte este enlace con tus estudiantes para que respondan la encuesta:</p>
<div class="mt-4 bg-gray-100 p-4 rounded-md flex justify-between items-center">
<a href="{{ url('/survey/' . $survey->uuid) }}" id="survey-link"
class="text-blue-600 underline flex-1 mr-4">
{{ url('/survey/' . $survey->uuid) }}
</a>
<!-- Botón para copiar la URL -->
<button id="copy-button"
class="ml-4 text-white bg-indigo-600 px-4 py-2 rounded-md hover:bg-indigo-500 focus:outline-none">
Copiar
</button>

<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg p-6">
<h3 class="text-lg font-bold mb-4">Resultados de la Encuesta: {{ $survey->title }}</h3>

@foreach ($questions as $index => $question)
<div class="mb-8">
<label class="block text-gray-700 font-semibold mb-2">{{ $index + 1 }}. {{ $question['question'] }}</label>

<div class="space-y-2">
@foreach ($responses as $response)
<div class="flex items-center p-4 bg-gray-50 border border-gray-200 rounded-md shadow-sm">
<span class="text-gray-600">{{ $response[$index] ?? 'No respondida' }}</span>
</div>
@endforeach
</div>
</div>
</div>
@endforeach

<a href="{{ route('surveys.index') }}" class="mt-4 inline-block px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-700">
Volver a Encuestas
</a>
</div>
</div>
</div>

<script>
document.getElementById('copy-button').addEventListener('click', function() {
const url = document.getElementById('survey-link').textContent;
navigator.clipboard.writeText(url).then(function() {
alert('URL copiada al portapapeles');
}).catch(function(error) {
alert('Error al copiar la URL: ' + error);
});
});
</script>
</x-app-layout>
Loading

0 comments on commit 0e3edfb

Please sign in to comment.