setType("SoundTask"); $this->setName("Sound Task"); $this->setTable("sound_task"); $this->setActions(array("view"=>_("View"), "edit"=>_("Edit"), "compose"=>_("Compose"))); if ($id) { $data = $this->load($id); if ($data) { $this->setSound($data->sound); $this->setSoundAutoplay($data->sound_autoplay); $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->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()) { $insert = query_insert("INSERT INTO " . DB_PREFIX . $this->getTable() . " (base_id, table_height, table_width, multiple_choice, answer_required, choices_per_page, table_has_border, sound_autoplay, text_title, text_input, text_value, text_cols, text_rows, text_charcount, text_charcounter, text_pos) values (".$id.", ".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->getSoundAutoplay().", '".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.")"); if ($insert) { $this->setId($id); $this->save_sound(); return $id; } } return false; } function save() { if (parent::save()) { $upd = query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET 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().", sound_autoplay=".$this->getSoundAutoplay().", 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()); if ($upd) { $this->save_sound(); return $this->getId(); } } return false; } function copy($locked=false) { $choices = $this->getChoices(1); $copy = $this->create(); if ($copy) { if ($locked) { $lock = $this->lock($copy); } foreach ($choices as $choice) { $choice->setParent($copy); if (!$choice->copy($locked)) return false; } if ($this->getSound()) { $sound = new Sound($this->getSound()); $this->load($copy); $this->setSound($sound->copy($locked)); } return true; } return false; } function save_sound() { if ($this->getSound()) { return query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET sound=".$this->getSound()." WHERE base_id=".$this->getId()); } return query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET sound=NULL WHERE base_id=".$this->getId()); } function delete() { if (is_admin() || $this->getCreator()==get_logged_in_user()->getId()) { $choices = $this->getChoices(); if (parent::delete()) { if ($this->getSound()) { $img = new Sound($this->getSound()); $img->delete(); } foreach ($choices as $choice) { $choice->delete(); } return true; } } return false; } function answer($data) { $examinee = get_examinee()->getId(); $textfield = ""; if (isset($data['textfield_'.$this->getId()])) $textfield = $data['textfield_'.$this->getId()]; $timer = parent::timer($data); if ($timer) { query_insert("INSERT INTO " . DB_PREFIX . "sound_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 . "sound_task s LEFT JOIN " . DB_PREFIX . "sound_answer a ON s.base_id=a.base_id WHERE s.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 ($ca && !$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 = "soundtask_".$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)) { $sound_file = get_file("sound_file"); if (!isset($sound_file["error"]) || $sound_file["error"] == 0) { if ($this->getSound() > 0) { $sound = new Sound($this->getSound()); } else { $sound = new Sound(); } $sound_id = $sound->upload($sound_file); if ($sound_id) { $this->setSound($sound_id); } } else if (get_input("delete_sound", false)) { $sound = new Sound($this->getSound()); if ($sound->delete()) $this->setSound(NULL); } $this->setSoundAutoplay(0); if (isset($data["sound_autoplay"])) $this->setSoundAutoplay($data["sound_autoplay"]); 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 getSound() { return $this->sound; } function setSound($sound) { $this->sound = $sound; } function getListenSound() { $sound = new Sound($this->sound); return $sound->listen(); } function getSoundAutoplay() { return $this->sound_autoplay; } function setSoundAutoplay($sound_autoplay) { if ($sound_autoplay) { $this->sound_autoplay = 1; } else { $this->sound_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); $snd = $obj->file[0][0]; if ($snd) { $sound = new Sound(); $sound_id = $sound->save_from_dir($dir, $obj->file[0][0]); if ($sound_id > 0) { $this->setSound($sound_id); } } if ($version==2) { $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]); } if (isset($obj->autoPlay[0][0]) && $obj->autoPlay[0][0] && $obj->autoPlay[0][0] != "False") { $this->setSoundAutoplay(1); } else { $this->setSoundAutoplay(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; } } ?>