/** * @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; }