(.*?)<\/Code>/',$response, $matches);
$code = $matches[1];
switch ( $message ) {
case 'Command recieved':
return $code;
break;
default:
return false;
break;
}
return true;
} else {
//$response = 'No LePressStudent plugin found at ' . $wpURL;
return false;
}
return false;
}
function create_key( $length ) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$key = "";
for ($i=0; $i<$length; $i++) {
$key .= substr( $chars, rand( 0, strlen( $chars ) - 1), 1 );
}
return $key;
}
function invite_key_exists( $key ) {
global $wpdb;
$group_table_name = $wpdb->prefix . get_class( $this ) . "_group_invites";
$personal_table_name = $wpdb->prefix . get_class( $this ) . "_personal_invites";
$key = $wpdb->escape( $key );
$sql = "SELECT id FROM $group_table_name WHERE invite_key='$key'";
if ( $row = $wpdb->get_row( $sql ) ) {
return true;
} else {
$sql = "SELECT id FROM $personal_table_name WHERE invite_key='$key'";
if ( $row = $wpdb->get_row( $sql ) ) {
return true;
} else {
return false;
}
}
}
function generate_key( $course_id, $group ) {
global $wpdb;
$key = $this->create_key(5);
$table_name = "";
while ( $this->invite_key_exists( $key ) ){
$key = $this->create_key(5);
}
if ( $group ) {
$table_name = $wpdb->prefix . get_class( $this ) . "_group_invites";
} else {
$table_name = $wpdb->prefix . get_class( $this ) . "_personal_invites";
}
$insert = "INSERT INTO " . $table_name .
" (term_id, invite_key) " .
"VALUES ( $course_id , '$key' )";
$wpdb->query( $insert );
return $key;
}
function add_assignement( $postID, $start, $end ) {
global $wpdb;
$postID = $wpdb->escape( $postID );
$start = $wpdb->escape( $start );
$end = $wpdb->escape( $end );
$table_name = $wpdb->prefix . get_class( $this ) . "_assignments";
$insert = "INSERT INTO " . $table_name .
" (post_id, start, end) " .
"VALUES (" . $postID . ", '" . $start . "', '" . $end . "')";
$wpdb->query( $insert );
//echo "added assignement $insert";
}
//remove assignment from database
function remove_assignment($post_id) {
global $wpdb;
$post_id = $wpdb->escape( $post_id );
$table_name = $wpdb->prefix . get_class( $this ) . "_assignments";
$delete = "DELETE FROM " . $table_name . " WHERE post_id = '".$post_id."'";
$wpdb->query( $delete);
}
function get_assignments_names( $term_id, $assignment_id = false) {
$names = array();
if ( $term_id < 0 ) return $names;
$lepress_posts = get_posts('numberposts=-1&cat='.$term_id);
if(count($lepress_posts) > 0) {
foreach($lepress_posts as $post) {
$cat_ids = get_the_category($post->ID);
foreach($cat_ids as $cat) {
if($term_id === $cat->cat_ID) {
$assignment = $this->getAssignment($post->ID, $assignment_id);
if(is_array($assignment)) {
$id = $assignment['id'];
if($id) {
$names[$id]['id'] = $id;
$names[$id]['name'] = $post->post_title;
$names[$id]['post_id'] = $post->ID;
$names[$id]['start'] = date( 'j.n.Y', strtotime( $assignment['start'] ) );
$names[$id]['end'] = date( 'j.n.Y', strtotime( $assignment['end']) );
}
}
}
}
}
}
return $names;
}
function getAssignment ( $post_id, $assignment_id = false) {
global $wpdb;
$assignments_table_name = $wpdb->prefix . get_class( $this ) . "_assignments";
$sql = "SELECT id, start, end FROM $assignments_table_name WHERE post_id = $post_id";
if($assignment_id) {
$sql .= ' AND id="'.$wpdb->escape($assignment_id).'"';
}
if ( $row = $wpdb->get_row( $sql ) ) {
return array('id'=> $row->id, 'start' => $row->start, 'end' => $row->end);
} else {
return false;
}
}
function post_is_assignment ( $post_id ) {
global $wpdb;
$assignments_table_name = $wpdb->prefix . get_class( $this ) . "_assignments";
$sql = "SELECT id FROM $assignments_table_name WHERE post_id = $post_id";
if ( $row = $wpdb->get_row( $sql ) ) {
return true;
} else {
return false;
}
}
function assignment_start ( $post_id ) {
global $wpdb;
if ( $post_id < 0 ) return false;
$assignments_table_name = $wpdb->prefix . get_class( $this ) . "_assignments";
$sql = "SELECT start
FROM $assignments_table_name
WHERE post_id = $post_id";
if ( $row = $wpdb->get_row( $sql ) ) {
return date( 'j.n.Y', strtotime( $row->start ) );
} else return false;
}
function assignment_end ( $post_id ) {
global $wpdb;
if ( $post_id < 0 ) return false;
$assignments_table_name = $wpdb->prefix . get_class( $this ) . "_assignments";
$sql = "SELECT end
FROM $assignments_table_name
WHERE post_id = $post_id";
if ( $row = $wpdb->get_row( $sql ) ) {
return date( 'j.n.Y', strtotime( $row->end ) );
} else return false;
}
function get_assessment( $post_id, $user_id ){
global $wpdb;
$post_id = (int)$post_id;
$user_id = (int)$user_id;
$user = $this->get_user( $user_id );
$user['wp_url'];
$comments = get_comments('post_id=' . $post_id . '&orderby=comment_date_gmt&order=DESC');
foreach( $comments as $comment ){
if( $comment->comment_type == 'trackback' ){
//get trackebacker wp blog url
$agent = explode( ';' , $comment->comment_agent );
$blog_url = '';
for( $i = 0; $i < count( $agent ); $i++){
if ( strcasecmp( substr( ltrim( $agent[$i] ), 0, 7 ), 'http://' ) == 0 ){
//if agent information starts with 'http://' then it is users blog url
$blog_url = ltrim( $agent[$i] );
}
}
if( $blog_url != '' && $blog_url == $user['wp_url'] ){ //we need to know blog url and it needs to be this users
$comment_id = $comment->comment_ID;
$feedback_id = false;
$feedback_url = false;
$grade = false;
$table_name = $wpdb->prefix . get_class( $this ) . "_filed_assignments";
$sql = "SELECT id, grade, feedback_url
FROM $table_name
WHERE comment_id = $comment->comment_ID AND user_id=$user_id";
if ( $row = $wpdb->get_row( $sql ) ) {
$feedback_id = $row->id;
$feedback_url = $row->feedback_url;
$grade = is_null($row->grade) ? NULL : $row->grade;
}
$assessment = array();
if($feedback_id) {
$assessment['id'] = $comment_id;
$assessment['comment_url'] = $comment->comment_author_url;
$assessment['first_name'] = $user['first_name'];
$assessment['last_name'] = $user['last_name'];
$assessment['wp_url'] = $user['wp_url'];
$assessment['email'] = $user['email'];
$assessment['feedback_id'] = $feedback_id;
$assessment['feedback_url'] = $feedback_url;
$assessment['grade'] = $grade;
$assignment_end_date = $this->assignment_end($post_id);
if(date("Y-m-d", strtotime($comment->comment_date)) > date("Y-m-d", strtotime($assignment_end_date))) {
$assessment['submission_time'] = ''.date("d.m.Y H:i", strtotime($comment->comment_date)).'';
} else {
$assessment['submission_time'] = date("d.m.Y H:i", strtotime($comment->comment_date));
}
}
return $assessment;
}else{
continue;
}
}
}
return false;
}
function is_comment_assessment( $comment_id ) {
global $wpdb;
$id = (int)$comment_id;
$comment = get_comment($id);
//check if coment is on any course
$categories = get_the_category( $comment->comment_post_ID );
$courses_ids = array();
foreach( $categories as $category ){
if( $this->get_is_course( $category->term_id ) ){
array_push( $courses_ids, $category->term_id );
}
}
if( count( $courses_ids ) == 0 ){ //then this comment's post is not in course
return false;
}
//get trackebacker wp blog url
$agent = explode( ';' , $comment->comment_agent );
$blog_url = '';
for( $i = 0; $i < count( $agent ); $i++){
if ( strcasecmp( substr( ltrim( $agent[$i] ), 0, 7 ), 'http://' ) == 0 ){
//if agent information starts with 'http://' then it is users blog url
$blog_url = ltrim( $agent[$i] );
break;
}
}
if( $blog_url == '' ){ //we need to know blog url
return false;
}
//echo 'url: ' . $blog_url;
//check if there is user with that wp url and if he/she is subscribed to a course where this comments post is
$blog_url = $wpdb->escape( $blog_url );
$courses_sql_str = "(" . implode( ',', $courses_ids ) . ')';
$users_table_name = $wpdb->prefix . get_class( $this ) . "_users";
$subs_table_name = $wpdb->prefix . get_class( $this ) . "_subscribed_users";
$subscriptions = array();
$sql = "SELECT $users_table_name.id as user_id, $subs_table_name.id AS subs_id
FROM $users_table_name, $subs_table_name
WHERE $subs_table_name.user_id = $users_table_name.id AND
$subs_table_name.accepted = 1 AND
$users_table_name.wp_url = '$blog_url' AND
$subs_table_name.term_id IN $courses_sql_str";
$rows = $wpdb->get_results( $sql, OBJECT );
foreach ($rows as $row){
$subscriptions[$id]["user_id"] = $row->user_id;
$subscriptions[$id]["subs_id"] = $row->subs_id;
}
if( count( $subscriptions ) == 0){ //user isnt subscribed to this course
return false;
}
return $subscriptions[$id]["user_id"];
}
function add_assesment( $comment_id, $user_id, $trackback_url ) {
global $wpdb;
$comment_id = $wpdb->escape( $comment_id );
$user_id = $wpdb->escape( $user_id );
$trackback_url = $wpdb->escape( $trackback_url );
$table_name = $wpdb->prefix . get_class( $this ) . "_filed_assignments";
$insert = "INSERT INTO " . $table_name .
" (comment_id, user_id, feedback_url) " .
"VALUES (" . $comment_id . ", " . $user_id . ", '" . $trackback_url . "')";
$wpdb->query( $insert );
}
function set_assesment_grade( $comment_id, $grade ){
global $wpdb;
$comment_id = $wpdb->escape( $comment_id );
$grade = $wpdb->escape( $grade );
$table_name = $wpdb->prefix . get_class( $this ) . "_filed_assignments";
$wpdb->query("UPDATE $table_name SET grade='$grade' WHERE comment_id=$comment_id");
}
function update_assesment_dates( $assesment_id, $start, $end ){
global $wpdb;
$assesment_id = (int)$wpdb->escape( $assesment_id );
$start = $wpdb->escape( date( 'Y-n-j', strtotime( $start ) ) );
$end = $wpdb->escape( date( 'Y-n-j', strtotime( $end ) ) );
$table_name = $wpdb->prefix . get_class( $this ) . "_assignments";
$wpdb->query("UPDATE $table_name SET start='$start', end='$end' WHERE id=$assesment_id");
}
function send_assesment_feedback( $comment_id, $feedback_content ){
global $wpdb;
$comment_id = $wpdb->escape( $comment_id );
$feedback_content = $wpdb->escape( $feedback_content );
$table_name = $wpdb->prefix . get_class( $this ) . "_filed_assignments";
$sql = "SELECT id, grade, feedback_url
FROM $table_name
WHERE comment_id = $comment_id";
if ( $row = $wpdb->get_row( $sql ) ) {
$feedback_id = $row->id;
$grade = $row->grade;
$trackback_url = $row->feedback_url;
$trackback_url_has_query_params = @parse_url($trackback_url, PHP_URL_QUERY);
if($trackback_url_has_query_params == NULL) {
$trackback_url .= '?grade=' . $grade;
}else{
$trackback_url .= '&grade=' . $grade;
}
$comment = get_comment( $comment_id );
$post_id = $comment->comment_post_ID;
trackback( $trackback_url, 'Your homework grade is ' . $grade, $feedback_content, $post_id );
return true;
}else{
//feedback could not be sent
return false;
}
}
function get_ungraded_assignments_count(){
global $wpdb;
$table_name = $wpdb->prefix . get_class( $this ) . "_filed_assignments";
$table_name_subscr = $wpdb->prefix . get_class( $this ) . "_subscribed_users";
$sql = "SELECT comment_id
FROM $table_name, $table_name_subscr
WHERE grade IS NULL and $table_name_subscr.user_id = $table_name.user_id GROUP BY comment_id";
$total = 0;
$result = $wpdb->get_results($sql, OBJECT);
foreach($result as $row) {
$comment = get_comment($row->comment_id);
if($comment) {
$post = get_post($comment->comment_post_ID);
if($post) {
if($this->post_is_assignment($comment->comment_post_ID) && $post->post_status != "trash") {
$total++;
}
}
}
}
return $total;
}
function get_assignments_that_ended ( $days ) {
global $wpdb;
$table_name = $wpdb->prefix . get_class( $this ) . "_assignments";
$sql = "SELECT id, post_id FROM $table_name WHERE end < sysdate() - interval $days day";
$rows = $wpdb->get_results( $sql, OBJECT );
$assignments = array();
foreach ($rows as $row){
$id = $row->id;
$assignments[$id]["id"] = $row->id;
$assignments[$id]["post_id"] = $row->post_id;
}
return $assignments;
}
function get_default_course_id(){
global $wpdb;
$course = array();
$terms_table_name = $wpdb->prefix . "terms";
$table_name = $wpdb->prefix . get_class( $this ) . "_course_categories";
$sql = "SELECT $table_name.term_id as term_out_id
FROM $table_name, $terms_table_name WHERE $table_name.term_id = $terms_table_name.term_id AND is_course=1
LIMIT 1";
if ( $row = $wpdb->get_row( $sql ) ) {
return $row->term_out_id;
}else{
return false;
}
}
//end of functions with directly database
//done
function notify_subscription_user_email ( $subscription_id, $accepted, $responseText ) {
global $wpdb;
$subscription = $this->get_subscription( $subscription_id );
$course_name = $this->get_course_name( $subscription['term_id'] );
$to = $this->get_user_email( $subscription['user_id'] );
$subject = 'Subject not set';
$message = __( 'Dear Student!' ) . "\r\n";
if ( $accepted ) {
$subject = __( 'You have been accepted to a ' ) . $course_name . ' ' . __( 'course' );
$message.= sprintf( __( 'You have been accepted to a course. Teacher response: %s' ), "\r\n" . $responseText );
}else {
$subject = __( 'You have been rejected to a ' ) . $course_name . ' ' . __( 'course' );
$message.= sprintf( __( 'You have been rejected to a course. Teacher response: %s' ), "\r\n" . $responseText );
}
wp_mail($to, $subject, $message);
}
function invite_students_email( $emails, $course_ids, $key, $teacher_message ) {
global $wpdb;
$subject = __( 'You have been invited to a course' );
$message = __( 'Dear Student!' );
foreach($course_ids as $course_id) {
$course_name = $this->get_course_name( $course_id );
$course_url = urldecode(get_category_link( $course_id ));
$message .= "\r\n". "\r\n".sprintf( __( 'I invite you to join %s course.' ), $course_name) . "\r\n\r\n";
$message .= __( 'Message from teacher: ' ) . $teacher_message . "\r\n\r\n";
$message .= __( 'URL for this course is: ' ) . $course_url . "\r\n";
if($key == 1) {
//personal key
$invite_key = $this->generate_key( $course_id, false );
$key_info = "\r\n".__( 'When subscribing to a course, use following key: ' ) . $invite_key . "\r\n";
$message .= $key_info;
}
if($key == 2) {
//group key
$invite_key = $this->generate_key( $course_id, true );
$key_info = "\r\n".__( 'When subscribing to a course, use following key: ' ) . $invite_key . "\r\n";
$message .= $key_info;
}
if($key) {
$key_txt = "and key into appropriate fields";
} else {
$key_txt = "in appropriate field";
}
}
$download_note = __("\r\n".'Please go to Subscribtions page of your LePress dashboard in your WordPress blog and enter course URL '.$key_txt.' of Subscribe to Course form and then submit the form.')."\r\n";
$download_note .= "\r\n" . __( 'To subscribe to course(s), you need a LePress plugin for your wordpress blog.' ) . "\r\n";
$download_note .= __( 'You can download this plugin from here: ' ) . $this->plugin_download_url;
foreach($emails as $to) {
$msg = $message . $download_note;
wp_mail($to, $subject, $msg);
}
}
function post_deleted_handler($post_id) {
$cat_data = get_the_category($post_id);
//if the post category is course - then it's obviously assignment
foreach($cat_data as $cat) {
if($this->get_is_course($cat->term_id)) {
$this->remove_assignment($post_id);
break;
}
}
}
function comment_deleted_handler($comment_ids) {
$comment_ids = implode(',', $comment_ids);
global $wpdb;
$table_name_filed = $wpdb->prefix . get_class( $this ) . "_filed_assignments";
$delete_filed = "DELETE FROM " . $table_name_filed . " WHERE comment_id IN (".$comment_ids.")";
$wpdb->query( $delete_filed);
}
function post_assignment_handler() {
$action = $_REQUEST['action'] != '-1' ? $_REQUEST['action'] : $_REQUEST['action2'];
switch($action) {
case 'mark_as_assignment':
foreach($_REQUEST['post'] as $post_id) {
if(!$this->post_is_assignment ($post_id)) {
$categories = get_the_category($post_id);
foreach($categories as $category) {
if($this->get_is_course($category->term_id)) {
$this->add_assignement($post_id, date('Y-m-d'), date('Y-m-d', strtotime('+1 day')));
break;
}
}
}
}
break;
case 'mark_as_not_assignment':
foreach($_REQUEST['post'] as $post_id) {
if($this->post_is_assignment ($post_id)) {
$this->remove_assignment($post_id);
}
}
break;
}
}
function LePress_columns($defaults) {
$defaults['lepress_assignment'] = __('Assignment');
return $defaults;
}
function custom_column_handler($column_name, $post_id) {
if( $column_name == 'lepress_assignment' ) {
if($this->post_is_assignment($post_id)) {
_e('Yes');
} else {
_e('No');
}
}
}
function admin_head_Code() {
if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] == "write_assignment_page" ) {
wp_enqueue_script( 'common' );
wp_enqueue_script( 'jquery-color' );
wp_print_scripts('editor');
if (function_exists('add_thickbox')) add_thickbox();
wp_print_scripts('media-upload');
if (function_exists('wp_tiny_mce')) wp_tiny_mce();
wp_admin_css();
wp_enqueue_script('utils');
do_action("admin_print_styles-post-php");
do_action('admin_print_styles');
}
$break = explode('/', $_SERVER["SCRIPT_NAME"]);
$pfile = $break[count($break) - 1];
if($pfile == "edit.php" && $_GET['post_type'] == "post") {
echo '';
}
}
function awaiting_mod( $awaiting_what ){
$count = 0;
if( $awaiting_what == "awaiting authorisation" ){
$count = count( $this->get_unsubscribed_users() );
}elseif( $awaiting_what == "new assignments" ){
$count = $this->get_ungraded_assignments_count();
}
$text = '
' . $count . '
';
return $text;
}
function manage_courses_menu(){
add_menu_page( 'LePress Teacher', 'LePress', 3, __FILE__, array( &$this, 'manage_courses_page' ) );
add_submenu_page( __FILE__, 'Courses Managing', 'Courses', 3, __FILE__, array( &$this, 'manage_courses_page' ) );
add_submenu_page( __FILE__, 'Subscriptions', 'Subscriptions' . $this->awaiting_mod( 'awaiting authorisation' ), 3, 'manage_subscriptions_page', array( &$this, 'manage_subscriptions_page' ) );
add_submenu_page( __FILE__, 'Write assignment', 'Write assignment', 3, 'write_assignment_page', array( &$this, 'write_assignment_page' ) );
add_submenu_page( __FILE__, 'Assignments', 'Manage assignments' . $this->awaiting_mod( 'new assignments' ), 3, 'manage_assignments_page', array( &$this, 'manage_assignments_page' ) );
}
function rss2_feed_item(){
global $post;
if ( $this->post_is_assignment( $post->ID ) == true ) {
echo '';
echo '' . $this->assignment_start( $post->ID ) . ' ';
$end = $this->assignment_end( $post->ID );
if ( $end ) {
echo '' . $end . ' ';
}
echo ' ';
echo '' . get_trackback_url() . ' ';
echo '' . $post->ID . ' ';
}
//echo '' . $post->ID . ' ';
}
function install () {
global $wpdb;
$table_name = $wpdb->prefix . get_class($this) . "_course_categories";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
id bigint(20) NOT NULL AUTO_INCREMENT,
term_id bigint(20) NOT NULL,
is_course bigint(1) DEFAULT '0' NOT NULL,
organis_name VARCHAR(200),
teacher_first_name VARCHAR(200),
teacher_last_name VARCHAR(200),
teacher_email VARCHAR(200),
UNIQUE KEY id (id)
) DEFAULT CHARSET=utf8;";
//require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
$table_name = $wpdb->prefix . get_class($this) . "_users";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
id bigint(20) NOT NULL AUTO_INCREMENT,
first_name VARCHAR(200),
last_name VARCHAR(200),
wp_url VARCHAR(2000),
email VARCHAR(200),
UNIQUE KEY id (id)
) DEFAULT CHARSET=utf8;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
$table_name = $wpdb->prefix . get_class($this) . "_subscribed_users";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
id bigint(20) NOT NULL AUTO_INCREMENT,
term_id bigint(20) NOT NULL,
user_id bigint(20) NOT NULL,
accepted int(1),
accept_key VARCHAR(6),
UNIQUE KEY id (id)
) DEFAULT CHARSET=utf8;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
$table_name = $wpdb->prefix . get_class($this) . "_group_invites";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
id bigint(20) NOT NULL AUTO_INCREMENT,
term_id bigint(20) NOT NULL,
invite_key VARCHAR(5) NOT NULL,
UNIQUE KEY id (id)
) DEFAULT CHARSET=utf8;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
$table_name = $wpdb->prefix . get_class($this) . "_personal_invites";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
id bigint(20) NOT NULL AUTO_INCREMENT,
term_id bigint(20) NOT NULL,
wp_url VARCHAR(2000) NOT NULL,
invite_key VARCHAR(5) NOT NULL,
UNIQUE KEY id (id)
) DEFAULT CHARSET=utf8;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
$table_name = $wpdb->prefix . get_class($this) . "_assignments";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
id bigint(20) NOT NULL AUTO_INCREMENT,
post_id bigint(20) NOT NULL,
start date NOT NULL,
end date,
UNIQUE KEY id (id)
) DEFAULT CHARSET=utf8;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
$table_name = $wpdb->prefix . get_class($this) . "_filed_assignments";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
id bigint(20) NOT NULL AUTO_INCREMENT,
comment_id bigint(20) NOT NULL,
user_id bigint(20) NOT NULL,
grade VARCHAR(2) NULL,
feedback_url VARCHAR(2000) NOT NULL,
UNIQUE KEY id (id)
) DEFAULT CHARSET=utf8;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
//if you add table table here, dont forget to add it to uninstall
wp_schedule_event(time(), 'daily', 'scheduled_old_assignment_check_hook');
}
function manage_courses_page() {
$action = ( isset( $_REQUEST['action'] ) ) ? $_REQUEST['action'] : '';
switch ( $action ) {
case 'edit':
include( 'edit_category.php' );
break;
case 'editedcat':
$cat_ID = (int) ( isset( $_REQUEST['cat_ID'] ) ) ? $_REQUEST['cat_ID'] : null;
$is_course = ( empty( $_REQUEST['category_is_course'] ) ) ? '0' : '1';
$organisation_name = ( isset( $_REQUEST['category_organisation_name'] ) ) ? $_REQUEST['category_organisation_name'] : '';
$first_name = ( isset( $_REQUEST['teacher_f_name'] ) ) ? trim($_REQUEST['teacher_f_name']) : '';
$last_name = ( isset( $_REQUEST['teacher_l_name'] ) ) ? trim($_REQUEST['teacher_l_name']) : '';
$email = ( isset( $_REQUEST['teacher_email'] ) ) ? trim($_REQUEST['teacher_email']) : '';
$desc = ( isset( $_REQUEST['description'] ) ) ? $_REQUEST['description'] : '';
$parent = ( isset( $_REQUEST['parent'] ) ) ? $_REQUEST['parent'] : '';
if ($cat_ID && $this->get_course($cat_ID)){
if(!empty($first_name) && !empty($last_name) && !empty($email)) {
$this->update_category( $cat_ID, $is_course, $organisation_name, $first_name, $last_name, $email, $desc, $parent );
include( 'manage_courses_page.php' );
} else {
$validate_failed = true;
include('edit_category.php');
}
}else echo __('You cannot change data if category is not course.');
break;
default:
include( 'manage_courses_page.php' );
}
}
function getSubsStudents() { //for xmlhttprequest
if( isset( $_REQUEST['id'] ) ){
echo "";
foreach ( $this->get_subscribed_users_full_data( $_REQUEST['id'] ) as $userId ) {
echo "";
echo "" . $userId["subsId"] . " ";
echo "" . $userId["fName"] . " " . $userId["lName"] . " ";
echo "".$userId["email"]." ";
echo "View assignments ";
echo "" . $userId["wpURL"] . " ";
echo "Send e-mail ";
echo "Delete ";
echo " ";
}
echo "";
}
}
function get_widget_calendar( $course_id, $month, $year, $widget_id ){
$assignments = $this->get_assignments_names( $course_id );
$this_month_assignments = array();
$return_str = '';
$assignments_next_month = false;
$assignments_prev_month = false;
$timestamp = mktime(0,0,0,$month,1,$year);
$maxday = date( 't',$timestamp );
$month_name = date( 'F',$timestamp );
$thismonth = getdate( $timestamp );
$startday = $thismonth['wday'];
//sort assignments
foreach( $assignments as $assignment ){
if ( date( 'n', strtotime( $assignment['end'] ) ) == $month && date( 'Y', strtotime( $assignment['end'] ) ) == $year ){
//array_push( $this_month_assignments[ date( 'j', strtotime( $assignment['end'] ) ) ], $assignment );
if( isset( $this_month_assignments[ date( 'j', strtotime( $assignment['end'] ) ) ] ) ){
array_push( $this_month_assignments[ date( 'j', strtotime( $assignment['end'] ) ) ], $assignment );
}else{
$this_month_assignments[ date( 'j', strtotime( $assignment['end'] ) ) ] = array();
array_push( $this_month_assignments[ date( 'j', strtotime( $assignment['end'] ) ) ], $assignment );
}
}
if ( ( date( 'n', strtotime( $assignment['end'] ) ) < $month && date( 'Y', strtotime( $assignment['end'] ) ) <= $year )
|| date( 'Y', strtotime( $assignment['end'] ) ) < $year ){
$assignments_prev_month = true;
}
if ( ( date( 'n', strtotime( $assignment['end'] ) ) > $month && date( 'Y', strtotime( $assignment['end'] ) ) >= $year )
|| date( 'Y', strtotime( $assignment['end'] ) ) > $year ){
$assignments_next_month = true;
}
}
//end of sorting
$return_str .= '';
$return_str .= '' . __( $month_name ) . ', ' . $year . ' ';
$return_str .= 'M T W T F S S ';
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 1 ) $return_str .= "";
if($i < $startday){
$return_str .= " ";
}else{
$today_border = '';
if(($i-$startday + 1) == date('j') && $month == date('n') && $year == date('Y')) {
$today_border = "border: 1px solid red;";
}
if( isset( $this_month_assignments[($i - $startday + 1)] ) ){
$return_str .= '' . ($i - $startday + 1) . '';
$return_str .= ' ';
}else{
$return_str .= '' . ($i - $startday + 1) . ' ';
}
}
if(($i % 7) == 0 ) $return_str .= " ";
}
$return_str .= '';
if( $assignments_prev_month == true ){
$prev_month = $month - 1;
$prev_year = $year;
if( $prev_month <= 0 ){
$prev_month = 12;
$prev_year --;
}
$return_str .= '' . __( 'prev' ) . ' ';
}else{
$return_str .= ' ';
}
$return_str .= ' ';
if( $assignments_next_month == true ){
$next_month = $month + 1;
$next_year = $year;
if( $next_month > 12 ){
$next_month = 1;
$next_year ++;
}
$return_str .= '' . __( 'next' ) . ' ';
}else{
$return_str .= ' ';
}
$return_str .= '
';
return $return_str;
}
function get_widget_body( $widget_id, $instance) {
global $wp_query;
$course_id = -1;
if ( isset( $_REQUEST['course_id'] ) ){
$course_id = $_REQUEST['course_id'];
}elseif ( $wp_query->query_vars['cat'] ){
$course_id = $wp_query->query_vars['cat'];
}elseif ( $wp_query->query_vars['category_name'] ){
$course_id = get_cat_ID( $wp_query->query_vars['category_name'] );
}else{
$course_id = $this->get_default_course_id();
}
if(!$this->get_is_course($course_id) && isSet($course_id)) {
$course_id = $this->get_default_course_id();
}
//if no courses exist
if( $course_id == false ){return __('No active courses');}
$return_str = '';
$return_str = $debug;
//dropdown box with list of courses
$return_str .= '';
$return_str .= '';
//calendar
$return_str .= '';
if( isset( $course_id ) ) {
$return_str .= $this->get_widget_calendar( $course_id, date('n'), date('Y'), $widget_id );
}
$return_str .= '';
if($instance['collapse_students'] == 1) {
$collapsed = "none";
$sign = "+";
} else {
$collapsed = "block";
$sign = "-";
}
//course students
$return_str .= '' . '
'. __( 'Participants' ) . ' '.$sign.'
';
if( isset( $course_id ) ) {
foreach( $this->get_subscribed_users_full_data( $course_id ) as $student ) {
$return_str .= '';
$return_str .= '';
$return_str .= '';
$return_str .= ' ' . $student['lName']
. ' ' . $student['fName'] . '';
$return_str .= '';
}
}
//course assignements
$return_str .= '' . ''
. __( 'Assignments' ) . '
';
if ( isset( $course_id ) ){
if($instance['collapse_assignments'] == 1) {
$collapsed = "none";
$sign = "+";
} else {
$collapsed = "block";
$sign = "-";
}
foreach( $this->get_assignments_names( $course_id ) as $assignment ) {
$salt = time();
$return_str .= '- ' .
$assignment['end'] . ' ' . $assignment['name'] . ' '.$sign.'';
$return_str .= '';
$return_str .= '
';
$return_str .= '';
foreach( $this->get_course_users_ids( $course_id ) as $userId ){
$post_id = $assignment['post_id'];
$assessment = $this->get_assessment( $post_id , $userId['id'] );
if( $assessment != false ){
$return_str .= '';
if( $assessment['grade'] != false ){
$return_str .= ' ';
}else{
$return_str .= ' ';
}
$return_str .= ''.$assessment['last_name'] . ' ' . $assessment['first_name'].'
';
$return_str .= '';
}
}
$return_str .= '';
$return_str .= '';
}
}
$return_str .= '
'; //end of assignments
return $return_str;
}
function uninstall() {
global $wpdb;
$list_of_tables_to_drop = Array( '_course_categories', '_users','_subscribed_users', /*'_unsubscribed_users', */
'_group_invites', '_personal_invites', '_assignments', '_filed_assignments' );
foreach ( $list_of_tables_to_drop as $table_name ) {
$full_table_name = $wpdb->prefix . get_class($this) . $table_name;
$sql = "DROP TABLE " . $full_table_name;
$wpdb->query( $sql );
}
wp_clear_scheduled_hook( 'scheduled_old_assignment_check_hook' );
}
function runInit() {
if(isSet($_REQUEST)) {
if($_REQUEST['page'] == "write_assignment_page" || $_REQUEST['page'] == 'manage_assignments_page') {
//First register our script
wp_register_script( 'epoch-calendar', plugins_url('/js/epoch_classes.js', __FILE__));
wp_register_script( 'epoch-calendar_init', plugins_url('/js/load_epoch_calendar.js', __FILE__));
//and then load it
wp_enqueue_script( 'epoch-calendar');
wp_enqueue_script( 'epoch-calendar_init');
//First register our stylesheet
wp_register_style( 'LePressTeacherCss-epoch-calendar', plugins_url('/js/epoch_styles.css', __FILE__));
//and then load it
wp_enqueue_style( 'LePressTeacherCss-epoch-calendar');
}
}
}
function write_assignment_page() {
include( 'write_assignement_page.php' );
}
function manage_assignments_page() {
include( 'manage_assignments_page.php' );
}
function manage_subscriptions_page() {
include( 'manage_subscriptions_page.php' );
}
function wp_head() {
global $wp_query;
global $current_user;
get_currentuserinfo();
wp_enqueue_script( 'jquery' );
wp_enqueue_script('LePressTeacherWidgetScript'); //load javascript for widget
if ( isset( $_REQUEST['action'] ) && ($wp_query->is_category || is_404())) {
switch ( $_REQUEST['action'] ) {
case "LePressTeacherUpdateProfile":
//basic check here, if parameter siteurl is not empty
if(!empty($_REQUEST['siteurl'])) {
if($_REQUEST['siteurl'] == get_bloginfo('siteurl')) {
return;
}
//all the other security check done by update_user method
$this->update_user($_REQUEST['fname'], $_REQUEST['lname'], $_REQUEST['siteurl'], $_REQUEST['email'], $_REQUEST['student_key'], $_REQUEST['term_id']);
}
break;
case "LePressStudentGetClassmates":
//basic check here, if parameter siteurl is not empty
if(!empty($_REQUEST['siteurl'])) {
if($_REQUEST['siteurl'] == get_bloginfo('siteurl')) {
return;
}
//check key validity
if($this->check_communication_key_validity($_REQUEST['term_id'], $_REQUEST['siteurl'], $_REQUEST['student_key'])) {
$subscribed_users = $this->get_subscribed_users_full_data($_REQUEST['term_id']);
echo '';
echo '';
foreach($subscribed_users as $key => $subscriber) {
echo '';
echo ''.$subscriber['fName'].' ';
echo ''.$subscriber['lName'].' ';
echo ''.$subscriber['wpURL'].' ';
echo ''.$subscriber['email'].' ';
echo ' ';
}
echo ' ';
echo ' ';
}
}
break;
case "LePressStudentGetCourseUpdate":
//basic check here, if parameter siteurl is not empty
if(!empty($_REQUEST['siteurl'])) {
if($_REQUEST['siteurl'] == get_bloginfo('siteurl')) {
return;
}
//check key validity
echo '';
if($this->check_communication_key_validity($_REQUEST['term_id'], $_REQUEST['siteurl'], $_REQUEST['student_key'])) {
$course = $this->get_course($_REQUEST['term_id']);
if($course) {
echo '';
echo '1 ';
echo '';
echo ''.$course['teacher_first_name'].' ';
echo ''.$course['teacher_last_name'].' ';
echo ''.$course['teacher_email'].' ';
echo ''.$course['organis_name'].' ';
echo ' ';
echo ' ';
} else {
echo '';
echo '-1 ';
echo ' ';
}
} else {
echo '';
echo '-1 ';
echo ' ';
}
echo ' ';
}
break;
case "LePressTeacherSubscribe":
if ( isset( $_REQUEST['siteurl'] ) &&
isset( $_REQUEST['fname'] ) && isset( $_REQUEST['lname'] ) &&
isset( $_REQUEST['email'] ) ) {
if($_REQUEST['siteurl'] == get_bloginfo('siteurl')) {
//Disable subscription to your own blog
$response = "You cannot subscribe to your own course";
echo "" . $response . " ";
return;
}
//$course_id = $this->get_course_id_by_slug( $_REQUEST['slug'] );
if ( $wp_query->query_vars['cat'] ){
$course_id = $wp_query->query_vars['cat'];
}else{
$course_id = get_cat_ID( $wp_query->query_vars['category_name'] );
}
if ( $course_id && $this->get_is_course( $course_id ) ) {
if ( isset ( $_REQUEST['inviteKey'] ) && $_REQUEST['inviteKey'] != '' ) {
//key is set if LePressTeacher has send invitation or requester has a personal or group key for specific course
if ( $this->check_key_validity( $course_id, $_REQUEST['inviteKey'] ) ) {
//invite key is found in database
$user_id;
//check if user with this WP siteurl and e-mail exists in database, if it does user ID will be returned, otherwise new user will be created
if ( !( $user_id = $this->get_user_id( $_REQUEST['siteurl'], $_REQUEST['email'] ) ) ) {
$this->add_user( $_REQUEST['fname'], $_REQUEST['lname'], $_REQUEST['siteurl'], $_REQUEST['email'] );
$user_id = $this->get_user_id( $_REQUEST['siteurl'], $_REQUEST['email'] );
}
$course = $this->get_course( $course_id );
//subscribe to course
$alreadySubcribed = $this->add_subscription( true, $user_id, $course_id, $_REQUEST['acceptKey'] );
if($alreadySubscribed) {
$response = "You are already subscribed to that course ";
} else {
//remove invitation from database
$this->remove_personal_invite( $course_id, $_REQUEST['inviteKey'] );
$response = "Added to Course " .
"" . $this->get_course_name( $course_id ) . " " .
"" . $this->get_course_organisation_name( $course_id ) . " " .
"" . $course['teacher_email'] . " " .
"" . $course['teacher_first_name'] . " " .
"" . $course['teacher_last_name'] . " " .
"" . $course_id . " " .
"" . get_category_feed_link( $course_id, "rss2" ) . " " .
"" . get_category_link( $course_id ) . " ";
}
} else {
$response = "Bad inviteKey ";
}
} else {
//subscriber doesn't have invite key, subscription request will be made
$user_id;
//check if user with this WP siteurl and e-mail exists in database, if it does user ID will be returned, otherwise new user will be created
if ( !( $user_id = $this->get_user_id( $_REQUEST['siteurl'], $_REQUEST['email'] ) ) ) {
$this->add_user( $_REQUEST['fname'], $_REQUEST['lname'], $_REQUEST['siteurl'], $_REQUEST['email'] );
$user_id = $this->get_user_id( $_REQUEST['siteurl'], $_REQUEST['email'] );
}
$course = $this->get_course( $course_id );
//subscription request is made
$alreadySubscribed = $this->add_subscription( false, $user_id, $course_id, $_REQUEST['acceptKey'] );
if($alreadySubscribed) {
$response = "You are already subscribed to that course ";
} else {
$response = "Subscription request made " .
"" . $this->get_course_name( $course_id ) . " " .
"" . $this->get_course_organisation_name( $course_id ) . " " .
"" . $course['teacher_email'] . " " .
"" . $course['teacher_first_name'] . " " .
"" . $course['teacher_last_name'] . " " .
"" . $course_id . " " .
"" . get_category_feed_link( $course_id, "rss2" ) . " " .
"" . get_category_link( $course_id ) . " ";
}
}
} else {
$response = "No course with that id: $course_id ";
}
}else{
$response = "Seems like you have one of following parameters not set in your Wordpress: siteurl, first name, last name, email. ";
}
echo "" . $response . " ";
break;
case "LePressTeacherUnsubscribe":
if ( isset( $_REQUEST['siteurl'] ) && $_REQUEST['siteurl'] != '' &&
isset( $_REQUEST['id'] ) && $_REQUEST['id'] != '' &&
isset ( $_REQUEST['key'] ) && $_REQUEST['key'] != '' ) {
//neccesary fields exist
$subscription_id = $this->check_communication_key_validity( $_REQUEST['id'], $_REQUEST['siteurl'], $_REQUEST['key'] );
if( $subscription_id != false ){
//echo 'validation is good';
$this->remove_subscription(intval($subscription_id));
$subscription_id_exist = $this->check_communication_key_validity( $_REQUEST['id'], $_REQUEST['siteurl'], $_REQUEST['key'] );
if(!$subscription_id_exist) {
echo "Successfully unsubscribed 1
";
} else {
echo "Unsubscribing failed - validation failed 0
";
}
}else{
//doesn't exist such data on teacher blog, return true - to delete
echo "Successfully unsubscribed 1
";
}
}else{
//something happend, cancel
echo "Unsubscribing failed - parameters missing 0
";
}
break;
case "LePressTeacherVersionCheck":
echo "" . $this->version . " ";
break;
}
}
}
function trackback_received( $comment_id ){
$user_id = $this->is_comment_assessment( $comment_id ); //returns false or user_id
if ( $user_id != false && isset( $_REQUEST['trackback_url'] ) ){
$trackback_url = $_REQUEST['trackback_url'];
$this->add_assesment( $comment_id, $user_id, $trackback_url );
}
}
function check_old_assignments(){
global $LePressTeacher_plugin;
$assignments = $LePressTeacher_plugin->get_assignments_that_ended( 90 );
foreach( $assignments as $assignment ){
$my_post = array();
$my_post['ID'] = $assignment['post_id'];
$my_post['post_status'] = 'private';
// Update the post into the database
wp_update_post( $my_post );
}
}
}
}
class LePressTeacher_Widget extends WP_Widget {
function LePressTeacher_Widget() {
// outputs the content of the widget
$widget_ops = array('classname' => 'LePressTeacher_Widget', 'description' => __( "Widget for LePress Teacher plugin.") );
$this->WP_Widget('lepressteacher_widget', __('LePress Teacher'), $widget_ops);
}
function widget( $args, $instance ) {
global $LePressTeacher_plugin, $wp_query;
if( $wp_query->is_category == false && $instance['visible_everywhere'] == 0 ) return ;
// widget actual processes
extract($args);
$title = apply_filters( 'widget_title', __('LePress Teacher') );
# Before the widget
echo $before_widget;
# The title
if ( $title )
echo $before_title . $title . $after_title;
# Make the widget body
echo '';
echo $LePressTeacher_plugin->get_widget_body( $this->id, $instance);
echo '';
# After the widget
echo $after_widget;
}
function form($instance) {
// outputs the options form on admin
//Defaults
$instance = wp_parse_args( (array) $instance, array( 'visible_everywhere'=>1, 'collapse_students' => 1, 'collapse_assignments' => 1) );
# Output the options
echo '
' .
'
' .
'
' .
'
';
}
function update($new_instance, $old_instance) {
// processes widget options to be saved
return $new_instance;
}
}
if (class_exists("LePressTeacher")) {
global $LePressTeacher_plugin;
$version = explode('.',PHP_VERSION);
if( (int)$version[0] >= 5 ){
$LePressTeacher_plugin = new LePressTeacher();
}else{
add_action( 'admin_menu', 'lepress_teacher_before_install_error_menu', 1);
}
}
function lepress_teacher_before_install_error_menu() {
add_menu_page( 'LePress Teacher', 'LePress', 3, __FILE__, 'lepress_teacher_before_install_error_page' );
}
function lepress_teacher_before_install_error_page() {
$version = explode('.',PHP_VERSION);
echo 'You are using PHP version ' . $version[0] . '. Unfortunatelly only PHP version 5+ is supported';
}
//Actions and Filters
if (isset($LePressTeacher_plugin)) {
global $LePressTeacher_plugin;
//Actions
register_activation_hook( __FILE__, array(&$LePressTeacher_plugin, 'install'));
register_deactivation_hook( __FILE__, array(&$LePressTeacher_plugin, 'uninstall'));
//init
add_action('init', array(&$LePressTeacher_plugin, 'runInit'),1);
add_action( 'create_category', array( &$LePressTeacher_plugin, 'create_category' ), 1 );
add_action( 'admin_head', array( &$LePressTeacher_plugin, 'admin_head_Code' ), 1 );
add_action( 'admin_menu', array( &$LePressTeacher_plugin, 'manage_courses_menu' ), 1 );
add_action( 'wp_head', array( &$LePressTeacher_plugin, 'wp_head' ), 1 );
add_action( 'rss2_item', array( &$LePressTeacher_plugin, 'rss2_feed_item' ) );
add_action( 'trackback_post', array( &$LePressTeacher_plugin, 'trackback_received'), 1 );
add_action( 'wp_ajax_getSubsStudents', array( &$LePressTeacher_plugin, 'getSubsStudents' ), 1 );
add_action( 'scheduled_old_assignment_check_hook', array( &$LePressTeacher_plugin, 'check_old_assignments' ), 10, 3 );
//init widget
add_action('widgets_init', create_function('', 'return register_widget("LePressTeacher_Widget");'), 1);
wp_register_script('LePressTeacherWidgetScript', plugin_dir_url( __FILE__ ) . '/js/widget.js', array( 'jquery' ) );
//Category deletion via WP categories menu
add_action('delete_category', array( &$LePressTeacher_plugin, 'category_deleted_handler'), 1, 1);
//Post deletion via WP menu
add_action('delete_post', array( &$LePressTeacher_plugin, 'post_deleted_handler'), 1, 1);
//Comment deletion
add_action('deleted_comment', array( &$LePressTeacher_plugin, 'comment_deleted_handler'), 1, 1);
//Post assignment bulk actions
if(isSet($_REQUEST['action'])) {
$LePressTeacher_plugin->post_assignment_handler();
}
add_filter('manage_posts_columns', array( &$LePressTeacher_plugin, 'LePress_columns'));
add_action('manage_posts_custom_column', array( &$LePressTeacher_plugin, 'custom_column_handler'), 10, 2);
}
?>