/** * @desc Checks if PG code is acceptable. * @param string $code The PG Code. * @throws Exception If there is an error with the PG code. * @return object Information on the question. */ public function CodeCheck($code) { $haserrors = false; $haswarnings = false; $client = WebworkClient::Get(); $env = WebworkQuestion::DefaultEnvironment(); $result = $client->renderProblem($env, $code); if (isset($result->errors) && $result->errors != '' && $result->errors != null) { $haserrors = true; } if (isset($result->warnings) && $result->warnings != '' && $result->warnings != null) { $haswarnings = true; } //Validity Check if ($haserrors == false && $haswarnings == false) { $output = new stdClass(); $output->grading = $result->grading; $derivation = new stdClass(); $derivation->html = $result->output; $derivation->seed = $result->seed; $output->derivation = $derivation; } else { //NOW WE ARE INVALID going to throw $msg = "<h2>PG Code Problems</h2>"; if ($haserrors) { $msg .= "<h3>Errors</h3>"; $msg .= $result->errors; } if ($haswarnings) { $msg .= "<h3>Warnings</h3>"; $msg .= $result->warnings; } throw new Exception($msg); } return $output; }
/** * @desc Grades a particular question state. * @param object $state The state to grade. * @return true. */ public function grade(&$state) { $seed = $state->responses['seed']; $answers = array(); if (isset($state->responses['answers']) && is_array($state->responses['answers'])) { foreach ($state->responses['answers'] as $answerobj) { if (is_string($answerobj->field) && is_string($answerobj->answer)) { array_push($answers, array('field' => $answerobj->field, 'answer' => $answerobj->answer)); } } } if (isset($state->responses) && is_array($state->responses)) { foreach ($state->responses as $key => $value) { if (is_string($key) && is_string($value)) { array_push($answers, array('field' => $key, 'answer' => $value)); } } } //combine results from the answer array for checkboxes $checkanswers = array(); $tempanswers = array(); for ($i = 0; $i < count($answers); $i++) { $fieldname = $answers[$i]['field']; $pos = strpos($fieldname, '_'); if ($pos !== FALSE) { $fieldname = substr($fieldname, 0, $pos); if (isset($checkanswers[$fieldname])) { $checkanswers[$fieldname] .= $answers[$i]['answer']; } else { $checkanswers[$fieldname] = $answers[$i]['answer']; } } else { array_push($tempanswers, $answers[$i]); } } foreach ($checkanswers as $key => $value) { array_push($tempanswers, array('field' => $key, 'answer' => $value)); } $answers = $tempanswers; //base64 encoding for ($i = 0; $i < count($answers); $i++) { $answers[$i]['field'] = base64_encode($answers[$i]['field']); $answers[$i]['answer'] = base64_encode($answers[$i]['answer']); } $client = WebworkClient::Get(); $env = WebworkQuestion::DefaultEnvironment(); $env->problemSeed = $seed; $results = $client->renderProblemAndCheck($env, $this->_data->code, $answers); //process the question $question = $results->problem; $derivation = new stdClass(); $derivation->seed = $question->seed; $derivation->html = base64_decode($question->output); $this->_derivation = $derivation; //assign a grade $answers = $results->answers; $state->raw_grade = $this->processAnswers($answers); // mark the state as graded $state->event = $state->event == QUESTION_EVENTCLOSE ? QUESTION_EVENTCLOSEANDGRADE : QUESTION_EVENTGRADE; //put the responses into the state to remember if (!is_array($state->responses)) { $state->responses = array(); } $state->responses['answers'] = $answers; return true; }