Created
July 8, 2024 06:21
-
-
Save developerck/e550db5b1e5067742bab474c331fe7ff to your computer and use it in GitHub Desktop.
Export all question and correct answer in csv for moodle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'config.php'; | |
require_once($CFG->libdir.'/questionlib.php'); | |
require_once($CFG->libdir.'/csvlib.class.php'); | |
require_admin(); | |
raise_memory_limit(MEMORY_HUGE); | |
ini_set('max_execution_time', 600); | |
$from = optional_param("from",0,PARAM_INT); | |
$to = optional_param("to",0,PARAM_INT); | |
$where= [] ; | |
$params = []; | |
if($from){ | |
$where[] = " id >= ? "; | |
$params[] =$from; | |
} | |
if($to){ | |
$where[] = " id <= ? "; | |
$params[] =$to; | |
} | |
$where = implode(" and ", $where); | |
$sql = "select * from {question} where 1 "; | |
if($where){ | |
$sql = $sql. " and ". $where; | |
} | |
if(!$from && !$to){ | |
$sql = $sql. " limit 0, 1000"; | |
} | |
$rs = $DB->get_recordset_sql($sql, $params); | |
$categories = $DB->get_records('question_categories', array(), '', 'id, name, parent'); | |
$data = []; | |
foreach ($rs as $q){ | |
$csv_data = []; | |
$qobj = question_bank::load_question($q->id); | |
$context = context::instance_by_id($qobj->contextid); | |
$course_name = ''; | |
if ($context->contextlevel == CONTEXT_COURSE) { | |
$instanceid = $context->instanceid; | |
$course_name = $DB->get_field('course', 'fullname', array('id' => $instanceid)); | |
} | |
$qparents = question_categorylist_parents($qobj->category); | |
$qparents_name = []; | |
foreach($qparents as $qp){ | |
$qparents_name[$qp ] = $categories[$qp]->name; | |
} | |
$tags = \core_tag_tag::get_item_tags_array('core_question', 'question', $q->id); | |
$csv['sno'] = $q->id; | |
$csv['subject'] = $course_name; | |
$csv['topic'] = implode(" > ", $qparents_name); | |
$csv['tags'] = implode(" , ", $tags); | |
$csv['question_type'] = $q->qtype; | |
$csv['question_name'] = $qobj->name; | |
$csv['question_text'] = $qobj->questiontext; | |
$answers = $qobj->answers; | |
for ($i=1;$i<=10;$i++){ | |
$csv['opt'.$i] = ''; | |
if($q->qtype == 'multichoice' && is_array($answers) && !empty($answers)){ | |
$ans = ''; | |
$ans = array_pop($answers); | |
$csv['opt'.$i] = $ans->answer; | |
} | |
} | |
$right_answer = []; | |
switch($q->qtype){ | |
case 'multichoice': | |
$k= 1; | |
foreach ($qobj->answers as $ans){ | |
if($ans->fraction > 0 ){ | |
$right_answer[] = $k; | |
} | |
$k++; | |
} | |
$csv['right_answer'] = implode(",", $right_answer); | |
break; | |
case 'truefalse': | |
$right_answer = $qobj->get_correct_response(); | |
if(isset($right_answer['answer']) ){ | |
if($right_answer['answer']){ | |
$csv['right_answer'] = 1; | |
}else{ | |
$csv['right_answer'] = 2; | |
} | |
} | |
break; | |
default: | |
$right_answer = $qobj->get_correct_response(); | |
if(is_array($right_answer)){ | |
$csv['right_answer'] = $qobj->summarise_response($right_answer); | |
}else{ | |
$csv['right_answer'] = $right_answer; | |
} | |
} | |
$csv['explanation'] = $qobj->generalfeedback; | |
$csv['correct_marks'] = $qobj->defaultmark; | |
$csv['negative_marks'] = $qobj->penalty; | |
$data[] = $csv; | |
} | |
print_object($data);die; | |
if(!empty($data)){ | |
$filename = date("dmy")."_questions.csv"; | |
output_csv($data, $header, $filename); | |
}else{ | |
echo $OUTPUT->box("No Data found!"); | |
} | |
function output_csv($data, $filename) { | |
global $CFG; | |
// Create CSV file | |
$csv = new csv_export_writer(); | |
// Define headers | |
$headers = array(); | |
foreach ($data[0] as $key => $value) { | |
$headers[] = $key; | |
} | |
$csv->add_data($headers); | |
// Add data rows | |
foreach ($data as $row) { | |
$csv->add_data($row); | |
} | |
// Set headers | |
$csv->download_file($filename); | |
die; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment