setExerciseData(""); } else { // type specific data loading global $kdb; $typeData = $kdb->getTypeData($rid, self::$table); $this->setExerciseData($typeData['data']); } } protected function setExerciseData($d) { $this->exerciseData = $d; } public function getExerciseData() { return $this->exerciseData; } function mathAdd($a, $b){ return $a+$b; } function mathSub($a, $b){ return $a-$b; } function mathMul($a, $b){ return $a*$b; } function mathDiv($a, $b){ return $a/$b; } public function calculateUserAnswer($opRow, $answers){ $ccc = 0; $ac = 0; $operChar = array(); $operations = array(); foreach ($opRow as $opItem){ if($opItem=="?"){ $opItem=$answers[$ac]; $ac++; } if ($opItem=="=" || $opItem=="<" || $opItem==">"){ $operChar []= $opItem; $ccc++; }else{ $operations[$ccc] []= $opItem; } } $ccc = 0; $cntr = 1; foreach ($operations as $operation){ foreach ($operation as $aa){ if ($aa=="x" && count($operations[$ccc])>2){ $operations[$ccc][$cntr-1] = $this->mathMul($operations[$ccc][$cntr-1], $operations[$ccc][$cntr+1]); array_splice($operations[$ccc], $cntr+1, 1); array_splice($operations[$ccc], $cntr, 1); }elseif($aa=="d" && count($operations[$ccc])>2){ $operations[$ccc][$cntr-1] = $this->mathDiv($operations[$ccc][$cntr-1], $operations[$ccc][$cntr+1]); array_splice($operations[$ccc], $cntr+1, 1); array_splice($operations[$ccc], $cntr, 1); } } foreach ($operation as $aa){ if ($aa=="+" && count($operations[$ccc])>2){ $operations[$ccc][$cntr-1] = $this->mathAdd($operations[$ccc][$cntr-1], $operations[$ccc][$cntr+1]); array_splice($operations[$ccc], $cntr+1, 1); array_splice($operations[$ccc], $cntr, 1); }elseif($aa=="-" && count($operations[$ccc])>2){ $operations[$ccc][$cntr-1]= $this->mathSub($operations[$ccc][$cntr-1], $operations[$ccc][$cntr+1]); array_splice($operations[$ccc], $cntr+1, 1); array_splice($operations[$ccc], $cntr, 1); } } $ccc++; } $ansIsOk = true; $ch = 0; foreach ($operChar as $oc){ switch ($oc){ case "=": if (array_sum($operations[$ch])!=array_sum($operations[$ch+1])){ $ansIsOk = false; } break; case "<": if (array_sum($operations[$ch])>=array_sum($operations[$ch+1])){ $ansIsOk = false; } break; case ">": if (array_sum($operations[$ch])<=array_sum($operations[$ch+1])){ $ansIsOk = false; } break; } } return $ansIsOk; } public function calculateUserAnswers($userAnswers){ $okAnswers= array(); $exNr = 0; foreach ($this->getExerciseArray($this->getExerciseData()) as $exercise){ $roNr = 0; $exDa = $exercise['operations']; foreach ($exDa as $oRow){ $rName = "e".$exNr."_r".$roNr; if (array_key_exists($rName , $userAnswers)){ $okAnswers[$rName] = $this->calculateUserAnswer($oRow, $userAnswers[$rName]); } $roNr++; } $exNr++; } return $okAnswers; } public function getExerciseArray(){ $eRows = array(); $data = $this->getExerciseData(); foreach (split("_", $data) as $exercise){ $exDa = split("/", $exercise); if (count($exDa)>2){ $oRows = array(); foreach (split("\|", $exDa[2]) as $op_rows){ $oRows []= split(",", $op_rows); } $eRows []= array('title' => $exDa[0], 'description' => $exDa[1], 'operations' => $oRows); } } return $eRows; } public function getDataCount(){ $exNr = 0; $dataCount = array(); foreach ($this->getExerciseArray() as $exercise){ $rwNr = 0; foreach ($exercise['operations'] as $op_rows){ $itNr = 0; foreach ($op_rows as $item){ if ($item=="?"){ $dataCount []= 'e'.$exNr.'_r'.$rwNr.'_i'.$itNr; } $itNr++; } $rwNr++; } $exNr++; } return $dataCount; } protected function answerContent() { global $krihvel, $kdb, $user; if(isset($_GET['aid']) && is_numeric($_GET['aid'])){ $aobj = $kdb->getAnswerByID($_GET['aid'], "MathAnswer"); }else{ $aobj = $kdb->getAnswerByLatest($this->getResourceID(), $user->getId(), "MathAnswer"); } $exercises = $this->createExercises($this->getExerciseArray($this->getExerciseData()), $aobj); return $this->getAnswerView($exercises); } public function makeResourceFromXML($xml) { parent::makeResourceFromXML($xml); if ($this->getResourceID()!="new"){ global $kdb; $res = $this->kdb->query("INSERT INTO math (resourceID, data) values (".$this->getResourceID().", '".$xml['data']."')"); if ($res) { $_SESSION['krihvel_notice'] = "\"".gettext("Math")."\" ".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 = 'getExerciseData().']]>\n'; return $xml; } /** * @Secured('roles' = {'authenticated'}, 'valid' = {}) * */ public function actionCopy() { $this->updateData($_POST); $rid = $this->getResourceID(); parent::copy(); if ($this->getResourceID() != $rid){ $res = $this->kdb->query("INSERT INTO math (resourceID, data) values (".$this->getResourceID().", '".$this->getExerciseData()."')"); if ($res) { $_SESSION['krihvel_notice'] = gettext("You have new copy of")." \"".gettext("Math")."\""; } else { $_SESSION['krihvel_notice'] = gettext("Making copy failed"); } } return array('rid' => $this->getResourceID(), 'mode' => 'view'); } /** * @Secured('roles' = {'authenticated'}, 'valid' = {'isOwner'}) * */ 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() { $rid = $this->getResourceID(); parent::save(); if ($rid == "new"){ $this->kdb->query("INSERT INTO math (resourceID, data) values (".$this->getResourceID().", '".$this->getExerciseData()."')"); } } /** * @Secured('roles' = {'authenticated'}, 'valid' = {'isOwner'}) * */ public function actionCompile() { $this->kdb->query("UPDATE math SET data='".$_POST['e_data']."' WHERE resourceID=".$this->getResourceID()); $_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 MathAnswer(); $a->setResourceID($this->getResourceID()); $a->setAssignmentID($_POST['assignment']); $a->setUserID($user->getId()); $a->setGrade(0); $a->setComment(""); $a->setExponate(0); $data = $_POST; $datacount = $this->getDataCount(); $user_answers = array(); $user_answers_db = ""; foreach ($datacount as $boxname){ $user_answers_db .= $boxname.":".$data[$boxname].";"; $eName = split("_", $boxname); $rName = $eName[0]."_".$eName[1]; if (array_key_exists($rName, $user_answers)){ $user_answers[$rName][] = $data[$boxname]; }else{ $user_answers[$rName] = array($data[$boxname]); } } $uadb = substr_replace($user_answers_db ,"",-1); $uad = $this->calculateUserAnswers($user_answers); $a->setCorrect(serialize($uad)); $a->setAnswer($uadb); $a->save(); $_SESSION['krihvel_notice'] = gettext("Answered"); return array('rid' => $this->getResourceID(), 'mode' => "answer"); } } ?>