def get_topics_all(course, archived=2, numq=True): """ Return a summary of all topics in the course. if archived=0, only return non archived courses if archived=1, only return archived courses if archived=2, return all courses if numq is true then include the number of questions in the topic """ ret = None if archived == 0: ret = run_sql("""SELECT topic, title, position, visibility, archived FROM topics WHERE course=%s AND (archived='0' OR archived IS NULL) ORDER BY position, topic;""", (course,)) elif archived == 1: ret = run_sql("""SELECT topic, title, position, visibility, archived FROM topics WHERE course=%s AND archived='1' ORDER BY position, topic;""", (course,)) elif archived == 2: ret = run_sql("""SELECT topic, title, position, visibility, 0 FROM topics WHERE course=%s ORDER BY position, topic;""", (course,)) info = {} if ret: count = 0 for row in ret: info[count] = {'id': int(row[0]), 'title': row[1], 'position': row[2], 'visibility': row[3], 'archived': row[4]} if info[count]['position'] is None or info[count]['position'] is "None": info[count]['position'] = 0 if numq: info[count]['numquestions'] = Topics.get_num_qs(int(row[0])) count += 1 else: # we probably don't have the archived flag in the Db yet ret = run_sql( """SELECT topic, title, position, visibility FROM topics WHERE course=%s ORDER BY position, topic;""", (course,)) if ret: count = 0 for row in ret: info[count] = {'id': int(row[0]), 'title': row[1], 'position': row[2], 'visibility': row[3]} if info[count]['position'] is None or info[count]['position'] is "None": info[count]['position'] = 0 if numq: info[count]['numquestions'] = Topics.get_num_qs(int(row[0])) count += 1 return info
def get_topic_list(cid, numq=True): """ Return a list of dicts with topic information for the given course. [{ tid: int Topic ID name: string Name of Topic num: int Number of questions (if numq is false, then None) visibility: int Who can see the topic. 0 = Noone, 1 = Staff, 2 = Course, 3 = Student, 4 = Guest },] """ # TODO: magic numbers! tlist = [] topics = Courses.get_topics(int(cid)) for topic in topics: if numq: num = Topics.get_num_qs(topic) else: num = None tlist.append({ 'tid': topic, 'name': Topics.get_name(topic), 'num': num, 'visibility': Topics.get_vis(topic) }) return tlist
def test_create_topic(self): """ Fetch a topic back and check it """ course_id = Courses.create("TEST101", "Test topic position logic", 1, 1) self.assertDictContainsSubset( {course_id: {'active': 1, 'assess_visibility': 'enrol', 'id': course_id, 'name': 'TEST101', 'owner': 1, 'practice_visibility': 'all', 'title': 'Test topic position logic', 'type': 1 } }, Courses.get_courses_dict(), ) topic1_id = Topics.create(course_id, "TESTTOPIC1", 1, 2) topic2_id = Topics.create(course_id, "TESTTOPIC2", 3, 3) self.assertGreater(topic1_id, 0) self.assertIsInstance(topic1_id, int) self.assertGreater(topic2_id, 0) self.assertIsInstance(topic2_id, int) self.assertNotEqual(topic1_id, topic2_id) topic1 = Topics.get_topic(topic1_id) topic2 = Topics.get_topic(topic2_id) self.assertEqual(topic1['id'], topic1_id) self.assertEqual(topic2['id'], topic2_id) self.assertEqual(topic1['title'], "TESTTOPIC1") self.assertEqual(topic2['title'], "TESTTOPIC2") self.assertEqual(topic1['visibility'], 1) self.assertEqual(topic2['visibility'], 3) self.assertEqual(Topics.get_name(topic1_id), topic1['title']) Topics.set_name(topic1_id, "NEWNAME1") self.assertEqual(Topics.get_name(topic1_id), "NEWNAME1") self.assertEqual(Topics.get_num_qs(topic1_id), 0) self.assertEqual(Topics.get_pos(topic1_id), 2) Topics.set_pos(topic1_id, 8) self.assertEqual(Topics.get_pos(topic1_id), 8)
def get_topic_list(cid, numq=True): """ Return a list of dicts with topic information for the given course. [{ tid: int Topic ID name: string Name of Topic num: int Number of questions (if numq is false, then None) visibility: int Who can see the topic. 0 = Noone, 1 = Staff, 2 = Course, 3 = Student, 4 = Guest },] """ # TODO: magic numbers! tlist = [] topics = Courses.get_topics(int(cid)) for topic in topics: if numq: num = Topics.get_num_qs(topic) else: num = None tlist.append({'tid': topic, 'name': Topics.get_name(topic), 'num': num, 'visibility': Topics.get_vis(topic)}) return tlist
def get_topics_all(course, archived=2, numq=True): """ Return a summary of all topics in the course. if archived=0, only return non archived courses if archived=1, only return archived courses if archived=2, return all courses if numq is true then include the number of questions in the topic """ ret = None if archived == 0: ret = run_sql( """SELECT topic, title, position, visibility, archived FROM topics WHERE course=%s AND (archived='0' OR archived IS NULL) ORDER BY position, topic;""", (course, )) elif archived == 1: ret = run_sql( """SELECT topic, title, position, visibility, archived FROM topics WHERE course=%s AND archived='1' ORDER BY position, topic;""", (course, )) elif archived == 2: ret = run_sql( """SELECT topic, title, position, visibility, 0 FROM topics WHERE course=%s ORDER BY position, topic;""", (course, )) info = {} if ret: count = 0 for row in ret: info[count] = { 'id': int(row[0]), 'title': row[1], 'position': row[2], 'visibility': row[3], 'archived': row[4] } if info[count]['position'] is None or info[count][ 'position'] is "None": info[count]['position'] = 0 if numq: info[count]['numquestions'] = Topics.get_num_qs(int(row[0])) count += 1 else: # we probably don't have the archived flag in the Db yet ret = run_sql( """SELECT topic, title, position, visibility FROM topics WHERE course=%s ORDER BY position, topic;""", (course, )) if ret: count = 0 for row in ret: info[count] = { 'id': int(row[0]), 'title': row[1], 'position': row[2], 'visibility': row[3] } if info[count]['position'] is None or info[count][ 'position'] is "None": info[count]['position'] = 0 if numq: info[count]['numquestions'] = Topics.get_num_qs(int( row[0])) count += 1 return info