/** * Get a list of the PRT notes that should be present for a given PRT. * @param string $prtname the name of a PRT. * @return array list of nodes that should be present in the form definitino for this PRT. */ protected function get_prt_graph($prtname) { if (array_key_exists($prtname, $this->prtgraph)) { return $this->prtgraph[$prtname]; } // If the form has been submitted and is being redisplayed, and this is // an existing PRT, base things on the submitted data. $submitted = optional_param_array($prtname . 'truenextnode', null, PARAM_RAW); if ($submitted) { $truescoremode = optional_param_array($prtname . 'truescoremode', null, PARAM_RAW); $truescore = optional_param_array($prtname . 'truescore', null, PARAM_RAW); $falsenextnode = optional_param_array($prtname . 'falsenextnode', null, PARAM_RAW); $falsescoremode = optional_param_array($prtname . 'falsescoremode', null, PARAM_RAW); $falsescore = optional_param_array($prtname . 'falsescore', null, PARAM_RAW); $graph = new stack_abstract_graph(); $deletednode = null; $lastkey = -1; foreach ($submitted as $key => $truenextnode) { if (optional_param($prtname . 'nodedelete' . $key, false, PARAM_BOOL)) { // Slightly odd to register the button here, especially since // now this node has been deleted, this button will not exist, // but anyway this works, and in necessary to stop the form // from being submitted. $this->_form->registerNoSubmitButton($prtname . 'nodedelete' . $key); // For deleted nodes, we add them to the tree anyway, and // then remove them again below. We have to do it that way // because we also need to delete links that point to the // deleted node. $deletednode = $key; } if ($truenextnode == -1 || !array_key_exists($truenextnode, $submitted)) { $left = null; } else { $left = $truenextnode + 1; } if ($falsenextnode[$key] == -1 || !array_key_exists($falsenextnode[$key], $submitted)) { $right = null; } else { $right = $falsenextnode[$key] + 1; } $graph->add_node($key + 1, $left, $right, $truescoremode[$key] . round($truescore[$key], 2), $falsescoremode[$key] . round($falsescore[$key], 2), '#fgroup_id_' . $prtname . 'node_' . $key); $lastkey = max($lastkey, $key); } if (optional_param($prtname . 'nodeadd', false, PARAM_BOOL)) { $graph->add_node($lastkey + 2, null, null, '+0', '-0', '#fgroup_id_' . $prtname . 'node_' . $lastkey + 1); } if (!is_null($deletednode)) { $graph->remove_node($deletednode + 1); } $graph->layout(); $this->prtgraph[$prtname] = $graph; return $graph; } // Otherwise, if an existing question is being edited, and this is an // existing PRT, base things on the existing question definition. if (!empty($this->question->prts[$prtname]->nodes)) { $graph = new stack_abstract_graph(); foreach ($this->question->prts[$prtname]->nodes as $node) { if ($node->truenextnode == -1) { $left = null; } else { $left = $node->truenextnode + 1; } if ($node->falsenextnode == -1) { $right = null; } else { $right = $node->falsenextnode + 1; } $graph->add_node($node->nodename + 1, $left, $right, $node->truescoremode . round($node->truescore, 2), $node->falsescoremode . round($node->falsescore, 2), '#fgroup_id_' . $prtname . 'node_' . $node->nodename); } $graph->layout(); $this->prtgraph[$prtname] = $graph; return $graph; } // Otherwise, it is a new PRT. Just one node. $graph = new stack_abstract_graph(); $graph->add_node('1', null, null, '=1', '=0', '#fgroup_id_' . $prtname . 'node_0'); $graph->layout(); $this->prtgraph[$prtname] = $graph; return $graph; }