0

I am trying to make a webservice in moodle that can be called externally. The webservice makes an entry in the database table mdl_quiz. But the quiz does not show up on the front-end.

Here is my code of the externallib file:

global $CFG, $DB;
$params = self::validate_parameters(
    self::create_quiz_parameters(), 
    ['quiz' => $quiz]
);

foreach ($params['quiz'] as $quiz) {
    $courseid  = $quiz['courseid'];
    $quizname  = $quiz['quizname'];
    $intro     = $quiz['intro'];
    $attempts  = $quiz['attempts'];
    $timeopen  = $quiz['timeopen'];
    $timeclose = $quiz['timeclose'];

    $quiz = new stdClass();
    $quiz->course    = $courseid;
    $quiz->name      = $quizname;
    $quiz->timeopen  = $timeopen;
    $quiz->timeclose = $timeclose;
    $quiz->attempts  = $attempts;
    $quiz->intro     = $intro;
    $rqa = $DB->insert_record('quiz', $quiz);

    if (isset($rqa)) {
        $moduleid = $DB->get_field('modules', 'id', ['name' => 'quiz'], MUST_EXIST);
        $instanceid = 50;
        $sectionid = 1;

        $newcm = new stdClass();
        $newcm->course           = $courseid;
        $newcm->module           = $moduleid;
        $newcm->section          = $sectionid;
        $newcm->added            = time();
        $newcm->instance         = $instanceid;
        $newcm->visible          = 1;
        $newcm->groupmode        = 0;
        $newcm->groupingid       = 0;
        $newcm->groupmembersonly = 0;
        $newcm->showdescription  = 0;
        $cmid = $DB->insert_record('course_modules', $newcm);
    }
}   
thanksd
  • 54,176
  • 22
  • 157
  • 150
Usman Qadeer
  • 157
  • 1
  • 12

1 Answers1

0

You should use add_moduleinfo() instead of inserting records manually. This will do most of the work for you.

For an example see Create Moodle activities programmatically

Community
  • 1
  • 1
Russell England
  • 9,436
  • 1
  • 27
  • 41