/** * 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 } } }
/** * 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; }
/** * 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); }