static function todo($lesson_id, $student_id) { global $wpdb; $todo_homeworks = $todo_exam = $todo_admin_approval = NULL; // for homeworks automatically detect if there is a post that contains [namaste-assignments lesson_id="X"] // If yes, generate proper submit link instead of going into the admin $homework_posts = $wpdb->get_results("SELECT ID, post_content FROM {$wpdb->posts}\n\t\t\tWHERE post_status = 'publish' AND post_date < NOW()\n\t\t\tAND post_content LIKE '%[namaste-assignments%' ORDER BY ID DESC"); $post_found = null; foreach ($homework_posts as $post) { if (stristr($post->post_content, '[namaste-assignments lesson_id="' . $lesson_id . '"]') or stristr($post->post_content, '[namaste-assignments lesson_id=' . $lesson_id . ']')) { $post_found = $post->ID; break; } } // todo homeworks $required_homeworks = get_post_meta($lesson_id, 'namaste_required_homeworks', true); if (!is_array($required_homeworks)) { $required_homeworks = array(); } if (!empty($required_homeworks)) { // select all completed homeworks of this student and see if all required are satisfied $completed_homeworks = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT(homework_id) FROM " . NAMASTE_STUDENT_HOMEWORKS . " WHERE student_id=%d AND status='approved'", $student_id)); $ids = array(0); foreach ($completed_homeworks as $hw) { $ids[] = $hw->homework_id; } $todo_homeworks = array(); foreach ($required_homeworks as $required_id) { if (!empty($required_id) and !in_array($required_id, $ids)) { $homework = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . NAMASTE_HOMEWORKS . " WHERE id=%d", $required_id)); if (empty($homework->id)) { continue; } // define the submit link if ($post_found) { $permalink = get_permalink($post_found); $params = array('id' => $homework->id, 'submit_solution' => 1); $target_url = add_query_arg($params, $permalink); $homework->submit_link = $target_url; } else { $homework->submit_link = admin_url("admin.php?page=namaste_submit_solution&id=" . $homework->id); } $todo_homeworks[] = $homework; } } } // todo exam $use_exams = get_option('namaste_use_exams'); $todo_exam = NamasteLMSLessonModel::todo_exam($lesson_id, $student_id, 'id'); if (!empty($todo_exam)) { if ($use_exams == 'watu') { $todo_exam = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}watu_master WHERE ID=%d", $todo_exam)); $codesearch = "[WATU " . $todo_exam->ID . "]"; } if ($use_exams == 'watupro') { $todo_exam = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}watupro_master WHERE ID=%d", $todo_exam)); $codesearch = "[WATUPRO " . $todo_exam->ID . "]"; } // find the post to match it to the exam $post = $wpdb->get_row("SELECT * FROM {$wpdb->posts} WHERE post_content LIKE '%{$codesearch}%' \n\t\t\t\tAND post_status='publish' AND post_title!='' ORDER BY post_date DESC"); $todo_exam->post_link = get_permalink(@$post->ID); } // admin approval? $todo_admin_approval = false; $lesson_completion = get_post_meta($lesson_id, 'namaste_completion', true); if (is_array($lesson_completion) and in_array('admin_approval', $lesson_completion)) { $todo_admin_approval = true; } // namaste-mark button? $todo_mark = false; $lesson = get_post($lesson_id); if (strstr($lesson->post_content, '[namaste-mark')) { $todo_mark = true; } $nothing = false; if (empty($todo_homeworks) and empty($todo_exam) and empty($todo_admin_approval) and empty($todo_mark)) { $nothing = true; } // return todo return array("todo_homeworks" => $todo_homeworks, "todo_exam" => $todo_exam, "todo_admin_approval" => $todo_admin_approval, "todo_mark" => $todo_mark, "todo_nothing" => $nothing); }
function completeness($lesson, $student_id, $homeworks, $solutions) { if ($lesson->lesson_status == 1) { return 100; } $completeness = $completeness_parts = 0; $lesson_completion = get_post_meta($lesson->ID, 'namaste_completion', true); if (!is_array($lesson_completion)) { $lesson_completion = array(); } $required_homeworks = get_post_meta($lesson->ID, 'namaste_required_homeworks', true); if (!is_array($required_homeworks)) { $required_homeworks = array(); } $required_exam = get_post_meta($lesson->ID, 'namaste_required_exam', true); if (in_array('admin_approval', $lesson_completion)) { $completeness_parts += 1; } if (!empty($required_exam)) { $completeness_parts += 1; } if (sizeof($required_homeworks)) { $completeness_parts += 2; } // when there are no requirements at all but the lesson is not completed, return 0 if (!$completeness_parts) { return 0; } // if required exam, let's see whether it's completed if (NamasteLMSLessonModel::todo_exam($lesson->ID, $student_id, 'boolean')) { $completeness += 1; } // if required homeworks, let's see what part of them are completed so we can calculate part of 2 if (sizeof($required_homeworks)) { $num_required = $num_completed = 0; foreach ($homeworks as $homework) { if ($homework->lesson_id == $lesson->ID and in_array($homework->id, $required_homeworks)) { $num_required++; foreach ($solutions as $solution) { if ($solution->homework_id == $homework->id and $solution->status == 'approved') { $num_completed++; break; } } // end foreach solution } // end if match } // end foreach homework if ($num_required) { $homework_completeness = round($num_completed / $num_required * 2, 2); } else { $homework_completeness = 0; } $completeness += $homework_completeness; } // end if sizeof homeworks // now calculate completeness in % $comp_perc = round(100 * ($completeness / $completeness_parts)); return $comp_perc; }