To theme quiz take page, I create page--node--take.tpl.php

Question body, I get from

$question_node = node_load($question_nid);
$question_node->body[und][0][value];

Alternative choice, I get from

$formmultichoice_elements = drupal_get_form("quiz_question_answering_form", $question_node, $_SESSION['quiz'][$node_quiz->nid]['result_id']);

foreach ($formmultichoice_elements[question][$question_nid][question][body]['#object']->alternatives as $choice):

endforeach

This is working for the one page per one question.

But when I try on QUIZ page (which contain multiple questions in one page), it show error

EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7930

Could you please advise how to solve this?
Are there any better way to theme Quiz take page?

Thank you

Comments

Supakorn created an issue. See original summary.

Supakorn’s picture

Issue summary: View changes
djdevin’s picture

Component: Code - Quiz core » User interface
Category: Bug report » Support request

You'll have to paste your whole code to get help.

Remember that Quiz pages contain an array of questions so you'll have to handle that otherwise you will likely get errors.

Supakorn’s picture

Hello djdevin,

This is my code.
I have updated my code which (I think) it is more properly.

Now, the error message has changed to below.... ( but single question quiz page still works)

Fatal error: Call to a member function getLayout() on boolean in ......\sites\all\modules\quiz\question_types\quiz_question\quiz_question.module on line 150

global $user;
global $base_url;
$curr_path = explode("/",current_path());
$quiz_nid = $curr_path[1];
$weight = $curr_path[3];
$node_quiz = node_load($quiz_nid);
$paragraph = $node_quiz->body[und][0][value];

$question_build = quiz_build_question_list($node_quiz);

$question_node = node_load($question_build[$weight]['nid']);
$question_nid = $question_node->nid;
$question = $question_node->body[und][0][value];
$question_summary = $question_node->body[und][0][summary];

$current_quiz_result_id = $_SESSION['quiz'][$node_quiz->nid]['result_id'];
$formmultichoice_elements = drupal_get_form("quiz_question_answering_form", $question_node, $current_quiz_result_id);

<?php
	foreach ($formmultichoice_elements[question][$question_nid][question][body]['#object']->alternatives as $choice):
?>
                      <div class="form-check" style="cursor:pointer;"
					  onclick="document.getElementById('edit-question-<?php echo $question_nid?>-answer-user-answer-<?php echo $choice[id] ?>').checked = true";
					  >
                        
                          <input id="edit-question-<?php echo $question_nid?>-answer-user-answer-<?php echo $choice[id] ?>" type="radio" name="question[<?php echo $question_nid?>][answer][user_answer]" value="<?php echo $choice[id] ?>" class="form-check-input">
						  <?php echo $choice[answer][value]?>
                        
                      </div>
<?php 
	endforeach
?>
Supakorn’s picture

Hi djdevin,

I have solved it. But I am not sure this is the best solution or not??

this is my whole code.

global $user;
global $base_url;
$curr_path = explode("/",current_path());
$quiz_nid = $curr_path[1];
$weight = $curr_path[3];
$node_quiz = node_load($quiz_nid);
$paragraph = $node_quiz->body[und][0][value];

$question_build = quiz_build_question_list($node_quiz);

$qution_node = node_load($question_build[$weight]['nid']);

//$current_quiz_result_id = $_SESSION['quiz'][$node_quiz->nid]['result_id'];
$resultidid = _quiz_active_result_id($user->uid, $node_quiz->nid, $node_quiz->vid, $now = NULL);

$_SESSION['quiz'][$node_quiz->nid]['result_id'] = $resultidid;
// this is the problem last time that show error. Because this function _quiz_show_finish_button() use $_SESSION['quiz'][$node_quiz->nid]['result_id'] which quiz page does not have value. So, I have to find the value for it.

$formmultichoice_elements = drupal_get_form("quiz_question_answering_form", $question_node, $resultidid);

?>
<?php if ($question_build[1]['type']!="quiz_page"){
	
$question_nid = $question_node->nid;
$question = $question_node->body[und][0][value];
$question_summary = $question_node->body[und][0][summary];	
?>
<form action="<?php echo $formmultichoice_elements['#action'];?>" method="post" id="quiz-question-answering-form" accept-charset="UTF-8">
	<div class="container content">
            <div class="row" style="margin-top:20px;">
              <div class="col-sm-8">
                <h1 class="titleTest"><?php echo $node_quiz->title?></h1>
				
              </div>
			  <div class="col-sm-4" align="right">
	<span id="minutes"></span>:<span id="seconds" style="margin-right:20px;"></span>		  
	<a href="<?php echo $base_url?>/quiz-review?quiz_id=<?php echo $node_quiz->nid;?>&vid=<?php echo $node_quiz->vid; ?>&weight=<?php echo $weight;?>">
	<button type="button" class="btn btn-primary" id="edit-navigation-back" name="op" value="Back" style="cursor:pointer;">Review</button></a>		  
			  
<?php 
	  if ($weight < $question_qty){
?>	

<?php 	if ($weight > 1){ ?>
	<button type="submit" class="btn btn-primary" id="edit-navigation-back" name="op" value="Back" style="cursor:pointer;">Back</button>
<?php	}?>

	<button type="submit" class="btn btn-primary" id="edit-navigation-skip" name="op" value="Leave blank" style="cursor:pointer; display:none">Leave blank</button>
	
	<button type="button" class="btn btn-primary" id="edit-navigation-submit" name="op" value="Next" style="cursor:pointer;" 
	onclick="
if($('input:radio').is(':checked')){
	document.getElementById('quiz-question-answering-form').submit();
 }else{
	document.getElementById('edit-navigation-skip').click();
 }
	
	"
	>Next</button>


<?php } ?>

<?php if ($weight == $question_qty){ ?>
<?php 	if ($weight > 1){ ?>
	<button type="submit" class="btn btn-primary" id="edit-navigation-back" name="op" value="Back" style="cursor:pointer;">Back</button>
<?php	}?>

	<button type="submit" class="btn btn-primary" id="edit-navigation-skip" name="op" value="Leave blank and finish" style="cursor:pointer; display:none">Leave blank and finish</button>
	
	<button type="button" class="btn btn-primary" id="edit-navigation-submit" name="op" value="Finish" style="cursor:pointer;"
	onclick="
if($('input:radio').is(':checked')){
	document.getElementById('quiz-question-answering-form').submit();
 }else{
	document.getElementById('edit-navigation-skip').click();
 }	
	"
	>Finish</button>
	
<?php } ?>
              </div>
            </div>
	  <div class="row" style="margin-bottom:10px;">
        <div class="col-lg-12 mb-12">
          <div class="card h-100">
            <div align="center"><h6 class="card-header">Choose the option that best answers the question.</h6></div>
            <div class="row card-body col-md-12" style="margin-left:0px; margin-right:0px;">
				<div class="col-md-6 card-text">
					<div>
						<label><?php echo $question;?></label>
					</div>
					<div>
					
<?php
	foreach ($formmultichoice_elements[question][$question_nid][question][body]['#object']->alternatives as $choice):
?>
                      <div class="form-check" style="cursor:pointer;"
					  onclick="document.getElementById('edit-question-<?php echo $question_nid?>-answer-user-answer-<?php echo $choice[id] ?>').checked = true";
					  >
                        
                          <input id="edit-question-<?php echo $question_nid?>-answer-user-answer-<?php echo $choice[id] ?>" type="radio" name="question[<?php echo $question_nid?>][answer][user_answer]" value="<?php echo $choice[id] ?>" class="form-check-input">
						  <?php echo $choice[answer][value]?>
                        
                      </div>
<?php 
	endforeach
?>					
					</div>
				</div>
				<div class="col-md-6 card-text" >
					<div class="scroll">
					<?php echo $question_summary;?>
					</div>
				</div>
            </div>
          </div>
        </div>
      </div>	
	</div>
	<input type="hidden" name="form_build_id" value="<?php echo $formmultichoice_elements['#build_id'];?>" />
	<input type="hidden" name="form_token" value="<?php echo $formmultichoice_elements[form_token]['#default_value'];?>" />
	<input type="hidden" name="form_id" value="<?php echo $formmultichoice_elements[form_id]['#value']?>" />	
</form>
<?php }else{ 
$qnr_pid = $question_build[$weight]['qnr_id'];


?>

<form action="<?php echo $formmultichoice_elements['#action'];?>" method="post" id="quiz-question-answering-form" accept-charset="UTF-8">
<button type="button" class="btn btn-primary" id="edit-navigation-submit" name="op" value="Next" style="cursor:pointer;" 
	onclick="

	document.getElementById('quiz-question-answering-form').submit();

	
	"
	>Next</button>
<?php
foreach ($question_build as $question_object):
	if($question_object[qnr_pid]!=$qnr_pid){
       continue;
    }
	 //echo $question_object[nid];
	 $question_nid = $question_object[nid];
	 $question_node_page = node_load($question_object[nid]);
	 $formmultichoice_elements_question_quizpage = drupal_get_form("quiz_question_answering_form", $question_node_page, $resultidid);
	 $question = $question_node_page->body[und][0][value];
?>

	<label><?php echo $question;?></label>

<?php
		foreach ($formmultichoice_elements_question_quizpage[question][$question_object[nid]][question][body]['#object']->alternatives as $choice):
?>
                      <div class="form-check" style="cursor:pointer;"
					  onclick="document.getElementById('edit-question-<?php echo $question_nid?>-answer-user-answer-<?php echo $choice[id] ?>').checked = true";
					  >
                        
                          <input id="edit-question-<?php echo $question_nid?>-answer-user-answer-<?php echo $choice[id] ?>" type="radio" name="question[<?php echo $question_nid?>][answer][user_answer]" value="<?php echo $choice[id] ?>" class="form-check-input">
						  <?php echo $choice[answer][value]?>
                        
                      </div>

<?php
	endforeach
?>
<?php
	
endforeach
?>
	<input type="hidden" name="form_build_id" value="<?php echo $formmultichoice_elements['#build_id'];?>" />
	<input type="hidden" name="form_token" value="<?php echo $formmultichoice_elements[form_token]['#default_value'];?>" />
	<input type="hidden" name="form_id" value="<?php echo $formmultichoice_elements[form_id]['#value']?>" />
</form>
djdevin’s picture

I'm not a themer but that is likely the wrong approach.

You are overriding the entire form which means it likely won't survive updates to the module, and might be insecure as you're also overriding all the input HTML that Drupal generates. That might break a lot of Form API stuff to.

Supakorn’s picture

So, what is the best way to theme page node take. Or which function should be use in the page?

could you please advise.

steinmb’s picture

Status: Active » Fixed

I see nothing in there that is specific to this module. In general the tpl. files should not contain logic and large chunk of PHP -code.

https://www.drupal.org/docs/7/theming as a starting point or any drupal front en book. Use template_preprocess or a hook alter that allow you to only change the bits you need.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.