LMSouq
moodle-core Open

Create quiz using rest api in moodle

MU
mukesh patel
1 month ago
3 views
Problem Description
I am new in moodle and I am trying to create rest api in moodle to add a new quiz in moodle with following code. It adds the entry in quiz table but it's not showing in course UI, what is the reason? Can anyone help what else I need to do for adding quiz using rest api? public static function create_quiz($courseid, $quizname, $timeopen, $timeclose, $attemptallowed, $intro, $questions) { global $DB; $params = self::validate_parameters(self::create_quiz_parameters(), array('courseid' => $courseid, 'quizname' => $quizname, 'timeopen' => $timeopen, 'timeclose' => $timeclose, 'attemptallowed' => $attemptallowed, 'intro' => $intro, 'questions' => $questions)); $q_param = array('courseid' => $params['courseid'], 'quizname' => $params['quizname']); //var_dump("Query parameter".$q_param); $sql = "SELECT q.id FROM {quiz} q WHERE q.course=:courseid AND q.name=:quizname"; $rqa = $DB->get_record_sql($sql, $q_param); if (isset($rqa) && $rqa != null) { $result = array(); $result['quizid'] = 0; $result['status'] = "failure"; $result['message'] = "This quiz name already exists in this course. Please try with different quiz name"; } else { $quiz = new stdClass(); $quiz->course = $courseid; $quiz->name = $quizname; $quiz->timeopen = $timeopen; $quiz->timeclose = $timeclose; $quiz->attempts = $attemptallowed; $quiz->intro = $intro; $quiz->questions = $questions; $rqa = $DB->insert_record('quiz', $quiz); if (isset($rqa)) { $result = array(); $result['quizid'] = $rqa; $result['status'] = "success"; $result['message'] = "Quiz created succesfully"; } else { $result = array(); $result['quizid'] = 0; $result['status'] = "failure"; $result['message'] = "Some error occured please try again"; } } return $result; } /** * Describes the create_category return value * @return external_single_structure * @since Moodle 2.4 */ public static function create_quiz_retur...

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base