示例#1
1
 /**
  * Checks that a two user id's are in the same company.
  *
  * Parameters - 
  *              $userid = int;
  *
  * Returns boolean.
  *
  * */
 public static function check_can_manage($userid)
 {
     global $DB, $USER;
     $context = context_system::instance();
     // Set the companyid
     $companyid = iomad::get_my_companyid($context);
     // Get the list of users.
     $myusers = self::get_my_users($companyid);
     // If the user is in the list, return true.
     if (!empty($myusers[$userid])) {
         return true;
     }
     // Return a false by default.
     return false;
 }
    $linktext = get_string('editcompany', 'block_iomad_company_admin');
} else {
    $linktext = get_string('addnewcompany', 'block_iomad_company_admin');
}
// Set the url.
$linkurl = new moodle_url('/blocks/iomad_company_admin/company_edit_form.php', array('returnurl' => $returnurl));
// Build the nav bar.
//company_admin_fix_breadcrumb($PAGE, $linktext, $linkurl);
$PAGE->navbar->add($linktext, $linkurl);
$blockpage = new blockpage($PAGE, $OUTPUT, 'iomad_company_admin', 'block', $new ? 'addnewcompany' : 'editacompany');
$blockpage->setup();
if (!$new) {
    // Set the companyid
    //GWL
    if (!has_capability('block/iomad_company_admin:manageallcompany', $context)) {
        $companyid = iomad::get_my_companyid($context);
    }
    // end of code
    $isadding = false;
    $companyrecord = $DB->get_record('company', array('id' => $companyid), '*', MUST_EXIST);
    iomad::require_capability('block/iomad_company_admin:company_edit', $context);
} else {
    $isadding = true;
    $companyid = 0;
    $companyrecord = new stdClass();
    iomad::require_capability('block/iomad_company_admin:company_add', $context);
}
$urlparams = array('companyid' => $companyid);
if ($returnurl) {
    $urlparams['returnurl'] = $returnurl;
}
示例#3
0
$systemcontext = context_system::instance();
require_login();
// Correct the navbar.
// Set the name for the page.
$linktext = get_string('edit_users_title', 'block_iomad_company_admin');
// Set the url.
$linkurl = new moodle_url('/blocks/iomad_company_admin/editusers.php');
// Build the nav bar.
//company_admin_fix_breadcrumb($PAGE, $linktext, $linkurl);
require_once $CFG->libdir . '/adminlib.php';
admin_externalpage_setup('managecompanyusersetting');
// Print the page header.
$blockpage = new blockpage($PAGE, $OUTPUT, 'iomad_company_admin', 'block', 'company_edit_users_title');
$blockpage->setup();
// Set the companyid
$companyid = iomad::get_my_companyid($systemcontext);
require_login(null, false);
// Adds to $PAGE, creates $OUTPUT.
$baseurl = new moodle_url(basename(__FILE__), $params);
$returnurl = $baseurl;
$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');
}
// Get the associated department id.
$company = new company($companyid);
$parentlevel = company::get_company_parentnode($company->id);
$companydepartment = $parentlevel->id;
if (iomad::has_capability('block/iomad_company_admin:edit_all_departments', context_system::instance())) {
    $userhierarchylevel = $parentlevel->id;
 /**
  * Loads the courses in Moodle into the navigation.
  *
  * @global moodle_database $DB
  * @param string|array $categoryids An array containing categories to load courses
  *                     for, OR null to load courses for all categories.
  * @return array An array of navigation_nodes one for each course
  */
 protected function load_all_courses($categoryids = null)
 {
     global $CFG, $DB, $SITE, $USER;
     // Work out the limit of courses.
     $limit = 20;
     if (!empty($CFG->navcourselimit)) {
         $limit = $CFG->navcourselimit;
     }
     $toload = empty($CFG->navshowallcourses) ? self::LOAD_ROOT_CATEGORIES : self::LOAD_ALL_CATEGORIES;
     // If we are going to show all courses AND we are showing categories then
     // to save us repeated DB calls load all of the categories now
     if ($this->show_categories()) {
         $this->load_all_categories($toload);
     }
     // Will be the return of our efforts
     $coursenodes = array();
     // Check if we need to show categories.
     if ($this->show_categories()) {
         // IOMAD - If not logged in, don't show any courses in the navigation.
         if (!isloggedin()) {
             return array();
         }
         if (iomad::is_company_user()) {
             $companyid = iomad::get_my_companyid(context_system::instance());
             $sharedsql = " AND ( c.id IN (\n                                   SELECT courseid FROM {company_course}\n                                   WHERE companyid = {$companyid})\n                               OR c.id IN (\n                                   SELECT courseid FROM {iomad_courses}\n                                   WHERE shared=1)\n                               OR c.id IN (\n                                   SELECT courseid FROM {company_shared_courses}\n                                   WHERE companyid = {$companyid})) ";
         } else {
             if (!is_siteadmin()) {
                 $sharedsql = " AND c.id IN (select courseid FROM {iomad_courses} WHERE shared=1) ";
             } else {
                 $sharedsql = "";
             }
         }
         // Hmmm we need to show categories... this is going to be painful.
         // We now need to fetch up to $limit courses for each category to
         // be displayed.
         if ($categoryids !== null) {
             if (!is_array($categoryids)) {
                 $categoryids = array($categoryids);
             }
             list($categorywhere, $categoryparams) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'cc');
             $categorywhere = 'WHERE cc.id ' . $categorywhere;
         } else {
             if ($toload == self::LOAD_ROOT_CATEGORIES) {
                 $categorywhere = 'WHERE cc.depth = 1 OR cc.depth = 2';
                 $categoryparams = array();
             } else {
                 $categorywhere = '';
                 $categoryparams = array();
             }
         }
         // First up we are going to get the categories that we are going to
         // need so that we can determine how best to load the courses from them.
         // IOMAD - Add $sharedsql after $categorywhere to hide other company
         // categories.
         $sql = "SELECT cc.id, COUNT(c.id) AS coursecount\n                        FROM {course_categories} cc\n                    LEFT JOIN {course} c ON c.category = cc.id\n                            {$categorywhere}\n                            {$sharedsql}\n                    GROUP BY cc.id";
         $categories = $DB->get_recordset_sql($sql, $categoryparams);
         $fullfetch = array();
         $partfetch = array();
         foreach ($categories as $category) {
             if (!$this->can_add_more_courses_to_category($category->id)) {
                 continue;
             }
             if ($category->coursecount > $limit * 5) {
                 $partfetch[] = $category->id;
             } else {
                 if ($category->coursecount > 0) {
                     $fullfetch[] = $category->id;
                 }
             }
         }
         $categories->close();
         if (count($fullfetch)) {
             // First up fetch all of the courses in categories where we know that we are going to
             // need the majority of courses.
             list($categoryids, $categoryparams) = $DB->get_in_or_equal($fullfetch, SQL_PARAMS_NAMED, 'lcategory');
             $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
             $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
             $categoryparams['contextlevel'] = CONTEXT_COURSE;
             $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category {$ccselect}\n                            FROM {course} c\n                                {$ccjoin}\n                            WHERE c.category {$categoryids}\n                        ORDER BY c.sortorder ASC";
             $coursesrs = $DB->get_recordset_sql($sql, $categoryparams);
             foreach ($coursesrs as $course) {
                 if ($course->id == $SITE->id) {
                     // This should not be necessary, frontpage is not in any category.
                     continue;
                 }
                 if (array_key_exists($course->id, $this->addedcourses)) {
                     // It is probably better to not include the already loaded courses
                     // directly in SQL because inequalities may confuse query optimisers
                     // and may interfere with query caching.
                     continue;
                 }
                 if (!$this->can_add_more_courses_to_category($course->category)) {
                     continue;
                 }
                 context_helper::preload_from_record($course);
                 if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
                     continue;
                 }
                 $coursenodes[$course->id] = $this->add_course($course);
             }
             $coursesrs->close();
         }
         if (count($partfetch)) {
             // Next we will work our way through the categories where we will likely only need a small
             // proportion of the courses.
             foreach ($partfetch as $categoryid) {
                 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
                 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
                 $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category {$ccselect}\n                                FROM {course} c\n                                    {$ccjoin}\n                                WHERE c.category = :categoryid\n                            ORDER BY c.sortorder ASC";
                 $courseparams = array('categoryid' => $categoryid, 'contextlevel' => CONTEXT_COURSE);
                 $coursesrs = $DB->get_recordset_sql($sql, $courseparams, 0, $limit * 5);
                 foreach ($coursesrs as $course) {
                     if ($course->id == $SITE->id) {
                         // This should not be necessary, frontpage is not in any category.
                         continue;
                     }
                     if (array_key_exists($course->id, $this->addedcourses)) {
                         // It is probably better to not include the already loaded courses
                         // directly in SQL because inequalities may confuse query optimisers
                         // and may interfere with query caching.
                         // This also helps to respect expected $limit on repeated executions.
                         continue;
                     }
                     if (!$this->can_add_more_courses_to_category($course->category)) {
                         break;
                     }
                     context_helper::preload_from_record($course);
                     if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
                         continue;
                     }
                     $coursenodes[$course->id] = $this->add_course($course);
                 }
                 $coursesrs->close();
             }
         }
     } else {
         // Prepare the SQL to load the courses and their contexts
         list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses), SQL_PARAMS_NAMED, 'lc', false);
         $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
         $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
         $courseparams['contextlevel'] = CONTEXT_COURSE;
         $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category {$ccselect}\n                        FROM {course} c\n                            {$ccjoin}\n                        WHERE c.id {$courseids}\n                    ORDER BY c.sortorder ASC";
         $coursesrs = $DB->get_recordset_sql($sql, $courseparams);
         foreach ($coursesrs as $course) {
             if ($course->id == $SITE->id) {
                 // frotpage is not wanted here
                 continue;
             }
             if ($this->page->course && $this->page->course->id == $course->id) {
                 // Don't include the currentcourse in this nodelist - it's displayed in the Current course node
                 continue;
             }
             context_helper::preload_from_record($course);
             if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
                 continue;
             }
             $coursenodes[$course->id] = $this->add_course($course);
             if (count($coursenodes) >= $limit) {
                 break;
             }
         }
         $coursesrs->close();
     }
     return $coursenodes;
 }
 /**
  * Retrieves number of records from course table
  *
  * Not all fields are retrieved. Records are ready for preloading context
  *
  * @param string $whereclause
  * @param array $params
  * @param array $options may indicate that summary and/or coursecontacts need to be retrieved
  * @param bool $checkvisibility if true, capability 'moodle/course:viewhiddencourses' will be checked
  *     on not visible courses
  * @return array array of stdClass objects
  */
 protected static function get_course_records($whereclause, $params, $options, $checkvisibility = false)
 {
     global $DB;
     $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
     $fields = array('c.id', 'c.category', 'c.sortorder', 'c.shortname', 'c.fullname', 'c.idnumber', 'c.startdate', 'c.visible', 'c.cacherev');
     if (!empty($options['summary'])) {
         $fields[] = 'c.summary';
         $fields[] = 'c.summaryformat';
     } else {
         $fields[] = $DB->sql_substr('c.summary', 1, 1) . ' as hassummary';
     }
     // IOMAD - Remove courses which don't belong to your company
     // and add in shared courses.
     $context = context_system::instance();
     //GWL : Provide access of Mange Course & Category to siteadmin
     if (!is_siteadmin() && !has_capability('moodle/category:manage', $context)) {
         //GWL : Provide access of Manage Course & Category to Siteadmin (if (!is_siteadmin())
         if (!isloggedin()) {
             $whereclause .= " AND c.id NOT IN (SELECT courseid FROM {company_course})";
         } else {
             $whereclause .= " AND (\n                                   c.id IN (\n                                    SELECT courseid FROM {company_course}\n                                    WHERE companyid = :companyid\n                                   ) OR c.id IN (\n                                    SELECT courseid FROM {iomad_courses}\n                                    WHERE shared = 1\n                                   )\n                                  )";
             $companyid = iomad::get_my_companyid(context_system::instance());
             $params['companyid'] = $companyid;
         }
     }
     $sql = "SELECT " . join(',', $fields) . ", {$ctxselect}\n                FROM {course} c\n                JOIN {context} ctx ON c.id = ctx.instanceid AND ctx.contextlevel = :contextcourse\n                WHERE " . $whereclause . " ORDER BY c.sortorder";
     $list = $DB->get_records_sql($sql, array('contextcourse' => CONTEXT_COURSE) + $params);
     if ($checkvisibility) {
         // Loop through all records and make sure we only return the courses accessible by user.
         foreach ($list as $course) {
             if (isset($list[$course->id]->hassummary)) {
                 $list[$course->id]->hassummary = strlen($list[$course->id]->hassummary) > 0;
             }
             if (empty($course->visible)) {
                 // Load context only if we need to check capability.
                 context_helper::preload_from_record($course);
                 if (!has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
                     unset($list[$course->id]);
                 }
             }
         }
     }
     // Preload course contacts if necessary.
     if (!empty($options['coursecontacts'])) {
         self::preload_course_contacts($list);
     }
     return $list;
 }
示例#6
0
function iomad_get_course_listing($returncount = 0, $sort = 'id', $dir = 'ASC', $page = 0, $recordsperpage = 0, $extraselect = '', array $extraparams = null, $search = '', $firstinitial = '', $lastinitial = '')
{
    global $DB;
    //$fullname = $DB->sql_fullname();
    //$select = "u.deleted <> 1";
    $context = context_system::instance();
    if (!has_capability('block/iomad_company_admin:manageallcompanycourses', $context)) {
        $companyid = iomad::get_my_companyid($context);
    }
    $company = optional_param('company', 'all', PARAM_ALPHANUM);
    $params = array();
    $select = "cc.companyid = :companyid";
    if ($company && iomad::has_capability('block/iomad_company_admin:manageallcompanycourses', $context)) {
        if ($company == 'all') {
            $select = '1=1';
        } else {
            $params['companyid'] = $company;
        }
    } else {
        $params['companyid'] = $companyid;
    }
    if (!empty($search)) {
        $search = trim($search);
        $select .= " AND (" . $DB->sql_like("c.fullname", ':search1', false, false);
        //" OR " . $DB->sql_like('u.email', ':search2', false, false) .
        //" OR u.username = :search3)";
        $params['search1'] = "%{$search}%";
        //$params['search2'] = "%$search%";
        //$params['search3'] = "$search";
    }
    /* if ($firstinitial) {
        $select .= " AND " . $DB->sql_like('u.fullname', ':fni', false, false);
        $params['fni'] = "$firstinitial%";
        }
        if ($lastinitial) {
        $select .= " AND " . $DB->sql_like('u.fullname', ':lni', false, false);
        $params['lni'] = "$lastinitial%";
        }
       */
    if ($extraselect) {
        $select .= " AND {$extraselect}";
        $params = $params + (array) $extraparams;
    }
    if ($sort) {
        $sort = " ORDER by c.{$sort} {$dir}";
    }
    // Warning: will return UNCONFIRMED USERS!
    //echo "<br>SELECT c.* FROM mdl_course} c JOIN mdl_company_course cc ON cc.courseid = c.id WHERE $select";
    /* if (!$returncount) {
           return $DB->get_records_sql("SELECT DISTINCT c.*,cc.companyid
                                    FROM {course} c JOIN {company_course} cc ON cc.courseid = c.id
                                    WHERE $select
                                    $sort", $params, $page, $recordsperpage);
       } else {
           return $DB->count_records_sql("SELECT count(DISTINCT c.id)
                                    FROM {course} c JOIN {company_course} cc ON cc.courseid = c.id
                                    WHERE $select
                                    $sort", $params);
       }*/
    // code by sumit
    if (!$returncount) {
        return $DB->get_records_sql("SELECT concat('course-',c.id,'-company-',cc.companyid),c.*,cc.companyid\n                                 FROM {course} c JOIN {company_course} cc ON cc.courseid = c.id\n                                 WHERE {$select}\n                                 {$sort}", $params, $page, $recordsperpage);
    } else {
        return $DB->count_records_sql("SELECT count(c.id)\n                                 FROM {course} c JOIN {company_course} cc ON cc.courseid = c.id\n                                 WHERE {$select}\n                                 {$sort}", $params);
    }
    // End
}