/** * Testing Sensei_Core_Modules::get_term_author */ public function testGetTermAuthor() { // setup assertions $test_user_id = wp_create_user('teacherGetTermAuthor', 'teacherGetTermAuthor', '*****@*****.**'); //insert a general term wp_insert_term('Get Started', 'module'); //insert a term as if from the user wp_insert_term('Get Started Today', 'module', array('description' => 'A yummy apple.', 'slug' => $test_user_id . '-get-started-today')); // does the function exist? $this->assertTrue(method_exists('Sensei_Core_Modules', 'get_term_authors'), 'The function Sensei_Core_Modules::get_term_author does not exist '); // does the taxonomy exist $module_taxonomy = get_taxonomy('module'); $this->assertTrue($module_taxonomy->public, 'The module taxonomy is not loaded'); // does it return empty array id for bogus term nam? $term_authors = Sensei_Core_Modules::get_term_authors('bogusnonexistan'); $this->assertTrue(empty($term_authors), 'The function should return false for an invalid term'); //does it return the admin user for a valid term ? $admin = get_user_by('email', get_bloginfo('admin_email')); $term_authors = Sensei_Core_Modules::get_term_authors('Get Started'); $this->assertTrue($admin == $term_authors[0], 'The function should return admin user for normal module term.'); // does it return the expected new user for the given term registered with that id in front of the slug? $term_authors = Sensei_Core_Modules::get_term_authors('Get Started Today'); $this->assertTrue(get_userdata($test_user_id) == $term_authors[0], 'The function should admin user for normal module term.'); // what about terms with the same name but different slug? // It should return 2 authors as we've created 2 with the same name // insert a term that is the same as the first one wp_insert_term('Get Started', 'module', array('description' => 'A yummy apple.', 'slug' => $test_user_id . '-get-started')); $term_authors = Sensei_Core_Modules::get_term_authors('Get Started'); $this->assertTrue(2 == count($term_authors), 'The function should admin user for normal module term.'); }
public function calculate_user_module_progress_custom($user_id, $module_id, $course_id) { $lessons = Sensei_Core_Modules::get_lessons($course_id, $module_id); //if (is_wp_error($lessons) || 0 >= count($lessons)) return 0; $completed = false; $lesson_count = 0; $completed_count = 0; $strcom = ''; foreach ($lessons as $lesson) { $lesson_id = $lesson->ID; $lesson_id = (int) $lesson_id; $quiz_id = WooThemes_Sensei_Lesson::lesson_quizzes($lesson_id, $post_status = 'any'); $quiz_id = (int) $quiz_id; $lesson_quiz_questions = WooThemes_Sensei_Utils::sensei_get_quiz_questions($quiz_id); if (count($lesson_quiz_questions) > 0) { $lesson_status = WooThemes_Sensei_Utils::user_lesson_status($lesson_id, $user_id); $lesson_status = $lesson_status->comment_approved; ++$lesson_count; if ($lesson_status == 'passed') { ++$completed_count; } else { $question = $lesson_quiz_questions[0]; $question_title = $question->post_title; if ($question_title == 'Offline Upload') { ++$completed_count; } //$strcom = $strcom.' -- Lesson ID: '.$lesson_id.' '.print_r($lesson_quiz_questions, true); } } //$strcom = $strcom.' -- '.$user_id.' '.$lesson_id.' '.$lesson_status. ' '.print_r($lesson_quizzes, true); } $module_progress = $completed_count / $lesson_count * 100; return (double) $module_progress; //return $strcom; }
/** * Add the teacher name next to modules. Only works in Admin for Admin users. * This will not add name to terms belonging to admin user. * * Hooked into 'get_terms' * * @since 1.8.0 */ public function append_teacher_name_to_module($terms, $taxonomies, $args) { // only for admin users ont he module taxonomy if (empty($terms) || !current_user_can('manage_options') || !in_array('module', $taxonomies) || !is_admin()) { return $terms; } // in certain cases the array is passed in as reference to the parent term_id => parent_id // simply return this as wp doesn't need an array of stdObject Term if (isset($args['fields']) && 'id=>parent' == $args['fields']) { return $terms; } // loop through and update all ters adding the author name foreach ($terms as $index => $term) { if (is_numeric($term)) { // the term id was given, get the term object $term = get_term($term, 'module'); } $author = Sensei_Core_Modules::get_term_author($term->slug); if (!user_can($author, 'manage_options')) { $term->name = $term->name . ' (' . $author->display_name . ') '; } // add the term to the teachers terms $users_terms[] = $term; } return $users_terms; }
/** * Update all the course terms set(selected) on the given course. Moving course term ownership to * the new author. Making sure the course terms are maintained. * * This function also checks if terms are shared, with other courses * * @param $course_id * @param $new_teacher_id * @return void */ public static function update_course_modules_author($course_id, $new_teacher_id) { if (empty($course_id) || empty($new_teacher_id)) { return false; } $terms_selected_on_course = wp_get_object_terms($course_id, 'module'); if (empty($terms_selected_on_course)) { return; } foreach ($terms_selected_on_course as $term) { $term_author = Sensei_Core_Modules::get_term_author($term->slug); if ($new_teacher_id != $term_author->ID) { $new_term = ''; //if the new teacher is admin first check to see if the term with this name already exists if (user_can($new_teacher_id, 'manage_options')) { $slug_without_teacher_id = str_ireplace(' ', '-', trim($term->name)); $term_args = array('slug' => $slug_without_teacher_id, 'hide_empty' => false); $existing_admin_terms = get_terms('module', $term_args); if (!empty($existing_admin_terms)) { // insert it even if it exists $new_term = get_term($existing_admin_terms[0]->term_id, 'module', ARRAY_A); } } if (empty($new_term)) { //setup the new slug $new_author_term_slug = $new_teacher_id . '-' . str_ireplace(' ', '-', trim($term->name)); // create new term and set it $new_term = wp_insert_term($term->name, 'module', array('slug' => $new_author_term_slug)); } // if term exists if (is_wp_error($new_term) && isset($new_term->errors['term_exists'])) { $existing_term = get_term_by('slug', $new_author_term_slug, 'module'); $term_id = $existing_term->term_id; } else { // for a new term simply get the term from the returned value $term_id = $new_term['term_id']; } // end if term exist // set the terms selected on the course wp_set_object_terms($course_id, $term_id, 'module', true); // remove old term wp_remove_object_terms($course_id, $term->term_id, 'module'); // update the lessons within the current module term $lessons = Sensei()->course->course_lessons($course_id); foreach ($lessons as $lesson) { if (has_term($term->slug, 'module', $lesson)) { // add the new term, the false at the end says to replace all terms on this module // with the new term. wp_set_object_terms($lesson->ID, $term_id, 'module', false); update_post_meta($lesson->ID, '_order_module_' . intval($term_id), 0); } } // end for each } } }
/** * Testing Sensei_Teacher::update_course_modules_author * This test focus on changing module author * * @since 1.8.0 */ public function testUpdateCourseModulesAuthorChange() { // setup assertions $test_teacher_id = wp_create_user('teacherCourseModulesAuthor', 'teacherCourseModulesAuthor', '*****@*****.**'); // create test course with current admin as owner $test_course_id = $this->factory->get_random_course_id(); $administrator = get_user_by('email', get_bloginfo('admin_email')); wp_update_post(array('ID' => $test_course_id, 'post_author' => $administrator->ID)); //insert sample module terms $term_start = wp_insert_term('Sample Test Start', 'module'); $term_end = wp_insert_term('Sample Test End', 'module'); // assign sample terms to course wp_set_object_terms($test_course_id, array($term_start['term_id'], $term_end['term_id']), 'module', true); // run the function passing in new teacher Sensei_Teacher::update_course_modules_author($test_course_id, $test_teacher_id); // set the current active user to be the teacher so that get object terms // only return the teachers terms $current_user = get_current_user_id(); wp_set_current_user($test_teacher_id); // check the if the object terms have change to the new new user within the slug $updated_module_terms = wp_get_object_terms($test_course_id, 'module'); $assert_message = 'Course module term authors not updated.'; foreach ($updated_module_terms as $term) { // skip $term_start and $term_end if ($term_start['term_id'] == $term->term_id || $term_end['term_id'] == $term->term_id) { continue; } $updated_author = Sensei_Core_Modules::get_term_author($term->slug); $this->assertEquals($test_teacher_id, $updated_author->ID, $assert_message); } // modules should be removed from the course foreach ($updated_module_terms as $term) { // skip $term_start and $term_end $this->assertFalse($term_start['term_id'] == $term->term_id || $term_end['term_id'] == $term->term_id, 'The old modules should no longer be on the course'); } // reset current user for other tests wp_set_current_user($current_user); //when the lessons are moved back to admin they should be duplciated // first clear all the object term on the test course. $terms = wp_get_object_terms($test_course_id, 'module'); foreach ($terms as $term) { wp_remove_object_terms($test_course_id, array($term->term_id), 'module'); } $admin_module = wp_insert_term('Admin Test Module', 'module'); wp_set_object_terms($test_course_id, array($admin_module['term_id']), 'module', true); Sensei_Teacher::update_course_modules_author($test_course_id, $administrator->ID); // move to teacher and then back to admin Sensei_Teacher::update_course_modules_author($test_course_id, $test_teacher_id); Sensei_Teacher::update_course_modules_author($test_course_id, $administrator->ID); // after the update this course should still only have one module as course should not be duplicated for admin $admin_term_after_multiple_updates = wp_get_object_terms($test_course_id, 'module'); $message = 'A new admin term with slug {adminID}-slug should not have been created. The admin term should not be duplicated when passed back to admin'; $this->assertFalse(strpos($admin_term_after_multiple_updates[0]->slug, (string) $administrator->ID), $message); }
/** * Add data for our newly-added custom columns. * @access public * @since 1.0.0 * @param string $column_name * @param int $id * @return void */ public function add_column_data($column_name, $id) { global $wpdb, $post; switch ($column_name) { case 'id': echo $id; break; case 'lesson-course': $lesson_course_id = get_post_meta($id, '_lesson_course', true); if (0 < absint($lesson_course_id)) { echo '<a href="' . esc_url(get_edit_post_link(absint($lesson_course_id))) . '" title="' . esc_attr(sprintf(__('Edit %s', 'woothemes-sensei'), get_the_title(absint($lesson_course_id)))) . '">' . get_the_title(absint($lesson_course_id)) . '</a>'; } // End If Statement break; case 'lesson-prerequisite': $lesson_prerequisite_id = get_post_meta($id, '_lesson_prerequisite', true); if (0 < absint($lesson_prerequisite_id)) { echo '<a href="' . esc_url(get_edit_post_link(absint($lesson_prerequisite_id))) . '" title="' . esc_attr(sprintf(__('Edit %s', 'woothemes-sensei'), get_the_title(absint($lesson_prerequisite_id)))) . '">' . get_the_title(absint($lesson_prerequisite_id)) . '</a>'; } // End If Statement break; case 'lesson-modules': //echo '<ul>'; $modules = new Sensei_Core_Modules(); $module = $modules->get_lesson_module($id); if ($module) { echo substr($module->name, 0, 50) . '...'; } //echo '</ul>'; break; default: break; } // End Switch Statement }