Skip to content

Instantly share code, notes, and snippets.

@codezi
Last active February 29, 2020 14:05
Show Gist options
  • Save codezi/60384355469ed81ac20a392e4550e687 to your computer and use it in GitHub Desktop.
Save codezi/60384355469ed81ac20a392e4550e687 to your computer and use it in GitHub Desktop.
<?
public function actionImport(ParameterBag $params)
{
$category = $this->assertViewableCategory($params->category_id);
/** @var \ZixVN\Exams\ControllerPlugin\Question $questionPlugin */
$questionPlugin = $this->plugin('ZixVN\Exams:Question');
$creator = $this->service('ZixVN\Exams:Question\Creator', $category);
/** @var \ZixVN\Exams\ControllerPlugin\Question $questionPlugin */
$questionPlugin = $this->plugin('ZixVN\Exams:Question');
if ($this->isPost())
{
$tags = $this->filter('tags', 'str');
$catId = $params->category_id;
/** Import file docx to temp folder */
$uploader = $this->request()->getFile('docx');
if (empty($uploader))
{
return $this->error('Vui lòng chọn file.');
}
$tempFile = $uploader->getTempFile();
/** Prepare questions data for importing. */
list($questions, $errors) = Docx::prepareQuestionData($tempFile, $catId, $tags);
$allImg = Docx::getImageFromDocxToFolder($tempFile);
/** Check errors */
if (!empty($errors))
{
if (isset($errors['error']['ABCD']))
{
return $this->error('Lỗi đáp án A, B, C, D của các câu hỏi: ' . implode(", ", $errors['error']['ABCD']));
}
}
/**********************************************************
* Thêm lời giải vào các câu hỏi có sẵn với tags tìm được *
**********************************************************/
if ($upload_explain)
{
if (empty($tags))
{
return $this->error('Vui lòng nhập tags.');
}
$questionFinder = $this->finder('Truonglv\Quiz:Question');
$tagNames = [];
/** @var $questionsFound All QuestionIds */
$questionsFound = $questionFinder
//->where('explain', '=', '')
->withTags($tags, $tagNames)->fetch();
/** Prepare questions data for importing. */
$questions = Docx::prepareQuestionData($tempFile, $catId, $tags);
if (count($questionsFound) != count($questions))
{
return $this->error('Tổng số câu hỏi tìm thấy và số câu hỏi đưa lên không khớp nhau.');
}
$db = \XF::db();
$i = 1;
foreach ($questionsFound as $question)
{
$db->update('tl_quiz_questions', ['explain' => $questions[$i]['explain']], 'id = ?', $question['id']);
$i++;
}
return $this->redirect($this->buildLink('question-categories/import', $category));
}
/**********************************
* Thêm câu hỏi vào bảng dữ liệu. *
**********************************/
$db = \XF::db();
$attach_count = [];
foreach ($questions AS $question)
{
///** Thêm câu hỏi vào bảng dữ liệu */
//$db->insert('tl_quiz_questions', $question);
$question = $questionPlugin->saveQuestionFromData($creator, $question);
/** Thêm ảnh vào từng câu hỏi mà có ảnh */
$question_id = $db->lastInsertId();
foreach ($question as $col => $value)
{
if(preg_match_all('/(\[ATTACH=full\](.*?)\[\/ATTACH\])/', $value, $output_array))
{
$indexes = $output_array[2]; //return array id
foreach ($indexes as $index)
{
$fileName = $allImg[$index]; //Lấy tên file ảnh số 1
$attachmentId = Docx::uploadImageFromFolderToXenforo(\XF::getRootDirectory() . '/img/', $fileName, $question_id);
$value_update = str_replace('[ATTACH=full]' . $index . '[/ATTACH]', '[ATTACH=full]' . $attachmentId . '[/ATTACH]', $value);
$db->update('tl_quiz_questions', [$col => $value_update], 'id = ?', $question_id);
}
$attach_count[$col] = count($indexes);
//array(3) {
// ["content"]=> int(2)
// ["option_a"]=> int(1)
// ["option_b"]=> int(1)
//}
}
}
/*************************
** Update count attach **
*************************/
$total_attach = array_sum($attach_count);
/** @var \Truonglv\Quiz\Finder\Question $questionFinder */
$questionFinder = $this->finder('Truonglv\Quiz:Question');
$question = $questionFinder->where('id', $question_id)->fetchOne();
\XF::db()->update('tl_quiz_questions', ['attach_count' => $total_attach], 'id = ?', $question_id);
$attach_count = [];
/** Set tags của câu hỏi */
$question_context = $this->assertQuestionViewable($question_id);
/** @var \XF\Service\Tag\Changer $tagger */
$tagger = $this->service('XF:Tag\Changer', Constants::CONTENT_TYPE_QUESTIONS, $question_context);
$tagger->setTags($question['tags']);
$tagger->save();
}
/***************************************************************************
* Cập nhật question_count của thư mục hiện tại và các thư mục cha của nó. *
***************************************************************************/
$db->update('tl_quiz_categories', ['question_count' => $category['question_count'] + count($questions)], 'category_id = ?', $category['category_id']);
session_start();
session_destroy();
return $this->redirect($this->buildLink('quiz/categories', $category));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment