Question: how to create automatically (dinamically) quizzes

Question: how to create automatically (dinamically) quizzes

by Melvyn Morales Morejón -
Number of replies: 0

Hi Im trying to create automatically (dinamically) quizzes from a php script, what we want is that the student in a course query de question bank and can choose the questions in order to create a new quiz. I checked that the quiz record is created and a record in the course_module table, when I try to view the quiz It prints “invalid course_module ID”.

Here´s the script:

function create_quiz_from_course_topic($course_obj, $course_cat_id, $topic_cat_id)
{
    global $DB;
    
    $course_cat_obj = $DB->get_record('question_categories', array('id' => $course_cat_id));
    $topic_cat_obj = $DB->get_record('question_categories', array('id' => $topic_cat_id));
    $questions_obj = $DB->get_records('question', array('category' => $topic_cat_obj->id));
    
    $tmp_questions = '';
    $sumgrades = 0.0;
    $qArray = array();
    
    foreach($questions_obj as $qKey => $qVal)
    {
        $tmp_questions .= $qVal->id.',';
        $sumgrades += $qVal->defaultgrade;
        $qArray[] = $qVal->id;
    }
    
    //eliminar la ultima coma y poner un cero para marcar el final de la pagina
    $tmp_questions.= '0';
    
    //crear el questionario
    $quiz_obj = new stdClass;
    $quiz_obj->course = $course_obj->id;
    $quiz_obj->name = $course_cat_obj->name.' '.$topic_cat_obj->name;
    $quiz_obj->intro = ' ';
    $quiz_obj->introformat = 1;
    $quiz_obj->questions = $tmp_questions;
    $quiz_obj->sumgrades = $sumgrades;
    $quiz_obj->timemodified = time();
    $quiz_obj->optionflags = 1;
    $quiz_obj->questionsperpage = 10;
    $quiz_obj->penaltyscheme = 1;
    
    $DB->insert_record('quiz', $quiz_obj);
    $quiz_obj = $DB->get_record('quiz', array('name' =>$course_cat_obj->name.' '.$topic_cat_obj->name));
    
    //crear las instancias de las preguntas
    foreach($qArray as $qKey => $qVal)
    {
        $qInst = new stdClass;
            
        $qInst->quiz = $quiz_obj->id;
        $qInst->question = $qVal;
        $qInst->grade = 1.0;
        
        $DB->insert_record('quiz_question_instances', $qInst);
    }
    
    //registrarlo en el curso
    $cm = new stdClass;
    $cm->course = $course_obj->id;
    $cm->module = get_quiz_module()->id;
    $cm->instance = $quiz_obj->id;
    $cm->added = time();
    $cm->visible = 1;
    $cm->visibleold = 1;
    $cm->idnumber = ' ';
    
    $DB->insert_record('course_modules', $cm);
    $cm = $DB->get_record('course_modules', array('module' => get_quiz_module()->id, 'instance' => $quiz_obj->id));
    
    //añadirlo a la tabla inpstest
    $insptests_obj = new stdClass;
    $insptests_obj->course_cat_id = $course_cat_obj->id;
    $insptests_obj->topic_cat_id = $topic_cat_obj->id;
    $insptests_obj->quiz_id = $quiz_obj->id;
        
    $DB->insert_record('insptests', $insptests_obj);
    
    return quiz_obj;
}

Average of ratings: -