def handle(self, *args, **options):

        if len(args):
            sis_term_id = args[0]
        else:
            raise CommandError("term_sis_id is required")

        report_client = Reports()

        term = report_client.get_term_by_sis_id(sis_term_id)

        user_report = report_client.create_course_sis_export_report(
            settings.RESTCLIENTS_CANVAS_ACCOUNT_ID, term_id=term.term_id
        )

        sis_data = report_client.get_report_data(user_report)

        report_client.delete_report(user_report)

        ind_study_regexp = re.compile("-[A-F0-9]{32}$")
        course_client = Courses()
        print ["course_id", "name", "published", "public_syllabus"]

        row_count = sum(1 for row in csv.reader(sis_data))
        curr_row = 0
        for row in csv.reader(sis_data):
            curr_row += 1
            if not len(row):
                continue

            sis_course_id = row[0]
            course_name = row[1]

            try:
                valid_academic_course_sis_id(sis_course_id)
            except CoursePolicyException:
                continue

            if ind_study_regexp.match(sis_course_id):
                continue

            try:
                course = course_client.get_course_by_sis_id(sis_course_id, params={"include": "syllabus_body"})
            except DataFailureException as ex:
                print ex
                continue

            if course.syllabus_body is None:
                continue

            csv_line = [sis_course_id, course_name, str(course.workflow_state), course.public_syllabus]

            print csv_line
            print "Remaining: %s" % (row_count - curr_row)
            print csv_line
            sleep(1)
def get_unused_course_report_data(term_sis_id):
    report_client = Reports()
    term = report_client.get_term_by_sis_id(term_sis_id)

    unused_course_report = report_client.create_unused_courses_report(
        settings.RESTCLIENTS_CANVAS_ACCOUNT_ID, term_id=term.term_id)

    report_data = report_client.get_report_data(unused_course_report)

    report_client.delete_report(unused_course_report)

    return report_data
    def handle(self, *args, **options):

        if len(args):
            sis_term_id = args[0]
        else:
            raise CommandError("term_sis_id is required")

        report_client = Reports()

        term = report_client.get_term_by_sis_id(sis_term_id)

        user_report = report_client.create_course_provisioning_report(
            settings.RESTCLIENTS_CANVAS_ACCOUNT_ID, term_id=term.term_id)

        sis_data = report_client.get_report_data(user_report)

        report_client.delete_report(user_report)

        ind_study_regexp = re.compile("-[A-F0-9]{32}$")
        course_client = Courses()

        for row in csv.reader(sis_data):
            if not len(row):
                continue

            sis_course_id = row[1]
            status = row[8]

            try:
                valid_academic_course_sis_id(sis_course_id)
            except CoursePolicyException:
                continue

            if ind_study_regexp.match(sis_course_id):
                continue

            if status is not None and status == "active":
                print sis_course_id
Пример #4
0
 def __init__(self):
     self._accounts = CanvasAccounts(per_page=50)
     self._analytics = CanvasAnalytics()
     self._reports = CanvasReports()
Пример #5
0
class ReportBuilder():
    def __init__(self):
        self._accounts = CanvasAccounts(per_page=50)
        self._analytics = CanvasAnalytics()
        self._reports = CanvasReports()

    def build_subaccount_activity_report(self, root_account_id, sis_term_id):
        report = Report(report_type=Report.SUBACCOUNT_ACTIVITY,
                        started_date=datetime.utcnow().replace(tzinfo=utc))
        report.save()

        accounts = []
        account_courses = {}

        root_account = self._accounts.get_account_by_sis_id(root_account_id)
        accounts.append(root_account)
        accounts.extend(self._accounts.get_all_sub_accounts_by_sis_id(root_account_id))

        for account in accounts:
            sis_account_id = account.sis_account_id
            if sis_account_id is None:
                continue

            account_courses[sis_account_id] = {
                "courses": 0,
                "active_courses": 0,
                "ind_study_courses": 0,
                "active_ind_study_courses": 0,
            }

            activity = SubaccountActivity(report=report,
                                          term_id=sis_term_id,
                                          subaccount_id=sis_account_id,
                                          subaccount_name=account.name)

            data = self._analytics.get_statistics_by_account(sis_account_id,
                                                             sis_term_id)

            for key, val in data.items():
                if key == "courses":
                    continue

                setattr(activity, key, val)

            data = self._analytics.get_activity_by_account(sis_account_id,
                                                           sis_term_id)

            for item in data["by_category"]:
                setattr(activity, "%s_views" % item["category"], item["views"])

            activity.save()

        # Generate course totals
        term = self._reports.get_term_by_sis_id(sis_term_id)
        course_prov_report = self._reports.create_course_provisioning_report(
            root_account.account_id, term.term_id,
            params={"include_deleted": True})

        course_data = self._reports.get_report_data(course_prov_report)
        header = course_data.pop(0)
        for row in csv.reader(course_data):
            if not len(row):
                continue

            sis_course_id = row[1]
            sis_account_id = row[5]
            if (sis_course_id is None or sis_account_id is None or
                sis_account_id not in account_courses):
                continue

            status = row[8]
            ind_study = True if len(sis_course_id.split("-")) == 6 else False
            is_active = True if status == "active" else False
            for sis_id in account_courses:
                if sis_account_id.find(sis_id) == 0:
                    account_courses[sis_id]["courses"] += 1
                    if is_active:
                        account_courses[sis_id]["active_courses"] += 1
                    if ind_study:
                        account_courses[sis_id]["ind_study_courses"] += 1
                        if is_active:
                            account_courses[sis_id]["active_ind_study_courses"] += 1

        # Save course totals
        for sis_account_id in account_courses:
            try:
                totals = account_courses[sis_account_id]
                activity = SubaccountActivity.objects.get(report=report,
                    term_id=sis_term_id, subaccount_id=sis_account_id)
                activity.courses = totals["courses"]
                activity.active_courses = totals["active_courses"]
                activity.ind_study_courses = totals["ind_study_courses"]
                activity.active_ind_study_courses = totals["active_ind_study_courses"]
                activity.save()
            except SubaccountActivity.DoesNotExist:
                continue

        report.finished_date = datetime.utcnow().replace(tzinfo=utc)
        report.save()
    def handle(self, *args, **options):
        if len(args) == 2:
            subaccount_id = args[0]
            sis_term_id = args[1]
        else:
            raise CommandError("find_active_instructors <subaccount_id> <term_id>")

        accounts = Accounts()
        reports = Reports()
        pws = PWS()

        account = accounts.get_account(subaccount_id)
        term = reports.get_term_by_sis_id(sis_term_id)

        enrollment_report = reports.create_enrollments_provisioning_report(account.account_id, term.term_id)
        enrollment_data = reports.get_report_data(enrollment_report)

        all_instructors = {}

        enrollment_csv_data = csv.reader(enrollment_data)
        header = enrollment_csv_data.next()
        course_id_idx = header.index("course_id")
        sis_user_id_idx = header.index("user_id")
        role_idx = header.index("role")
        status_idx = header.index("status")

        for row in enrollment_csv_data:
            if not len(row):
                continue

            course_id = row[course_id_idx]
            sis_user_id = row[sis_user_id_idx]
            role = row[role_idx]
            status = row[status_idx]

            if sis_user_id != "" and role.lower() == "teacher" and status.lower() == "active":
                if course_id not in all_instructors:
                    all_instructors[course_id] = []

                all_instructors[course_id].append(sis_user_id)

        course_report = reports.create_course_provisioning_report(account.account_id, term.term_id)
        course_data = reports.get_report_data(course_report)

        course_csv_data = csv.reader(course_data)
        header = course_csv_data.next()
        course_id_idx = header.index("course_id")
        sis_account_id_idx = header.index("account_id")
        status_idx = header.index("status")

        active_instructors = {}
        for row in course_csv_data:
            if not len(row):
                continue

            course_id = row[course_id_idx]
            sis_account_id = row[sis_account_id_idx]
            status = row[status_idx]

            if sis_account_id != "" and status.lower() == "active" and course_id in all_instructors:
                for sis_user_id in all_instructors[course_id]:
                    if sis_user_id not in active_instructors:
                        try:
                            person = pws.get_person_by_regid(sis_user_id)
                            email = person.uwnetid + "@uw.edu"
                            active_instructors[sis_user_id] = email
                        except InvalidRegID:
                            continue
                        except DataFailureException as err:
                            if err.status == 404:
                                continue
                            else:
                                raise

        filename = "-".join(["active-instructors", subaccount_id, sis_term_id])
        outpath = dirname(__file__) + "/" + filename + ".txt"

        f = open(outpath, "w")
        data = active_instructors.values()
        data.sort()
        f.write("\n".join(data))
        f.close()

        reports.delete_report(enrollment_report)
        reports.delete_report(course_report)

        print outpath