function watupro_edit_choice() { global $wpdb; // select choice $choice = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_ANSWERS . " WHERE ID=%d", $_GET['id'])); // select question $question = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_QUESTIONS . " WHERE ID=%d", $choice->question_id)); if (!empty($_POST['ok'])) { $wpdb->query($wpdb->prepare("UPDATE " . WATUPRO_ANSWERS . " SET answer=%s WHERE ID=%d", $_POST['answer'], $choice->ID)); // redirect to questions page watupro_redirect("admin.php?page=watupro_question&question=" . $question->ID . "&action=edit&quiz=" . $question->exam_id); } // select quiz $quiz = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_EXAMS . " WHERE ID=%d", $question->exam_id)); if (watupro_intel() and $quiz->is_personality_quiz) { $exam = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_EXAMS . " WHERE ID=%d", $question->exam_id)); $grades = WTPGrade::get_grades($exam); } if (@file_exists(get_stylesheet_directory() . '/watupro/edit-choice.html.php')) { require get_stylesheet_directory() . '/watupro/edit-choice.html.php'; } else { require WATUPRO_PATH . "/views/edit-choice.html.php"; } }
function watupro_exam() { global $wpdb, $user_ID; $multiuser_access = 'all'; if (watupro_intel()) { $multiuser_access = WatuPROIMultiUser::check_access('exams_access'); } if (isset($_REQUEST['submit'])) { // prepare advanced settings - email grades and contact info fields $advanced_settings = $wpdb->get_var($wpdb->prepare("SELECT advanced_settings FROM " . WATUPRO_EXAMS . "\n\t\t\tWHERE id=%d", @$_REQUEST['quiz'])); if (!empty($advanced_settings)) { $advanced_settings = unserialize(stripslashes($advanced_settings)); } else { $advanced_settings = array(); } // email grades $advanced_settings['email_grades'] = @$_POST['email_grades']; // flag for review $advanced_settings['flag_for_review'] = @$_POST['flag_for_review']; // dont display question numbers $advanced_settings['dont_display_question_numbers'] = @$_POST['dont_display_question_numbers']; // contact fields $advanced_settings['contact_fields'] = array(); $advanced_settings['contact_fields']['email'] = $_POST['ask_for_email']; $advanced_settings['contact_fields']['email_label'] = $_POST['ask_for_email_label']; $advanced_settings['contact_fields']['name'] = $_POST['ask_for_name']; $advanced_settings['contact_fields']['name_label'] = $_POST['ask_for_name_label']; $advanced_settings['contact_fields']['phone'] = $_POST['ask_for_phone']; $advanced_settings['contact_fields']['phone_label'] = $_POST['ask_for_phone_label']; $advanced_settings['contact_fields']['company'] = $_POST['ask_for_company']; $advanced_settings['contact_fields']['company_label'] = $_POST['ask_for_company_label']; $advanced_settings['ask_for_contact_details'] = $_POST['ask_for_contact_details']; $_POST['advanced_settings'] = serialize($advanced_settings); if ($_REQUEST['action'] == 'edit') { //Update goes here $exam_id = $_REQUEST['quiz']; if ($multiuser_access == 'own') { $editor_id = $wpdb->get_var($wpdb->prepare("SELECT editor_id FROM " . WATUPRO_EXAMS . " WHERE ID=%d", $exam_id)); if ($editor_id != $user_ID) { wp_die('You can edit only your own exams', 'watupro'); } } if (empty($_POST['use_different_email_output'])) { $_POST['email_output'] = ''; } WTPExam::edit($_POST, $exam_id); if (!empty($_POST['auto_publish'])) { watupro_auto_publish($exam_id); } $wp_redirect = admin_url('admin.php?page=watupro_exams&message=updated'); // save advanced settings if ($exam_id and watupro_intel()) { $_GET['exam_id'] = $exam_id; $_POST['ok'] = true; watupro_advanced_exam_settings(); } } else { // add new exam $exam_id = WTPExam::add($_POST); if ($exam_id == 0) { $wp_redirect = admin_url('admin.php?page=watupro_exams&message=fail'); } if ($exam_id and !empty($_POST['auto_publish'])) { watupro_auto_publish($exam_id); } $wp_redirect = admin_url('admin.php?page=watupro_questions&message=new_quiz&quiz=' . $exam_id); } echo "<meta http-equiv='refresh' content='0;url={$wp_redirect}' />"; exit; } $action = 'new'; if ($_REQUEST['action'] == 'edit') { $action = 'edit'; } // global answer_display $answer_display = get_option('watupro_show_answers'); // global single page display $single_page = get_option('watupro_single_page'); $dquiz = array(); $grades = array(); if ($action == 'edit') { $dquiz = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_EXAMS . " WHERE ID=%d", $_GET['quiz'])); $single_page = $dquiz->single_page; if ($multiuser_access == 'own' and $dquiz->editor_id != $user_ID) { wp_die('You can edit only your own exams', 'watupro'); } $grades = WTPGrade::get_grades($dquiz); $final_screen = stripslashes($dquiz->final_screen); $schedule_from = $dquiz->schedule_from; list($schedule_from) = explode(" ", $schedule_from); $schedule_to = $dquiz->schedule_to; list($schedule_to) = explode(" ", $schedule_to); $advanced_settings = unserialize(stripslashes($dquiz->advanced_settings)); } else { $final_screen = __("<p>You have completed %%QUIZ_NAME%%.</p>\n\n<p>You scored %%SCORE%% correct out of %%TOTAL%% questions.</p>\n\n<p>You have collected %%POINTS%% points.</p>\n\n<p>Your obtained grade is <b>%%GRADE%%</b></p>\n\n<p>Your answers are shown below:</p>\n\n%%ANSWERS%%", 'watupro'); $schedule_from = date("Y-m-d"); $schedule_to = date("Y-m-d"); } // select certificates if any $certificates = $wpdb->get_results("SELECT * FROM " . WATUPRO_CERTIFICATES . " ORDER BY title"); $cnt_certificates = sizeof($certificates); // categories if any $cats = $wpdb->get_results("SELECT * FROM " . WATUPRO_CATS . " ORDER BY name"); // select other exams $other_exams = $wpdb->get_results("SELECT * FROM " . WATUPRO_EXAMS . " WHERE ID!='" . @$dquiz->ID . "' ORDER BY name"); if (watupro_intel()) { require_once WATUPRO_PATH . "/i/models/dependency.php"; $dependencies = WatuPRODependency::select(@$dquiz->ID); } // check if recaptcha keys are in place $recaptcha_public = get_option('watupro_recaptcha_public'); $recaptcha_private = get_option('watupro_recaptcha_private'); // is this quiz currently published? if (!empty($_GET['quiz'])) { $quiz_id = intval($_GET['quiz']); $is_published = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%[watupro " . $quiz_id . "]%' \n\t\t\t\tAND post_status='publish' AND post_title!=''"); } else { $is_published = false; } wp_enqueue_script('jquery-ui-datepicker'); wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css'); if (@file_exists(get_stylesheet_directory() . '/watupro/exam_form.php')) { require get_stylesheet_directory() . '/watupro/exam_form.php'; } else { require WATUPRO_PATH . "/views/exam_form.php"; } }
$percent = 0; } else { $percent = number_format($score / $total * 100, 2); } $percent = round($percent); // generic rating $rating = $_watu->calculate_rating($total, $score, $percent); // assign grade list($grade, $certificate_id, $do_redirect, $grade_obj) = WTPGrade::calculate($exam_id, $achieved, $percent, 0, $user_grade_ids); // assign certificate if any $certificate = ""; if (!empty($certificate_id) and is_user_logged_in()) { $certificate = WatuPROCertificate::assign($exam, $taking_id, $certificate_id, $user_ID); } // category grades if any $catgrades = WTPGrade::replace_category_grades($exam->final_screen, $taking_id, $exam->ID); // replace some old confusingly named vars $exam->final_screen = str_replace("%%SCORE%%", "%%CORRECT%%", $exam->final_screen); // url to share the final screen and maybe redirect to it? $post_url = empty($post) ? get_permalink($_POST['post_id']) : get_permalink($post->ID); $post_url .= strstr($post_url, "?") ? "&" : "?"; $share_url = $post_url . "waturl=" . base64_encode($exam->ID . "|" . $taking_id); if (!empty($exam->shareable_final_screen) and !empty($exam->redirect_final_screen)) { $do_redirect = $share_url; } // time spent on this quiz $time_spent = ''; if (strstr($exam->final_screen, '%%TIME-SPENT%%') or strstr($exam->email_output, '%%TIME-SPENT%%')) { $taking = $wpdb->get_row($wpdb->prepare("SELECT start_time, end_time FROM " . WATUPRO_TAKEN_EXAMS . " WHERE ID=%d", $taking_id)); $taking->end_time = date('Y-m-d H:i:s'); $time_spent = WTPRecord::time_spent_human(WTPRecord::time_spent($taking));
function watupro_takings() { global $wpdb, $wp_roles, $user_ID; $roles = $wp_roles->roles; $multiuser_access = 'all'; if (watupro_intel()) { $multiuser_access = WatuPROIMultiUser::check_access('exams_access'); } // select user groups $groups = $wpdb->get_results("SELECT * FROM " . WATUPRO_GROUPS . " ORDER BY name"); // shows data for a taken exam $ob = empty($_GET['ob']) ? "id" : $_GET['ob']; $dir = !empty($_GET['dir']) ? $_GET['dir'] : "DESC"; $odir = $dir == 'ASC' ? 'DESC' : 'ASC'; $offset = empty($_GET['offset']) ? 0 : $_GET['offset']; // select exam $exam = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_EXAMS . " WHERE ID=%d", $_GET['exam_id'])); if (!empty($_POST['cleanup']) or !empty($_POST['blankout'])) { if ($multiuser_access == 'own' and $exam->editor_id != $user_ID) { wp_die(__('You can manage only the results on exams created by you.', 'watupro')); } if (!empty($_POST['cleanup'])) { // now cleanup $wpdb->query($wpdb->prepare("DELETE FROM " . WATUPRO_TAKEN_EXAMS . " WHERE exam_id=%d", $exam->ID)); $wpdb->query($wpdb->prepare("DELETE FROM " . WATUPRO_STUDENT_ANSWERS . " WHERE exam_id=%d", $exam->ID)); } if (!empty($_POST['blankout'])) { $wpdb->query($wpdb->prepare("UPDATE " . WATUPRO_TAKEN_EXAMS . " \n\t\t\t\tSET details='data removed', catgrades='data removed' WHERE exam_id=%d", $exam->ID)); $wpdb->query($wpdb->prepare("UPDATE " . WATUPRO_STUDENT_ANSWERS . " \n\t\t\t\tSET question_text='data removed', snapshot='data removed' WHERE exam_id=%d", $exam->ID)); } } // check access if ($multiuser_access == 'own') { if ($exam->editor_id != $user_ID) { wp_die(__('You can only view results on your own quizzes.', 'watupro')); } } // search/filter $filters = array(); $joins = array(); $filter_sql = $left_join_sql = $role_join_sql = $group_join_sql = $left_join = ""; $join_sql = "LEFT JOIN {$wpdb->users} tU ON tU.ID=tT.user_id"; // add filters and joins // display name if (!empty($_GET['dn'])) { switch ($_GET['dnf']) { case 'contains': $like = "%{$_GET['dn']}%"; break; case 'starts': $like = "{$_GET['dn']}%"; break; case 'ends': $like = "%{$_GET['dn']}"; break; case 'equals': default: $like = $_GET['dn']; break; } $joins[] = $wpdb->prepare(" display_name LIKE %s ", $like); } // email if (!empty($_GET['email'])) { switch ($_GET['emailf']) { case 'contains': $like = "%{$_GET['email']}%"; break; case 'starts': $like = "{$_GET['email']}%"; break; case 'ends': $like = "%{$_GET['email']}"; break; case 'equals': default: $like = $_GET['email']; break; } $joins[] = $wpdb->prepare(" user_email LIKE %s ", $like); $filters[] = $wpdb->prepare(" ((user_id=0 AND email LIKE %s) OR (user_id!=0 AND user_email LIKE %s)) ", $like, $like); $left_join = 'LEFT'; // when email is selected, do left join because it might be without logged user } // WP user role - when selected role the join always becomes right join if (!empty($_GET['role'])) { $left_join = ''; $blog_prefix = $wpdb->get_blog_prefix(); $role_join_sql = "JOIN {$wpdb->usermeta} tUM ON tUM.user_id = tU.id \n\t\t\tAND tUM.meta_key = '{$blog_prefix}capabilities' AND tUM.meta_value LIKE '%:" . '"' . $_GET['role'] . '"' . ";%'"; } // Watupro user group if (!empty($_GET['ugroup'])) { $left_join = ''; $group_join_sql = "JOIN {$wpdb->usermeta} tUM2 ON tUM2.user_id = tU.id \n\t\t\tAND tUM2.meta_key = 'watupro_groups' AND tUM2.meta_value LIKE '%:" . '"' . $_GET['ugroup'] . '"' . ";%'"; } else { $group_join_sql = "LEFT JOIN {$wpdb->usermeta} tUM2 ON tUM2.user_id = tU.id \n\t\t\tAND tUM2.meta_key = 'watupro_groups' "; } // IP if (!empty($_GET['ip'])) { switch ($_GET['ipf']) { case 'contains': $like = "%{$_GET['ip']}%"; break; case 'starts': $like = "{$_GET['ip']}%"; break; case 'ends': $like = "%{$_GET['ip']}"; break; case 'equals': default: $like = $_GET['ip']; break; } $filters[] = $wpdb->prepare(" ip LIKE %s ", $like); } // Date if (!empty($_GET['date'])) { switch ($_GET['datef']) { case 'after': $filters[] = $wpdb->prepare(" date>%s ", $_GET['date']); break; case 'before': $filters[] = $wpdb->prepare(" date<%s ", $_GET['date']); break; case 'equals': default: $filters[] = $wpdb->prepare(" date=%s ", $_GET['date']); break; } } // Points if (!empty($_GET['points'])) { switch ($_GET['pointsf']) { case 'less': $filters[] = $wpdb->prepare(" points<%d ", $_GET['points']); break; case 'more': $filters[] = $wpdb->prepare(" points>%d ", $_GET['points']); break; case 'equals': default: $filters[] = $wpdb->prepare(" points=%d ", $_GET['points']); break; } } // % correct if (!empty($_GET['percent_correct'])) { switch ($_GET['percentf']) { case 'less': $filters[] = $wpdb->prepare(" percent_correct < %d ", $_GET['percent_correct']); break; case 'more': $filters[] = $wpdb->prepare(" percent_correct > %d ", $_GET['percent_correct']); break; case 'equals': default: $filters[] = $wpdb->prepare(" percent_correct = %d ", $_GET['percent_correct']); break; } } // passed taking ID from the manage user-certificates page if (!empty($_GET['taking_id'])) { $filters[] = $wpdb->prepare(" tT.ID=%d ", $_GET['taking_id']); } // Grade if (!empty($_GET['grade'])) { $filters[] = $wpdb->prepare(" grade_id=%d ", $_GET['grade']); } // construct filter & join SQLs if (sizeof($filters)) { $filter_sql = " AND " . implode(" AND ", $filters); } if (sizeof($joins)) { $join_sql = " {$left_join} JOIN {$wpdb->users} tU ON tU.ID=tT.user_id AND " . implode(" AND ", $joins); } $limit_sql = "LIMIT {$offset},10"; if (!empty($_GET['export'])) { $limit_sql = ""; } // select takings $in_progress = empty($_GET['in_progress']) ? 0 : 1; // completed or "in progress" takings $q = "SELECT SQL_CALC_FOUND_ROWS tT.*, tU.display_name as display_name, tU.user_email as user_email,\n\ttUM2.meta_value as user_groups\n\tFROM " . WATUPRO_TAKEN_EXAMS . " tT \n\t{$join_sql} {$role_join_sql} {$group_join_sql}\n\tWHERE tT.exam_id={$exam->ID} AND tT.in_progress={$in_progress} {$filter_sql}\n\tORDER BY {$ob} {$dir} {$limit_sql}"; // echo $q; $takings = $wpdb->get_results($q); $count = $wpdb->get_var("SELECT FOUND_ROWS()"); // fill user groups foreach ($takings as $cnt => $taking) { if (empty($taking->user_groups)) { continue; } $ugroups = unserialize($taking->user_groups); $ugroup_names = array(); foreach ($groups as $group) { if (in_array($group->ID, $ugroups)) { $ugroup_names[] = $group->name; } } $takings[$cnt]->user_groups = implode(', ', $ugroup_names); } // end filling user groups info // select number of in_progress takings unless we are showing them now if (!$in_progress) { $num_unfinished = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM " . WATUPRO_TAKEN_EXAMS . "\n\t\t\tWHERE exam_id = %d AND in_progress=1", $exam->ID)); } if (!empty($_GET['export'])) { $_record = new WTPRecord(); $_record->export($takings, $exam); } // grades for the dropdown $grades = WTPGrade::get_grades($exam); // this var will be added to links at the view $filters_url = "dn=" . @$_GET['dn'] . "&dnf=" . @$_GET['dnf'] . "&email=" . @$_GET['email'] . "&emailf=" . @$_GET['emailf'] . "&ip=" . @$_GET['ip'] . "&ipf=" . @$_GET['ipf'] . "&date=" . @$_GET['date'] . "&datef=" . @$_GET['datef'] . "&points=" . @$_GET['points'] . "&pointsf=" . @$_GET['pointsf'] . "&grade=" . @$_GET['grade'] . "&role=" . @$_GET['role'] . "&ugroup=" . @$_GET['ugroup'] . "&percent_correct=" . @$_GET['percent_correct'] . "&percentf=" . @$_GET['percentf']; $display_filters = (!sizeof($filters) and !sizeof($joins) and empty($role_join_sql) and empty($_GET['ugroup'])) ? false : true; wp_enqueue_script('thickbox', null, array('jquery')); wp_enqueue_style('thickbox.css', '/' . WPINC . '/js/thickbox/thickbox.css', null, '1.0'); if (@file_exists(get_stylesheet_directory() . '/watupro/takings.php')) { require get_stylesheet_directory() . '/watupro/takings.php'; } else { require WATUPRO_PATH . "/views/takings.php"; } }