Skip to content
Snippets Groups Projects
Commit 6a00d775 authored by Ron Lucke's avatar Ron Lucke
Browse files

ref #7

parent 6b21df8d
No related branches found
No related tags found
No related merge requests found
<?php
use Mooc\DB\Block as dbBlock;
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
......@@ -6,6 +8,7 @@
* the License, or (at your option) any later version.
*
* @author Elmar Ludwig
* @author Ron Lucke
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
*/
......@@ -153,5 +156,203 @@ class AssistantController extends StudipController
$this->redirect(URLHelper::getURL('dispatch.php/course/scm'));
}
/***********************
* C O U R S E W A R E *
* S T A R T *
**********************/
public function courseware_action()
{
$remote_cid = '2fff5f8bc52281d4237139fc30e7f248';
$current_cid = $this->course_id;
$plugin_manager = \PluginManager::getInstance();
$this->coursewarePlugin = $plugin_manager->getPlugin('Courseware');
$current_courseware = dbBlock::findCourseware($current_cid);
$remote_courseware = $this->getRemoteCourseware($remote_cid);
$import_folder = $this->createImportFolder($current_cid);
// import remote course
//chapters
foreach ($remote_courseware['children'] as $chapter) {
$db_block = dbBlock::find($chapter['id']);
$data = array('title' => $db_block->title, 'cid' => $current_cid, 'publication_date' => $chapter['publication_date'], 'withdraw_date' => $chapter['withdraw_date']);
$current_chapter = $this->createAnyBlock($current_courseware->id, 'Chapter', $data);
//subchapters
foreach($chapter['children'] as $subchapter) {
$db_block = dbBlock::find($subchapter['id']);
$data = array('title' => $db_block->title, 'cid' => $current_cid, 'publication_date' => $subchapter['publication_date'], 'withdraw_date' => $subchapter['withdraw_date']);
$current_subchapter = $this->createAnyBlock($current_chapter->id, 'Subchapter', $data);
//sections
foreach($subchapter['children'] as $section) {
$db_block = dbBlock::find($section['id']);
$data = array('title' => $db_block->title, 'cid' => $current_cid, 'publication_date' => null, 'withdraw_date' => null);
$current_section = $this->createAnyBlock($current_subchapter->id, 'Section', $data);
//blocks
foreach($section['children'] as $block) {
$remote_db_block = dbBlock::find($block['id']);
$remote_ui_block = $this->coursewarePlugin->getBlockFactory()->makeBlock($remote_db_block);
$data = array('title' => $remote_db_block->title, 'cid' => $cid, 'publication_date' => null, 'withdraw_date' => null);
$new_block = $this->createAnyBlock($current_section->id, $remote_db_block->type, $data, $remote_db_block->sub_type);
$new_ui_block = $this->coursewarePlugin->getBlockFactory()->makeBlock($new_block);
if (gettype($new_ui_block) != 'object') {
$new_block->delete();
break 2;
}
$files = $remote_ui_block->getFiles();
foreach($files as &$file) {
$remote_file = FileRef::find($file['id']);
if ($remote_file != null) {
$file = FileManager::copyFileRef($remote_file, $import_folder, \User::findCurrent());
}
}
if ($remote_ui_block->exportProperties() != null) {
$new_ui_block->importProperties($remote_ui_block->exportProperties());
}
$new_ui_block->importContents($remote_ui_block->exportContents(), $files);
}
}
}
}
$this->redirect(URLHelper::getURL('plugins.php/courseware/courseware', array('cid' => $this->course_id)));
}
private function getRemoteCourseware($cid)
{
$grouped = $this->getGrouped($cid);
$remote_courseware = current($grouped['']);
$this->buildTree($grouped, $remote_courseware);
return $remote_courseware;
}
private function getGrouped($cid)
{
$grouped = array_reduce(
dbBlock::findBySQL('seminar_id = ? ORDER BY id, position', array($cid)),
function($memo, $item) {
$arr = $item->toArray();
$arr['isStrucutalElement'] = true;
$arr['childType'] = $this->getSubElement($arr['type']);
if (!$item->isStructuralBlock()) {
$arr['isStrucutalElement'] = false;
$arr['isBlock'] = true;
$ui_block = $this->coursewarePlugin->getBlockFactory()->makeBlock($item);
$arr['ui_block'] = $ui_block;
}
$arr['isPublished'] = $item->isPublished();
$memo[$item->parent_id][] = $arr;
return $memo;
},
array());
return $grouped;
}
private function buildTree($grouped, &$root)
{
$this->addChildren($grouped, $root);
if ($root['type'] !== 'Section') {
if (!empty($root['children'])) {
foreach($root['children'] as &$child) {
$this->buildTree($grouped, $child);
}
}
} else {
$root['children'] = $this->addChildren($grouped, $root);
}
}
private function getSubElement($type)
{
$sub_element = null;
switch($type) {
case 'Courseware':
$sub_element = 'Chapter';
break;
case 'Chapter':
$sub_element = 'Subchapter';
break;
case 'Subchapter':
$sub_element = 'Section';
break;
case 'Section':
$sub_element = 'Block';
break;
case 'Block':
default:
}
return $sub_element;
}
private function addChildren($grouped, &$parent)
{
$parent['children'] = $grouped[$parent['id']];
if ($parent['children'] != null) {
usort($parent['children'], function($a, $b) {
return $a['position'] - $b['position'];
});
}
return $parent['children'];
}
private function createAnyBlock($parent, $type, $data, $sub_type = '')
{
$block = new dbBlock();
$parent_id = is_object($parent) ? $parent->id : $parent;
$block->setData(array(
'seminar_id' => $data['cid'],
'parent_id' => $parent_id,
'type' => $type,
'sub_type' => $sub_type,
'title' => $data['title'],
'publication_date' => $data['publication_date'],
'withdraw_date' => $data['withdraw_date'],
'position' => $block->getNewPosition($parent_id)
));
$block->store();
return $block;
}
private function createImportFolder($cid = null)
{
if($cid == null) {
$root_folder = Folder::findTopFolder($GLOBALS['SessionSeminar']);
} else {
$root_folder = Folder::findTopFolder($cid);
}
$parent_folder = FileManager::getTypedFolder($root_folder->id);
// create new folder for import
$request = array('name' => 'Courseware-Tutorial', 'description' => _('Dateien für das Courseware Tutorial'));
$new_folder = new StandardFolder();
$new_folder->setDataFromEditTemplate($request);
$new_folder->user_id = User::findCurrent()->id;
$courseware_folder = $parent_folder->createSubfolder($new_folder);
return FileManager::getTypedFolder($courseware_folder->id);
}
/***********************
* C O U R S E W A R E *
* E N D *
**********************/
}
......@@ -99,7 +99,7 @@
</h1>
<div class="accordion_content">
<p>Mit dem Stud.IP-Plugin "Courseware" können Sie vielfältige Quellen zusammenführen und interaktive multimediale Lernmodule erstellen.</p>
<?= Studip\LinkButton::create(_('Kurs dafür einrichten'), $controller->url_for('courseware')) ?>
<?= Studip\LinkButton::create(_('Kurs dafür einrichten'), $controller->url_for('assistant/courseware')) ?>
<ul>
<li>
<a href="<?= $controller->link_for('assistant/courseware_info/example') ?>" data-dialog="size=640x400">Beispiel</a>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment