1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-29 22:01:38 +08:00
2019-12-13 00:10:10 +08:00

52 lines
1.2 KiB
PHP

<?php
namespace App\Transformers;
class ChapterList extends Transformer
{
public function handleProcess($chapters, $studyHistory)
{
$status = [];
if ($studyHistory) {
foreach ($studyHistory as $value) {
$status[$value['chapter_id']] = $value['finished'];
}
}
foreach ($chapters as $key => $chapter) {
$chapters[$key]['finished'] = isset($status[$chapter['id']]) ? $status[$chapter['id']] : 0;
}
return $chapters;
}
public function handleTree($chapters)
{
$list = [];
foreach ($chapters as $chapter) {
if ($chapter['parent_id'] == 0) {
$list[$chapter['id']] = $chapter;
$list[$chapter['id']]['child'] = [];
} else {
$list[$chapter['parent_id']]['child'][] = $chapter;
}
}
usort($list, function($a, $b) {
return $a['priority'] > $b['priority'];
});
foreach ($list as $key => $value) {
usort($list[$key]['child'], function($a, $b) {
return $a['priority'] > $b['priority'];
});
}
return $list;
}
}