Saving Quizzes w/ API

Jump to solution
jhveem
Community Explorer

I've written a simple script to add a series of quizzes to the end of each module in a department.

There's some other stuff happening around this to figure out the title, answers, and some other pieces, but the code at the bottom shows my API requests to create the quiz and then to add a question to the quiz. 

The quiz and question is created fine, but if a student takes the quiz, the question does not show up. If an instructor opens the quiz, there is a message saying the instructor must first save the quiz. If they click the save button, the questions then show up for students.

Is there a way to simulate that save action with the API?

url = "/api/v1/courses/" + courseId + "/quizzes";
await $.post(url, {
      quiz: {
        title: modTitle + ': Hours Submission',
        message: `<p><span style="font-size: 18pt;"><strong>Submit Your Hours for This Module</strong></span></p>
<p><span>Report on your progress, and be as accurate as possible. This information is not graded. It is used to help the instructor better meet the needs of the student.</span></p>
<p><span>Please be specific when talking about labs or quizzes that took a lot less or a lot more time than listed.</span></p>`,
        'quiz_type': 'assignment',
        'allowed_attempts': -1,
        'question_count': 3,
        'published': true
      }
    }).done(function (data) {
      quizId = data.id;
    });
 url = "/api/v1/courses/" + courseId + "/quizzes/" + quizId + "/questions";
await $.post(url, {
      question: {
        'question_name': 'Time Spent in Course',
        'question_text': 'Approximately how many hours total have you spent on this class?',
        'question_type': 'multiple_choice_question',
        'points_possible': 0,
        'answers': answers
      }
    });
0 Likes
1 Solution
James
Community Champion

 @jhveem  

I have only used the quiz API to modified an existing quiz by adding questions and then published it from within Canvas. Within the web interface, I have noticed that if the quiz is published and I go back and make substantial questions, I get warning messages and the changes aren't updated unless I click a button to save. That makes me wonder if it's the fact that it's published before I add the questions that causes the problem?

I cannot find an example right now to recreate. That means that the following is based off of knowledge and understanding of the API and how things work in general, but I have not tested it specifically with your case.

What I notice in your code is that you publish the quiz upon creation and then you add questions to it.

Have you tried creating the quiz to get a shell, adding the questions, and then edit the quiz to add the particulars, including that it is published? It may be that just the published=true is needed as part of the edit after the quiz is created, again -- I'm doing this without testing.

The "Save Quiz" button uses an internal (non-API) AJAX call, but that doesn't mean that the functionality isn't there that just updates the setting (not whether or not it is published). In other words, you may be able to just call the edit the quiz endpoint to get it working.

View solution in original post

0 Likes