本文整理汇总了PHP中context_instance_preload函数的典型用法代码示例。如果您正苦于以下问题:PHP context_instance_preload函数的具体用法?PHP context_instance_preload怎么用?PHP context_instance_preload使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了context_instance_preload函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_string
} else {
$countrysort = (strpos($sort, 'country') !== false);
$timeformat = get_string('strftimedate');
if ($userlist) {
$usersprinted = array();
foreach ($userlist as $user) {
if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
continue;
}
$usersprinted[] = $user->id; /// Add new user to the array of users printed
context_instance_preload($user);
if ($user->lastaccess) {
$lastaccess = format_time(time() - $user->lastaccess, $datestring);
} else {
$lastaccess = $strnever;
}
if (empty($user->country)) {
$country = '';
} else {
if($countrysort) {
$country = '('.$user->country.') '.$countries[$user->country];
}
else {
示例2: get_course_user_profiles
/**
* Get course participant's details
*
* @param array $userlist array of user ids and according course ids
* @return array An array of arrays describing course participants
* @since Moodle 2.2
*/
public static function get_course_user_profiles($userlist)
{
global $CFG, $USER, $DB;
require_once $CFG->dirroot . "/user/lib.php";
$params = self::validate_parameters(self::get_course_user_profiles_parameters(), array('userlist' => $userlist));
$userids = array();
$courseids = array();
foreach ($params['userlist'] as $value) {
$userids[] = $value['userid'];
$courseids[$value['userid']] = $value['courseid'];
}
// cache all courses
$courses = array();
list($cselect, $cjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
list($sqlcourseids, $params) = $DB->get_in_or_equal(array_unique($courseids));
$coursesql = "SELECT c.* {$cselect}\n FROM {course} c {$cjoin}\n WHERE c.id {$sqlcourseids}";
$rs = $DB->get_recordset_sql($coursesql, $params);
foreach ($rs as $course) {
// adding course contexts to cache
context_instance_preload($course);
// cache courses
$courses[$course->id] = $course;
}
$rs->close();
list($uselect, $ujoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
list($sqluserids, $params) = $DB->get_in_or_equal($userids);
$usersql = "SELECT u.* {$uselect}\n FROM {user} u {$ujoin}\n WHERE u.id {$sqluserids}";
$users = $DB->get_recordset_sql($usersql, $params);
$result = array();
foreach ($users as $user) {
if (!empty($user->deleted)) {
continue;
}
context_instance_preload($user);
$course = $courses[$courseids[$user->id]];
$context = context_course::instance($courseids[$user->id], IGNORE_MISSING);
self::validate_context($context);
if ($userarray = user_get_user_details($user, $course)) {
$result[] = $userarray;
}
}
$users->close();
return $result;
}
示例3: required_param
require_once "{$CFG->dirroot}/enrol/locallib.php";
require_once "{$CFG->dirroot}/enrol/renderer.php";
$ueid = required_param('ue', PARAM_INT);
// user enrolment id
$filter = optional_param('ifilter', 0, PARAM_INT);
$confirm = optional_param('confirm', false, PARAM_BOOL);
// Get the user enrolment object
$ue = $DB->get_record('user_enrolments', array('id' => $ueid), '*', MUST_EXIST);
// Get the user for whom the enrolment is
$user = $DB->get_record('user', array('id' => $ue->userid), '*', MUST_EXIST);
// Get the course the enrolment is to
list($ctxsql, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$sql = "SELECT c.* {$ctxsql}\n FROM {course} c\n LEFT JOIN {enrol} e ON e.courseid = c.id\n {$ctxjoin}\n WHERE e.id = :enrolid";
$params = array('enrolid' => $ue->enrolid);
$course = $DB->get_record_sql($sql, $params, MUST_EXIST);
context_instance_preload($course);
if ($course->id == SITEID) {
redirect(new moodle_url('/'));
}
require_login($course);
require_capability("enrol/globalclassroom:unenrol", get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST));
$manager = new course_enrolment_manager($PAGE, $course, $filter);
$table = new course_enrolment_users_table($manager, $PAGE);
// The URL of the enrolled users page for the course.
$usersurl = new moodle_url('/enrol/users.php', array('id' => $course->id));
// The URl to return the user too after this screen.
$returnurl = new moodle_url($usersurl, $manager->get_url_params() + $table->get_url_params());
// The URL of this page
$url = new moodle_url('/enrol/globalclassroom/unenroluser.php', $returnurl->params());
$url->param('ue', $ueid);
$PAGE->set_url($url);
示例4: get_courses_wmanagers
/**
* Retrieve course records with the course managers and other related records
* that we need for print_course(). This allows print_courses() to do its job
* in a constant number of DB queries, regardless of the number of courses,
* role assignments, etc.
*
* The returned array is indexed on c.id, and each course will have
* - $course->managers - array containing RA objects that include a $user obj
* with the minimal fields needed for fullname()
*
* @deprecated since 2.5
*
* To get list of all courses with course contacts ('managers') use
* coursecat::get(0)->get_courses(array('recursive' => true, 'coursecontacts' => true));
*
* To get list of courses inside particular category use
* coursecat::get($id)->get_courses(array('coursecontacts' => true));
*
* Additionally you can specify sort order, offset and maximum number of courses,
* see {@link coursecat::get_courses()}
*
* Please note that code of this function is not changed to use coursecat class because
* coursecat::get_courses() returns result in slightly different format. Also note that
* get_courses_wmanagers() DOES NOT check that users are enrolled in the course and
* coursecat::get_courses() does.
*
* @global object
* @global object
* @global object
* @uses CONTEXT_COURSE
* @uses CONTEXT_SYSTEM
* @uses CONTEXT_COURSECAT
* @uses SITEID
* @param int|string $categoryid Either the categoryid for the courses or 'all'
* @param string $sort A SQL sort field and direction
* @param array $fields An array of additional fields to fetch
* @return array
*/
function get_courses_wmanagers($categoryid = 0, $sort = "c.sortorder ASC", $fields = array())
{
/*
* The plan is to
*
* - Grab the courses JOINed w/context
*
* - Grab the interesting course-manager RAs
* JOINed with a base user obj and add them to each course
*
* So as to do all the work in 2 DB queries. The RA+user JOIN
* ends up being pretty expensive if it happens over _all_
* courses on a large site. (Are we surprised!?)
*
* So this should _never_ get called with 'all' on a large site.
*
*/
global $USER, $CFG, $DB;
debugging('Function get_courses_wmanagers() is deprecated, please use coursecat::get_courses()', DEBUG_DEVELOPER);
$params = array();
$allcats = false;
// bool flag
if ($categoryid === 'all') {
$categoryclause = '';
$allcats = true;
} elseif (is_numeric($categoryid)) {
$categoryclause = "c.category = :catid";
$params['catid'] = $categoryid;
} else {
debugging("Could not recognise categoryid = {$categoryid}");
$categoryclause = '';
}
$basefields = array('id', 'category', 'sortorder', 'shortname', 'fullname', 'idnumber', 'startdate', 'visible', 'newsitems', 'groupmode', 'groupmodeforce');
if (!is_null($fields) && is_string($fields)) {
if (empty($fields)) {
$fields = $basefields;
} else {
// turn the fields from a string to an array that
// get_user_courses_bycap() will like...
$fields = explode(',', $fields);
$fields = array_map('trim', $fields);
$fields = array_unique(array_merge($basefields, $fields));
}
} elseif (is_array($fields)) {
$fields = array_merge($basefields, $fields);
}
$coursefields = 'c.' . join(',c.', $fields);
if (empty($sort)) {
$sortstatement = "";
} else {
$sortstatement = "ORDER BY {$sort}";
}
$where = 'WHERE c.id != ' . SITEID;
if ($categoryclause !== '') {
$where = "{$where} AND {$categoryclause}";
}
// pull out all courses matching the cat
list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$sql = "SELECT {$coursefields} {$ccselect}\n FROM {course} c\n {$ccjoin}\n {$where}\n {$sortstatement}";
$catpaths = array();
$catpath = NULL;
if ($courses = $DB->get_records_sql($sql, $params)) {
//.........这里部分代码省略.........
示例5: get_course_category_tree
/**
* This function generates a structured array of courses and categories.
*
* The depth of categories is limited by $CFG->maxcategorydepth however there
* is no limit on the number of courses!
*
* Suitable for use with the course renderers course_category_tree method:
* $renderer = $PAGE->get_renderer('core','course');
* echo $renderer->course_category_tree(get_course_category_tree());
*
* @global moodle_database $DB
* @param int $id
* @param int $depth
*/
function get_course_category_tree($id = 0, $depth = 0)
{
global $DB, $CFG;
$viewhiddencats = has_capability('moodle/category:viewhiddencategories', get_context_instance(CONTEXT_SYSTEM));
$categories = get_child_categories($id);
$categoryids = array();
foreach ($categories as $key => &$category) {
if (!$category->visible && !$viewhiddencats) {
unset($categories[$key]);
continue;
}
$categoryids[$category->id] = $category;
if (empty($CFG->maxcategorydepth) || $depth <= $CFG->maxcategorydepth) {
list($category->categories, $subcategories) = get_course_category_tree($category->id, $depth + 1);
foreach ($subcategories as $subid => $subcat) {
$categoryids[$subid] = $subcat;
}
$category->courses = array();
}
}
if ($depth > 0) {
// This is a recursive call so return the required array
return array($categories, $categoryids);
}
// The depth is 0 this function has just been called so we can finish it off
list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
list($catsql, $catparams) = $DB->get_in_or_equal(array_keys($categoryids));
$sql = "SELECT\n c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.summary,c.category\n {$ccselect}\n FROM {course} c\n {$ccjoin}\n WHERE c.category {$catsql} ORDER BY c.sortorder ASC";
if ($courses = $DB->get_records_sql($sql, $catparams)) {
// loop throught them
foreach ($courses as $course) {
if ($course->id == SITEID) {
continue;
}
context_instance_preload($course);
if (!empty($course->visible) || has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) {
$categoryids[$course->category]->courses[$course->id] = $course;
}
}
}
return $categories;
}
示例6: get_categories
/**
* Returns a sorted list of categories. Each category object has a context
* property that is a context object.
*
* When asking for $parent='none' it will return all the categories, regardless
* of depth. Wheen asking for a specific parent, the default is to return
* a "shallow" resultset. Pass false to $shallow and it will return all
* the child categories as well.
*
* @global object
* @uses CONTEXT_COURSECAT
* @param string $parent The parent category if any
* @param string $sort the sortorder
* @param bool $shallow - set to false to get the children too
* @return array of categories
*/
function get_categories($parent = 'none', $sort = NULL, $shallow = true)
{
global $DB;
if ($sort === NULL) {
$sort = 'ORDER BY cc.sortorder ASC';
} elseif ($sort === '') {
// leave it as empty
} else {
$sort = "ORDER BY {$sort}";
}
list($ccselect, $ccjoin) = context_instance_preload_sql('cc.id', CONTEXT_COURSECAT, 'ctx');
if ($parent === 'none') {
$sql = "SELECT cc.* {$ccselect}\n FROM {course_categories} cc\n {$ccjoin}\n {$sort}";
$params = array();
} elseif ($shallow) {
$sql = "SELECT cc.* {$ccselect}\n FROM {course_categories} cc\n {$ccjoin}\n WHERE cc.parent=?\n {$sort}";
$params = array($parent);
} else {
$sql = "SELECT cc.* {$ccselect}\n FROM {course_categories} cc\n {$ccjoin}\n JOIN {course_categories} ccp\n ON ((cc.parent = ccp.id) OR (cc.path LIKE " . $DB->sql_concat('ccp.path', "'/%'") . "))\n WHERE ccp.id=?\n {$sort}";
$params = array($parent);
}
$categories = array();
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $cat) {
context_instance_preload($cat);
$catcontext = get_context_instance(CONTEXT_COURSECAT, $cat->id);
if ($cat->visible || has_capability('moodle/category:viewhiddencategories', $catcontext)) {
$categories[$cat->id] = $cat;
}
}
$rs->close();
return $categories;
}
示例7: get_users_by_courseid
/**
* Get course participants details
* @param int $courseid course id
* @param array $options options {
* 'name' => option name
* 'value' => option value
* }
* @return array An array of users
*/
public static function get_users_by_courseid($courseid, $options) {
global $CFG, $USER, $DB;
require_once($CFG->dirroot . "/user/lib.php");
$params = self::validate_parameters(
self::get_users_by_courseid_parameters(),
array(
'courseid'=>$courseid,
'options'=>$options
)
);
$withcapability = '';
$groupid = 0;
$onlyactive = false;
foreach ($options as $option) {
switch ($option['name']) {
case 'withcapability':
$withcapability = $option['value'];
break;
case 'groupid':
$groupid = (int)$option['value'];
break;
case 'onlyactive':
$onlyactive = !empty($option['value']);
break;
}
}
// to overwrite this parameter, you need role:review capability
if ($withcapability) {
require_capability('moodle/role:review', $coursecontext);
}
// need accessallgroups capability if you want to overwrite this option
if (!empty($groupid) && groups_is_member($groupid)) {
require_capability('moodle/site:accessallgroups', $context);
}
// to overwrite this option, you need course:enrolereview permission
if ($onlyactive) {
require_capability('moodle/course:enrolreview', $coursecontext);
}
list($coursectxselect, $coursectxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$coursesql = "SELECT c.* $coursectxselect
FROM {course} c $coursectxjoin
WHERE c.id = $courseid";
$course = $DB->get_record_sql($coursesql);
context_instance_preload($course);
$coursecontext = get_context_instance(CONTEXT_COURSE, $params['courseid']);
if ($courseid == SITEID) {
$context = get_system_context();
} else {
$context = $coursecontext;
}
try {
self::validate_context($context);
} catch (Exception $e) {
$exceptionparam = new stdClass();
$exceptionparam->message = $e->getMessage();
$exceptionparam->courseid = $params['courseid'];
throw new moodle_exception(get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
}
list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
list($ctxselect, $ctxjoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
$records = $DB->get_records_sql($enrolledsql, $enrolledparams);
$sqlparams['courseid'] = $courseid;
$sql = "SELECT u.* $ctxselect
FROM {user} u $ctxjoin
WHERE u.id IN ($enrolledsql)
ORDER BY u.id ASC";
$enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams);
$users = array();
foreach ($enrolledusers as $user) {
if (!empty($user->deleted)) {
continue;
}
context_instance_preload($user);
if ($userdetails = user_get_user_details($user, $course)) {
$users[] = $userdetails;
}
}
$enrolledusers->close();
return $users;
}
示例8: get_courses_search
/**
* A list of courses that match a search
*
* @global object
* @global object
* @param array $searchterms An array of search criteria
* @param string $sort A field and direction to sort by
* @param int $page The page number to get
* @param int $recordsperpage The number of records per page
* @param int $totalcount Passed in by reference.
* @return object {@link $COURSE} records
*/
function get_courses_search($searchterms, $sort, $page, $recordsperpage, &$totalcount)
{
global $CFG, $DB;
if ($DB->sql_regex_supported()) {
$REGEXP = $DB->sql_regex(true);
$NOTREGEXP = $DB->sql_regex(false);
}
$searchcond = array();
$params = array();
$i = 0;
// Thanks Oracle for your non-ansi concat and type limits in coalesce. MDL-29912
if ($DB->get_dbfamily() == 'oracle') {
$concat = "(c.summary|| ' ' || c.fullname || ' ' || c.idnumber || ' ' || c.shortname)";
} else {
$concat = $DB->sql_concat("COALESCE(c.summary, '')", "' '", 'c.fullname', "' '", 'c.idnumber', "' '", 'c.shortname');
}
foreach ($searchterms as $searchterm) {
$i++;
$NOT = false;
/// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
/// will use it to simulate the "-" operator with LIKE clause
/// Under Oracle and MSSQL, trim the + and - operators and perform
/// simpler LIKE (or NOT LIKE) queries
if (!$DB->sql_regex_supported()) {
if (substr($searchterm, 0, 1) == '-') {
$NOT = true;
}
$searchterm = trim($searchterm, '+-');
}
// TODO: +- may not work for non latin languages
if (substr($searchterm, 0, 1) == '+') {
$searchterm = trim($searchterm, '+-');
$searchterm = preg_quote($searchterm, '|');
$searchcond[] = "{$concat} {$REGEXP} :ss{$i}";
$params['ss' . $i] = "(^|[^a-zA-Z0-9]){$searchterm}([^a-zA-Z0-9]|\$)";
} else {
if (substr($searchterm, 0, 1) == "-") {
$searchterm = trim($searchterm, '+-');
$searchterm = preg_quote($searchterm, '|');
$searchcond[] = "{$concat} {$NOTREGEXP} :ss{$i}";
$params['ss' . $i] = "(^|[^a-zA-Z0-9]){$searchterm}([^a-zA-Z0-9]|\$)";
} else {
$searchcond[] = $DB->sql_like($concat, ":ss{$i}", false, true, $NOT);
$params['ss' . $i] = "%{$searchterm}%";
}
}
}
if (empty($searchcond)) {
$totalcount = 0;
return array();
}
$searchcond = implode(" AND ", $searchcond);
$courses = array();
$c = 0;
// counts how many visible courses we've seen
// Tiki pagination
$limitfrom = $page * $recordsperpage;
$limitto = $limitfrom + $recordsperpage;
list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$fields = array_diff(array_keys($DB->get_columns('course')), array('modinfo', 'sectioncache'));
$sql = "SELECT c." . join(',c.', $fields) . " {$ccselect}\n FROM {course} c\n {$ccjoin}\n WHERE {$searchcond} AND c.id <> " . SITEID . "\n ORDER BY {$sort}";
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $course) {
if (!$course->visible) {
// preload contexts only for hidden courses or courses we need to return
context_instance_preload($course);
$coursecontext = context_course::instance($course->id);
if (!has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
continue;
}
}
// Don't exit this loop till the end
// we need to count all the visible courses
// to update $totalcount
if ($c >= $limitfrom && $c < $limitto) {
$courses[$course->id] = $course;
}
$c++;
}
$rs->close();
// our caller expects 2 bits of data - our return
// array, and an updated $totalcount
$totalcount = $c;
return $courses;
}
示例9: load_all_courses
/**
* Loads of the the courses in Moodle into the navigation.
*
* @param string|array $categoryids Either a string or array of category ids to load courses for
* @return array An array of navigation_node
*/
protected function load_all_courses($categoryids=null) {
global $CFG, $DB, $USER;
if ($categoryids !== null) {
if (is_array($categoryids)) {
list ($select, $params) = $DB->get_in_or_equal($categoryids);
} else {
$select = '= ?';
$params = array($categoryids);
}
array_unshift($params, SITEID);
$select = ' AND c.category '.$select;
} else {
$params = array(SITEID);
$select = '';
}
list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$sql = "SELECT c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.category,cat.path AS categorypath $ccselect
FROM {course} c
$ccjoin
LEFT JOIN {course_categories} cat ON cat.id=c.category
WHERE c.id <> ?$select
ORDER BY c.sortorder ASC";
$limit = 20;
if (!empty($CFG->navcourselimit)) {
$limit = $CFG->navcourselimit;
}
$courses = $DB->get_records_sql($sql, $params, 0, $limit);
$coursenodes = array();
foreach ($courses as $course) {
context_instance_preload($course);
$coursenodes[$course->id] = $this->add_course($course);
}
return $coursenodes;
}
示例10: enrol_get_users_courses
/**
* Returns list of courses user is enrolled into.
*
* - $fields is an array of fieldnames to ADD
* so name the fields you really need, which will
* be added and uniq'd
*
* @param int $userid
* @param bool $onlyactive return only active enrolments in courses user may see
* @param string|array $fields
* @param string $sort
* @return array
*/
function enrol_get_users_courses($userid, $onlyactive = false, $fields = NULL, $sort = 'visible DESC,sortorder ASC')
{
global $DB;
// Guest account does not have any courses
if (isguestuser($userid) or empty($userid)) {
return array();
}
$basefields = array('id', 'category', 'sortorder', 'shortname', 'fullname', 'idnumber', 'startdate', 'visible', 'groupmode', 'groupmodeforce');
if (empty($fields)) {
$fields = $basefields;
} else {
if (is_string($fields)) {
// turn the fields from a string to an array
$fields = explode(',', $fields);
$fields = array_map('trim', $fields);
$fields = array_unique(array_merge($basefields, $fields));
} else {
if (is_array($fields)) {
$fields = array_unique(array_merge($basefields, $fields));
} else {
throw new coding_exception('Invalid $fileds parameter in enrol_get_my_courses()');
}
}
}
if (in_array('*', $fields)) {
$fields = array('*');
}
$orderby = "";
$sort = trim($sort);
if (!empty($sort)) {
$rawsorts = explode(',', $sort);
$sorts = array();
foreach ($rawsorts as $rawsort) {
$rawsort = trim($rawsort);
if (strpos($rawsort, 'c.') === 0) {
$rawsort = substr($rawsort, 2);
}
$sorts[] = trim($rawsort);
}
$sort = 'c.' . implode(',c.', $sorts);
$orderby = "ORDER BY {$sort}";
}
$params = array('siteid' => SITEID);
if ($onlyactive) {
$subwhere = "WHERE ue.status = :active AND e.status = :enabled AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)";
$params['now1'] = round(time(), -2);
// improves db caching
$params['now2'] = $params['now1'];
$params['active'] = ENROL_USER_ACTIVE;
$params['enabled'] = ENROL_INSTANCE_ENABLED;
} else {
$subwhere = "";
}
$coursefields = 'c.' . join(',c.', $fields);
list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
//note: we can not use DISTINCT + text fields due to Oracle and MS limitations, that is why we have the subselect there
$sql = "SELECT {$coursefields} {$ccselect}\n FROM {course} c\n JOIN (SELECT DISTINCT e.courseid\n FROM {enrol} e\n JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = :userid)\n {$subwhere}\n ) en ON (en.courseid = c.id)\n {$ccjoin}\n WHERE c.id <> :siteid\n {$orderby}";
$params['userid'] = $userid;
$courses = $DB->get_records_sql($sql, $params);
// preload contexts and check visibility
foreach ($courses as $id => $course) {
context_instance_preload($course);
if ($onlyactive) {
if (!$course->visible) {
if (!($context = get_context_instance(CONTEXT_COURSE, $id))) {
unset($courses[$id]);
continue;
}
if (!has_capability('moodle/course:viewhiddencourses', $context, $userid)) {
unset($courses[$id]);
continue;
}
}
}
$courses[$id] = $course;
}
//wow! Is that really all? :-D
return $courses;
}
示例11: html_table
$table = new html_table();
$table->head = array(get_string("course"), get_string("timetaken", "quiz"), get_string("status"), get_string("backupnext"));
$table->headspan = array(1, 3, 1, 1);
$table->attributes = array('class' => 'generaltable backup-report');
$table->data = array();
$strftimedatetime = get_string("strftimerecent");
$strerror = get_string("error");
$strok = get_string("ok");
$strunfinished = get_string("unfinished");
$strskipped = get_string("skipped");
list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$sql = "SELECT bc.*, c.fullname {$select}\n FROM {backup_courses} bc\n JOIN {course} c ON c.id = bc.courseid\n {$join}";
$rs = $DB->get_recordset_sql($sql);
foreach ($rs as $backuprow) {
// Cache the course context
context_instance_preload($backuprow);
// Prepare a cell to display the status of the entry
if ($backuprow->laststatus == 1) {
$status = $strok;
$statusclass = 'backup-ok';
// Green
} else {
if ($backuprow->laststatus == 2) {
$status = $strunfinished;
$statusclass = 'backup-unfinished';
// Red
} else {
if ($backuprow->laststatus == 3) {
$status = $strskipped;
$statusclass = 'backup-skipped';
// Green
示例12: get_users_by_id
/**
* Get user information
* - This function is matching the permissions of /user/profil.php
* - It is also matching some permissions from /user/editadvanced.php for the following fields:
* auth, confirmed, idnumber, lang, theme, timezone, mailformat
*
* @param array $userids array of user ids
* @return array An array of arrays describing users
* @since Moodle 2.2
*/
public static function get_users_by_id($userids)
{
global $CFG, $USER, $DB;
require_once $CFG->dirroot . "/user/lib.php";
//iteramos los parametros y reemplazamos por los ID
if (is_array($userids)) {
foreach ($userids as $indice => $dni) {
$u = $DB->get_record('user', array('username' => $dni));
if (is_object($u)) {
$userids[$indice] = $u->id;
}
}
}
$params = self::validate_parameters(self::get_users_by_id_parameters(), array('userids' => $userids));
list($uselect, $ujoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
list($sqluserids, $params) = $DB->get_in_or_equal($userids);
$usersql = "SELECT u.* {$uselect}\n FROM {user} u {$ujoin}\n WHERE u.id {$sqluserids}";
$users = $DB->get_recordset_sql($usersql, $params);
$result = array();
$hasuserupdatecap = has_capability('moodle/user:update', get_system_context());
foreach ($users as $user) {
if (!empty($user->deleted)) {
continue;
}
context_instance_preload($user);
$usercontext = get_context_instance(CONTEXT_USER, $user->id);
self::validate_context($usercontext);
$currentuser = $user->id == $USER->id;
if ($userarray = user_get_user_details($user)) {
//fields matching permissions from /user/editadvanced.php
if ($currentuser or $hasuserupdatecap) {
$userarray['auth'] = $user->auth;
$userarray['confirmed'] = $user->confirmed;
$userarray['idnumber'] = $user->idnumber;
$userarray['lang'] = $user->lang;
$userarray['theme'] = $user->theme;
$userarray['timezone'] = $user->timezone;
$userarray['mailformat'] = $user->mailformat;
}
$result[] = $userarray;
}
}
$users->close();
return $result;
}
示例13: test_everything_in_accesslib
//.........这里部分代码省略.........
$prevsize = context_inspection::test_context_cache_size();
for ($i = 0; $i < 100; $i++) {
context_user::instance($testusers[$i]);
$this->assertEqual(context_inspection::test_context_cache_size(), $prevsize);
}
context_user::instance($testusers[102]);
$this->assertEqual(context_inspection::test_context_cache_size(), $prevsize + 1);
unset($testusers);
// =================================================================
// ======= basic test of legacy functions ==========================
// =================================================================
// note: watch out, the fake site might be pretty borked already
$this->assertIdentical(get_system_context(), context_system::instance());
foreach ($DB->get_records('context') as $contextid => $record) {
$context = context::instance_by_id($contextid);
$this->assertIdentical(get_context_instance_by_id($contextid), $context);
$this->assertIdentical(get_context_instance($record->contextlevel, $record->instanceid), $context);
$this->assertIdentical(get_parent_contexts($context), $context->get_parent_context_ids());
if ($context->id == SYSCONTEXTID) {
$this->assertIdentical(get_parent_contextid($context), false);
} else {
$this->assertIdentical(get_parent_contextid($context), $context->get_parent_context()->id);
}
}
$children = get_child_contexts($systemcontext);
$this->assertEqual(count($children), $DB->count_records('context') - 1);
unset($children);
$DB->delete_records('context', array('contextlevel' => CONTEXT_BLOCK));
create_contexts();
$this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK)));
$DB->set_field('context', 'depth', 0, array('contextlevel' => CONTEXT_BLOCK));
build_context_path();
$this->assertFalse($DB->record_exists('context', array('depth' => 0)));
$lastcourse = $DB->get_field_sql("SELECT MAX(id) FROM {course}");
$DB->delete_records('course', array('id' => $lastcourse));
$lastcategory = $DB->get_field_sql("SELECT MAX(id) FROM {course_categories}");
$DB->delete_records('course_categories', array('id' => $lastcategory));
$lastuser = $DB->get_field_sql("SELECT MAX(id) FROM {user} WHERE deleted=0");
$DB->delete_records('user', array('id' => $lastuser));
$DB->delete_records('block_instances', array('parentcontextid' => $frontpagepagecontext->id));
$DB->delete_records('course_modules', array('id' => $frontpagepagecontext->instanceid));
cleanup_contexts();
$count = 1;
//system
$count += $DB->count_records('user', array('deleted' => 0));
$count += $DB->count_records('course_categories');
$count += $DB->count_records('course');
$count += $DB->count_records('course_modules');
$count += $DB->count_records('block_instances');
$this->assertEqual($DB->count_records('context'), $count);
context_helper::reset_caches();
preload_course_contexts($SITE->id);
$this->assertEqual(context_inspection::test_context_cache_size(), 1);
context_helper::reset_caches();
list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx');
$sql = "SELECT c.id {$select} FROM {course_categories} c {$join}";
$records = $DB->get_records_sql($sql);
foreach ($records as $record) {
context_instance_preload($record);
$record = (array) $record;
$this->assertEqual(1, count($record));
// only id left
}
$this->assertEqual(count($records), context_inspection::test_context_cache_size());
accesslib_clear_all_caches(true);
$DB->delete_records('cache_flags', array());
mark_context_dirty($systemcontext->path);
$dirty = get_cache_flags('accesslib/dirtycontexts', time() - 2);
$this->assertTrue(isset($dirty[$systemcontext->path]));
accesslib_clear_all_caches(false);
$DB->delete_records('cache_flags', array());
$course = $DB->get_record('course', array('id' => $testcourses[2]));
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$oldpath = $context->path;
$miscid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}");
$categorycontext = context_coursecat::instance($miscid);
$course->category = $miscid;
$DB->update_record('course', $course);
context_moved($context, $categorycontext);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$this->assertIdentical($context->get_parent_context(), $categorycontext);
$this->assertTrue($DB->record_exists('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $testcourses[2])));
delete_context(CONTEXT_COURSE, $testcourses[2]);
$this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $testcourses[2])));
$name = get_contextlevel_name(CONTEXT_COURSE);
$this->assertFalse(empty($name));
$context = get_context_instance(CONTEXT_COURSE, $testcourses[2]);
$name = print_context_name($context);
$this->assertFalse(empty($name));
$url = get_context_url($coursecontext);
$this->assertFalse($url instanceof modole_url);
$page = $DB->get_record('page', array('id' => $testpages[7]));
$context = get_context_instance(CONTEXT_MODULE, $page->id);
$coursecontext = get_course_context($context);
$this->assertEqual($coursecontext->contextlevel, CONTEXT_COURSE);
$this->assertEqual(get_courseid_from_context($context), $page->course);
$caps = fetch_context_capabilities($systemcontext);
$this->assertTrue(is_array($caps));
unset($caps);
}
示例14: test_everything_in_accesslib
//.........这里部分代码省略.........
$context = context::instance_by_id($contextid);
$this->assertSame(get_context_instance_by_id($contextid), $context);
$this->assertSame(get_context_instance($record->contextlevel, $record->instanceid), $context);
$this->assertSame(get_parent_contexts($context), $context->get_parent_context_ids());
if ($context->id == SYSCONTEXTID) {
$this->assertSame(get_parent_contextid($context), false);
} else {
$this->assertSame(get_parent_contextid($context), $context->get_parent_context()->id);
}
}
$CFG->debug = 0;
$children = get_child_contexts($systemcontext);
$CFG->debug = DEBUG_DEVELOPER;
$this->assertEquals(count($children), $DB->count_records('context')-1);
unset($children);
$DB->delete_records('context', array('contextlevel'=>CONTEXT_BLOCK));
create_contexts();
$this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_BLOCK)));
$DB->set_field('context', 'depth', 0, array('contextlevel'=>CONTEXT_BLOCK));
build_context_path();
$this->assertFalse($DB->record_exists('context', array('depth'=>0)));
$lastcourse = $DB->get_field_sql("SELECT MAX(id) FROM {course}");
$DB->delete_records('course', array('id'=>$lastcourse));
$lastcategory = $DB->get_field_sql("SELECT MAX(id) FROM {course_categories}");
$DB->delete_records('course_categories', array('id'=>$lastcategory));
$lastuser = $DB->get_field_sql("SELECT MAX(id) FROM {user} WHERE deleted=0");
$DB->delete_records('user', array('id'=>$lastuser));
$DB->delete_records('block_instances', array('parentcontextid'=>$frontpagepagecontext->id));
$DB->delete_records('course_modules', array('id'=>$frontpagepagecontext->instanceid));
cleanup_contexts();
$count = 1; //system
$count += $DB->count_records('user', array('deleted'=>0));
$count += $DB->count_records('course_categories');
$count += $DB->count_records('course');
$count += $DB->count_records('course_modules');
$count += $DB->count_records('block_instances');
$this->assertEquals($DB->count_records('context'), $count);
context_helper::reset_caches();
preload_course_contexts($SITE->id);
$this->assertEquals(context_inspection::test_context_cache_size(), 1);
context_helper::reset_caches();
list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx');
$sql = "SELECT c.id $select FROM {course_categories} c $join";
$records = $DB->get_records_sql($sql);
foreach ($records as $record) {
context_instance_preload($record);
$record = (array)$record;
$this->assertEquals(1, count($record)); // only id left
}
$this->assertEquals(count($records), context_inspection::test_context_cache_size());
accesslib_clear_all_caches(true);
$DB->delete_records('cache_flags', array());
mark_context_dirty($systemcontext->path);
$dirty = get_cache_flags('accesslib/dirtycontexts', time()-2);
$this->assertTrue(isset($dirty[$systemcontext->path]));
accesslib_clear_all_caches(false);
$DB->delete_records('cache_flags', array());
$course = $DB->get_record('course', array('id'=>$testcourses[2]));
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$oldpath = $context->path;
$miscid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}");
$categorycontext = context_coursecat::instance($miscid);
$course->category = $miscid;
$DB->update_record('course', $course);
context_moved($context, $categorycontext);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$this->assertEquals($context->get_parent_context(), $categorycontext);
$this->assertTrue($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$testcourses[2])));
delete_context(CONTEXT_COURSE, $testcourses[2]);
$this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$testcourses[2])));
$name = get_contextlevel_name(CONTEXT_COURSE);
$this->assertFalse(empty($name));
$context = get_context_instance(CONTEXT_COURSE, $testcourses[2]);
$name = print_context_name($context);
$this->assertFalse(empty($name));
$url = get_context_url($coursecontext);
$this->assertFalse($url instanceof modole_url);
$page = $DB->get_record('page', array('id'=>$testpages[7]));
$context = get_context_instance(CONTEXT_MODULE, $page->id);
$coursecontext = get_course_context($context);
$this->assertEquals($coursecontext->contextlevel, CONTEXT_COURSE);
$this->assertEquals(get_courseid_from_context($context), $page->course);
$caps = fetch_context_capabilities($systemcontext);
$this->assertTrue(is_array($caps));
unset($caps);
}
示例15: calendar_get_default_courses
/**
* Returns the default courses to display on the calendar when there isn't a specific
* course to display.
*
* @global moodle_database $DB
* @return array Array of courses to display
*/
function calendar_get_default_courses()
{
global $CFG, $DB;
if (!isloggedin()) {
return array();
}
$courses = array();
if (!empty($CFG->calendar_adminseesall) && has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM))) {
list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$sql = "SELECT c.* {$select}\n FROM {course} c\n JOIN {event} e ON e.courseid = c.id\n {$join}";
$courses = $DB->get_records_sql($sql, null, 0, 20);
foreach ($courses as $course) {
context_instance_preload($course);
}
return $courses;
}
$courses = enrol_get_my_courses();
return $courses;
}