public function process($departmentid, $roletype)
 {
     global $DB, $USER;
     $companymanagerrole = $DB->get_record('role', array('shortname' => 'companymanager'));
     $departmentmanagerrole = $DB->get_record('role', array('shortname' => 'companydepartmentmanager'));
     $companycoursenoneditorrole = $DB->get_record('role', array('shortname' => 'companycoursenoneditor'));
     $companycourseeditorrole = $DB->get_record('role', array('shortname' => 'companycourseeditor'));
     // Process incoming assignments.
     if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
         $userstoassign = $this->potentialusers->get_selected_users();
         if (!empty($userstoassign)) {
             foreach ($userstoassign as $adduser) {
                 $allow = true;
                 // GWL : Check the userid is valid.
                 if (!company::check_valid_user($this->selectedcompany, $adduser->id, $this->departmentid)) {
                     print_error('invaliduserdepartment', 'block_iomad_company_management');
                 }
                 if ($allow) {
                     if ($roletype != 0) {
                         // Adding a manager type.
                         // Add user to the company manager table.
                         if ($userrecord = $DB->get_record('company_users', array('companyid' => $this->selectedcompany, 'userid' => $adduser->id))) {
                             if ($roletype == 1 && $userrecord->managertype == 0) {
                                 // Give them the company manager role.
                                 role_assign($companymanagerrole->id, $adduser->id, $this->context->id);
                                 // Deal with company courses.
                                 if ($companycourses = $DB->get_records('company_course', array('companyid' => $this->selectedcompany))) {
                                     foreach ($companycourses as $companycourse) {
                                         if ($DB->record_exists('course', array('id' => $companycourse->courseid))) {
                                             if ($DB->record_exists('company_created_courses', array('companyid' => $companycourse->companyid, 'courseid' => $companycourse->courseid))) {
                                                 company_user::enrol($adduser, array($companycourse->courseid), $companycourse->companyid, $companycourseeditorrole->id);
                                             } else {
                                                 company_user::enrol($adduser, array($companycourse->courseid), $companycourse->companyid, $companycoursenoneditorrole->id);
                                             }
                                         }
                                     }
                                 }
                             } else {
                                 if ($roletype == 2 && $userrecord->managertype == 0) {
                                     // Give them the department manager role.
                                     role_assign($departmentmanagerrole->id, $adduser->id, $this->context->id);
                                     // Deal with company courses.
                                     if ($companycourses = $DB->get_records('company_course', array('companyid' => $this->selectedcompany))) {
                                         foreach ($companycourses as $companycourse) {
                                             if ($DB->record_exists('course', array('id' => $companycourse->courseid))) {
                                                 company_user::enrol($adduser, array($companycourse->courseid), $companycourse->companyid, $companycoursenoneditorrole->id);
                                             }
                                         }
                                     }
                                 } else {
                                     if ($roletype == 1 && ($userrecord->managertype = 2)) {
                                         // Give them the department manager role.
                                         role_unassign($departmentmanagerrole->id, $adduser->id, $this->context->id);
                                         role_assign($companymanagerrole->id, $adduser->id, $this->context->id);
                                         // Deal with course permissions.
                                         if ($companycourses = $DB->get_records('company_course', array('companyid' => $this->selectedcompany))) {
                                             foreach ($companycourses as $companycourse) {
                                                 if ($DB->record_exists('course', array('id' => $companycourse->courseid))) {
                                                     // If its a company created course then assign the editor role to the user.
                                                     if ($DB->record_exists('company_created_courses', array('companyid' => $this->selectedcompany, 'courseid' => $companycourse->courseid))) {
                                                         company_user::unenrol($adduser, array($companycourse->courseid), $companycourse->companyid);
                                                         company_user::enrol($adduser, array($companycourse->courseid), $companycourse->companyid, $companycourseeditorrole->id);
                                                     } else {
                                                         company_user::enrol($adduser, array($companycourse->courseid), $companycourse->companyid, $companycoursenoneditorrole->id);
                                                     }
                                                 }
                                             }
                                         }
                                     } else {
                                         if ($roletype == 2 && ($userrecord->managertype = 1)) {
                                             // Give them the department manager role.
                                             role_unassign($companymanagerrole->id, $adduser->id, $this->context->id);
                                             role_assign($departmentmanagerrole->id, $adduser->id, $this->context->id);
                                             // Deal with company course roles.
                                             if ($companycourses = $DB->get_records('company_course', array('companyid' => $this->selectedcompany))) {
                                                 foreach ($companycourses as $companycourse) {
                                                     if ($DB->record_exists('course', array('id' => $companycourse->courseid))) {
                                                         company_user::unenrol($adduser, array($companycourse->courseid), $companycourse->companyid);
                                                         company_user::enrol($adduser, array($companycourse->courseid), $companycourse->companyid, $companycoursenoneditorrole->id);
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                             $userrecord->managertype = $roletype;
                             $DB->update_record('company_users', $userrecord);
                         } else {
                             if ($roletype == 1 && $DB->get_records_sql('SELECT id FROM {company_users}
                                                         WHERE
                                                         userid = :userid
                                                         AND managertype = :roletype
                                                         AND companyid != :companyid', array('userid' => $adduser->id, 'roletype' => 1, 'companyid' => $this->selectedcompany))) {
                                 // We have a company manager from another company.
                                 // We need to add another record.
                                 $company = new company($this->selectedcompany);
                                 $company->assign_user_to_company($adduser->id);
                                 $DB->set_field('company_users', 'managertype', 1, array('userid' => $adduser->id, 'companyid' => $this->selectedcompany));
                                 // Deal with company courses.
                                 if ($companycourses = $DB->get_records('company_course', array('companyid' => $this->selectedcompany))) {
                                     foreach ($companycourses as $companycourse) {
                                         if ($DB->record_exists('course', array('id' => $companycourse->courseid))) {
                                             if ($DB->record_exists('company_created_courses', array('companyid' => $companycourse->companyid, 'courseid' => $companycourse->courseid))) {
                                                 company_user::enrol($adduser, array($companycourse->courseid), $companycourse->companyid, $companycourseeditorrole->id);
                                             } else {
                                                 company_user::enrol($adduser, array($companycourse->courseid), $companycourse->companyid, $companycoursenoneditorrole->id);
                                             }
                                         }
                                     }
                                 }
                             } else {
                                 // Assign the user to department as staff.
                                 company::assign_user_to_department($departmentid, $adduser->id);
                             }
                         }
                     } else {
                         // Assign the user to department as staff.
                         company::assign_user_to_department($departmentid, $adduser->id);
                     }
                 }
             }
             $this->potentialusers->invalidate_selected_users();
             $this->currentusers->invalidate_selected_users();
         }
     }
     // Process incoming unassignments.
     if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
         $userstounassign = $this->currentusers->get_selected_users();
         if (!empty($userstounassign)) {
             // Check if we are mearly removing the manager role.
             if ($roletype != 0) {
                 foreach ($userstounassign as $removeuser) {
                     // GWL : Check the userid is valid.
                     if (!company::check_valid_user($this->selectedcompany, $adduser->id, $this->departmentid)) {
                         print_error('invaliduserdepartment', 'block_iomad_company_management');
                     }
                     $userrecord = $DB->get_record('company_users', array('companyid' => $this->selectedcompany, 'userid' => $removeuser->id));
                     // Is this a manager from another company?
                     if ($DB->get_records_sql("SELECT id FROM {company_users}\n                                                  WHERE userid = :userid\n                                                  AND companyid != :companyid\n                                                  AND managertype = 1", array('userid' => $removeuser->id, 'companyid' => $this->selectedcompany))) {
                         // Remove the user from this company.
                         $DB->delete_records('company_users', (array) $userrecord);
                     } else {
                         // Remove the manager status from the user.
                         $userrecord->managertype = 0;
                         $DB->update_record('company_users', $userrecord);
                         role_unassign($companymanagerrole->id, $removeuser->id, $this->context->id);
                         role_unassign($departmentmanagerrole->id, $removeuser->id, $this->context->id);
                     }
                     // Remove their capabilities from the company courses.
                     if ($companycourses = $DB->get_records('company_course', array('companyid' => $this->selectedcompany))) {
                         foreach ($companycourses as $companycourse) {
                             if ($DB->record_exists('course', array('id' => $companycourse->courseid))) {
                                 company_user::unenrol($removeuser, array($companycourse->courseid), $companycourse->companyid);
                             }
                         }
                     }
                 }
             } else {
                 foreach ($userstounassign as $removeuser) {
                     // GWL : Check the userid is valid.
                     if (!company::check_valid_user($this->selectedcompany, $removeuser->id, $this->departmentid)) {
                         print_error('invaliduserdepartment', 'block_iomad_company_management');
                     }
                     // Assign the user to parent department as staff.
                     company::assign_user_to_department($this->companydepartment, $removeuser->id);
                 }
             }
             $this->potentialusers->invalidate_selected_users();
             $this->currentusers->invalidate_selected_users();
         }
     }
 }
 public function process()
 {
     global $DB, $CFG;
     $this->create_user_selectors();
     // Process incoming enrolments.
     if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
         $userstoassign = $this->potentialusers->get_selected_users();
         if (!empty($userstoassign)) {
             foreach ($userstoassign as $adduser) {
                 $allow = true;
                 // GWL : Check the userid is valid.
                 if (!company::check_valid_user($this->selectedcompany, $adduser->id, $this->departmentid)) {
                     print_error('invaliduserdepartment', 'block_iomad_company_management');
                 }
                 if ($allow) {
                     company_user::enrol($adduser, array($this->course->id), $this->selectedcompany);
                     EmailTemplate::send('user_added_to_course', array('course' => $this->course, 'user' => $adduser));
                 }
             }
             $this->potentialusers->invalidate_selected_users();
             $this->currentusers->invalidate_selected_users();
         }
     }
     // Process incoming unenrolments.
     if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
         $userstounassign = $this->currentusers->get_selected_users();
         if (!empty($userstounassign)) {
             foreach ($userstounassign as $removeuser) {
                 // GWL : Check the userid is valid.
                 if (!company::check_valid_user($this->selectedcompany, $removeuser->id, $this->departmentid)) {
                     print_error('invaliduserdepartment', 'block_iomad_company_management');
                 }
                 company_user::unenrol($removeuser, array($this->course->id), $this->selectedcompany);
             }
             $this->potentialusers->invalidate_selected_users();
             $this->currentusers->invalidate_selected_users();
         }
     }
 }
Esempio n. 3
0
function unassign_courses_to_user($department, $title, $companyid, $courses, $userid = null)
{
    global $DB;
    $whereconditon = array('department' => $department, 'title' => $title, 'company' => $companyid);
    $companycourses = $DB->get_records('local_dept_title_courses', $whereconditon);
    if ($courses) {
        if (!is_null($userid)) {
            // get all users match with provided department & title
            $whereconditon['user'] = $userid;
        }
        $userorusers = $DB->get_records('local_dept_title_user', $whereconditon);
        foreach ($userorusers as $companyuser) {
            $user = $DB->get_record('user', array('id' => $companyuser->user, 'deleted' => 0), '*', MUST_EXIST);
            company_user::unenrol($user, $courses, $companyid);
        }
    }
}
 public function process()
 {
     global $DB, $CFG;
     $this->create_course_selectors();
     // Process incoming enrolments.
     if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
         $coursestoassign = $this->potentialcourses->get_selected_courses();
         if (!empty($coursestoassign)) {
             foreach ($coursestoassign as $addcourse) {
                 $allow = true;
                 if ($allow) {
                     company_user::enrol($this->user, array($addcourse->id));
                     EmailTemplate::send('user_added_to_course', array('course' => $addcourse, 'user' => $this->user));
                 }
             }
             $this->potentialcourses->invalidate_selected_courses();
             $this->currentcourses->invalidate_selected_courses();
         }
     }
     // Process incoming unenrolments.
     if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
         $coursestounassign = $this->currentcourses->get_selected_courses();
         if (!empty($coursestounassign)) {
             foreach ($coursestounassign as $removecourse) {
                 company_user::unenrol($this->user, array($removecourse->id));
             }
             $this->potentialcourses->invalidate_selected_courses();
             $this->currentcourses->invalidate_selected_courses();
         }
     }
 }