示例#1
0
    def delete(self):
        sectionId = int(request.args.get('sectionId'))

        db = Mysql()

        # Get Topic and modify section_count
        topicId = db.getOne(
            """
            select topic_id from Section where section_id=%s;
            """, (sectionId,)
        )["topic_id"]
        db.modify(
            """
            update Topic set section_count=section_count-1 where topic_id=%s;
            """, (topicId,)
        )

        # Get Course
        courseId = db.getOne(
            """
            select course_id from Topic where topic_id=%s;
            """, (topicId,)
        )["course_id"]
        db.modify(
            """
            update Course set section_count=section_count-1 where course_id=%s;
            """, (courseId,)
        )

        # Get All Students
        s = db.getAll(
            """
            select `student_id`,`process_detail` from StudentCourse where course_id=%s;
            """, (courseId,)
        )
        if s:
            for student in s:
                pd = eval(student["process_detail"])
                pd.pop(str(sectionId))
                pd = str(pd)
                db.modify(
                    """
                    update StudentCourse set process_detail=%s where student_id=%s;
                    """, (pd,student["student_id"])
                )

        # Delete Section
        db.delete(
            """
            delete from Section where section_id=%s;
            """, (sectionId,)
        )

        return {
            "success": True
        }
示例#2
0
    def delete(self):
        problemId = int(request.args.get("problemId"))

        db = Mysql()

        # Get practice
        practiceId = db.getOne(
            """
            select practice_id from Problem where problem_id=%s;
            """, (problemId,)
        )["practice_id"]

        # Delete Problem
        db.delete(
            """
            delete from Problem where problem_id=%s;
            """, (problemId,)
        )

        # Reset Status of Practice
        sl = db.getAll(
            """
            select student_id from StudentPractice where practice_id=%s;
            """, (practiceId,)
        )
        if sl:
            for student in sl:
                id = student["student_id"]
                db.modify(
                    """
                    update StudentPractice set status=0 where student_id=%s and practice_id=%s;
                    """, (id,practiceId)
                )

        p = db.getAll(
            """
            select * from Problem where practice_id=%s;
            """, (practiceId,)
        )
        pl = []

        if p:
            for each in p:
                ch = json.loads(each["choices"])
                d = {
                    "id": str(each["problem_id"]),
                    "problem": each["content"],
                    "choices": [{"cid": i, "value": ch[i]} for i in range(0, len(ch))],
                    "correctAnswer": each["answer"]
                }
                pl.append(d)

        return {
            "success": True,
            "problemList": pl
        }
示例#3
0
    def put(self):
        data = request.get_json() or request.form
        practiceId = int(data["practiceId"])
        title = data["title"]
        relatedCourse = data["relatedCourse"]
        relatedTopic = data["relatedTopic"]
        relatedSection = data["relatedSection"]

        db = Mysql()

        rl = db.getOne(
            """
            select relation,course_id from Practice where practice_id=%s;
            """, (practiceId,)
        )

        db.modify(
            """
            update Practice set title=%s where practice_id=%s;
            """, (title,practiceId)
        )
        # Relation modify here
        if relatedCourse == "":
            # no change
            pass
        else:
            if relatedSection == "" or relatedSection == "0":
                if relatedTopic == "" or relatedTopic == "0":
                    # relation 0
                    courseId = int(relatedCourse)
                    db.modify(
                        """
                        update Practice set relation=0,course_id=%s where practice_id=%s;
                        """, (courseId, practiceId)
                    )
                else:
                    # relation 1
                    courseId = int(relatedCourse)
                    topicId = int(relatedTopic)
                    db.modify(
                        """
                        update Practice set relation=1,course_id=%s,topic_id=%s where practice_id=%s;
                        """, (courseId, topicId, practiceId)
                    )
            else:
                # relation 2
                courseId = int(relatedCourse)
                topicId = int(relatedTopic)
                sectionId = int(relatedSection)
                db.modify(
                    """
                    update Practice set relation=2,course_id=%s,topic_id=%s,section_id=%s where practice_id=%s;
                    """, (courseId, topicId, sectionId, practiceId)
                )

            # Handle students
            # 删除以前的所有记录
            db.delete(
                """
                delete from StudentPractice where practice_id=%s;
                """, (practiceId,)
            )
            if rl["relation"] != 3:
                courseBefore = rl["course_id"]
                # 以前的课程 -1
                db.modify(
                    """
                    update Course set practice_count=practice_count-1 where course_id=%s;
                    """, (courseBefore,)
                )
            # 新课程 +1
            db.modify(
                """
                update Course set practice_count=practice_count+1 where course_id=%s;
                """, (courseId,)
            )
            # 新增新的课程学生的记录
            sl = db.getAll(
                """
                select student_id from StudentCourse where course_id=%s;
                """, (courseId,)
            )
            if sl:
                for student in sl:
                    db.insertOne(
                        """
                        insert into StudentPractice (`student_id`,`practice_id`,`status`) values (%s,%s,0);
                        """, (student["student_id"],practiceId)
                    )


        return {
            "success": True
        }
示例#4
0
    def delete(self):
        topicId = int(request.args.get("topicId"))
        courseId = int(request.args.get("courseId"))

        db = Mysql()

        # Delete Topic itself
        db.delete(
            """
            delete from Topic where topic_id=%s;
            """,(topicId,)
        )
        db.modify(
            """
            update Practice set relation=3 where topic_id=%s and relation=1;
            """, (topicId,)
        )

        # Get Section list and delete
        sectionIds = db.getAll(
            """
            select section_id as id from Section where topic_id=%s;
            """,(topicId,)
        )
        if sectionIds:
            cnt = len(sectionIds)
            # Delete all sections related with the topic
            db.delete(
                """
                delete from Section where topic_id=%s;
                """,(topicId,)
            )

            # Delete the entities related with sections
            for section in sectionIds:
                id = section["id"]
                db.delete(
                    """
                    delete from Entity where section_id=%s;
                    """,(id,)
                )

            # Untie the relationship of practice
            for section in sectionIds:
                id = section["id"]
                db.modify(
                    """
                    update Practice set relation=3 where section_id=%s and relation=2;
                    """, (id,)
                )

            # Delete the learning process related with deleted sections
            pd = db.getAll(
                """
                select `student_id`,`process_detail` where course_id=%s;
                """, (courseId,)
            )
            if pd:
                for each in pd:
                    process = eval(each["process_detail"])
                    for section in sectionIds:
                        process.pop(str(section["id"]))
                    db.modify(
                        """
                        update StudentCourse set process=detail=%s where student_id=%s and course_id=%s;
                        """, (str(process),each["student_id"],courseId)
                    )

        else :
            cnt = 0

        db.modify(
            """
            update Course set topic_count=topic_count-1, section_count=section_count-%s where course_id=%s;
            """, (cnt, courseId)
        )

        db.close()

        return {
            "state": "success"
        }