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
2 Solutions
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
jblumberg
Community Contributor

I just needed to solve this same problem.  This solution is not correct so I want to get the correct answer posted in case anyone runs into the same problem.

If you edit a quiz via the API and the quiz is published, then it will enter a draft state where you need to save the quiz via the user interface.  To avoid this you need to unpublish the quiz before making edits:

1. Unpublish the quiz, note I always set "notify_of_update" as part of all edits as the documentation lists this as a parameter:

quiz:{"published": false, "notify_of_update": false}
 
2. Then make any edits to the quiz or quiz questions
 
3. Republish the quiz (can be done as part of #2)
 
You won't see the "Save Quiz" button doing it this way.

View solution in original post

0 Likes