table ratings laravel

0

I have a table and its respective Qualification model, where I have 8 notes, average, exam, final grade. In my controller I calculate the average with the notes other than 0, and the final grade.

function guardarCalificacion(CalificacionRequest $request) {
    $calificacion = new Calificacion($request->all());

    $calificacion->id_profesor = auth('profesor')->user()->id;

    $profesor        = Profesor::find(auth('profesor')->user()->id);
    $mis_asignaturas = $profesor->asignaturas->all();

    $cantidad = 0;

    if (($calificacion->n1) != null) {
        $cantidad = $cantidad + 1;
    } else {
        $calificacion->n1 = 0;
    }

    if ($calificacion->n2 != null) {
        $cantidad = $cantidad + 1;
    } else {
        $calificacion->n2 = 0;
    }

    if ($calificacion->n3 != null) {
        $cantidad = $cantidad + 1;
    } else {
        $calificacion->n3 = 0;
    }

    if ($calificacion->n4 != null) {
        $cantidad = $cantidad + 1;
    } else {
        $calificacion->n4 = 0;
    }

    if ($calificacion->n5 != null) {
        $cantidad = $cantidad + 1;
    } else {
        $calificacion->n5 = 0;
    }

    if ($calificacion->n6 != null) {
        $cantidad = $cantidad + 1;
    } else {
        $calificacion->n6 = 0;
    }

    if ($calificacion->n7 != null) {
        $cantidad = $cantidad + 1;
    } else {
        $calificacion->n7 = 0;
    }

    if ($calificacion->n8 != null) {
        $cantidad = $cantidad + 1;
    } else {
        $calificacion->n8 = 0;
    }

    $calificacion->promedio = ($calificacion->n1 + $calificacion->n2 + $calificacion->n3 + $calificacion->n4 + $calificacion->n5 + $calificacion->n6 + $calificacion->n7 + $calificacion->n8) / $cantidad;

    if ($calificacion->examen != null) {
        $calificacion->final = ($calificacion->promedio + $calificacion->examen) / 2;
    }

    $calificacion->save();

    flash('Calificacion agregada exitosamente!', 'success');
    return view('datos-profesor/asignaturas')->with('calificacion', $calificacion)->with('profesor', $profesor)->with('mis_asignaturas', $mis_asignaturas);
}

What I want is to get that variable of average and final note of my table, so that they do not depend on each other. that those two variables are calculated in the controller and displayed in the view, without the need for my Qualifications table to have those two attributes ($calificacion->promedio , $calificacion->final) , how can I do that and send them to my view? I have the idea of simply placing

$promedio = ($calificacion->n1 + $calificacion->n2 + $calificacion->n3 + $calificacion->n4 + $calificacion->n5 + $calificacion->n6 + $calificacion->n7 + $calificacion->n8) / $cantidad;

$final = ($calificacion->promedio + $calificacion->examen) / 2;

But I do not know how to send it to view, this is my view:

<table class="table table-bordered">                           
    <tr>    
        <th>N1</th>
        <th>N2</th>
        <th>N3</th>
        <th>N4</th>
        <th>N5</th>
        <th>N6</th>
        <th>N7</th>
        <th>N8</th>
        <th>Promedio  </th>
        <th>Examen  </th>
        <th>Final  </th>
        <th>Observacion  </th>
        <th>Opciones  </th>
    </tr>                                
    @foreach($mis_notas as $mis_notas)  
    <tr>
        <td>{{$mis_notas->n1}}</td>
        <td>{{$mis_notas->n2}}</td>
        <td>{{$mis_notas->n3}}</td>
        <td>{{$mis_notas->n4}}</td>
        <td>{{$mis_notas->n5}}</td>
        <td>{{$mis_notas->n6}}</td>
        <td>{{$mis_notas->n7}}</td>
        <td>{{$mis_notas->n8}}</td>
        <td>{{$mis_notas->promedio}}</td>
        <td>{{$mis_notas->examen}}</td>
        <td>{{$mis_notas->final}}</td>
        <td>{{$mis_notas->observacion}}</td>
        <td><a class="btn btn-warning" href="{{URL('modificar/calificacion', array($mis_notas->id_alumno, $mis_notas->id_asignatura, $mis_notas->id ))}}"> Agregar Notas</a></td>
    </tr>
    @endforeach
</table>

How could I do it? to send variable $promedio and $final to the view? since it is within% co_of% of my_notes ??

Qualification Model

protected $table = "calificaciones";

protected $fillable = ['n1', 'n2', 'n3', 'n4', 'n5', 'n6', 'n7', 'n8', 'promedio', 'examen', 'final', 'observacion', 'id_alumno',  'id_asignatura', 'id_profesor'];

public function alumno()
{ 
    return $this->belongsTo(Alumno::class, 'id_alumno', 'id');
}

public function asignatura()
{
    return $this->belongsTo(Asignatura::class, 'id_asignatura', 'id');
}

public function profesor()
{
    return $this->belongsTo(Profesor::class, 'id_profesor', 'id');
}

Student Model

protected $table = "alumnos";

protected $fillable = ['rut', 'nombre', 'apellido_paterno', 'apellido_materno', 'email', 'password', 'sexo', 'telefono', 'foto', 'fecha_nacimiento', 'direccion', 'id_curso', 'id_apoderado'];

protected $hidden = ['password', 'remember_token'];

public function curso()
{
    return $this->belongsTo(Curso::class,'id_curso','id');
}

public function apoderado()
{
    return $this->belongsTo(Apoderado::class,'id_apoderado','id'); 
}

public function matriculas()
{
    return $this->hasMany('App\Matricula');
}

public function asignaturas()
{
    return $this->belongsToMany('App\Asignatura');
}

public function conductas()
{
    return $this->hasMany('App\Conducta','id_alumno');
}

public function calificaciones()
{
    return $this->hasMany('App\Calificacion','id_alumno');
}

Subject Model

protected $table = "asignaturas";

protected $fillable = ['nombre', 'horario', 'periodo', 'codigo', 'id_sala', 'id_curso', 'id_profesor'];

public function sala()
{
    return $this->belongsTo(Sala::class,'id_sala','id');
}

public function curso()
{
    return $this->belongsTo(Curso::class,'id_curso','id');
}

public function profesor()
{
    return $this->belongsTo(Profesor::class,'id_profesor','id');
}

public function alumnos()
{
    return $this->belongsToMany('App\Alumno')->withTimestamps();
}

public function conductas()
{
    return $this->hasMany('App\Conducta','id_asignatura');
}

public function eventos()
{
    return $this->hasMany('App\Evento','id_asignatura');
}

public function calificaciones()
{
    return $this->hasMany('App\Calificacion','id_asignatura');
}

The student enrolls in a course and the course has subjects

    
asked by Edgardo Escobar 28.05.2017 в 01:51
source

1 answer

0

To obtain the average grade of a student, you could define a table "notes" like the following:

Schema::create('notas', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('id_alumno')->unsigned();
    $table->integer('id_evaluacion')->unsigned();
    $table->string('nota', 3, 2); // un entero, dos decimales ... 2.00, 7.00, 4.00

    $table->timestamps();
});

then you enter data like these:

id  id_alumno  id_evaluacion  nota
1,  1,         1,             4.60
2,  1,         2,             5.60
3,  1,         3,             4.40
4,  1,         4,             6.50
5,  1,         5,             2.30
6,  1,         6,             4.50
7,  1,         7,             7.00
8,  1,         8,             2.60
9,  2,         1,             4.70
10, 2,         2,             6.70
11, 2,         3,             4.30
12, 2,         4,             1.20
13, 2,         5,             4.60
14, 2,         6,             7.00
15, 2,         7,             5.60

And to get the average you use a Query like this:

SELECT sum(nota)/count(*) as Promedio FROM notas WHERE id_alumno = 1 GROUP BY id_alumno;

(in Laravel it would be like this, but I have not proved it: P)

DB::table('notas')
    ->select('sum(nota)/count(*)')
    ->where('id_alumno', 1)
    ->groupBy('id_alumno')
    ->get();

Where the result is:

Promedio
4.687500
    
answered by 28.05.2017 / 05:43
source