<?php
// This file is part of
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
class Teacherview {
    private $cmid;
    private $gradeimporterid;
    private $context;
    private $table;
    private $cellstyle = "border:1px solid black";

    public function __construct(int $cmid, int $gradeimporterid) {
        $this->$cmid = $cmid;
        $this->$gradeimporterid = $gradeimporterid;
        $this->context = context_module::instance($this->get_cmid);
        $this->$table = make_table($cmid, $gradeimporterid);
    }

    private function get_cmid() {
        return $this->cmid;
    }
    private function get_gradeimporterid() {
        return $this->gradeimporterid;
    }
    private function get_context() {
        return $this->context;
    }
    private function get_cellstyle() {
        return $this->cellstyle;
    }
    public function get_table() {
        return $this->table;
    }


    private function make_table() {
        global $DB, $CFG;

        // Prepare variables
        $studentlist = get_studentlist();

        // Create table
        $table = new html_table();
        $table->align = array('center');
        $table->attributes = array('class' => 'generaltable mod_index');
        $table->head = get_table_head();

        return $table;
    }

    private function get_studentlist() {
        // Get students list with userid as key and fullname as value
        $enrolledusers = get_enrolled_users($this->get_context(), 'mod/gradeimporter:view',
                                        0, 'u.id, u.firstname, u.lastname',
                                        'u.firstname, u.lastname'
                                    );
        $studentlist = array();
        foreach ($enrolledusers as $user) {
            $userslist[$user->id] = array('name' => $user->firstname." ".$user->lastname);
        }
        return $studentlist;
    }

    private function get_table_head() {
        global $CFG;
        $tp = $CFG->prefix;
        // Creates teacher view table head
        $head = new html_table_row();

        // Creates name column header
        $cell = new html_table_cell(get_string('nameCol', 'gradeimporter'));
        $cell->style = $this->get_cellstyle();
        $cell->colspan = 1;

        // Add name header to header row
        $header->cells[] = $cell;

        // Get Submissions names and ids
        $sql = "SELECT id, name, type
                FROM {$tp}gradeimporter_submission
                WHERE gradeimporterid = {$this->get_gradeimporterid()}
                ORDER BY type, id";
        $submissions = $DB->get_records($sql);
        foreach ($submissions as $submission) {
            // $subname = editSub($submission->name, $gradeimporterid, $cmid, $submission->id);
            $cell = new html_table_cell($submission->name);
            $cell->colspan = 2;
            $cell->style = $this->get_cellstyle();
            $header->cells[] = $cell;
        }
        return $header;
    }
}