You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

222 lines
6.2 KiB

const { database } = require('../database')
const { getNiveau } = require('./Niveau')
const { matiereSysteme } = require('../function/System')
/**
* Function to insert notes into the database
* @param {Object} formData - The form data containing subject names and values
* @param {number} etudiant_id - The student ID
* @param {string} etudiant_niveau - The student level
* @returns {Promise} - Promise resolving to the database response or an error
*/
async function insertNoteRepech(
etudiant_id,
etudiant_niveau,
mention_id,
formData,
annee_scolaire
) {
// Extract keys and values dynamically
const matiere_id = Object.keys(formData)
const values = Object.values(formData)
const query = database.prepare(
`INSERT INTO notesrepech (etudiant_id, matiere_id, etudiant_niveau, mention_id, note, annee_scolaire) VALUES (?, ?, ?, ?, ?, ?)`
)
console.log(annee_scolaire)
try {
let response
for (let j = 0; j < matiere_id.length; j++) {
response = await query.run(
etudiant_id,
matiere_id[j],
etudiant_niveau,
mention_id,
parseFloat(values[j].replace(',', '.')) || 0,
annee_scolaire
)
}
return response
} catch (error) {
return error
}
}
/**
*
* @returns promise
*/
async function getNoteOnline() {
const query = database.prepare('SELECT notes.* FROM notes ')
try {
let response = await query.all()
return response
} catch (error) {
return error
}
}
/**
*
* @returns promise
*/
async function getNoteRepech(id, niveau) {
let semestre = await matiereSysteme(niveau)
const query2 = database.prepare(
'SELECT notesrepech.*, matieres.* FROM notesrepech JOIN matieres ON (notesrepech.matiere_id = matieres.id) WHERE notesrepech.etudiant_id = ? AND notesrepech.etudiant_niveau = ?'
)
try {
let response2 = query2.all(id, niveau)
return response2
} catch (error) {
console.error('Error in query2:', error)
return error
}
}
/**
* Verify if a student has notes
* @returns {Promise<Array>} - Promise resolving to an array of notes or an empty array
*/
async function verifyEtudiantIfHeHasNotesRepech() {
try {
// Prepare the query to filter by etudiant_id and etudiant_niveau
const query = database.prepare('SELECT DISTINCT etudiant_id, etudiant_niveau FROM notesrepech')
// Execute the query with the provided parameters
const response = query.all()
// Return the response
return response
} catch (error) {
console.error('Error verifying student notes:', error)
throw error
}
}
/**
* function to show moyenne in screen
*
* @returns promise
*/
async function showMoyenRepech(niveau, scolaire) {
const query = database.prepare(
`SELECT DISTINCT etudiant_id FROM notesrepech WHERE etudiant_niveau = ? AND annee_scolaire = ?`
)
let etudiantWithNotes = await query.all(niveau, scolaire)
let allEtudiantWithNotes = []
const query2 = database.prepare(
'SELECT notesrepech.*, etudiants.*, matieres.id, matieres.nom AS nomMat, matieres.credit FROM notesrepech INNER JOIN etudiants ON (notesrepech.etudiant_id = etudiants.id) INNER JOIN matieres ON (notesrepech.matiere_id = matieres.id) WHERE notesrepech.etudiant_id = ?'
)
try {
for (let index = 0; index < etudiantWithNotes.length; index++) {
allEtudiantWithNotes.push(query2.all(etudiantWithNotes[index].etudiant_id))
}
return allEtudiantWithNotes
} catch (error) {
return error
}
}
/**
* function used when updating note
* @param {Object} formData - The form data containing subject names and values
* @param {string} niveau - The student level
* @returns {Promise} - Promise resolving to the database response or an error
*/
async function updateNoteRepech(formData, niveau, id) {
// Extract keys and values dynamically
const matiere_id = Object.keys(formData)
const values = Object.values(formData)
const query = database.prepare(
'UPDATE notesrepech SET note= ? WHERE etudiant_id = ? AND etudiant_niveau = ? AND matiere_id = ?'
)
try {
let response
for (let index = 0; index < matiere_id.length; index++) {
let data = values[index]
if (typeof data === 'string') {
console.log(parseFloat(data.replace(',', '.')))
} else {
console.log(parseFloat(String(data).replace(',', '.')))
}
response = await query.run(data, id, niveau, matiere_id[index])
}
return response
} catch (error) {
return error
}
}
async function blockShowMoyeneRepech() {
const query = database.prepare(
'SELECT DISTINCT etudiant_niveau, annee_scolaire FROM notesrepech ORDER BY annee_scolaire DESC'
)
const queryMention = database.prepare('SELECT * FROM mentions')
try {
let response = await query.all()
let mention = await queryMention.all()
let niveau = response.map((item) => item.etudiant_niveau)
let annee_scolaire = response.map((item) => item.annee_scolaire)
const query2 = database.prepare(
`SELECT notesrepech.*, etudiants.id AS etudiantsId, etudiants.mention_id AS mentionId, etudiants.niveau, matieres.* FROM notesrepech INNER JOIN etudiants ON (notesrepech.etudiant_id = etudiants.id) INNER JOIN matieres ON (notesrepech.matiere_id = matieres.id) WHERE notesrepech.etudiant_niveau = ? AND notesrepech.annee_scolaire = ?`
)
let allData = []
for (let index = 0; index < niveau.length; index++) {
allData.push(await query2.all(niveau[index], annee_scolaire[index]))
}
return { response, allData, mention }
} catch (error) {
return error
}
}
/**
* get all note with matiere for single student
* @param {*} id
* @param {*} niveau
* @param {*} annee_scolaire
* @returns promise
*/
async function getMatiereAndNoteRepech(id, niveau, annee_scolaire) {
const query = database.prepare(
'SELECT * FROM notesrepech INNER JOIN matieres ON (notesrepech.matiere_id = matieres.id) WHERE notesrepech.etudiant_id = ? AND notesrepech.etudiant_niveau = ? AND notesrepech.annee_scolaire = ?'
)
try {
let response = await query.all(id, niveau, annee_scolaire)
return response
} catch (error) {
return error
}
}
module.exports = {
insertNoteRepech,
getNoteRepech,
showMoyenRepech,
getNoteOnline,
verifyEtudiantIfHeHasNotesRepech,
updateNoteRepech,
blockShowMoyeneRepech,
getMatiereAndNoteRepech
}