setType("multipleChoice"); $this->setInteractionType("choiceInteraction"); $this->setName("Multiple Choice"); $this->setTable("multiple_choice"); if ($id) { $data = $this->load($id); if ($data) { $this->setChoices(unserialize($data->choices)); $this->setScore($data->score); } } } function create() { if ($id = parent::create()) { $serialized_choices = serialize($this->getChoices()); $insert_query = "INSERT INTO " . DB_PREFIX . $this->getTable() . " (base_id, choices, score) values (".$id.", '".mysql_real_escape_string($serialized_choices)."', ".$this->getScore().")"; $insert = query_insert($insert_query); return $id; } return false; } function save() { if (parent::save()) { $update_query = "UPDATE " . DB_PREFIX . $this->getTable() . " SET"; $serialized_choices = serialize($this->getChoices()); $update_query .= " choices='".mysql_real_escape_string($serialized_choices). "',"; $update_query .= " score=".$this->getScore(); $update_query .= " WHERE base_id=".$this->getId(); if (query_update($update_query)) return $this->getId(); } return false; } //function copy($locked=true) { // $copy = $this->create(); /*if ($copy && $locked) { $lock = $this->lock($copy); }*/ //return $copy; //} function initialiseChoices() { $choices = array(); for ($i=1; $i<=4; $i++) { $choice_data = array(); $choice_data["choice_text"] = ""; $choice_data["correct"] = false; $choice_data["fixed"] = false; $choices[$i] = $choice_data; } $this->choices = $choices; } function answer($data) { $candidate = $data["candidate"]; $chosen = $data["chosen"]; $score = $this->calculateScore($chosen); $insert = query_insert("INSERT INTO " . DB_PREFIX . "multiple_choice_answers (base_id, candidate, chosen, score) VALUES (". $this->getId() .", ". $candidate .", '". $chosen ."', ". $score .")"); return true; } function calculateScore($chosen) { if (!$chosen) { return 0; } $choices = $this->getChoices(); $choice = $choices[$chosen]; if ($choice["correct"]) { return $this->getScore(); } return 0; } function getCandidateAnswer($token) { global $TeKe; $candidate_id = $TeKe->plugin->assessments->getCandidateByToken($token); $query = "SELECT * FROM " . DB_PREFIX . "multiple_choice_answers"; $query .= " WHERE base_id=". $this->getId(); $query .= " AND candidate=". $candidate_id; $res = query_row($query); return $res; } function getCandidateAnswerForCSV($token) { // returns either "0," or "1," depending is answer correct or not $answer = $this->getCandidateAnswer($token); if (!($answer->chosen)) { return "0,"; } $choices = $this->getChoices(); $choice = $choices[$answer->chosen]; if ($choice["correct"]) { return "1,"; } return "0,"; } function build($data) { if (parent::build($data)) { /*if (isset($data["table_width"])) { if (is_numeric($data["table_width"])) { $this->setTableWidth($data["table_width"]); } else { $this->addBuildError(_("Table width")); } }*/ $this->saveChoices($data["choice_text"], $data["correct_choice"]); if (isset($data["score"]) && is_numeric($data["score"])) { $this->setScore($data["score"]); } if ($this->getBuildError()) { global $TeKe; $TeKe->add_system_message(_("Problems with fields") . ": " . implode(",", $this->getBuildError()), 'error'); return false; } if ($this->getId() > 0) { return $this->save(); } else { return $this->create(); } } } function saveChoices($new_choices, $correct_choice) { $choices = array(); $choice_value = 0; foreach ($new_choices as $choice_nr => $choice) { if ($choice != "") { $choice_data = array(); $choice_data["choice_text"] = $choice; if ($choice_nr == $correct_choice) { $choice_data["correct"] = true; } else { $choice_data["correct"] = false; } $choice_data["fixed"] = false; $choice_value++; $choices [$choice_value]= $choice_data; } } $this->choices = $choices; } function getChoices() { if (empty($this->choices)) { $this->initialiseChoices(); } return $this->choices; } function setChoices($choices) { $this->choices = $choices; } function getScore() { return (string)(floatval($this->score)); } function setScore($score) { $this->score = $score; } function getMaximumScore() { return $this->score; } function getRightChoice() { $choices = $this->getChoices(); foreach ($choices as $choice_nr => $choice) { if ($choice["correct"] == true) { return $choice_nr; } } return false; } function setRightChoice($right_choice) { $this->right_choice = $right_choice; } function buildFromXML($resource_dom, $dom) { parent::buildFromXML($resource_dom); $item_body = $this->findItemBodyText($dom); $choices = $this->findChoices($dom); $correct_choice = $this->findCorrectChoice($dom); $this->setTitle($this->getTitleFromXML($dom)); $this->setItemBody($item_body); $score = $this->findScore($dom); $this->setScore($score); $this->saveChoices($choices, $correct_choice); return $this->create(); } function getTitleFromXML($dom) { $assessmentItem = $dom->getElementsByTagName("assessmentItem")->item(0); return $assessmentItem->getAttribute("title"); } function findItemBodyText($dom) { $item_body = $dom->getElementsByTagName("itemBody")->item(0); $children = $item_body->childNodes; for ($i=0; $i<$children->length; $i++) { $item = $children->item($i); if ($item->nodeType != XML_TEXT_NODE && $item->nodeType != XML_CDATA_SECTION_NODE) { return $children->item($i)->textContent; } } return ""; } function findChoices($dom) { $choice_interaction = $dom->getElementsByTagName("choiceInteraction")->item(0); $choices = $choice_interaction->getElementsByTagName("simpleChoice"); $new_choices = array(); for ($i=0; $i<$choices->length; $i++) { $choice = $choices->item($i); $identifier = $choice->getAttribute("identifier"); $choice_text = $this->findNodeText($choice); $new_choices[$identifier] = $choice_text; } return $new_choices; } function findNodeText($node) { $children = $node->childNodes; for ($i=0; $i<$children->length; $i++) { $item = $children->item($i); if ($item->nodeType == XML_TEXT_NODE || $item->nodeType == XML_CDATA_SECTION_NODE) { $child_text = $children->item($i)->textContent; if (trim($child_text) != "") { return $children->item($i)->textContent; } } } return ""; } function findCorrectChoice($dom) { $correct_response = $dom->getElementsByTagName("correctResponse")->item(0); $value = $correct_response->getElementsByTagName("value")->item(0); $correct = $this->findNodeText($value); return $correct; } function findScore($dom) { $response_processing = $dom->getElementsByTagName("responseProcessing")->item(0); $response_if = $response_processing->getElementsByTagName("responseIf")->item(0); $match = $response_if->getElementsByTagName("match"); if ($match->length > 0) { $base_value = $response_if->getElementsByTagName("baseValue")->item(0); return $base_value->textContent; } return 1; } function getXML() { $xml = " getId()."\" title=\"".htmlentities($this->getTitle())."\" adaptive=\"false\" timeDependent=\"false\">\n"; $xml .= "getId()."\" cardinality=\"single\" baseType=\"identifier\">\n"; $xml .= "\n"; $xml .= ""; $xml .= $this->getRightChoice(); $xml .= "\n"; $xml .= "\n"; $xml .= "\n"; $xml .= "\n"; $xml .= "\n"; $xml .= "0\n"; $xml .= "\n"; $xml .= "\n"; $xml .= "\n"; $xml .= "

\ngetItemBody()."]]>\n

\n"; $xml .= "getId()."\" shuffle=\"false\" maxChoices=\"1\">\n"; $choices = $this->getChoices(); foreach ($choices as $choice_nr => $choice) { $xml .= "\n"; $xml .= "\n"; $xml .= "\n"; } $xml .= "\n"; $xml .= "
\n"; $xml .= $this->match_correct_xml()."\n"; $xml .= "
"; return $xml; } } ?>