/** * @desc Deletes question from the question_webwork table * @param integer $questionid The question being deleted * @return boolean to indicate success of failure. */ function delete_question($questionid) { try { $wwquestion = WebworkQuestionFactory::LoadByParent($questionid); $wwquestion->remove(); } catch (Exception $e) { print_error('error_question_id_no_child', 'qtype_webwork'); } return true; }
/** * @desc Validates that the question is without PG errors as mandated by codecheck level. * @param $data array The form data that needs to be validated. * @return array An array of errors indexed by field. */ function validation($data) { //init $errors = array(); //build dataobject $dataobject = new stdClass(); $dataobject->code = $data['code']; $dataobject->codecheck = $data['codecheck']; //attempt to find an ID (if updating) if (isset($this->question->webwork)) { $dataobject->id = $this->question->webwork->getId(); } //try and build the question object try { if (isset($this->_form->_submitValues['makecopy'])) { $wwquestion = WebworkQuestionFactory::CreateFormCopy($dataobject); } else { if (isset($dataobject->id)) { $wwquestion = WebworkQuestionFactory::CreateFormUpdate($dataobject); } else { $wwquestion = WebworkQuestionFactory::CreateFormNew($dataobject); } } WebworkQuestionFactory::Store($data['storekey'], $wwquestion); } catch (Exception $e) { $errors['code'] = $e->getMessage(); } return $errors; }
public static function CreateFromForm($formdata) { WebworkQuestionFactory::ParseFormData($formdata); $output = WebworkQuestionFactory::CodeCheck($formdata->code); $formdata->grading = $output->grading; $wwquestion = new WebworkQuestion($formdata, $output->derivation); return $wwquestion; }
/** * @desc Takes the data in the questions array and creates derivations for it. Returns the questions that passed codecheck * @param array $data The questions to be added. * @return array The questions that passed the codecheck. */ function readquestions($data) { global $COURSE; $courseid = $COURSE->id; echo "<h2>Import Stage</h2>"; flush(); $correctdata = array(); $errors = array(); $currentcategoryid = null; //this is where we load question requests $start = time(); $count = 1; $total = count($this->questions); echo "<h3>Generation</h3>"; echo "We have {$total} questions and categories to generate!<br>"; flush(); for ($i = 0; $i < count($this->questions); $i++) { $question = $this->questions[$i]; if (isset($question->qtype) && $question->qtype == 'webwork') { echo "Question #{$count} ({$question->name}) ... "; flush(); $qstart = time(); try { $wwquestion = WebworkQuestionFactory::Import($question->code); if (isset($currentcategoryid)) { $previousid = get_field('question', 'id', 'category', $currentcategoryid, 'name', $question->name); if ($previousid) { $previouscode = get_field('question_webwork', 'code', 'question', $previousid); if ($previouscode == $question->code) { echo "Detected Duplicate<br>"; throw new Exception('Ignoring: Question already exists.'); } else { set_field('question', 'name', $question->name . '_old', 'id', $previousid); } } } $qend = time(); $qdiff = $qend - $qstart; echo "Generated ~({$qdiff} secs)<br>"; $question->webwork = $wwquestion; $question->questiontext = addslashes(base64_decode($wwquestion->render(0, array(), 0))); $question->questiontextformat = 1; unset($question->code); unset($question->codecheck); unset($question->seed); array_push($correctdata, $question); } catch (Exception $e) { $errors[$question->name] = $e->getMessage(); } $count++; } elseif (isset($question->qtype) && $question->qtype == 'category') { //category addition echo "<h3>Category Added {$question->name}</h3>"; flush(); //var_dump($question); $question->category = $question->name; $catarray = explode('/', $question->name); $parentid = null; foreach ($catarray as $catname) { if (!isset($parentid)) { $parentid = get_field('question_categories', 'id', 'name', $catname, 'course', $courseid); } else { $parentid = get_field('question_categories', 'id', 'name', $catname, 'course', $courseid, 'parent', $parentid); } } $currentcategoryid = $parentid; array_push($correctdata, $question); } else { echo "Unused File<br>"; flush(); } } $end = time(); $diff = $end - $start; echo "<br><b>Finished Generation, total time ~({$diff} secs)</b><br><br>"; flush(); if (count($errors) > 0) { echo "<h3>Errors</h3>"; echo "<br>Found " . count($errors) . " Question(s) with Errors:<br>"; foreach ($errors as $name => $error) { echo "Name: " . $name . "<br>"; echo "Error:" . $error . "<br><br>"; } } echo "<h2>Import Stage</h2>"; echo "<h3>Questions and Categories</h3>"; echo "Importing " . count($correctdata) . " Questions and Categories<br>"; flush(); return $correctdata; }