/**
  * Sensei_Teacher::save_teacher_meta_box
  *
  * Save the new teacher / author to course and all lessons
  *
  * Hooked into admin_init
  *
  * @since 1.8.0
  * @access public
  * @parameters
  * @return array $users user id array
  */
 public function save_teacher_meta_box($course_id)
 {
     // check if this is a post from saving the teacher, if not exit early
     if (!isset($_POST['sensei-course-teacher-author']) || !isset($_POST['post_ID'])) {
         return;
     }
     //don't fire this hook again
     remove_action('save_post', array($this, 'save_teacher_meta_box'));
     // get the current post object
     $post = get_post($course_id);
     // get the current teacher/author
     $current_author = absint($post->post_author);
     $new_author = absint($_POST['sensei-course-teacher-author']);
     // loop through all post lessons to update their authors as well
     $this->update_course_lessons_author($course_id, $new_author);
     // do not do any processing if the selected author is the same as the current author
     if ($current_author == $new_author) {
         return;
     }
     // save the course  author
     $post_updates = array('ID' => $post->ID, 'post_author' => $new_author);
     wp_update_post($post_updates);
     // ensure the the modules are update so that then new teacher has access to them
     Sensei_Teacher::update_course_modules_author($course_id, $new_author);
     // notify the new teacher
     $this->teacher_course_assigned_notification($new_author, $course_id);
 }
Esempio n. 2
0
 /**
  * Testing Sensei_Teacher::update_course_modules_author
  * Test to see if the lessons in the course was assigned to
  * a new author.
  *
  * @since 1.8.0
  */
 public function testUpdateCourseModulesAuthorChangeLessons()
 {
     // setup assertions
     $test_teacher_id = wp_create_user('teacherCourseModulesAuthorLessons', 'teacherCourseModulesAuthorLessons', '*****@*****.**');
     // 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
     $test_module_1 = wp_insert_term('Lesson Test Module', 'module');
     $test_module_2 = wp_insert_term('Lesson Test Module 2', 'module');
     // assign sample terms to course
     wp_set_object_terms($test_course_id, array($test_module_1['term_id'], $test_module_2['term_id']), 'module', true);
     // add sample lessons to course and assign them to modules
     $test_lessons = $this->factory->get_lessons();
     foreach ($test_lessons as $lesson_id) {
         update_post_meta($lesson_id, '_lesson_course', intval($test_course_id));
     }
     // split array in 2 and assign each group of lessons to one of the modules
     $array_middle = round((count($test_lessons) + 1) / 2);
     $lesson_in_module_1 = array_slice($test_lessons, 0, $array_middle);
     $lesson_in_module_2 = array_slice($test_lessons, $array_middle);
     // assign lessons to module 1
     foreach ($lesson_in_module_1 as $lesson_id) {
         wp_set_object_terms($lesson_id, $test_module_1['term_id'], 'module', false);
     }
     // assign lessons to module 2
     foreach ($lesson_in_module_2 as $lesson_id) {
         wp_set_object_terms($lesson_id, $test_module_2['term_id'], 'module', false);
     }
     // Do the update changing the author
     Sensei_Teacher::update_course_modules_author($test_course_id, $test_teacher_id);
     // check each lesson
     // do the lessons for module 1 group now belong to ta new module term with the new teacher as owner?
     $expected_module_1_slug = $test_teacher_id . '-' . str_ireplace(' ', '-', strtolower('Lesson Test Module'));
     foreach ($lesson_in_module_1 as $lesson_id) {
         $term_after_update = wp_get_object_terms($lesson_id, 'module');
         $this->assertEquals($expected_module_1_slug, $term_after_update[0]->slug, 'Lesson module was not updated, ID: ' . $lesson_id);
     }
     // do the lessons for module 2 group now belong to ta new module term with the new teacher as owner?
     $expected_module_2_slug = $test_teacher_id . '-' . str_ireplace(' ', '-', strtolower(trim('Lesson Test Module 2')));
     foreach ($lesson_in_module_2 as $lesson_id) {
         $term_after_update = wp_get_object_terms($lesson_id, 'module');
         $this->assertEquals($expected_module_2_slug, $term_after_update[0]->slug, 'Lesson module was not updated, ID: ' . $lesson_id);
     }
 }