예제 #1
0
def course_single_branch(id, course_id, course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("course_single_branch",
                    id=id,
                    course_id=course_id,
                    course_label=course.getLabel()))
    if not mpull_requests.Branch.exists(id):
        abort(404)
    branch = mpull_requests.Branch(id)

    if not (branch.getDetail("author") == cuser.id or cuser.isMod(
    )) or cuser.isDisabled() or branch.getDetail("course_id") != course.id:
        abort(404)

    return render_template('courses/pull-requests/branch.html',
                           title="Branch #" + str(branch.id) + " für " +
                           course.getTitle(),
                           thispage="courses",
                           course=course,
                           branch=branch)
예제 #2
0
def course_single_pr(id, course_id, course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("course_single_pr",
                    id=id,
                    course_id=course_id,
                    course_label=course.getLabel()))
    if not mpull_requests.PullRequest.exists(id):
        abort(404)
    pr = mpull_requests.PullRequest(id)
    branch = pr.getBranch()

    if pr.isHiddenAsSpam() and not cuser.isLoggedIn() or pr.getDetail(
            "course_id") != course.id:
        abort(404)

    return render_template('courses/pull-requests/pr.html',
                           title="PR #" + str(pr.id) + " für " +
                           course.getTitle(),
                           thispage="courses",
                           course=course,
                           pr=pr,
                           branch=branch)
예제 #3
0
def branch_cancel(branch_id, course_id, course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("branch_submit",
                    branch_id=branch_id,
                    course_id=course_id,
                    course_label=course.getLabel()))
    if not mpull_requests.Branch.exists(branch_id):
        abort(404)
    branch = mpull_requests.Branch(branch_id)

    if branch.getDetail("pull_request") or branch.isAbandoned(): abort(404)

    if request.method == "POST":
        branch.setDetail("abandoned", 1)
        branch.setDetail("abandoned_date", time.time())
        return jsonify({
            "url":
            url_for("course_single_branch",
                    id=branch.id,
                    course_id=course.id,
                    course_label=course.getLabel())
        })
    else:
        return render_template('courses/pull-requests/cancel.html',
                               title="Verwerfen: Branch #" + str(branch.id) +
                               " für " + course.getTitle(),
                               thispage="courses",
                               course=course,
                               branch=branch)
예제 #4
0
def branch_revert_override(override_id,
                           branch_id,
                           course_id,
                           course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("branch_revert_override",
                    branch_id=branch_id,
                    course_id=course_id,
                    course_label=course.getLabel(),
                    override_id=override_id))
    if not mpull_requests.Branch.exists(branch_id):
        abort(404)
    branch = mpull_requests.Branch(branch_id)

    if branch.getDetail("pull_request") or branch.isAbandoned(): abort(404)

    if not (branch.getDetail("author") == cuser.id or cuser.isMod(
    )) or cuser.isDisabled() or branch.getDetail("course_id") != course.id:
        abort(404)

    override = branch.getSingleOverride(override_id)
    if not override or override["branch"] != branch_id:
        abort(404)
    branch.removeOverride(override_id)

    return redirect(
        url_for("course_single_branch",
                id=branch_id,
                course_id=course_id,
                course_label=course.getLabel()))
예제 #5
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)
예제 #6
0
파일: survey.py 프로젝트: pilearn-dev/core
def survey_edit(id, label=None):
    cuser = muser.getCurrentUser()
    if msurvey.Survey.exists(id):
        survey = msurvey.Survey(id)
        forum = mforum.Forum(survey.getDetail("associated_forum"))
        if not (cuser.isDev() or survey.getDetail("survey_owner")
                == cuser.id) or cuser.isDisabled():
            if survey.getDetail("associated_forum") == 0:
                abort(404)
            else:
                cou = mcourses.Courses(forum.id)
                if not cou.getCourseRole(cuser) > 3 or cuser.isDIsabled():
                    abort(404)
        if request.method == "GET":
            if label != survey.getLabel():
                return redirect(
                    url_for("survey_edit", id=id, label=survey.getLabel()))
            else:
                return render_template("survey/edit.html",
                                       title="Umfrage bearbeiten - " +
                                       survey.getTitle(),
                                       survey=survey,
                                       forum=forum)
        elif request.method == "POST":
            survey.setDetail("content", request.json["content"])
            survey.setDetail("title", request.json["title"])
            survey.setDetail("state", request.json["state"])
            return "ok"
    else:
        abort(404)
예제 #7
0
파일: survey.py 프로젝트: pilearn-dev/core
def survey_results(id, label=None):
    cuser = muser.getCurrentUser()
    if msurvey.Survey.exists(id):
        survey = msurvey.Survey(id)
        forum = mforum.Forum(survey.getDetail("associated_forum"))
        if not (cuser.isDev() or survey.getDetail("survey_owner") == cuser.id):
            if survey.getDetail("associated_forum") == 0:
                abort(404)
            else:
                cou = mcourses.Courses(forum.id)
                if not cou.getCourseRole(cuser) > 3:
                    abort(404)
        if request.method == "GET":
            if label != survey.getLabel():
                return redirect(
                    url_for("survey_results", id=id, label=survey.getLabel()))
            else:
                return render_template("survey/results_direct.html",
                                       title="Umfrageergebnisse - " +
                                       survey.getTitle(),
                                       survey=survey,
                                       forum=forum,
                                       int=int,
                                       len=len)
        elif request.method == "POST":
            RESULT = {}
            con = survey.getContent()
            i = 1
            for c in con:
                if c["type"] == "text-answer":
                    RESULT[i] = {
                        "question": c["data"]["question"],
                        "data": [],
                        "type": "text-answer"
                    }
                if c["type"] == "multiple-choice":
                    selc = len(c["data"]["choices"])
                    RESULT[i] = {
                        "question": c["data"]["question"],
                        "data": ["0" for _ in range(selc)],
                        "type": "multiple-choice",
                        "choices": c["data"]["choices"]
                    }
                i += 1
            subm = survey.getSubmissions()
            for s in subm:
                s = json.loads(s[0])
                for field, value in list(s.items()):
                    r = RESULT[int(field)]
                    if r["type"] == "text-answer":
                        if value:
                            r["data"].append(value)
                    elif r["type"] == "multiple-choice":
                        r["data"][int(value)] += str(
                            int(r["data"][int(value) - 1]) + 1)
            survey.setDetail("results", json.dumps(RESULT))
            return "done"
    else:
        abort(404)
예제 #8
0
def course_related_proposal(id, label=None):
    if not mcourses.Courses.exists(id):
        abort(404)
    course = mcourses.Courses(id)
    if course.getLabel() != label:
        return redirect(
            url_for("course_related_proposal", id=id, label=course.getLabel()))
    return redirect(
        url_for("proposal.single", id=mproposal.Proposal.byCourse(id).id))
예제 #9
0
def course_info(id, label=None):
    if not mcourses.Courses.exists(id):
        abort(404)
    course = mcourses.Courses(id)
    if course.getLabel() != label:
        return redirect(url_for("course_info", id=id, label=course.getLabel()))
    return render_template('courses/info.html',
                           title=course.getTitle(),
                           thispage="courses",
                           data=course,
                           ForumAnnouncement=mforum.ForumAnnouncement)
예제 #10
0
def course_decide_pr(id, course_id, course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("course_single_pr",
                    id=id,
                    course_id=course_id,
                    course_label=course.getLabel()))
    if not mpull_requests.PullRequest.exists(id):
        abort(404)
    pr = mpull_requests.PullRequest(id)
    branch = pr.getBranch()

    if pr.isHiddenAsSpam() and not cuser.isLoggedIn() or pr.getDetail(
            "course_id") != course.id:
        abort(404)

    if pr.getState() != 0:
        abort(404)

    if not (cuser.isMod() or course.getCourseRole(cuser) >= 3):
        abort(403)

    if request.json["decision"] not in list(range(-1, 2)):
        abort(400)

    if request.json["as_abuse"] not in [True, False]:
        abort(400)

    pr.setDetail("decision", request.json["decision"])
    pr.setDetail("decision_date", time.time())
    pr.setDetail("hide_as_spam", request.json["as_abuse"])

    branch.setDetail("decision", request.json["decision"])
    branch.setDetail("decision_date", time.time())
    branch.setDetail("hide_as_spam", request.json["as_abuse"])

    if request.json["decision"] == 1:
        au = branch.getAuthor()
        au.setReputationChange(
            "pr",
            "[Pull Request #" + str(pr.id) + " für " + course.getTitle() +
            "](/c/" + str(course.id) + "/pull-request/" + str(pr.id) + ")",
            branch.getDetail("delta_factor"))
        au.regetRep()
        branch.apply()

    return "ok"
예제 #11
0
def course_create_branch(id, label=None):
    if not mcourses.Courses.exists(id):
        abort(404)
    course = mcourses.Courses(id)
    cuser = muser.getCurrentUser()
    if not cuser.isLoggedIn() or cuser.isDisabled():
        abort(404)

    existing_branch = mpull_requests.Branch.getByCourseAndUser(
        course.id, cuser.id, True)
    if existing_branch:
        abort(400)

    mpull_requests.Branch.new(course.id, cuser.id)
    return "okay"
예제 #12
0
def course_enroll(id, label=None):
    if not mcourses.Courses.exists(id) or muser.require_login(
    ) or muser.getCurrentUser().isDisabled():
        abort(404)
    course = mcourses.Courses(id)
    if course.getLabel() != label:
        return redirect(
            url_for("course_enroll", id=id, label=course.getLabel()))
    cuser = muser.getCurrentUser()
    if course.getCourseRole(cuser) == 4 or (
            cuser.isDev() and
        (course.isEnrolled(cuser) or course.getDetail("manual_enrollment"))):
        if request.method == "POST":
            user = muser.User.safe(request.form["id"])
            if not user:
                abort(400)
            course.enroll(user)
            if "add_another" in request.form:
                return redirect(url_for("course_enroll", id=id, label=label))
            else:
                return redirect(
                    url_for("course_admin",
                            id=id,
                            label=label,
                            page="membership"))
        return render_template("courses/enroll.html",
                               title="Benutzer einschreiben: " +
                               course.getTitle(),
                               thispage="courses",
                               course=course)
    elif not course.getDetail("manual_enrollment"):
        if not course.isEnrolled(cuser):
            course.enroll(cuser)
            enrolled_count = course.getEnrolledCount()
            earners = course.getEnrolledByPerm(4) + course.getEnrolledByPerm(3)
            if enrolled_count - len(earners) < 50:
                for e in earners:
                    e.setDetail("reputation", 1 + e.getInfo()["reputation"])
                    e.setReputationChange(
                        "enroll", "Kurs: [" + course.getTitle() + "](/c/" +
                        str(course.id) + ")", 1)
        if course.getDetail("state") != 0:
            return redirect(
                url_for("course_start", label=course.getLabel(), id=course.id))
        return redirect(
            url_for("course_info", label=course.getLabel(), id=course.id))
    else:
        abort(403)
예제 #13
0
def course_edit_overview(id, label=None):
    if not mcourses.Courses.exists(id):
        abort(404)
    course = mcourses.Courses(id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != label and request.method != "POST":
        return redirect(
            url_for("course_edit_overview", id=id, label=course.getLabel()))
    if not (cuser.isMod()
            or course.getCourseRole(cuser) >= 3) or cuser.isDisabled():
        abort(404)

    return render_template('courses/edit/index.html',
                           title=course.getTitle(),
                           thispage="courses",
                           course=course)
예제 #14
0
def branch_item(unit_id, override_id, branch_id, course_id, course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("branch_item",
                    branch_id=branch_id,
                    course_id=course_id,
                    course_label=course.getLabel(),
                    unit_id=unit_id,
                    override_id=override_id))
    if not mpull_requests.Branch.exists(branch_id):
        abort(404)
    branch = mpull_requests.Branch(branch_id)

    if not (branch.getDetail("author") == cuser.id or cuser.isMod(
    )) or cuser.isDisabled() or branch.getDetail("course_id") != course.id:
        abort(404)

    data = _mkdata(unit_id, override_id, course_id, branch)

    if branch.getDetail("pull_request") or branch.isAbandoned():
        return render_template(
            'courses/pull-requests/item.static/' + data["type"] + '.html',
            title="Branch #" + str(branch.id) + " für " + course.getTitle(),
            thispage="courses",
            course=course,
            branch=branch,
            data=data,
            unit_id=unit_id,
            override_id=override_id,
            int=int)
    else:
        return render_template(
            'courses/pull-requests/item/' + data["type"] + '.html',
            title="Branch #" + str(branch.id) + " für " + course.getTitle(),
            thispage="courses",
            course=course,
            branch=branch,
            data=data,
            unit_id=unit_id,
            override_id=override_id)
예제 #15
0
def course_result_confirmation_of_participation(id, label=None):
    if not mcourses.Courses.exists(id):
        abort(404)
    course = mcourses.Courses(id)
    cuser = muser.getCurrentUser()
    if not course.isEnrolled(cuser) or cuser.isDisabled():
        abort(404)

    if course.getLabel() != label:
        return redirect(
            url_for("course_result_confirmation_of_participation",
                    id=id,
                    label=course.getLabel()))

    eligibility_criteria = course.getViewRatings(cuser)

    if eligibility_criteria[0] < 80 or eligibility_criteria[1] < 80:
        return "Anforderungen nicht erfüllt: mind. 80%% besucht und mind. 80%% der Punkte bei Quizzen; du hast %i%% besucht und %i%% der Punkte" % (
            eligibility_criteria[1], eligibility_criteria[0])

    html = render_template("certificates/confirmation_of_participation.html",
                           course_title=course.getTitle(),
                           user_name=cuser.getDetail("certificate_full_name"),
                           now=ctimes.stamp2germandate(time.time()),
                           cwd=os.getcwd().replace("\\", "/"),
                           course_id=course.id,
                           user_id=cuser.id)

    pdf = pdfkit.from_string(html,
                             False,
                             options={
                                 "title": _("Teilnahmeurkunde"),
                                 'margin-top': '0mm',
                                 'margin-right': '0mm',
                                 'margin-bottom': '0mm',
                                 'margin-left': '0mm',
                                 'encoding': "UTF-8",
                                 'quiet': ''
                             })

    http_response = make_response(pdf)
    http_response.headers["Content-Type"] = "application/pdf"
    return http_response
예제 #16
0
def course_result(id, label=None):
    if not mcourses.Courses.exists(id):
        abort(404)
    course = mcourses.Courses(id)
    cuser = muser.getCurrentUser()
    if not course.isEnrolled(cuser) or cuser.isDisabled():
        abort(404)

    if course.getLabel() != label:
        return redirect(
            url_for("course_result", id=id, label=course.getLabel()))

    CoP_ratings = eligibility_criteria = course.getViewRatings(cuser)

    return render_template('courses/result.html',
                           title=course.getTitle(),
                           thispage="courses",
                           course=course,
                           CoP_ratings=CoP_ratings)
예제 #17
0
def course_start(id, label=None):
    if not mcourses.Courses.exists(id):
        abort(404)
    course = mcourses.Courses(id)
    if course.getLabel() != label:
        return redirect(url_for("course_start", id=id,
                                label=course.getLabel()))
    cuser = muser.getCurrentUser()
    if course.getDetail("state") == 0 and not (
            cuser.isMod() or course.getCourseRole(cuser) >= 2):
        abort(403)
    last_unit = 0
    if course.isEnrolled(cuser):
        last_unit = course.getLastVisitedUnitId(cuser)
    if last_unit == 0:
        last_unit = course.getFirstUnit(cuser.isMod()
                                        or course.getCourseRole(cuser) >= 3)
    if last_unit == 0:
        abort(403)
    return redirect(
        url_for("unit_show", unit_id=last_unit, course_id=course.id))
예제 #18
0
def course_prs(course_id, course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("course_prs",
                    course_id=course_id,
                    course_label=course.getLabel()))

    open_prs, closed_prs = mpull_requests.PullRequest.getByCourse(
        course.id,
        True), mpull_requests.PullRequest.getByCourse(course.id, False)

    return render_template('courses/pull-requests/prs.html',
                           title="Pull Requests für " + course.getTitle(),
                           thispage="courses",
                           course=course,
                           open_prs=open_prs,
                           closed_prs=closed_prs)
예제 #19
0
def branch_submit(branch_id, course_id, course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("branch_submit",
                    branch_id=branch_id,
                    course_id=course_id,
                    course_label=course.getLabel()))
    if not mpull_requests.Branch.exists(branch_id):
        abort(404)
    branch = mpull_requests.Branch(branch_id)

    if branch.getDetail("pull_request") or branch.isAbandoned(): abort(404)

    if request.method == "POST":
        if course.getCourseRole(cuser) < 3:
            branch.calculateRepDelta()
        else:
            branch.setDetail("delta_factor", 0)
        pr = mpull_requests.PullRequest.new(course.id, branch.id, cuser.id,
                                            request.json["title"],
                                            request.json["description"])
        branch.setDetail("pull_request", pr.id)
        return jsonify({
            "url":
            url_for("course_single_pr",
                    id=pr.id,
                    course_id=course.id,
                    course_label=course.getLabel())
        })
    else:
        return render_template('courses/pull-requests/submit.html',
                               title="Neues Pull Request zu Branch #" +
                               str(branch.id) + " für " + course.getTitle(),
                               thispage="courses",
                               course=course,
                               branch=branch)
예제 #20
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)
예제 #21
0
def branch_new_item(branch_id, course_id, course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("branch_new_item",
                    branch_id=branch_id,
                    course_id=course_id,
                    course_label=course.getLabel()))
    if not mpull_requests.Branch.exists(branch_id):
        abort(404)
    branch = mpull_requests.Branch(branch_id)

    if branch.getDetail("pull_request") or branch.isAbandoned(): abort(404)

    if not (branch.getDetail("author") == cuser.id or cuser.isMod(
    )) or cuser.isDisabled() or branch.getDetail("course_id") != course.id:
        abort(404)

    empty_set = {
        "info": "[]",
        "quiz": "[]",
        "extvideo": '{"platform":"youtube", "embedcode": ""}',
        "syllabus": ''
    }

    x = branch.newUnitOverride("Neue Seite", empty_set[request.json["type"]],
                               request.json["type"])

    return url_for("branch_item",
                   branch_id=branch_id,
                   course_id=course_id,
                   course_label=course.getLabel(),
                   unit_id="-",
                   override_id=x)
예제 #22
0
def course_branches(id, label=None):
    if not mcourses.Courses.exists(id):
        abort(404)
    course = mcourses.Courses(id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != label and request.method != "POST":
        return redirect(
            url_for("course_branches", id=id, label=course.getLabel()))
    if not cuser.isLoggedIn() or cuser.isDisabled():
        abort(404)

    existing_branch = mpull_requests.Branch.getByCourseAndUser(
        course.id, cuser.id, True)
    if existing_branch:
        return redirect(
            url_for("course_single_branch",
                    course_id=id,
                    course_label=course.getLabel(),
                    id=existing_branch.id))

    return render_template('courses/pull-requests/branches.html',
                           title=course.getTitle(),
                           thispage="courses",
                           course=course)
예제 #23
0
def branch_update_item(unit_id,
                       override_id,
                       branch_id,
                       course_id,
                       course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("branch_update_item",
                    branch_id=branch_id,
                    course_id=course_id,
                    course_label=course.getLabel(),
                    unit_id=unit_id,
                    override_id=override_id))
    if not mpull_requests.Branch.exists(branch_id):
        abort(404)
    branch = mpull_requests.Branch(branch_id)

    if branch.getDetail("pull_request") or branch.isAbandoned(): abort(404)

    if not (branch.getDetail("author") == cuser.id or cuser.isMod(
    )) or cuser.isDisabled() or branch.getDetail("course_id") != course.id:
        abort(404)

    data = _mkdata(unit_id, override_id, course_id, branch)

    data["title"] = request.json["title"]
    data["content"] = json.dumps(request.json["content"])

    result = branch.updateOrMakeOverride(unit_id, override_id, data)
    if type(result) == bool: abort(500)

    return jsonify({"override_id": result})
예제 #24
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)
예제 #25
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)
예제 #26
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)
예제 #27
0
def branch_unit_reorder(branch_id, course_id, course_label=None):
    if not mcourses.Courses.exists(course_id):
        abort(404)
    course = mcourses.Courses(course_id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != course_label and request.method != "POST":
        return redirect(
            url_for("branch_unit_reorder",
                    branch_id=branch_id,
                    course_id=course_id,
                    course_label=course.getLabel()))
    if not mpull_requests.Branch.exists(branch_id):
        abort(404)
    branch = mpull_requests.Branch(branch_id)

    if branch.getDetail("pull_request") or branch.isAbandoned(): abort(404)

    if request.method == "POST":
        datar = (request.json)
        for item in datar:
            unit_id, override_id = item["id"].split("/")

            data = _mkdata(unit_id, override_id, course_id, branch)

            unit_id = int(unit_id) if unit_id != "-" else 0
            if data["parent_unit"] != 0 or data["parent_override"] != 0 or data[
                    "unit_order"] != item["order"]:
                data["parent_unit"] = 0
                data["parent_override"] = 0
                data["unit_order"] = item["order"]

                data["content"] = json.dumps(data["content"])
                branch.updateOrMakeOverride(unit_id, override_id, data)

            for subitem in item["subitems"]:
                sub_unit_id, sub_override_id = subitem["id"].split("/")
                data = _mkdata(sub_unit_id, sub_override_id, course_id, branch)

                sub_unit_id = int(sub_unit_id) if sub_unit_id != "-" else 0
                parent_unit, parent_override = unit_id, int(
                    override_id) if override_id != "-" else 0

                if data["parent_unit"] != parent_unit or data[
                        "parent_override"] != parent_override or data[
                            "unit_order"] != subitem["order"]:
                    data["parent_unit"] = parent_unit
                    data["parent_override"] = parent_override
                    data["unit_order"] = subitem["order"]

                    data["content"] = json.dumps(data["content"])
                    branch.updateOrMakeOverride(sub_unit_id, sub_override_id,
                                                data)

        return "ok"
    else:
        return render_template('courses/pull-requests/reorder.html',
                               title="Branch #" + str(branch.id) + " für " +
                               course.getTitle(),
                               thispage="courses",
                               course=course,
                               branch=branch)
예제 #28
0
def course_admin(id, label=None, page="identity"):
    if not mcourses.Courses.exists(id):
        abort(404)
    course = mcourses.Courses(id)
    cuser = muser.getCurrentUser()
    if course.getLabel() != label and request.method != "POST":
        return redirect(url_for("course_admin", id=id,
                                label=course.getLabel()))
    if not (cuser.isMod()
            or course.getCourseRole(cuser) == 4) or cuser.isDisabled():
        abort(404)
    if page == "identity":
        if request.method == "POST":

            data = request.json

            title = data["title"].strip()
            shortdesc = data["shortdesc"].strip()
            longdesc = data["longdesc"].strip()
            requirements = data["requirements"].strip()

            errors = _validateCourse(title, shortdesc, longdesc, requirements)

            if len(errors):
                return jsonify({"result": "error", "errors": errors})
            else:
                course.setDetail("title", title)
                course.setDetail("shortdesc", shortdesc)
                course.setDetail("longdesc", longdesc)
                course.setDetail("requirements", requirements)
                return jsonify({"result": "ok"})

        else:
            return render_template('courses/admin/identity.html',
                                   title=course.getTitle(),
                                   thispage="courses",
                                   course=course)
    elif page == "picture":
        if request.method == "POST":

            data = request.json

            url = data["picture_url"].strip()
            if url != "":
                if not re.match("^[a-zA-Z]+\/[a-zA-Z_0-9+]+\.jpg$", url):
                    url = ""

            course.setDetail("picture_url", url)

            return "true"
        else:
            directories = os.listdir(
                os.path.join(os.getcwd(),
                             "main/static/CourseStockImages_small"))
            files = [(i,
                      os.listdir(
                          os.path.join(os.getcwd(),
                                       "main/static/CourseStockImages_small",
                                       i))) for i in directories]
            return render_template('courses/admin/picture.html',
                                   title=course.getTitle(),
                                   thispage="courses",
                                   course=course,
                                   files=files)
    elif page == "announcements":
        return render_template("announcements/list.html",
                               title=course.getTitle(),
                               forum=mforum.Forum(course.id),
                               announcements=mforum.ForumAnnouncement.byForum(
                                   course.id, True),
                               thispage="courses")
    elif page == "publish":
        if request.method == "POST":
            course.setDetail("state", 1)
            return "{ok}"
        else:
            return render_template('courses/admin/publish.html',
                                   title=course.getTitle(),
                                   thispage="courses",
                                   course=course)
    elif page == "membership":
        if request.method == "POST":
            if request.json["action"] == "give-role":
                user_to_receive = muser.User(request.json["user"])
                role_to_receive = request.json["role"]
                if not role_to_receive in range(1, 5):
                    return jsonify({
                        "result": "error",
                        "error": "Ungültige Ziel-Rolle"
                    })
                if course.isEnrolled(user_to_receive):
                    course.setCourseRole(user_to_receive, role_to_receive)
                    return jsonify({"result": "success"})
                else:
                    return jsonify({
                        "result":
                        "error",
                        "error":
                        "Nur möglich für Benutzer, die im Kurs eingeschrieben sind."
                    })
            elif request.json["action"] == "revoke-role":
                user_to_receive = muser.User(request.json["user"])
                if course.isEnrolled(user_to_receive) and course.getCourseRole(
                        user_to_receive) != 1:
                    course.setCourseRole(user_to_receive, 1)
                    return jsonify({"result": "success"})
            elif request.json["action"] == "kick-remove":
                user_to_receive = muser.User(request.json["user"])
                if course.isEnrolled(user_to_receive) and course.getCourseRole(
                        user_to_receive) == 1:
                    course.unenroll(user_to_receive)
                    user_to_receive.customflag(
                        "Benutzer von " + cuser.getHTMLName(False) + " (#" +
                        str(cuser.id) + ") aus dem Kurs " + course.getTitle() +
                        " (#" + str(course.id) + ") geworfen.",
                        muser.User.from_id(-1))
                    return jsonify({"result": "success"})
                else:
                    return jsonify({
                        "result":
                        "error",
                        "error":
                        "Nicht möglich: Benutzer nicht eingeschrieben oder Benutzer hat Rolle."
                    })

            return jsonify({"result": "error", "error": "Ungültige Anfrage"})
        else:
            return render_template('courses/admin/membership.html',
                                   title=course.getTitle(),
                                   thispage="courses",
                                   course=course)
    else:
        abort(404)
예제 #29
0
 def getCourse(self):
     return mcourses.Courses(self.getDetail("course_id"))
예제 #30
0
 def getCourse(self):
     print((self.getDetail("courseid")))
     return mcourses.Courses(self.getDetail("courseid"))