Drupal_get_form displaying 'Array' - php

I am working in Drupal 7, and had to make an edit to a pre-existing module. The module was set to display a form if a certain variable was set to true. I had to add 3 additional variables, and now instead of showing the form, I see 'Array'. I didn't change anything other than the logic in the if/else to determine if the form should be pulled.
I found a similar problem, that was never answered here ( https://stackoverflow.com/questions/10130535/drupal-7-renderdrupal-get-formform-not-working )
My code is as such:
<?php
/**
* #todo Please document this function.
* #see http://drupal.org/node/1354
*/
function du_r2t4_block_confirm_view() {
$provider = du_r2t4_get_provider();
//drupal_set_message("<pre>provider: ".var_export($provider,TRUE)."</pre>");
$term = $provider->getCurrentTerm();
//drupal_set_message("<pre>term: ".var_export($term,TRUE)."</pre>");
if(!$term) return du_r2t4_get_msg_wrapped("no_term");
global $user;
$person = $provider->getPerson($user->name, ($term ? $term->code : NULL));
//$person->termCode = NULL;
//$person->satisfied = TRUE;
//$person->satisfied = FALSE;
//$person->satisfied = NULL;
//drupal_set_message("<pre>person: ".var_export($person,TRUE)."</pre>");
$enrollment = $provider->getEnrollment($person->username, $term->code);
//$enrollment = array();
//drupal_set_message("<pre>enrollment: ".var_export($enrollment,TRUE)."</pre>");
$registered = $term->code && $term->code == $person->termCode && !empty($enrollment);
if(!$registered) return du_r2t4_get_msg_wrapped("no_reg");
$content = "";
if($person->satisfied === TRUE) $content .= du_r2t4_get_msg_wrapped("satisfied");
elseif($person->satisfied === FALSE) $content .= du_r2t4_get_msg_wrapped("required");
else $content .= du_r2t4_get_msg_wrapped("unknown");
$confirmed = $provider->getLastConfirmation($person);
if($confirmed) $content .= du_r2t4_get_msg_wrapped("confirmed");
/*
* TODO move details to form builder
*/
$content .= "<table style=\"width:auto;\"><tbody style=\"border:0;\">";
$content .= "<tr><td valign=\"top\">Name:</td><td valign=\"top\">{$person->firstName} {$person->lastName}</td></tr>";
$content .= "<tr><td valign=\"top\">Id:</td><td valign=\"top\">{$person->identity}</td></tr>";
$content .= "<tr><td valign=\"top\">Term:</td><td valign=\"top\">{$term->description}</td></tr>";
$content .= "<tr><td valign=\"top\">Classes:</td><td valign=\"top\">";
/**
* Session 2
*/
$se2 = FALSE;
$s4e = FALSE;
$s4m = FALSE;
$s4l = FALSE;
/**
*
* Session 2
*
*/
foreach ($enrollment as $k => $v) {
switch ($v->sessionCode){
case "SE2":
$se2 = TRUE;
$content .= $v->crn ." ". $v->subjectCode.$v->courseNumber ." ".$v->courseTitle ." (" . $v->sessionDescription.")<br />";
break;
case "S4E":
$s4e = TRUE;
$content .= $v->crn ." ". $v->subjectCode.$v->courseNumber ." ".$v->courseTitle ." (" . $v->sessionDescription.")<br />";
break;
case "S4M":
$s4m = TRUE;
$content .= $v->crn ." ". $v->subjectCode.$v->courseNumber ." ".$v->courseTitle ." (" . $v->sessionDescription.")<br />";
break;
case "S4L":
$s4l = TRUE;
$content .= $v->crn ." ". $v->subjectCode.$v->courseNumber ." ".$v->courseTitle ." (" . $v->sessionDescription.")<br />";
break;
default:
$content .= "You're not enrolled in any classes.";
}
}
$content .= "</td></tr>";
$content .= "</table>";
//if($_SESSION["du_r2t4_success"]) $content .= "<p>You have intent to attend confirmation has been submitted.</p>";
//else $content .= drupal_get_form("du_r2t4_form_confirm",$term,$person);
$content .= "<p>".du_r2t4_get_form_confirm_precontent()."</p>";
//if(!$se2 || !$s4e || !$s4m || !$s4l || $_SESSION["du_r2t4_success"]) $_SESSION["du_r2t4_success"] = false;
//else $content .= drupal_get_form("du_r2t4_form_confirm",$term,$person,$enrollment);
//
if ($se2 || $s4e || $s4m || $s4l || !$_SESSION["du_r2t4_success"])
{
$content .= drupal_get_form("du_r2t4_form_confirm",$term,$person,$enrollment);
}
else
{
$_SESSION["du_r2t4_success"] = false;
}
//$content .= drupal_get_form("du_r2t4_form_confirm",$term,$person);
return $content;
}
The section that isn't working correctly is:
if ($se2 || $s4e || $s4m || $s4l || !$_SESSION["du_r2t4_success"])
{
$content .= drupal_get_form("du_r2t4_form_confirm",$term,$person,$enrollment);
}
else
{
$_SESSION["du_r2t4_success"] = false;
}
Any help is much appreciated!

All drupal_get_form does is turn your form array into something renderable. If you want your form to be displayed as HTML, you should try something like:
<?php
$form = drupal_get_form(...);
$content .= drupal_render($form);
?>

Related

moddle change in table summary of exam

I use Moodle 3.9.1+ . I want to change summary exam table to div to be able to show the each question and its situation beside each-other like below. I want to have each 5 question in a row in fact. As I know it's not possible to do so with table and because of that I want to use div to be able to do so with css.
I found the file /mod/quiz/renderer.php has a function with below code that makes the summary exam table.
public function summary_table($attemptobj, $displayoptions) {
// Prepare the summary table header.
$table = new html_table();
$table->attributes['class'] = 'generaltable quizsummaryofattempt boxaligncenter';
$table->head = array(get_string('question', 'quiz'), get_string('status', 'quiz'));
$table->align = array('left', 'left');
$table->size = array('', '');
$markscolumn = $displayoptions->marks >= question_display_options::MARK_AND_MAX;
if ($markscolumn) {
$table->head[] = get_string('marks', 'quiz');
$table->align[] = 'left';
$table->size[] = '';
}
$tablewidth = count($table->align);
$table->data = array();
// Get the summary info for each question.
$slots = $attemptobj->get_slots();
foreach ($slots as $slot) {
// Add a section headings if we need one here.
$heading = $attemptobj->get_heading_before_slot($slot);
if ($heading) {
$cell = new html_table_cell(format_string($heading));
$cell->header = true;
$cell->colspan = $tablewidth;
$table->data[] = array($cell);
$table->rowclasses[] = 'quizsummaryheading';
}
// Don't display information items.
if (!$attemptobj->is_real_question($slot)) {
continue;
}
// Real question, show it.
$flag = '';
if ($attemptobj->is_question_flagged($slot)) {
// Quiz has custom JS manipulating these image tags - so we can't use the pix_icon method here.
$flag = html_writer::empty_tag('img', array('src' => $this->image_url('i/flagged'),
'alt' => get_string('flagged', 'question'), 'class' => 'questionflag icon-post'));
}
if ($attemptobj->can_navigate_to($slot)) {
$row = array(html_writer::link($attemptobj->attempt_url($slot),
$attemptobj->get_question_number($slot) . $flag),
$attemptobj->get_question_status($slot, $displayoptions->correctness));
} else {
$row = array($attemptobj->get_question_number($slot) . $flag,
$attemptobj->get_question_status($slot, $displayoptions->correctness));
}
if ($markscolumn) {
$row[] = $attemptobj->get_question_mark($slot);
}
$table->data[] = $row;
$table->rowclasses[] = 'quizsummary' . $slot . ' ' . $attemptobj->get_question_state_class(
$slot, $displayoptions->correctness);
}
// Print the summary table.
$output = html_writer::table($table);
return $output;
}
Can anyone help me to change this code and show desired information in div format?
I could at last solve the problem but it may not be very professional:
* Create the summary page
*
* #param quiz_attempt $attemptobj
* #param mod_quiz_display_options $displayoptions
*/
public function summary_page($attemptobj, $displayoptions) {
$output = '';
$output .= $this->header();
$output .= $this->heading(format_string($attemptobj->get_quiz_name()));
$output .= $this->heading(get_string('summaryofattempt', 'quiz'), 3);
$output .= $this->summary_table($attemptobj, $displayoptions);
$output .= $this->summary_page_controls($attemptobj);
$output .= $this->footer();
return $output;
}
/**
* Generates the table of summarydata
* sara
* #param quiz_attempt $attemptobj
* #param mod_quiz_display_options $displayoptions
*/
public function summary_table($attemptobj, $displayoptions) {
// Prepare the summary table header.
$table = new html_table();
$table->attributes['class'] = 'generaltable quizsummaryofattempt boxaligncenter';
$table->head = array(get_string('question', 'quiz'), get_string('status', 'quiz'));
$table->align = array('left', 'left');
$table->size = array('', '');
$markscolumn = $displayoptions->marks >= question_display_options::MARK_AND_MAX;
if ($markscolumn) {
$table->head[] = get_string('marks', 'quiz');
$table->align[] = 'left';
$table->size[] = '';
}
$tablewidth = count($table->align);
$table->data = array();
// Get the summary info for each question.
$slots = $attemptobj->get_slots();
foreach ($slots as $slot) {
// Add a section headings if we need one here.
$heading = $attemptobj->get_heading_before_slot($slot);
if ($heading) {
$cell = new html_table_cell(format_string($heading));
$cell->header = true;
$cell->colspan = $tablewidth;
$table->data[] = array($cell);
$table->rowclasses[] = 'quizsummaryheading';
}
// Don't display information items.
if (!$attemptobj->is_real_question($slot)) {
continue;
}
// Real question, show it.
$flag = '';
if ($attemptobj->is_question_flagged($slot)) {
// Quiz has custom JS manipulating these image tags - so we can't use the pix_icon method here.
$flag = html_writer::empty_tag('img', array('src' => $this->image_url('i/flagged'),
'alt' => get_string('flagged', 'question'), 'class' => 'questionflag icon-post'));
}
if ($attemptobj->can_navigate_to($slot)) {
$row = array(html_writer::link($attemptobj->attempt_url($slot),
$attemptobj->get_question_number($slot) . $flag),
$attemptobj->get_question_status($slot, $displayoptions->correctness));
} else {
$row = array($attemptobj->get_question_number($slot) . $flag,
$attemptobj->get_question_status($slot, $displayoptions->correctness));
}
if ($markscolumn) {
$row[] = $attemptobj->get_question_mark($slot);
}
$table->data[] = $row;
$table->rowclasses[] = 'quizsummary' . $slot . ' ' . $attemptobj->get_question_state_class(
$slot, $displayoptions->correctness);
$counter=0;
foreach($row as $r)
{
if($counter ==0)
{
$output .= html_writer::start_tag('div', array('class' => 'qsummary'.' '.'questionNo' .' q'. $slot. ' '.$attemptobj->get_question_status($slot, $displayoptions->correctness)));
$output .= $r;
$output .= html_writer::end_tag('div');
$counter=1;
}
else{
if($counter ==1)
{
$output .= html_writer::start_tag('div', array('class' => 'qsummary' .' '.'questionStat'.' q'. $slot. ' '.$attemptobj->get_question_status($slot, $displayoptions->correctness)));
$output .= $r;
$output .= html_writer::end_tag('div');
$counter=1;
}
}
}
}
// Print the summary table.
// $output = html_writer::table($table);
return $output;
}

idssphp duping array in a foreach

OK...
your answer is fine #Random, however it's still duping the ids, so i integrated your code to my dev code... so here's my final code....
$gr = mysql_query("select id from permission_sets");
$permission_arr = array(
'start_perms' => isset($_POST['START_'.$id]),
'reply_perms' => isset($_POST['REPLY_'.$id]),
'read_perms' => isset($_POST['READ_'.$id]),
'upload_perms' => isset($_POST['UPLOAD_'.$id]),
'show_perms' => isset($_POST['SHOW_'.$id]),
'download_perms' => isset($_POST['DOWNLOAD_'.$id]));
// saving the beginning of the line : "group: 1,2,3"
$header = 'PID: ';
$read = "";
$reply = "";
$start = "";
$upload = "";
$download = "";
$show = "";
$subPIDs = "";
while($row = mysql_fetch_array($gr)){
foreach($row as $pid){
//-----------------------------
// Get perm id and set it..
//-----------------------------
$pid = $pid.",";
if($permission_arr['start_perms'] == 1){
$start .= $pid;
$subPIDs .= $start;
}
if($permission_arr['read_perms'] == 1){
$read .= $pid;
$subPIDs .= $read;
}
if($permission_arr['reply_perms'] == 1){
$reply .= $pid;
$subPIDs .= $reply;
}
if($permission_arr['upload_perms'] == 1){
$upload .= $pid;
$subPIDs .= $upload;
}
if($permission_arr['download_perms'] == 1){
$download .= $pid;
$subPIDs .= $download;
}
if($permission_arr['show_perms'] == 1){
$show .= $pid;
$subPIDs .= $show;
}
$header .= $subPIDs;
}
} // End while
//------------------------------------
// Store the data in a new array..
//------------------------------------
$perm_arr = array(
'start_perms' => $start,
'reply_perms' => $reply,
'read_perms' => $read,
'upload_perms' => $upload,
'show_perms' => $show,
'download_perms' => $download);
$head = $header.' = ';
foreach($perm_arr as $key => $val){
if($val){
$head .= $key.'<br>'.$head;
}
}
print $head;
echo '<pre>';
print_r($perm_arr);
this does everything yours does... we just need to stop dupes in the output, in the array is no dupe, only in output... also when i mark only 1 box ihe array gets all or no ids...
try this :
// list of permissions with boolean
$permission_arr = array(
'start_perms' => isset($_POST['START_'.$id]),
'reply_perms' => isset($_POST['REPLY_'.$id]),
'read_perms' => isset($_POST['READ_'.$id]),
'upload_perms' => isset($_POST['UPLOAD_'.$id]),
'show_perms' => isset($_POST['SHOW_'.$id]),
'download_perms' => isset($_POST['DOWNLOAD_'.$id]));
echo 'sql';
$gr = mysql_query("select id from permission_sets");
// saving the beginning of the line : "group: 1,2,3"
$header = 'group: ';
$separator = '';
while($row = mysql_fetch_array($gr)) {
foreach($row as $g){
$header .= $separator.$g;
$separator = ',';
}
}
$header = $header.' = ';
foreach($permission_arr as $k => $p){
if($p) {
// the permission is OK, we print this permission
// output like "group: 1,2,3 = start_perms, <br>"
print $header.$k.", <br>";
}
}

Hot to feed php function

Hi I am try to display icecat product data and I am a complete php newbie, I have tracked down a code snippet that I believe should do the job, It says that It requires that you feed the function with an EAN product number.
Could someone please give me an example of what this means as I have no idea, here is a EAN Product number EAN/UPC code:0885909481842
Here is the snippet code I am trying to use.
function getICEcatProductSpecs($ean, $drawdescription = 0, $drawpicture = 0)
{
// Username and password for usage with ICEcat
$username = "Your ICEcat username goes here";
$password = "Your ICEcat password goes here";
// Return 0 and exit function if no EAN available
if($ean == null)
{
return 0;
}
// Get the product specifications in XML format
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode($username.":".$password)
)
));
$data = file_get_contents('http://data.icecat.biz/xml_s3/xml_server3.cgi?ean_upc='.$ean.';lang=nl;output=productxml', false, $context);
$xml = new SimpleXMLElement($data);
// Create arrays of item elements from the XML feed
$productPicture = $xml->xpath("//Product");
$productDescription = $xml->xpath("//ProductDescription");
$categories = $xml->xpath("//CategoryFeatureGroup");
$spec_items = $xml->xpath("//ProductFeature");
//Draw product specifications table if any specs available for the product
if($spec_items != null)
{
$categoryList = array();
foreach($categories as $categoryitem) {
$catId = intval($categoryitem->attributes());
$titleXML = new SimpleXMLElement($categoryitem->asXML());
$title = $titleXML->xpath("//Name");
$catName = $title[0]->attributes();
//echo $catId . $catName['Value']. "<br />";
$categoryList[$catId] = $catName['Value'];
}
$specs = "<table class='productspecs'>";
$i = 0;
$drawnCategories = array();
foreach($spec_items as $item) {
$specValue = $item->attributes();
$titleXML = new SimpleXMLElement($item->asXML());
$title = $titleXML->xpath("//Name");
$specName = $title[0]->attributes();
$specCategoryId = intval($specValue['CategoryFeatureGroup_ID']);
if($specName['Value'] != "Source data-sheet")
{
$class = $i%2==0?"odd":"even";
$specs .= "<tr class='".$class."'>
<td>
<table>";
if(!in_array($specCategoryId, $drawnCategories))
{
$specs .= " <tr class='speccategory'>
<th><h3>".$categoryList[$specCategoryId]."</h3></th>
</tr>";
$drawnCategories[$i] = $specCategoryId;
}
$specs .= " <tr>
<th>".utf8_decode($specName['Value']).":</th>
</tr>
<tr>
<td>";
if($specValue['Presentation_Value'] == "Y")
{
$specs .= "Ja <img src='".SCRIPT_ROOT."images/check_green.png' alt='Ja' />";
}
else if($specValue['Presentation_Value'] == "N")
{
$specs .= "Nee <img src='".SCRIPT_ROOT."images/check_red.png' alt='Nee' />";
}
else
{
$specs .= str_replace('\n', '<br />', utf8_decode($specValue['Presentation_Value']));
}
$specs .= "</td>
</tr>
</table>
</td>
</tr>";
}
$i++;
}
$specs .= "</table>";
//Draw product description and link to manufacturer if available
if( $drawdescription != 0)
{
foreach($productDescription as $item) {
$productValues = $item->attributes();
if($productValues['URL'] != null)
{
$specs .= "<p id='manufacturerlink'><a href='".$productValues['URL']."'>Productinformation from manufacturer</a></p>";
}
if($productValues['LongDesc'] != null)
{
$description = utf8_decode(str_replace('\n', '', $productValues['LongDesc']));
$description = str_replace('<b>', '<strong>', $description);
$description = str_replace('<B>', '<strong>', $description);
$description = str_replace('</b>', '</strong>', $description);
$specs .= "<p id='manudescription'>".$description."</p>";
}
}
}
//Draw product picture if available
if( $drawdescription != 0)
{
foreach($productPicture as $item) {
$productValues = $item->attributes();
if($productValues['HighPic'] != null)
{
$specs .= "<div id='manuprodpic'><img src='".$productValues['HighPic']."' alt='' /></div>";
}
}
}
return $specs;
}
else
{
return 0;
}
}
Any advice will be very appreciated thx in advance Peter
My understanding is that it's saying that if you call this function you should pass EAN parameter to it, like this:
getICEcatProductSpecs("0885909481842", 0, 0);

Adding a Dojo Datagrid to a ZF Dojo_Form, how can I do it?

I have this Form with several subForms I'm using a tabContainer,
In one of them I have a button when clicked would like that in that "space" subForm
a datagrid would appear with some values.
Is it possible to do this, how?
I tried creating a datagrid View Helper but this didn't work properly the DataGrid does not show.
Code used:
/** * Description of DataGrid * * #author andref * #version $Id$
*/ class Zend_View_Helper_DataGrid {
private $_nameDG = 'DefaultSt';
private $_action = '';
private $_key = '';
private $_selectMode = "single";
private $_fields = array();
private $_storage = 'getall/';
public function dataGrid($key, $action = null, $options = array() )
{
if (count($options) > 0 ) {
if (array_key_exists("selectmode", $options)) {
$this->_selectMode = $options['selectmode'];
} elseif (array_key_exists("fields", $options)) {
if (!is_array($options['fields'])) {
throw new Exception("fields is not an array");
}
$this->_fields = $options['fields'];
} elseif(array_key_exists("selectmode", $options)) {
$this->_selectMode = $options['selectmode'];
} elseif(array_key_exists("storage", $options)) {
$this->_storage = $options['storage'];
}
}
if ($action !== null ) {
$this->_action = $action;
}
if ($key === null ) {
throw new Exception("Key cannot be null.");
}
$this->_key = $key;
return $this->_init();
}
private function _init()
{
$str = '';
if (!empty($this->_action)) {
$str .= $this->addJS();
}
$str .= $this->storageType();
$str .= $this->draw();
return $str;
}
/**
* Returns double click handler to direct to a form
*
* #return String
*/
public function addJS()
{
$str = "<script type=\"text/Javascript\">\n";
$str .= "function pickit(event)\n";
$str .= "{\n";
$str .= "grid = dijit.byId('grid".$this->_key."');\n";
$str .= "selected_index = grid.focus.rowIndex;\n";
$str .= "selected_item = grid.getItem(selected_index);\n";
//Not sure if this is the most efficient way but it worked for me
$str .= "selected_id = grid.store.getValue(selected_item, \"".$this->_key."\");\n";
$str .= "location.href = " . $this->_action . "+selected_id;\n";
$str .= "}\n";
$str .= "</script>\n";
return $str;
}
public function storageType()
{
if (strpos($this->_storage, "[") !== false ) {
$st = "jsonData".$this->_key . " = " . $this->_storage."\n";
$st .= "<gett dojoType=\"dojo.data.ItemFileReadStore\"
jsId=\"jsonStore" . $this->_key . "\"
data=\"jsonData" . $this->_key . "\" id=\"store" . $this->_key . "\" />";
} else {
$st = '<gett dojoType="dojo.data.ItemFileReadStore" jsId=\"jsonStore'.$this->_key.'"
url="' . $this->_storage . '" id="store'.$this->_key.'" />';
}
return $st;
}
public function draw()
{
$str = '';
$str .= "<table dojoType=\"dojox.grid.DataGrid\" id=\"grid".$this->_key."\" jsid=\"gridJ".$this->_key."\"
query=\"{ " . $this->_key . ": '*' }\" store=\"jsonStore".$this->_key."\"
selectionMode=\"" . $this->_selectMode . "\" autoWidth=\"true\"
style=\"width: 100%; height: 400px\" onRowDblClick='pickit();'>\n";
$str .= "<thead>\n";
$str .= "<tr>\n";
foreach ($this->_fields as $k => $v ) {
$str .= "\t<th field=\"$k\">$v</th>\n";
}
$str .= "</tr>\n";;
$str .= "</thead>\n";
$str .= "</table>\n";
return $str;
}
}
try creating a grid in pure js and place it
using grid.placeat("idOfElement") in your element. Take a look here:
http://dojotoolkit.org/reference-guide/1.8/dojox/grid/DataGrid.html

How do I sort an associative array and split into two div's based on a value?

I like to sort an associative array into two div's based on a value.
Here's what I have now:
function showDivs($array) {
/*
* sort en group by
*/
$luxury = false;
$no_luxury = false;
$return = '';
while (list($key, $value) = each($array)) {
if ($value["MinimumRate"] < 500) {
if ($no_luxury === false) {
$return .= '<div id="no_luxury" >';
}
$return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
if ($no_luxury === false) {
$return .='</div>';
$no_luxury = true;
}
}
if ($value["MinimumRate"] > 500) {
if ($luxury === false) {
$return .='<div id="luxury" >';
}
$return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
if ($luxury === false) {
$return .='</div>';
$luxury = true;
}
}
}
return $return;
}
My code doesn't work correctly.
The div is being created and closed on the first item only. I think you meant to open it on start and close it in the end. Try this:
function showDivs($array) {
/*
* sort en group by
*/
$luxury = array();
$not_luxury = array();
foreach ($array as $value) {
if ($value["MinimumRate"] > 500) {
$luxury[] = $value;
} else {
$not_luxury[] = $value;
}
}
$return = '<div id="no_luxury" >';
foreach ($luxury as $value) {
$return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
}
$return .= '</div>';
$return .= '<div id="luxury" >';
foreach ($luxury as $value) {
$return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
}
$return .= '</div>';
return $return;
}
If the problem is when MinimumRate = 500, you whould do:
if ($value["MinimumRate"] <= 500) // this is your first 'if' in your code
or
if ($value["MinimumRate"] >= 500) // this is the second 'if' in your code

Categories