uwezi

Create Calculated Question in Python

May 11th, 2019
197
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from canvasapi import Canvas
  2. import itertools
  3. import random
  4.  
  5. API_URL = "https://canvas.instructure.com"
  6. API_KEY = <your api key here>
  7.  
  8. canvas = Canvas(API_URL, API_KEY)
  9.  
  10. # create a calculated_question
  11. # example of a potential divider
  12. #
  13. #  U2 = U0 * R2 / ( R1 + R2 )
  14. #
  15.  
  16. E3  = [1, 2, 5]
  17. E6  = [1.0, 1.5, 2.2, 3.3, 4.7, 6.8]
  18. E12 = [1.0, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6, 6.8, 8.2]
  19.  
  20. coursename = 'test'
  21. quizname   = 'test'
  22.  
  23. # define the input variable names
  24. #   each variable has its own range, format and scale
  25. #  
  26. variables = \
  27.     [
  28.       {
  29.         'name':   'U0',
  30.         'unit':   'V',
  31.         'format': '{:.1f}',
  32.         'scale':  '1',
  33.         'range':  [1.2, 1.5, 4.5, 9, 12, 24, 48, 110, 220]
  34.       },
  35.       {
  36.         'name':   'R1',
  37.         'unit':   'ohm',
  38.         'format': '{:.1f}',
  39.         'scale':  '1',
  40.         'range':  [ i*j for i, j in itertools.product([10, 100, 1000], E12)]
  41.       },
  42.       {
  43.         'name':   'R2',
  44.         'unit':   'ohm',
  45.         'format': '{:.1f}',
  46.         'scale':  '1',
  47.         'range':  [ i*j for i, j in itertools.product([10, 100, 1000], E12)]
  48.       },
  49.     ]
  50.  
  51. # how many sets of answers
  52. rows = 30
  53.  
  54. # create an empty list of lists (array) for the values
  55. values = [ [ i for i in range(len(variables))] for _ in range(rows)]
  56.  
  57. # create an empty list for the calculated results
  58. results = [i for i in range(rows)]
  59.  
  60. # fill the array of input values with random choices from the given ranges
  61. for i in range(rows):
  62.     for j in range(len(variables)):
  63.         values[i][j] = random.choice(variables[j].get('range'))
  64.  
  65.     # and calculate the result value    
  66.     results[i] = values[i][0] * values[i][2] / (values[i][1]+values[i][2])
  67.  
  68. # format the text field for the question
  69. #   an HTML table is created which presents the variables and their values
  70. question_text = '<p><table border="1"><tr><th></th><th>value</th><th>unit</th></tr>';
  71. for j in range(len(variables)):
  72.     question_text += '<tr>'
  73.     question_text += '<td style="text-align:center;">' + variables[j].get('name') + '</td>'
  74.     question_text += '<td style="text-align:right;">[' + variables[j].get('name') + ']</td>'
  75.     question_text += '<td style="text-align:center;">' + variables[j].get('unit') + '</td>'
  76.     question_text += '</tr>'
  77. question_text += '</table></p>'
  78.  
  79. # format the central block of values and results
  80. answers = []
  81. for i in range(rows):
  82.     answers.append(\
  83.         {
  84.           'weight': '100',
  85.           'variables':
  86.           [
  87.             {
  88.               'name': variables[j].get('name'),
  89.               'value': variables[j].get('format').format(values[i][j])
  90.             } for j in range(len(variables))
  91.           ],
  92.           'answer_text': '{:.5g}'.format(results[i])
  93.         })
  94.  
  95. # format the block of variables,
  96. #   'min' and 'max' do not matter since the values are created inside the script
  97. #   'scale' determines the decimal places during output  
  98. variables_block = []
  99. for j in range(len(variables)):
  100.     variables_block.append(\
  101.         {
  102.           'name':  variables[j].get('name'),
  103.           'min':   '1.0',
  104.           'max':   '10.0',
  105.           'scale': variables[j].get('scale')
  106.         })
  107.  
  108. # put together the structure of the question
  109. new_question = \
  110.     {
  111.       'question_name':           'Question 6',
  112.       'question_type':           'calculated_question',
  113.       'question_text':           question_text,
  114.       'points_possible':         '1.0',
  115.       'correct_comments':        '',
  116.       'incorrect_comments':      '',
  117.       'neutral_comments':        '',
  118.       'correct_comments_html':   '',
  119.       'incorrect_comments_html': '',
  120.       'neutral_comments_html':   '',
  121.       'answers':                 answers,
  122.       'variables':               variables_block,
  123.       'formulas':                ['automated by python'],
  124.       'answer_tolerance':        '5%',
  125.       'formula_decimal_places':  '1',
  126.       'matches':                 None,
  127.       'matching_answer_incorrect_matches': None,
  128.     }
  129.                                  
  130.  
  131. courses  = canvas.get_courses()
  132. for course in courses:
  133.     if course.name.lower() == coursename.lower():
  134.         print('found course')
  135.         quizzes = course.get_quizzes()
  136.         for quiz in quizzes:
  137.             if quiz.title.lower() == quizname.lower():
  138.                 print('found quiz')
  139.  
  140.                 question = quiz.create_question(question = new_question)
RAW Paste Data