<?php
  function decrementLetter($char)
  {
    $len = strlen($char);
    // last character is A or a
    if(ord($char[$len - 1]) === 65 || ord($char[$len - 1]) === 97)
    {
      if($len === 1)
      { // one character left
        return null;
      }
      else
      { // 'ABA'--;  => 'AAZ'; recursive call
        $char = decrementLetter(substr($char, 0, -1)).'Z';
      }
    }
    else
    {
      $char[$len - 1] = chr(ord($char[$len - 1]) - 1);
    }
    return $char;
  }

  function getSchoolInfo($pSchoolID)
	{
		global $connexion;

		$requete = 'SELECT * FROM t_liste_ecoles_08 WHERE ID = ' . escapeValue($pSchoolID);
		if(!$resultat = $connexion->query($requete)){ die('There was an error running the query [' . $connexion->error . ']'); }
		$ligne = $resultat->fetch_assoc();
		return $ligne;
	}

	function getUserInfos()
	{
		global $connexion;

		$requete = 'SELECT * FROM isoactif_t_usager_ecole,t_liste_ecoles_08,t_arse WHERE u_id = \'' . escapeValue($_SESSION['usagerEcoleIsoActif']->u_id) . '\' AND u_e_id_fk = ID AND a_id_fk = a_id';
		if(!$resultat = $connexion->query($requete)){ die('There was an error running the query [' . $connexion->error . ']'); }
		$ligne = $resultat->fetch_assoc();
		return $ligne;
	}

  function afficherCertificationPrimaire($pPourcentage)
	{
    global $txt;

		$html = '';

		if ($pPourcentage <= 60)
			$html .= $txt['certParticipation'];
		else if ($pPourcentage >= 61 && $pPourcentage <= 70)
			$html .= $txt['certBronze'];
		else if ($pPourcentage >= 71 && $pPourcentage <= 80)
			$html .= $txt['certArgent'];
		else if ($pPourcentage >= 81 && $pPourcentage <= 90)
			$html .= $txt['certOr'];
		else if ($pPourcentage >= 91)
			$html .= $txt['certExcellence'];

		return $html;
	}

  function calculerTotalPoints($pUserID,$pSecteur)
  {
    global $connexion;

    if ($pSecteur == 'P')
      $requete = 'SELECT IFNULL(fp_q1, 0) + IFNULL(fp_q2_1, 0)+IFNULL(fp_q2_2, 0)+IFNULL(fp_q3_1, 0)+IFNULL(fp_q3_2, 0)+IFNULL(fp_q4, 0)+IFNULL(fp_q5, 0)+IFNULL(fp_q6, 0)+IFNULL(fp_q7_1, 0)+IFNULL(fp_q7_2, 0)+IFNULL(fp_q8, 0)+IFNULL(fp_q9, 0)+IFNULL(fp_q10, 0) AS totalPoints FROM isoactif_form_p WHERE fp_u_id_fk = ' . escapeValue($pUserID);
    else
      $requete = 'SELECT IFNULL(fs_q1, 0) + IFNULL(fs_q2, 0)+IFNULL(fs_q3_1, 0)+IFNULL(fs_q3_2, 0)+IFNULL(fs_q4, 0)+IFNULL(fs_q5, 0)+IFNULL(fs_q6_1, 0)+IFNULL(fs_q6_boni, 0)+IFNULL(fs_q6_2, 0)+IFNULL(fs_q7, 0)+IFNULL(fs_q8, 0)+IFNULL(fs_q8_boni, 0)+IFNULL(fs_q9, 0)+IFNULL(fs_q9_boni, 0)+IFNULL(fs_q10, 0) AS totalPoints FROM isoactif_form_s WHERE fs_u_id_fk = ' . escapeValue($pUserID);

    if(!$resultat = $connexion->query($requete)){ die('There was an error running the query [' . $connexion->error . ']'); }
    $ligne = $resultat->fetch_assoc();
    $totalPoints = $ligne['totalPoints'];
    return $totalPoints;
  }
?>
