Link to home
Start Free TrialLog in
Avatar of TonyReba
TonyRebaFlag for United States of America

asked on

php dynamic page

Hi folks, I am working on modifying a Joomla CMS component called j quarks, it does almost all what I need for my quiz system to work, however, I intend to enhance the component and at the end of the quiz (if a score of 100%) , have the user print out a certificate of completion with his name , quiz name , and date quiz taken .  I have never programmed anything big on php, I undertand the code, but I am more a Asp.net person..  Is there any php guru here that can help me building this page, or at least point me into the right direction.   This are the files I have .
The first one (form.php)  is a form that user fills out once he completes the quiz, and ask for its name and department  ->

next step is the quiz result pages which I have included a link to a new page  . I added to it a link page to a certificate php page that I need to populate

How can I pass the values from the form to this page?
default.php  (results page)


<?php defined( '_JEXEC' ) or die( 'Restricted access' ); ?>

<?php
    
    $url = JRoute::_("components/com_jquarks/assets/stylesheets/jquarks_style.css");
    $document =& JFactory::getDocument();
    $document->addHeadLink($url, "stylesheet", "rel");
?>
<style type="text/css">
#errormsg p {
	color: #F00;
}
</style>


<div class="componentheading">
	<?php echo JText::_('Quiz Results'); ?>
</div>

<!-- Rick Modified ........ Old line 11 was echo JText::_('SESSION_RESULTS'); ?> -->


<!-- Score and answers status -->
<?php
    $score = $this->session->score;
    $totalScore = $this->totalQuestions - $this->totalInput + $this->inputEvaluated;
    $percent = $score * 100 / $totalScore;
?>

<!-- Rick Modified  Redirect if less than 100 %---><br />
<div class="contentheading">
<p>
     <span class="session-title"><?php echo JText::_('QUIZ').': ' ;?></span>
    <?php echo $this->session->title; ?>
 </p>
 <p>
     <span class="session-title"><?php echo JText::_('SCORE').': ' ;?></span>
    <?php echo  round($percent, 2) . '%&nbsp;&nbsp;&nbsp;'; ?>
     <span class="note-small">
          <?php echo JText::_('INPUT_QUESTIONS_ARE_NOT_RECORDED_IN_THE_SCORE'); ?>
     </span>
 </p>
 <p>
     <span class="session-title"><?php echo JText::_('IS_UNANSWERED').': ' ; ?></span>
    <?php echo $this->session->unanswered; ?>
 </p>
</div>
 
 <div id="errormsg">
 <!-- Rick added Line to show 100 percent is required to pass test --> 
 
 </br>
  <p>
  
  
  <?php
if( $percent >=100 )
{ 
echo " Congratulations, you passed this course \n";
echo '<br>' ;
echo " Print this page and turn in to your supervisor";
} 

else 
{
    echo "A score of 100 is required to complete this course.
	Please click the link below to re-take the test !!.";
	echo '<br>' ;
	echo '</br>';
    echo '<a href="index.php?option=com_jquarks&view=quiz&id=1&Itemid=2">Re-take test</a>.'; 
}
 ?>



  <a href="certificate.php" title="Sexual Harassment Certificate of Completion" target="_blank">Print Certificate</a>  
</div>

<!-- Answers details -->
<br />



<div >

<?php

     //defining format for evaluated propositions
    $li      = '<li>';
    $eli     = '</li>';
    $ligreen = '<li class="green-li">';
    $corr    = '<span class="li-highlight-green">  ('  . JText::_('GOOD')    . ')</span>';
    $incorr  = '<span class="li-highlight-red">  ('    . JText::_('WRONG')   . ')</span>';
    $omi     = '<span class="li-highlight-orange">  (' . JText::_('OMITTED') . ')</span>';


     $idq = NULL; // contains the actual question id
     foreach ($this->results as $res) :

        // show question statement if this latter doesn't exist
        $idq_temp = $res->question_id;
        if ($idq_temp != $idq)
        {
            $idq = $idq_temp;
            if ($idq != NULL) {
                echo '</ul>';
            }
            echo '<br /><span class="session-title">' . $res->statement . '</span>';
            echo '<ul class="result-list">';
        }

        switch($res->type_id) :
                case 1: // input question
                    echo $li . $res->altanswer. $eli;
                    break;

                case 2: // option question
                    if ($res->answer_id != NULL) // exists in answers
                    {
                        if($res->correct) {
                            echo $ligreen . $res->answer . $corr . $eli; //correct
                        }
                        else {
                            echo $li . $res->answer . $incorr . $eli;  // incorrect
                        }

                    }
                    else {
                        if ($res->correct) {
                            echo $ligreen . $res->answer . $eli;
                        }
                        else {
                            echo $li . $res->answer . $eli;
                        }
                    }
                    break;

                 case 3: //checkbox question
                 case 4: // option as checkbox
                          if($res->correct) {
                              $debut = $ligreen . $res->answer;
                          }
                          else {
                              $debut = $li      . $res->answer;
                          }
                          if (is_null($res->status)) {
                              $fin = $eli;
                          }
                          else
                          {
                              switch($res->status)
                              {
                                  case -2: $fin = $eli;           break;
                                  case  0: $fin = $incorr . $eli; break;
                                  case  1: $fin = $corr   . $eli; break;
                                  case  2: $fin = $omi    . $eli; break;
                              }
                          }
                          echo $debut.$fin;
                
         endswitch;
    endforeach;
?>
</div>

Open in new window

form.php

<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
JHTML::_('behavior.mootools');
?>
<script type="text/javascript" language="javascript">

	function validEmail(email) 
	{
		pattern = new RegExp("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$") ;

		return pattern.test(email) ;
	} 

	function checkForm() 
	{
		lastname = document.getElementById('lastname').value ;
		firstname = document.getElementById('firstname').value ;
		email = document.getElementById('email').value ;
		
		if (lastname != '' && firstname != '' && validEmail(email)) {
			return true ;
		} 
		else 
		{
			alert("<?php echo JText::_('ERROR_FILL_REQUIRED_FIELDS_PRVOIDE_VALIDE_EMAIL') ; ?>") ;
			return false ;
		}
	}

</script>

<p style="font-style: normal; font-size: 16px;font-weight: bold; color: #333;"><?php echo JText::_('Thank you for taking this quiz') ; ?></p>
</br>
<form method="post" onsubmit="return checkForm();">
  <table>
		<tr>
			<td><?php echo JText::_('First Name') ; ?> :</td>
			<td><input type="text" id="firstname" name="firstname" size="15" /> *</td>
		</tr>
		<tr>
			<td><?php echo JText::_('Last Name') ; ?> :</td>
			<td><input type="text" id="lastname" name="lastname" size="15" /> *</td>
		</tr>
        
                
		<tr>
			<td><?php echo JText::_('EMAIL') ; ?> :</td>
			<td><input type="text" id="email" name="email" size="30"/> *</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td></tr>
		
  </table>
<input type="hidden" name="option"     value="com_jquarks" />
	<input type="hidden" name="controller" value="who" />
	<input type="hidden" name="task"       value="store" />
	<input type="hidden" name="sessionId"  value="<?php echo $this->sessionId ; ?>"/>
	<?php echo JHTML::_( 'form.token' ); ?>
  <input type="reset" value="<?php echo JText::_('RESET') ;?>" />
  <input type="submit" value="<?php echo ($this->isShowResults)? JText::_('SEND_AND_VIEW_RESULTS') : JText::_('SEND'); ?>"/>
</form>

Open in new window

SOLUTION
Avatar of Erdinç Güngör Çorbacı
Erdinç Güngör Çorbacı
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TonyReba

ASKER

If I remove this , the data obtain from quiz does not get inserted on the database

<input type="hidden" name="option"     value="com_jquarks" />
      <input type="hidden" name="controller" value="who" />
      <input type="hidden" name="task"       value="store" />
      <input type="hidden" name="sessionId"  value="<?php echo $this->sessionId ; ?>"/>
      <?php echo JHTML::_( 'form.token' ); ?>
  <input type="reset" value="<?php echo JText::_('RESET') ;?>" />
  <input type="submit" value="<?php echo ($this->isShowResults)? JText::_('SEND_AND_VIEW_RESULTS') : JText::_('SEND'); ?>"/>
</form>
if you just want to check before inserting to the db AJAX is better i think.
and indeed you dont need to remove that part just carry the values needed to the second page.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial