<?php require 'database.php'; if (isset($_POST['fn'])) { if ($_POST['fn'] == 'register_user') { echo register_user($_POST['username'], $_POST['email'], $_POST['full_name'], $_POST['password']); } else { if ($_POST['fn'] == 'login') { echo login($_POST['username'], $_POST['password']); } else { if ($_POST['fn'] == 'loadQuestions') { header('Content-Type: application/json'); echo json_encode(load_questions()); } } } } function register_user($username, $email, $full_name, $password) { $db = connect_database(); $results = $db->query("SELECT * FROM User WHERE username='******'"); if ($results->num_rows != 0) { error_log("{$username} already exists"); return -1; } $results->close(); $results = $db->query("SELECT * FROM User WHERE email='{$email}'"); if ($results->num_rows != 0) { error_log("{$email} already exists"); return -2; }
function updateQuestionTypes() { // Add/replace standard question types by deleting all questions in the // category CR_PROTOTYPES and reloading them from the file // questions-CR_PROTOTYPES.xml. // Find id of CR_PROTOTYPES category global $DB, $CFG; $success = true; $systemcontext = context_system::instance(); $systemcontextid = $systemcontext->id; if (!$systemcontextid) { $systemcontextid = 1; // HACK ALERT: occurs when phpunit initialising itself } $category = $DB->get_record('question_categories', array('contextid' => $systemcontextid, 'name' => 'CR_PROTOTYPES')); if ($category) { $prototypecategoryid = $category->id; } else { // CR_PROTOTYPES category not defined yet. Add it $category = array('name' => 'CR_PROTOTYPES', 'contextid' => $systemcontextid, 'info' => 'Category for CodeRunner question built-in prototypes. FOR SYSTEM USE ONLY.', 'infoformat' => 0, 'parent' => 0); $prototypecategoryid = $DB->insert_record('question_categories', $category); if (!$prototypecategoryid) { throw new coding_exception("Upgrade failed: couldn't create CR_PROTOTYPES category"); } $category = $DB->get_record('question_categories', array('id' => $prototypecategoryid)); } // Delete all existing prototypes $prototypes = $DB->get_records_select('question', "category = {$prototypecategoryid} and name like '%PROTOTYPE_%'"); foreach ($prototypes as $question) { $DB->delete_records('question_coderunner_options', array('questionid' => $question->id)); $DB->delete_records('question', array('id' => $question->id)); } $dbfiles = scandir(__DIR__); foreach ($dbfiles as $file) { // Load any files in the db directory ending with _PROTOTYPES.xml if (strpos(strrev($file), strrev('_PROTOTYPES.xml')) === 0) { $filename = __DIR__ . '/' . $file; load_questions($category, $filename, $systemcontextid); } } return $success; }