public function process()
 {
     global $DB;
     $context = context_system::instance();
     // Get process ok to unenroll confirmation.
     $oktounenroll = optional_param('oktounenroll', false, PARAM_BOOL);
     // Process incoming assignments.
     if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
         $coursestoassign = $this->potentialcourses->get_selected_courses();
         if (!empty($coursestoassign)) {
             $company = new company($this->selectedcompany);
             foreach ($coursestoassign as $addcourse) {
                 // Check if its a shared course.
                 if ($DB->get_record_sql("SELECT id FROM {iomad_courses}\n                                             WHERE courseid={$addcourse->id}\n                                             AND shared != 0")) {
                     if ($companycourserecord = $DB->get_record('company_course', array('companyid' => $this->selectedcompany, 'courseid' => $addcourse->id))) {
                         // Already assigned to the company so we are just moving it within it.
                         $companycourserecord->departmentid = $this->departmentid;
                         $DB->update_record('company_course', $companycourserecord);
                     } else {
                         $sharingrecord = new object();
                         $sharingrecord->courseid = $addcourse->id;
                         $sharingrecord->companyid = $company->id;
                         $DB->insert_record('company_shared_courses', $sharingrecord);
                         if ($this->departmentid != $this->companydepartment) {
                             $company->add_course($addcourse, $this->departmentid);
                         } else {
                             $company->add_course($addcourse, $this->companydepartment);
                         }
                     }
                 } else {
                     // If company has enrollment then we must have BOTH
                     // oktounenroll true and the company_course_unenrol capability.
                     if (!empty($addcourse->has_enrollments)) {
                         if (iomad::has_capability('block/iomad_company_admin:company_course_unenrol', $context) and $oktounenroll) {
                             $this->unenroll_all($addcourse->id);
                             $company->add_course($addcourse);
                         }
                     } else {
                         if ($companycourserecord = $DB->get_record('company_course', array('companyid' => $this->selectedcompany, 'courseid' => $addcourse->id))) {
                             $companycourserecord->departmentid = $this->departmentid;
                             $DB->update_record('company_course', $companycourserecord);
                         } else {
                             if ($this->departmentid != $this->companydepartment) {
                                 $company->add_course($addcourse, $this->departmentid);
                             } else {
                                 $company->add_course($addcourse, $this->companydepartment);
                             }
                         }
                     }
                 }
             }
             $this->potentialcourses->invalidate_selected_courses();
             $this->currentcourses->invalidate_selected_courses();
         }
     }
     // Process incoming unassignments.
     if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
         $coursestounassign = $this->currentcourses->get_selected_courses();
         if (!empty($coursestounassign)) {
             $company = new company($this->selectedcompany);
             foreach ($coursestounassign as $removecourse) {
                 // Check if its a shared course.
                 if ($DB->get_record_sql("SELECT id FROM {iomad_courses}\n                                             WHERE courseid=:removecourse\n                                             AND shared != 0", array('removecourse' => $removecourse->id))) {
                     $DB->delete_records('company_shared_courses', array('companyid' => $company->id, 'courseid' => $removecourse->id));
                     company::delete_company_course_group($company->id, $removecourse, $oktounenroll);
                 } else {
                     // If company has enrollment then we must have BOTH
                     // oktounenroll true and the company_course_unenrol capability.
                     if (!empty($removecourse->has_enrollments)) {
                         if (iomad::has_capability('block/iomad_company_admin:company_course_unenrol', $context) and $oktounenroll) {
                             $this->unenroll_all($removecourse->id);
                             if ($this->departmentid != $this->companydepartment) {
                                 // Dump it into the default company department.
                                 $company->remove_course($removecourse, $company->id, $this->companydepartment);
                             } else {
                                 // Remove it from the company.
                                 $company->remove_course($removecourse, $company->id);
                             }
                         }
                     } else {
                         if ($this->departmentid != $this->companydepartment) {
                             // Move the course to the company default department.
                             $company->remove_course($removecourse, $company->id, $this->companydepartment);
                         } else {
                             $company->remove_course($removecourse, $company->id);
                         }
                     }
                 }
             }
             $this->potentialcourses->invalidate_selected_courses();
             $this->currentcourses->invalidate_selected_courses();
         }
     }
 }