Example #1
0
 /**
  * Edit course entry.
  */
 public static function edit_entry()
 {
     $entry_id = isset($_GET['entry_id']) ? absint($_GET['entry_id']) : 0;
     $entry = edr_get_entry($entry_id);
     if (count($_POST)) {
         // Verify nonce.
         check_admin_referer('ib_educator_edit_entry_' . $entry_id);
         $errors = new WP_Error();
         $api = IB_Educator::get_instance();
         $current_user_id = get_current_user_id();
         $who = '';
         // Check capabilities.
         if (current_user_can('manage_educator')) {
             $who = 'admin';
         } elseif ($entry->course_id && current_user_can('edit_ib_educator_course', $entry->course_id)) {
             $who = 'lecturer';
         }
         if (empty($who)) {
             return;
         }
         // Payment ID.
         if ('admin' == $who && isset($_POST['payment_id'])) {
             if (empty($_POST['payment_id'])) {
                 $entry->payment_id = 0;
             } else {
                 $payment = edr_get_payment($_POST['payment_id']);
                 if ($payment->ID) {
                     $entry->payment_id = $payment->ID;
                 }
             }
         }
         // Origin.
         if ('admin' == $who && isset($_POST['entry_origin']) && array_key_exists($_POST['entry_origin'], edr_get_entry_origins())) {
             $entry->entry_origin = $_POST['entry_origin'];
         }
         // Membership ID.
         if ('admin' == $who && isset($_POST['membership_id']) && 'membership' == $entry->entry_origin) {
             $entry->object_id = intval($_POST['membership_id']);
         }
         // Student ID.
         if ('admin' == $who && isset($_POST['student_id'])) {
             if (!empty($_POST['student_id'])) {
                 $entry->user_id = intval($_POST['student_id']);
             } else {
                 $errors->add('no_student', __('Please select a student.', 'ibeducator'));
             }
         }
         // Course ID.
         if ('admin' == $who && isset($_POST['course_id'])) {
             if (!empty($_POST['course_id'])) {
                 $entry->course_id = intval($_POST['course_id']);
             } else {
                 $errors->add('no_course', __('Please select a course.', 'ibeducator'));
             }
         }
         // Entry status.
         $prev_status = '';
         if (isset($_POST['entry_status']) && array_key_exists($_POST['entry_status'], edr_get_entry_statuses())) {
             if ($entry->ID && $entry->entry_status != $_POST['entry_status']) {
                 $prev_status = $entry->entry_status;
             }
             $entry->entry_status = $_POST['entry_status'];
         }
         // Grade.
         if (isset($_POST['grade']) && is_numeric($_POST['grade'])) {
             $entry->grade = $_POST['grade'];
         }
         // Entry date.
         if ('admin' == $who) {
             if (isset($_POST['entry_date'])) {
                 $entry->entry_date = sanitize_text_field($_POST['entry_date']);
             } elseif (empty($entry->entry_date)) {
                 $entry->entry_date = date('Y-m-d H:i:s');
             }
         }
         // Check the course prerequisites.
         if (!isset($_POST['ignore_prerequisites']) && !$api->check_prerequisites($entry->course_id, $entry->user_id)) {
             $prerequisites_html = '';
             $prerequisites = $api->get_prerequisites($entry->course_id);
             $courses = get_posts(array('post_type' => 'ib_educator_course', 'post_status' => 'publish', 'include' => $prerequisites));
             if (!empty($courses)) {
                 foreach ($courses as $course) {
                     $prerequisites_html .= '<br><a href="' . esc_url(get_permalink($course->ID)) . '">' . esc_html($course->post_title) . '</a>';
                 }
             }
             $errors->add('prerequisites', sprintf(__('You have to complete the prerequisites for this course: %s', 'ibeducator'), $prerequisites_html));
         }
         if ($errors->get_error_code()) {
             ib_edu_message('edit_entry_errors', $errors);
         } elseif ($entry->save()) {
             if ($prev_status) {
                 /**
                  * Do something on entry status change.
                  *
                  * @param IB_Educator_Entry $entry
                  * @param string $prev_status
                  */
                 do_action('edr_entry_status_change', $entry, $prev_status);
             }
             wp_redirect(admin_url('admin.php?page=ib_educator_entries&edu-action=edit-entry&entry_id=' . $entry->ID . '&edu-message=saved'));
             exit;
         }
     }
 }
Example #2
0
}
$entry_id = isset($_GET['entry_id']) ? absint($_GET['entry_id']) : 0;
$entry = edr_get_entry($entry_id);
$who = '';
if (current_user_can('manage_educator')) {
    $who = 'admin';
} elseif ($entry->course_id && current_user_can('edit_ib_educator_course', $entry->course_id)) {
    $who = 'lecturer';
}
// Check capabilities.
if (empty($who)) {
    // Current user cannot create entries.
    echo '<p>' . __('Access denied', 'ibeducator') . '</p>';
    return;
}
$statuses = edr_get_entry_statuses();
$origins = edr_get_entry_origins();
$student = null;
$course = null;
$input = array('payment_id' => isset($_POST['payment_id']) ? $_POST['payment_id'] : $entry->payment_id, 'membership_id' => isset($_POST['membership_id']) ? $_POST['membership_id'] : $entry->object_id, 'entry_origin' => isset($_POST['entry_origin']) ? $_POST['entry_origin'] : $entry->entry_origin, 'entry_status' => isset($_POST['entry_status']) ? $_POST['entry_status'] : $entry->entry_status, 'grade' => isset($_POST['grade']) ? $_POST['grade'] : $entry->grade, 'entry_date' => isset($_POST['entry_date']) ? $_POST['entry_date'] : (!empty($entry->entry_date) ? $entry->entry_date : date('Y-m-d H:i:s')));
if ('admin' == $who && isset($_POST['student_id'])) {
    $student = get_user_by('id', $_POST['student_id']);
} elseif ($entry->ID) {
    $student = get_user_by('id', $entry->user_id);
}
if ('admin' == $who && isset($_POST['course_id'])) {
    $course = get_post($_POST['course_id']);
} elseif ($entry->ID) {
    $course = get_post($entry->course_id);
}
?>
Example #3
0
 /**
  * Prepare items.
  * Fetch and setup entries(items).
  */
 public function prepare_items()
 {
     $this->_column_headers = array($this->get_columns(), array(), $this->get_sortable_columns());
     $entries = null;
     $api = IB_Educator::get_instance();
     $statuses = edr_get_entry_statuses();
     $args = array('per_page' => $this->get_items_per_page('entries_per_page', 10), 'page' => $this->get_pagenum());
     /**
      * Search by status.
      */
     if (!empty($_GET['status']) && array_key_exists($_GET['status'], $statuses)) {
         $args['entry_status'] = $_GET['status'];
     }
     // Search by ID.
     if (!empty($_GET['id'])) {
         $args['entry_id'] = $_GET['id'];
     }
     // Search by course id.
     if (!empty($_GET['course_id'])) {
         $args['course_id'] = $_GET['course_id'];
     }
     if (!empty($_GET['student'])) {
         $user = get_user_by('slug', $_GET['student']);
         if ($user) {
             $args['user_id'] = $user->ID;
         }
     }
     // Check capabilities.
     if (current_user_can('manage_educator')) {
         // Get all entries.
         $entries = $api->get_entries($args, 'ARRAY_A');
     } elseif (current_user_can('educator_edit_entries')) {
         // Get the entries for the current lecturer's courses only.
         $course_ids = $api->get_lecturer_courses(get_current_user_id());
         if (!empty($course_ids)) {
             if (empty($args['course_id']) || !in_array($args['course_id'], $course_ids)) {
                 $args['course_id'] = $course_ids;
             }
             $entries = $api->get_entries($args, 'ARRAY_A');
         }
     }
     if (!empty($entries)) {
         $this->set_pagination_args(array('total_items' => $entries['num_items'], 'per_page' => $args['per_page']));
         $this->items = $entries['rows'];
     }
 }
Example #4
0
 /**
  * Get available statuses.
  *
  * @return array
  */
 public static function get_statuses()
 {
     _ib_edu_deprecated_function('IB_Educator_Entry::get_statuses', '1.7', 'edr_get_entry_statuses');
     return edr_get_entry_statuses();
 }