implement copying of Stations for Seminary copy feature

This commit is contained in:
oliver 2016-01-30 20:15:13 +01:00
commit 40a233fa7d
4342 changed files with 1215466 additions and 0 deletions

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 keyword access.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class KeywordStationtypeAgent extends \hhu\z\agents\StationtypeAgent
{
}
?>

View file

@ -0,0 +1,168 @@
<?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 keyword access.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class KeywordStationtypeController extends \hhu\z\controllers\StationtypeController
{
/**
* Required components
*
* @var array
*/
public $components = array('validation', 'questtypedata');
/**
* Save the answer of a Character group for 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
* @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)
{
$this->Keyword->setCharactergroupSubmission($station['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 $groupsgroup Current Groups group 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)
{
// Get right answers
$task = $this->Keyword->getKeywordTask($station['id']);
// Match regex with user answers
return $this->isMatching($task['keyword_regex'], $answer);
}
/**
* Action: quest.
*
* Show 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
* @param array $charactergroup Current Character group data
*/
public function quest($seminary, $groupsgroup, $quest, $station, $charactergroup)
{
}
/**
* 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 right answers
$task = $this->Keyword->getKeywordTask($station['id']);
// Values
$keyword = $task['keyword_regex'];
$validations = array();
// Save data
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('save')))
{
// Get params and validate them
$keyword = $this->request->getPostParam('keyword');
// Validate regex
$keywordValidation = @preg_match($keyword, '') !== false;
if($keywordValidation !== true) {
$validations = $this->Validation->addValidationResult($validations, 'keyword', 'regex', $keywordValidation);
}
// Save and redirect
if(empty($validations))
{
// Save keyword
$this->Keyword->setKeywordForStation(
$this->Auth->getUserId(),
$station['id'],
$keyword
);
// Redirect
$this->redirect(
$this->linker->link(
array(
'station',
$seminary['url'],
$groupsgroup['url'],
$quest['url'],
$station['url']
),
1
)
);
}
}
// Pass data to view
$this->set('keyword', $keyword);
$this->set('validations', $validations);
}
/**
* 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,121 @@
<?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 keyword access.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class KeywordStationtypeModel extends \hhu\z\models\StationtypeModel
{
/**
* Copy a Station.
*
* @param int $userId ID of creating user
* @param int $sourceStationId ID of Station to copy from
* @param int $targetStationId ID of Station to copy to
* @param int $seminaryMediaIds Mapping of SeminaryMedia-IDs from source Seminary to targetSeminary
*/
public function copyStation($userId, $sourceStationId, $targetStationId, $seminaryMediaIds)
{
// Copy keyword
$this->db->query(
'INSERT INTO stationtypes_keyword '.
'(station_id, created_user_id, keyword_regex) '.
'SELECT ?, ?, keyword_regex '.
'FROM stationtypes_keyword '.
'WHERE station_id = ?',
'iii',
$targetStationId, $userId,
$sourceStationId
);
}
/**
* Get the task of a keyword Station
*
* @param int $stationId ID of Station
* @return array Task data
*/
public function getKeywordTask($stationId)
{
$data = $this->db->query(
'SELECT keyword_regex '.
'FROM stationtypes_keyword '.
'WHERE station_id = ?',
'i',
$stationId
);
if(!empty($data)) {
return $data[0];
}
return null;
}
/**
* Set the keyword (regex) for a Station.
*
* @param int $userId ID of creating user
* @param int $stationId ID ot Station to set keyword for
* @param string $keyword Keyword (regex)
*/
public function setKeywordForStation($userId, $stationId, $keyword)
{
$this->db->query(
'INSERT INTO stationtypes_keyword '.
'(station_id, created_user_id, keyword_regex) '.
'VALUES '.
'(?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'created_user_id = ?, keyword_regex = ?',
'iisis',
$stationId, $userId, $keyword,
$userId, $keyword
);
}
/**
* 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($stationId, $charactergroupId, $answer)
{
$this->db->query(
'INSERT INTO stationtypes_keyword_charactergroups '.
'(station_id, charactergroup_id, keyword) '.
'VALUES '.
'(?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'keyword = ?',
'iiss',
$stationId, $charactergroupId, $answer, $answer
);
var_dump("saved");
}
}
?>

View file

@ -0,0 +1,29 @@
<?php if(!empty($validations)) : ?>
<ul>
<?php foreach($validations as $field => &$settings) : ?>
<li>
<ul>
<?php foreach($settings as $setting => $value) : ?>
<li>
<?php
switch($setting) {
case 'regex': echo _('Regex invalid');
break;
default: echo _('Regex invalid');
}
?>
</li>
<?php endforeach ?>
</ul>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
<form method="post">
<fieldset>
<label for="keyword"><?=_('Keyword')?>:</label>
<input type="text" name="keyword" value="<?=$keyword?>" placeholder="/regex/i" />
</fieldset>
<input type="submit" name="save" value="<?=_('save')?>" />
</form>

View file

@ -0,0 +1,4 @@
<form method="post" class="keyword">
<input type="text" id="keyword" name="answer" />
<input type="submit" name="submit" value="<?=_('solve')?>" />
</form>

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,192 @@
<?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);
}
/**
* 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);
}
/**
* 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,141 @@
<?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
{
/**
* Copy a Station.
*
* @param int $userId ID of creating user
* @param int $sourceStationId ID of Station to copy from
* @param int $targetStationId ID of Station to copy to
* @param int $seminaryMediaIds Mapping of SeminaryMedia-IDs from source Seminary to targetSeminary
*/
public function copyStation($userId, $sourceStationId, $targetStationId, $seminaryMediaIds)
{
// Copy answers
$this->db->query(
'INSERT INTO stationtypes_multiplechoice '.
'(created_user_id, station_id, pos, answer, tick) '.
'SELECT ?, ?, pos, answer, tick '.
'FROM stationtypes_multiplechoice '.
'WHERE station_id = ?',
'iii',
$userId, $targetStationId,
$sourceStationId
);
}
/**
* 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
);
}
/**
* 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 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,59 @@
<form method="post">
<fieldset>
<legend><?=_('Question')?></legend>
<?=$t->t($task)?>
</fieldset>
<fieldset>
<legend><?=_('Answers')?></legend>
<ul id="answers">
<?php foreach($answers as $answerIndex => &$answer) : ?>
<li>
<label>
<input id="answer-<?=$answerIndex?>-tick" type="checkbox" name="answers[<?=$answerIndex?>][tick]" <?php if(array_key_exists('tick', $answer) && $answer['tick']) : ?>checked="checked"<?php endif ?> />
<?=_('correct answer')?>
</label>
<textarea id="answer-<?=$answerIndex?>" name="answers[<?=$answerIndex?>][answer]" class="answer">
<?=$answer['answer']?>
</textarea>
<button class="remove-answer" type="button"></button>
<hr />
</li>
<?php endforeach ?>
<li>
<button class="add-answer" type="button">+</button>
</li>
</ul>
</fieldset>
<input type="submit" name="save" value="<?=_('save')?>" />
</form>
<script>
// Text editors
$('textarea.answer').markItUp(mySettings);
// Answers
var answerIndex = <?=count($answers)?>;
var answerElement = '<label><input id="answer-ANSWERINDEX-tick" type="checkbox" name="answers[ANSWERINDEX][tick]" /> <?=_('correct answer')?></label>' +
'<textarea id="answer-ANSWERINDEX" name="answers[ANSWERINDEX][answer]" class="answer"></textarea>' +
'<button class="remove-answer" type="button"></button>' +
'<hr />';
$(".add-answer").click(addAnswer);
$(".remove-answer").click(removeAnswer);
function addAnswer(event)
{
event.preventDefault();
var parent = $(event.target).parent();
var element = '<li>' + answerElement.replace(/ANSWERINDEX/g, answerIndex) + '</li>';
parent.before(element);
$("#answers li:nth-last-child(2) .remove-answer").click(removeAnswer);
$('#answers li:nth-last-child(2) textarea.answer').markItUp(mySettings);
answerIndex++;
}
function removeAnswer(event)
{
event.preventDefault();
$(event.target).parent().remove();
}
</script>

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>