Esempio n. 1
0
def course_unit_reorder(id, label=None):
    if not mcourses.Courses.exists(id):
        abort(404)
    course = mcourses.Courses(id)
    cuser = muser.getCurrentUser()
    if not (course.getCourseRole(cuser) >= 3) or cuser.isDisabled():
        abort(404)
    if request.method == "POST":
        data = (request.json)
        for item in data:
            u = mcourses.Units(item["id"])
            if u.getDetail("courseid") != id:
                return "invalid"
            u.setDetail("unit_order", item["order"])
            u.setDetail("parent", 0)
            for subitem in item["subitems"]:
                u = mcourses.Units(subitem["id"])
                if u.getDetail("courseid") != id:
                    return "invalid"
                u.setDetail("unit_order", subitem["order"])
                u.setDetail("parent", item["id"])
        return "ok"
    else:
        if course.getLabel() != label:
            return redirect(
                url_for("course_unit_reorder", id=id, label=course.getLabel()))
        return render_template('courses/edit/reorder.html',
                               title="Kursmodule neu anordnen: " +
                               course.getTitle(),
                               thispage="courses",
                               data=course)
Esempio n. 2
0
    def apply(self):
        change_requests = self.getOverrides()
        last_round_changes = -1
        while last_round_changes != 0:
            while None in change_requests:
                del change_requests[change_requests.index(None)]
            last_round_changes = 0

            for change in change_requests:
                if change["overrides"]:
                    if change["parent_override"] and not change["parent_unit"]:
                        continue
                    unit = mcourses.Units(change["overrides"])
                    unit.setDetail("title", change["title"])
                    unit.setDetail("content", change["content"])
                    unit.setDetail("parent", change["parent_unit"])
                    unit.setDetail("unit_order", change["unit_order"])
                else:
                    if change["parent_override"] and not change["parent_unit"]:
                        continue
                    unit = mcourses.Units.new(self.getDetail("course_id"),
                                              change["content"],
                                              change["type"],
                                              change["parent_unit"])
                    unit = mcourses.Units(unit)
                    unit.setDetail("title", change["title"])
                    unit.setDetail("unit_order", change["unit_order"])
                    unit.setDetail("availible", 1)

                    self.mapOverrideToUnit(change["id"], unit.id)

                    for chr in change_requests:
                        if chr is None: continue
                        if chr["parent_override"] == change["id"]:
                            chr["parent_unit"] = unit.id
Esempio n. 3
0
def _mkdata(unit_id, override_id, course_id, branch):
    if unit_id == "-":
        data = {
            "title": "",
            "type": "",
            "content": None,
            "parent_unit": 0,
            "parent_override": 0,
            "unit_order": 0
        }
    else:
        unit_id = int(unit_id)
        unit = mcourses.Units(unit_id)
        if not unit or unit.getDetail("courseid") != course_id:
            abort(404)

        data = {
            "title": unit.getTitle(),
            "type": unit.getType(),
            "content": unit.getJSON(),
            "parent_unit": unit.getDetail("parent"),
            "parent_override": 0,
            "unit_order": unit.getDetail("unit_order")
        }

    if override_id != "-":
        override_id = int(override_id)
        override = branch.getSingleOverride(override_id)
        if not override or (unit_id != "-" and override["overrides"] != unit_id
                            ) or override["branch"] != branch.id:
            abort(404)
        data["title"] = override["title"]

        # Do not allow arbitrary changing the type
        if not data["type"]:
            data["type"] = override["type"]

        data["content"] = json.loads(override["content"])
        data["parent_unit"] = override["parent_unit"]
        data["parent_override"] = override["parent_override"]
        data["unit_order"] = override["unit_order"]

    return data
Esempio n. 4
0
 def calculateRepDelta(self):
     score = 0
     changes = self.getOverrides()
     for change in changes:
         if change["created"]:
             score += 10
         else:
             unit = mcourses.Units(change["overrides"])
             if unit.getTitle() != change["title"]:
                 score += 1
             if json.loads(unit.getDetail("content")) != json.loads(
                     change["content"]):
                 score += 5
                 if len(unit.getDetail("content")) <= 2 * len(
                         change["content"]):
                     score += 5
     if score > 50:
         score = 50
     if score < 2:
         score = 2
     self.setDetail("delta_factor", score)
     return score
Esempio n. 5
0
def unit_new(course_id):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if not (cuser.isMod()
            or course.getCourseRole(cuser) >= 3) or cuser.isDisabled():
        abort(404)
    empty_set = {
        "info": "[]",
        "quiz": "[]",
        "extvideo": '{"platform":"youtube", "embedcode": ""}',
        "pinboard": '0',
        "syllabus": "\"\""
    }
    if request.json["type"] == "survey":
        s = msurvey.Survey.new(course.id, cuser.id)
        empty_set["survey"] = '{"survey":' + str(s.id) + '}'
    x = mcourses.Units.new(course.id, empty_set[request.json["type"]],
                           request.json["type"], request.json["parent"])
    mcourses.Units(x).setDetail("title", _("Neue Seite"))
    return url_for("unit_show", unit_id=x, course_id=course_id)
Esempio n. 6
0
def unit_submit(unit_id, course_id, unit_label=None, course_label=None):
    if not mcourses.Courses.exists(course_id) or not mcourses.Units.exists(
            unit_id) or muser.require_login() or muser.getCurrentUser(
            ).isDisabled():
        abort(404)
    course = mcourses.Courses(course_id)
    unit = mcourses.Units(unit_id)
    cuser = muser.getCurrentUser()
    if unit.getType() == "quiz":
        MAX_SCORE = 0
        TOTAL_SCORE = 0
        RESULT_DATA = {}
        data = request.json
        master = unit.getJSON()
        k = 0
        for i in master:
            k += 1
            if i["type"] == "text-answer":
                MAX_SCORE += int(i["data"]["points"])
                if str(k) in list(data.keys()):
                    _ = data[str(k)]
                    if _.lower() in [x.lower() for x in i["data"]["correct"]]:
                        TOTAL_SCORE += int(i["data"]["points"])
                        RESULT_DATA[k] = ({
                            "max": i["data"]["points"],
                            "sum": i["data"]["points"],
                            "selection": _,
                            "correct": i["data"]["correct"]
                        })
                    else:
                        RESULT_DATA[k] = ({
                            "max": i["data"]["points"],
                            "sum": 0,
                            "selection": _,
                            "correct": i["data"]["correct"]
                        })
            elif i["type"] == "multiple-choice":
                MAX_SCORE += int(i["data"]["points"])
                if str(k) in list(data.keys()):
                    _ = data[str(k)]
                    if i["data"]["choices"][int(_)].startswith("*"):
                        TOTAL_SCORE += int(i["data"]["points"])
                        RESULT_DATA[k] = ({
                            "max": i["data"]["points"],
                            "sum": i["data"]["points"],
                            "selection": _,
                            "correct": i["data"]["choices"]
                        })
                    else:
                        RESULT_DATA[k] = ({
                            "max": i["data"]["points"],
                            "sum": 0,
                            "selection": _,
                            "correct": i["data"]["choices"]
                        })
            elif i["type"] == "multiple-answer":
                MAX_SCORE += int(i["data"]["points"])
                if str(k) in list(data.keys()):
                    _ = data[str(k)]
                    total = float(len(i["data"]["choices"]))
                    correct = 0
                    Zid = 0
                    for Z in i["data"]["choices"]:
                        if Z.startswith("*") == (Zid in [int(x) for x in _]):
                            correct += 1
                        Zid += 1
                    pts = (correct / total) * int(i["data"]["points"])
                    pts = round(pts, 1)
                    TOTAL_SCORE += pts
                    RESULT_DATA[k] = ({
                        "max": i["data"]["points"],
                        "sum": pts,
                        "selection": _,
                        "correct": i["data"]["choices"]
                    })

        unit.addViewData(
            cuser,
            json.dumps({
                "result": RESULT_DATA,
                "max": MAX_SCORE,
                "sum": TOTAL_SCORE
            }))
        return "{ok}"
    elif unit.getType() == "pinboard":
        aid = int(unit.getJSON())
        if aid == 0:
            abort(500)
        else:
            a = mforum.Article(aid)

        data = request.json
        answer = mforum.Answer.createNew(a.getDetail("forumID"), aid,
                                         data["comment"], muser.User(-1))
        answer.addRevision(data["comment"], cuser, "Ursprüngliche Version")

        a.setDetail("last_activity_date", time.time())
        answer.setDetail("last_activity_date", time.time())
        answer.setDetail("creation_date", time.time())
        return "{ok}"
    abort(500)
Esempio n. 7
0
def unit_edit(unit_id, course_id, unit_label=None, course_label=None):
    if not mcourses.Courses.exists(course_id) or not mcourses.Units.exists(
            unit_id):
        abort(404)
    course = mcourses.Courses(course_id)
    unit = mcourses.Units(unit_id)
    cuser = muser.getCurrentUser()
    if not (cuser.isMod()
            or course.getCourseRole(cuser) >= 3) or cuser.isDisabled():
        abort(404)
    if request.method == "POST":
        unit.setDetail("title", request.json["title"])
        unit.setDetail("availible", request.json["availible"])
        unit.setDetail("content", json.dumps(request.json["content"]))
        return "{ok}"
    else:
        if course.getLabel() != course_label:
            x = url_for(
                "unit_edit",
                course_id=course_id,
                course_label=course.getLabel(),
                unit_id=unit_id,
                unit_label=unit_label if unit_label else unit.getLabel())
            return redirect(x)
        if unit.getLabel() != unit_label:
            return redirect(
                url_for("unit_edit",
                        course_id=course_id,
                        course_label=course_label,
                        unit_id=unit_id,
                        unit_label=unit.getLabel()))
        if unit.getType() == "info":
            return render_template('courses/edit/unit-info.html',
                                   title=course.getTitle() + " - " +
                                   unit.getTitle(),
                                   thispage="courses",
                                   course=course,
                                   data=unit)
        elif unit.getType() == "extvideo":
            return render_template('courses/edit/unit-extvideo.html',
                                   title=course.getTitle() + " - " +
                                   unit.getTitle(),
                                   thispage="courses",
                                   course=course,
                                   data=unit)
        elif unit.getType() == "syllabus":
            return render_template('courses/edit/unit-syllabus.html',
                                   title=course.getTitle() + " - " +
                                   unit.getTitle(),
                                   thispage="courses",
                                   course=course,
                                   data=unit)
        elif unit.getType() == "survey":
            return render_template('courses/edit/unit-survey.html',
                                   title=course.getTitle() + " - " +
                                   unit.getTitle(),
                                   thispage="courses",
                                   course=course,
                                   data=unit)
        elif unit.getType() == "quiz":
            return render_template('courses/edit/unit-quiz.html',
                                   title=course.getTitle() + " - " +
                                   unit.getTitle(),
                                   thispage="courses",
                                   course=course,
                                   data=unit)
        elif unit.getType() == "pinboard":
            return render_template('courses/edit/unit-pinboard.html',
                                   title=course.getTitle() + " - " +
                                   unit.getTitle(),
                                   thispage="courses",
                                   course=course,
                                   data=unit)
        abort(500)
Esempio n. 8
0
def unit_show(unit_id, course_id, unit_label=None, course_label=None):
    if not mcourses.Courses.exists(course_id) or not mcourses.Units.exists(
            unit_id):
        abort(404)
    course = mcourses.Courses(course_id)
    unit = mcourses.Units(unit_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label:
        x = url_for("unit_show",
                    course_id=course_id,
                    course_label=course.getLabel(),
                    unit_id=unit_id,
                    unit_label=unit_label if unit_label else unit.getLabel())
        return redirect(x)
    if unit.getLabel() != unit_label:
        return redirect(
            url_for("unit_show",
                    course_id=course_id,
                    course_label=course_label,
                    unit_id=unit_id,
                    unit_label=unit.getLabel()))
    if course.getDetail("state") == 0 and not (
            cuser.isMod() or course.getCourseRole(cuser) >= 2):
        abort(403)
    if unit.isDisabled() and not (cuser.isMod()
                                  or course.getCourseRole(cuser) >= 3):
        abort(403)
    course.setLastVisitedUnitId(cuser, unit.id)
    if unit.getType() == "info":
        if cuser.isLoggedIn() and not cuser.isDisabled():
            unit.addViewData(cuser, None)
        return render_template('courses/units/info.html',
                               title=course.getTitle() + " - " +
                               unit.getTitle(),
                               thispage="courses",
                               course=course,
                               data=unit)
    elif unit.getType() == "extvideo":
        if cuser.isLoggedIn() and not cuser.isDisabled():
            unit.addViewData(cuser, None)
        return render_template('courses/units/extvideo.html',
                               title=course.getTitle() + " - " +
                               unit.getTitle(),
                               thispage="courses",
                               course=course,
                               data=unit)
    elif unit.getType() == "syllabus":
        if cuser.isLoggedIn() and not cuser.isDisabled():
            unit.addViewData(cuser, None)
        return render_template('courses/units/syllabus.html',
                               title=course.getTitle() + " - " +
                               unit.getTitle(),
                               thispage="courses",
                               course=course,
                               data=unit)
    elif unit.getType() == "survey":
        if cuser.isLoggedIn() and not cuser.isDisabled():
            unit.addViewData(cuser, None)
        s = msurvey.Survey(unit.getJSON()["survey"])
        return render_template('courses/units/survey.html',
                               title=course.getTitle() + " - " +
                               unit.getTitle(),
                               thispage="courses",
                               course=course,
                               data=unit,
                               survey=s)
    elif unit.getType() == "quiz":
        try:
            return render_template('courses/units/quiz.html',
                                   title=course.getTitle() + " - " +
                                   unit.getTitle(),
                                   thispage="courses",
                                   course=course,
                                   data=unit,
                                   int=int)
        except Exception as e:
            if request.values.get("re-submit", 0) == "true":
                raise e
            data = {"re-submit": "true", "submission-error": "incomplete"}
            return redirect(
                url_for("unit_show",
                        course_id=course_id,
                        course_label=course_label,
                        unit_id=unit_id,
                        unit_label=unit.getLabel(),
                        **data))
    elif unit.getType() == "pinboard":
        aid = int(unit.getJSON())
        if aid == 0:
            a = None
        else:
            a = mforum.Article(aid)
            if cuser.isLoggedIn() and not cuser.isDisabled():
                unit.addViewData(cuser, None)
        return render_template('courses/units/pinboard.html',
                               title=course.getTitle() + " - " +
                               unit.getTitle(),
                               thispage="courses",
                               course=course,
                               data=unit,
                               post=a)
    abort(500)