Exemple #1
0
function assign_department_and_title_to_user($companyid, $departmentid = 0, $titleid = 0, $userid)
{
    global $DB;
    $transaction = $DB->start_delegated_transaction();
    if ($departmentid && !company_department::check_valid_department($companyid, $departmentid)) {
        print_error('Invaliddeparment', 'block_iomad_company_admin');
    }
    if ($titleid && !company_title::check_valid_title($companyid, $titleid)) {
        print_error('Invaliddeparment', 'block_iomad_company_admin');
    }
    $userrecord = array();
    $userrecord['department'] = $departmentid;
    $userrecord['user'] = $userid;
    $userrecord['company'] = $companyid;
    $userrecord['title'] = $titleid;
    if ($currentuser = $DB->get_record('local_dept_title_user', array('user' => $userid, 'company' => $companyid))) {
        remove_user_mapping_from_department_title($currentuser->department, $currentuser->title, $companyid, $userid);
        $userrecord['id'] = $currentuser->id;
        if (!$DB->update_record('local_dept_title_user', $userrecord)) {
            print_error(get_string('cantupdatedepartmentusersdb', 'block_iomad_company_admin'));
        }
        if ($coursesobj = get_department_title_courses($companyid, $departmentid, $titleid)) {
            $courses = array();
            foreach ($coursesobj as $deptcourseobj) {
                $courses[] = $deptcourseobj->course;
            }
            assign_courses_to_user($departmentid, $titleid, $companyid, $courses, $userid);
        }
        //return true;
    } else {
        if ($DB->insert_record('local_dept_title_user', $userrecord)) {
            if ($coursesobj = get_department_title_courses($companyid, $departmentid, $titleid)) {
                $courses = array();
                foreach ($coursesobj as $deptcourseobj) {
                    $courses[] = $deptcourseobj->course;
                }
                assign_courses_to_user($departmentid, $titleid, $companyid, $courses, $userid);
            }
        }
    }
    $transaction->allow_commit();
}
                // Check the title is valid.
                if (!empty($createdata->titleid) && !company_title::check_valid_title($companyid, $createdata->titleid)) {
                    print_error('invalidtitle', 'block_iomad_company_admin');
                }
                if (!empty($createdata->titleid) && !company_title::can_manage_title($createdata->titleid)) {
                    print_error('invalidtitle', 'block_iomad_company_admin');
                }
                company_title::create_title($createdata->titleid, $companyid, $createdata->fullname, $createdata->shortname);
            }
            $mform = new user_title_display_form(null, $companyid);
            // Redisplay the form.
            $blockpage->display_header();
            $mform->display();
            echo $OUTPUT->footer();
        } else {
            $blockpage->display_header();
            // Check the title is valid.
            if (!empty($titleid) && !company_title::check_valid_title($companyid, $titleid)) {
                print_error('invalidtitle', 'block_iomad_company_admin');
            }
            // Code by sumit 3/feb/2016
            if ($editform->is_submitted() && !$editform->is_cancelled()) {
                $editform->display();
            } else {
                $mform->display();
            }
            // End of code
            echo $OUTPUT->footer();
        }
    }
}