function onordercomplete($invoiceitem, $invoice)
 {
     global $DB;
     $transaction = $DB->start_delegated_transaction();
     if ($DB->get_record('iomad_courses', array('courseid' => $invoiceitem->invoiceableitemid, 'licensed' => 1))) {
         // Get the item's single purchase details.
         $courseinfo = $DB->get_record('course_shopsettings', array('courseid' => $invoiceitem->invoiceableitemid));
         // Get name for company license.
         $company = company::get_company_byuserid($invoice->userid);
         $course = $DB->get_record('course', array('id' => $invoiceitem->invoiceableitemid), 'id, shortname', MUST_EXIST);
         $licensename = $company->shortname . " [" . $course->shortname . "] " . date("Y-m-d");
         $count = $DB->count_records_sql("SELECT COUNT(*) FROM {companylicense} WHERE name LIKE '" . str_replace("'", "\\'", $licensename) . "%'");
         if ($count) {
             $licensename .= ' (' . ($count + 1) . ')';
         }
         // Create mdl_companylicense record.
         $companylicense = new stdClass();
         $companylicense->name = $licensename;
         $companylicense->allocation = 1;
         $companylicense->used = 1;
         $companylicense->validlength = $courseinfo->single_purchase_validlength;
         if (!empty($courseinfo->single_purchase_shelflife)) {
             $companylicense->expirydate = $courseinfo->single_purchase_shelflife * 86400 + time();
             // 86400 = 24*60*60 = number of seconds in a day.
         } else {
             $companylicense->expirydate = 0;
         }
         $companylicense->companyid = $company->id;
         $companylicenseid = $DB->insert_record('companylicense', $companylicense);
         // Create mdl_companylicense_courses record for the course.
         $clc = new stdClass();
         $clc->licenseid = $companylicenseid;
         $clc->courseid = $course->id;
         $DB->insert_record('companylicense_courses', $clc);
         //  Assign the license to the user.
         $DB->insert_record('companylicense_users', array('userid' => $invoice->userid, 'licenseid' => $companylicenseid));
     } else {
         // Enrol user into course.
         $user = new stdClass();
         $user->id = $invoice->userid;
         company_user::enrol($user, array($invoiceitem->invoiceableitemid));
     }
     // Mark the invoice item as processed.
     $invoiceitem->processed = 1;
     $DB->update_record('invoiceitem', $invoiceitem);
     $transaction->allow_commit();
 }
示例#2
0
 /**
  * Associates a course to a company
  *
  * Parameters -
  *              $course = stdclass();
  *              $departmentid = int;
  *              $own = boolean;
  *
  * */
 public function add_course($course, $departmentid = 0, $own = false, $licensed = false)
 {
     global $DB;
     if ($departmentid != 0) {
         // Adding to a specified department.
         $companydepartment = $departmentid;
     } else {
         // Put course in default company department.
         $companydepartmentnode = self::get_company_parentnode($this->id);
         $companydepartment = $companydepartmentnode->id;
     }
     if (!$DB->record_exists('company_course', array('companyid' => $this->id, 'courseid' => $course->id))) {
         $DB->insert_record('company_course', array('companyid' => $this->id, 'courseid' => $course->id, 'departmentid' => $companydepartment));
     }
     // Set up defaults for course management.
     if (!$DB->get_record('iomad_courses', array('courseid' => $course->id))) {
         $DB->insert_record('iomad_courses', array('courseid' => $course->id, 'licensed' => $licensed, 'shared' => 0));
     }
     // Set up manager roles.
     if (!$licensed) {
         if ($companymanagers = $DB->get_records_sql("SELECT * FROM {company_users}\n                                                         WHERE companyid = :companyid\n                                                         AND managertype != 0", array('companyid' => $this->id))) {
             $companycoursenoneditorrole = $DB->get_record('role', array('shortname' => 'companycoursenoneditor'));
             $companycourseeditorrole = $DB->get_record('role', array('shortname' => 'companycourseeditor'));
             foreach ($companymanagers as $companymanager) {
                 if ($user = $DB->get_record('user', array('id' => $companymanager->userid, 'deleted' => 0))) {
                     if ($DB->record_exists('course', array('id' => $course->id))) {
                         if (!$own) {
                             // Not created by a company manager.
                             company_user::enrol($user, array($course->id), $this->id, $companycoursenoneditorrole->id);
                         } else {
                             if ($companymanager->managertype == 2) {
                                 // Assign the department manager course access role.
                                 company_user::enrol($user, array($course->id), $this->id, $companycoursenoneditorrole->id);
                             } else {
                                 // Assign the company manager course access role.
                                 company_user::enrol($user, array($course->id), $this->id, $companycourseeditorrole->id);
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($own && $departmentid == 0) {
         // Add it to the list of company created courses.
         if (!$DB->record_exists('company_created_courses', array('companyid' => $this->id, 'courseid' => $course->id))) {
             $DB->insert_record('company_created_courses', array('companyid' => $this->id, 'courseid' => $course->id));
         }
     }
 }
 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();
         }
     }
 }
示例#4
0
function xmldb_local_iomad_upgrade($oldversion)
{
    global $CFG, $DB;
    $result = true;
    $dbman = $DB->get_manager();
    if ($oldversion < 2011090600) {
        // Define table department to be created.
        $table = new xmldb_table('department');
        // Adding fields to table department.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
        $table->add_field('shortname', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
        $table->add_field('company', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('parent', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null);
        // Adding keys to table department.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for department.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table department_users to be created.
        $table = new xmldb_table('department_users');
        // Adding fields to table department_users.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('userid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('departmentid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table department_users.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for department_users.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table department_courses to be created.
        $table = new xmldb_table('department_courses');
        // Adding fields to table department_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('departmentid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table department_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for department_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011090600, 'local', 'iomad');
    }
    // Licensing added.
    if ($oldversion < 2011091500) {
        // Define table companylicense to be created.
        $table = new xmldb_table('companylicense');
        // Adding fields to table companylicense.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
        $table->add_field('allocation', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('validlength', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('expirydate', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('used', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null);
        // Adding keys to table companylicense.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for companylicense.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table companylicense_users to be created.
        $table = new xmldb_table('companylicense_users');
        // Adding fields to table companylicense_users.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('licenseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('userid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table companylicense_users.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for companylicense_users.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table companylicense_courses to be created.
        $table = new xmldb_table('companylicense_courses');
        // Adding fields to table companylicense_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('licenseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table companylicense_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for companylicense_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011091500, 'local', 'iomad');
    }
    if ($oldversion < 2011092300) {
        // Define field id to be added to companylicense_users.
        $table = new xmldb_table('companylicense_users');
        $field = new xmldb_field('isusing', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'userid');
        // Conditionally launch add field id.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011092300, 'local', 'iomad');
    }
    if ($oldversion < 2011092600) {
        // Define field timecompleted to be added to companylicense_users.
        $table = new xmldb_table('companylicense_users');
        $field = new xmldb_field('timecompleted', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null, 'isusing');
        // Conditionally launch add field timecompleted.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field score to be added to companylicense_users.
        $table = new xmldb_table('companylicense_users');
        $field = new xmldb_field('score', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'timecompleted');
        // Conditionally launch add field score.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field result to be added to companylicense_users.
        $table = new xmldb_table('companylicense_users');
        $field = new xmldb_field('result', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'score');
        // Conditionally launch add field result.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011092600, 'local', 'iomad');
    }
    if ($oldversion < 2011103000) {
        // Define table company_course_groups to be created.
        $table = new xmldb_table('company_course_groups');
        // Adding fields to table company_course_groups.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('groupid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table company_course_groups.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for company_course_groups.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table iomad_courses to be created.
        $table = new xmldb_table('iomad_courses');
        // Adding fields to table iomad_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('licensed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('shared', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        // Adding keys to table iomad_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for iomad_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011103000, 'local', 'iomad');
    }
    if ($oldversion < 2011111000) {
        // Define table iomad_courses to be created.
        $table = new xmldb_table('iomad_courses');
        // Adding fields to table iomad_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('licensed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0');
        $table->add_field('shared', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0');
        // Adding keys to table iomad_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for iomad_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011111000, 'local', 'iomad');
    }
    if ($oldversion < 2011111401) {
        // Define table classroom to be created.
        $table = new xmldb_table('classroom');
        // Adding fields to table classroom.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
        $table->add_field('address', XMLDB_TYPE_CHAR, '70', null, null, null, null);
        $table->add_field('city', XMLDB_TYPE_CHAR, '120', null, null, null, null);
        $table->add_field('country', XMLDB_TYPE_CHAR, '2', null, null, null, null);
        $table->add_field('postcode', XMLDB_TYPE_CHAR, '20', null, null, null, null);
        $table->add_field('capacity', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table classroom.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for classroom.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        upgrade_plugin_savepoint(true, 2011111401, 'local', 'iomad');
    }
    if ($oldversion < 2011111800) {
        // Define field validlength to be added to iomad_courses.
        $table = new xmldb_table('iomad_courses');
        $field = new xmldb_field('validlength', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, '0', 'shared');
        // Conditionally launch add field validlength.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011111800, 'local', 'iomad');
    }
    if ($oldversion < 2011111801) {
        // Define field warnexpire to be added to iomad_courses.
        $table = new xmldb_table('iomad_courses');
        $field = new xmldb_field('warnexpire', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'validlength');
        // Conditionally launch add field warnexpire.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field warncompletion to be added to iomad_courses.
        $table = new xmldb_table('iomad_courses');
        $field = new xmldb_field('warncompletion', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'warnexpire');
        // Conditionally launch add field warncompletion.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011111801, 'local', 'iomad');
    }
    if ($oldversion < 2011112000) {
        // Define field category to be added to company.
        $table = new xmldb_table('company');
        $field = new xmldb_field('category', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'theme');
        // Conditionally launch add field category.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011112000, 'local', 'iomad');
    }
    if ($oldversion < 2012012500) {
        // Define table company_course_groups to be created.
        // ADDED AGAIN DUE TO git branching timelines.
        $table = new xmldb_table('company_course_groups');
        // Adding fields to table company_course_groups.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('groupid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table company_course_groups.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for company_course_groups.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table company_shared_courses to be created.
        $table = new xmldb_table('company_shared_courses');
        // Adding fields to table company_shared_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table company_shared_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for company_shared_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2012012500, 'local', 'iomad');
    }
    if ($oldversion < 2012051500) {
        // Change the role permissions for company and create the department manager role.
        $systemcontext = context_system::instance();
        // Create the Company Manager role.
        if (!($companymanager = $DB->get_record('role', array('shortname' => 'companymanager')))) {
            $companymanagerid = create_role('Company Manager', 'companymanager', '(Iomad) Manages individual companies - can upload users etc.');
        } else {
            $companymanagerid = $companymanager->id;
        }
        // If not done already, allow assignment at system context.
        $levels = get_role_contextlevels($companymanagerid);
        if (empty($levels)) {
            $level = null;
            $level->roleid = $companymanagerid;
            $level->contextlevel = CONTEXT_SYSTEM;
            $DB->insert_record('role_context_levels', $level);
        }
        // Create new Company Department Manager role.
        if (!($companydepartmentmanager = $DB->get_record('role', array('shortname' => 'companydepartmentmanager')))) {
            $companydepartmentmanagerid = create_role('Company Department Manager', 'companydepartmentmanager', '(Iomad) Manages departments within companies - can upload users etc.');
        } else {
            $companydepartmentmanagerid = $companydepartmentmanager->id;
        }
        // If not done already, allow assignment at system context.
        $levels = get_role_contextlevels($companydepartmentmanagerid);
        if (empty($levels)) {
            $level = null;
            $level->roleid = $companydepartmentmanagerid;
            $level->contextlevel = CONTEXT_SYSTEM;
            $DB->insert_record('role_context_levels', $level);
        }
        $companydepartmentmanagercaps = array('block/iomad_reports:view', 'block/iomad_online_users:viewlist', 'block/iomad_link:view', 'block/iomad_company_admin:view_licenses', 'block/iomad_company_admin:view', 'block/iomad_company_admin:user_upload', 'block/iomad_company_admin:user_create', 'block/iomad_company_admin:editusers', 'block/iomad_company_admin:edit_departments', 'block/iomad_company_admin:company_view', 'block/iomad_company_admin:company_course_users', 'block/iomad_company_admin:assign_department_manager', 'block/iomad_company_admin:company_manager', 'block/iomad_company_admin:allocate_licenses', 'local/iomad_dashboard:view');
        if ($DB->get_records('role_capabilities', array('roleid' => $companydepartmentmanagerid))) {
            $DB->delete_records('role_capabilities', array('roleid' => $companydepartmentmanagerid));
        }
        foreach ($companydepartmentmanagercaps as $cap) {
            assign_capability($cap, CAP_ALLOW, $companydepartmentmanagerid, $systemcontext->id);
        }
        $companymanagercaps = array('block/iomad_company_admin:assign_company_manager', 'block/iomad_company_admin:assign_department_manager', 'block/iomad_online_users:viewlist', 'block/iomad_link:view', 'block/iomad_company_admin:view_licenses', 'block/iomad_company_admin:view', 'block/iomad_company_admin:user_upload', 'block/iomad_company_admin:user_create', 'block/iomad_company_admin:editusers', 'block/iomad_company_admin:edit_departments', 'block/iomad_company_admin:company_view', 'block/iomad_company_admin:company_course_users', 'block/iomad_company_admin:assign_department_manager', 'block/iomad_company_admin:allocate_licenses', 'block/iomad_company_admin:assign_company_manager', 'block/iomad_company_admin:classrooms', 'block/iomad_company_admin:classrooms_delete', 'block/iomad_company_admin:classrooms_edit', 'block/iomad_company_admin:company_edit', 'block/iomad_company_admin:company_course_unenrol', 'block/iomad_company_admin:company_manager', 'block/iomad_company_admin:company_user_profiles', 'block/iomad_company_admin:createcourse', 'local/iomad_dashboard:view');
        if ($DB->get_records('role_capabilities', array('roleid' => $companymanagerid))) {
            $DB->delete_records('role_capabilities', array('roleid' => $companymanagerid));
        }
        foreach ($companymanagercaps as $cap) {
            assign_capability($cap, CAP_ALLOW, $companymanagerid, $systemcontext->id);
        }
        //  Deal with the database.
        // Define field id to be added to companymanager.
        $table = new xmldb_table('companymanager');
        $field = new xmldb_field('departmentmanager', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'userid');
        // Conditionally launch add field departmentmanager.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Conditionally launch add field id.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $DB->set_field('companymanager', 'departmentmanager', 0);
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2012051500, 'local', 'iomad');
    }
    if ($oldversion < 2012052200) {
        // Define table company_created_courses to be created.
        $table = new xmldb_table('company_created_courses');
        // Adding fields to table company_created_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table company_created_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for company_created_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Change the role permissions for company and create the department manager role..
        $systemcontext = context_system::instance();
        // Create the Company Course Editor.
        if (!($companycourseeditor = $DB->get_record('role', array('shortname' => 'companycourseeditor')))) {
            $companycourseeditorid = create_role('Company Course Editor', 'companycourseeditor', '(Iomad) Teacher style role for Company manager provided to them when they create their own course.');
        } else {
            $companycourseeditorid = $companycourseeditor->id;
        }
        // If not done already, allow assignment at system context.
        $levels = get_role_contextlevels($companycourseeditorid);
        if (empty($levels)) {
            $level = null;
            $level->roleid = $companycourseeditorid;
            $level->contextlevel = CONTEXT_COURSE;
            $DB->insert_record('role_context_levels', $level);
        }
        // Create new Company Course Non Editor role.
        if (!($companycoursenoneditor = $DB->get_record('role', array('shortname' => 'companycoursenoneditor')))) {
            $companycoursenoneditorid = create_role('Company Course Non Editor', 'companycoursenoneditor', '(Iomad) Non editing teacher style role form Company and department managers');
        } else {
            $companycoursenoneditorid = $companycoursenoneditor->id;
        }
        // If not done already, allow assignment at system context.
        $levels = get_role_contextlevels($companycoursenoneditorid);
        if (empty($levels)) {
            $level = null;
            $level->roleid = $companycoursenoneditorid;
            $level->contextlevel = CONTEXT_COURSE;
            $DB->insert_record('role_context_levels', $level);
        }
        if ($DB->get_records('role_capabilities', array('roleid' => $companycourseeditorid))) {
            $DB->delete_records('role_capabilities', array('roleid' => $companycourseeditorid));
        }
        $companycourseeditorcaps = array('block/side_bar_block:editblock', 'block/side_bar_block:viewblock', 'booktool/importhtml:import', 'booktool/print:print', 'enrol/authorize:manage', 'enrol/license:manage', 'enrol/license:unenrol', 'enrol/manual:enrol', 'enrol/manual:unenrol', 'gradereport/grader:view', 'gradereport/overview:view', 'gradereport/user:view', 'mod/assignment:exportownsubmission', 'mod/assignment:grade', 'mod/assignment:view', 'mod/book:edit', 'mod/book:read', 'mod/book:viewhiddenchapters', 'mod/certificate:manage', 'mod/certificate:view', 'mod/choice:choose', 'mod/choice:deleteresponses', 'mod/choice:downloadresponses', 'mod/choice:readresponses', 'mod/courseclassroom:grade', 'mod/courseclassroom:invite', 'mod/courseclassroom:viewattendees', 'mod/forum:addnews', 'mod/forum:addquestion', 'mod/forum:createattachment', 'mod/forum:deleteanypost', 'mod/forum:deleteownpost', 'mod/forum:editanypost', 'mod/forum:exportdiscussion', 'mod/forum:exportownpost', 'mod/forum:exportpost', 'mod/forum:managesubscriptions', 'mod/forum:movediscussions', 'mod/forum:postwithoutthrottling', 'mod/forum:rate', 'mod/forum:replynews', 'mod/forum:replypost', 'mod/forum:splitdiscussions', 'mod/forum:startdiscussion', 'mod/forum:viewallratings', 'mod/forum:viewanyrating', 'mod/forum:viewdiscussion', 'mod/forum:viewhiddentimedposts', 'mod/forum:viewqandawithoutposting', 'mod/forum:viewrating', 'mod/forum:viewsubscribers', 'mod/page:view', 'mod/resource:view', 'mod/scorm:deleteresponses', 'mod/scorm:savetrack', 'mod/scorm:viewreport', 'mod/scorm:viewscores', 'mod/url:view', 'moodle/block:edit', 'moodle/block:view', 'moodle/calendar:manageentries', 'moodle/calendar:managegroupentries', 'moodle/calendar:manageownentries', 'moodle/course:activityvisibility', 'moodle/course:changefullname', 'moodle/course:changesummary', 'moodle/course:manageactivities', 'moodle/course:managefiles', 'moodle/course:managegroups', 'moodle/course:markcomplete', 'moodle/course:reset', 'moodle/course:sectionvisibility', 'moodle/course:setcurrentsection', 'moodle/course:update', 'moodle/course:viewhiddenactivities', 'moodle/course:viewhiddensections', 'moodle/course:viewparticipants', 'moodle/grade:edit', 'moodle/grade:hide', 'moodle/grade:lock', 'moodle/grade:manage', 'moodle/grade:managegradingforms', 'moodle/grade:manageletters', 'moodle/grade:manageoutcomes', 'moodle/grade:unlock', 'moodle/grade:viewall', 'moodle/grade:viewhidden', 'moodle/notes:manage', 'moodle/notes:view', 'moodle/rating:rate', 'moodle/rating:view', 'moodle/rating:viewall', 'moodle/rating:viewany', 'moodle/role:switchroles', 'moodle/site:accessallgroups', 'moodle/site:manageblocks', 'moodle/site:trustcontent', 'moodle/site:viewfullnames', 'moodle/site:viewreports', 'moodle/site:viewuseridentity', 'moodle/user:viewdetails', 'report/courseoverview:view', 'report/log:view', 'report/log:viewtoday', 'report/loglive:view', 'report/outline:view', 'report/participation:view', 'report/progress:view');
        foreach ($companycourseeditorcaps as $rolecapability) {
            // Assign_capability will update rather than insert if capability exists.
            assign_capability($rolecapability, CAP_ALLOW, $companycourseeditorid, $systemcontext->id);
        }
        if ($DB->get_records('role_capabilities', array('roleid' => $companycoursenoneditorid))) {
            $DB->delete_records('role_capabilities', array('roleid' => $companycoursenoneditorid));
        }
        $companycoursenoneditorcaps = array('block/side_bar_block:viewblock', 'gradereport/grader:view', 'gradereport/user:view', 'mod/assignment:view', 'mod/book:read', 'mod/certificate:manage', 'mod/certificate:view', 'mod/choice:readresponses', 'mod/feedback:view', 'mod/forum:addquestion', 'mod/forum:createattachment', 'mod/forum:deleteownpost', 'mod/forum:replypost', 'mod/forum:startdiscussion', 'mod/forum:viewdiscussion', 'mod/forum:viewqandawithoutposting', 'mod/page:view', 'mod/quiz:attempt', 'mod/quiz:view', 'mod/resource:view', 'mod/survey:participate', 'moodle/block:view', 'moodle/grade:viewall', 'moodle/site:viewfullnames', 'moodle/site:viewuseridentity');
        foreach ($companycoursenoneditorcaps as $rolecapability) {
            // Assign_capability will update rather than insert if capability exists.
            assign_capability($rolecapability, CAP_ALLOW, $companycoursenoneditorid, $systemcontext->id);
        }
        // Deal with role assignments.
        // Get the list of company courses.
        $companycourses = $DB->get_records('companycourse');
        // Get the managers.
        foreach ($companycourses as $companycourse) {
            $companymanagers = $DB->get_records('companymanager', array('companyid' => $companycourse->companyid));
            if ($DB->record_exists('course', array('id' => $companycourse->courseid))) {
                if ($DB->record_exists('scorm', array('course' => $companycourse->courseid))) {
                    // This is a scorm course so only noneditor role to be applied.
                    foreach ($companymanagers as $companymanager) {
                        if ($user = $DB->get_record('user', array('id' => $companymanager->userid, 'deleted' => 0))) {
                            company_user::enrol($user, array($companycourse->courseid), $companycourse->companyid, $companycoursenoneditorid);
                        }
                    }
                } else {
                    // Add it to the company created courses.
                    $DB->insert_record('company_created_courses', array('companyid' => $companycourse->companyid, 'courseid' => $companycourse->courseid));
                    // Set up the manager roles.
                    foreach ($companymanagers as $companymanager) {
                        if ($user = $DB->get_record('user', array('id' => $companymanager->userid, 'deleted' => 0))) {
                            if ($companymanager->departmentmanager) {
                                // Lowly department manager, no more than that.
                                company_user::enrol($user, array($companycourse->courseid), $companycourse->companyid, $companycoursenoneditorid);
                            } else {
                                company_user::enrol($user, array($companycourse->courseid), $companycourse->companyid, $companycourseeditorid);
                            }
                        }
                    }
                }
            }
        }
        // Restrict the default modules.
        $allowedmodules = '1,3,5,7,10,12,14,15,17,20,21,22';
        set_config('restrictbydefault', 1);
        set_config('restrictmodulesfor', 'all');
        set_config('defaultallowedmodules', $allowedmodules);
        // Restrict the modules for every course.
        $sitecourses = $DB->get_records_select('course', "id != " . SITEID);
        foreach ($sitecourses as $sitecourse) {
            foreach (explode(',', $allowedmodules) as $module) {
                $DB->insert_record('course_allowed_modules', array('course' => $sitecourse->id, 'module' => $module));
                $DB->set_field('course', 'restrictmodules', '1', array('id' => $sitecourse->id));
            }
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2012052200, 'local', 'iomad');
    }
    if ($oldversion < 2013050100) {
        // Define table companyusers to be created.
        $table = new xmldb_table('companyusers');
        // Adding fields to table companyusers.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('usserid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        // Adding keys to table companyusers.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for companyusers.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Need to deal with current company allocations.
        /*if ($companyfield = $DB->get_record('user_info_field', array('shortname' => 'company'))) {
            if ($companyusers = $DB->get_records('user_info_data', array('fieldid' => $companyfield->id))) {
                foreach($companyusers as $companyuser) {
          */
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2013050100, 'local', 'iomad');
    }
    if ($oldversion < 2014012400) {
        $systemcontext = context_system::instance();
        // Get the Company Manager role.
        if ($companymanager = $DB->get_record('role', array('shortname' => 'companymanager'))) {
            $companymanagerid = $companymanager->id;
            $companymanagercaps = array('local/iomad_dashboard:view', 'block/iomad_reports:view', 'local_report_attendance:view', 'local_report_completion:view', 'local_report_users:view', 'local_report_scorm_overview:view');
            foreach ($companymanagercaps as $cap) {
                assign_capability($cap, CAP_ALLOW, $companymanagerid, $systemcontext->id);
            }
        }
        // Get the Company Department Manager role.
        if ($companydepartmentmanager = $DB->get_record('role', array('shortname' => 'companydepartmentmanager'))) {
            $companydepartmentmanagerid = $companydepartmentmanager->id;
            $companydepartmentmanagercaps = array('local/iomad_dashboard:view', 'block/iomad_reports:view', 'local_report_attendance:view', 'local_report_completion:view', 'local_report_users:view', 'local_report_scorm_overview:view');
            foreach ($companydepartmentmanagercaps as $cap) {
                assign_capability($cap, CAP_ALLOW, $companydepartmentmanagerid, $systemcontext->id);
            }
        }
        // Get the Client Administrator role.
        if ($clientadministrator = $DB->get_record('role', array('shortname' => 'clientadministrator'))) {
            $clientadministratorid = $clientadministrator->id;
            $clientadministratorcaps = array('local/iomad_dashboard:view', 'block/iomad_reports:view', 'local_report_attendance:view', 'local_report_completion:view', 'local_report_users:view', 'local_report_scorm_overview:view');
            foreach ($clientadministratorcaps as $cap) {
                assign_capability($cap, CAP_ALLOW, $clientadministratorid, $systemcontext->id);
            }
        }
    }
    if ($oldversion < 2014022600) {
        // Change the site to force user allowed themes.
        set_config('allowuserthemes', 1);
    }
    if ($oldversion < 2014052700) {
        // Define field suspended to be added to company_users.
        $table = new xmldb_table('company_users');
        $field = new xmldb_field('suspended', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'departmentid');
        // Conditionally launch add field suspended.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field suspended to be added to company.
        $table = new xmldb_table('company');
        $field = new xmldb_field('suspended', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'profileid');
        // Conditionally launch add field suspended.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2014052700, 'local', 'iomad');
    }
    if ($oldversion < 2014052702) {
        // Define new table company_role_restriction
        $table = new xmldb_table('company_role_restriction');
        // Adding fields to table department.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('roleid', XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
        // Adding keys to table department.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('company_roleidcompanyid', XMLDB_KEY_UNIQUE, array('roleid', 'companyid', 'capability'));
        // Conditionally launch create table for company_role_restriction.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
    }
    if ($oldversion < 2014120400) {
        // Define field licensecourseid to be added to companylicense_users.
        $table = new xmldb_table('companylicense_users');
        $field = new xmldb_field('licensecourseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'userid');
        // Conditionally launch add field licensecourseid.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2014120400, 'local', 'iomad');
    }
    if ($oldversion < 2014121900) {
        // Deal with licenseses which have already been allocated.
        $licenseusers = $DB->get_records('companylicense_users', array('licensecourseid' => 0));
        foreach ($licenseusers as $licenseuser) {
            if ($licenseuser->timecompleted != null) {
                continue;
            }
            // Are they using the license?
            if ($licenseuser->isusing == 1) {
                // Get the course.
                $enrolrecords = $DB->get_records_sql("SELECT e.courseid,ue.userid\n                                                    FROM {enrol} e JOIN {user_enrolments} ue\n                                                    ON e.id=ue.enrolid\n                                                    WHERE userid = :userid\n                                                    AND courseid IN\n                                                     (SELECT courseid FROM {companylicense_courses}\n                                                      WHERE licenseid = :licenseid)", array('userid' => $licenseuser->userid, 'licenseid' => $licenseuser->licenseid));
                // Do we have more than one record?
                if (count($enrolrecords > 1)) {
                    foreach ($enrolrecords as $enrolrecord) {
                        // Check if we already have a record for this course.
                        if ($DB->get_record('companylicense_users', array('userid' => $licenseuser->userid, 'licenseid' => $licenseuser->licenseid, 'licensecourseid' => $enrolrecord->courseid))) {
                            continue;
                        } else {
                            $licenseuser->licensecourseid = $enrolrecord->courseid;
                            $DB->update_record('companylicense_users', $licenseuser);
                        }
                    }
                } else {
                    list($enrolcourseid, $enroluserid) = each($enrolrecords);
                    $licenseuser->licensecourseid = $enrolcourseid;
                    $DB->update_record('companylicense_users', $licenseuser);
                }
            } else {
                // Get the courses.
                $licensecourses = $DB->get_records('companylicense_courses', array('licenseid' => $licenseuser->licenseid));
                if (count($licensecourses) == 1) {
                    // Only one course so add it.
                    $licensecourse = array_pop($licensecourses);
                    $licenseuser->licensecourseid = $licensecourse->id;
                    $DB->update_record('companylicense_users', $licenseuser);
                } else {
                    //  Dont know which course to assign so we are going to remove the record as its not being used.
                    $DB->delete_records('companylicense_users', array('id' => $licenseuser->id));
                }
            }
        }
        //  Update the used counts for each license.
        $licenses = $DB->get_records('companylicense');
        foreach ($licenses as $license) {
            $licensecount = $DB->count_records('companylicense_users', array('licenseid' => $license->id));
            $license->used = $licensecount;
            $DB->update_record('companylicense', $license);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2014121900, 'local', 'iomad');
    }
    if ($oldversion < 2015020800) {
        // Define table company_domains to be created.
        $table = new xmldb_table('company_domains');
        // Adding fields to table company_domains.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('domain', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
        // Adding keys to table company_domains.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for company_domains.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2015020800, 'local', 'iomad');
    }
    return $result;
}
                 //  $companymanagerrole = $DB->get_record('role', array('shortname' => 'companymanager'));
                 role_unassign_all(array('roleid' => $departmentmanagerrole->id, 'userid' => $userid, 'contextid' => $systemcontext->id), false, false);
                 role_unassign_all(array('roleid' => $companyinstructorrole->id, 'userid' => $userid, 'contextid' => $systemcontext->id), false, false);
                 // role_unassign_all(array('roleid' => $companymanagerrole->id, 'userid' => $userid, 'contextid' => $systemcontext->id), false, false);
                 // Assign the user as a company manager.
                 $companymanagerrole = $DB->get_record('role', array('shortname' => 'companymanager'));
                 // Give them the manager role.        else if ($usernew->managertype == 1) {
                 role_assign($companymanagerrole->id, $usernew->id, $systemcontext->id);
                 if ($companycourses = $DB->get_records('company_course', array('companyid' => $companyid))) {
                     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' => $companyid, 'courseid' => $companycourse->courseid))) {
                                 company_user::enrol($user, array($companycourse->courseid), $companycourse->companyid, $companycourseeditorrole->id);
                             } else {
                                 company_user::enrol($user, array($companycourse->courseid), $companycourse->companyid, $companycoursenoneditorrole->id);
                             }
                         }
                     }
                 }
             } else {
                 $departmentmanagerrole = $DB->get_record('role', array('shortname' => 'companydepartmentmanager'));
                 $companyinstructorrole = $DB->get_record('role', array('shortname' => 'instructor'));
                 $companymanagerrole = $DB->get_record('role', array('shortname' => 'companymanager'));
                 role_unassign_all(array('roleid' => $departmentmanagerrole->id, 'userid' => $userid, 'contextid' => $systemcontext->id), false, false);
                 role_unassign_all(array('roleid' => $companyinstructorrole->id, 'userid' => $userid, 'contextid' => $systemcontext->id), false, false);
                 role_unassign_all(array('roleid' => $companymanagerrole->id, 'userid' => $userid, 'contextid' => $systemcontext->id), false, false);
             }
         }
     }
 }
        //
        // Code End
        // Code added by Sumit here we asign region or location to user and assign manager to company
        $data->userdepartment = $userlocation ? $userlocation : $data->userregion;
        $data->userdepartment = $data->managertype || !$data->userdepartment ? $parentnode->id : $data->userdepartment;
        company::assign_user_to_department($data->userdepartment, $userid);
        /* END -  GWL  : Assign user to department */
        // Code added by Sumit here we asign region or location to user and assign manager to company
        $data->userdepartment = $userlocation ? $userlocation : $data->userregion;
        $data->userdepartment = $data->managertype || !$data->userdepartment ? $parentnode->id : $data->userdepartment;
        company::assign_user_to_department($data->userdepartment, $userid);
        /* END -  GWL  : Assign user to department */
        // Enrol the user on the courses.
        if (!empty($createcourses)) {
            $userdata = $DB->get_record('user', array('id' => $userid));
            company_user::enrol($userdata, $createcourses, $companyid);
        }
        if (isset($data->submitandback)) {
            // redirect($returnurl);
            // Code by sumit
            redirect(new moodle_url('/blocks/iomad_company_admin/editusers.php'));
            // end of code
        } else {
            redirect($linkurl . "?createdok=1");
        }
    }
}
$blockpage->display_header();
// GWL : Check the department is valid.
if (!empty($departmentid) && !company::check_valid_department($companyid, $departmentid)) {
    print_error('invaliddepartment', 'block_iomad_company_admin');
示例#7
0
function assign_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) {
        /* $courseslist = array();
           foreach($companycourses as $depttitlecourse){
           $courseslist[] = $depttitlecourse->course;
           } */
        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::enrol($user, $courses, $companyid, 5);
        }
    }
}
 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();
         }
     }
 }
 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();
         }
     }
 }
示例#10
0
                 $ccache[$shortname]->groups = null;
             }
             company_user::enrol($user, $ccache[$shortname], $companyid);
         }
         if (!empty($formdata->selectedcourses)) {
             // add the user to the courses selected in the upload form.
             $courseids = array();
             foreach ($formdata->selectedcourses as $selectedcourse) {
                 $courseids[] = $selectedcourse->id;
             }
             company_user::enrol($user, $courseids, $companyid);
         }
     }
     // Enrol user into courses that were selected on the form.
     if (isset($formdata->selectedcourses)) {
         company_user::enrol($user, array_keys($formdata->selectedcourses));
     }
     // If user was set to have password generated, generate it now, so that it can be downloaded.
     company_user::generate_temporary_password($user, $formdata->sendnewpasswordemails);
 }
 $upt->flush();
 $upt->close();
 // Close table.
 $cir->close();
 $cir->cleanup(true);
 echo $OUTPUT->box_start('boxwidthnarrow boxaligncenter generalbox', 'uploadresults');
 echo '<p>';
 if ($optype != UU_UPDATE) {
     echo get_string('userscreated', 'tool_uploaduser') . ': ' . $usersnew . '<br />';
 }
 if ($optype == UU_UPDATE or $optype == UU_ADD_UPDATE) {