Пример #1
0
 static function print_meta_box($post)
 {
     global $wpdb;
     // select lessons in this course
     $_lesson = new NamasteLMSLessonModel();
     $lessons = $_lesson->select($post->ID);
     // required lessons
     $required_lessons = get_post_meta($post->ID, 'namaste_required_lessons', true);
     if (!is_array($required_lessons)) {
         $required_lessons = array();
     }
     // enrollment - for now free or admin approved, in the future also paid
     $enroll_mode = get_post_meta($post->ID, 'namaste_enroll_mode', true);
     $fee = get_post_meta($post->ID, 'namaste_fee', true);
     $currency = get_option('namaste_currency');
     $use_points_system = get_option('namaste_use_points_system');
     //get award points for compliting the course
     $award_points = get_post_meta($post->ID, 'namaste_award_points', true);
     if ($award_points === '') {
         $award_points = get_option('namaste_points_course');
     }
     do_action('namaste_single_course_points_print', $post);
     // other courses
     $other_courses = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->posts} tP\t\t\t\n\t\t\tWHERE post_type = 'namaste_course'  AND (post_status='publish' OR post_status='draft') \n\t\t\tAND ID!=%d ORDER BY post_title", $post->ID));
     // course will be accessible after these course(s) are completed
     $course_access = get_post_meta($post->ID, 'namaste_access', true);
     if (!is_array($course_access)) {
         $course_access = array();
     }
     $unenroll_allowed = get_post_meta($post->ID, 'namaste_unenroll', true);
     wp_nonce_field(plugin_basename(__FILE__), 'namaste_noncemeta');
     if (@file_exists(get_stylesheet_directory() . '/namaste/course-meta-box.php')) {
         require get_stylesheet_directory() . '/namaste/course-meta-box.php';
     } else {
         require NAMASTE_PATH . "/views/course-meta-box.php";
     }
 }
Пример #2
0
 static function cleanup($course_id, $student_id)
 {
     global $wpdb;
     $wpdb->query($wpdb->prepare("DELETE FROM " . NAMASTE_STUDENT_COURSES . " \n\t\t \tWHERE course_id = %d AND user_id=%d", $course_id, $student_id));
     $wpdb->query($wpdb->prepare("DELETE FROM " . NAMASTE_STUDENT_LESSONS . " WHERE student_id= %d AND lesson_id IN (SELECT ID FROM {$wpdb->posts} tP JOIN {$wpdb->postmeta} tM ON tM.post_id = tP.ID AND tM.meta_key = 'namaste_course' AND tM.meta_value = %d WHERE post_type = 'namaste_lesson');", $student_id, $course_id));
     // delete solutions
     $wpdb->query($wpdb->prepare("DELETE FROM " . NAMASTE_STUDENT_HOMEWORKS . " WHERE student_id=%d\n\t\t\tAND homework_id IN (SELECT id FROM " . NAMASTE_HOMEWORKS . " WHERE course_id=%d)", $student_id, $course_id));
     // cleanup exams data?
     $use_exams = get_option('namaste_use_exams');
     if ($use_exams and get_option('namaste_cleanup_exams') == 'yes') {
         // select all exam IDs of related lessons
         $_lesson = new NamasteLMSLessonModel();
         $lessons = $_lesson->select($course_id);
         $exam_ids = array(0);
         foreach ($lessons as $lesson) {
             $exam_id = get_post_meta($lesson->ID, 'namaste_required_exam', true);
             if (!empty($exam_id)) {
                 $exam_ids[] = $exam_id;
             }
         }
         // now delete
         if ($use_exams == 'watu') {
             $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}watu_takings WHERE user_id=%d AND exam_id IN (" . implode(',', $exam_ids) . ")", $student_id));
         }
         if ($use_exams == 'watupro') {
             $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}watupro_taken_exams WHERE user_id=%d AND exam_id IN (" . implode(',', $exam_ids) . ")", $student_id));
             $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}watupro_student_answers WHERE user_id=%d AND exam_id IN (" . implode(',', $exam_ids) . ")", $student_id));
         }
     }
 }
Пример #3
0
 static function student_lessons($simplified = false, $ob = null, $dir = null, $in_shortcode = false)
 {
     global $wpdb, $user_ID;
     // student_id
     $student_id = (empty($_GET['student_id']) or !current_user_can('namaste_manage')) ? $user_ID : $_GET['student_id'];
     // select this student
     $student = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->users} WHERE ID=%d", $student_id));
     // select this course
     $course = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE id=%d", $_GET['course_id']));
     // am I enrolled?
     if (!current_user_can('namaste_manage') and !$in_shortcode) {
         $enrolled = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . NAMASTE_STUDENT_COURSES . " WHERE user_id = %d AND course_id = %d AND (status = 'enrolled' OR status = 'completed')", $student_id, $course->ID));
         if (!$enrolled) {
             _e("You must enroll in the course first before you can see the lessons", 'namaste');
             return false;
         }
     }
     // end enrolled check
     // change student-lesson status?
     if (!empty($_POST['change_status'])) {
         $multiuser_access = 'all';
         $multiuser_access = NamasteLMSMultiUser::check_access('students_access');
         if ($multiuser_access == 'view') {
             wp_die(__('You are not allowed to do this.', 'namaste'));
         }
         $result = NamasteLMSStudentModel::lesson_status($student->ID, $_POST['lesson_id'], $_POST['status']);
         if (!$result) {
             $error = __('The lesson cannot be completed because there are unsatisfied requirements', 'namaste');
         }
     }
     // select lessons
     $_lesson = new NamasteLMSLessonModel();
     $select_ob = empty($ob) ? 'post_title' : $ob;
     $lessons = $_lesson->select($course->ID, 'array', null, $ob, $dir);
     $ids = array(0);
     foreach ($lessons as $lesson) {
         $ids[] = $lesson->ID;
     }
     $id_sql = implode(",", $ids);
     // select homeworks and match to lessons
     $homeworks = NamasteLMSHomeworkModel::select("WHERE lesson_id IN ({$id_sql})");
     // using exams? select them too
     $use_exams = get_option('namaste_use_exams');
     $exams_table = $use_exams == 'watu' ? $wpdb->prefix . 'watu_master' : $wpdb->prefix . 'watupro_master';
     $shortcode = $use_exams == 'watu' ? 'WATU' : 'WATUPRO';
     // select student-lesson relation so we can match status
     $student_lessons = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . NAMASTE_STUDENT_LESSONS . "\r\n\t\t\tWHERE student_id = %d", $student_id));
     foreach ($lessons as $cnt => $lesson) {
         $lesson_homeworks = array();
         foreach ($homeworks as $homework) {
             if ($homework->lesson_id == $lesson->ID) {
                 $lesson_homeworks[] = $homework;
             }
         }
         $lessons[$cnt]->homeworks = $lesson_homeworks;
         if ($use_exams) {
             $required_exam = get_post_meta($lesson->ID, 'namaste_required_exam', true);
             if ($required_exam) {
                 $exam = $wpdb->get_row("SELECT tE.*, tP.id as post_id FROM {$exams_table} tE, {$wpdb->posts} tP\r\n\t\t\t\t\t\tWHERE tE.ID = {$required_exam} AND tP.post_content LIKE CONCAT('%[{$shortcode} ', tE.ID, ']%')\r\n\t\t\t\t\t\tAND tP.post_status='publish' AND post_title!=''");
                 $lessons[$cnt]->exam = $exam;
             }
         }
         // status
         $status = null;
         foreach ($student_lessons as $l) {
             if ($l->lesson_id == $lesson->ID) {
                 $status = $l;
             }
         }
         if (empty($status->id)) {
             $lessons[$cnt]->status = __('Not started', 'namaste');
             $lessons[$cnt]->statuscode = -1;
         } else {
             if ($status->status == 1) {
                 $lessons[$cnt]->status = __('Completed on', 'namaste') . ' ' . date(get_option('date_format'), strtotime($status->completion_date));
                 $lessons[$cnt]->statuscode = 1;
             } else {
                 // in progress
                 $lessons[$cnt]->status = "<a href='#' onclick='namasteInProgress(" . $lesson->ID . ", " . $student_id . ");return false;'>" . __('In progress', 'namaste') . "</a>";
                 $lessons[$cnt]->statuscode = 0;
             }
         }
         // end defining status
     }
     // external reorder?
     if (empty($ob)) {
         $lessons = apply_filters('namaste-reorder-lessons', $lessons);
     }
     // enqueue thickbox
     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() . '/namaste/student-lessons.php')) {
         require get_stylesheet_directory() . '/namaste/student-lessons.php';
     } else {
         require NAMASTE_PATH . "/views/student-lessons.php";
     }
 }
Пример #4
0
 static function lessons($atts)
 {
     global $post;
     $status = @$atts[0];
     $course_id = empty($atts[1]) ? $post->ID : $atts[1];
     $ob = empty($atts[2]) ? '' : "tP." . $atts[2];
     $dir = empty($atts[3]) ? 'ASC' : $atts[3];
     $list_tag = empty($atts[4]) ? 'ul' : $atts[4];
     // validate the user input
     if ($list_tag != 'ul' && $list_tag != 'ol') {
         $list_tag = 'ul';
     }
     // are we in the course desc page or in a lesson of this course?
     $post = get_post($course_id);
     if ($post->post_type == 'namaste_lesson') {
         $course_id = get_post_meta($course_id, 'namaste_course', true);
     }
     // when status column is NOT passed we have a simple task and won't call the student_lessons() method
     // this is because the student_lessons() method is for logged in users only.
     if (empty($status) or !is_user_logged_in()) {
         $_lesson = new NamasteLMSLessonModel();
         $lessons = $_lesson->select($course_id, 'array', null, $ob, $dir);
         $content = "<" . $list_tag . ">";
         foreach ($lessons as $lesson) {
             $content .= "<li><a href='" . get_permalink($lesson->ID) . "'>" . $lesson->post_title . "</a></li>";
         }
         $content .= "</" . $list_tag . ">";
         return $content;
     }
     // status column is requested so we'll have to call the model method
     ob_start();
     $_GET['course_id'] = $course_id;
     $simplified = empty($status) ? 2 : 1;
     // simplified is always at least 1 when called as shortcode. But will be 2 if status column is not requested
     NamasteLMSLessonModel::student_lessons($simplified, $ob, $dir, true);
     $content = ob_get_contents();
     ob_end_clean();
     return $content;
 }
Пример #5
0
 static function full_select($id)
 {
     global $wpdb;
     $_course = new NamasteLMSCourseModel();
     $_lesson = new NamasteLMSLessonModel();
     // select this homework and lesson
     $homework = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . NAMASTE_HOMEWORKS . "\n\t\t\tWHERE id=%d", $id));
     // select course
     $course = $_course->select($homework->course_id);
     // select lesson
     $lesson = $_lesson->select($course->ID, 'single', $homework->lesson_id);
     return array($homework, $course, $lesson);
 }
Пример #6
0
function namaste_ajax()
{
    global $wpdb, $user_ID;
    $type = empty($_POST['type']) ? $_GET['type'] : $_POST['type'];
    switch ($type) {
        case 'lessons_for_course':
            $_lesson = new NamasteLMSLessonModel();
            echo $_lesson->select($_POST['course_id'], 'json', null, 'ID');
            break;
            // load notes for student homework
        // load notes for student homework
        case 'load_notes':
            // unless I am manager I can see other user's notes
            if ($user_ID != $_GET['student_id'] and !current_user_can('namaste_manage')) {
                wp_die('You are not allowed to see these notes.', 'namaste');
            }
            // select notes
            $notes = $wpdb->get_results($wpdb->prepare("SELECT tN.*, tU.user_login as username\n\t\t\t  FROM " . NAMASTE_HOMEWORK_NOTES . " tN JOIN {$wpdb->users} tU ON tU.ID = tN.teacher_id\n\t\t\t\tWHERE homework_id=%d AND student_id=%d ORDER BY tN.id DESC", $_GET['homework_id'], $_GET['student_id']));
            // select homework
            $homework = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . NAMASTE_HOMEWORKS . " WHERE id=%d", $_GET['homework_id']));
            if (@file_exists(get_stylesheet_directory() . '/namaste/homework-notes.php')) {
                require get_stylesheet_directory() . '/namaste/homework-notes.php';
            } else {
                require NAMASTE_PATH . "/views/homework-notes.php";
            }
            break;
            // show lesson progress
        // show lesson progress
        case 'lesson_progress':
            // if i am not manager I can see only my own todo
            if (!current_user_can('namaste_manage') and $user_ID != $_GET['student_id']) {
                die(__("You are not allowed to view this", 'namaste'));
            }
            // select lesson and student
            $lesson = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID=%d", $_GET['lesson_id']));
            $student = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->users} WHERE ID=%d", $_GET['student_id']));
            $todo = NamasteLMSLessonModel::todo($_GET['lesson_id'], $_GET['student_id']);
            if (@file_exists(get_stylesheet_directory() . '/namaste/lesson-todo.php')) {
                require get_stylesheet_directory() . '/namaste/lesson-todo.php';
            } else {
                require NAMASTE_PATH . "/views/lesson-todo.php";
            }
            break;
            // display payment screen for a course
        // display payment screen for a course
        case 'course_payment':
            // select course
            $course = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID=%d", $_GET['course_id']));
            $fee = get_post_meta($course->ID, 'namaste_fee', true);
            $fee = apply_filters('namaste-coupon-applied', $fee);
            $currency = get_option('namaste_currency');
            $accept_other_payment_methods = get_option('namaste_accept_other_payment_methods');
            $accept_paypal = get_option('namaste_accept_paypal');
            $paypal_id = get_option('namaste_paypal_id');
            if ($accept_other_payment_methods) {
                $other_payment_methods = stripslashes(get_option('namaste_other_payment_methods'));
                $other_payment_methods = str_replace('{{course-id}}', $course->ID, $other_payment_methods);
                $other_payment_methods = str_replace('{{course-name}}', $course->post_title, $other_payment_methods);
                $other_payment_methods = str_replace('{{user-id}}', $_GET['student_id'], $other_payment_methods);
                $other_payment_methods = str_replace('{{amount}}', $fee, $other_payment_methods);
                $other_payment_methods = str_replace('{{item-type}}', 'course', $other_payment_methods);
                $other_payment_methods = do_shortcode($other_payment_methods);
            }
            // return URL
            $paypal_return = get_option('namaste_paypal_return');
            if (empty($paypal_return)) {
                $paypal_return = get_permalink($course->ID);
            }
            if (!strstr($paypal_return, 'http')) {
                $paypal_return = 'http://' . $paypal_return;
            }
            if (@file_exists(get_stylesheet_directory() . '/namaste/course-pay.php')) {
                require get_stylesheet_directory() . '/namaste/course-pay.php';
            } else {
                require NAMASTE_PATH . "/views/course-pay.php";
            }
            break;
            // set student's grade for a course or lesson
        // set student's grade for a course or lesson
        case 'set_grade':
            if (!current_user_can('namaste_manage')) {
                die(__('You are not allowed to grade', 'namaste'));
            }
            if ($_POST['grade_what'] == 'course') {
                $table = NAMASTE_STUDENT_COURSES;
                $field = 'course_id';
                $student_field = 'user_id';
                do_action('namaste_graded_course', $_POST['student_id'], $_POST['item_id'], $_POST['grade']);
            } else {
                $table = NAMASTE_STUDENT_LESSONS;
                $field = 'lesson_id';
                $student_field = 'student_id';
                do_action('namaste_graded_lesson', $_POST['student_id'], $_POST['item_id'], $_POST['grade']);
            }
            // now update the grade
            $wpdb->query($wpdb->prepare("UPDATE {$table} SET grade=%s WHERE {$field}=%d AND {$student_field}=%d", $_POST['grade'], $_POST['item_id'], $_POST['student_id']));
            break;
    }
    exit;
}
Пример #7
0
 static function manage()
 {
     global $wpdb;
     $_course = new NamasteLMSCourseModel();
     $_lesson = new NamasteLMSLessonModel();
     // select grades
     $grades = explode(",", stripslashes(get_option('namaste_grading_system')));
     // select courses
     $courses = $_course->select();
     $courses = apply_filters('namaste-homeworks-select-courses', $courses);
     // if course is selected, select in progress students in the course,
     // select lessons and homeworks in it too
     if (!empty($_GET['course_id'])) {
         do_action('namaste-check-permissions', 'course', $_GET['course_id']);
         $this_course = $_course->select($_GET['course_id']);
         $lessons = $_lesson->select($_GET['course_id']);
         $lids = array(0);
         foreach ($lessons as $lesson) {
             $lids[] = $lesson->ID;
         }
         $students = $wpdb->get_results($wpdb->prepare("SELECT tU.*, tS.status as namaste_status, tS.grade as course_grade \n\t\t \t\tFROM {$wpdb->users} tU JOIN " . NAMASTE_STUDENT_COURSES . " tS \n\t\t \t\tON tS.user_id = tU.ID AND tS.course_id=%d AND (tS.status='enrolled' OR status='completed')\n\t\t \t\tORDER BY user_nicename", $_GET['course_id']));
         $stids = array(0);
         foreach ($students as $student) {
             $stids[] = $student->ID;
         }
         // select student-lessons and student-homeworks for this course so we can have the grades
         $student_lessons = $wpdb->get_results("SELECT * FROM " . NAMASTE_STUDENT_LESSONS . "\n\t\t\t\tWHERE lesson_id IN (" . implode(',', $lids) . ") AND student_id IN (" . implode(',', $stids) . ")\n\t\t\t\tORDER BY id");
         $student_homeworks = $wpdb->get_results("SELECT tSH.*, tH.lesson_id as lesson_id FROM " . NAMASTE_STUDENT_HOMEWORKS . " tSH\n\t\t\t\tJOIN " . NAMASTE_HOMEWORKS . " tH ON tH.id = tSH.homework_id\n\t\t\t\tWHERE tH.lesson_id IN (" . implode(',', $lids) . ") AND tSH.student_id IN (" . implode(',', $stids) . ") \n\t\t\t\tORDER BY id");
         // now match everything together so we have it ready for the table
         foreach ($students as $cnt => $student) {
             $lesson_grades = array();
             $lesson_ids = array();
             foreach ($lessons as $lesson) {
                 foreach ($student_lessons as $student_lesson) {
                     if ($student_lesson->student_id == $student->ID and $student_lesson->lesson_id == $lesson->ID) {
                         // get homework grades
                         $homework_grades = array();
                         $lesson_ids[] = $lesson->ID;
                         foreach ($student_homeworks as $student_homework) {
                             if ($student_homework->student_id != $student->ID) {
                                 continue;
                             }
                             if ($student_homework->lesson_id != $lesson->ID) {
                                 continue;
                             }
                             if (empty($student_homework->grade)) {
                                 continue;
                             }
                             $homework_grades[] = $student_homework->grade;
                         }
                         // end foreach student_homeworks
                         $lesson_grades[] = array('homework_grades' => $homework_grades, "final_grade" => $student_lesson->grade, 'lesson_id' => $student_lesson->lesson_id);
                     }
                     // end student & lesson match
                 }
                 // end foreach $student_lessons
             }
             // end foreach $lesson
             $students[$cnt]->lesson_grades = $lesson_grades;
             $students[$cnt]->lesson_ids = $lesson_ids;
         }
         // end foreach $student
     }
     // end if $_GET[course_id]
     if (@file_exists(get_stylesheet_directory() . '/namaste/gradebook.html.php')) {
         require get_stylesheet_directory() . '/namaste/gradebook.html.php';
     } else {
         require NAMASTE_PATH . "/views/gradebook.html.php";
     }
 }