<?php

/**
 * class adultQuest
 *
 *  A template to get you started building templates.  Rename all adultQuest
 *  with the name of your plugin
 *
 *
 *
 */
include_once(MODOSDATE_DIR . 'modPlugin.php');

class adultQuest extends modPlugin {

   /**
   * Holds the language phrases
   *
   * @access private
   */
	var $lang=array();
   /**
   * The name name of the plugin class.
   *
   * @access private
   */
   var $plugin_class_name = "adultQuest";

   /**
   * The text that appears in the admin plugin list
   *
   * @access private
   */
   var $display_name ;

   /**
   * User ID
   *
   * @access private
   */
   var $uid;

   /**
   * The link text that appears on the user's menu
   *
   * @access private
   */
   var $user_menu_text ;

   /**
   * Appear on users menu (true or false)
   *
   * @access private
   */
   var $user_menu_appear = true;

   /**
   * Questionnaire Pages table
   *
   * @access private
   */
   var $lang_aqpages_table="adultQuest_pages";

   /**
   * Answers table
   *
   * @access private
   */
   var $lang_aqanswers_table="adultQuest_answers";

   /**
   * The link text that appears on the admin's menu
   *
   * @access private
   */
   var $admin_menu_text ;

   /**
   * Appear on admin's menu (true or false)
   *
   * @access private
   */
   var $admin_menu_appear = true;
   /**
   * Constructor
   *
   * @return
   * @access public
   */
  function adultQuest()
  {

    $this->modPlugin();
	$pluginDir = dirname(__FILE__).'/../';
	$this->lang = $this->modLoadLang($pluginDir);
  	$this->user_menu_text=$this->modGetLang('user_title');
  	$this->display_name = $this->admin_menu_text=$this->modGetLang('admin_title');
  } // end of member method pluginClass

  function showPage($pid)
  {
  	$tname=$this->modGetOne($this->lang_aqpages_table,array('pid'=>$pid),'tname');
  	$datax =& $this->modGetAll($this->lang_aqanswers_table,array('pid'=>$pid,'uid'=>$this->uid,"qid"));
  	for($i=0;$i<=$datax[count($datax)-1]['qid'];$i++)
  		$data[$i][0]=0;
  	foreach ($datax as $item)
  		$data[$item['qid']][++$data[$item['qid']][0]]=$item;
  	$this->modSmartyAssign('pid',$pid);
  	$this->modSmartyAssign('data',$data);
  	unset($data, $datax);
  	return $this->modSmartyFetch($tname);
  }

  function showFirstPage()
  {
  	  $data =& $this->modGetAll($this->lang_aqpages_table,array(),"ord");
      $i=0;

      foreach ($data as $item)
      {
      	$pid=$item['pid'];
      	$qnumber=$item['qnumber'];
      	$ans=array();
      	for($i=0;$i<$qnumber;$i++)
      	{
      		$answer =& $this->modGetAll($this->lang_aqanswers_table,array('pid'=>$pid,'uid'=>$this->uid,'qid'=>($i+1)));
      		foreach($answer as $item2)
      		{
      			$ans[$i]['answer'].="; ".$item2['answer'];
      		}
			unset($answer);
      		$ans[$i]['answer']=nl2br(htmlspecialchars(substr($ans[$i]['answer'],1)));
      		$ans[$i]['page']="q$pid";
      	}
      	$this->modSmartyAssign('pid',$pid);
      	$this->modSmartyAssign('ans',$ans);
      	$text.=$this->modSmartyFetch("showpage.tpl");
      }
	  unset($data, $ans);
      return $text;
  }

   /**
   * Does the processing to display a user page.  Called from plugin.php
   *
   * @return array
   * @access public
   */
   function  displayPluginPage() {

      $this->modSmartyAssign('lang', $this->modGetLang() );
   	  $this->modSmartyAssign('plugin', $this->plugin_class_name );
   	  $udata =& $this->modGetLoggedInUser();
	  $this->uid=$udata['id'];

      if(isset($_GET['showpage']))
      {
      	$pid=$_GET['showpage'];
      	if(isset($_POST['edit'])) {
      		$this->modDeleteRows($this->lang_aqanswers_table,array('pid'=>$pid,'uid'=>$this->uid));
      		$max=$this->modGetOne($this->lang_aqpages_table,array('pid'=>$pid),'qnumber');
      		for($i=1;$i<=$max;$i++)
      		{
      			$ans=$_POST['q'.$i];
      			if($ans=="checkbox"){
      				for($j=1;$j<=99;$j++)
      				{
      					$ans=$_POST['q'.$i.'_'.$j];
      					if($ans)
      							$this->modAddRec($this->lang_aqanswers_table,array('qid'=>$i,'answer'=>$ans,'pid'=>$pid,'uid'=>$this->uid));
      				}
      			}
      			else
      				$this->modAddRec($this->lang_aqanswers_table,array('qid'=>$i,'answer'=>$ans,'pid'=>$pid,'uid'=>$this->uid));
      		}
      		header("Location: ?plugin=$this->plugin_class_name#p$pid");
      	}
      	else
      		$text=$this->showPage($pid);
      }
      else
      {
		$text=$this->showFirstPage();
      }


	  $text = $this->modSmartyFetch("hdr.tpl").$text;
      return $text;

   }
   /**
   * WARNING: USER IS NOT VALIDATED HERE.  BE CAREFUL
   * Does the processing to display 100% plugin content.  Called from pluginraw.php
   *
   * @return array
   * @access public
   */
   function  displayPluginContent() {

   }
    /**
   * Returns the content that will appear in the left column of a page.  Designed to be overridden by plugins
   *
   * @return array
   * @access public
   */
  function displayLeftCol() {



  }
    /**
   * Returns the content that will appear in the main content area of the page.  This content will appear after the existing main content.  Designed to be overridden by plugins
   *
   * @return array
   * @access public
   */
  function displayMain() {

    }
  /**
   * Does the processing to display a admin page.  Called from plugin.php
   *
   * @return array
   * @access public
   */
   function  displayPluginAdminPage() {

   	$this->modSmartyAssign('lang',$this->modGetLang());
   	$this->modSmartyAssign('plugin', $this->plugin_class_name );

   	$do=$_GET['do'];

   	if ($do=="moveup")
	{
		$page=$_GET['page'];
		$ord=$this->modGetOne($this->lang_aqpages_table,array('pid'=>$page),'ord');
		$ord2=$ord;
		$ord = $ord - 1;
		$page2=$this->modGetOne($this->lang_aqpages_table,array('ord'=>$ord),'pid');
		if($ord)
		{
			$this->modEditRec($this->lang_aqpages_table,array('ord'=>$ord),array('pid'=>$page));
			$this->modEditRec($this->lang_aqpages_table,array('ord'=>$ord2),array('pid'=>$page2));
		}
	}

	if ($do=="movedown")
	{
		$page=$_GET['page'];
		$ord=$this->modGetOne($this->lang_aqpages_table,array('pid'=>$page),'ord');
		$ord++;
		$ord2=$ord-1;
		$page2=$this->modGetOne($this->lang_aqpages_table,array('ord'=>$ord),'pid');
		$max=$this->modGetOne($this->lang_aqpages_table,array(),'count(*)');
		if($ord<=$max)
		{
			$this->modEditRec($this->lang_aqpages_table,array('ord'=>$ord),array('pid'=>$page));
			$this->modEditRec($this->lang_aqpages_table,array('ord'=>$ord2),array('pid'=>$page2));
		}
	}

   	$data =& $this->modGetAll($this->lang_aqpages_table,array(),'ord');
   	$this->modSmartyAssign('data',$data);
   	unset($data);
   	return $this->modSmartyFetch("admin/showpages.tpl");
   }
}


?>