$disable_meta = get_string('metaalreadyinmeta');
} else if ($course->metacourse) {
- if (count_records('course_meta', 'parent_course', $course->id) > 0) {
+ if ($DB->count_records('course_meta', array('parent_course'=>$course->id)) > 0) {
$disable_meta = get_string('metaalreadyhascourses');
}
$mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
$mform->setHelpButton('fullname', array('coursefullname', get_string('fullnamecourse')), true);
- $mform->setDefault('fullname', get_string('defaultcoursefullname'));
$mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
$mform->setType('fullname', PARAM_MULTILANG);
$mform->addElement('text','shortname', get_string('shortnamecourse'),'maxlength="100" size="20"');
$mform->setHelpButton('shortname', array('courseshortname', get_string('shortnamecourse')), true);
- $mform->setDefault('shortname', get_string('defaultcourseshortname'));
$mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
$mform->setType('shortname', PARAM_MULTILANG);
+ $fullname = get_string('defaultcoursefullname');
+ $shortname = get_string('defaultcourseshortname');
+ while ($DB->record_exists('course', array('fullname'=>$fullname))
+ or $DB->record_exists('course', array('fullname'=>$fullname))) {
+ $fullname++;
+ $shortname++;
+ }
+ $mform->setDefault('fullname', $fullname);
+ $mform->setDefault('shortname', $shortname);
+
+
$mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
$mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
$mform->setType('idnumber', PARAM_RAW);
$roles = get_assignable_roles($context);
if (!empty($course)) {
// add current default role, so that it is selectable even when user can not assign it
- if ($current_role = get_record('role', 'id', $course->defaultrole)) {
+ if ($current_role = $DB->get_record('role', array('id'=>$course->defaultrole))) {
$roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
}
}
$choices = array();
- if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
+ if ($sitedefaultrole = $DB->get_record('role', array('id'=>$CFG->defaultcourseroleid))) {
$choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
} else {
$choices[0] = get_string('sitedefault');
$mform->addElement('header','rolerenaming', get_string('rolerenaming'));
$mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
- if ($roles = get_records('role')) {
+ if ($roles = $DB->get_records('role')) {
foreach ($roles as $role) {
$mform->addElement('text', 'role_'.$role->id, $role->name);
if ($coursecontext) {
- if ($rolename = get_record('role_names', 'roleid', $role->id, 'contextid', $coursecontext->id)) {
+ if ($rolename = $DB->get_record('role_names', array('roleid'=>$role->id, 'contextid'=>$coursecontext->id))) {
$mform->setDefault('role_'.$role->id, $rolename->name);
}
}
}
function definition_after_data() {
- global $CFG;
+ global $DB;
$mform =& $this->_form;
// add availabe groupings
if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
$options = array();
- if ($groupings = get_records('groupings', 'courseid', $courseid)) {
+ if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
foreach ($groupings as $grouping) {
$options[$grouping->id] = format_string($grouping->name);
}
/// perform some extra moodle validation
function validation($data, $files) {
+ global $DB;
+
$errors = parent::validation($data, $files);
- if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
+ if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
if (!empty($data['id'])) {
unset($foundcourses[$data['id']]);
}
function build_logs_array($course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='',
$modname="", $modid=0, $modaction="", $groupid=0) {
-
+ global $DB;
// It is assumed that $date is the GMT time of midnight for that day,
// and so the next 86400 seconds worth of logs are printed.
}
$joins = array();
+ $oarams = array();
if ($course->id != SITEID || $modid != 0) {
- $joins[] = "l.course='$course->id'";
+ $joins[] = "l.course = :courseid";
+ $params['courseid'] = $course->id;
}
if ($modname) {
- $joins[] = "l.module = '$modname'";
+ $joins[] = "l.module = :modname";
+ $params['modname'] = $modname;
}
if ('site_errors' === $modid) {
$joins[] = "( l.action='error' OR l.action='infected' )";
} else if ($modid) {
- $joins[] = "l.cmid = '$modid'";
+ $joins[] = "l.cmid = :modid";
+ $params['modid'] = $modid;
}
if ($modaction) {
+ $ILIKE = $DB->sql_ilike();
$firstletter = substr($modaction, 0, 1);
if (preg_match('/[[:alpha:]]/', $firstletter)) {
- $joins[] = "lower(l.action) LIKE '%" . strtolower($modaction) . "%'";
+ $joins[] = "l.action $ILIKE :modaction";
+ $params['modaction'] = '%'.$modaction.'%';
} else if ($firstletter == '-') {
- $joins[] = "lower(l.action) NOT LIKE '%" . strtolower(substr($modaction, 1)) . "%'";
+ $joins[] = "l.action NOT $ILIKE :modaction";
+ $params['modaction'] = '%'.substr($modaction, 1).'%';
}
}
}
}
else if ($user) {
- $joins[] = "l.userid = '$user'";
+ $joins[] = "l.userid = :userid";
+ $params['userid'] = $user;
}
if ($date) {
$enddate = $date + 86400;
- $joins[] = "l.time > '$date' AND l.time < '$enddate'";
+ $joins[] = "l.time > :date AND l.time < :enddate";
+ $params['date'] = $date;
+ $params['enddate'] = $enddate;
}
$selector = implode(' AND ', $joins);
$totalcount = 0; // Initialise
$result = array();
- $result['logs'] = get_logs($selector, $order, $limitfrom, $limitnum, $totalcount);
+ $result['logs'] = get_logs($selector, $params, $order, $limitfrom, $limitnum, $totalcount);
$result['totalcount'] = $totalcount;
return $result;
}
}
-/*
+/**
* Create a course and either return a $course object or false
*
* @param object $data - all the data needed for an entry in the 'course' table
*/
function create_course($data) {
- global $CFG, $USER;
+ global $CFG, $USER, $DB;
// preprocess allowed mods
$allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
// place at beginning of category
fix_course_sortorder();
- $data->sortorder = get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$data->category");
+ $data->sortorder = $DB->get_field_sql("SELECT MIN(sortorder)-1 FROM {course} WHERE category=?", array($data->category));
if (empty($data->sortorder)) {
$data->sortorder = 100;
}
- if ($newcourseid = insert_record('course', $data)) { // Set up new course
+ if ($newcourseid = $DB->insert_record('course', $data)) { // Set up new course
- $course = get_record('course', 'id', $newcourseid);
+ $course = $DB->get_record('course', array('id'=>$newcourseid));
// Setup the blocks
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
$section = new object();
$section->course = $course->id; // Create a default section.
$section->section = 0;
- $section->id = insert_record('course_sections', $section);
+ $section->id = $DB->insert_record('course_sections', $section);
fix_course_sortorder();
}
-/*
+/**
* Update a course and return true or false
*
* @param object $data - all the data needed for an entry in the 'course' table
*/
function update_course($data) {
- global $USER, $CFG;
+ global $USER, $CFG, $DB;
// Preprocess allowed mods
$allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
}
$movecat = false;
- $oldcourse = get_record('course', 'id', $data->id); // should not fail, already tested above
+ $oldcourse = $DB->get_record('course', array('id'=>$data->id)); // should not fail, already tested above
if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $oldcourse->category))
or !has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $data->category))) {
// can not move to new category, keep the old one
unset($data->category);
+
} elseif ($oldcourse->category != $data->category) {
$movecat = true;
}
// Update with the new data
- if (update_record('course', $data)) {
+ if ($DB->update_record('course', $data)) {
- $course = get_record('course', 'id', $data->id);
+ $course = $DB->get_record('course', array('id'=>$data->id));
add_to_log($course->id, "course", "update", "edit.php?id=$course->id", $course->id);
if (empty($dvalue)) {
- delete_records('role_names', 'contextid', $context->id, 'roleid', $roleid);
+ $DB->delete_records('role_names', array('contextid'=>$context->id, 'roleid'=>$roleid));
- } else if ($t = get_record('role_names', 'contextid', $context->id, 'roleid', $roleid)) {
+ } else if ($t = $DB->get_record('role_names', array('contextid'=>$context->id, 'roleid'=>$roleid))) {
$t->name = $dvalue;
- update_record('role_names', $t);
+ $DB->update_record('role_names', $t);
} else {
$t->contextid = $context->id;
$t->roleid = $roleid;
$t->name = $dvalue;
- insert_record('role_names', $t);
+ $DB->insert_record('role_names', $t);
}
}
* safely from 1.4 to 1.5
*/
function fix_course_sortorder($categoryid=0, $n=0, $safe=0, $depth=0, $path='') {
-
- global $CFG;
+ global $CFG, $DB;
$count = 0;
if ($categoryid > 0){
// update depth and path
- $cat = get_record('course_categories', 'id', $categoryid);
+ $cat = $DB->get_record('course_categories', array('id'=>$categoryid));
if ($cat->parent == 0) {
$depth = 0;
$path = '';
} else if ($depth == 0 ) { // doesn't make sense; get from DB
// this is only called if the $depth parameter looks dodgy
- $parent = get_record('course_categories', 'id', $cat->parent);
+ $parent = $DB->get_record('course_categories', array('id'=>$cat->parent));
$path = $parent->path;
$depth = $parent->depth;
}
$depth = $depth + 1;
if ($cat->path !== $path) {
- set_field('course_categories', 'path', addslashes($path), 'id', $categoryid);
+ $DB->set_field('course_categories', 'path', $path, array('id'=>$categoryid));
}
if ($cat->depth != $depth) {
- set_field('course_categories', 'depth', $depth, 'id', $categoryid);
+ $DB->set_field('course_categories', 'depth', $depth, array('id'=>$categoryid));
}
}
// get some basic info about courses in the category
- $info = get_record_sql('SELECT MIN(sortorder) AS min,
- MAX(sortorder) AS max,
- COUNT(sortorder) AS count
- FROM ' . $CFG->prefix . 'course
- WHERE category=' . $categoryid);
- if (is_object($info)) { // no courses?
+ $info = $DB->get_record_sql("SELECT MIN(sortorder) AS min,
+ MAX(sortorder) AS max,
+ COUNT(sortorder) AS count
+ FROM {course}
+ WHERE category=?", array($categoryid));
+ if ($info) { // no courses?
$max = $info->max;
$count = $info->count;
$min = $info->min;
$shift = $count + $catgap;
}
// UPDATE course SET sortorder=sortorder+$shift
- execute_sql("UPDATE {$CFG->prefix}course
- SET sortorder=sortorder+$shift
- WHERE category=$categoryid", 0);
+ $DB->execute("UPDATE {course}
+ SET sortorder=sortorder+?
+ WHERE category=?", array($shift, $categoryid));
$n = $n + $catgap + $count;
} else { // do it slowly
// will stop us -- shift things aside for a moment...
if ($safe || ($n >= $min && $n+$count+1 < $min && $CFG->dbfamily==='mysql')) {
$shift = $max + $n + 1000;
- execute_sql("UPDATE {$CFG->prefix}course
- SET sortorder=sortorder+$shift
- WHERE category=$categoryid", 0);
+ $DB->execute_sql("UPDATE {$CFG->prefix}course
+ SET sortorder=sortorder+?
+ WHERE category=?", array($shift, $categoryid));
}
$courses = get_courses($categoryid, 'c.sortorder ASC', 'c.id,c.sortorder');
- begin_sql();
+ $DB->begin_sql();
$tx = true; // transaction sanity
foreach ($courses as $course) {
if ($tx && $course->sortorder != $n ) { // save db traffic
- $tx = $tx && set_field('course', 'sortorder', $n,
- 'id', $course->id);
+ $tx = $tx && $DB->set_field('course', 'sortorder', $n, array('id'=>$course->id));
}
$n++;
}
if ($tx) {
- commit_sql();
+ $DB->commit_sql();
} else {
- rollback_sql();
+ $DB->rollback_sql();
if (!$safe) {
// if we failed when called with !safe, try
// to recover calling self with safe=true
}
}
}
- set_field('course_categories', 'coursecount', $count, 'id', $categoryid);
+ $DB->set_field('course_categories', 'coursecount', $count, array('id'=>$categoryid));
// $n could need updating
- $max = get_field_sql("SELECT MAX(sortorder) from {$CFG->prefix}course WHERE category=$categoryid");
+ $max = $DB->get_field_sql("SELECT MAX(sortorder) FROM {course} WHERE category=?", array($categoryid));
if ($max > $n) {
$n = $max;
}
* useful if a category has been removed manually
**/
function fix_coursecategory_orphans() {
-
- global $CFG;
+ global $DB;
// Note: the handling of sortorder here is arguably
// open to race conditions. Hard to fix here, unlikely
// to hit anyone in production.
$sql = "SELECT c.id, c.category, c.shortname
- FROM {$CFG->prefix}course c
- LEFT OUTER JOIN {$CFG->prefix}course_categories cc ON c.category=cc.id
- WHERE cc.id IS NULL AND c.id != " . SITEID;
+ FROM {course} c
+ LEFT OUTER JOIN {course_categories} cc ON c.category=cc.id
+ WHERE cc.id IS NULL AND c.id <> " . SITEID;
- $rs = get_recordset_sql($sql);
+ if (!$rs = $DB->get_recordset_sql($sql)) {
+ return;
+ }
- if (!rs_EOF($rs)) { // we have some orphans
+ if ($rs->valid()) { // we have some orphans
// the "default" category is the lowest numbered...
- $default = get_field_sql("SELECT MIN(id)
- FROM {$CFG->prefix}course_categories");
- $sortorder = get_field_sql("SELECT MAX(sortorder)
- FROM {$CFG->prefix}course
- WHERE category=$default");
-
-
- begin_sql();
- $tx = true;
- while ($tx && $course = rs_fetch_next_record($rs)) {
- $tx = $tx && set_field('course', 'category', $default, 'id', $course->id);
- $tx = $tx && set_field('course', 'sortorder', ++$sortorder, 'id', $course->id);
- }
- if ($tx) {
- commit_sql();
- } else {
- rollback_sql();
+ $default = $DB->get_field_sql("SELECT MIN(id)
+ FROM {course_categories}");
+ $sortorder = $DB->get_field_sql("SELECT MAX(sortorder)
+ FROM {course}
+ WHERE category=?", array($default));
+
+
+ $DB->begin_sql();
+ foreach ($rs as $course) {
+ if (!$DB->set_field('course', 'category', $default, array('id'=>$course->id))
+ or !$DB->set_field('course', 'sortorder', ++$sortorder, array('id'=>$course->id))) {
+ $DB->rollback_sql();
+ return;
+ }
}
+ $DB->commit_sql();
}
- rs_close($rs);
+ $rs->close();
}
/**
* @return array {@link $COURSE} of course objects
*/
function get_my_remotecourses($userid=0) {
- global $CFG, $USER;
+ global $DB, $USER;
if (empty($userid)) {
$userid = $USER->id;
$sql = "SELECT c.remoteid, c.shortname, c.fullname,
c.hostid, c.summary, c.cat_name,
h.name AS hostname
- FROM {$CFG->prefix}mnet_enrol_course c
- JOIN {$CFG->prefix}mnet_enrol_assignments a ON c.id=a.courseid
- JOIN {$CFG->prefix}mnet_host h ON c.hostid=h.id
- WHERE a.userid={$userid}";
+ FROM {mnet_enrol_course} c
+ JOIN {mnet_enrol_assignments} a ON c.id=a.courseid
+ JOIN {mnet_host} h ON c.hostid=h.id
+ WHERE a.userid=?";
- return get_records_sql($sql);
+ return $DB->get_records_sql($sql, array($userid));
}
/**
* strings and files is a bit odd, but this is because we
* need to maintain backward compatibility with many different
* existing language translations and older sites.
- *
- * @uses $CFG
*/
function make_default_scale() {
-
- global $CFG;
+ global $CFG, $DB;
$defaultscale = NULL;
$defaultscale->courseid = 0;
$file = '';
}
- $defaultscale->description = addslashes(implode('', $file));
+ $defaultscale->description = implode('', $file);
- if ($defaultscale->id = insert_record('scale', $defaultscale)) {
- execute_sql('UPDATE '. $CFG->prefix .'forum SET scale = \''. $defaultscale->id .'\'', false);
+ if ($defaultscale->id = $DB->insert_record('scale', $defaultscale)) {
+ $DB->execute("UPDATE {forum} SET scale = ?", array($defaultscale->id));
}
}
* @return object
*/
function get_scales_menu($courseid=0) {
+ global $DB;
- global $CFG;
-
- $sql = "SELECT id, name FROM {$CFG->prefix}scale
- WHERE courseid = '0' or courseid = '$courseid'
+ $sql = "SELECT id, name
+ FROM {scale}
+ WHERE courseid = 0 or courseid = ?
ORDER BY courseid ASC, name ASC";
+ $params = array($courseid);
- if ($scales = get_records_sql_menu($sql)) {
+ if ($scales = $DB->get_records_sql_menu($sql, $params)) {
return $scales;
}
make_default_scale();
- return get_records_sql_menu($sql);
+ return $DB->get_records_sql_menu($sql, $params);
}
* @param array $timezones An array of timezone records
*/
function update_timezone_records($timezones) {
-/// Given a set of timezone records, put them in the database
-
- global $CFG;
+ global $DB;
/// Clear out all the old stuff
- execute_sql('TRUNCATE TABLE '.$CFG->prefix.'timezone', false);
+ $DB->execute("TRUNCATE TABLE {timezone}");
/// Insert all the new stuff
foreach ($timezones as $timezone) {
if (is_array($timezone)) {
$timezone = (object)$timezone;
}
- insert_record('timezone', $timezone);
+ $DB->insert_record('timezone', $timezone);
}
}
/**
* Just gets a raw list of all modules in a course
*
- * @uses $CFG
* @param int $courseid The id of the course as found in the 'course' table.
* @return object
*/
function get_course_mods($courseid) {
- global $CFG;
+ global $DB;
if (empty($courseid)) {
return false; // avoid warnings
}
- return get_records_sql("SELECT cm.*, m.name as modname
- FROM {$CFG->prefix}modules m,
- {$CFG->prefix}course_modules cm
- WHERE cm.course = ".intval($courseid)."
- AND cm.module = m.id AND m.visible = 1"); // no disabled mods
+ return $DB->get_records_sql("SELECT cm.*, m.name as modname
+ FROM {modules} m, {course_modules} cm
+ WHERE cm.course = ? AND cm.module = m.id AND m.visible = 1",
+ array($courseid)); // no disabled mods
}
* @return object course module instance with instance and module name
*/
function get_coursemodule_from_id($modulename, $cmid, $courseid=0) {
+ global $DB;
- global $CFG;
+ $params = array();
+ $courseselect = "";
- $courseselect = ($courseid) ? 'cm.course = '.intval($courseid).' AND ' : '';
-
- return get_record_sql("SELECT cm.*, m.name, md.name as modname
- FROM {$CFG->prefix}course_modules cm,
- {$CFG->prefix}modules md,
- {$CFG->prefix}$modulename m
- WHERE $courseselect
- cm.id = ".intval($cmid)." AND
- cm.instance = m.id AND
- md.name = '$modulename' AND
- md.id = cm.module");
+ if ($courseid) {
+ $courseselect = "cm.course = :courseid AND ";
+ $params['courseid'] = $courseid;
+ }
+ $params['cmid'] = $cmid;
+ $params['modulename'] = $modulename;
+
+ return $DB->get_record_sql("SELECT cm.*, m.name, md.name as modname
+ FROM {course_modules} cm, {modules} md, {".$modulename."} m
+ WHERE $courseselect
+ cm.id = :cmid AND
+ cm.instance = m.id AND
+ md.name = :modulename AND
+ md.id = cm.module", $params);
}
/**
* @return object course module instance with instance and module name
*/
function get_coursemodule_from_instance($modulename, $instance, $courseid=0) {
+ global $DB;
- global $CFG;
-
- $courseselect = ($courseid) ? 'cm.course = '.intval($courseid).' AND ' : '';
+ $params = array();
+ $courseselect = "";
- return get_record_sql("SELECT cm.*, m.name, md.name as modname
- FROM {$CFG->prefix}course_modules cm,
- {$CFG->prefix}modules md,
- {$CFG->prefix}$modulename m
- WHERE $courseselect
- cm.instance = m.id AND
- md.name = '$modulename' AND
- md.id = cm.module AND
- m.id = ".intval($instance));
+ if ($courseid) {
+ $courseselect = "cm.course = :courseid AND ";
+ $params['courseid'] = $courseid;
+ }
+ $params['instance'] = $instance;
+ $params['modulename'] = $modulename;
+
+ return $DB->get_record_sql("SELECT cm.*, m.name, md.name as modname
+ FROM {course_modules} cm, {modules} md, {".$modulename."} m
+ WHERE $courseselect
+ cm.instance = m.id AND
+ md.name = :$modulename AND
+ md.id = cm.module AND
+ m.id = :$instance", $params);
}
* @return array of cm objects, false if not found or error
*/
function get_coursemodules_in_course($modulename, $courseid, $extrafields='') {
- global $CFG;
+ global $DB;
if (!empty($extrafields)) {
$extrafields = ", $extrafields";
}
- return get_records_sql("SELECT cm.*, m.name, md.name as modname $extrafields
- FROM {$CFG->prefix}course_modules cm,
- {$CFG->prefix}modules md,
- {$CFG->prefix}$modulename m
- WHERE cm.course = $courseid AND
- cm.instance = m.id AND
- md.name = '$modulename' AND
- md.id = cm.module");
+ $params = array();
+ $params['courseid'] = $courseid;
+ $params['modulename'] = $modulename;
+
+
+ return $DB->get_records_sql("SELECT cm.*, m.name, md.name as modname $extrafields
+ FROM {course_modules} cm, {modules} md, {".$modulename."} m
+ WHERE cm.course = :courseid AND
+ cm.instance = m.id AND
+ md.name = :modulename AND
+ md.id = cm.module");
}
/**
* and course_sections tables, or an empty array if an error occurred.
*/
function get_all_instances_in_courses($modulename, $courses, $userid=NULL, $includeinvisible=false) {
- global $CFG;
+ global $CFG, $DB;
$outputarray = array();
return $outputarray;
}
- if (!$rawmods = get_records_sql("SELECT cm.id AS coursemodule, m.*, cw.section, cm.visible AS visible,
- cm.groupmode, cm.groupingid, cm.groupmembersonly
- FROM {$CFG->prefix}course_modules cm,
- {$CFG->prefix}course_sections cw,
- {$CFG->prefix}modules md,
- {$CFG->prefix}$modulename m
- WHERE cm.course IN (".implode(',',array_keys($courses)).") AND
- cm.instance = m.id AND
- cm.section = cw.id AND
- md.name = '$modulename' AND
- md.id = cm.module")) {
+ list($coursessql, $params) = $DB->get_in_or_equal(array_keys($courses), SQL_PARAMS_NAMED, 'c0');
+ $params['modulename'] = $modulename;
+
+ if (!$rawmods = $DB->get_records_sql("SELECT cm.id AS coursemodule, m.*, cw.section, cm.visible AS visible,
+ cm.groupmode, cm.groupingid, cm.groupmembersonly
+ FROM {course_modules} cm, {course_sections} cw, {modules} md,
+ {".$modulename."} m
+ WHERE cm.course $coursessql AND
+ cm.instance = m.id AND
+ cm.section = cw.id AND
+ md.name = :modulename AND
+ md.id = cm.module", $params)) {
return $outputarray;
}
* and the module's type (eg "forum") returns whether the object
* is visible or not, groupmembersonly visibility not tested
*
- * @uses $CFG
* @param $moduletype Name of the module eg 'forum'
* @param $module Object which is the instance of the module
* @return bool
*/
function instance_is_visible($moduletype, $module) {
-
- global $CFG;
+ global $DB;
if (!empty($module->id)) {
- if ($records = get_records_sql("SELECT cm.instance, cm.visible, cm.groupingid, cm.id, cm.groupmembersonly, cm.course
- FROM {$CFG->prefix}course_modules cm,
- {$CFG->prefix}modules m
- WHERE cm.course = '$module->course' AND
- cm.module = m.id AND
- m.name = '$moduletype' AND
- cm.instance = '$module->id'")) {
+ $params = array('courseid'=>$module->course, 'moduletype'=>$moduletype, 'moduleid'=>$module->id);
+ if ($records = $DB->get_records_sql("SELECT cm.instance, cm.visible, cm.groupingid, cm.id, cm.groupmembersonly, cm.course
+ FROM {course_modules} cm, {modules} m
+ WHERE cm.course = :courseid AND
+ cm.module = m.id AND
+ m.name = :moduletype AND
+ cm.instance = :moduleid")) {
foreach ($records as $record) { // there should only be one - use the first one
return $record->visible;
* @return void
*/
function user_accesstime_log($courseid=0) {
-
global $USER, $CFG, $DB;
if (!isloggedin() or !empty($USER->realuser)) {
/**
* Select all log records based on SQL criteria
*
- * @uses $CFG
* @param string $select SQL select criteria
+ * @param array $params named sql type params
* @param string $order SQL order by clause to sort the records returned
* @param string $limitfrom ?
* @param int $limitnum ?
* @return object
* @todo Finish documenting this function
*/
-function get_logs($select, $order='l.time DESC', $limitfrom='', $limitnum='', &$totalcount) {
- global $CFG;
+function get_logs($select, array $params=null, $order='l.time DESC', $limitfrom='', $limitnum='', &$totalcount) {
+ global $DB;
if ($order) {
- $order = 'ORDER BY '. $order;
+ $order = "ORDER BY $order";
+ }
+
+ $selectsql = "";
+ $countsql = "";
+
+ if ($select) {
+ $select = "WHERE $select";
}
- $selectsql = $CFG->prefix .'log l LEFT JOIN '. $CFG->prefix .'user u ON l.userid = u.id '. ((strlen($select) > 0) ? 'WHERE '. $select : '');
- $countsql = $CFG->prefix.'log l '.((strlen($select) > 0) ? ' WHERE '. $select : '');
+ $sql = "SELECT COUNT(*)
+ FROM {log} l
+ $select";
+
+ $totalcount = $DB->count_records_sql($sql, $params);
- $totalcount = count_records_sql("SELECT COUNT(*) FROM $countsql");
+ $sql = "SELECT l.*, u.firstname, u.lastname, u.picture
+ FROM {log} l
+ LEFT JOIN {user} u ON l.userid = u.id
+ $select
+ $order";
- return get_records_sql('SELECT l.*, u.firstname, u.lastname, u.picture
- FROM '. $selectsql .' '. $order, $limitfrom, $limitnum) ;
+ return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum) ;
}
* @todo Finish documenting this function
*/
function get_logs_usercourse($userid, $courseid, $coursestart) {
- global $CFG;
+ global $DB;
+ $params = array();
+
+ $courseselect = '';
if ($courseid) {
- $courseselect = ' AND course = \''. $courseid .'\' ';
- } else {
- $courseselect = '';
+ $courseselect = "AND course = :courseid";
+ $params['courseid'] = $courseid;
}
+ $params['userid'] = $userid;
+ $params['coursestart'] = $coursestart;
- return get_records_sql("SELECT floor((time - $coursestart)/". DAYSECS .") as day, count(*) as num
- FROM {$CFG->prefix}log
- WHERE userid = '$userid'
- AND time > '$coursestart' $courseselect
- GROUP BY floor((time - $coursestart)/". DAYSECS .") ");
+ return $DB->get_records_sql("SELECT FLOOR((time - :coursestart)/". DAYSECS .") AS day, COUNT(*) AS num
+ FROM {log}
+ WHERE userid = :userid
+ AND time > :coursestart $courseselect
+ GROUP BY FLOOR((time - :coursestart)/". DAYSECS .")", $params);
}
/**
* @todo Finish documenting this function
*/
function get_logs_userday($userid, $courseid, $daystart) {
- global $CFG;
+ global $DB;
+
+ $params = array();
+ $courseselect = '';
if ($courseid) {
- $courseselect = ' AND course = \''. $courseid .'\' ';
- } else {
- $courseselect = '';
+ $courseselect = "AND course = :courseid";
+ $params['courseid'] = $courseid;
}
+ $params['userid'] = $userid;
+ $params['daystart'] = $daystart;
- return get_records_sql("SELECT floor((time - $daystart)/". HOURSECS .") as hour, count(*) as num
- FROM {$CFG->prefix}log
- WHERE userid = '$userid'
- AND time > '$daystart' $courseselect
- GROUP BY floor((time - $daystart)/". HOURSECS .") ");
+ return $DB->get_records_sql("SELECT FLOOR((time - :daystart)/". HOURSECS .") AS hour, COUNT(*) AS num
+ FROM {log}
+ WHERE userid = :userid
+ AND time > :daystart $courseselect
+ GROUP BY FLOOR((time - :daystart)/". HOURSECS .") ");
}
/**
* @return int
*/
function count_login_failures($mode, $username, $lastlogin) {
+ global $DB;
- $select = 'module=\'login\' AND action=\'error\' AND time > '. $lastlogin;
+ $params = array('mode'=>$mode, 'username'=>$username, 'lastlogin'=>$lastlogin);
+ $select = "module='login' AND action='error' AND time > :lastlogin";
+
+ $count = new object();
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { // Return information about all accounts
- if ($count->attempts = count_records_select('log', $select)) {
- $count->accounts = count_records_select('log', $select, 'COUNT(DISTINCT info)');
+ if ($count->attempts = $DB->count_records_select('log', $select, $params)) {
+ $count->accounts = $DB->count_records_select('log', $select, $params, 'COUNT(DISTINCT info)');
return $count;
}
} else if ($mode == 'everybody' or ($mode == 'teacher' and isteacherinanycourse())) {
- if ($count->attempts = count_records_select('log', $select .' AND info = \''. $username .'\'')) {
+ if ($count->attempts = $DB->count_records_select('log', "$select AND info = :username", $params)) {
return $count;
}
}
* @return bool
*/
function course_parent_visible($course = null) {
- global $CFG;
+ global $CFG, $DB;
//return true;
static $mycache;
if (isset($course->categorypath)) {
$path = $course->categorypath;
} else {
- $path = get_field('course_categories', 'path',
- 'id', $course->category);
+ $path = $DB->get_field('course_categories', 'path', array('id'=>$course->category));
}
$catids = substr($path,1); // strip leading slash
$catids = str_replace('/',',',$catids);
$sql = "SELECT MIN(visible)
- FROM {$CFG->prefix}course_categories
- WHERE id IN ($catids)";
- $vis = get_field_sql($sql);
+ FROM {course_categories}
+ WHERE id IN ($catids)";
+ $vis = $DB->get_field_sql($sql);
// cast to force assoc array
$k = (string)$course->category;
* @return array
*/
function get_creatable_categories() {
+ global $DB;
$creatablecats = array();
- if ($cats = get_records('course_categories')) {
+ if ($cats = $DB->get_records('course_categories')) {
foreach ($cats as $cat) {
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) {
$creatablecats[$cat->id] = $cat->name;