/** * @desc Prints the question. Calls question_webwork_derived, and prints out the html associated with derivedid. * @param $question object The question object to print. * @param $state object The state of the responses for the question. * @param $cmoptions object Options containing course ID. * @param $options object */ function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) { global $CFG, $USER; $readonly = empty($options->readonly) ? '' : 'disabled="disabled"'; //Formulate question image and text $questiontext = $this->format_text($question->questiontext, $question->questiontextformat, $cmoptions); $image = get_question_image($question, $cmoptions->course); $derivationid = $state->responses['derivationid']; $derivation = get_record('question_webwork_derived', 'id', $derivationid); $unparsedhtml = base64_decode($derivation->html); //partial answers $showPartiallyCorrectAnswers = $question->grading; //new array keyed by field $fieldhash = $state->responses['answers']; $answerfields = array(); $parser = new HtmlParser($unparsedhtml); $currentselect = ""; while ($parser->parse()) { //change some attributes of html tags for moodle compliance if ($parser->iNodeType == NODE_TYPE_ELEMENT) { $nodename = $parser->iNodeName; $name = $parser->iNodeAttributes['name']; //handle generic change of node's attribute name if ($nodename == "INPUT" || $nodename == "SELECT" || $nodename == "TEXTAREA") { $parser->iNodeAttributes['name'] = 'resp' . $question->id . '_' . $name; if ($state->event == QUESTION_EVENTGRADE && isset($fieldhash[$name])) { if ($showPartiallyCorrectAnswers) { $parser->iNodeAttributes['class'] = $parser->iNodeAttributes['class'] . ' ' . question_get_feedback_class($fieldhash[$name]['score']); } } if (!strstr($name, 'previous')) { $answerfields[$name] = $fieldhash[$name]; } } //handle specific change if ($nodename == "INPUT") { //put submitted value into field if (isset($fieldhash[$name])) { $parser->iNodeAttributes['value'] = $fieldhash[$name]['answer']; } } else { if ($nodename == "SELECT") { $currentselect = $name; } else { if ($nodename == "OPTION") { if ($parser->iNodeAttributes['value'] == $fieldhash[$currentselect]['answer']) { $parser->iNodeAttributes['selected'] = '1'; } } else { if ($nodename == "TEXTAREA") { } } } } } $problemhtml .= $parser->printTag(); } //for the seed form field $qid = $question->id; $seed = $state->responses['seed']; //if the student has answered include "{$CFG->dirroot}/question/type/webwork/display.html"; }
/** * @desc Generates the HTML for a particular question. * @param integer $seed The seed of the question. * @param array $answers An array of answers that needs to be rendered. * @param object $event The event object. * @return string The HTML question representation. */ public function render($seed, &$answers, $event) { //JIT Derivation creation //Usually we have this from the check answers call if (!isset($this->_derivation)) { $client = WebworkClient::Get(); $env = WebworkQuestion::DefaultEnvironment(); $env->problemSeed = $seed; $result = $client->renderProblem($env, $this->_data->code); $derivation = new stdClass(); $derivation->html = base64_decode($result->output); $derivation->seed = $result->seed; $this->_derivation = $derivation; } $orderedanswers = array(); $tempanswers = array(); foreach ($answers as $answer) { $tempanswers[$answer->field] = $answer; } $answers = $tempanswers; $showpartialanswers = $this->_data->grading; $questionhtml = ""; $parser = new HtmlParser($this->_derivation->html); $currentselect = ""; $textarea = false; $checkboxes = array(); while ($parser->parse()) { //change some attributes of html tags for moodle compliance if ($parser->iNodeType == NODE_TYPE_ELEMENT) { $nodename = $parser->iNodeName; if (isset($parser->iNodeAttributes['name'])) { $name = $parser->iNodeAttributes['name']; } //handle generic change of node's attribute name if ($nodename == "INPUT" || $nodename == "SELECT" || $nodename == "TEXTAREA") { $parser->iNodeAttributes['name'] = 'resp' . $this->_data->question . '_' . $name; if ($event == QUESTION_EVENTGRADE && isset($answers[$name])) { if ($showpartialanswers) { if (isset($parser->iNodeAttributes['class'])) { $class = $parser->iNodeAttributes['class']; } else { $class = ""; } $parser->iNodeAttributes['class'] = $class . ' ' . question_get_feedback_class($answers[$name]->score); } } } //handle specific change if ($nodename == "INPUT") { $nodetype = strtoupper($parser->iNodeAttributes['type']); if ($nodetype == "CHECKBOX") { if (strstr($answers[$name]->answer, $parser->iNodeAttributes['value'])) { //FILLING IN ANSWER (CHECKBOX) array_push($orderedanswers, $answers[$name]); $parser->iNodeAttributes['checked'] = '1'; } $parser->iNodeAttributes['name'] = $parser->iNodeAttributes['name'] . '_' . $parser->iNodeAttributes['value']; } else { if ($nodetype == "TEXT") { if (isset($answers[$name])) { //FILLING IN ANSWER (FIELD) array_push($orderedanswers, $answers[$name]); $parser->iNodeAttributes['value'] = $answers[$name]->answer; } } } } else { if ($nodename == "SELECT") { $currentselect = $name; } else { if ($nodename == "OPTION") { if ($parser->iNodeAttributes['value'] == $answers[$currentselect]->answer) { //FILLING IN ANSWER (DROPDOWN) array_push($orderedanswers, $answers[$currentselect]); $parser->iNodeAttributes['selected'] = '1'; } } else { if ($nodename == "TEXTAREA") { if (isset($answers[$name])) { array_push($orderedanswers, $answers[$name]); $textarea = true; $questionhtml .= $parser->printTag(); $questionhtml .= $answers[$name]->answer; } } } } } } if (!$textarea) { $questionhtml .= $parser->printTag(); } else { $textarea = false; } } $answers = $orderedanswers; return $questionhtml; }
/** * @desc Parses a derivation record. This changes all paths of external entities. Copies all external entities to local data folder. * @param object $derivation The derivation object. * @return bool true. */ function webwork_parse_change_derivation(&$derivation) { //webwork question id $wwquestionid = $derivation->question_webwork; //assign paths $wwquestioncopyto = webwork_get_wwquestion_path_full($wwquestionid); $wwquestionreplacer = webwork_get_wwquestion_url($wwquestionid); $derivationcopyto = webwork_get_derivation_path_full($wwquestionid, $derivation->id); $derivationreplacer = webwork_get_derivation_url($wwquestionid, $derivation->id); $parsedhtml = ""; $html = base64_decode($derivation->html); $parser = new HtmlParser($html); while ($parser->parse()) { if ($parser->iNodeType == NODE_TYPE_ELEMENT) { $nodename = $parser->iNodeName; if (strcasecmp($nodename, 'img') == 0) { //image, path fixing if (isset($parser->iNodeAttributes['src'])) { $src = $parser->iNodeAttributes['src']; if (!(strstr($src, '/tmp/') == false)) { //image produced by the server $srccapture = strrchr($src, '/'); webwork_copy_file($src, $derivationcopyto . $srccapture); $parser->iNodeAttributes['src'] = $derivationreplacer . $srccapture; } } } else { if (strcasecmp($nodename, 'a') == 0) { //hyperlink, path fixing if (isset($parser->iNodeAttributes['href'])) { $href = $parser->iNodeAttributes['href']; $hrefcapture = strrchr($href, '/'); webwork_copy_file($href, $derivationcopyto . $hrefcapture); $parser->iNodeAttributes['href'] = $derivationreplacer . $hrefcapture; } } } } $parsedhtml .= $parser->printTag(); } $derivation->html = base64_encode($parsedhtml); return true; }