学生端选课进课闭环
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
|
||||
|
||||
/* 左右两侧的页面内容 */
|
||||
.sidebar {
|
||||
background-color: #f0f0f0;
|
||||
padding: 20px;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: column; /* 使子元素垂直排列 */
|
||||
}
|
||||
|
||||
body {
|
||||
@@ -16,7 +18,8 @@
|
||||
.chatbox {
|
||||
flex-grow: 1;
|
||||
padding: 20px;
|
||||
height: 75%;
|
||||
width: 100%;
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
border: 1px solid #ccc;
|
||||
background-color: #f9f9f9;
|
||||
@@ -69,14 +72,7 @@
|
||||
height: 100%;
|
||||
position: relative;
|
||||
}
|
||||
/* 中间的拖拽条 */
|
||||
.resizer {
|
||||
width: 5px;
|
||||
background-color: #ccc;
|
||||
cursor: ew-resize;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
|
||||
.chatbox-header {
|
||||
padding: 10px;
|
||||
background-color: #f1f1f1;
|
||||
@@ -110,19 +106,16 @@
|
||||
padding: 5px 10px;
|
||||
}
|
||||
#leftSidebar, .vscode-web, #rightSidebar {
|
||||
height: 100vh;
|
||||
height: 100%;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
#dragbar, #dragbar2 {
|
||||
background-color: #ccc;
|
||||
cursor: col-resize;
|
||||
}
|
||||
|
||||
/* 主容器,使用 grid 布局 */
|
||||
#maxcontainer {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 5px 3fr 5px 1fr; /* 默认宽度比例 */
|
||||
height: 100vh;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
/* 各区域基础样式 */
|
||||
@@ -133,22 +126,24 @@
|
||||
.vscode-web {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border: none;
|
||||
}
|
||||
|
||||
|
||||
.resizer {
|
||||
background-color: #ccc;
|
||||
cursor: col-resize;
|
||||
width: 5px; /* 设置拖动条宽度 */
|
||||
width: 5px;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
.gutter {
|
||||
background-color: #ccc; /* 分隔条颜色 */
|
||||
cursor: col-resize;
|
||||
}#container {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 5px 3fr 5px 1fr; /* 初始比例 */
|
||||
height: 100vh;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
#leftSidebar, #vscodeWeb, #rightSidebar {
|
||||
@@ -174,13 +169,18 @@
|
||||
}
|
||||
|
||||
.progress-title {
|
||||
height: 60px; /* 每个进度节点的高度 */
|
||||
height: 60px;
|
||||
text-align: center;
|
||||
line-height: 60px; /* 垂直居中 */
|
||||
margin: 2px; /* 水平方向上的间隔 */
|
||||
line-height: 60px;
|
||||
margin-right: 2px;
|
||||
border: 1px solid #ddd;
|
||||
transition: background-color 0.3s ease;
|
||||
position: relative;
|
||||
|
||||
/* 新增以下属性实现文字溢出省略 */
|
||||
white-space: nowrap; /* 防止文字换行 */
|
||||
overflow: hidden; /* 隐藏溢出内容 */
|
||||
text-overflow: ellipsis; /* 溢出部分显示为省略号 */
|
||||
}
|
||||
|
||||
|
||||
@@ -295,4 +295,42 @@
|
||||
}
|
||||
.toggle-button .icon {
|
||||
font-size: 20px;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* 设置滚动条的宽度、颜色等 */
|
||||
-webkit-scrollbar {
|
||||
width: 8px; /* 初始滚动条宽度 */
|
||||
height: 8px; /* 水平滚动条宽度 */
|
||||
}
|
||||
|
||||
/* 设置滚动条轨道的颜色 */
|
||||
-webkit-scrollbar-track {
|
||||
background: #f1f1f1;
|
||||
}
|
||||
|
||||
/* 设置滚动条滑块的颜色 */
|
||||
-webkit-scrollbar-thumb {
|
||||
background: #888;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
/* 当鼠标悬停在滚动条上时,改变其宽度和颜色 */
|
||||
-webkit-scrollbar:hover {
|
||||
width: 12px; /* 鼠标悬停时变粗 */
|
||||
height: 12px;
|
||||
}
|
||||
|
||||
/* 滚动条滑块的颜色变化 */
|
||||
-webkit-scrollbar-thumb:hover {
|
||||
background: #555; /* 鼠标悬停时变暗 */
|
||||
}
|
||||
|
||||
@@ -1,120 +1,133 @@
|
||||
// dashboard.js
|
||||
let courseData;
|
||||
function show_user_data(user_data, course_brief_data_list){
|
||||
courseData = user_data.course_process_dict
|
||||
courseData = {}
|
||||
for (let i=0;i<course_brief_data_list.length;i++){
|
||||
courseData[course_brief_data_list[i]._id] = {}
|
||||
}
|
||||
for (let i=0;i<course_brief_data_list.length;i++){
|
||||
course_brief_data = course_brief_data_list[i]
|
||||
console.log(course_brief_data)
|
||||
courseData[course_brief_data.course_id].course_id = course_brief_data.course_id;
|
||||
courseData[course_brief_data.course_id].title = course_brief_data.course_name;
|
||||
courseData[course_brief_data.course_id].lessons = course_brief_data.lessons;
|
||||
courseData[course_brief_data.course_id].course_img_path = course_brief_data.course_img_path;
|
||||
courseData[course_brief_data.course_id].course_description = course_brief_data.course_description;
|
||||
courseData[course_brief_data.course_id].course_create_date = course_brief_data.course_create_date;
|
||||
courseData[course_brief_data.course_id].course_update_data = course_brief_data.course_update_data;
|
||||
courseData[course_brief_data._id]._id = course_brief_data._id;
|
||||
courseData[course_brief_data._id].title = course_brief_data.name;
|
||||
courseData[course_brief_data._id].chapters = course_brief_data.chapters;
|
||||
courseData[course_brief_data._id].image_url = course_brief_data.image_url;
|
||||
courseData[course_brief_data._id].description = course_brief_data.description;
|
||||
courseData[course_brief_data._id].created_at = course_brief_data.created_at;
|
||||
courseData[course_brief_data._id].updated_at = course_brief_data.updated_at;
|
||||
}
|
||||
for (var course in courseData){
|
||||
courseData[course].chapters = courseData[course].lessons
|
||||
courseData[course].chapters = courseData[course].chapters
|
||||
console.log(courseData[course])
|
||||
}
|
||||
|
||||
let courseCardsContainer = document.getElementById('course-cards');
|
||||
courseCardsContainer.innerHTML = "";
|
||||
for (var course in courseData){
|
||||
course_id = course;
|
||||
course = courseData[course];
|
||||
let courseCard = document.createElement('div');
|
||||
|
||||
courseCard.className = 'course-card';
|
||||
courseCard.innerHTML = `
|
||||
<img src="${course.course_img_path}" alt="课程封面", class="course-image">
|
||||
<div class="course-info">
|
||||
<h3>${course.title}</h3>
|
||||
<p>${course.course_description}</p>
|
||||
</div>
|
||||
<button class="select-button" >查看目录</button>
|
||||
`;
|
||||
courseCardsContainer.appendChild(courseCard);
|
||||
courseCard.data = course_id
|
||||
courseCard.addEventListener('click',() => {
|
||||
openCourseProgress(courseCard.data);
|
||||
});
|
||||
}
|
||||
}
|
||||
function openCourseProgress(courseId) {
|
||||
console.log(courseId)
|
||||
const course = courseData[courseId];
|
||||
if (!course) return;
|
||||
fetch(`/dashboard/get_course_progress`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({course_id: courseId})
|
||||
}).then(response => response.json())
|
||||
.then(data => {
|
||||
console.log(data)
|
||||
/*
|
||||
course_last_study_date: "2025-09-06"
|
||||
course_put_in_date: "2025-09-06"
|
||||
course_total_study_time:0
|
||||
lesson_processs:Array(1):
|
||||
(3) [Array(0), '基础算法', '二分']
|
||||
material_id:"68bacdfadf5aeae0912f7f18"
|
||||
user_uuid: $binary: {base64: 'r4hz5J1JSWe1hKJqXnAgwQ==', subType: '04'}
|
||||
|
||||
// 更新右侧滑出卡片的内容
|
||||
document.getElementById('course-title').textContent = course.title;
|
||||
///////////////////////// course.progress;还没有计算
|
||||
document.getElementById('progress').textContent = course.progress;
|
||||
*/
|
||||
course.course_last_study_date = data.data.course_last_study_date;
|
||||
course.course_put_in_date = data.data.course_put_in_date;
|
||||
course.course_total_study_time = data.data.course_total_study_time;
|
||||
course.lesson_processs = data.data.lesson_processs;
|
||||
course.material_id = data.data.material_id;
|
||||
course.user_uuid = data.data.user_uuid;
|
||||
document.getElementById('course-title').textContent = course.title;
|
||||
///////////////////////// course.progress;还没有计算
|
||||
document.getElementById('progress').textContent = course.progress;
|
||||
// 更新章节列表
|
||||
const chapterList = document.getElementById('chapter-list');
|
||||
chapterList.innerHTML = ''; // 清空之前的章节
|
||||
|
||||
// 更新章节列表
|
||||
const chapterList = document.getElementById('chapter-list');
|
||||
chapterList.innerHTML = ''; // 清空之前的章节
|
||||
course.chapters.forEach(chapter => {
|
||||
const chapterItem = document.createElement('li');
|
||||
chapterItem.classList.add('chapter-item');
|
||||
|
||||
const chapterTitle = document.createElement('div');
|
||||
chapterTitle.classList.add('chapter-title');
|
||||
chapterTitle.textContent = chapter.chapter_name;
|
||||
chapterItem.appendChild(chapterTitle);
|
||||
|
||||
course.chapters.forEach(chapter => {
|
||||
const chapterItem = document.createElement('li');
|
||||
chapterItem.classList.add('chapter-item');
|
||||
|
||||
const chapterTitle = document.createElement('div');
|
||||
chapterTitle.classList.add('chapter-title');
|
||||
chapterTitle.textContent = chapter.lesson_id;
|
||||
chapterItem.appendChild(chapterTitle);
|
||||
// 创建子章节
|
||||
if (chapter.lessons && chapter.lessons.length > 0) {
|
||||
const subChapterList = document.createElement('ul');
|
||||
subChapterList.classList.add('sub-chapter-list');
|
||||
|
||||
// 创建子章节
|
||||
if (chapter.subChapters && chapter.subChapters.length > 0) {
|
||||
const subChapterList = document.createElement('ul');
|
||||
subChapterList.classList.add('sub-chapter-list');
|
||||
|
||||
chapter.subChapters.forEach(subChapter => {
|
||||
const subChapterItem = document.createElement('li');
|
||||
subChapterItem.classList.add('sub-chapter-item');
|
||||
subChapterItem.textContent = subChapter;
|
||||
console.log('-------------------')
|
||||
console.log(course)
|
||||
// 查找学生进度
|
||||
for(let i = 0; i < course.lesson_processs.length; i++) {
|
||||
if (course.lesson_processs[i][1] == chapter.lesson_id) {//确定lesson_id对应正确
|
||||
console.log('-------------------')
|
||||
console.log(course.lesson_processs[i][0])
|
||||
lesson_chapters_progress = course.lesson_processs[i][0];//寻找对应的每一个章节的进度
|
||||
for (let j=0; j<lesson_chapters_progress.length;j++){
|
||||
chapter_progress = lesson_chapters_progress[j]
|
||||
if(chapter_progress.title == subChapter){
|
||||
subChapterItem.textContent += ("(已"+(chapter_progress.is_rebuttal?"申辩" : "完成")+" 得分"+ (chapter_progress.is_rebuttal?chapter_progress.rebuttal_score : chapter_progress.score) +")");
|
||||
chapter.lessons.forEach(subChapter => {
|
||||
const subChapterItem = document.createElement('li');//课时层级
|
||||
subChapterItem.classList.add('sub-chapter-item');
|
||||
subChapterItem.textContent = subChapter.lesson_name;
|
||||
console.log('-------------------')
|
||||
console.log(course)
|
||||
// 查找学生进度
|
||||
for(let i = 0; i < course.lesson_processs.length; i++) {
|
||||
if (course.lesson_processs[i][2] == subChapter.lesson_name && course.lesson_processs[i][1] == chapter.chapter_name) {//确定lesson_id对应正确
|
||||
///////////////////////////////////////////
|
||||
//这里之后要做lesson的各个step的进度和得分
|
||||
////////////////////////////////////////////
|
||||
|
||||
console.log('-------------------')
|
||||
console.log(course.lesson_processs[i][0])
|
||||
lesson_chapters_progress = course.lesson_processs[i][0];//寻找对应的每一个章节的进度
|
||||
for (let j=0; j<lesson_chapters_progress.length;j++){
|
||||
chapter_progress = lesson_chapters_progress[j]
|
||||
if(chapter_progress.title == subChapter){
|
||||
subChapterItem.textContent += ("(已"+(chapter_progress.is_rebuttal?"申辩" : "完成")+" 得分"+ (chapter_progress.is_rebuttal?chapter_progress.rebuttal_score : chapter_progress.score) +")");
|
||||
}
|
||||
}
|
||||
if (course.lesson_processs[i][0].length === 1) {
|
||||
subChapterItem.classList.add('completed');
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (course.lessons[i].progress === 1) {
|
||||
subChapterItem.classList.add('completed');
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 添加点击事件跳转到学习页面
|
||||
subChapterItem.addEventListener('click', () => {
|
||||
const courseId = encodeURIComponent(course.course_id); // 对课程名称进行编码
|
||||
const url = `/desktop_nouser/${courseId}/${chapter.lesson_id}`;
|
||||
window.location.href = url; // 跳转到该学习页面
|
||||
// 添加点击事件跳转到学习页面
|
||||
subChapterItem.addEventListener('click', () => {
|
||||
const courseId = encodeURIComponent(course._id); // 对课程名称进行编码
|
||||
const url = `/desktop_nouser/${courseId}/${chapter.chapter_name}/${subChapter.lesson_name}`;
|
||||
window.location.href = url; // 跳转到该学习页面
|
||||
});
|
||||
|
||||
subChapterList.appendChild(subChapterItem);
|
||||
});
|
||||
|
||||
subChapterList.appendChild(subChapterItem);
|
||||
});
|
||||
chapterItem.appendChild(subChapterList);
|
||||
|
||||
chapterItem.appendChild(subChapterList);
|
||||
// 添加点击事件切换子章节显示
|
||||
chapterItem.addEventListener('click', () => {
|
||||
chapterItem.classList.toggle('open');
|
||||
});
|
||||
}
|
||||
|
||||
// 添加点击事件切换子章节显示
|
||||
chapterItem.addEventListener('click', () => {
|
||||
chapterItem.classList.toggle('open');
|
||||
});
|
||||
}
|
||||
|
||||
chapterList.appendChild(chapterItem);
|
||||
chapterList.appendChild(chapterItem);
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
// 打开滑出卡片
|
||||
document.getElementById('courseProgress').classList.add('open');
|
||||
}
|
||||
|
||||
16
Html/apps/static/二分.html
Normal file
16
Html/apps/static/二分.html
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
img {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
</style>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown.min.css">
|
||||
</head>
|
||||
<body class="markdown-body">
|
||||
<p>b'# \xe4\xba\x8c\xe5\x88\x86\n> Auto-generated at 2025-09-05 19:48\n\n## \xe9\x98\xb6\xe6\xae\xb5\xe4\xb8\x80\xef\xbc\x9a\xe7\xa4\xba\xe4\xbe\x8b\xe9\x98\xb6\xe6\xae\xb5\n### \xe5\xbc\x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\xe7\x9a\x84\xe7\xad\x94\xe6\xa1\x88\xe5\xa4\x87\xe9\x80\x89\xe5\x8c\xba\xef\xbc\x8c\xe7\x8c\x9c\xe6\xb5\x8b625\xef\xbc\x8c\xe5\x81\x87\xe8\xae\xbe\xe8\xbf\x87\xe5\xb0\x8f\xef\xbc\x8c\n4. \xe5\x88\x99\xe5\xaf\xb9\xe4\xba\x8e626\xe5\x88\xb0749\xe5\x8c\xba\xe9\x97\xb4......\n5. \xef\xbc\x88688-749\xef\xbc\x89\n6. \xef\xbc\x88718-749\xef\xbc\x89\n7. \xef\xbc\x88734-749\xef\xbc\x89\n8. \xef\xbc\x88742-749\xef\xbc\x89\n9. \xef\xbc\x88746-749\xef\xbc\x89\n10. \xef\xbc\x88748-749\xef\xbc\x89\n11. \xef\xbc\x88749-749\xef\xbc\x89\n\n\xe5\x9c\xa8\xe6\x9c\x80\xe5\xb7\xae\xe7\x9a\x84\xe6\x83\x85\xe5\x86\xb5\xe4\xb8\x8b\xef\xbc\x8c\xe7\xac\xac11\xe6\xac\xa1\xe7\x9a\x84\xe7\xad\x94\xe6\xa1\x88\xe5\xa4\x87\xe9\x80\x89\xe5\x8c\xba\xe5\xb0\xb1\xe4\xb8\x80\xe5\xae\x9a\xe9\x95\xbf\xe5\xba\xa6\xe4\xb8\xba1\xe4\xba\x86\xef\xbc\x8c\xe4\xb9\x9f\xe5\xb0\xb1\xe6\x98\xaf\xe5\xbf\x85\xe7\x84\xb6\xe6\x98\xaf\xe7\xad\x94\xe6\xa1\x88\xe3\x80\x82\n\n\xe5\x9b\xa0\xe6\xad\xa4\xe5\xa6\x82\xe6\x9e\x9c\xe5\xba\x8f\xe5\x88\x97\xe6\x98\xaf\xe6\x9c\x89\xe5\xba\x8f\xe7\x9a\x84\xef\xbc\x8c\xe5\xb0\xb1\xe5\x8f\xaf\xe4\xbb'</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
56
Html/apps/static/基础算法-二分.html
Normal file
56
Html/apps/static/基础算法-二分.html
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
img {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
</style>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown.min.css">
|
||||
</head>
|
||||
<body class="markdown-body">
|
||||
<h1>二分</h1>
|
||||
<blockquote>
|
||||
<p>Auto-generated at 2025-09-05 19:48</p>
|
||||
</blockquote>
|
||||
<h2>阶段一:示例阶段</h2>
|
||||
<h3>引入</h3>
|
||||
<p>二分是一个很简单基础,但很重要的知识点,为以后许多高级的数据结构与算法铺垫。</p>
|
||||
<p>下面是一个用二分的简单场景:</p>
|
||||
<p>假设小明从0到1000之间选择了一个数字但不告诉你,你可以不断猜测这个数,每次猜测小明会告知你的猜测得过大还是过小,问最多几次就一定能猜中?</p>
|
||||
<p>答案是利用二分查找的原理,猜测11次即可。</p>
|
||||
<ol>
|
||||
<li>对于0到1000的答案备选区,猜测中位数500,假设过小,</li>
|
||||
<li>则对于501到1000的答案备选区,猜测750,假设过大</li>
|
||||
<li>则对于501到749的答案备选区,猜测625,假设过小,</li>
|
||||
<li>则对于626到749区间......</li>
|
||||
<li>(688-749)</li>
|
||||
<li>(718-749)</li>
|
||||
<li>(734-749)</li>
|
||||
<li>(742-749)</li>
|
||||
<li>(746-749)</li>
|
||||
<li>(748-749)</li>
|
||||
<li>(749-749)</li>
|
||||
</ol>
|
||||
<p>在最差的情况下,第11次的答案备选区就一定长度为1了,也就是必然是答案。</p>
|
||||
<p>因此如果序列是有序的,就可以通过二分查找快速定位所需要的数据。</p>
|
||||
<h4>思考题(询问Agent以学习计算方法,或验证你的答案)</h4>
|
||||
<p>对于上面那个题目,如果问题区间是1到4000,最差情况下需要猜测几次?</p>
|
||||
<h3>练习:二分查找</h3>
|
||||
<p>试试对于下面的题目,用代码实现一下二分查找。</p>
|
||||
<h4>题目:有序数组寻址</h4>
|
||||
<p>给出一个长度为n的有序数组(从小到大),有q次询问,对于每次询问,输出指定数在数组中的下标。如果不存在则输出-1。</p>
|
||||
<h5>输入</h5>
|
||||
<p>第一行一个整数n。(1<=n<=10^5)</p>
|
||||
<p>第二行n个用空格分开的整数ai。(0<=ai<=10^8)</p>
|
||||
<p>第三行一个整数q,表示询问的次数。(1<=q<=10^4)</p>
|
||||
<p>后q行,每行一个整数b,表示询问的数。(0<=b<=10^8)</p>
|
||||
<h5>输出</h5>
|
||||
<p>q行,每行一个整数,对应每次询问的返回结果。</p>
|
||||
<h5>提示:</h5>
|
||||
<p>完成代码后,通知Agent进行评测。</p>
|
||||
<p>如果你还不完全会这个算法,询问Agent获取提示并进行学习。</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user