From d65058741824d14caf7dae3227d2d417c0dd1cf2 Mon Sep 17 00:00:00 2001 From: cgfhtw Date: Mon, 18 Nov 2024 09:58:59 +0100 Subject: [PATCH] use fhcApi for grades tab --- .../frontend/v1/stv/Grades.php} | 120 +++++++++++------- public/js/api/stv.js | 2 + public/js/api/stv/grades.js | 15 +++ .../Details/Noten/Zeugnis.js | 31 +++-- .../Details/Noten/Zeugnis/Actions.js | 9 +- 5 files changed, 115 insertions(+), 62 deletions(-) rename application/controllers/{components/stv/Noten.php => api/frontend/v1/stv/Grades.php} (57%) create mode 100644 public/js/api/stv/grades.js diff --git a/application/controllers/components/stv/Noten.php b/application/controllers/api/frontend/v1/stv/Grades.php similarity index 57% rename from application/controllers/components/stv/Noten.php rename to application/controllers/api/frontend/v1/stv/Grades.php index fb61de065..d521466cc 100644 --- a/application/controllers/components/stv/Noten.php +++ b/application/controllers/api/frontend/v1/stv/Grades.php @@ -1,36 +1,57 @@ . + */ if (! defined('BASEPATH')) exit('No direct script access allowed'); -class Noten extends Auth_Controller +/** + * This controller operates between (interface) the JS (GUI) and the back-end + * Provides data to the ajax get calls about grades + * This controller works with JSON calls on the HTTP GET or POST and the output is always JSON + */ +class Grades extends FHCAPI_Controller { public function __construct() { parent::__construct([ - 'get' => 'student/noten:r', - 'getZeugnis' => 'student/noten:r', - 'update' => ['admin:w', 'assistenz:w'] + 'list' => 'student/noten:r', + 'getCertificate' => 'student/noten:r', + 'updateCertificate' => ['admin:w', 'assistenz:w'], + 'getGradeFromPoints' => 'student/noten:r' ]); // Load Libraries $this->load->library('VariableLib', ['uid' => getAuthUID()]); } - public function get() + public function list() { $this->load->model('codex/Note_model', 'NoteModel'); - $result = $this->NoteModel->addOrder('note'); + $this->NoteModel->addOrder('note'); $result = $this->NoteModel->load(); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - } + + $grades = $this->getDataOrTerminateWithError($result); - return $this->outputJson($result); + $this->terminateWithSuccess($grades); } - public function getZeugnis($prestudent_id, $all = null) + public function getCertificate($prestudent_id, $all = null) { $this->load->model('crm/Student_model', 'StudentModel'); $this->load->model('education/Zeugnisnote_model', 'ZeugnisnoteModel'); @@ -38,26 +59,25 @@ class Noten extends Auth_Controller $result = $this->StudentModel->loadWhere([ 'prestudent_id' => $prestudent_id ]); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - return $this->outputJson($result); - } - if (!hasData($result)) - return $this->outputJsonSuccess(null); + + $student = $this->getDataOrTerminateWithError($result); + if (!$student) + $this->terminateWithSuccess([]); - $student_uid = current(getData($result))->student_uid; + + $student_uid = current($student)->student_uid; $studiensemester_kurzbz = ($all === null) ? $this->variablelib->getVar('semester_aktuell') : null; - $result = $this->ZeugnisnoteModel->getZeugnisnoten($student_uid, $studiensemester_kurzbz); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - } - return $this->outputJson($result); + $result = $this->ZeugnisnoteModel->getZeugnisnoten($student_uid, $studiensemester_kurzbz); + + $grades = $this->getDataOrTerminateWithError($result); + + $this->terminateWithSuccess($grades); } - public function update() + public function updateCertificate() { $this->load->model('crm/Student_model', 'StudentModel'); $this->load->model('organisation/Studienplan_model', 'StudienplanModel'); @@ -66,13 +86,10 @@ class Noten extends Auth_Controller $this->load->library('form_validation'); - $_POST = json_decode(utf8_encode($this->input->raw_input_stream), true); - if (empty($_POST) || !is_array(current($_POST))) { $result = $this->hasPermissionUpdate($this->input->post('lehrveranstaltung_id'), $this->input->post('student_uid')); if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_FORBIDDEN); - return $this->outputJson($result); + $this->terminateWithError(getError($result), self::ERROR_TYPE_AUTH, REST_Controller::HTTP_FORBIDDEN); } $this->form_validation->set_rules('lehrveranstaltung_id', 'Lehrverantaltung ID', 'required|numeric'); @@ -86,8 +103,7 @@ class Noten extends Auth_Controller $uid = isset($data['student_uid']) ? $data['student_uid'] : null; $result = $this->hasPermissionUpdate($lvid, $uid); if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_FORBIDDEN); - return $this->outputJson($result); + $this->terminateWithError(getError($result), self::ERROR_TYPE_AUTH, REST_Controller::HTTP_FORBIDDEN); } $this->form_validation->set_rules($i . '[lehrveranstaltung_id]', '#' . $i . ' Lehrverantaltung ID', 'required|numeric'); @@ -97,13 +113,11 @@ class Noten extends Auth_Controller } $post = $_POST; } - if ($this->form_validation->run() == false) { - $this->output->set_status_header(REST_Controller::HTTP_BAD_REQUEST); - return $this->outputJsonError($this->form_validation->error_array()); - } + if (!$this->form_validation->run()) + $this->terminateWithValidationErrors($this->form_validation->error_array()); - $final_result = success(); $this->ZeugnisnoteModel->db->trans_start(); + $authUID = getAuthUID(); foreach ($post as $i => $data) { $note = $data['note']; @@ -112,21 +126,39 @@ class Noten extends Auth_Controller 'note' => $note, 'benotungsdatum' => date('c'), 'updateamum' => date('c'), - 'updatevon' => getAuthUID() + 'updatevon' => $authUID ]); - if (isError($result)) { - $final_result = $result; - break; - } + $this->getDataOrTerminateWithError($result); } $this->ZeugnisnoteModel->db->trans_complete(); - if (isError($final_result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - } + $this->terminateWithSuccess(true); + } - $this->outputJson($final_result); + public function getGradeFromPoints() + { + $this->load->library('form_validation'); + + $this->form_validation->set_rules("lehrveranstaltung_id", "Lehrverantaltung ID", "required|integer"); // TODO(chris): phrase + $this->form_validation->set_rules("points", "Punkte", "required|numeric"); // TODO(chris): phrase + + if (!$this->form_validation->run()) + $this->terminateWithValidationErrors($this->form_validation->error_array()); + + $this->load->model('education/Notenschluesselaufteilung_model', 'NotenschluesselaufteilungModel'); + + $studiensemester_kurzbz = $this->variablelib->getVar('semester_aktuell'); + + $result = $this->NotenschluesselaufteilungModel->getNote( + $this->input->post('points'), + $this->input->post('lehrveranstaltung_id'), + $studiensemester_kurzbz + ); + + $note = $this->getDataOrTerminateWithError($result); + + $this->terminateWithSuccess($note); } protected function hasPermissionUpdate($lehrveranstaltung_id, $student_uid) diff --git a/public/js/api/stv.js b/public/js/api/stv.js index 14fcc6661..9a2f08f1d 100644 --- a/public/js/api/stv.js +++ b/public/js/api/stv.js @@ -2,12 +2,14 @@ import verband from './stv/verband.js'; import students from './stv/students.js'; import filter from './stv/filter.js'; import konto from './stv/konto.js'; +import grades from './stv/grades.js'; export default { verband, students, filter, konto, + grades, configStudent() { return this.$fhcApi.get('api/frontend/v1/stv/config/student'); }, diff --git a/public/js/api/stv/grades.js b/public/js/api/stv/grades.js new file mode 100644 index 000000000..ec34c3b0c --- /dev/null +++ b/public/js/api/stv/grades.js @@ -0,0 +1,15 @@ +export default { + list() { + return this.$fhcApi.get('api/frontend/v1/stv/grades/list'); + }, + getCertificate(prestudent_id, all) { + all = all ? '/all' : ''; + return this.$fhcApi.get('api/frontend/v1/stv/grades/getCertificate/' + prestudent_id + all); + }, + updateCertificate(data) { + return this.$fhcApi.post('api/frontend/v1/stv/grades/updateCertificate', data); + }, + getGradeFromPoints(points) { + return this.$fhcApi.post('api/frontend/v1/stv/grades/getGradeFromPoints', data); + } +} \ No newline at end of file diff --git a/public/js/components/Stv/Studentenverwaltung/Details/Noten/Zeugnis.js b/public/js/components/Stv/Studentenverwaltung/Details/Noten/Zeugnis.js index caeb0cfaa..698d837e2 100644 --- a/public/js/components/Stv/Studentenverwaltung/Details/Noten/Zeugnis.js +++ b/public/js/components/Stv/Studentenverwaltung/Details/Noten/Zeugnis.js @@ -1,5 +1,4 @@ import {CoreFilterCmpt} from "../../../../filter/Filter.js"; -import {CoreRESTClient} from '../../../../../RESTClient.js'; import ZeugnisActions from './Zeugnis/Actions.js'; const LOCAL_STORAGE_ID = 'stv_details_noten_zeugnis_2024-01-11_stdsem_all'; @@ -20,18 +19,24 @@ export default { }; }, computed: { - ajaxURL() { - return CoreRESTClient._generateRouterURI('components/stv/Noten/getZeugnis/' + this.student.prestudent_id + this.stdsem); - }, tabulatorOptions() { return { - ajaxURL: this.ajaxURL, + ajaxURL: 'dummy', + ajaxRequestFunc: (url, config, params) => { + return this.$fhcApi.factory.stv.grades.getCertificate(params.prestudent_id, params.stdsem); + }, + ajaxParams: () => { + return { + prestudent_id: this.student.prestudent_id, + stdsem: this.stdsem + }; + }, ajaxResponse: (url, params, response) => { - if (!response.retval) + if (!response.data) this.validStudent = false; else this.validStudent = true; - return response.retval || []; + return response.data || []; }, columns: [ { field: 'zeugnis', title: 'Zeugnis', formatter: 'tickCross' }, @@ -63,15 +68,17 @@ export default { } }, watch: { - ajaxURL(n) { - if (this.$refs.table) - this.$refs.table.tabulator.setData(n); + student(n) { + this.$refs.table.reloadTable(); + }, + stdsem(n) { + this.$refs.table.reloadTable(); } }, methods: { setGrades(selected) { - CoreRESTClient - .post('components/stv/Noten/update', selected) + this.$fhcApi.factory + .stv.grades.updateCertificate(selected) .then(this.$refs.table.reloadTable) .catch(this.$fhcAlert.handleFormValidation); }, diff --git a/public/js/components/Stv/Studentenverwaltung/Details/Noten/Zeugnis/Actions.js b/public/js/components/Stv/Studentenverwaltung/Details/Noten/Zeugnis/Actions.js index 7e8e2cbe5..a6c37a0ea 100644 --- a/public/js/components/Stv/Studentenverwaltung/Details/Noten/Zeugnis/Actions.js +++ b/public/js/components/Stv/Studentenverwaltung/Details/Noten/Zeugnis/Actions.js @@ -1,5 +1,3 @@ -import {CoreRESTClient} from '../../../../../../RESTClient.js'; - export default { emits: [ 'setGrades' @@ -36,11 +34,10 @@ export default { } }, created() { - CoreRESTClient - .get('components/stv/Noten/get') - .then(result => result.data) + this.$fhcApi.factory + .stv.grades.list() .then(result => { - this.grades = result.retval; + this.grades = result.data; }) .catch(this.$fhcAlert.handleSystemError); },