setType("ChoiceTask");
$this->setName("Choice Task");
$this->setTable("choice_task");
$this->setActions(array("view"=>_("View"), "edit"=>_("Edit"), "compose"=>_("Compose")));
if ($id) {
$data = $this->load($id);
if ($data) {
$this->setBodyImage($data->body_image);
$this->setBodyVideo($data->body_video);
$this->setTableHeight($data->table_height);
$this->setTableWidth($data->table_width);
$this->setMultipleChoice($data->multiple_choice);
$this->setAnswerRequired($data->answer_required);
$this->setChoicesPerPage($data->choices_per_page);
$this->setTableHasBorder($data->table_has_border);
$this->setVideoAutoplay($data->video_autoplay);
$this->setVideoZoom($data->video_zoom);
$this->setTextTitle($data->text_title);
$this->setTextInput($data->text_input);
$this->setTextValue($data->text_value);
$this->setTextCols($data->text_cols);
$this->setTextRows($data->text_rows);
$this->setTextCharcount($data->text_charcount);
$this->setTextCharcounter($data->text_charcounter);
$this->setTextPos($data->text_pos);
}
}
}
function create() {
if ($id = parent::create()) {
$bi1 = "";
$bi2 = "";
if ($this->getBodyImage()) {
$bi1 = "body_image, ";
$bi2 = "'".$this->getBodyImage()."', ";
}
$insert = query_insert("INSERT INTO " . DB_PREFIX . $this->getTable() . " (base_id, ".$bi1."body_video, table_height, table_width, multiple_choice, answer_required, choices_per_page, table_has_border, video_zoom, video_autoplay, text_title, text_input, text_value, text_cols, text_rows, text_charcount, text_charcounter, text_pos) values (".$id.", ".$bi2."'".mysql_real_escape_string($this->getBodyVideo())."', ".mysql_real_escape_string($this->getTableHeight()).", ".mysql_real_escape_string($this->getTableWidth()).", ".$this->getMultipleChoice().", ".$this->getAnswerRequired().", ".mysql_real_escape_string($this->getChoicesPerPage()).", ".$this->getTableHasBorder().", ".$this->getVideoZoom().", ".$this->getVideoAutoplay().", '".mysql_real_escape_string($this->text_title)."', ".$this->text_input.", '".mysql_real_escape_string($this->text_value)."', ".$this->text_cols.", ".$this->text_rows.", ".$this->text_charcount.", ".$this->text_charcounter.", ".$this->text_pos.")");
return $id;
}
return false;
}
function save() {
if (parent::save()) {
$bi = "";
if ($this->getBodyImage()) $bi = "body_image='".$this->getBodyImage()."',";
if (query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET ".$bi." body_video='".mysql_real_escape_string($this->getBodyVideo())."', table_height=".mysql_real_escape_string($this->getTableHeight()).", table_width=".mysql_real_escape_string($this->getTableWidth()).", multiple_choice=".$this->getMultipleChoice().", answer_required=".$this->getAnswerRequired().", choices_per_page=".mysql_real_escape_string($this->getChoicesPerPage()).", table_has_border=".$this->getTableHasBorder().", video_zoom=".$this->getVideoZoom().", video_autoplay=".$this->getVideoAutoplay().", text_title='".mysql_real_escape_string($this->text_title)."', text_input =".$this->text_input.", text_value='".mysql_real_escape_string($this->text_value)."', text_cols=".$this->text_cols.", text_rows=".$this->text_rows.", text_charcount=".$this->text_charcount.", text_charcounter=".$this->text_charcounter.", text_pos=".$this->text_pos." WHERE base_id=".$this->getId())) return $this->getId();
}
return false;
}
function copy($locked=false) {
if ($this->getBodyImage()) {
$image = new Image($this->getBodyImage());
$this->setBodyImage($image->copy($locked));
}
$copy = $this->create();
if ($copy){
if ($locked) {
$lock = $this->lock($copy);
}
$choices = $this->getChoices();
foreach ($choices as $choice) {
$choice->setParent($copy);
if (!$choice->copy($locked)) return false;
}
return true;
}
return false;
}
function delete() {
if (is_admin() || $this->getCreator()==get_logged_in_user()->getId()) {
$choices = $this->getChoices();
if (parent::delete()) {
if ($this->getBodyImage()) {
$img = new Image($this->getBodyImage());
$img->delete();
}
foreach ($choices as $choice) {
$choice->delete();
}
return true;
}
}
return false;
}
function answer($data) {
$textfield = "";
if (isset($data['textfield_'.$this->getId()])) $textfield = $data['textfield_'.$this->getId()];
$selected_choices = array();
if (isset($data['ch_answer_'.$this->getId()])) {
$selected_choices = $data['ch_answer_'.$this->getId()];
}
if ($this->getAnswerRequired() && empty($textfield) && empty($selected_choices)) {
return false;
}
$examinee = get_examinee()->getId();
$timer = parent::timer($data);
if ($timer) {
query_insert("INSERT INTO " . DB_PREFIX . "choice_answer ".
"(base_id, examinee, textfield, time_load, time_start, time_end, time_submit) values ".
"(".$this->getId().", ".$examinee.", '".mysql_real_escape_string($textfield)."', ".
$timer["load"].", ".$timer["start"].", ".$timer["end"].", ".$timer["submit"].
") ON DUPLICATE KEY UPDATE textfield='".mysql_real_escape_string($textfield)."', ".
"time_load=".$timer["load"].", time_start=".$timer["start"].", time_end=".$timer["end"].", time_submit=".$timer["submit"]);
}
$choices = $this->getChoices(1);
foreach ($choices as $choice) {
if (!$choice->answer($data)) return false;
}
return true;
}
function collect($examinees) {
$answers = array();
$start = "start_time";
$end = "end_time";
$submit = "submit_time";
$answers['title'][$start] = _("Start time");
$answers['title'][$end] = _("End time");
$answers['title'][$submit] = _("Submit time");
if ($this->getTextInput()) {
$textfield = "textfield";
$title = _("Text");
if ($this->getTextTitle()) $title = $this->getTextTitle();
$answers ['title'][$textfield]= $title;
}
foreach ($examinees as $examinee) {
$answer = query_row("SELECT * FROM " . DB_PREFIX . "choice_answer a LEFT JOIN " . DB_PREFIX . "choice_task c ON c.base_id=a.base_id WHERE c.base_id=".$this->getId()." AND a.examinee=".$examinee);
if ($answer) {
$answers[$examinee][$start] = $answer->time_start;
$answers[$examinee][$end] = $answer->time_end;
$answers[$examinee][$submit] = $answer->time_submit;
if ($this->getTextInput()) {
$answers[$examinee][$textfield] = $answer->textfield;
}
} else {
$answers[$examinee][$start] = "-";
$answers[$examinee][$end] = "-";
$answers[$examinee][$submit] = "-";
if ($this->getTextInput()) {
$answers[$examinee][$textfield] = "-";
}
}
}
$ca = array('selection'=>array());
$choices = $this->getChoices(1);
foreach ($choices as $choice) {
$id = "choice_".$choice->getId();
$title = _("None")." ".$choice->getId();
if ($choice->getTitle()) $title = $choice->getTitle();
$ca[$id] = array('id'=>$choice->getId(), 'title'=>$title, 'answers'=>$choice->collect($examinees));
}
if (count($ca) > 1 && !$this->getMultipleChoice()) {
$choice_counter = 1;
$choice_selection_title = _("Selection");
$choice_selection_answers = array();
$choice_selection_answers["title"] = array("choice_selection_".$this->getId() => $choice_selection_title);
foreach ($ca as $c) {
if (isset($c["id"])) {
$cid = $c["id"];
foreach ($c["answers"]as $e => $a) {
if ($e != "title") {
if (!array_key_exists($e, $choice_selection_answers)) $choice_selection_answers[$e]["choice_selection_".$this->getId()] = "-";
if ($a["choice_".$cid] > 0) {
$choice_selection_answers[$e]["choice_selection_".$this->getId()] = $choice_counter;
}
}
}
$choice_counter++;
}
}
$choice_selection = array("id"=>$this->getId(), "title"=> $choice_selection_title, "answers" => $choice_selection_answers);
$ca['selection'] = $choice_selection;
}else {
unset($ca['selection']);
}
if ($answers || $ca) {
$id = "choicetask_".$this->getId();
$title = _("None")." ".$this->getId();
if ($this->getTitle()) $title = $this->getTitle();
return array('id'=>$id, 'title'=>$title, 'answers'=>$answers, 'choices'=>$ca);
}
return array();
}
function build($data) {
if (parent::build($data)) {
$body_image = get_file("body_image");
if (!isset($body_image["error"]) || $body_image["error"] == 0) {
if ($this->getBodyImage() > 0) {
$image = new Image($this->getBodyImage());
} else {
$image = new Image();
}
$image_id = $image->upload($body_image);
if ($image_id) {
$this->setBodyImage($image_id);
}
} else if (get_input("delete_body_image", false)) {
$image = new Image($this->getBodyImage());
if ($image->delete()) $this->setBodyImage(NULL);
}
if (isset($data["body_video"]) && !empty($data["body_video"])) $this->setBodyVideo($data["body_video"]);
$this->setVideoAutoplay(0);
if (isset($data["video_autoplay"])) $this->setVideoAutoplay($data["video_autoplay"]);
if (isset($data["video_zoom"])) $this->setVideoZoom($data["video_zoom"]);
if (isset($data["table_height"])) {
if (is_numeric($data["table_height"])) {
$this->setTableHeight($data["table_height"]);
} else {
$this->addBuildError(_("Table height"));
}
}
if (isset($data["table_width"])) {
if (is_numeric($data["table_width"])) {
$this->setTableWidth($data["table_width"]);
} else {
$this->addBuildError(_("Table width"));
}
}
$this->setMultipleChoice(0);
if (isset($data["multiple_choice"])) $this->setMultipleChoice($data["multiple_choice"]);
$this->setAnswerRequired(0);
if (isset($data["answer_required"])) $this->setAnswerRequired($data["answer_required"]);
if (isset($data["choices_per_page"])) {
if (is_numeric($data["choices_per_page"])) {
$this->setChoicesPerPage($data["choices_per_page"]);
} else {
$this->addBuildError(_("Choices per page"));
}
}
$this->setTableHasBorder(0);
if (isset($data["table_has_border"])) $this->setTableHasBorder($data["table_has_border"]);
$this->setTextInput(0);
if (isset($data["text_input"])) $this->setTextInput($data["text_input"]);
if (isset($data["text_title"])) $this->setTextTitle($data["text_title"]);
if (isset($data["text_value"])) $this->setTextValue($data["text_value"]);
if (isset($data["text_cols"])) $this->setTextCols($data["text_cols"]);
if (isset($data["text_rows"])) $this->setTextRows($data["text_rows"]);
if (isset($data["text_charcount"])) {
if (is_numeric($data["text_charcount"])) {
$this->setTextCharcount($data["text_charcount"]);
} else {
$this->setTextCharcount(0);
}
}
$this->setTextCharcounter(0);
if (isset($data["text_charcounter"])) $this->setTextCharcounter($data["text_charcounter"]);
if (isset($data["text_pos"])) $this->setTextPos($data["text_pos"]);
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 compose($data) {
$nr = 0;
foreach ($this->getChoices() as $choice) {
$choice->setParent($this->getId());
$choice->build($nr, $data);
$nr++;
}
return true;
}
function getBodyImage() {
return $this->body_image;
}
function setBodyImage($body_image) {
$this->body_image = $body_image;
}
function getBodyVideoView() {
$video = new YoutubeVideo($this->body_video);
$video->resize($this->getVideoZoom());
if ($this->getVideoAutoplay()) {
$video->setAutoplay(1);
}
return $video->view();
}
function getBodyVideo() {
return $this->body_video;
}
function setBodyVideo($body_video) {
$this->body_video = $body_video;
}
function getVideoZoom() {
return $this->video_zoom;
}
function setVideoZoom($video_zoom) {
$this->video_zoom = $video_zoom;
}
function getVideoAutoplay() {
return $this->video_autoplay;
}
function setVideoAutoplay($video_autoplay) {
if ($video_autoplay) {
$this->video_autoplay = 1;
} else {
$this->video_autoplay = 0;
}
}
function getTableHeight() {
return $this->table_height;
}
function setTableHeight($table_height) {
$this->table_height = $table_height;
}
function getTableWidth() {
return $this->table_width;
}
function setTableWidth($table_width) {
$this->table_width = $table_width;
}
function getMultipleChoice() {
return $this->multiple_choice;
}
function setMultipleChoice($table_has_border) {
if ($table_has_border) {
$this->multiple_choice = 1;
} else {
$this->multiple_choice = 0;
}
}
function getAnswerRequired() {
return $this->answer_required;
}
function setAnswerRequired($answer_required) {
if ($answer_required) {
$this->answer_required = 1;
} else {
$this->answer_required = 0;
}
}
function getTableHasBorder() {
return $this->table_has_border;
}
function setTableHasBorder($table_has_border) {
if ($table_has_border) {
$this->table_has_border = 1;
} else {
$this->table_has_border = 0;
}
}
function getTextTitle() {
return $this->text_title;
}
function setTextTitle($text_title) {
$this->text_title = $text_title;
}
function getTextValue() {
return $this->text_value;
}
function setTextValue($text_value) {
$this->text_value = $text_value;
}
function getTextInput() {
return $this->text_input;
}
function setTextInput($text_input) {
if ($text_input) {
$this->text_input = 1;
} else {
$this->text_input = 0;
}
}
function getTextCols() {
return $this->text_cols;
}
function setTextCols($text_cols) {
$this->text_cols = $text_cols;
}
function getTextRows() {
return $this->text_rows;
}
function setTextRows($text_rows) {
$this->text_rows = $text_rows;
}
function getTextCharcount() {
return $this->text_charcount;
}
function setTextCharcount($text_charcount) {
$text_charcount = (int) $text_charcount;
if (is_numeric($text_charcount)) {
$this->text_charcount = $text_charcount;
}
}
function getTextCharcounter() {
return $this->text_charcounter;
}
function setTextCharcounter($text_charcounter) {
if ($text_charcounter) {
$this->text_charcounter = 1;
} else {
$this->text_charcounter = 0;
}
}
function getTextPos() {
return $this->text_pos;
}
function setTextPos($text_pos) {
$this->text_pos = $text_pos;
}
function getChoicesPerPage() {
return $this->choices_per_page;
}
function setChoicesPerPage($choices_per_page) {
$this->choices_per_page = $choices_per_page;
}
/* function getChoices() {
$choices = array();
foreach(query_rows("SELECT id FROM " . DB_PREFIX . "choices WHERE parent=".$this->getId()) as $ch) {
$choices []= new ChoiceObject($ch->id);
}
for ($i=count($choices); $i<($this->getTableHeight()*$this->getTableWidth()); $i++) {
$choices []= new ChoiceObject();
}
return $choices;
}
*/
function getChoices($existing = false) {
$choices = array();
if ($this->getId() > 0) {
foreach(query_rows("SELECT id FROM " . DB_PREFIX . "choices WHERE parent =".$this->getId()." ORDER BY pos ASC") as $ch) {
$choices []= new ChoiceObject($ch->id);
}
}
if (!$existing) {
for ($i=count($choices); $i<($this->getTableHeight()*$this->getTableWidth()); $i++) {
$choices []= new ChoiceObject();
}
}
return $choices;
}
function buildFromXML($obj, $folder=0, $dir, $version=1) {
parent::buildFromXML($obj, $folder, $dir, $version);
$img = $obj->imageFile[0][0];
if ($obj->imageFile[0][0]) {
$image = new Image();
$image_id = $image->save_from_dir($dir, $obj->imageFile[0][0]);
if ($image_id > 0) {
$this->setBodyImage($image_id);
}
}
if ($version==2) {
$this->setBodyVideo($obj->body_video[0][0]);
$this->setVideoAutoplay($obj->video_autoplay[0][0]);
$this->setVideoZoom($obj->video_zoom[0][0]);
$this->setTextInput($obj->text_input[0][0]);
$this->setTextTitle($obj->text_title[0][0]);
$this->setTextValue($obj->text_value[0][0]);
$this->setTextCols($obj->text_cols[0][0]);
$this->setTextRows($obj->text_rows[0][0]);
}
$this->setTableHeight($obj->tableHeight[0][0]);
$this->setTableWidth($obj->tableWidth[0][0]);
if (isset($obj->multipleChoice[0][0]) && $obj->multipleChoice[0][0] && $obj->multipleChoice[0][0] != "False") {
$this->setMultipleChoice(1);
} else {
$this->setMultipleChoice(0);
}
if (isset($obj->answerRequired[0][0]) && $obj->answerRequired[0][0] && $obj->answerRequired[0][0] != "False") {
$this->setAnswerRequired(1);
} else {
$this->setAnswerRequired(0);
}
if (isset($obj->choicesPerPage[0][0]) && $obj->choicesPerPage[0][0] && $obj->choicesPerPage[0][0] != "False") {
$this->setChoicesPerPage(1);
} else {
$this->setChoicesPerPage(0);
}
if (isset($obj->tableHasBorder[0][0]) && $obj->tableHasBorder[0][0] && $obj->tableHasBorder[0][0] != "False") {
$this->setTableHasBorder(1);
} else {
$this->setTableHasBorder(0);
}
return $this->create();
}
function getObjectsXML() {
$xml = "getElements()."]]>\n";
return $xml;
}
}
?>