moodleNotifer
Functions
login.py File Reference

This file is used get the courses information from the moodle. More...

Go to the source code of this file.

Functions

def main.login.prune_dict (list_of_data, keys)
 
def main.login.string_from_html (data)
 
def main.login.get_list_of_courses (request_helper)
 This definition is used to get list of courses the user registered. More...
 
def main.login.get_assignments_info (request_helper, course_id, all=True)
 This definition is used to get the assignment information for the courses. More...
 
def main.login.get_quizzes_info (request_helper, course_id, all=True)
 This definition is used to get the information about quizzes for the courses. More...
 
def main.login.get_course_contents (request_helper, course_id)
 This definition is used to get the content added in the course in weekly. More...
 
def main.login.get_discussion_forum_id (request_helper, course_id)
 This definition is used to get the Announcements forum. More...
 
def main.login.get_discussion_forum_discussions (request_helper, forum_id)
 This definition is used to get latest 5 discussions in the discussion forum. More...
 
def main.login.get_discussion_forum_posts ()
 

Detailed Description

This file is used get the courses information from the moodle.

grep -rnw moodle_dl -e post_REST : command to get all places in code where pose_REST has been called

Definition in file login.py.

Function Documentation

◆ get_assignments_info()

def main.login.get_assignments_info (   request_helper,
  course_id,
  all = True 
)

This definition is used to get the assignment information for the courses.

Returns information of all assignments , if the second aruguement is false it returns info of only the latest assignment
input : object of class RequestHelper
output : list of all assignments with following attributes {'id','name','course','duedate','intro','introattachments'}

Definition at line 65 of file login.py.

65 def get_assignments_info(request_helper,course_id,all = True):
66  '''
67  Returns information of all assignments , if the second aruguement is false it returns info of only the latest assignment
68  input : object of class RequestHelper
69  output : list of all assignments with following attributes {'id','name','course','duedate','intro','introattachments'}
70  '''
71  extra_data = {}
72  courseids = {"0":course_id}
73  extra_data.update({'courseids': courseids})
74  result = request_helper.post_REST('mod_assign_get_assignments', extra_data)
75  assignments = result['courses'][0]['assignments']
76  l = []
77 
78  for assign in assignments:
79  data = dict((k, assign[k]) for k in assign if k in ['id','name','course','duedate','timemodified','intro','introattachments'])
80  soup = BeautifulSoup(data['intro'],'lxml')
81  #processing intro
82  if data['intro']:
83  data['intro'] = string_from_html(data['intro'])
84  #processing introattachments
85  if data['introattachments']:
86  data['introattachments'] = prune_dict(data['introattachments'],['filename','fileurl'])
87  l.append(data)
88 
89  if not l:
90  #print("Empty assignments for",extra_data)
91  return l
92  if(all == True):
93  return l
94  else:
95  return l[-1]
96 

◆ get_course_contents()

def main.login.get_course_contents (   request_helper,
  course_id 
)

This definition is used to get the content added in the course in weekly.

returns contents of course week wise as uploaded
input : object of class RequestHelper
output: course contents for all weeks

Definition at line 129 of file login.py.

129 def get_course_contents(request_helper,course_id):
130  '''
131  returns contents of course week wise as uploaded
132  input : object of class RequestHelper
133  output: course contents for all weeks
134  '''
135  data = {'courseid': course_id}
136  result = request_helper.post_REST('core_course_get_contents', data)
137  l = []
138  for week in result:
139  data = dict((k, week[k]) for k in week if k in ['id','name','modules'])
140  if data['modules']:
141  files = []
142  for mod in data['modules']:
143  fl = dict((j, mod[j]) for j in mod if j in ['url','name','contents'])
144  if 'contents' in fl.keys():
145  fl['contents'] = prune_dict(fl['contents'],['filename','fileurl'])
146  files.append(fl)
147  data['modules'] = files
148  l.append(data)
149  return l
150 
151 

◆ get_discussion_forum_discussions()

def main.login.get_discussion_forum_discussions (   request_helper,
  forum_id 
)

This definition is used to get latest 5 discussions in the discussion forum.

input : object of class RequestHelper, forum_id
output: returns most recent 5 discussions of the discussion forum

Definition at line 174 of file login.py.

174 def get_discussion_forum_discussions(request_helper,forum_id):
175  '''
176  input : object of class RequestHelper, forum_id
177  output: returns most recent 5 discussions of the discussion forum
178  '''
179  data = {
180  'forumid': forum_id,
181  }
182  result = request_helper.post_REST('mod_forum_get_forum_discussions', data)
183  discussions = result['discussions']
184  if(len(discussions) > 5):
185  discussions = discussions[0:5]
186  discussions = prune_dict(discussions,['name','created','message'])
187  for i in discussions:
188  i['message'] = string_from_html(i['message'])
189  return discussions
190 
191 

◆ get_discussion_forum_id()

def main.login.get_discussion_forum_id (   request_helper,
  course_id 
)

This definition is used to get the Announcements forum.

input : object of class RequestHelper and course id
output: dictionary containing id and name of the Announcements forum

Definition at line 155 of file login.py.

155 def get_discussion_forum_id(request_helper,course_id):
156  '''
157  input : object of class RequestHelper and course id
158  output: dictionary containing id and name of the Announcements forum
159  '''
160  extra_data = {}
161  courseids = {"0":course_id}
162  extra_data.update({'courseids': courseids})
163  result = request_helper.post_REST('mod_forum_get_forums_by_courses', extra_data)
164  forums = {}
165  for forum in result:
166  if(forum['name'] in ['Announcements']):
167  forums = dict((k, forum[k]) for k in forum if k in ['id','name'])
168  return forums
169 
170 

◆ get_discussion_forum_posts()

def main.login.get_discussion_forum_posts ( )
input : object of class RequestHelper

Definition at line 192 of file login.py.

192 def get_discussion_forum_posts():
193  '''
194  input : object of class RequestHelper
195  '''
196  data = {
197  'discussionid': 16545,
198  'sortby': 'modified',
199  'sortdirection': 'ASC',
200  }
201  posts_result = request_helper.post_REST('mod_forum_get_forum_discussion_posts', data)
202  print(posts_result)
203 
204 
205 
206 
207 
208 #function Call to get list of courses taken by a student
209 '''
210 course_ids,course_names = get_list_of_courses(request_helper)
211 print(course_ids,course_names)
212 '''
213 
214 #function Call to get assignments corresponding to a subject
215 '''
216 course_id = 219 #eg for 699
217 assignments = get_assignments_info(request_helper,course_id,False)
218 print(assignments)
219 '''
220 
221 #function Call to get quizzes corresponding to a subject
222 '''
223 course_id = 230 #eg for 771 course
224 quizzes = get_quizzes_info(request_helper,course_id,True)
225 print(quizzes)
226 '''
227 
228 #function Call to get quizzes corresponding to a subject
229 '''
230 course_id = 230 #eg for 771 course
231 quizzes = get_quizzes_info(request_helper,course_id,True)
232 print(quizzes)
233 '''
234 
235 #function Call to get announcement forum id
236 '''
237 ann_forum = get_discussion_forum_id(request_helper,230)
238 print(ann_forum)
239 '''
240 
241 #function Call to get course contents
242 '''
243 cc = get_course_contents(request_helper,230)
244 print(cc[-1])
245 '''
246 
247 #function Call to get 5 most recent posts from a discussion forum
248 '''
249 discs = get_discussion_forum_discussions(request_helper,229)
250 # for d in discs:
251 # print(d)
252 print(type(discs),discs)
253 '''

◆ get_list_of_courses()

def main.login.get_list_of_courses (   request_helper)

This definition is used to get list of courses the user registered.

input : object of class RequestHelper
output : two dictionaries:  1. course_ids with key value pairs as 'SubjectCode' : 'Id used by moodle for it'
                            2. course_names with key value pairs as 'SubjectCode' : 'Name of the Subject'

Definition at line 42 of file login.py.

42 def get_list_of_courses(request_helper):
43  '''
44  input : object of class RequestHelper
45  output : two dictionaries: 1. course_ids with key value pairs as 'SubjectCode' : 'Id used by moodle for it'
46  2. course_names with key value pairs as 'SubjectCode' : 'Name of the Subject'
47  '''
48  result = request_helper.post_REST('core_webservice_get_site_info')
49  if 'userid' not in result:
50  raise RuntimeError('Error could not receive your user ID!')
51  userid = result.get('userid', '')
52  data = {'userid': userid}
53  result = request_helper.post_REST('core_enrol_get_users_courses', data)
54  course_names = {}
55  course_ids = {}
56  for index in result:
57  course_ids[index['shortname'][3:6]] = index['id']
58  course_names[index['shortname'][3:6]] = index['fullname']
59  return course_ids,course_names
60 
61 

◆ get_quizzes_info()

def main.login.get_quizzes_info (   request_helper,
  course_id,
  all = True 
)

This definition is used to get the information about quizzes for the courses.

returns information about all quizes if all is true else about the latest quiz
input : object of class RequestHelper, couse_id, all flag
output : list of all quizzes with following attributes {''id','name','timeopen','timeclose','intro','grade'}

Definition at line 100 of file login.py.

100 def get_quizzes_info(request_helper,course_id,all = True):
101  '''
102  returns information about all quizes if all is true else about the latest quiz
103  input : object of class RequestHelper, couse_id, all flag
104  output : list of all quizzes with following attributes {''id','name','timeopen','timeclose','intro','grade'}
105  '''
106  extra_data = {}
107  courseids = {"0":course_id}
108  extra_data.update({'courseids': courseids})
109  result = request_helper.post_REST('mod_quiz_get_quizzes_by_courses', extra_data)
110  quizzes = result['quizzes']
111  l = []
112  if not quizzes:
113  return l
114  for quiz in quizzes:
115  data = dict((k, quiz[k]) for k in quiz if k in ['id','name','timeopen','timeclose','intro','grade'])
116  #intro processing
117  if data['intro']:
118  data['intro'] = string_from_html(data['intro'])
119  l.append(data)
120  if(all == True):
121  return l
122  else:
123  return l[-1]
124 
125