<?php
// $Id: nivelamento_documentos.php,v 1.10 2007/03/24 17:50:52 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_documentos.php,v 1
// ---------------------------------------------------------------- //
// //
// ----------------------------------------------------------------- //
include_once XOOPS_ROOT_PATH."/class/xoopsobject.php";
include_once XOOPS_ROOT_PATH."/class/xoopsformloader.php";
include_once XOOPS_ROOT_PATH."/class/xoopseditor/xoopseditor.php";
include_once XOOPS_ROOT_PATH."/class/xoopseditor/xoopseditor.inc.php";
//include_once XOOPS_ROOT_PATH."/class/xoopseditor/mastop_publish/formmpublishtextarea.php";
include_once XOOPS_ROOT_PATH."/Frameworks/xoops22/class/xoopsform/formselecteditor.php";
include_once XOOPS_ROOT_PATH."/Frameworks/xoops22/class/xoopsform/formeditor.php";
include_once XOOPS_ROOT_PATH."/Frameworks/art/functions.sanitizer.php";
//include_once XOOPS_ROOT_PATH."/Frameworks/xoops22/class/xoopsform/xoopsformloader.php";
include_once("nivelamento_perguntas.php");
/**
* nivelamento_documentos class.
* $this class is responsible for providing data access mechanisms to the data source
* of XOOPS user class objects.
*/
class nivelamento_documentos extends XoopsObject
{
var $db;
// constructor
function nivelamento_documentos ($id=null)
{
$this->db =& Database::getInstance();
$this->initVar("cod_documento",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("titulo",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("tipo",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("cod_prova",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("cods_perguntas",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("documento",XOBJ_DTYPE_TXTAREA, null, false);
$this->initVar("uid_elaborador",XOBJ_DTYPE_INT,null,false,10);
$this->initVar("fonte",XOBJ_DTYPE_TXTBOX, null, false);
$this->initVar("html",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_documentos").' WHERE cod_documento='.$id;
$myrow = $this->db->fetchArray($this->db->query($sql));
$this->assignVars($myrow);
if (!$myrow) {
$this->setNew();
}
}
function getAllnivelamento_documentoss($criteria=array(), $asobject=false, $sort="cod_documento", $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_documento FROM ".$db->prefix("nivelamento_documentos")."$where_query ORDER BY $sort $order";
$result = $db->query($sql,$limit,$start);
while ( $myrow = $db->fetchArray($result) ) {
$ret[] = $myrow['nivelamento_documentos_id'];
}
} else {
$sql = "SELECT * FROM ".$db->prefix("nivelamento_documentos")."$where_query ORDER BY $sort $order";
$result = $db->query($sql,$limit,$start);
while ( $myrow = $db->fetchArray($result) ) {
$ret[] = new nivelamento_documentos ($myrow);
}
}
return $ret;
}
}
// -------------------------------------------------------------------------
// ------------------nivelamento_documentos user handler class -------------------
// -------------------------------------------------------------------------
/**
* nivelamento_documentoshandler class.
* This class provides simple mecanisme for nivelamento_documentos object
*/
class Xoopsnivelamento_documentosHandler extends XoopsObjectHandler
{
/**
* create a new nivelamento_documentos
*
* @param bool $isNew flag the new objects as "new"?
* @return object nivelamento_documentos
*/
function &create($isNew = true) {
$nivelamento_documentos = new nivelamento_documentos();
if ($isNew) {
$nivelamento_documentos->setNew();
}
else{
$nivelamento_documentos->unsetNew();
}
return $nivelamento_documentos;
}
/**
* retrieve a nivelamento_documentos
*
* @param int $id of the nivelamento_documentos
* @return mixed reference to the {@link nivelamento_documentos} object, FALSE if failed
*/
function &get($id) {
$sql = 'SELECT * FROM '.$this->db->prefix('nivelamento_documentos').' WHERE cod_documento='.$id;
if (!$result = $this->db->query($sql)) {
return false;
}
$numrows = $this->db->getRowsNum($result);
if ($numrows == 1) {
$nivelamento_documentos = new nivelamento_documentos();
$nivelamento_documentos->assignVars($this->db->fetchArray($result));
return $nivelamento_documentos;
}
return false;
}
/**
* insert a new nivelamento_documentos in the database
*
* @param object $nivelamento_documentos reference to the {@link nivelamento_documentos} object
* @param bool $force
* @return bool FALSE if failed, TRUE if already present and unchanged or successful
*/
function insert(&$nivelamento_documentos, $force = false) {
Global $xoopsConfig;
if (get_class($nivelamento_documentos) != 'nivelamento_documentos') {
return false;
}
if (!$nivelamento_documentos->isDirty()) {
return true;
}
if (!$nivelamento_documentos->cleanVars()) {
return false;
}
foreach ($nivelamento_documentos->cleanVars as $k => $v) {
${$k} = $v;
}
$now = "date_add(now(), interval ".$xoopsConfig['server_TZ']." hour)";
if ($nivelamento_documentos->isNew()) {
// ajout/modification d'un nivelamento_documentos
$nivelamento_documentos = new nivelamento_documentos();
$format = "INSERT INTO %s (cod_documento, titulo, tipo, cod_prova, cods_perguntas, documento, uid_elaborador, fonte, html)";
$format .= "VALUES (%u, %s, %u, %u, %s, %s, %u, %s, %u)";
$sql = sprintf($format ,
$this->db->prefix('nivelamento_documentos'),
$cod_documento
,$this->db->quoteString($titulo)
,$tipo
,$cod_prova
,$this->db->quoteString($cods_perguntas)
,$this->db->quoteString($documento)
,$uid_elaborador
,$this->db->quoteString($fonte)
,$html
);
$force = true;
} else {
$format = "UPDATE %s SET ";
$format .="cod_documento=%u, titulo=%s, tipo=%u, cod_prova=%u, cods_perguntas=%s, documento=%s, uid_elaborador=%u, fonte=%s, html=%u";
$format .=" WHERE cod_documento = %u";
$sql = sprintf($format, $this->db->prefix('nivelamento_documentos'),
$cod_documento
,$this->db->quoteString($titulo)
,$tipo
,$cod_prova
,$this->db->quoteString($cods_perguntas)
,$this->db->quoteString($documento)
,$uid_elaborador
,$this->db->quoteString($fonte)
,$html
,$cod_documento);
}
if (false != $force) {
$result = $this->db->queryF($sql);
} else {
$result = $this->db->query($sql);
}
if (!$result) {
return false;
}
if (empty($cod_documento)) {
$cod_documento = $this->db->getInsertId();
}
$nivelamento_documentos->assignVar('cod_documento', $cod_documento);
return true;
}
/**
* delete a nivelamento_documentos from the database
*
* @param object $nivelamento_documentos reference to the nivelamento_documentos to delete
* @param bool $force
* @return bool FALSE if failed.
*/
function delete(&$nivelamento_documentos, $force = false)
{
if (get_class($nivelamento_documentos) != 'nivelamento_documentos') {
return false;
}
$sql = sprintf("DELETE FROM %s WHERE cod_documento = %u", $this->db->prefix("nivelamento_documentos"), $nivelamento_documentos->getVar('cod_documento'));
if (false != $force) {
$result = $this->db->queryF($sql);
} else {
$result = $this->db->query($sql);
}
if (!$result) {
return false;
}
return true;
}
/**
* retrieve nivelamento_documentoss 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_documentos} objects
*/
function &getObjects($criteria = null, $id_as_key = false)
{
$ret = array();
$limit = $start = 0;
$sql = 'SELECT * FROM '.$this->db->prefix('nivelamento_documentos');
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_documentos = new nivelamento_documentos();
$nivelamento_documentos->assignVars($myrow);
if (!$id_as_key) {
$ret[] =& $nivelamento_documentos;
} else {
$ret[$myrow['cod_documento']] =& $nivelamento_documentos;
}
unset($nivelamento_documentos);
}
return $ret;
}
/**
* retrieve nivelamento_documentoss 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_documentos} objects
*/
function &getDocumentosProvaPergunta($cod_prova,$cod_pergunta)
{
$criteria = new criteria('cod_prova',$cod_prova);
$cod_documentos = array();
$documentos_prova = $this->getObjects($criteria);
$i = 0;
foreach ($documentos_prova as $documento_prova){
$cods_perguntas=explode(",",$documento_prova->getVar('cods_perguntas'));
if (in_array($cod_pergunta,$cods_perguntas)){
$documentos[$i]['titulo']= $documento_prova->getVar('titulo');
$documentos[$i]['fonte']= $documento_prova->getVar('fonte');
/*if ($xoopsModuleConfig['editorpadrao']=="dhtmlext"||$xoopsModuleConfig['editorpadrao']=="textarea"){
$documentos[$i]['documento']= text_filter($documento_prova->getVar('documento',"s"),true);}
else {
$documentos[$i]['documento']= text_filter($documento_prova->getVar('documento',"n"),true);
}*/
if ($documento_prova->getVar("html")==1){
//$documentos[$i]['documento']= text_filter($documento_prova->getVar('documento',"n"),true);
$documentos[$i]['documento']= $documento_prova->getVar('documento',"n");
}else{
$documentos[$i]['documento']= text_filter($documento_prova->getVar('documento',"s"),true);
}
$i++;
}
}
return $documentos;
}
/**
* count nivelamento_documentoss matching a condition
*
* @param object $criteria {@link CriteriaElement} to match
* @return int count of nivelamento_documentoss
*/
function getCount($criteria = null)
{
$sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('nivelamento_documentos');
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_documentoss 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_documentos');
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_perguntas {@link nivelamento_perguntas}
* @return bool FALSE if deletion failed
*/
function renderFormCadastrar($action, $cod_prova)
{
global $xoopsDB, $xoopsUser;
$fabrica_de_perguntas = new Xoopsnivelamento_perguntasHandler($xoopsDB);
$criteria = new criteria("cod_prova",$cod_prova);
$vetor_perguntas = $fabrica_de_perguntas->getObjects($criteria);
$campo_perguntas = new XoopsFormSelect(_MD_NIV_PERGASSOC, "campo_perguntas", null, 10, true);
foreach ($vetor_perguntas as $pergunta){
$campo_perguntas->addOption($pergunta->getVar('cod_pergunta'),$pergunta->getVar('titulo'));
}
$form = new XoopsThemeForm(_MD_NIV_CADASTRAR." "._MD_NIV_DOCUMENTO, "form_documento", $action, "post", true);
$campo_titulo = new XoopsFormTextArea(_MD_NIV_TITULO, "campo_titulo", "" , 2, 50);
$campo_fonte = new XoopsFormText(_MD_NIV_FONTE, "campo_fonte", 35, 20);
$campo_codprova = new XoopsFormHidden('campo_codprova', $cod_prova);
$form->setExtra('enctype="multipart/form-data"');
if (!is_object($GLOBALS['xoopsModule']) || $GLOBALS['xoopsModule']->getVar('dirname') != "nivelamento") {
$modhandler = &xoops_gethandler('module');
$module = &$modhandler->getByDirname("nivelamento");
$config_handler = &xoops_gethandler('config');
$moduleConfig = &$config_handler->getConfigsByCat(0,$module->getVar('mid'));
}
else {
$moduleConfig =& $GLOBALS['xoopsModuleConfig'];
}
$editor = $moduleConfig['editorpadrao'];
// Add the editor selection box
// If dohtml is disabled, set $noHtml = true
//$form->addElement(new XoopsFormSelectEditor($form, "editor", $editor, $noHtml = false));
// options for the editor
//required configs
$options['name'] ='campo_documento';
$options['value'] = empty($_REQUEST['message'])? "" : $_REQUEST['message'];
//optional configs
$options['rows'] = 25; // default value = 5
$options['cols'] = 60; // default value = 50
$options['width'] = '100%'; // default value = 100%
$options['height'] = '400px'; // default value = 400px
// "textarea": if the selected editor with name of $editor can not be created, the editor "textarea" will be used
// if no $onFailure is set, then the first available editor will be used
// If dohtml is disabled, set $noHtml to true
$campo_documento = new XoopsFormEditor(_MD_NIV_DOCUMENTO, $editor, $options, $nohtml = false, $onfailure = "textarea");
$botao_enviar = new XoopsFormButton(_MD_NIV_CADASTRAR, "botao_submit", "cadastrar", "submit");
$form->addElement($campo_codprova);
$form->addElement($campo_titulo,true);
$form->addElement($campo_documento,true);
$form->addElement($campo_fonte,true);
$form->addElement($campo_perguntas,true);
$form->addElement($botao_enviar);
$form->display();
return true;
}
function renderFormEditar($action, $cod_documento)
{
global $xoopsDB, $xoopsUser,$xoopsModuleConfig;;
$documento = $this->get($cod_documento);
$titulo = $documento->getVar('titulo',"s");
//$textodocumento = text_filter($documento->getVar('documento',"f"),true);
$textodocumento = $documento->getVar('documento',"f");
$fonte = $documento->getVar('fonte',"s");
$uid_elaborador = $documento->getVar('uid_elaborador',"s");
$cod_prova = $documento->getVar('cod_prova',"s");
$vetor_perguntas_selecionadas = explode(",",$documento->getVar('cods_perguntas',"s"));
$fabrica_de_perguntas = new Xoopsnivelamento_perguntasHandler($xoopsDB);
$criteria = new criteria("cod_prova",$cod_prova);
$vetor_perguntas = $fabrica_de_perguntas->getObjects($criteria);
$campo_perguntas = new XoopsFormSelect("Perguntas associadas", "campo_perguntas", $vetor_perguntas_selecionadas, 10, true);
foreach ($vetor_perguntas as $pergunta){
$campo_perguntas->addOption($pergunta->getVar('cod_pergunta'),$pergunta->getVar('titulo'));
}
$form = new XoopsThemeForm(_MD_NIV_EDITAR." "._MD_NIV_DOCUMENTO, "form_documento", $action, "post", true);
$campo_titulo = new XoopsFormTextArea(_MD_NIV_TITULO, "campo_titulo", $titulo , 2, 50);
$campo_fonte = new XoopsFormText(_MD_NIV_FONTE, "campo_fonte", 35, 20, $fonte);
$campo_coddocumento = new XoopsFormHidden('campo_coddocumento', $cod_documento);
$campo_codprova = new XoopsFormHidden('campo_codprova', $cod_prova);
$form->setExtra('enctype="multipart/form-data"');
$editor = $xoopsModuleConfig['editorpadrao'];
// Add the editor selection box
// If dohtml is disabled, set $noHtml = true
//$form->addElement(new XoopsFormSelectEditor($form, "editor", $editor, $noHtml = false));
// options for the editor
//required configs
$options['name'] ='campo_documento';
$options['value'] = empty($_REQUEST['message'])? $textodocumento : $_REQUEST['message'];
//optional configs
$options['rows'] = 25; // default value = 5
$options['cols'] = 60; // default value = 50
$options['width'] = '100%'; // default value = 100%
$options['height'] = '400px'; // default value = 400px
// "textarea": if the selected editor with name of $editor can not be created, the editor "textarea" will be used
// if no $onFailure is set, then the first available editor will be used
// If dohtml is disabled, set $noHtml to true
$campo_documento = new XoopsFormEditor(_MD_NIV_DOCUMENTO, $editor, $options, $nohtml = false, $onfailure = "textarea");
$botao_enviar = new XoopsFormButton(_MD_NIV_EDITAR, "botao_submit", "cadastrar", "submit");
$form->addElement($campo_coddocumento);
$form->addElement($campo_titulo,true);
$form->addElement($campo_documento,true);
$form->addElement($campo_fonte,true);
$form->addElement($campo_perguntas,true);
$form->addElement($campo_codprova);
$form->addElement($botao_enviar);
$form->display();
return true;
}
/**
* Copia os documentos e salva eles ligados à prova clone
*
* @param object $criteria {@link CriteriaElement} to match
* @return int count of nivelamento_perguntass
*/
function clonarDocumentos($criteria, $cod_prova)
{
global $xoopsDB;
$documentos = $this->getObjects($criteria);
foreach ($documentos as $documento){
$documento->setVar("cod_prova",$cod_prova);
$documento->setVar("cod_documento",0);
$documento->setNew();
$this->insert($documento);
}
}
}
?>