setType("multipleResponse"); $this->setInteractionType("choiceInteraction"); $this->setName("Multiple Response"); $this->setTable("multiple_response"); //$this->setActions(array("view"=>_("View"), "edit"=>_("Edit"), "compose"=>_("Compose"))); if ($id) { $data = $this->load($id); if ($data) { $this->setChoices(unserialize($data->choices)); //$this->setRightChoice(unserialize($data->right_choice)); } } } function create() { if ($id = parent::create()) { $serialized_choices = serialize($this->getChoices()); $insert = query_insert("INSERT INTO " . DB_PREFIX . $this->getTable() . " (base_id, choices) values (".$id.", '".mysql_real_escape_string($serialized_choices)."')"); 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 .= " 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["score"] = ""; $choice_data["fixed"] = false; $choices[$i] = $choice_data; } $this->choices = $choices; } function answer($data) { $candidate = $data["candidate"]; $chosen = $data["chosen"]; if (!is_array($chosen) && $chosen == "") { $chosen = array(); } $score = $this->calculateScore($chosen); $insert = query_insert("INSERT INTO " . DB_PREFIX . "multiple_response_answers (base_id, candidate, chosen, score) VALUES (". $this->getId() .", ". $candidate .", '". serialize($chosen) ."', ". $score .")"); return true; } function calculateScore($chosen) { if (!$chosen) { return 0; } $score = 0; $choices = $this->getChoices(); foreach ($chosen as $chosen_choice) { $choice = $choices[$chosen_choice]; $score += $choice["score"]; } if ($score < 0) { return 0; } return $score; } function getCandidateAnswer($token) { global $TeKe; $candidate_id = $TeKe->plugin->assessments->getCandidateByToken($token); $query = "SELECT * FROM " . DB_PREFIX . "multiple_response_answers"; $query .= " WHERE base_id=". $this->getId(); $query .= " AND candidate=". $candidate_id; $res = query_row($query); return $res; } function getCandidateAnswerForCSV($token) { $result = array(); $choices = $this->getChoices(); $answer = $this->getCandidateAnswer($token); $chosen = unserialize($answer->chosen); if (!is_array($chosen)) { $chosen = array($chosen); } foreach ($choices as $choice_nr => $choice) { if ($choice["correct"] && in_array($choice_nr, $chosen)) { $result []= "1"; } else if (!($choice["correct"]) && !(in_array($choice_nr, $chosen))) { $result []= "1"; } else { $result []= "0"; } } return $result; } 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"], $data["correct_choice"]); 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_text"] != "") { $choice_data = array(); $choice_data["choice_text"] = $choice["choice_text"]; if (is_numeric($choice["score"])) { $choice_data["score"] = $choice["score"]; } else { $choice_data["score"] = 0; } if (in_array($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 getMaximumScore() { $choices = $this->getChoices(); $score = 0; foreach ($choices as $choice_nr => $choice) { if ($choice["score"] > 0) { $score += $choice["score"]; } } return $score; } function getRightChoice() { return $this->right_choice; } 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_choices = $this->findCorrectChoices($dom); $this->setTitle($this->getTitleFromXML($dom)); $this->setItemBody($item_body); $this->saveChoices($choices, $correct_choices); 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"] = $choice_text; $score = $this->findChoiceScore($dom, $identifier); $new_choices[$identifier]["score"] = $score; } return $new_choices; } function findChoiceScore($dom, $identifier) { $map_entries = $dom->getElementsByTagName("mapEntry"); for ($i=0; $i<$map_entries->length; $i++) { $map_entry = $map_entries->item($i); $map_key = $map_entry->getAttribute("mapKey"); if ($identifier == $map_key) { return $map_entry->getAttribute("mappedValue"); } } return 0; } 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 findCorrectChoices($dom) { $correct_choices = array(); $correct_response = $dom->getElementsByTagName("correctResponse")->item(0); $values = $correct_response->getElementsByTagName("value"); for ($i=0; $i<$values->length; $i++) { $value = $values->item($i); $correct = $this->findNodeText($value); $correct_choices []= $correct; } return $correct_choices; } function getXML() { $xml = " getId()."\" title=\"".htmlentities($this->getTitle())."\" adaptive=\"false\" timeDependent=\"false\">\n"; $xml .= "getId()."\" cardinality=\"multiple\" baseType=\"identifier\">\n"; $xml .= "\n"; $choices = $this->getChoices(); foreach ($choices as $choice_nr => $choice) { if ($choice["correct"] == true) { $xml .= ""; $xml .= $choice_nr; $xml .= "\n"; } } $xml .= "\n"; $xml .= "getMaximumScore()."\" defaultValue=\"0\">\n"; foreach ($choices as $choice_nr => $choice) { $xml .= "\n"; } $xml .= "\n"; $xml .= "\n"; $xml .= "\n"; $xml .= "\n"; $xml .= "

\ngetItemBody()."]]>\n

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