<?php
// $Id: nivelamento_perguntas.php,v 1.10 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 //
// ------------------------------------------------------------------------ //
// nivelamento_perguntas.php,v 1
// ---------------------------------------------------------------- //
// Author: Marcello Brandao //
// ----------------------------------------------------------------- //
include_once XOOPS_ROOT_PATH."/class/xoopsobject.php";
include_once XOOPS_ROOT_PATH."/class/xoopsformloader.php";
//include_once("../class/nivelamento_respostas.php");
/**
* nivelamento_perguntas class.
* $this class is responsible for providing data access mechanisms to the data source
* of XOOPS user class objects.
*/
class nivelamento_perguntas extends XoopsObject
{
var $db;
// constructor
function nivelamento_perguntas ($id=null)
{
$this->db =& Database::getInstance();
$this->initVar("cod_pergunta",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("cod_prova",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("titulo",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("data_criacao",XOBJ_DTYPE_TXTBOX,null,false);
$this->initVar("data_update",XOBJ_DTYPE_TXTBOX,null,false);
$this->initVar("uid_elaborador",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("ordem",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_perguntas").' WHERE cod_pergunta='.$id;
$myrow = $this->db->fetchArray($this->db->query($sql));
$this->assignVars($myrow);
if (!$myrow) {
$this->setNew();
}
}
function getAllnivelamento_perguntass($criteria=array(), $asobject=false, $sort="cod_pergunta", $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_pergunta FROM ".$db->prefix("nivelamento_perguntas")."$where_query ORDER BY $sort $order";
$result = $db->query($sql,$limit,$start);
while ( $myrow = $db->fetchArray($result) ) {
$ret[] = $myrow['nivelamento_perguntas_id'];
}
} else {
$sql = "SELECT * FROM ".$db->prefix("nivelamento_perguntas")."$where_query ORDER BY $sort $order";
$result = $db->query($sql,$limit,$start);
while ( $myrow = $db->fetchArray($result) ) {
$ret[] = new nivelamento_perguntas ($myrow);
}
}
return $ret;
}
}
// -------------------------------------------------------------------------
// ------------------nivelamento_perguntas user handler class -------------------
// -------------------------------------------------------------------------
/**
* nivelamento_perguntashandler class.
* This class provides simple mecanisme for nivelamento_perguntas object
*/
class Xoopsnivelamento_perguntasHandler extends XoopsObjectHandler
{
/**
* create a new nivelamento_perguntas
*
* @param bool $isNew flag the new objects as "new"?
* @return object nivelamento_perguntas
*/
function &create($isNew = true) {
$nivelamento_perguntas = new nivelamento_perguntas();
if ($isNew) {
$nivelamento_perguntas->setNew();
}
//hack consertando
else {
$nivelamento_perguntas->unsetNew();
}
//fim do hack para consertar
return $nivelamento_perguntas;
}
/**
* retrieve a nivelamento_perguntas
*
* @param int $id of the nivelamento_perguntas
* @return mixed reference to the {@link nivelamento_perguntas} object, FALSE if failed
*/
function &get($id) {
$sql = 'SELECT * FROM '.$this->db->prefix('nivelamento_perguntas').' WHERE cod_pergunta='.$id;
if (!$result = $this->db->query($sql)) {
return false;
}
$numrows = $this->db->getRowsNum($result);
if ($numrows == 1) {
$nivelamento_perguntas = new nivelamento_perguntas();
$nivelamento_perguntas->assignVars($this->db->fetchArray($result));
return $nivelamento_perguntas;
}
return false;
}
/**
* insert a new nivelamento_perguntas in the database
*
* @param object $nivelamento_perguntas reference to the {@link nivelamento_perguntas} object
* @param bool $force
* @return bool FALSE if failed, TRUE if already present and unchanged or successful
*/
function insert(&$nivelamento_perguntas, $force = false) {
Global $xoopsConfig;
if (get_class($nivelamento_perguntas) != 'nivelamento_perguntas') {
return false;
}
if (!$nivelamento_perguntas->isDirty()) {
return true;
}
if (!$nivelamento_perguntas->cleanVars()) {
return false;
}
foreach ($nivelamento_perguntas->cleanVars as $k => $v) {
${$k} = $v;
}
$now = "date_add(now(), interval ".$xoopsConfig['server_TZ']." hour)";
if ($nivelamento_perguntas->isNew()) {
// ajout/modification d'un nivelamento_perguntas
$nivelamento_perguntas = new nivelamento_perguntas();
$format = "INSERT INTO %s (cod_pergunta, cod_prova, titulo, data_criacao, data_update, uid_elaborador,ordem)";
$format .= "VALUES (%u, %u, %s, %s, %s, %s, %u)";
$sql = sprintf($format ,
$this->db->prefix('nivelamento_perguntas'),
$cod_pergunta
,$cod_prova
,$this->db->quoteString($titulo)
,$now
,$now
,$this->db->quoteString($uid_elaborador)
,$ordem
);
$force = true;
} else {
$format = "UPDATE %s SET ";
$format .="cod_pergunta=%u, cod_prova=%u, titulo=%s, data_criacao=%s, data_update=%s, uid_elaborador=%s, ordem=%u";
$format .=" WHERE cod_pergunta = %u";
$sql = sprintf($format, $this->db->prefix('nivelamento_perguntas'),
$cod_pergunta
,$cod_prova
,$this->db->quoteString($titulo)
,$now
,$now
,$this->db->quoteString($uid_elaborador)
,$ordem
, $cod_pergunta);
}
if (false != $force) {
$result = $this->db->queryF($sql);
} else {
$result = $this->db->query($sql);
}
if (!$result) {
return false;
}
if (empty($cod_pergunta)) {
$cod_pergunta = $this->db->getInsertId();
}
$nivelamento_perguntas->assignVar('cod_pergunta', $cod_pergunta);
return true;
}
/**
* delete a nivelamento_perguntas from the database
*
* @param object $nivelamento_perguntas reference to the nivelamento_perguntas to delete
* @param bool $force
* @return bool FALSE if failed.
*/
function delete(&$nivelamento_perguntas, $force = false)
{
if (get_class($nivelamento_perguntas) != 'nivelamento_perguntas') {
return false;
}
$sql = sprintf("DELETE FROM %s WHERE cod_pergunta = %u", $this->db->prefix("nivelamento_perguntas"), $nivelamento_perguntas->getVar('cod_pergunta'));
if (false != $force) {
$result = $this->db->queryF($sql);
} else {
$result = $this->db->query($sql);
}
if (!$result) {
return false;
}
return true;
}
/**
* retrieve nivelamento_perguntass 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_perguntas} objects
*/
function &getObjects($criteria = null, $id_as_key = false)
{
$ret = array();
$limit = $start = 0;
$sql = 'SELECT * FROM '.$this->db->prefix('nivelamento_perguntas');
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_perguntas = new nivelamento_perguntas();
$nivelamento_perguntas->assignVars($myrow);
if (!$id_as_key) {
$ret[] =& $nivelamento_perguntas;
} else {
$ret[$myrow['cod_pergunta']] =& $nivelamento_perguntas;
}
unset($nivelamento_perguntas);
}
return $ret;
}
/**
* retrieve nivelamento_perguntass 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_perguntas} objects
*/
function &getCodObjects($criteria = null, $id_as_key = false)
{
$ret = array();
$limit = $start = 0;
$sql = 'SELECT cod_pergunta FROM '.$this->db->prefix('nivelamento_perguntas');
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);
while ($myrow = $this->db->fetchArray($result)) {
$ret[]=$myrow['cod_pergunta'];
}
return $ret;
}
/**
* count nivelamento_perguntass matching a condition
*
* @param object $criteria {@link CriteriaElement} to match
* @return int count of nivelamento_perguntass
*/
function getCount($criteria = null)
{
$sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('nivelamento_perguntas');
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_perguntass 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_perguntas');
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
$sql .= ' '.$criteria->renderWhere();
}
if (!$result = $this->db->query($sql)) {
return false;
}
return true;
}
/**
* cria form de inserção e edição de pergunta
*
* @param string $action caminho para arquivo que ...
* @param object $nivelamento_prova {@link nivelamento_pprova}
* @return bool FALSE if failed
*/
function renderFormCadastrar($action,$prova=null){
$form = new XoopsThemeForm(_MD_NIV_CADASTRAR." "._MD_NIV_PERGUNTA, "form_pergunta", $action, "post", true);
$campo_titulo = new XoopsFormTextArea(_MD_NIV_TITULO, "campo_titulo", "" , 2, 50);
$campo_ordem = new XoopsFormText(_MD_NIV_ORDEM, "campo_ordem", 3, 3, "0");
$cod_prova = $prova->getVar("cod_prova");
$titulo_prova = $prova->getVar("titulo");
$campo_prova_label = new XoopsFormLabel(_MD_NIV_PROVA, $titulo_prova);
$campo_prova_valor = new XoopsFormHidden("campo_cod_prova", $cod_prova);
$campo_resposta1 = new XoopsFormTextArea(_MD_NIV_RESPOSTA." 1 <br />(correta)", "campo_resposta1", "" , 2, 50 );
$campo_resposta1->setExtra ('style="background-color:#ECFFEC"');
$campo_resposta2 = new XoopsFormTextArea(_MD_NIV_RESPOSTA." 2 - (errada)", "campo_resposta2", "" , 2, 50);
$campo_resposta2->setExtra ('style="background-color:#FFF0F0"');
$campo_resposta3 = new XoopsFormTextArea(_MD_NIV_RESPOSTA." 3 - (errada)", "campo_resposta3", "" , 2, 50);
$campo_resposta3->setExtra ('style="background-color:#FFF0F0"');
$campo_resposta4 = new XoopsFormTextArea(_MD_NIV_RESPOSTA." 4 - (errada)", "campo_resposta4", "" , 2, 50);
$campo_resposta4->setExtra ('style="background-color:#FFF0F0"');
$campo_resposta5 = new XoopsFormTextArea(_MD_NIV_RESPOSTA." 5 - (errada)", "campo_resposta5", "" , 2, 50);
$campo_resposta5->setExtra ('style="background-color:#FFF0F0"');
$botao_enviar = new XoopsFormButton(_MD_NIV_CADASTRAR, "botao_submit", "cadastrar", "submit");
$form->addElement($campo_prova_label);
$form->addElement($campo_prova_valor);
$form->addElement($campo_ordem,true);
$form->addElement($campo_titulo,true);
$form->addElement($campo_resposta1,true);
$form->addElement($campo_resposta2,true);
$form->addElement($campo_resposta3,true);
$form->addElement($campo_resposta4,true);
$form->addElement($campo_resposta5,true);
$form->addElement($botao_enviar);
$form->display();
return true;
}
function renderFormEditar($action,$pergunta,$respostas=array()){
$cod_prova = $pergunta->getVar('cod_prova');
$titulo = $pergunta->getVar('titulo');
$cod_pergunta = $pergunta->getVar('cod_pergunta');
$ordem = $pergunta->getVar('ordem');
$form = new XoopsThemeForm(_MD_NIV_EDITAR." "._MD_NIV_PERGUNTA, "form_pergunta", $action, "post", true);
$campo_ordem = new XoopsFormText(_MD_NIV_ORDEM, "campo_ordem", 3, 3, $ordem);
$form->addElement($campo_ordem,true);
$campo_titulo = new XoopsFormTextArea(_MD_NIV_PERGUNTA, "campo_titulo", $titulo , 2, 50);
$form->addElement($campo_titulo,true);
$botao_enviar = new XoopsFormButton("", "botao_submit", _MD_NIV_SALVARALTERACOES, "submit");
$campo_cod_pergunta = new XoopsFormHidden("campo_cod_pergunta", $cod_pergunta);
$i=1;
foreach ($respostas as $resposta){
$titulo_resposta = $resposta->getVar("titulo");
$cod_resposta = $resposta->getVar("cod_resposta");
$nome_campo_titulo_resposta = "campo_resposta".$i;
if ($resposta->getVar("iscerta")==1){
$resposta_correta = new XoopsFormTextArea(_MD_NIV_RESPCORRETA.$i, $nome_campo_titulo_resposta, $titulo_resposta , 2, 50);
$resposta_correta->setExtra ('style="background-color:#ECFFEC"');
$cod_resposta_correta = new XoopsFormHidden("campo_cod_resp1",$cod_resposta);
$form->addElement($cod_resposta_correta,true);
$form->addElement($resposta_correta,true);
}
else{
$vetor_respostas_erradas[$i] = new XoopsFormTextArea(_MD_NIV_RESPOSTA.$i, $nome_campo_titulo_resposta, $titulo_resposta , 2, 50);
$vetor_respostas_erradas[$i]->setExtra ('style="background-color:#FFF0F0"');
$vetor_cod_respostas_erradas[$i] = new XoopsFormHidden("campo_cod_resp".$i,$cod_resposta);
$form->addElement($vetor_respostas_erradas[$i],true);
$form->addElement($vetor_cod_respostas_erradas[$i],true);
}
$i++;
}
$form->addElement($campo_prova_valor,true);
$form->addElement($campo_cod_pergunta,true);
$form->addElement($botao_enviar);
$form->display();
return true;
}
function renderFormResponder($action,$pergunta,$respostas=array(),$param_cod_resposta=0){
global $_GET;
$start = $_GET['start'];
$cod_prova = $pergunta->getVar('cod_prova');
$titulo = $pergunta->getVar('titulo');
$cod_pergunta = $pergunta->getVar('cod_pergunta');
$form = new XoopsThemeForm("$titulo", "form_resposta", $action, "post", true);
$botao_enviar = new XoopsFormButton("", "botao_submit", _SUBMIT, "submit");
$campo_cod_pergunta = new XoopsFormHidden("cod_pergunta", $cod_pergunta);
$campo_start = new XoopsFormHidden("start", $start);
$campo_respostas = new XoopsFormRadio(_MD_NIV_RESPOSTA,"cod_resposta");
shuffle($respostas);
$campo_respostas->setValue($param_cod_resposta);
foreach ($respostas as $resposta){
$titulo_resposta = $resposta->getVar("titulo");
$cod_resposta = $resposta->getVar("cod_resposta");
$campo_respostas->addOption($cod_resposta, $titulo_resposta."<br />");
}
//$form->addElement($campo_prova_valor,true);
$form->addElement($campo_cod_pergunta);
$form->addElement($campo_respostas,true);
$form->addElement($campo_start);
$form->addElement($botao_enviar);
//$form->display();
return $form;
}
function pegarultimocodigo(&$db)
{
return $db->getInsertId();
}
/**
* Copia as perguntas e salva elas ligadas à prova clone
*
* @param object $criteria {@link CriteriaElement} to match
* @return int count of nivelamento_perguntass
*/
function clonarPerguntas($criteria, $cod_prova)
{
global $xoopsDB;
$fabrica_de_respostas = new Xoopsnivelamento_respostasHandler($xoopsDB);
$perguntas = $this->getObjects($criteria);
foreach ($perguntas as $pergunta){
$cod_pergunta = $pergunta->getVar('cod_pergunta');
$pergunta->setVar("cod_prova",$cod_prova);
$pergunta->setVar("cod_pergunta",0);
$pergunta->setNew();
$this->insert($pergunta);
$cod_pergunta_clone = $xoopsDB->getInsertId();
$criteria_pergunta = new Criteria('cod_pergunta',$cod_pergunta);
$respostas = $fabrica_de_respostas->getObjects($criteria_pergunta);
foreach ($respostas as $resposta){
$resposta->setVar("cod_pergunta",$cod_pergunta_clone);
$resposta->setVar("cod_resposta",0);
$resposta->setNew();
$fabrica_de_respostas->insert($resposta);
}
}
}
}
?>