setExerciseData(""); $this->setMainQuestion(""); $this->setColCount(2); $this->setRowCount(2); $this->setQuestions(""); $this->setSolutionNr(""); } else { // type specific data loading global $kdb; $typeData = $kdb->getTypeData($rid, self::$table); $noRaw = unserialize($typeData['data']); $this->setExerciseData($noRaw['crossword']); $this->setMainQuestion($typeData['mainQuestion']); $this->setColCount($noRaw['cols']); $this->setRowCount($noRaw['rows']); $this->setQuestions(unserialize($typeData['questions'])); $this->setSolutionNr($typeData['mainQuestionCol']); } } protected function setExerciseData($d) { $this->exerciseData = $d; } public function getExerciseData() { return $this->exerciseData; } protected function setSolutionNr($sn) { $this->solutionNr = $sn; } public function getSolutionNr() { return $this->solutionNr; } protected function setMainQuestion($mq) { $this->mainQuestion = $mq; } public function getMainQuestion() { return $this->mainQuestion; } protected function setQuestions($qs) { $this->questions = $qs; } public function getQuestions() { return $this->questions; } protected function setRowCount($rc) { $this->rowCount = $rc; } public function getRowCount() { return $this->rowCount; } protected function setColCount($cc) { $this->colCount = $cc; } public function getColCount() { return $this->colCount; } function getQuestionArray(){ return $this->getQuestions(); } function getQuestionByNr($questionNr, $questions){ if ($questionNrgetResourceID()!="new"){ global $kdb; $res = $kdb->query("INSERT INTO crossword (resourceID, mainQuestion, mainQuestionCol, questions, data) values (".$this->getResourceID().", '".$xml['mainQuestion']."', '".$xml['mainQuestionCol']."', '".$xml['questions']."', '".$xml['data']."')"); if ($res) { $_SESSION['krihvel_notice'] = "\"".gettext("Crossword")."\" ".gettext("has been imported successfully!"); } else { $_SESSION['krihvel_notice'] = gettext("Upload failed! Zip file was corrupted!"); } } return array('rid' => $this->getResourceID(), 'mode' => 'view'); } public function makeXML() { $xml = 'getMainQuestion().']]>\n'; $xml .= 'getSolutionNr().']]>\n'; $xml .= 'getQuestions()).']]>\n'; $xml .= '$this->getExerciseData(), 'rows'=>$this->getRowCount(),'cols'=>$this->getColCount())).']]>\n'; return $xml; } // crosswordAnsOk function crosswordAnswersOk($row_count, $col_count, $answers, $data) { $cwAnsOk = array(); for($row_nr = 0; $row_nr < $row_count; $row_nr++) { for($col_nr = 0; $col_nr < $col_count; $col_nr++) { if(strcasecmp($data[$row_nr][$col_nr],$answers[$row_nr][$col_nr])) { $cwAnsOk []= 0; }else{ $cwAnsOk []= 1; } } } return $cwAnsOk; } /** * @Secured('roles' = {'authenticated'}, 'valid' = {}) * */ public function actionCopy() { global $kdb; $this->updateData($_POST); $rid = $this->getResourceID(); parent::copy(); if ($this->getResourceID() != $rid){ $res = $kdb->query("INSERT INTO crossword (resourceID, mainQuestion, mainQuestionCol, questions, data) values (".$this->getResourceID().", '".$this->getMainQuestion()."', '".$this->getSolutionNr()."', '".serialize($this->getQuestions())."', '".serialize(array('crossword'=>$this->getExerciseData(), 'rows'=>$this->getRowCount(),'cols'=>$this->getColCount()))."')"); if ($res) { $_SESSION['krihvel_notice'] = gettext("You have new copy of")." \"".gettext("Crossword")."\""; } else { $_SESSION['krihvel_notice'] = gettext("Making copy failed"); } } return array('rid' => $this->getResourceID(), 'mode' => 'view'); } /** * @Secured('roles' = {'authenticated'}, 'valid' = {}) * */ public function actionSave() { $mode = "view"; if ($this->getResourceID() == "new"){ $mode = "compile"; } $this->updateData($_POST); $this->save(); $_SESSION['krihvel_notice'] = gettext("Saved"); return array('rid' => $this->getResourceID(), 'mode' => $mode); } protected function save() { global $kdb; $rid = $this->getResourceID(); parent::save(); if ($rid == "new"){ $kdb->query("INSERT INTO crossword (resourceID) values (".$this->getResourceID().")"); } } public function collectCrossword($pdata) { $data = array(); $cwd = $this->getExerciseData(); $pa = 0; $aa = 0; for ($i=0;$i<$this->getRowCount();$i++){ $ikey = 't'.$i; $cwt = array(); $cwdd = $cwd[$ikey]; $isCorrect = 'correct'; $ca = 0; foreach (array_keys($cwdd) as $el) { $key = $ikey.$el; if (!isset($pdata[$key])) { $pdata[$key] = ""; } $cwt[$el] = $pdata[$key]; if (strcasecmp($cwdd[$el],$pdata[$key])==0) { $ca++; } else { $isCorrect = 'wrong'; } } $cwt['points'] = $ca; $cwt['answer'] = $isCorrect; $data[$ikey] = $cwt; if ($isCorrect=='correct') { $pa++; } $aa++; } $data['points'] = $pa; $data['rows'] = $aa; return $data; } /** * @Secured('roles' = {'authenticated'}, 'valid' = {}) * */ public function actionCompile() { global $kdb; $data = array(); $questions = array(); $cwt = array(); for ($i=0;$i<$_POST['y'];$i++){ $cwc = array(); for ($j=0; $j<$_POST['x']; $j++){ $cwc['c'.$j] = $_POST["t".$i."c".$j]; } $cwt['t'.$i] = $cwc; $questions []= $_POST['q'.$i]; } $data['crossword'] = $cwt; $data['rows'] = $_POST['y']; $data['cols'] = $_POST['x']; $q = "UPDATE crossword SET mainQuestion='%s', mainQuestionCol='%s', questions='%s', data='%s' WHERE resourceID=".$this->getResourceId(); $kdb->query(sprintf($q, $_POST['mainquestion'], $_POST['solution_col'], serialize($questions), serialize($data))); $_SESSION['krihvel_notice'] = gettext("Saved"); return array('rid' => $this->getResourceID(), 'mode' => "settings"); } /** * @Secured('roles' = {'authenticated'}, 'valid' = {}) * */ public function actionAnswer() { global $user, $krihvel; //$aid = $this->answerData($_POST); $a = new CrosswordAnswer(); $a->setResourceID($this->getResourceID()); $a->setAssignmentID($_POST['assignment']); $a->setUserID($user->getId()); $a->setGrade(0); $a->setComment(""); $a->setExponate(0); $a->setAnswer($this->collectCrossword($_POST)); $a->save(); return array('rid' => $this->getResourceID(), 'mode' => "answer"); } } ?>