<?php
// $Id: nivelamento_resultados.php,v 1.7 2007/03/24 14:41:41 marcellobrandao Exp $
// ------------------------------------------------------------------------ //
// XOOPS - PHP Content Management System //
// Copyright (c) 2000 XOOPS.org //
// <http://www.xoops.org/> //
// ------------------------------------------------------------------------ //
// This program 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 2 of the License, or //
// (at your option) any later version. //
// //
// You may not change or alter any portion of this comment or credits //
// of supporting developers from this source code or any supporting //
// source code which is considered copyrighted (c) material of the //
// original comment or credit authors. //
// //
// This program 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 this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
// ------------------------------------------------------------------------ //
include_once XOOPS_ROOT_PATH."/class/xoopsobject.php";
/**
* nivelamento_resultados class.
* $this class is responsible for providing data access mechanisms to the data source
* of XOOPS user class objects.
*/
class nivelamento_resultados extends XoopsObject
{
var $db;
// constructor
function nivelamento_resultados ($id=null)
{
$this->db =& Database::getInstance();
$this->initVar("cod_resultado",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("cod_prova",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("uid_aluno",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("data_inicio",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("data_fim",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("resp_certas",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("resp_erradas",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("nota_final",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("nivel",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("obs",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("fechada",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("terminou",XOBJ_DTYPE_INT,null,false,10);
if ( !empty($id) ) {
if ( is_array($id) ) {
$this->assignVars($id);
} else {
$this->load(intval($id));
}
} else {
$this->setNew();
}
}
function load($id)
{
$sql = 'SELECT * FROM '.$this->db->prefix("nivelamento_resultados").' WHERE cod_resultado='.$id;
$myrow = $this->db->fetchArray($this->db->query($sql));
$this->assignVars($myrow);
if (!$myrow) {
$this->setNew();
}
}
function getAllnivelamento_resultadoss($criteria=array(), $asobject=false, $sort="cod_resultado", $order="ASC", $limit=0, $start=0)
{
$db =& Database::getInstance();
$ret = array();
$where_query = "";
if ( is_array($criteria) && count($criteria) > 0 ) {
$where_query = " WHERE";
foreach ( $criteria as $c ) {
$where_query .= " $c AND";
}
$where_query = substr($where_query, 0, -4);
} elseif ( !is_array($criteria) && $criteria) {
$where_query = " WHERE ".$criteria;
}
if ( !$asobject ) {
$sql = "SELECT cod_resultado FROM ".$db->prefix("nivelamento_resultados")."$where_query ORDER BY $sort $order";
$result = $db->query($sql,$limit,$start);
while ( $myrow = $db->fetchArray($result) ) {
$ret[] = $myrow['nivelamento_resultados_id'];
}
} else {
$sql = "SELECT * FROM ".$db->prefix("nivelamento_resultados")."$where_query ORDER BY $sort $order";
$result = $db->query($sql,$limit,$start);
while ( $myrow = $db->fetchArray($result) ) {
$ret[] = new nivelamento_resultados ($myrow);
}
}
return $ret;
}
function getRespostasCertasAsArray(){
$respostas = explode(",",$this->getVar('resp_certas'));
$par_respostas = array();
foreach ($respostas as $resposta){
$x = explode("-",$resposta);
$par_respostas[$x[0]] = @$x[1];
}
return $par_respostas;
}
function getRespostasErradasAsArray(){
$respostas = explode(",",$this->getVar('resp_erradas'));
$par_respostas = array();
foreach ($respostas as $resposta){
$x = explode("-",$resposta);
$par_respostas[$x[0]] = $x[1];
}
return $par_respostas;
}
function getRespostasAsArray(){
$erradas= array();
$certas= array();
$erradas = $this->getRespostasErradasAsArray();
$certas = $this->getRespostasCertasAsArray();
$todas = $erradas + $certas;
return $todas;
}
function getCodPerguntasAsArray(){
$erradas= array();
$certas= array();
$erradas = $this->getRespostasErradasAsArray();
$certas = $this->getRespostasCertasAsArray();
$todas = $erradas + $certas;
return array_keys($todas);
}
function setRespostasCertasAsArray($respostasCertas){
$x = array();
foreach ($respostasCertas as $chave => $valor)
{
if (!($chave==null)){
$x[] = $chave."-".$valor; }
}
$y = implode(",",$x);
$this->setVar('resp_certas',$y);
}
function setRespostasErradasAsArray($respostasErradas){
$x = array();
foreach ($respostasErradas as $chave => $valor)
{
if (!($chave==null)){
$x[] = $chave."-".$valor; }
}
$y = implode(",",$x);
$this->setVar('resp_erradas',$y);
}
function getRespostaUsuario($cod_pergunta){
$respostas = $this->getRespostasAsArray();
return $respostas[$cod_pergunta];
}
function contarRespostas(){
$respostas = $this->getRespostasAsArray();
unset($respostas[null]);
$qtd = count( $respostas);
return $qtd;
}
}
// -------------------------------------------------------------------------
// ------------------nivelamento_resultados user handler class -------------------
// -------------------------------------------------------------------------
/**
* nivelamento_resultadoshandler class.
* This class provides simple mecanisme for nivelamento_resultados object
*/
class Xoopsnivelamento_resultadosHandler extends XoopsObjectHandler
{
/**
* create a new nivelamento_resultados
*
* @param bool $isNew flag the new objects as "new"?
* @return object nivelamento_resultados
*/
function &create($isNew = true) {
$nivelamento_resultados = new nivelamento_resultados();
if ($isNew) {
$nivelamento_resultados->setNew();
}else {
$nivelamento_resultados->unsetNew();
}
return $nivelamento_resultados;
}
/**
* retrieve a nivelamento_resultados
*
* @param int $id of the nivelamento_resultados
* @return mixed reference to the {@link nivelamento_resultados} object, FALSE if failed
*/
function &get($id) {
$sql = 'SELECT * FROM '.$this->db->prefix('nivelamento_resultados').' WHERE cod_resultado='.$id;
if (!$result = $this->db->query($sql)) {
return false;
}
$numrows = $this->db->getRowsNum($result);
if ($numrows == 1) {
$nivelamento_resultados = new nivelamento_resultados();
$nivelamento_resultados->assignVars($this->db->fetchArray($result));
return $nivelamento_resultados;
}
return false;
}
/**
* insert a new nivelamento_resultados in the database
*
* @param object $nivelamento_resultados reference to the {@link nivelamento_resultados} object
* @param bool $force
* @return bool FALSE if failed, TRUE if already present and unchanged or successful
*/
function insert(&$nivelamento_resultados, $force = false) {
Global $xoopsConfig;
if (get_class($nivelamento_resultados) != 'nivelamento_resultados') {
return false;
}
if (!$nivelamento_resultados->isDirty()) {
return true;
}
if (!$nivelamento_resultados->cleanVars()) {
return false;
}
foreach ($nivelamento_resultados->cleanVars as $k => $v) {
${$k} = $v;
}
//$now = "date_add(now(), interval ".$xoopsConfig['server_TZ']." hour)";
$now = "now()";
if ($nivelamento_resultados->isNew()) {
// ajout/modification d'un nivelamento_resultados
$nivelamento_resultados = new nivelamento_resultados();
$format = "INSERT INTO %s (cod_resultado, cod_prova, uid_aluno, data_inicio, data_fim, resp_certas, resp_erradas, nota_final, nivel, obs, fechada, terminou)";
$format .= "VALUES (%u, %u, %u, %s, %s, %s, %s, %u, %s, %s, %u, %u)";
$sql = sprintf($format ,
$this->db->prefix('nivelamento_resultados'),
$cod_resultado
,$cod_prova
,$uid_aluno
//,$this->db->quoteString($data_inicio)
,$now
,$now
,$this->db->quoteString($resp_certas)
,$this->db->quoteString($resp_erradas)
,$nota_final
,$this->db->quoteString($nivel)
,$this->db->quoteString($obs)
,$fechada
,$terminou
);
$force = true;
} else {
$format = "UPDATE %s SET ";
$format .="cod_resultado=%u, cod_prova=%u, uid_aluno=%u, data_inicio=%s, data_fim=%s, resp_certas=%s, resp_erradas=%s, nota_final=%u, nivel=%s, obs=%s, fechada=%u, terminou=%u";
$format .=" WHERE cod_resultado = %u";
$sql = sprintf($format, $this->db->prefix('nivelamento_resultados'),
$cod_resultado
,$cod_prova
,$uid_aluno
,$this->db->quoteString($data_inicio)
,$now
,$this->db->quoteString($resp_certas)
,$this->db->quoteString($resp_erradas)
,$nota_final
,$this->db->quoteString($nivel)
,$this->db->quoteString($obs)
,$fechada
,$terminou
,$cod_resultado);
}
if (false != $force) {
$result = $this->db->queryF($sql);
} else {
$result = $this->db->query($sql);
}
if (!$result) {
return false;
}
if (empty($cod_resultado)) {
$cod_resultado = $this->db->getInsertId();
}
$nivelamento_resultados->assignVar('cod_resultado', $cod_resultado);
return true;
}
/**
* delete a nivelamento_resultados from the database
*
* @param object $nivelamento_resultados reference to the nivelamento_resultados to delete
* @param bool $force
* @return bool FALSE if failed.
*/
function delete(&$nivelamento_resultados, $force = false)
{
if (get_class($nivelamento_resultados) != 'nivelamento_resultados') {
return false;
}
$sql = sprintf("DELETE FROM %s WHERE cod_resultado = %u", $this->db->prefix("nivelamento_resultados"), $nivelamento_resultados->getVar('cod_resultado'));
if (false != $force) {
$result = $this->db->queryF($sql);
} else {
$result = $this->db->query($sql);
}
if (!$result) {
return false;
}
return true;
}
/**
* retrieve nivelamento_resultadoss from the database
*
* @param object $criteria {@link CriteriaElement} conditions to be met
* @param bool $id_as_key use the UID as key for the array?
* @return array array of {@link nivelamento_resultados} objects
*/
function &getObjects($criteria = null, $id_as_key = false)
{
$ret = array();
$limit = $start = 0;
$sql = 'SELECT * FROM '.$this->db->prefix('nivelamento_resultados');
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
$sql .= ' '.$criteria->renderWhere();
if ($criteria->getSort() != '') {
$sql .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
}
$limit = $criteria->getLimit();
$start = $criteria->getStart();
}
$result = $this->db->query($sql, $limit, $start);
if (!$result) {
return $ret;
}
while ($myrow = $this->db->fetchArray($result)) {
$nivelamento_resultados = new nivelamento_resultados();
$nivelamento_resultados->assignVars($myrow);
if (!$id_as_key) {
$ret[] =& $nivelamento_resultados;
} else {
$ret[$myrow['cod_resultado']] =& $nivelamento_resultados;
}
unset($nivelamento_resultados);
}
return $ret;
}
/**
* count nivelamento_resultadoss matching a condition
*
* @param object $criteria {@link CriteriaElement} to match
* @return int count of nivelamento_resultadoss
*/
function getCount($criteria = null)
{
$sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('nivelamento_resultados');
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
$sql .= ' '.$criteria->renderWhere();
}
$result = $this->db->query($sql);
if (!$result) {
return 0;
}
list($count) = $this->db->fetchRow($result);
return $count;
}
/**
* delete nivelamento_resultadoss matching a set of conditions
*
* @param object $criteria {@link CriteriaElement}
* @return bool FALSE if deletion failed
*/
function deleteAll($criteria = null)
{
$sql = 'DELETE FROM '.$this->db->prefix('nivelamento_resultados');
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
$sql .= ' '.$criteria->renderWhere();
}
if (!$result = $this->db->query($sql)) {
return false;
}
return true;
}
/* cria form de edição de resultado
*
* @param string $action caminho para arquivo que ...
* @param object $nivelamento_perguntas {@link nivelamento_perguntas}
* @return bool FALSE if deletion failed
*/
function renderFormEditar($resultado,$prova,$qtd_perguntas,$action)
{
$cod_prova = $prova->getVar('cod_prova');
$titulo = $prova->getVar('titulo');
$descricao = $prova->getVar('descricao');
$instrucoes = $prova->getVar('instrucoes');
$tempo = $prova->getVar('tempo');
$cod_resultado = $resultado->getVar('cod_resultado');
$data_inicio = $resultado->getVar('data_inicio');
$data_fim = $resultado->getVar('data_fim');
$resp_certas = $resultado->getVar('resp_certas');
$resp_erradas = $resultado->getVar('resp_erradas');
$nota_final = $resultado->getVar('nota_final');
$nivel = $resultado->getVar('nivel');
$observacoes = $resultado->getVar('obs');
$qtd_acertos = count(explode(",",$resp_certas));
$qtd_erros = count(explode(",",$resp_erradas));
$texto_resp_certas = _MD_NIV_PERGDETALHES."<br />";
$vetor_resp_certas = explode(",",$resp_certas);
foreach ($vetor_resp_certas as $resp){
$detalhe_resp_certa = explode("-",$resp);
$texto_resp_certas .= "<a href=index.php?op=ver_detalhe_pergunta&cod_pergunta=".$detalhe_resp_certa[0]."&cod_resposta=".$detalhe_resp_certa[1].">".$detalhe_resp_certa[0]." </a> ";
}
$texto_resp_erradas = _MD_NIV_PERGDETALHES." <br />";
$vetor_resp_erradas = explode(",",$resp_erradas);
foreach ($vetor_resp_erradas as $resp2){
$detalhe_resp_errada = explode("-",$resp2);
$texto_resp_erradas .= "<a href=index.php?op=ver_detalhe_pergunta&cod_pergunta=".$detalhe_resp_errada[0]."&cod_resposta=".$detalhe_resp_errada[1].">".$detalhe_resp_errada[0]." </a> ";
}
if ($vetor_resp_certas[0] == ""){
$qtd_acertos = 0;
}
if ($vetor_resp_erradas[0] == ""){
$qtd_erros = 0;
}
$qtd_branco = $qtd_perguntas - $qtd_acertos - $qtd_erros;
$nota_sugest = round(100 * $qtd_acertos / $qtd_perguntas,2);
$form = new XoopsThemeForm(_MD_NIV_EDITAR." "._MD_NIV_RESULTADO, "form_resultado", $action, "post", true);
$campo_resp_certas = new XoopsFormLabel(_MD_NIV_RESPCERTAS, $texto_resp_certas);
$campo_resp_erradas = new XoopsFormLabel(_MD_NIV_RESPERR, $texto_resp_erradas);
$campo_sugest_nota_final = new XoopsFormLabel (_MD_NIV_SUGESTNOTA,$nota_sugest."/100 (". _MD_NIV_ACERTOU." ".$qtd_acertos." "._MD_NIV_ERROU." ".$qtd_erros." "._MD_NIV_SEMREPONDER." ".$qtd_branco." "._MD_NIV_DEUMTOTALDE." ".$qtd_perguntas." "._MD_NIV_PERGUNTAS." )");
$campo_nota_final = new XoopsFormText(_MD_NIV_NOTAFINAL, "campo_nota_final", 6, 10, $nota_final);
$campo_nivel = new XoopsFormText(_MD_NIV_NIVEL, "campo_nivel" , 10, 20, $nivel);
$campo_observacoes = new XoopsFormTextArea(_MD_NIV_OBS, "campo_observacoes", $observacoes , 2, 50);
$campo_cod_resultado = new XoopsFormHidden("campo_cod_resultado", $cod_resultado);
$botao_enviar = new XoopsFormButton("", "botao_submit", _MD_NIV_SALVARALTERACOES, "submit");
$form->addElement($campo_resp_certas,true);
$form->addElement($campo_resp_erradas,true);
$form->addElement($campo_sugest_nota_final);
$form->addElement($campo_nota_final,true);
$form->addElement($campo_nivel,true);
$form->addElement($campo_observacoes,true);
$form->addElement($campo_cod_resultado,true);
$form->addElement($botao_enviar);
$form->display();
return true;
}
function stats($cod_prova){
$criteria = new criteria ('cod_prova',$cod_prova);
$qtd_provas = $this->getCount($criteria);
$ret['qtd'] = $qtd_provas;
$sql = 'SELECT max(nota_final),min(nota_final),avg(nota_final) FROM '.$this->db->prefix('nivelamento_resultados');
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
$sql .= ' '.$criteria->renderWhere();
}
$result = $this->db->query($sql);
if (!$result) {
return 0;
}
while (list($max,$min,$media) = $this->db->fetchRow($result)){
$ret['max'] = $max;
$ret['min'] = $min;
$ret['media'] = $media;
}
return $ret;
}
}
?>