From c93f98f8d8bc99fd847aad4c8eac2172ed23a9b9 Mon Sep 17 00:00:00 2001 From: coderkun Date: Sun, 17 Aug 2014 20:30:55 +0200 Subject: [PATCH] implement task editing for Questtype ?Choiceinput? (Issue #36) --- configs/AppConfig.inc | 4 + .../ChoiceinputQuesttypeController.inc | 121 +++++++++++- .../choiceinput/ChoiceinputQuesttypeModel.inc | 142 ++++++++++++++ questtypes/choiceinput/html/edittask.tpl | 180 +++++++++++++++++- 4 files changed, 445 insertions(+), 2 deletions(-) diff --git a/configs/AppConfig.inc b/configs/AppConfig.inc index f243a49b..1b86c448 100644 --- a/configs/AppConfig.inc +++ b/configs/AppConfig.inc @@ -222,6 +222,10 @@ ), 'deadline' => array( 'regex' => '/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})?$/' + ), + 'choice' => array( + 'minlength' => 1, + 'maxlength' => 128 ) ); diff --git a/questtypes/choiceinput/ChoiceinputQuesttypeController.inc b/questtypes/choiceinput/ChoiceinputQuesttypeController.inc index 7ed38715..f92bd221 100644 --- a/questtypes/choiceinput/ChoiceinputQuesttypeController.inc +++ b/questtypes/choiceinput/ChoiceinputQuesttypeController.inc @@ -20,6 +20,12 @@ */ class ChoiceinputQuesttypeController extends \hhu\z\controllers\QuesttypeController { + /** + * Required components + * + * @var array + */ + public $components = array('validation'); @@ -162,7 +168,7 @@ /** - * TODO Action: edittask. + * Action: edittask. * * Edit the task of a Quest. * @@ -172,6 +178,119 @@ */ public function edittask($seminary, $questgroup, $quest) { + // Get Task + $task = $this->Choiceinput->getChoiceinputQuest($quest['id']); + $text = $task['text']; + + // Get lists + $choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']); + foreach($choiceLists as &$list) + { + $list['choices'] = $this->Choiceinput->getChoiceinputChoices($list['id']); + foreach($list['choices'] as $index => &$choice) { + //$choice['correct'] = ($choice['id'] == $list['questtypes_choiceinput_choice_id']); + if($choice['id'] == $list['questtypes_choiceinput_choice_id']) { + $list['answer'] = $index; + } + $choice = $choice['text']; + } + //$list = $list['choices']; + } + + // Values + $validations = array(); + + // Save data + if($this->request->getRequestMethod() == 'POST') + { + if(!is_null($this->request->getPostParam('preview')) || !is_null($this->request->getPostParam('save'))) + { + // Get params and validate them + if(is_null($this->request->getPostParam('text'))) { + throw new \nre\exceptions\ParamsNotValidException('text'); + } + $text = $this->request->getPostParam('text'); + if(is_null($this->request->getPostParam('lists'))) { + throw new \nre\exceptions\ParamsNotValidException('lists'); + } + $choiceLists = $this->request->getPostParam('lists'); + $choiceLists = array_values($choiceLists); + foreach($choiceLists as $listIndex => &$list) + { + // Validate choices + if(!array_key_exists('choices', $list)) { + throw new \nre\exceptions\ParamsNotValidException('choices'); + } + $choiceIndex = 0; + $answer = null; + foreach($list['choices'] as $index => &$choice) + { + // Validate choice + $choiceValidation = $this->Validation->validate($choice, \nre\configs\AppConfig::$validation['choice']); + if($choiceValidation !== true) + { + if(!array_key_exists($listIndex, $validations) || !is_array($validations[$listIndex])) { + $validations[$listIndex] = array(); + } + if(!array_key_exists('choices', $validations[$listIndex])) { + $validations[$listIndex]['choices'] = array(); + } + $validations[$listIndex]['choices'][$choiceIndex] = $choiceValidation; + } + + $choiceIndex++; + if(array_key_exists('answer', $list) && $list['answer'] == $index) { + $answer = $choiceIndex; + } + } + + // Validate correct answer + if(is_null($answer)) + { + if(!array_key_exists($listIndex, $validations) || !is_array($validations[$listIndex])) { + $validations[$listIndex] = array(); + } + if(!array_key_exists('answer', $validations[$listIndex])) { + $validations[$listIndex]['answer'] = array(); + } + $validations[$listIndex] = $this->Validation->addValidationResult($validations[$listIndex], 'answer', 'exist', true); + } + } + + // Save and redirect + if(!is_null($this->request->getPostParam('save')) && empty($validations)) + { + // Save text + $this->Choiceinput->setTextForQuest( + $this->Auth->getUserId(), + $quest['id'], + $text + ); + + // Save lists and choices + foreach($choiceLists as $listIndex => &$list) + { + // Save list + $this->Choiceinput->setListForText( + $quest['id'], + $listIndex + 1, + $list['choices'], + $list['answer'] + 1 + ); + } + + // Redirect + $this->redirect($this->linker->link(array('quest', $seminary['url'], $questgroup['url'], $quest['url']), 1)); + } + } + } + + + // Pass data to view + $this->set('task', $task); + $this->set('text', $text); + $this->set('choiceLists', $choiceLists); + $this->set('validations', $validations); } } diff --git a/questtypes/choiceinput/ChoiceinputQuesttypeModel.inc b/questtypes/choiceinput/ChoiceinputQuesttypeModel.inc index d35b3baf..2c7151bf 100644 --- a/questtypes/choiceinput/ChoiceinputQuesttypeModel.inc +++ b/questtypes/choiceinput/ChoiceinputQuesttypeModel.inc @@ -147,6 +147,148 @@ return null; } + + + /** + * Set the text for a Quest and correct choice input lists count. + * + * @param int $userId ID of user setting text + * @param int $questId ID of Quest to set text for + * @param string $text Text for Quest + */ + public function setTextForQuest($userId, $questId, $text) + { + $this->db->setAutocommit(false); + try { + // Set text + $this->db->query( + 'INSERT INTO questtypes_choiceinput '. + '(quest_id, created_user_id, text) '. + 'VALUES '. + '(?, ?, ?) '. + 'ON DUPLICATE KEY UPDATE '. + 'text = ?', + 'iiss', + $questId, + $userId, + $text, + $text + ); + + // Count fields + $listCount = substr_count($text, '[choiceinput]'); + + // Remove fields + $this->db->query( + 'DELETE FROM questtypes_choiceinput_lists '. + 'WHERE questtypes_choiceinput_quest_id = ? AND number > ?', + 'ii', + $questId, + $listCount + ); + + // Add fields + for($i=1; $i<=$listCount; $i++) + { + $this->db->query( + 'INSERT IGNORE INTO questtypes_choiceinput_lists '. + '(questtypes_choiceinput_quest_id, number, questtypes_choiceinput_choice_id) '. + 'VALUES '. + '(?, ?, NULL) ', + 'ii', + $questId, + $i + ); + } + + $this->db->commit(); + } + catch(\Exception $e) { + $this->db->rollback(); + $this->db->setAutocommit(true); + throw $e; + } + + $this->db->setAutocommit(true); + } + + + /** + * Set list of choices for a text. + * + * @param int $questId ID of Quest to set choices for + * @param int $number List number + * @param array $choices List of choices + * @param int $correctPos Position of correct answer + */ + public function setListForText($questId, $number, $choices, $correctPos) + { + // Get ID of list + $listId = $this->db->query( + 'SELECT id '. + 'FROM questtypes_choiceinput_lists '. + 'WHERE questtypes_choiceinput_quest_id = ? AND number = ?', + 'ii', + $questId, + $number + ); + $listId = $listId[0]['id']; + + // Manage choices + $this->db->setAutocommit(false); + try { + // Remove choices + $this->db->query( + 'DELETE FROM questtypes_choiceinput_choices '. + 'WHERE questtypes_choiceinput_list_id = ? AND pos > ?', + 'ii', + $listId, + count($choices) + ); + + // Add choices + foreach($choices as $index => &$choice) + { + $this->db->query( + 'INSERT INTO questtypes_choiceinput_choices '. + '(questtypes_choiceinput_list_id, pos, text) '. + 'VALUES '. + '(?, ?, ?) '. + 'ON DUPLICATE KEY UPDATE '. + 'text = ?', + 'iiss', + $listId, + $index + 1, + $choice, + $choice + ); + } + + // Set correct choice for list + $this->db->query( + 'UPDATE questtypes_choiceinput_lists '. + 'SET questtypes_choiceinput_choice_id = ('. + 'SELECT id '. + 'FROM questtypes_choiceinput_choices '. + 'WHERE questtypes_choiceinput_list_id = ? AND pos = ?'. + ') '. + 'WHERE id = ?', + 'iii', + $listId, + $correctPos, + $listId + ); + + $this->db->commit(); + } + catch(\Exception $e) { + $this->db->rollback(); + $this->db->setAutocommit(true); + throw $e; + } + + $this->db->setAutocommit(true); + } } diff --git a/questtypes/choiceinput/html/edittask.tpl b/questtypes/choiceinput/html/edittask.tpl index c65158e7..b89322f1 100644 --- a/questtypes/choiceinput/html/edittask.tpl +++ b/questtypes/choiceinput/html/edittask.tpl @@ -1 +1,179 @@ -

TODO

+ + + +
+
+ +
+ +
+
+ +
    + &$list) : ?> + +
      + $value) : ?> +
    • + +
    • + +
    + +
  • +
      + &$choice) : ?> +
    • + checked="checked" /> + class="invalid" /> + +
    • + +
    • + +
    • +
    +
  • + +
+
+ + +
+

+

+ + + + t(mb_substr($text, $posStart, $posEnd-$posStart, 'UTF-8'))?> + + + + t(mb_substr($text, $posStart, mb_strlen($text, 'UTF-8')-$posStart, 'UTF-8'))?> +

+ +