Please help with few lines of code and share your idea

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webbiesindia
Forum Newbie
Posts: 3
Joined: Sun Jun 28, 2015 12:52 am

Please help with few lines of code and share your idea

Post by webbiesindia »

I have an online exam based php script. Right now the situation is like this. We can create different levels in the php based software. Suppose I create Level 1 and Level 2. In each level I created 3 tests. eg. Test 1.1, 1.2, 1.3 and 2.1, 2.2, 2.3. Right now the candidate is able to take test only one by one, but I want them to take Test 1.1 under Level 1 and thus continuously he should be able to take Test 1.2 and 1.3.

What I meant was see, as soon as Test 1.1 finishes I want the candidate to take 1.2 and 1.3. How can I do that?

This is the working of the quiz code. (In the quiz, see Next Question is there and at last question Submit and See Result will come. Now again the quiz(test 1.1) should redirect to the next test. I will share results code next)

Code: Select all

<?php
  if (!defined("_VALID_PHP"))
      die('Direct access to this location is not allowed.');
?>

<?php if(!$user->userlevel == 1): print Filter::msgInfo(lang('ADMINONLY'), false); return; endif;?>


<?php
if(isset($_SESSION['qn']) && isset($_SESSION['eid'])){

	$exam = Core::getRowById("exams", $_SESSION['eid']);
	if(strtotime($_SESSION['quizend']) - strtotime(date("H:i:s")) <= 0){
		redirect_to("account.php?do=result");
	}else{

		
		if(isset($_POST['doQuize'])){

			  $data = array(
				'uid' => intval($_POST['uid']), 
				'token' => $_SESSION['token'], 
				'qid' => intval($_POST['qid']), 
				'type' => intval($_POST['type']), 
				'exam' => intval($_SESSION['eid']),
				'description' => $_POST['description'],
				'marks' => $_POST['marks']
			  );

			  $lastid = $db->insert("tquestions", $data);
			  
			  $answer = $_POST['answer'];
			  $correct = $_POST['correct'];
			  
			  if($_POST['type'] == 1 || $_POST['type'] == 3){
				  if(!empty($_POST['marked'])):
					$marked = $_POST['marked'];	
					  foreach($marked as $a => $b){
						$data = array(
							'tquestion' => intval($lastid), 
							'uid' => intval($_POST['uid']),
							'answer' => $answer[$a],
							'correct' => $correct[$a],
							'marked' => $marked[$a]
						);
						$db->insert("tanswers", $data);
					  }
				  endif;
			  } else if ($_POST['type'] == 2){
					
					$marked = intval($_POST['marked']);
					
					if($correct[$marked] == 1){
						$cor = 1; $mar = 1;
					}else{
						$cor = 4; $mar = $marked;
					}
					
					
					$data = array(
							'tquestion' => intval($lastid), 
							'uid' => intval($_POST['uid']),
							'answer' => $answer[$marked],
							'correct' => $cor,
							'marked' => $mar
					);
					$db->insert("tanswers", $data);
			  
			  } else if ($_POST['type'] == 4){
					$data = array(
							'tquestion' => intval($lastid), 
							'uid' => intval($_POST['uid']),
							'answer' => $_POST['answer'],
							'correct' => $_POST['correct'],
							'marked' => $_POST['marked']
					);
					$db->insert("tanswers", $data);
			  }
		}
	
		if($_SESSION['total'] >= 1){
			$qrow = $user->loadQuestion($_SESSION['eid'],$_SESSION['qn']);
			
			$_SESSION['total'] = $_SESSION['total'] - 1;
			$_SESSION['qn'] = $_SESSION['qn'] + 1;
			
			$remaining = (strtotime ($_SESSION['quizend']) - strtotime (date("H:i:s")));
			$mremaining = $remaining * 1000;
		}else{
			redirect_to("account.php?do=result");
		}
		?>
		
		<div class="row xform greentip">
			<section class="col col-9" style="margin-bottom: 0;">
				<?php echo '<h1>Exam: ' . $exam->title . '</h1>'; ?>
				<?php echo '<h3>Duration: ' . $exam->duration . ' minutes</h3>'; ?>
				<?php echo '<h3>Marks: ' . $user->sumMarks($exam->id) . '</h3>'; ?>
				<?php echo '<h3>Total Question: ' . $user->totalQues($exam->id) . '</h3>'; ?>
			</section>
			<section class="col col-3" style="margin-bottom: 0;">
				<div id="timer" style="float:right;">
					<script type="application/javascript">
					var myCountdownTest = new Countdown({
						time: <?php echo $remaining; ?>, 
						width:200, 
						height:80, 
						rangeHi:"minute"
					});
					</script>
					<script>
					setTimeout(function() {
						document.location.href='account.php?do=result';
					}, <?php echo $mremaining; ?>);
					</script>
				</div>
			</section>
		</div>
		<div id="msgresult"></div>
		<form id="ques_form" class="xform" method="post">
			<div class="row" id="ques_area">
				<section class="col col-12">
					<?php $description = $qrow->description; ?>
					<h2>Question-<?php echo $_SESSION['qn'] . " (" . $qrow->marks . "Marks )" ;?></h2>
					<h3><?php echo "$description";?></h3>
				
					<?php $arow = $user->loadAnswer($qrow->id);?>
					<input type="hidden" name="uid" value="<?php echo $user->uid;?>" />
					<input type="hidden" name="qid" value="<?php echo $qrow->id;?>" />
					<input type="hidden" name="type" value="<?php echo $qrow->type;?>" />
					<input type="hidden" name="description" value="<?php echo $qrow->description;?>" />
					<input type="hidden" name="marks" value="<?php echo $qrow->marks;?>" />
					<input name="doQuize" type="hidden" value="1" />

					<?php $k = 1; $l = 0; ?>
					<?php foreach ($arow as $answers):?>
					
						<?php if($qrow->type == 1){?>
							<label class="radio">
							<input type='radio' name='marked[0]' value='1' /><i></i><?php echo $k; ?>) True</label>
							<label class="radio">
							<input type='radio' name='marked[0]' value='0' /><i></i><?php echo $k+1; ?>) False</label>
							<input type="hidden" name="correct[]" value="<?php echo $answers->correct;?>" />
							<input type="hidden" name="answer[]" value="<?php echo $answers->answer;?>" />
						<?php } else if($qrow->type == 2){?>
							<label class="radio">
							<input type='radio' name='marked' value='<?php echo $l; ?>' /><i></i><?php echo $k; ?>) <?php echo $answers->answer; ?></label>
							<input type="hidden" name="correct[<?php echo $l; ?>]" value="<?php echo $answers->correct;?>" />
							<input type="hidden" name="answer[<?php echo $l; ?>]" value="<?php echo $answers->answer;?>" />
						<?php } else if($qrow->type == 3){?>
							<label class="radio">
							<input type='checkbox' name='marked[<?php echo $l; ?>]' value='1' /><i></i><?php echo $k; ?>) <?php echo $answers->answer; ?></label>
							<input type="hidden" name="correct[]" value="<?php echo $answers->correct;?>" />
							<input type="hidden" name="answer[]" value="<?php echo $answers->answer;?>" />
						<?php } else if($qrow->type == 4){?>
							<label class="input">
								<input type="text" name="marked">
							</label>
							
							<input type="hidden" name="correct" value="<?php echo $answers->answer;?>" />
							<input type="hidden" name="answer" value="<?php echo $answers->answer;?>" />
						<?php } ?>
						
						<?php $k++; $l++; ?>
					<?php endforeach;?>
					<?php unset($answers);?>
				</section>
			</div>			
			<footer><?php if($_SESSION['total'] >= 1){ echo '<button class="button" name="dosubmit" type="submit">Next Question</button>';}else{ echo 'This is the last question. <button class="button" name="dosubmit" type="submit">Submit & See Result</button>';} ?></footer>
			
			
			
		</form>		
		<?php
	}
}else{
	redirect_to("courses.php");
}
?>



Result page code (See right now, what I did is I put an option,
Take your next exam by clicking here
option. But this just goes back to exams page, where the candidate see test 1.1, 1.2, 1.3. But they should be connected.)

Code: Select all

<?php
  if (!defined("_VALID_PHP"))
      die('Direct access to this location is not allowed.');
?>

<?php if(!$user->userlevel == 1): print Filter::msgInfo(lang('ADMINONLY'), false); return; endif;?>
<?php $exam = Core::getRowById("exams", $_SESSION['eid']); ?>
<?php 
$totalscore = 0;
$pass = $core->passing_score;
$tQuestions = $user->tQuestions($_SESSION['token'], $_SESSION['eid']);
foreach ($tQuestions as $ques):
	$totalans = $user->totalAnswers($ques->qid);
	$newscore = $user->tAnswers($ques->id, $totalans, $ques->marks);
	$totalscore = $totalscore + $newscore;
endforeach;
unset($ques);
?>

<section class="widget">
  <div class="result">
	<h1>Thank You for taking the test</h1>
	<?php $pscore = round($totalscore / $_SESSION['fullmarks'] * 100 , 2); ?>
       
	<br><br><br><br>
	<p><a href="account.php?do=review" class=""></a> <a href="#" class=""></a></p><br><br> <a href="exams.php"
	class="button button-secondary">Take your next exam by clicking here</a></p><br><br>
  </div>
</section>

<?php
if($pscore >= 60)
{$remarks = 1;}
else{$remarks = 0;}

$totalans = $user->processResult($user->uid,$_SESSION['eid'],$_SESSION['token'],$_SESSION['fullmarks'],$_SESSION['duration'],$totalscore,$pscore,$remarks);
?>

Now, listen I'm sharing the code of exams page.

Code: Select all

<?php
	define("_VALID_PHP", true); 
	require_once("init.php"); 
?>
<?php include("header.php"); ?>
<?php switch(Filter::$do): case "details": ?>
<?php $examsinfo = Core::getRowById("exams", Filter::$id); ?>

<div class="row">
	<section class="xform">
		<h1>Exam Name: <?php echo $examsinfo->title; ?></h1>
		<h2>Total Question: <?php echo $user->totalQues(Filter::$id); ?></h2>
		<h2>Duration: <?php echo $examsinfo->duration; ?></h2><br/>
		<p>Syllabus: <?php echo $examsinfo->syllabus; ?></p>
		
		<?php if ($user->logged_in): ?>
		<?php $trow = $user->checkTaken($user->uid,Filter::$id);?>
		<?php if ($trow){?>
			<?php $rrow = $user->getResult($user->uid,Filter::$id);?>
			<p class="greentip"><i class="icon-lightbulb icon-2x pull-left"></i>You have already taken this exam </p>
			<a href="account.php?do=start&id=<?php echo Filter::$id;?>" class=""></a>
		<?php } else { ?>
			<a href="account.php?do=start&id=<?php echo Filter::$id;?>" class="button">Take This Exam</a>
		<?php } ?>
		<?php endif; ?>
		
	</section>
</div>



<?php break; ?>
<?php default: ?>
<?php $projectrow = $content->getExams();?>
<?php $ptype = $content->getProjectTypes();?>
<?php 
if(isset($_GET['sort']) && !empty($_GET['sort'])){
$courseinfo = Core::getRowById("project_types", $_GET['sort']);
?>
<div class="pagetip">
	<h1>Course Name: <?php echo $courseinfo->title;?></h1>
	<p>Description: <?php echo $courseinfo->description;?></p><br>

	<?php if($courseinfo->fees <= 0){ ?>
		<h2>FREE</h2>
	<?php } else { ?>
		<h2>Fees: $<?php echo $courseinfo->fees;?> USD<?php if($courseinfo->recurring == 1){ echo '/' . $courseinfo->days . $courseinfo->period; } ?></h2>
	<?php } ?>

	<?php if ($user->logged_in){ ?>
		
		
		<?php $erow = $user->checkEnrol($user->uid,$courseinfo->id);?>
		<?php if ($erow){?>
			<p class="greentip"><i class="icon-lightbulb icon-2x pull-left"></i>You have already enrolled in this course. You can take any exams.</p>
		<?php } else { ?>	
			<a href="account.php?do=enrolment&id=<?php echo $courseinfo->id;?>" class="button exam-enrol">Enrol Now</a>
		<?php } ?>			
		
	<?php } ?>

</div>
<?php } else { ?>
<div class="row heading-row">
	<h1>Available Exams</h1>
</div>
<?php } ?>

<section class="widget">
  <div class="content2">
    <div class="row">
      <div class="ptop10">
        <form class="xform" id="dForm" method="post" style="padding:0;padding-top:15px">
          <section class="col col-8">
            <select name="select" id="coursefilter">
              <option value="">-- Select Course --</option>
              <?php if ($ptype):?>
			  <?php foreach ($ptype as $prow):?>
              <option value="<?php echo $prow->id;?>"<?php if($prow->id == get('sort')) echo 'selected="selected"';?>><?php echo $prow->title;?></option>
              <?php endforeach;?>
              <?php unset($prow);?>
              <?php endif;?>
            </select>
          </section>
		  
		  
          <section class="col col-2"> <?php echo $pager->items_per_page();?> </section>
          <section class="col col-2"> <?php echo $pager->jump_menu();?> </section>
        </form>
      </div>
    </div>
    <?php if(!$projectrow):?>
    <?php echo Filter::msgInfo(lang('PROJ_NOPROJECT'),false);?>
    <?php else:?>
    <table class="myTable">
      <thead>
        <tr>
          <th class="header">Exam Title</th>
          <th class="header">Course Title</th>
          <th class="header">Duration</th>
          <th class="header"><?php echo lang('ACTIONS');?></th>
        </tr>
      </thead>
      <?php foreach ($projectrow as $row):?>
      <tr>
        <td><?php echo $row->title;?></td>
        <td><a href="exams.php?sort=<?php echo $row->course;?>"><?php echo $row->ctitle;?></a></td>
        <td><?php echo $row->duration;?> (minutes)</td>
        <td>
		  <a href="exams.php?do=details&id=<?php echo $row->id;?>" class="button" data-title="<?php echo 'Add Question to: '.$row->title;?>">Start Here</a>
      </tr>
      <?php endforeach;?>
      <?php unset($row);?>
    </table>
    <?php if($pager->display_pages()):?>
    <?php echo $pager->display_pages();?>
    <?php endif;?>
    <?php endif;?>
  </div>
</section>

<script type="text/javascript"> 
// <![CDATA[  
$(document).ready(function () {
    $('#coursefilter').change(function () {
		var res = $("#coursefilter option:selected").val();
		(res == "NA" ) ? window.location.href = 'exams.php' : window.location.href = 'exams.php?sort=' + res;
    })
});
// ]]>
</script>
<?php break;?>
<?php endswitch;?>
<?php include("footer.php");?>

Now please help me to keep the test continuous. Please.
Post Reply