From e2f1a5ae84b12d80d1f78b3ad8de7aec7d3cbccc Mon Sep 17 00:00:00 2001 From: oliver Date: Fri, 15 Jan 2016 12:30:00 +0100 Subject: [PATCH] implement task editing for Stationtype ?multiplechoice? --- .../MultiplechoiceStationtypeController.inc | 65 +++++++++++++++++++ .../MultiplechoiceStationtypeModel.inc | 49 ++++++++++++++ stationtypes/multiplechoice/html/edittask.tpl | 59 +++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 stationtypes/multiplechoice/html/edittask.tpl diff --git a/stationtypes/multiplechoice/MultiplechoiceStationtypeController.inc b/stationtypes/multiplechoice/MultiplechoiceStationtypeController.inc index b2f5d7d7..c1f0bc75 100644 --- a/stationtypes/multiplechoice/MultiplechoiceStationtypeController.inc +++ b/stationtypes/multiplechoice/MultiplechoiceStationtypeController.inc @@ -105,6 +105,71 @@ } + /** + * Action: edittask. + * + * Edit the task of a Station. + * + * @param array $seminary Current Seminary data + * @param array $groupsgroup Current Groups group data + * @param array $quest Current Quest data + * @param array $station Current Station data + */ + public function edittask($seminary, $groupsgroup, $quest, $station) + { + // Get questions + $answers = $this->Multiplechoice->getAnswers($station['id']); + + // Save data + if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('save'))) + { + // Get params + $answers = $this->request->getPostParam('answers'); + if(is_null($answers)) { + $answers = array(); + } + $answers = array_values($answers); + + // Save answers + foreach($answers as $answerIndex => &$answer) + { + $this->Multiplechoice->setAnswer( + $this->Auth->getUserId(), + $station['id'], + $answerIndex + 1, + $answer['answer'], + array_key_exists('tick', $answer) + ); + } + + // Delete deleted answers + $this->Multiplechoice->deleteAnswers( + $station['id'], + count($answers) + ); + + // Redirect + $this->redirect( + $this->linker->link( + array( + 'station', + $seminary['url'], + $groupsgroup['url'], + $quest['url'], + $station['url'] + ), + 1 + ) + ); + } + + + // Pass data to view + $this->set('task', $station['task']); + $this->set('answers', $answers); + } + + /** diff --git a/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc b/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc index d066919d..312f0070 100644 --- a/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc +++ b/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc @@ -42,6 +42,55 @@ } + /** + * Set an answer for a Station. + * + * @param int $userId ID of creating user + * @param int $stationId ID of Station to set answer for + * @param int $pos Position of answer + * @param string $answer Answer text + * @param int $tick Whether the answer is correct or not + */ + public function setAnswer($userId, $stationId, $pos, $answer, $tick) + { + $this->db->query( + 'INSERT INTO stationtypes_multiplechoice '. + '(created_user_id, station_id, pos, answer, tick) '. + 'VALUES '. + '(?, ?, ?, ?, ?) '. + 'ON DUPLICATE KEY UPDATE '. + 'answer = ?, tick = ?', + 'iiisisi', + $userId, + $stationId, + $pos, + $answer, + $tick, + $answer, + $tick + ); + } + + + /** + * Delete all answers of a Station above a given index. + * + * @param int $stationId ID of Station to delete answers for + * @param int $offset Offset to delete answers above + */ + public function deleteAnswers($stationId, $offset) + { + // Delete answers + $this->db->query( + 'DELETE FROM stationtypes_multiplechoice '. + 'WHERE station_id = ? AND pos > ?', + 'ii', + $stationId, + $offset + ); + } + + /** * Save Character group’s submitted answer for a station. * diff --git a/stationtypes/multiplechoice/html/edittask.tpl b/stationtypes/multiplechoice/html/edittask.tpl new file mode 100644 index 00000000..80317f00 --- /dev/null +++ b/stationtypes/multiplechoice/html/edittask.tpl @@ -0,0 +1,59 @@ +
+
+ + t($task)?> +
+
+ +
    + &$answer) : ?> +
  • + + + +
    +
  • + +
  • + +
  • +
+
+ +
+ +