print_heading($backuploglaststatus);
print_simple_box_start('center');
/// Now, get every record from backup_courses
- $courses = get_records("backup_courses");
+ $courses = $DB->get_records("backup_courses");
if (!$courses) {
notify(get_string('nologsfound'));
print_simple_box_start('center');
/// First, me get all the distinct backups for that course in backup_log
- $executions = get_records_sql("SELECT DISTINCT laststarttime,laststarttime
- FROM {$CFG->prefix}backup_log
- WHERE courseid = '$courseid'
- ORDER BY laststarttime DESC");
+ $executions = $DB->get_records_sql("SELECT DISTINCT laststarttime,laststarttime
+ FROM {backup_log}
+ WHERE courseid = ?
+ ORDER BY laststarttime DESC", array($courseid));
/// Iterate over backup executions
if (!$executions) {
print_simple_box("<center>".userdate($execution->laststarttime)."</center>", "center");
echo "</td>";
echo "</tr>";
- $logs = get_records_sql("SELECT *
- FROM {$CFG->prefix}backup_log
- WHERE courseid = '$courseid' AND
- laststarttime = '$execution->laststarttime'
- ORDER BY id");
+ $logs = $DB->get_records_sql("SELECT *
+ FROM {backup_log}
+ WHERE courseid = ? AND laststarttime = ?
+ ORDER BY id", array($courseid, $execution->laststarttime));
if ($logs) {
foreach ($logs as $log) {
echo "<tr>";
if (!empty($param->sql)) {
$sql = $param->sql;
} else {
- $sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
- ." WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0"
- ." GROUP BY courseid "
- .$param->extras
- ." ORDER BY ".$param->orderby;
+ $sql = "SELECT courseid,".$param->fields."
+ FROM {".'stats_'.$param->table."}
+ WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0
+ GROUP BY courseid
+ $param->extras
+ ORDER BY $param->orderby";
}
- error_log($sql);
+ //error_log($sql);
- $courses = get_records_sql($sql, 0, $numcourses);
+ $courses = $DB->get_records_sql($sql, $pram->params, 0, $numcourses);
if (empty($courses)) {
notify(get_string('statsnodata'));echo '</td></tr></table>';echo '<p>after notify</p>';
if (!empty($param->sql)) {
$sql = $param->sql;
} else {
- $sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
- ." WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0"
- ." GROUP BY courseid "
- .$param->extras
- ." ORDER BY ".$param->orderby;
+ $sql = "SELECT courseid, $param->fields
+ FROM {".'stats_'.$param->table."}
+ WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0
+ GROUP BY courseid
+ $param->extras
+ ORDER BY $param->orderby";
}
- $courses = get_records_sql($sql, 0, $numcourses);
+ $courses = $DB->get_records_sql($sql, $pram->$prams, 0, $numcourses);
if (empty($courses)) {
print_error('statsnodata', 'error', $CFG->wwwroot.'/'.$CFG->admin.'/report/course/index.php');
redirect($CFG->wwwroot.'/'.$CFG->admin.'/report/stats/index.php?time='.$time, '', 3);
}
- if (!$course = get_record("course","id",$courseid)) {
+ if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
print_error('invalidcourse', 'error');
}
if (!empty($userid)) {
- if (!$user = get_record('user','id',$userid)) {
+ if (!$user = $DB->get_record('user', array('id'=>$userid))) {
print_error('invaliduser', 'error');
}
}
$user = optional_param('user', 0, PARAM_INT); // Student ID
$date = optional_param('date', 0, PARAM_INT); // A time of a day (in GMT)
- if (! $course = get_record("course", "id", $id)) {
+ if (! $course = $DB->get_record("course", array("id"=>$id))) {
print_error('invalidcourseid');
}
}
if ($user) {
- if (! $user = get_record("user", "id", $user)) {
+ if (! $user = $DB->get_record("user", array("id"=>$user))) {
print_error("nousers");
}
}
$logformat = optional_param('logformat', 'showashtml', PARAM_ALPHA);
if ($hostid == $CFG->mnet_localhost_id) {
- if (!$course = get_record('course', 'id', $id) ) {
+ if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('That\'s an invalid course id'.$id);
}
} else {
- $course_stub = array_pop(get_records_select('mnet_log', " hostid='$hostid' AND course='$id' ", '', '*', '', '1'));
+ $course_stub = $DB->get_record('mnet_log', array('hostid'=>$hostid, 'course'=>$id), '*', true);
$course->id = $id;
$course->shortname = $course_stub->coursename;
$course->fullname = $course_stub->coursename;
$dateinfo = get_string('alldays');
if ($user) {
- if (!$u = get_record('user', 'id', $user) ) {
+ if (!$u = $DB->get_record('user', array('id'=>$user))) {
print_error('That\'s an invalid user!');
}
$userinfo = fullname($u, has_capability('moodle/site:viewfullnames', $context));
$mnet_peer = new mnet_peer();
$mnet_peer->set_id($hostid);
- $sql = "select distinct course, hostid, coursename from {$CFG->prefix}mnet_log";
- $courses = get_records_sql($sql);
+ $sql = "SELECT DISTINCT course, hostid, coursename FROM {mnet_log}";
+ $courses = $DB->get_records_sql($sql);
$remotecoursecount = count($courses);
// first check to see if we can override showcourses and showusers
- $numcourses = $remotecoursecount + count_records_select("course", "", "COUNT(id)");
+ $numcourses = $remotecoursecount + $DB->count_records('course');
if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) {
$showcourses = 1;
}
h.id,
h.name
from
- {$CFG->prefix}mnet_host h,
- {$CFG->prefix}mnet_log l
+ {mnet_host} h,
+ {mnet_log} l
where
h.id = l.hostid
order by
h.name";
- if ($hosts = get_records_sql($sql)) {
+ if ($hosts = $DB->get_records_sql($sql)) {
foreach($hosts as $host) {
$hostarray[$host->id] = $host->name;
}
$sites = array();
if ($CFG->mnet_localhost_id == $hostid) {
if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) {
- if ($ccc = get_records("course", "", "", "fullname","id,fullname,category")) {
+ if ($ccc = $DB->get_records("course", null, "fullname","id,fullname,category")) {
foreach ($ccc as $cc) {
if ($cc->id == SITEID) {
$sites["$hostid/$cc->id"] = format_string($cc->fullname).' ('.get_string('site').')';
}
} else {
if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) {
- $sql = "select distinct course, coursename from {$CFG->prefix}mnet_log where hostid = '$hostid'";
- if ($ccc = get_records_sql($sql)) {
+ $sql = "SELECT DISTINCT course, coursename FROM {mnet_log} where hostid = ?";
+ if ($ccc = $DB->get_records_sql($sql, array($hostid))) {
foreach ($ccc as $cc) {
if (1 == $cc->course) { // TODO: this might be wrong - site course may have another id
$sites["$hostid/$cc->course"] = $cc->coursename.' ('.get_string('site').')';
else {
$users = array();
if (!empty($selecteduser)) {
- $user = get_record('user','id',$selecteduser);
+ $user = $DB->get_record('user', array('id'=>$selecteduser));
$users[$selecteduser] = fullname($user);
}
else {
global $USER, $CFG, $DB;
// first check to see if we can override showcourses and showusers
- $numcourses = count_records_select("course", "", "COUNT(id)");
+ $numcourses = $DB->count_records("course");
if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) {
$showcourses = 1;
}
}
if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) {
- if ($ccc = get_records("course", "", "", "fullname","id,fullname,category")) {
+ if ($ccc = $DB->get_records("course", null, "fullname", "id,fullname,category")) {
foreach ($ccc as $cc) {
if ($cc->category) {
$courses["$cc->id"] = format_string($cc->fullname);
else {
$users = array();
if (!empty($selecteduser)) {
- $user = get_record('user','id',$selecteduser);
+ $user = $DB->get_record('user', array('id'=>$selecteduser));
$users[$selecteduser] = fullname($user);
}
else {
$id = required_param('id', PARAM_INT);
$page = optional_param('page', 0, PARAM_INT); // which page to show
- if (! $course = get_record("course", "id", $id) ) {
+ if (! $course = $DB->get_record("course", array("id"=>$id))) {
print_error('invalidcourseid');
}
$id = required_param('id',PARAM_INT); // course id
- if (!$course = get_record('course', 'id', $id)) {
+ if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('invalidcourseid');
}
$modinfo = get_fast_modinfo($course);
$sql = "SELECT cm.id, COUNT('x') AS numviews, MAX(time) AS lasttime
- FROM {$CFG->prefix}course_modules cm
- JOIN {$CFG->prefix}modules m ON m.id = cm.module
- JOIN {$CFG->prefix}log l ON l.cmid = cm.id
- WHERE cm.course = $course->id AND l.action LIKE 'view%' AND m.visible = 1
+ FROM {course_modules} cm
+ JOIN {modules} m ON m.id = cm.module
+ JOIN {log} l ON l.cmid = cm.id
+ WHERE cm.course = ? AND l.action LIKE 'view%' AND m.visible = 1
GROUP BY cm.id";
- $views = get_records_sql($sql);
+ $views = $DB->get_records_sql($sql, array($course->id));
$ri = 0;
$prevsecctionnum = 0;
$action = ''; // default to all (don't restrict)
}
- if (!$course = get_record('course', 'id', $id)) {
+ if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('invalidcourse');
}
- if ($roleid != 0 and !$role = get_record('role', 'id', $roleid)) {
+ if ($roleid != 0 and !$role = $DB->get_record('role', array('id'=>$roleid))) {
print_error('invalidrole');
}
$modinfo = get_fast_modinfo($course);
- $modules = get_records_select('modules', "visible = 1 AND name <> 'label'", 'name ASC');
+ $modules = $DB->get_records_select('modules', "visible = 1 AND name <> 'label'", null, 'name ASC');
$instanceoptions = array();
foreach ($modules as $module) {
$timeoptions = array();
// get minimum log time for this course
- $minlog = get_field_sql('SELECT min(time) FROM '.$CFG->prefix.'log WHERE course = '.$course->id);
+ $minlog = $DB->get_field_sql('SELECT min(time) FROM {log} WHERE course = ?', array($course->id));
$now = usergetmidnight(time());
$postfun = $cm->modname.'_get_post_actions';
if (!function_exists($viewfun) || !function_exists($postfun)) {
- error(get_string('modulemissingcode','error',$cm->modname), $baseurl);
+ print_error('modulemissingcode', 'error', $baseurl, $cm->modname);
}
$viewnames = $viewfun();
switch ($action) {
case 'view':
- $actionsql = 'l.action IN (\''.implode('\',\'', $viewnames).'\' )';
+ $actions = $viewnames;
break;
case 'post':
- $actionsql = 'l.action IN (\''.implode('\',\'', $postnames).'\' )';
+ $actions = $postnames;
break;
default:
// some modules have stuff we want to hide, ie mail blocked etc so do actually need to limit here.
- $actionsql = 'l.action IN (\''.implode('\',\'', array_merge($viewnames, $postnames)).'\' )';
+ $actions = array_merge($viewnames, $postnames);
}
+ list($actionsql, $params) = $DB->get_in_or_equal($actions, SQL_PARAMS_NAMED, 'action0');
+ $actionsql = "l.action $actionsql";
+
$relatedcontexts = get_related_contexts_string($context);
$sql = "SELECT ra.userid, u.firstname, u.lastname, u.idnumber, COUNT(l.action) AS count
- FROM {$CFG->prefix}role_assignments ra
- JOIN {$CFG->prefix}user u ON u.id = ra.userid
- LEFT OUTER JOIN {$CFG->prefix}log l ON l.userid = ra.userid
- WHERE ra.contextid $relatedcontexts AND ra.roleid = $roleid AND
+ FROM {role_assignments} ra
+ JOIN {user} u ON u.id = ra.userid
+ LEFT JOIN {log} l ON l.userid = ra.userid
+ WHERE ra.contextid $relatedcontexts AND ra.roleid = :roleid AND
(l.id IS NULL OR
- (l.cmid = $instanceid AND l.time > $timefrom AND $actionsql)
+ (l.cmid = :instanceid AND l.time > :timefrom AND $actionsql)
)";
+ $params['roleid'] = $roleid;
+ $params['instanceid'] = $instanceid;
+ $params['timefrom'] = $timefrom;
if ($table->get_sql_where()) {
$sql .= ' AND '.$table->get_sql_where(); //initial bar
FROM {$CFG->prefix}role_assignments ra
JOIN {$CFG->prefix}user u ON u.id = ra.userid
LEFT OUTER JOIN {$CFG->prefix}log l ON l.userid = ra.userid
- WHERE ra.contextid $relatedcontexts AND ra.roleid = $roleid AND
+ WHERE ra.contextid $relatedcontexts AND ra.roleid = :roleid AND
(l.id IS NULL OR
- (l.cmid = $instanceid AND l.time > $timefrom AND $actionsql)
+ (l.cmid = :instanceid AND l.time > :timefrom AND $actionsql)
)";
- $totalcount = count_records_sql($countsql);
+ $totalcount = $DB->count_records_sql($countsql, $params);
if ($table->get_sql_where()) {
- $matchcount = count_records_sql($countsql.' AND '.$table->get_sql_where());
+ $matchcount = $DB->count_records_sql($countsql.' AND '.$table->get_sql_where(), $params);
} else {
$matchcount = $totalcount;
}
$table->initialbars($totalcount > $perpage);
$table->pagesize($perpage, $matchcount);
- if (!$users = get_records_sql($sql, $table->get_page_start(), $table->get_page_size())) {
+ if (!$users = $DB->get_records_sql($sql, $params, $table->get_page_start(), $table->get_page_size())) {
$users = array(); // tablelib will handle saying 'Nothing to display' for us.
}
$userid = optional_param('userid', 0, PARAM_INT);
$roleid = optional_param('roleid',0,PARAM_INT);
- if (!$course = get_record("course","id",$courseid)) {
+ if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
print_error("invalidcourseid");
}
if (!empty($userid)) {
- if (!$user = get_record('user','id',$userid)) {
+ if (!$user = $DB->get_record('user', array('id'=>$userid))) {
print_error("nousers");
}
}
$param->table = 'user_'.$param->table;
}
+ // TODO: cleanup this ugly mess!
$sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
.(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
.' '.$param->extras
.' ORDER BY timeend DESC';
- $stats = get_records_sql($sql);
+ $stats = $DB->get_records_sql($sql, $param->params);
$stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
redirect($CFG->wwwroot.'/'.$CFG->admin.'/report/stats/index.php?time='.$time);
}
- if (!$course = get_record("course","id",$courseid)) {
+ if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
print_error("invalidcourseid");
}
if (!empty($userid)) {
- if (!$user = get_record('user','id',$userid)) {
+ if (!$user = $DB->get_record('user', array('id'=>$userid))) {
print_error("nousers");
}
}
if ($mode == STATS_MODE_DETAILED) {
$param = stats_get_parameters($time,null,$course->id,$mode); // we only care about the table and the time string (if we have time)
+ //TODO: lceanup this ugly mess
$sql = 'SELECT DISTINCT s.userid, u.firstname, u.lastname, u.idnumber
FROM '.$CFG->prefix.'stats_user_'.$param->table.' s
JOIN '.$CFG->prefix.'user u ON u.id = s.userid
. ((!empty($time)) ? ' AND timeend >= '.$param->timeafter : '')
.' ORDER BY u.lastname, u.firstname ASC';
- if (!$us = get_records_sql($sql)) {
+ if (!$us = $DB->get_records_sql($sql, $param->params)) {
print_error('nousers');
}
if (!empty($param->sql)) {
$sql = $param->sql;
} else {
+ //TODO: lceanup this ugly mess
$sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
.(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
.' ORDER BY timeend DESC';
}
- $stats = get_records_sql($sql);
+ $stats = $DB->get_records_sql($sql, $params);
if (empty($stats)) {
notify(get_string('statsnodata'));
return ' ' . $fieldname . ' ';
}
+ /**
+ * Returns the SQL to be used in order to CAST one CHAR column to REAL number.
+ *
+ * Be aware that the CHAR column you're trying to cast contains really
+ * numbers or the RDBMS will throw an error!
+ *
+ * @param string fieldname the name of the field to be casted
+ * @param boolean text to specify if the original column is one TEXT (CLOB) column (true). Defaults to false.
+ * @return string the piece of SQL code to be used in your statement.
+ */
+ public function sql_cast_char2real($fieldname, $text=false) {
+ return ' ' . $fieldname . ' ';
+ }
+
/**
* Returns the SQL text to be used to compare one TEXT (clob) column with
* one varchar column, because some RDBMS doesn't support such direct
return ' CAST(' . $fieldname . ' AS INT) ';
}
+ public function sql_cast_char2real($fieldname, $text=false) {
+ return " $fieldname::real ";
+ }
+
/**
* Does this driver suppoer regex syntax when searching
*/
}
function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
- global $CFG,$db;
+ global $CFG, $DB;
$param = new object();
+ $params->params = array();
if ($time < 10) { // dailies
// number of days to go back = 7* time
$param->extras = '';
- // compatibility - if we're in postgres, cast to real for some reports.
- $real = '';
- if ($CFG->dbfamily == 'postgres') {
- $real = '::real';
- }
-
switch ($report) {
// ******************** STATS_MODE_GENERAL ******************** //
case STATS_REPORT_LOGINS:
break;
case STATS_REPORT_READS:
- $param->fields = sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, stat1 as line1';
+ $param->fields = $DB->sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, stat1 as line1';
$param->fieldscomplete = true; // set this to true to avoid anything adding stuff to the list and breaking complex queries.
$param->aggregategroupby = 'roleid';
$param->stattype = 'activity';
$param->crosstab = true;
$param->extras = 'GROUP BY timeend,roleid,stat1';
if ($courseid == SITEID) {
- $param->fields = sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat1) as line1';
+ $param->fields = $DB->sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat1) as line1';
$param->extras = 'GROUP BY timeend,roleid';
}
break;
case STATS_REPORT_WRITES:
- $param->fields = sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, stat2 as line1';
+ $param->fields = $DB->sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, stat2 as line1';
$param->fieldscomplete = true; // set this to true to avoid anything adding stuff to the list and breaking complex queries.
$param->aggregategroupby = 'roleid';
$param->stattype = 'activity';
$param->crosstab = true;
$param->extras = 'GROUP BY timeend,roleid,stat2';
if ($courseid == SITEID) {
- $param->fields = sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat2) as line1';
+ $param->fields = $DB->sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat2) as line1';
$param->extras = 'GROUP BY timeend,roleid';
}
break;
case STATS_REPORT_ACTIVITY:
- $param->fields = sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat1+stat2) as line1';
+ $param->fields = $DB->sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat1+stat2) as line1';
$param->fieldscomplete = true; // set this to true to avoid anything adding stuff to the list and breaking complex queries.
$param->aggregategroupby = 'roleid';
$param->stattype = 'activity';
activity.all_activity / enrolments.highest_enrolments as line3
FROM (
SELECT courseid, (stat1+stat2) AS all_activity
- FROM '.$CFG->prefix.'stats_'.$param->table.'
+ FROM {stats_'.$param->table.'}
WHERE stattype=\'activity\' AND timeend >= '.$param->timeafter.' AND roleid = 0
) activity
INNER JOIN
(
SELECT courseid, max(stat1) AS highest_enrolments
- FROM '.$CFG->prefix.'stats_'.$param->table.'
+ FROM {stats_'.$param->table.'}
WHERE stattype=\'enrolments\' AND timeend >= '.$param->timeafter.' AND stat1 > '.$threshold.'
GROUP BY courseid
) enrolments
ceil(avg(active_enrolments)) as line2, avg(proportion_active) AS line3
FROM (
SELECT courseid, timeend, stat2 as active_enrolments,
- stat1 as all_enrolments, stat2'.$real.'/stat1'.$real.' as proportion_active
- FROM '.$CFG->prefix.'stats_'.$param->table.'
+ stat1 as all_enrolments, '.$DB->sql_cast_char2real('stat2').'/'.$DB->sql_cast_char2real('stat1').' AS proportion_active
+ FROM {stats_'.$param->table.'}
WHERE stattype=\'enrolments\' AND roleid = 0 AND stat1 > '.$threshold.'
) aq
WHERE timeend >= '.$param->timeafter.'
avg(proportion_active) AS line3
FROM (
SELECT courseid, timeend, stat1 as views, stat2 AS posts,
- stat2'.$real.'/stat1'.$real.' as proportion_active
- FROM '.$CFG->prefix.'stats_'.$param->table.'
+ '.$DB->sql_cast_char2real('stat2').'/'.$DB->sql_cast_char2real('stat1').' as proportion_active
+ FROM {stats_'.$param->table.'}
WHERE stattype=\'activity\' AND roleid = 0 AND stat1 > 0
) aq
WHERE timeend >= '.$param->timeafter.'
}
*/
//TODO must add the SITEID reports to the rest of the reports.
- return $param;
+ return array($sql, $params);
}
function stats_get_view_actions() {