Пример #1
0
/**
 * This will scan all the content pages that wordpress outputs for our special code. If the code is found, it will replace the requested quiz.
 */
function watupro_shortcode($attr)
{
    global $wpdb, $post;
    $exam_id = $attr[0];
    $contents = '';
    if (!is_numeric($exam_id)) {
        return $contents;
    }
    watupro_vc_scripts();
    ob_start();
    // select exam
    $exam = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_EXAMS . " WHERE id=%d", $exam_id));
    if (watupro_intel()) {
        WatuPROIntelligence::conditional_scripts($exam_id);
    }
    watupro_conditional_scripts($exam);
    // passed question ids?
    if (!empty($attr['question_ids'])) {
        $passed_question_ids = $attr['question_ids'];
    }
    // submitting without ajax?
    if (!empty($_POST['no_ajax']) and !empty($exam->no_ajax)) {
        require WATUPRO_PATH . "/show_exam.php";
        $contents = ob_get_clean();
        $contents = apply_filters('watupro_content', $contents);
        return $contents;
    }
    // other cases, show here
    if (empty($_GET['waturl']) or !$exam->shareable_final_screen) {
        // showing the exam
        if ($exam->mode == 'practice' and watupro_intel()) {
            WatuPracticeController::show($exam);
        } else {
            include WATUPRO_PATH . '/show_exam.php';
        }
        $contents = ob_get_contents();
    } else {
        // showing taking results
        $url = @base64_decode($_GET['waturl']);
        list($exam_id, $tid) = explode("|", $url);
        if (!is_numeric($exam_id) or !is_numeric($tid)) {
            return $contents;
        }
        // must check if public URL is allowed
        $taking = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_TAKEN_EXAMS . " WHERE ID=%d", $tid));
        $contents = WatuPRO::cleanup($taking->details, 'web');
        $post->ID = 0;
        $post->comment_status = 'closed';
    }
    ob_end_clean();
    $contents = apply_filters('watupro_content', $contents);
    return $contents;
}
Пример #2
0
function watupro_activate($update = false)
{
    global $wpdb;
    if (!$update) {
        watupro_init();
    }
    // Initial options.
    add_option('watupro_show_answers', 1);
    add_option('watupro_single_page', 0);
    add_option('watupro_answer_type', 'radio');
    $wpdb->show_errors();
    // exams
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_EXAMS . "'") != WATUPRO_EXAMS) {
        $sql = "CREATE TABLE `" . WATUPRO_EXAMS . "`(\n\t\t\t\t\t\t`ID` int(11) unsigned NOT NULL auto_increment,\n\t\t\t\t\t\t`name` varchar(255) NOT NULL DEFAULT '',\n\t\t\t\t\t\t`description` TEXT NOT NULL,\n\t\t\t\t\t\t`final_screen` TEXT NOT NULL,\n\t\t\t\t\t\t`added_on` datetime NOT NULL,\n\t\t\t         `is_active` TINYINT UNSIGNED NOT NULL DEFAULT '1',\n\t\t\t         `require_login` TINYINT UNSIGNED NOT NULL DEFAULT '0',\n\t\t\t         `take_again` TINYINT UNSIGNED NOT NULL DEFAULT '0', \n\t\t\t         `email_taker` TINYINT UNSIGNED NOT NULL DEFAULT '0', \n\t\t\t         `email_admin` TINYINT UNSIGNED NOT NULL DEFAULT '0', \n\t\t\t         `randomize_questions` TINYINT UNSIGNED DEFAULT '0', \n\t\t\t         `login_mode` VARCHAR(100) NOT NULL DEFAULT 'open',\n\t\t\t         `time_limit` INT UNSIGNED NOT NULL DEFAULT '0',\n\t\t\t\t\t\t`pull_random` INT UNSIGNED NOT NULL DEFAULT '0',\n\t\t\t\t\t\t`show_answers` VARCHAR(10) NOT NULL default '',\n\t\t\t\t\t\t`random_per_category` TINYINT UNSIGNED NOT NULL default 0,\n\t\t\t\t\t\t`group_by_cat` TINYINT UNSIGNED NOT NULL DEFAULT 0,\n\t\t\t\t\t\t`num_answers` INT UNSIGNED NOT NULL DEFAULT 0,\n\t\t\t\t\t\t`single_page` TINYINT UNSIGNED NOT NULL DEFAULT 0,\n\t\t\t\t\t\t`cat_id` INT UNSIGNED NOT NULL DEFAULT 0,\n\t\t\t\t\t\tPRIMARY KEY  (ID)\n\t\t\t\t\t) CHARACTER SET utf8;";
        $wpdb->query($sql);
    }
    // questions
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_QUESTIONS . "'") != WATUPRO_QUESTIONS) {
        $sql = "CREATE TABLE `" . WATUPRO_QUESTIONS . "` (\n\t\t\t\t\t\t\tID int(11) unsigned NOT NULL auto_increment,\n\t\t\t\t\t\t\texam_id int(11) unsigned NOT NULL DEFAULT '0',\n\t\t\t\t\t\t\tquestion mediumtext CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,\n\t\t\t\t\t\t\tanswer_type char(15) COLLATE utf8_unicode_ci NOT NULL default '',\n\t\t\t\t\t\t\tsort_order int(3) NOT NULL default 0,\n\t\t\t\t\t\t\tcat_id INT UNSIGNED NOT NULL DEFAULT 0,\n\t\t\t\t\t\t\trandom_per_category TINYINT UNSIGNED NOT NULL DEFAULT 0,\n\t\t\t\t\t\t\texplain_answer TEXT,\n\t\t\t\t\t\t\tPRIMARY KEY  (ID),\n\t\t\t\t\t\t\tKEY quiz_id (exam_id)\n\t\t\t\t\t\t) CHARACTER SET utf8;";
        $wpdb->query($sql);
    }
    // answers
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_ANSWERS . "'") != WATUPRO_ANSWERS) {
        $sql = "CREATE TABLE `" . WATUPRO_ANSWERS . "` (\n\t\t\t\t\t\tID int(11) unsigned NOT NULL auto_increment,\n\t\t\t\t\t\tquestion_id int(11) unsigned NOT NULL default '0',\n\t\t\t\t\t\tanswer TEXT NOT NULL,\n\t\t\t\t\t\tcorrect enum('0','1') NOT NULL default '0',\n\t\t\t\t\t\tpoint DECIMAL(6,2) DEFAULT '0.00',\n\t\t\t\t\t\tsort_order int(3) NOT NULL default 0,\n\t\t\t\t\t\tPRIMARY KEY  (ID)\n\t\t\t\t\t) CHARACTER SET utf8;";
        $wpdb->query($sql);
    }
    // grades
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_GRADES . "'") != WATUPRO_GRADES) {
        $sql = "CREATE TABLE `" . WATUPRO_GRADES . "` (\n\t\t\t\t `ID` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t `exam_id` int(11) NOT NULL default 0,\n\t\t\t\t `gtitle` varchar (255) NOT NULL default '',\n\t\t\t\t `gdescription` mediumtext COLLATE utf8_unicode_ci NOT NULL,\n\t\t\t\t `gfrom` DECIMAL(10,2) NOT NULL default '0.00',\n\t\t\t\t `gto` DECIMAL(10,2) NOT NULL default '0.00',\n\t\t\t\t `certificate_id` INT UNSIGNED NOT NULL default 0,\n\t\t\t\t PRIMARY KEY (`ID`)\n\t\t\t\t) CHARACTER SET utf8";
        $wpdb->query($sql);
    }
    // taken exams
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_TAKEN_EXAMS . "'") != WATUPRO_TAKEN_EXAMS) {
        $sql = "CREATE TABLE `" . WATUPRO_TAKEN_EXAMS . "` (\n\t\t\t\t  \t`ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t            `user_id` INT UNSIGNED NOT NULL ,\n\t            `exam_id` INT UNSIGNED NOT NULL ,\n\t            `date` DATE NOT NULL ,\n\t            `points` DECIMAL(6,2) NOT NULL DEFAULT '0.00',\n\t            `details` MEDIUMTEXT NOT NULL ,\n\t            `result` TEXT NOT NULL ,\n\t            `start_time` DATETIME NOT NULL DEFAULT '2000-01-01 00:00:00',\n\t\t\t\t  `ip` VARCHAR(20) NOT NULL,\n\t\t\t\t  `in_progress` TINYINT UNSIGNED NOT NULL default 0\n\t\t\t\t) CHARACTER SET utf8";
        $wpdb->query($sql);
    }
    // links to taken_exams
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_STUDENT_ANSWERS . "'") != WATUPRO_STUDENT_ANSWERS) {
        $sql = "CREATE TABLE `" . WATUPRO_STUDENT_ANSWERS . "` (\n\t\t\t\t  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n                  `user_id` INT UNSIGNED NOT NULL default 0, \n                  `exam_id` INT UNSIGNED NOT NULL default 0,\n                  `taking_id` INT UNSIGNED NOT NULL default 0,\n                  `question_id` INT UNSIGNED NOT NULL default 0,\n                  `answer` TEXT NOT NULL,\n\t\t\t\t  `points` DECIMAL(6,2) NOT NULL default '0.00',\n\t\t\t\t  `question_text` TEXT  NOT NULL\n\t\t\t\t) CHARACTER SET utf8";
        $wpdb->query($sql);
    }
    // certificates
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_CERTIFICATES . "'") != WATUPRO_CERTIFICATES) {
        $sql = "CREATE TABLE `" . WATUPRO_CERTIFICATES . "` (\n\t\t\t\t  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t\t\t  `title` VARCHAR(255) NOT NULL default '', \n           `html` TEXT NOT NULL \n\t\t\t\t) CHARACTER SET utf8";
        $wpdb->query($sql);
    }
    // question categories
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_QCATS . "'") != WATUPRO_QCATS) {
        $sql = "CREATE TABLE `" . WATUPRO_QCATS . "` (\n\t\t\t\t  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t\t\t  `name` VARCHAR(255) NOT NULL default ''\n\t\t\t\t) CHARACTER SET utf8";
        $wpdb->query($sql);
    }
    // exam categories
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_CATS . "'") != WATUPRO_CATS) {
        $sql = "CREATE TABLE `" . WATUPRO_CATS . "` (\n\t\t\t\t  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t\t\t  `name` VARCHAR(255) NOT NULL DEFAULT '',\n\t\t\t\t  `ugroups` VARCHAR(255) NOT NULL DEFAULT ''\n\t\t\t\t) CHARACTER SET utf8";
        $wpdb->query($sql);
    }
    // user groups - optionally user can have a group
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_GROUPS . "'") != WATUPRO_GROUPS) {
        $sql = "CREATE TABLE `" . WATUPRO_GROUPS . "` (\n\t\t\t\t  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t\t\t  `name` VARCHAR(255) NOT NULL DEFAULT '',\n\t\t\t\t  `is_def` TINYINT UNSIGNED NOT NULL DEFAULT 0\n\t\t\t\t) CHARACTER SET utf8";
        $wpdb->query($sql);
    }
    // keep track about user's certificates
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_USER_CERTIFICATES . "'") != WATUPRO_USER_CERTIFICATES) {
        $sql = "CREATE TABLE `" . WATUPRO_USER_CERTIFICATES . "` (\n\t\t\t\t\t\t  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t\t\t\t\t  `user_id` INT UNSIGNED NOT NULL default 0,\n\t\t\t\t\t\t  `certificate_id` INT UNSIGNED NOT NULL default 0\n\t\t\t\t\t\t) CHARACTER SET utf8";
        $wpdb->query($sql);
    }
    // files uploaded as user answers
    // keep track about user's certificates
    if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_USER_FILES . "'") != WATUPRO_USER_FILES) {
        $sql = "CREATE TABLE `" . WATUPRO_USER_FILES . "` (\n\t\t\t\t\t\t  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t\t\t\t\t  `user_id` INT UNSIGNED NOT NULL default 0,\n\t\t\t\t\t\t  `taking_id` INT UNSIGNED NOT NULL default 0,\n\t\t\t\t\t\t  `user_answer_id` INT UNSIGNED NOT NULL default 0,\n\t\t\t\t\t\t  `filename` VARCHAR(255) NOT NULL default '',\n\t\t\t\t\t\t  `filesize` INT UNSIGNED NOT NULL default 0 /* size in  KB */,\n\t\t\t\t\t\t  `filetype` VARCHAR(50) NOT NULL default '',\n\t\t\t\t\t\t  `filecontents` LONGBLOB\n\t\t\t\t\t\t) CHARACTER SET utf8";
        $wpdb->query($sql);
    }
    // intelligence tables
    if (watupro_intel()) {
        // exam dependencies
        if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_DEPENDENCIES . "'") != WATUPRO_DEPENDENCIES) {
            $sql = "CREATE TABLE `" . WATUPRO_DEPENDENCIES . "` (\n\t\t\t\t\t  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t  `exam_id` int(10) unsigned NOT NULL default 0,\n\t\t\t\t\t  `depend_exam` int(10) unsigned NOT NULL default 0,\n\t\t\t\t\t  `depend_points` int(11) NOT NULL default 0,\n\t\t\t\t\t  `mode` VARCHAR(100) NOT NULL DEFAULT 'points',\n\t\t\t\t\t  PRIMARY KEY (`ID`)\n\t\t\t\t\t) CHARACTER SET utf8";
            $wpdb->query($sql);
        }
        // exam fee payments
        if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_PAYMENTS . "'") != WATUPRO_PAYMENTS) {
            $sql = "CREATE TABLE `" . WATUPRO_PAYMENTS . "` (\n\t\t\t\t\t  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t  `exam_id` int(10) unsigned NOT NULL default 0,\n\t\t\t\t\t  `user_id` int(10) unsigned NOT NULL default 0,\n\t\t\t\t\t  `date` DATE NOT NULL,\n\t\t\t\t\t  `amount` DECIMAL(8,2) NOT NULL default '0.00',\n\t\t\t\t\t  `status` VARCHAR(100) NOT NULL default '',\n\t\t\t\t\t  `paycode` VARCHAR(100) NOT NULL default '',\n\t\t\t\t\t  PRIMARY KEY (`ID`)\n\t\t\t\t\t) CHARACTER SET utf8";
            $wpdb->query($sql);
            // add also the USD option by default
            update_option("watupro_currency", "USD");
        }
        // bundles of paid quizzes - will be stored in the DB so we can check the price
        // will generate shortcodes for publishing payment buttons
        if ($wpdb->get_var("SHOW TABLES LIKE '" . WATUPRO_BUNDLES . "'") != WATUPRO_BUNDLES) {
            $sql = "CREATE TABLE `" . WATUPRO_BUNDLES . "` (\n\t\t\t\t\t  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t  `price` DECIMAL(8,2),\n\t\t\t\t\t  `bundle_type` VARCHAR(100) NOT NULL DEFAULT 'quizzes', /* quizzes or category */\n\t\t\t\t\t  `quiz_ids` VARCHAR(255) NOT NULL DEFAULT '',\n\t\t\t\t\t  `cat_id` INT UNSIGNED NOT NULL DEFAULT 0,  \n\t\t\t\t\t  PRIMARY KEY (`ID`)\n\t\t\t\t\t) CHARACTER SET utf8";
            $wpdb->query($sql);
        }
        watupro_add_db_fields(array(array("name" => "method", "type" => "VARCHAR(100) NOT NULL DEFAULT 'paypal'"), array("name" => "bundle_id", "type" => "INT UNSIGNED NOT NULL DEFAULT 0")), WATUPRO_PAYMENTS);
        watupro_add_db_fields(array(array("name" => "redirect_url", "type" => "VARCHAR(255) NOT NULL DEFAULT ''")), WATUPRO_BUNDLES);
    }
    # $wpdb->print_error();
    update_option("watupro_delete_db", '');
    // add student role if not exists
    // most probably this is no longer required
    $res = add_role('student', 'Student', array('read' => true, 'watupro_exams' => true));
    // database upgrades - version 1.1
    $db_version = get_option("watupro_db_version");
    watupro_add_db_fields(array(array("name" => "end_time", "type" => "DATETIME NOT NULL DEFAULT '2000-01-01 00:00:00'"), array("name" => "grade_id", "type" => "INT UNSIGNED NOT NULL default 0"), array("name" => "percent_correct", "type" => "TINYINT UNSIGNED NOT NULL default 0"), array("name" => "email", "type" => "VARCHAR(255) NOT NULL DEFAULT ''"), array("name" => "catgrades", "type" => "TEXT"), array("name" => "serialized_questions", "type" => "TEXT"), array("name" => "num_hints_used", "type" => "SMALLINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "name", "type" => "VARCHAR(100) NOT NULL DEFAULT ''"), array("name" => "contact_data", "type" => "TEXT"), array("name" => "marked_for_review", "type" => "TEXT")), WATUPRO_TAKEN_EXAMS);
    watupro_add_db_fields(array(array("name" => "times_to_take", "type" => "SMALLINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "mode", "type" => "VARCHAR(100) DEFAULT 'live'"), array("name" => "fee", "type" => "DECIMAL(8,2) NOT NULL DEFAULT '0.00'"), array("name" => "require_captcha", "type" => "TINYINT NOT NULL DEFAULT '0'"), array("name" => "grades_by_percent", "type" => "TINYINT NOT NULL DEFAULT '0'"), array("name" => "admin_email", "type" => "VARCHAR(255) NOT NULL default ''"), array("name" => "disallow_previous_button", "type" => "TINYINT UNSIGNED NOT NULL default 0"), array("name" => "email_output", "type" => "TEXT"), array("name" => "live_result", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "gradecat_design", "type" => "TEXT"), array("name" => "is_scheduled", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "schedule_from", "type" => "DATETIME"), array("name" => "schedule_to", "type" => "DATETIME"), array("name" => "submit_always_visible", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "retake_grades", "type" => "TEXT"), array("name" => "show_pagination", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "advanced_settings", "type" => "TEXT"), array("name" => "enable_save_button", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "shareable_final_screen", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "redirect_final_screen", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "editor_id", "type" => "INT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "question_hints", "type" => "VARCHAR(10) NOT NULL DEFAULT ''"), array("name" => "takings_by_ip", "type" => "INT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "reuse_default_grades", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "store_progress", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "custom_per_page", "type" => "SMALLINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "randomize_cats", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "no_ajax", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "email_subject", "type" => "VARCHAR(255) NOT NULL default ''"), array("name" => "pay_always", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "reuse_questions_from", "type" => "VARCHAR(255) NOT NULL DEFAULT ''"), array("name" => "published_odd", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "published_odd_url", "type" => "VARCHAR(255) NOT NULL default ''")), WATUPRO_EXAMS);
    watupro_add_db_fields(array(array("name" => "is_required", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "correct_condition", "type" => "VARCHAR(255) NOT NULL DEFAULT ''"), array("name" => "max_selections", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "is_inactive", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "is_survey", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "elaborate_explanation", "type" => "VARCHAR(100) NOT NULL DEFAULT ''"), array("name" => "open_end_mode", "type" => "VARCHAR(255) NOT NULL DEFAULT ''"), array("name" => "tags", "type" => "VARCHAR(255) NOT NULL DEFAULT ''"), array("name" => "open_end_display", "type" => "VARCHAR(255) NOT NULL DEFAULT 'medium'"), array("name" => "exclude_on_final_screen", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "hints", "type" => "TEXT"), array("name" => "compact_format", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "round_points", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "importance", "type" => "TINYINT UNSIGNED DEFAULT 0"), array("name" => "calculate_whole", "type" => "TINYINT UNSIGNED DEFAULT 0"), array("name" => "unanswered_penalty", "type" => "DECIMAL(8,2) NOT NULL DEFAULT '0.00'"), array("name" => "truefalse", "type" => "TINYINT NOT NULL DEFAULT 0"), array("name" => "accept_feedback", "type" => "TINYINT NOT NULL DEFAULT 0"), array("name" => "feedback_label", "type" => "VARCHAR(100) NOT NULL DEFAULT ''"), array("name" => "reward_only_correct", "type" => "TINYINT NOT NULL DEFAULT 0")), WATUPRO_QUESTIONS);
    watupro_add_db_fields(array(array("name" => "require_approval", "type" => "TINYINT UNSIGNED NOT NULL default 0"), array("name" => "require_approval_notify_admin", "type" => "TINYINT UNSIGNED NOT NULL default 0"), array("name" => "approval_notify_user", "type" => "TINYINT UNSIGNED NOT NULL default 0"), array("name" => "approval_email_subject", "type" => "VARCHAR(255) NOT NULL default ''"), array("name" => "approval_email_message", "type" => "TEXT"), array("name" => "editor_id", "type" => "INT UNSIGNED NOT NULL DEFAULT 0")), WATUPRO_CERTIFICATES);
    watupro_add_db_fields(array(array("name" => "exam_id", "type" => "INT UNSIGNED NOT NULL default 0"), array("name" => "taking_id", "type" => "INT UNSIGNED NOT NULL default 0"), array("name" => "pending_approval", "type" => "TINYINT UNSIGNED NOT NULL default 0"), array("name" => "pdf_output", "type" => "LONGBLOB"), array("name" => "public_access", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0")), WATUPRO_USER_CERTIFICATES);
    watupro_add_db_fields(array(array("name" => "description", "type" => "TEXT NOT NULL"), array("name" => "editor_id", "type" => "INT UNSIGNED NOT NULL DEFAULT 0")), WATUPRO_QCATS);
    watupro_add_db_fields(array(array("name" => "editor_id", "type" => "INT UNSIGNED NOT NULL DEFAULT 0")), WATUPRO_CATS);
    watupro_add_db_fields(array(array("name" => "cat_id", "type" => "INT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "editor_id", "type" => "INT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "percentage_based", "type" => "INT UNSIGNED NOT NULL DEFAULT 0")), WATUPRO_GRADES);
    watupro_add_db_fields(array(array("name" => "is_correct", "type" => "TINYINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "snapshot", "type" => "TEXT NOT NULL"), array("name" => "hints_used", "type" => "TEXT"), array("name" => "num_hints_used", "type" => "SMALLINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "onpage_question_num", "type" => "SMALLINT UNSIGNED NOT NULL DEFAULT 0"), array("name" => "timestamp", "type" => "TIMESTAMP"), array("name" => "feedback", "type" => "TEXT")), WATUPRO_STUDENT_ANSWERS);
    watupro_add_db_fields(array(array('name' => 'explanation', 'type' => 'TEXT'), array("name" => "grade_id", "type" => "VARCHAR(255) NOT NULL DEFAULT '0' COMMENT 'Used only in personality quizzes' ")), WATUPRO_ANSWERS);
    // db updates 3.0
    if (empty($db_version) or $db_version < 3) {
        $sql = "ALTER TABLE " . WATUPRO_ANSWERS . " CHANGE `point` `point` DECIMAL(6,2) DEFAULT '0.00'";
        $wpdb->query($sql);
    }
    // db updates 3.4
    if (empty($db_version) or $db_version < 3.41) {
        $sql = "ALTER TABLE " . WATUPRO_EXAMS . " CHANGE `name` `name` VARCHAR(255) DEFAULT ''";
        $wpdb->query($sql);
    }
    // Intelligence specific fields
    if (watupro_intel()) {
        require_once WATUPRO_PATH . "/i/models/i.php";
        WatuPROIntelligence::activate();
    }
    // add indexes
    $index_taken_exams = $wpdb->get_var("SHOW INDEX FROM " . WATUPRO_TAKEN_EXAMS . " WHERE KEY_NAME = 'user_id'");
    if (empty($index_taken_exams)) {
        $wpdb->query("ALTER TABLE " . WATUPRO_TAKEN_EXAMS . " ADD INDEX user_id (user_id),\n\t\t\t\t\tADD INDEX exam_id (exam_id), ADD INDEX points (points), ADD INDEX ip (ip),\n\t\t\t\t\tADD INDEX grade_id (grade_id), ADD INDEX percent_correct (percent_correct),\n\t\t\t\t\tADD INDEX date (date)");
    }
    $index_student_answers = $wpdb->get_var("SHOW INDEX FROM " . WATUPRO_STUDENT_ANSWERS . " WHERE KEY_NAME = 'user_id'");
    if (empty($index_student_answers)) {
        $wpdb->query("ALTER TABLE " . WATUPRO_STUDENT_ANSWERS . " ADD INDEX user_id (user_id),\n\t\t\t\t\tADD INDEX exam_id (exam_id), ADD INDEX taking_id (taking_id), ADD INDEX question_id (question_id)");
    }
    // change pdf_output field
    if ($db_version < 4) {
        $wpdb->query("ALTER TABLE " . WATUPRO_USER_CERTIFICATES . " CHANGE pdf_output pdf_output LONGBLOB");
    }
    if ($db_version < 4.121) {
        $wpdb->query("ALTER TABLE " . WATUPRO_TAKEN_EXAMS . " CHANGE details details MEDIUMTEXT");
    }
    if ($db_version < 4.13) {
        $wpdb->query("ALTER TABLE " . WATUPRO_GRADES . " CHANGE gfrom gfrom DECIMAL(8,2) NOT NULL DEFAULT '0.00', \n\t\t\t\t\tCHANGE gto gto DECIMAL(8,2) NOT NULL DEFAULT '0.00'");
    }
    // once update all old quizzes with "store_progress" => true
    if ($db_version < 4.15) {
        $wpdb->query("UPDATE " . WATUPRO_EXAMS . " SET store_progress=1");
    }
    // multiple personality grades
    // once update all old quizzes with "store_progress" => true
    if ($db_version < 4.3) {
        $wpdb->query("ALTER TABLE " . WATUPRO_ANSWERS . " CHANGE grade_id grade_id VARCHAR(255) NOT NULL DEFAULT '0'");
    }
    // set current DB version
    update_option("watupro_db_version", 4.53);
}