create a function which takes the data out of the database and puts it into a structure/array?? (Tim your help will be appreciated)

create a function which takes the data out of the database and puts it into a structure/array?? (Tim your help will be appreciated)

by Partap S -
Number of replies: 8

Hello Everyone, 

I am trying to create a function which takes the data out of quiz and question database and puts it into a structure/array. 

Any help would be appreacited.

Which file should I look at to get some information/idea.

Thanks

Average of ratings: -
In reply to Partap S

Re: create a function which takes the data out of the database and puts it into a structure/array?? (Tim your help will be appreciated)

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

What have you tried so far?

In reply to Tim Hunt

Re: create a function which takes the data out of the database and puts it into a structure/array?? (Tim your help will be appreciated)

by Partap S -

Trying to create a function. and using below script to get database out and confused in how to stored in a array structure ?

/ load the questions needed by page
    $pagelist = $showall ? quiz_questions_in_quiz($attempt->layout) : quiz_questions_on_page($attempt->layout, $page);
    $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
           "  FROM {$CFG->prefix}question q,".
           "       {$CFG->prefix}quiz_question_instances i".
           " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
           "   AND q.id IN ($pagelist)";
    if (!$questions = get_records_sql($sql)) {
        error('No questions found');
    }

    // Load the question type specific information
    if (!get_question_options($questions)) {
        error('Could not load question options');
    }
In reply to Partap S

Re: create a function which takes the data out of the database and puts it into a structure/array?? (Tim your help will be appreciated)

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The data is already stored ina an array structure. Try adding print_object($questions); at the end of the script.

Also, I assume you have Debugging set to DEVELOPER level, so you will see any error messages.

This post contains some useful general advice about Moodle development: http://moodle.org/mod/forum/discuss.php?d=185942#p809987

In reply to Tim Hunt

Re: create a function which takes the data out of the database and puts it into a structure/array?? (Tim your help will be appreciated)

by Partap S -

Thanks for you reply. Your resposes are always great and helpful. Let me explain in brief what I am trying to do with my one function. So I am creating a function when it will be called it should get all the data out of quiz table and puts it in to array structure. So by calling all the get methods of quiz and question getting all the info about quiz and then returning that info  in a single structure....below is the code I have till now ? Please advice?

 

function quiz_info() {


$pagelist = $showall ? quiz_questions_in_quiz($attempt->layout) : quiz_questions_on_page($attempt->layout, $page);
$query = "SELECT q.*, i.grade AS maxgrade, i.id AS instance" .
" FROM {$CFG->prefix}question q," .
" {$CFG->prefix}quiz_question_instances i" .
" WHERE i.quiz = '$quiz->id' AND q.id = i.question" .
" AND q.id IN ($pagelist)" ;
if (!$questions = get_records_sql($query)) {
error('No questions found');
}

// Load the question type specific information
if (!get_question_options($questions)) {
error("Unable to load questiontype specific question information");
}
// tim is that the right way of getting all the data from quiz and question table
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$states = get_question_states($questions, $quiz, $attempt);
$catcontext = get_context_instance_by_id($category->contextid);
$bestgrade = quiz_get_best_grade($quiz, $USER->id);
$attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'all');
//
how to return here tim ? Please advice ?

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


which files to look at to get more information...I am looking at mod/quiz and mod/question for help ? PLEASE ADVICE?

}
In reply to Partap S

Re: create a function which takes the data out of the database and puts it into a structure/array?? (Tim your help will be appreciated)

by Partap S -

Your help will be greatly appreciated tim.

In reply to Partap S

Re: create a function which takes the data out of the database and puts it into a structure/array?? (Tim your help will be appreciated)

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

It would be a lot easier if we knew what you wanted to do with this data. 

In reply to Tim Hunt

Re: create a function which takes the data out of the database and puts it into a structure/array?? (Tim your help will be appreciated)

by Partap S -

Sure, I am trying to externally display quiz questions, allow students to take the quiz, then store the information back into Moodle as if the quiz was taken within Moodle......

below is the code I have written till now ?

 


require_once("../../config.php");
require_once("locallib.php");

function quiz_info() {
include('lib.php');
include('locallib.php');
include('question.php');
include('restore.php');
include('backuplib.php');
$pagelist = $showall ? quiz_questions_in_quiz($attempt->layout) : quiz_questions_on_page($attempt->layout, $page);
$query = "SELECT q.*, i.grade AS maxgrade, i.id AS instance" .
" FROM {$CFG->prefix}question q," .
" {$CFG->prefix}quiz_question_instances i" .
" WHERE i.quiz = '$quiz->id' AND q.id = i.question" .
" AND q.id IN ($pagelist)";

// load the questions
if (!$questions = get_records_sql($query)) {
error('No questions found');
}

// Load the question type specific information
if (!get_question_options($questions)) {
error("Unable to load questiontype specific question information");
}

// Get the quiz record
$quiz = get_record('quiz', 'id', $quizid);

// Get the course record
$course = get_record('course', 'id', $quiz->course);

// Get the grades for this quiz
$grades = get_records('quiz_grades', 'quiz', $quizid, 'grade, timemodified DESC');

// Get the attempts
$attempts = get_records_select('quiz_attempts', "quiz = '$quizid' AND userid = '$userid'" . $previewclause . $status_condition[$status], 'attempt ASC');

// get last attempt
$lastattempt_obj = get_record_select('quiz_attempts', "quiz = $quiz->id AND attempt = $numattempts AND userid = $USER->id", 'timefinish, timestart');

// old attempt
$oldattempts = get_records_select('quiz_attempts', "quiz = '$quiz->id'
AND userid = '$USER->id' AND preview = 1");

// Get the best current grade for a particular user in a quiz.
$grade = get_field('quiz_grades', 'grade', 'quiz', $quiz->id, 'userid', $userid);


// Return grade for given user or all users.
$usergrade = quiz_get_user_grades($quiz, $userid = 0);

// feedback
$feedbackcache[$quizid] = get_records('quiz_feedback', 'quizid', $quizid);

// graded states
$gradedstates = get_records_sql($gradedstatesql);

//Get users from attempts
$us_attempts = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}quiz_attempts a
WHERE a.quiz = '$quizid' and
u.id = a.userid");

//Get users from question_versions
$us_versions = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}quiz_question_versions v
WHERE v.quiz = '$quizid' and
u.id = v.userid");



$unfinishedattempt = quiz_get_user_attempt_unfinished($quizid, $userid);

$allgrades = quiz_get_all_question_grades($quiz);

$avggrades = quiz_get_average_grade_for_questions($quiz, $userids);

$options = quiz_get_reviewoptions($quiz, $attempt, $context);

$questions = get_records_sql("SELECT q.* FROM {$CFG->prefix}backup_ids bk, {$CFG->prefix}question q " .
"WHERE q.category= $category AND " .
"bk.old_id=q.id AND " .
"bk.backup_code = {$preferences->backup_unique_code} " .
"ORDER BY parent ASC, q.id");

$categories = get_records_sql("SELECT old_id, new_id
FROM {$CFG->prefix}backup_ids
WHERE backup_code = $restore->backup_unique_code AND
table_name = 'question_categories'");

$answers = get_records_sql('SELECT qa.id, qa.answer, qa.feedback, q.qtype
FROM ' . $CFG->prefix . 'question_answers qa,
' . $CFG->prefix . 'question q,
' . $CFG->prefix . 'question_categories qc
WHERE qa.question = q.id
AND q.category = qc.id ' .
'AND qc.contextid IN (' . $coursemodulecontextslist . ')');
}

?>
In reply to Partap S

Re: create a function which takes the data out of the database and puts it into a structure/array?? (Tim your help will be appreciated)

by Partap S -

So for now creating a function which gets all the records of quiz and return it in single structure ?