add Stationtype ?multiplechoice?

This commit is contained in:
oliver 2015-12-25 16:30:06 +01:00
parent cb2865c1d5
commit f31032cbb2
5 changed files with 273 additions and 0 deletions

View file

@ -1971,6 +1971,49 @@ CREATE TABLE `stationtypes_keyword_charactergroups` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `stationtypes_multiplechoice`
--
DROP TABLE IF EXISTS `stationtypes_multiplechoice`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `stationtypes_multiplechoice` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_user_id` int(11) NOT NULL,
`station_id` int(11) NOT NULL,
`pos` int(11) NOT NULL,
`answer` text COLLATE utf8mb4_unicode_ci NOT NULL,
`tick` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `questtypes_multiplechoice_id_2` (`station_id`,`pos`),
KEY `created_user_id` (`created_user_id`),
KEY `questtypes_multiplechoice_id` (`station_id`),
CONSTRAINT `stationtypes_multiplechoice_ibfk_1` FOREIGN KEY (`created_user_id`) REFERENCES `users` (`id`),
CONSTRAINT `stationtypes_multiplechoice_ibfk_2` FOREIGN KEY (`station_id`) REFERENCES `charactergroupsqueststations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `stationtypes_multiplechoice_charactergroups`
--
DROP TABLE IF EXISTS `stationtypes_multiplechoice_charactergroups`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `stationtypes_multiplechoice_charactergroups` (
`stationtypes_multiplechoice_id` int(11) NOT NULL,
`charactergroup_id` int(11) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ticked` tinyint(1) NOT NULL,
PRIMARY KEY (`stationtypes_multiplechoice_id`,`charactergroup_id`),
KEY `character_id` (`charactergroup_id`),
CONSTRAINT `stationtypes_multiplechoice_charactergroups_ibfk_1` FOREIGN KEY (`stationtypes_multiplechoice_id`) REFERENCES `stationtypes_multiplechoice` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `stationtypes_multiplechoice_charactergroups_ibfk_2` FOREIGN KEY (`charactergroup_id`) REFERENCES `charactergroups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `userroles`
--

View file

@ -0,0 +1,24 @@
<?php
/**
* The Legend of Z
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
* @copyright 2014 Heinrich-Heine-Universität Düsseldorf
* @license http://www.gnu.org/licenses/gpl.html
* @link https://bitbucket.org/coderkun/the-legend-of-z
*/
namespace hhu\z\stationtypes;
/**
* StationtypeAgent for a multiple choice task.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class MultiplechoiceStationtypeAgent extends \hhu\z\agents\StationtypeAgent
{
}
?>

View file

@ -0,0 +1,127 @@
<?php
/**
* The Legend of Z
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
* @copyright 2014 Heinrich-Heine-Universität Düsseldorf
* @license http://www.gnu.org/licenses/gpl.html
* @link https://bitbucket.org/coderkun/the-legend-of-z
*/
namespace hhu\z\stationtypes;
/**
* Controller of the StationtypeAgent for a multiple choice task.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class MultiplechoiceStationtypeController extends \hhu\z\controllers\StationtypeController
{
/**
* Save the answer of a Character group for a Station.
*
* @param array $seminary Current Seminary data
* @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data
* @param array $station Current Station data
* @param array $charactergroup Current Character group data
* @param array $answer Character group answer for the Station
*/
public function saveAnswer($seminary, $groupsgroup, $quest, $station, $charactergroup, $answer)
{
$answers = (!is_array($answer)) ? array() : $answer;
$solutions = $this->Multiplechoice->getAnswers($station['id']);
foreach($solutions as &$solution)
{
$answer = (array_key_exists($solution['pos']-1, $answers)) ? true : false;
$this->Multiplechoice->setCharactergroupSubmission($solution['id'], $charactergroup['id'], $answer);
}
}
/**
* Check if answer of a Character group for a Station matches the correct one.
*
* @param array $seminary Current Seminary data
* @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data
* @param array $station Current Station data
* @param array $charactergroup Current Character group data
* @param array $answer Character group answer for the Station
* @return boolean True/false for a right/wrong answer
*/
public function matchAnswer($seminary, $groupsgroup, $quest, $station, $charactergroup, $answer)
{
$answers = (!is_array($answer)) ? array() : $answer;
$solutions = $this->Multiplechoice->getAnswers($station['id']);
foreach($solutions as &$solution)
{
if(is_null($solution['tick'])) {
continue;
}
if($solution['tick']) {
if(!array_key_exists($solution['pos']-1, $answers)) {
return false;
}
}
else {
if(array_key_exists($solution['pos']-1, $answers)) {
return false;
}
}
}
// All questions correct answerd
return true;
}
/**
* Action: quest.
*
* Show the task of a Station.
*
* @param array $seminary Current Seminary data
* @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data
* @param array $station Current Station data
* @param array $charactergroup Current Character group data
*/
public function quest($seminary, $groupsgroup, $quest, $station, $charactergroup)
{
// Get answers
$answers = $this->Multiplechoice->getAnswers($station['id']);
// Pass data to view
$this->set('answers', $answers);
}
/**
* Check if an Character answer matches a Regex.
*
* @param string $regex Regex to match against
* @param string $answer Character answer to match
* @return boolean Whether answer matches Regex or not
*/
private function isMatching($regex, $answer)
{
$score = preg_match($regex, trim($answer));
return ($score !== false && $score > 0);
}
}
?>

View file

@ -0,0 +1,68 @@
<?php
/**
* The Legend of Z
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
* @copyright 2014 Heinrich-Heine-Universität Düsseldorf
* @license http://www.gnu.org/licenses/gpl.html
* @link https://bitbucket.org/coderkun/the-legend-of-z
*/
namespace hhu\z\stationtypes;
/**
* Model of the StationtypeAgent for a multiple choice task.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class MultiplechoiceStationtypeModel extends \hhu\z\models\StationtypeModel
{
/**
* Get all answers for a Station
*
* @param int $stationId ID of Station
* @return array List of answers
*/
public function getAnswers($stationId)
{
return $this->db->query(
'SELECT id, pos, answer, tick '.
'FROM stationtypes_multiplechoice '.
'WHERE station_id = ? '.
'ORDER BY pos',
'i',
$stationId
);
}
/**
* Save Character groups submitted answer for a station.
*
* @param int $stationId ID of Station
* @param int $charactergroupId ID of Character group
* @param string $answer Submitted answer
*/
public function setCharactergroupSubmission($answerId, $charactergroupId, $answer)
{
$this->db->query(
'INSERT INTO stationtypes_multiplechoice_charactergroups '.
'(stationtypes_multiplechoice_id, charactergroup_id, ticked) '.
'VALUES '.
'(?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'ticked = ?',
'iiii',
$answerId, $charactergroupId, $answer, $answer
);
}
}
?>

View file

@ -0,0 +1,11 @@
<form method="post" class="multiplechoice">
<ol class="mchoice">
<?php foreach($answers as $i => &$answer) : ?>
<li class="cf">
<input type="checkbox" id="answer[<?=$i?>]" name="answer[<?=$i?>]" value="true" />
<label for="answer[<?=$i?>]"><?=$t->t($answer['answer'])?></label>
</li>
<?php endforeach ?>
</ol>
<input type="submit" name="submit" value="<?=_('solve')?>" />
</form>