$data->$gradeboundaryname = $boundary;
$i++;
}
-$data->override = record_exists('grade_letters', 'contextid', $contextid);
+$data->override = $DB->record_exists('grade_letters', array('contextid' => $contextid));
$mform = new edit_letter_form(null, array('num'=>$num, 'admin'=>$admin));
$mform->set_data($data);
} else if ($data = $mform->get_data()) {
if (!$admin and empty($data->override)) {
- delete_records('grade_letters', 'contextid', $context->id);
+ $DB->delete_records('grade_letters', array('contextid' => $context->id));
redirect($returnurl);
}
krsort($letters, SORT_NUMERIC);
$old_ids = array();
- if ($records = get_records('grade_letters', 'contextid', $context->id, 'lowerboundary ASC', 'id')) {
+ if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC', 'id')) {
$old_ids = array_keys($records);
}
if ($old_id = array_pop($old_ids)) {
$record->id = $old_id;
- update_record('grade_letters', $record);
+ $DB->update_record('grade_letters', $record);
} else {
- insert_record('grade_letters', $record);
+ $DB->insert_record('grade_letters', $record);
}
}
foreach($old_ids as $old_id) {
- delete_records('grade_letters', 'id', $old_id);
+ $DB->delete_records('grade_letters', array('id' => $old_id));
}
redirect($returnurl);
class edit_letter_form extends moodleform {
- function definition() {
+ public function definition() {
$mform =& $this->_form;
$num = $this->_customdata['num'];
$admin = $this->_customdata['admin'];
$mform->addElement('header', 'gradeletters', get_string('gradeletters', 'grades'));
-
+
// Only show "override site defaults" checkbox if editing the course grade letters
if (!$admin) {
$mform->addElement('checkbox', 'override', get_string('overridesitedefaultgradedisplaytype', 'grades'));
$mform->setHelpButton($gradelettername, array('gradeletter', get_string('gradeletter', 'grades'), 'grade'));
}
$mform->setType($gradelettername, PARAM_TEXT);
-
+
if (!$admin) {
$mform->disabledIf($gradelettername, 'override', 'notchecked');
$mform->disabledIf($gradelettername, $gradeboundaryname, 'eq', -1);
}
$mform->setDefault($gradeboundaryname, -1);
$mform->setType($gradeboundaryname, PARAM_INT);
-
+
if (!$admin) {
$mform->disabledIf($gradeboundaryname, 'override', 'notchecked');
}
$courseid = optional_param('id', SITEID, PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
-if (!$course = get_record('course', 'id', $courseid)) {
+if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
require_login($course);
$courseid = required_param('id', PARAM_INT);
/// Make sure they can even access this course
-if (!$course = get_record('course', 'id', $courseid)) {
+if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
require_login($course);
$co_standard_used = array();
$co_standard_notused = array();
-if ($courseused = get_records('grade_outcomes_courses', 'courseid', $courseid, '', 'outcomeid')) {
+if ($courseused = $DB->get_records('grade_outcomes_courses', array('courseid' => $courseid), '', 'outcomeid')) {
$courseused = array_keys($courseused);
} else {
$courseused = array();
// fix wrong entries in outcomes_courses
foreach ($courseused as $oid) {
if (!array_key_exists($oid, $standardoutcomes) and !array_key_exists($oid, $co_custom)) {
- delete_records('grade_outcomes_courses', 'outcomeid', $oid, 'courseid', $courseid);
+ $DB->delete_records('grade_outcomes_courses', array('outcomeid' => $oid, 'courseid' => $courseid));
}
}
$goc = new object();
$goc->courseid = $courseid;
$goc->outcomeid = $oid;
- insert_record('grade_outcomes_courses', $goc);
+ $DB->insert_record('grade_outcomes_courses', $goc);
}
}
// now check all used standard outcomes are in outcomes_course too
+$params = array($courseid);
$sql = "SELECT DISTINCT outcomeid
- FROM {$CFG->prefix}grade_items
- WHERE courseid=$courseid and outcomeid IS NOT NULL";
-if ($realused = get_records_sql($sql)) {
+ FROM {grade_items}
+ WHERE courseid=? and outcomeid IS NOT NULL";
+if ($realused = $DB->get_records_sql($sql, $params)) {
$realused = array_keys($realused);
foreach ($realused as $oid) {
if (array_key_exists($oid, $standardoutcomes)) {
$goc = new object();
$goc->courseid = $courseid;
$goc->outcomeid = $oid;
- insert_record('grade_outcomes_courses', $goc);
+ $DB->insert_record('grade_outcomes_courses', $goc);
}
}
}
$goc = new object();
$goc->courseid = $courseid;
$goc->outcomeid = $add;
- insert_record('grade_outcomes_courses', $goc);
+ $DB->insert_record('grade_outcomes_courses', $goc);
}
} else if (!empty($data->remove) && !empty($data->removeoutcomes)) {
if (!array_key_exists($remove, $co_standard_notused)) {
continue;
}
- delete_records('grade_outcomes_courses', 'courseid', $courseid, 'outcomeid', $remove);
+ $DB->delete_records('grade_outcomes_courses', array('courseid' => $courseid, 'outcomeid' => $remove));
}
}
redirect('course.php?id='.$courseid); // we must redirect to get fresh data
// a bit complex access control :-O
if ($id) {
/// editing existing outcome
- if (!$outcome_rec = get_record('grade_outcomes', 'id', $id)) {
+ if (!$outcome_rec = $DB->get_record('grade_outcomes', array('id' => $id))) {
print_error('invalidoutcome');
}
if ($outcome_rec->courseid) {
$outcome_rec->standard = 0;
- if (!$course = get_record('course', 'id', $outcome_rec->courseid)) {
+ if (!$course = $DB->get_record('course', array('id' => $outcome_rec->courseid))) {
print_error('invalidcourseid');
}
require_login($course);
$courseid = $course->id;
} else {
if ($courseid) {
- if (!$course = get_record('course', 'id', $courseid)) {
+ if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('invalidcourseid');
}
}
} else if ($courseid){
/// adding new outcome from course
- if (!$course = get_record('course', 'id', $courseid)) {
+ if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
$outcome_rec = new object();
require_once $CFG->libdir.'/formslib.php';
class edit_outcome_form extends moodleform {
- function definition() {
+ public function definition() {
global $CFG, $COURSE;
$mform =& $this->_form;
<?php // $Id$
- // Exports selected outcomes in CSV format.
+ // Exports selected outcomes in CSV format.
require_once '../../../config.php';
require_once $CFG->dirroot.'/grade/lib.php';
/// Make sure they can even access this course
if ($courseid) {
- if (!$course = get_record('course', 'id', $courseid)) {
+ if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
require_login($course);
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
header("Content-Type: text/csv; charset=utf-8");
-// TODO: make the filename more useful, include a date, a specific name, something...
+// TODO: make the filename more useful, include a date, a specific name, something...
header('Content-Disposition: attachment; filename=outcomes.csv');
// sending header with clear names, to make 'what is what' as easy as possible to understand
$outcomes = array();
if ( $courseid ) {
$outcomes = array_merge(grade_outcome::fetch_all_global(), grade_outcome::fetch_all_local($courseid));
-} else {
+} else {
$outcomes = grade_outcome::fetch_all_global();
}
$line[] = $outcome->get_name();
$line[] = $outcome->get_shortname();
$line[] = $outcome->description;
-
+
$scale = $outcome->load_scale();
$line[] = $scale->get_name();
$line[] = $scale->compact_items();
$line[] = $scale->description;
-
+
echo format_csv($line, ';', '"');
}
/// Make sure they can even access this course
if ($courseid) {
- if (!$course = get_record('course', 'id', $courseid)) {
+ if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
require_login($course);
// open the file, start importing data
if ($handle = fopen($imported_file['userfile']['tmp_name'], 'r')) {
- $line = 0; // will keep track of current line, to give better error messages.
+ $line = 0; // will keep track of current line, to give better error messages.
$file_headers = '';
// $csv_data needs to have at least these columns, the value is the default position in the data file.
$optional_headers = array('outcome_description'=>2, 'scale_description' => 5);
$imported_headers = array(); // will later be initialized with the values found in the file
- // data should be separated by a ';'. *NOT* by a comma! TODO: version 2.0
+ // data should be separated by a ';'. *NOT* by a comma! TODO: version 2.0
// or whenever we can depend on PHP5, set the second parameter (8192) to 0 (unlimited line length) : the database can store over 128k per line.
while ( $csv_data = fgetcsv($handle, 8192, ';', '"')) { // if the line is over 8k, it won't work...
$line++;
-
+
// be tolerant on input, as fgetcsv returns "an array comprising a single null field" on blank lines
if ($csv_data == array(null)) {
continue;
}
-
+
// on first run, grab and analyse the header
if ($file_headers == '') {
-
+
$file_headers = array_flip($csv_data); // save the header line ... TODO: use the header line to let import work with columns in arbitrary order
-
+
$error = false;
foreach($headers as $key => $value) {
// sanity check #1: make sure the file contains all the mandatory headers
continue; // we don't import headers
}
- // sanity check #2: every line must have the same number of columns as there are
+ // sanity check #2: every line must have the same number of columns as there are
// headers. If not, processing stops.
- if ( count($csv_data) != count($file_headers) ) {
+ if ( count($csv_data) != count($file_headers) ) {
print_box(get_string('importoutcomenofile', 'grades', $line));
//print_box(var_export($csv_data, true) ."<br />". var_export($header, true));
break;
- }
+ }
// sanity check #3: all required fields must be present on the current line.
foreach ($headers as $header => $position) {
//var_dump($csv_data);
+ $params = array($csv_data[$imported_headers['outcome_shortname']]);
+ $wheresql = 'shortname = ? ';
+
if ($local_scope) {
- $outcome = get_records_select('grade_outcomes', 'shortname = "'. $csv_data[$imported_headers['outcome_shortname']] .'" and courseid = '. $courseid );
+ $params[] = $courseid;
+ $wheresql .= ' AND courseid = ?';
} else {
- $outcome = get_records_select('grade_outcomes', 'shortname = "'. $csv_data[$imported_headers['outcome_shortname']] .'" and courseid is null');
+ $wheresql .= ' AND courseid IS NULL';
}
- //var_export($outcome);
+
+ $outcome = $DB->get_records_select('grade_outcomes', $wheresql, $params);
if ($outcome) {
// already exists, print a message and skip.
}
// new outcome will be added, search for compatible existing scale...
- $scale = get_records_select('scale', 'name ="'. $csv_data[$imported_headers['scale_name']] .'" and scale ="'. $csv_data[$imported_headers['scale_items']] .'" and (courseid = '. $courseid .' or courseid = 0)');
+ $params = array($csv_data[$imported_headers['scale_name']], $csv_data[$imported_headers['scale_items']], $courseid);
+ $wheresql = 'name = ? AND scale = ? AND (courseid = ? OR courseid = 0)';
+ $scale = $DB->get_records_select('scale', $wheresql, $params);
if ($scale) {
// already exists in the right scope: use it.
if (!has_capability('moodle/course:managescales', $context)) {
print_box(get_string('importskippednomanagescale', 'grades', $csv_data[$imported_headers['outcome_shortname']]));
continue;
- } else {
+ } else {
// scale doesn't exists : create it.
- $scale_data = array('name' => $csv_data[$imported_headers['scale_name']],
- 'scale' => $csv_data[$imported_headers['scale_items']],
- 'description' => $csv_data[$imported_headers['scale_description']],
+ $scale_data = array('name' => $csv_data[$imported_headers['scale_name']],
+ 'scale' => $csv_data[$imported_headers['scale_items']],
+ 'description' => $csv_data[$imported_headers['scale_description']],
'userid' => $USER->id);
if ($local_scope) {
}
// add outcome
- $outcome_data = array('shortname' => $csv_data[$imported_headers['outcome_shortname']],
- 'fullname' => $csv_data[$imported_headers['outcome_name']],
- 'scaleid' => $scale_id,
- 'description' => $csv_data[$imported_headers['outcome_description']],
+ $outcome_data = array('shortname' => $csv_data[$imported_headers['outcome_shortname']],
+ 'fullname' => $csv_data[$imported_headers['outcome_name']],
+ 'scaleid' => $scale_id,
+ 'description' => $csv_data[$imported_headers['outcome_description']],
'usermodified' => $USER->id);
if ($local_scope) {
class import_outcomes_form extends moodleform {
- function definition() {
+ public function definition() {
global $COURSE, $USER;
$mform =& $this->_form;
$mform->addElement('radio', 'scope', get_string('importstandard', 'grades'), null, 'global');
$mform->setDefault('scope', 'custom');
}
-
+
$mform->addElement('file', 'userfile', get_string('importoutcomes', 'grades'));
$mform->addElement('submit', 'save', get_string('uploadthisfile'));
/// Make sure they can even access this course
if ($courseid) {
- if (!$course = get_record('course', 'id', $courseid)) {
+ if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
require_login($course);
if(!$deleteconfirmed){
print_header(get_string('outcomedelete', 'grades'));
- notice_yesno(get_string('outcomeconfirmdelete', 'grades', $outcome->fullname),
+ notice_yesno(get_string('outcomeconfirmdelete', 'grades', $outcome->fullname),
"index.php?id={$courseid}", "index.php?id={$courseid}",
- array('outcomeid' => $outcome->id,
- 'action'=> 'delete',
- 'sesskey' => $USER->sesskey,
+ array('outcomeid' => $outcome->id,
+ 'action'=> 'delete',
+ 'sesskey' => $USER->sesskey,
'deleteconfirmed'=> 1)
);
print_footer();
" src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a> ";
}
$line[] = $buttons;
-
+
$data[] = $line;
}
$table = new object();
if ($outcomes = grade_outcome::fetch_all_global()) {
- $return = print_heading($strstandardoutcome, '', 2, 'main', true);
+ $return = print_heading($strstandardoutcome, '', 2, 'main', true);
$data = array();
foreach($outcomes as $outcome) {
$line = array();
// a bit complex access control :-O
if ($id) {
/// editing existing scale
- if (!$scale_rec = get_record('scale', 'id', $id)) {
+ if (!$scale_rec = $DB->get_record('scale', array('id' => $id))) {
print_error('invalidscaleid');
}
if ($scale_rec->courseid) {
$scale_rec->standard = 0;
- if (!$course = get_record('course', 'id', $scale_rec->courseid)) {
+ if (!$course = $DB->get_record('course', array('id' => $scale_rec->courseid))) {
print_error('invalidcourseid');
}
require_login($course);
$courseid = $course->id;
} else {
if ($courseid) {
- if (!$course = get_record('course', 'id', $courseid)) {
+ if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('invalidcourseid');
}
}
} else if ($courseid){
/// adding new scale from course
- if (!$course = get_record('course', 'id', $courseid)) {
+ if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
$scale_rec = new object();
/// Make sure they can even access this course
if ($courseid) {
- if (!$course = get_record('course', 'id', $courseid)) {
+ if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
require_login($course);
global $USER, $CFG;
$mform =& $this->_form;
-
+
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
$can_view_admin_links = false;
if (has_capability('moodle/grade:manage', $systemcontext)) {
if ($can_view_admin_links) {
$link = '<a href="' . $CFG->wwwroot . '/admin/settings.php?section=gradereport' . $plugin . '">' . $strchangedefaults . '</a>';
$mform->addElement('static', 'gradeitemsettingslink', $link);
- }
+ }
$functionname($mform);
}
}
$courseid = optional_param('id', SITEID, PARAM_INT);
-if (!$course = get_record('course', 'id', $courseid)) {
+if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
require_login($course);
$eid = required_param('eid', PARAM_ALPHANUM);
/// Make sure they can even access this course
-if (!$course = get_record('course', 'id', $courseid)) {
+if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
require_login($course);
require_once $CFG->libdir.'/formslib.php';
class edit_calculation_form extends moodleform {
- var $available;
- var $noidnumbers;
+ public $available;
+ public $noidnumbers;
function definition() {
global $COURSE;
} else {
$module = get_record_sql("SELECT cm.*, m.name as modname
- FROM {$CFG->prefix}modules m, {$CFG->prefix}course_modules cm
+ FROM {modules} m, {course_modules} cm
WHERE cm.id = {$data->cmid} AND cm.module = m.id ");
$grade_item->itemtype = 'mod';
$grade_item->itemmodule = $module->modname;
$this->userkey = '';
$this->previewrows = false;
$this->updatedgradesonly = $updatedgradesonly;
-
+
$this->displaytype = $displaytype;
$this->decimalpoints = $decimalpoints;
}
// if (empty($user->idnumber)) { // Not sure why this was here, ccommented out for MDL-13722
// continue;
// }
-
+
$gradeupdated = false; // if no grade is update at all for this user, do not display this row
$rowstr = '';
foreach ($this->columns as $itemid=>$unused) {
$gradetxt = $this->format_grade($userdata->grades[$itemid]);
-
+
// get the status of this grade, and put it through track to get the status
$g = new grade_export_update_buffer();
$grade_grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$user->id));
$rowstr .= "<td>$gradetxt</td>";
$gradeupdated = true;
}
-
+
if ($this->export_feedback) {
$rowstr .= '<td>'.$this->format_feedback($userdata->feedbacks[$itemid]).'</td>';
}
}
- // if we are requesting updated grades only, we are not interested in this user at all
+ // if we are requesting updated grades only, we are not interested in this user at all
if (!$gradeupdated && $this->updatedgradesonly) {
- continue;
+ continue;
}
echo '<tr>';
- echo "<td>$user->firstname</td><td>$user->lastname</td><td>$user->idnumber</td><td>$user->institution</td><td>$user->department</td><td>$user->email</td>";
+ echo "<td>$user->firstname</td><td>$user->lastname</td><td>$user->idnumber</td><td>$user->institution</td><td>$user->department</td><td>$user->email</td>";
echo $rowstr;
echo "</tr>";
-
+
$i++; // increment the counter
}
echo '</table>';
if (count($this->update_list) > $buffersize) {
$list = implode(',', $this->update_list);
- $sql = "UPDATE {$CFG->prefix}grade_grades SET exported = {$this->export_time} WHERE id IN ($list)";
+ $sql = "UPDATE {grade_grades} SET exported = {$this->export_time} WHERE id IN ($list)";
execute_sql($sql, false);
$this->update_list = array();
}
$mform2->display();
//} else if (($formdata = data_submitted()) && !empty($formdata->map)) {
-
+
// else if grade import mapping form is submitted
} else if ($formdata = $mform2->get_data()) {
if ($status and !empty($newfeedbacks)) {
foreach ($newfeedbacks as $newfeedback) {
$sql = "SELECT *
- FROM {$CFG->prefix}grade_import_values
+ FROM {grade_import_values}
WHERE importcode=$importcode AND userid=$studentid AND itemid=$newfeedback->itemid AND importer={$USER->id}";
if ($feedback = get_record_sql($sql)) {
$newfeedback->id = $feedback->id;
/// first select distinct new grade_items with this batch
if ($newitems = get_records_sql("SELECT *
- FROM {$CFG->prefix}grade_import_newitem
+ FROM {grade_import_newitem}
WHERE importcode = $importcode AND importer={$USER->id}")) {
// instances of the new grade_items created, cached
/// then find all existing items
if ($gradeitems = get_records_sql("SELECT DISTINCT (itemid)
- FROM {$CFG->prefix}grade_import_values
+ FROM {grade_import_values}
WHERE importcode = $importcode AND importer={$USER->id} AND itemid > 0")) {
$modifieditems = array();
/**
* This function returns an array of grades that were included in the import,
- * but wherer the user does not currenly have a graded role on the course. These gradse
+ * but wherer the user does not currenly have a graded role on the course. These gradse
* are still stored in the database, but will not be visible in the gradebook unless
* this user subsequently enrols on the course in a graded roles.
*
function get_unenrolled_users_in_import($importcode, $courseid) {
global $CFG;
$relatedctxcondition = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $courseid));
-
- $sql = "SELECT giv.id, u.firstname, u.lastname, u.idnumber AS useridnumber,
+
+ $sql = "SELECT giv.id, u.firstname, u.lastname, u.idnumber AS useridnumber,
COALESCE(gi.idnumber, gin.itemname) AS gradeidnumber
FROM
- {$CFG->prefix}grade_import_values giv
- JOIN {$CFG->prefix}user u ON giv.userid = u.id
- LEFT JOIN {$CFG->prefix}grade_items gi ON gi.id = giv.itemid
- LEFT JOIN {$CFG->prefix}grade_import_newitem gin ON gin.id = giv.newgradeitem
- LEFT JOIN {$CFG->prefix}role_assignments ra ON (giv.userid = ra.userid AND
+ {grade_import_values} giv
+ JOIN {user} u ON giv.userid = u.id
+ LEFT JOIN {grade_items} gi ON gi.id = giv.itemid
+ LEFT JOIN {grade_import_newitem} gin ON gin.id = giv.newgradeitem
+ LEFT JOIN {role_assignments} ra ON (giv.userid = ra.userid AND
ra.roleid IN ($CFG->gradebookroles) AND
ra.contextid $relatedctxcondition)
WHERE giv.importcode = $importcode
* Returns detailed info about users and their grades.
*/
class graded_users_iterator {
- var $course;
- var $grade_items;
- var $groupid;
- var $users_rs;
- var $grades_rs;
- var $gradestack;
- var $sortfield1;
- var $sortorder1;
- var $sortfield2;
- var $sortorder2;
+ public $course;
+ public $grade_items;
+ public $groupid;
+ public $users_rs;
+ public $grades_rs;
+ public $gradestack;
+ public $sortfield1;
+ public $sortorder1;
+ public $sortfield2;
+ public $sortorder2;
/**
* Constructor
* @param string $sortfield2 The second field of the users table by which the array of users will be sorted
* @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC)
*/
- function graded_users_iterator($course, $grade_items=null, $groupid=0, $sortfield1='lastname', $sortorder1='ASC', $sortfield2='firstname', $sortorder2='ASC') {
+ public function graded_users_iterator($course, $grade_items=null, $groupid=0, $sortfield1='lastname', $sortorder1='ASC', $sortfield2='firstname', $sortorder2='ASC') {
$this->course = $course;
$this->grade_items = $grade_items;
$this->groupid = $groupid;
* Initialise the iterator
* @return boolean success
*/
- function init() {
- global $CFG;
+ public function init() {
+ global $CFG, $DB;
$this->close();
return false;
}
- if (strpos($CFG->gradebookroles, ',') === false) {
- $gradebookroles = " = {$CFG->gradebookroles}";
- } else {
- $gradebookroles = " IN ({$CFG->gradebookroles})";
- }
+ list($gradebookroles_sql, $params) = $DB->get_in_or_equal(explode(',', $CFG->gradebookroles));
$relatedcontexts = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $this->course->id));
if ($this->groupid) {
- $groupsql = "INNER JOIN {$CFG->prefix}groups_members gm ON gm.userid = u.id";
- $groupwheresql = "AND gm.groupid = {$this->groupid}";
+ $groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id";
+ $groupwheresql = "AND gm.groupid = ?";
+ // $params contents: gradebookroles
+ $params[] = $this->groupid;
} else {
$groupsql = "";
$groupwheresql = "";
}
}
+ // $params contents: gradebookroles and groupid (for $groupwheresql)
$users_sql = "SELECT u.* $ofields
- FROM {$CFG->prefix}user u
- INNER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
+ FROM {user} u
+ INNER JOIN {role_assignments} ra ON u.id = ra.userid
$groupsql
- WHERE ra.roleid $gradebookroles
+ WHERE ra.roleid $gradebookroles_sql;
AND ra.contextid $relatedcontexts
$groupwheresql
ORDER BY $order";
- $this->users_rs = get_recordset_sql($users_sql);
+ $this->users_rs = $DB->get_recordset_sql($users_sql, $params);
if (!empty($this->grade_items)) {
$itemids = array_keys($this->grade_items);
- $itemids = implode(',', $itemids);
+ list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids);
+ $params = array_merge($params, $grades_params);
+ // $params contents: gradebookroles, groupid (for $groupwheresql) and itemids
$grades_sql = "SELECT g.* $ofields
- FROM {$CFG->prefix}grade_grades g
- INNER JOIN {$CFG->prefix}user u ON g.userid = u.id
- INNER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
+ FROM {grade_grades} g
+ INNER JOIN {user} u ON g.userid = u.id
+ INNER JOIN {role_assignments} ra ON u.id = ra.userid
$groupsql
- WHERE ra.roleid $gradebookroles
+ WHERE ra.roleid $gradebookroles_sql
AND ra.contextid $relatedcontexts
- AND g.itemid IN ($itemids)
$groupwheresql
+ AND g.itemid $itemidsql
ORDER BY $order, g.itemid ASC";
- $this->grades_rs = get_recordset_sql($grades_sql);
+ $this->grades_rs = $DB->get_recordset_sql($grades_sql, $params);
} else {
$this->grades_rs = false;
}
return false; // no users present
}
- if (!$user = rs_fetch_next_record($this->users_rs)) {
+ if (!$user = $this->users_rs->next()) {
if ($current = $this->_pop()) {
// this is not good - user or grades updated between the two reads above :-(
}
*/
function close() {
if ($this->users_rs) {
- rs_close($this->users_rs);
+ $this->users_rs->close();
$this->users_rs = null;
}
if ($this->grades_rs) {
- rs_close($this->grades_rs);
+ $this->grades_rs->close();
$this->grades_rs = null;
}
$this->gradestack = array();
* Internal function
*/
function _pop() {
+ global $DB;
if (empty($this->gradestack)) {
if (!$this->grades_rs) {
return NULL; // no grades present
}
- if (!$grade = rs_fetch_next_record($this->grades_rs)) {
+ if (!$grade = $this->grades_rs->next()) {
return NULL; // no more grades
}
* Utility class used for return tracking when using edit and other forms in grade plugins
*/
class grade_plugin_return {
- var $type;
- var $plugin;
- var $courseid;
- var $userid;
- var $page;
+ public $type;
+ public $plugin;
+ public $courseid;
+ public $userid;
+ public $page;
/**
* Constructor
* @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST
*/
- function grade_plugin_return ($params=null) {
+ public function grade_plugin_return ($params=null) {
if (empty($params)) {
$this->type = optional_param('gpr_type', null, PARAM_SAFEDIR);
$this->plugin = optional_param('gpr_plugin', null, PARAM_SAFEDIR);
* Returns return parameters as options array suitable for buttons.
* @return array options
*/
- function get_options() {
+ public function get_options() {
if (empty($this->type)) {
return array();
}
* @param string $default default url when params not set
* @return string url
*/
- function get_return_url($default, $extras=null) {
+ public function get_return_url($default, $extras=null) {
global $CFG;
if (empty($this->type) or empty($this->plugin)) {
* Returns string with hidden return tracking form elements.
* @return string
*/
- function get_form_fields() {
+ public function get_form_fields() {
if (empty($this->type)) {
return '';
}
* @param object $mform moodle form object
* @return void
*/
- function add_mform_elements(&$mform) {
+ public function add_mform_elements(&$mform) {
if (empty($this->type)) {
return;
}
* @param string $url
* @return string $url with erturn tracking params
*/
- function add_url_params($url) {
+ public function add_url_params($url) {
if (empty($this->type)) {
return $url;
}
* General structure representing grade items in course
*/
class grade_structure {
- var $context;
+ public $context;
- var $courseid;
+ public $courseid;
/**
* 1D array of grade items only
*/
- var $items;
+ public $items;
/**
* Returns icon of element
* @param bool $spacerifnone return spacer if no icon found
* @return string icon or spacer
*/
- function get_element_icon(&$element, $spacerifnone=false) {
+ public function get_element_icon(&$element, $spacerifnone=false) {
global $CFG;
switch ($element['type']) {
* @param bool $spacerifnone return spacer if no icon found
* @return header string
*/
- function get_element_header(&$element, $withlink=false, $icon=true, $spacerifnone=false) {
+ public function get_element_header(&$element, $withlink=false, $icon=true, $spacerifnone=false) {
global $CFG;
$header = '';
* @param $grade_grade object
* @return string eid
*/
- function get_grade_eid($grade_grade) {
+ public function get_grade_eid($grade_grade) {
if (empty($grade_grade->id)) {
return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid;
} else {
* @param $grade_item object
* @return string eid
*/
- function get_item_eid($grade_item) {
+ public function get_item_eid($grade_item) {
return 'i'.$grade_item->id;
}
* @param object $element
* @return string
*/
- function get_edit_icon($element, $gpr) {
+ public function get_edit_icon($element, $gpr) {
global $CFG;
if (!has_capability('moodle/grade:manage', $this->context)) {
* @param object $element
* @return string
*/
- function get_hiding_icon($element, $gpr) {
+ public function get_hiding_icon($element, $gpr) {
global $CFG;
if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:hide', $this->context)) {
* @param object $element
* @return string
*/
- function get_locking_icon($element, $gpr) {
+ public function get_locking_icon($element, $gpr) {
global $CFG;
$strparams = $this->get_params_for_iconstr($element);
$strunlock = get_string('unlockverbose', 'grades', $strparams);
$strlock = get_string('lockverbose', 'grades', $strparams);
-
+
// Don't allow an unlocking action for a grade whose grade item is locked: just print a state icon
if ($element['type'] == 'grade' && $element['object']->grade_item->is_locked()) {
$strparamobj = new stdClass();
$strparamobj->itemname = $element['object']->grade_item->itemname;
$strnonunlockable = get_string('nonunlockableverbose', 'grades', $strparamobj);
- $action = '<img src="'.$CFG->pixpath.'/t/unlock_gray.gif" alt="'.$strnonunlockable.'" class="iconsmall" title="'.$strnonunlockable.'"/>';
+ $action = '<img src="'.$CFG->pixpath.'/t/unlock_gray.gif" alt="'.$strnonunlockable.'" class="iconsmall" title="'.$strnonunlockable.'"/>';
} elseif ($element['object']->is_locked()) {
$icon = 'unlock';
$tooltip = $strunlock;
* @param object $element
* @return string
*/
- function get_calculation_icon($element, $gpr) {
+ public function get_calculation_icon($element, $gpr) {
global $CFG;
if (!has_capability('moodle/grade:manage', $this->context)) {
return '';
* A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
* @var string $commonvars
*/
- var $commonvars;
+ public $commonvars;
/**
* 1D array of elements
*/
- var $elements;
+ public $elements;
/**
* Constructor, retrieves and stores array of all grade_category and grade_item
* @param boolean $category_grade_last category grade item is the last child
* @param array $collapsed array of collapsed categories
*/
- function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) {
+ public function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) {
global $USER, $CFG;
$this->courseid = $courseid;
* @param array $element The seed of the recursion
* @return void
*/
- function flatten(&$element, $category_grade_last, $nooutcomes) {
+ public function flatten(&$element, $category_grade_last, $nooutcomes) {
if (empty($element['children'])) {
return array();
}
* @param int $eid
* @return object element
*/
- function locate_element($eid) {
+ public function locate_element($eid) {
// it is a grade - construct a new object
if (strpos($eid, 'n') === 0) {
if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
* The basic representation of the tree as a hierarchical, 3-tiered array.
* @var object $top_element
*/
- var $top_element;
+ public $top_element;
/**
* A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
* @var string $commonvars
*/
- var $commonvars;
+ public $commonvars;
/**
* 2D array of grade items and categories
*/
- var $levels;
+ public $levels;
/**
* Grade items
*/
- var $items;
+ public $items;
/**
* Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item
* @param boolean $category_grade_last category grade item is the last child
* @param array $collapsed array of collapsed categories
*/
- function grade_tree($courseid, $fillers=true, $category_grade_last=false, $collapsed=null, $nooutcomes=false) {
+ public function grade_tree($courseid, $fillers=true, $category_grade_last=false, $collapsed=null, $nooutcomes=false) {
global $USER, $CFG;
$this->courseid = $courseid;
* @param array $collapsed array of collapsed categories
* @return void
*/
- function category_collapse(&$element, $collapsed) {
+ public function category_collapse(&$element, $collapsed) {
if ($element['type'] != 'category') {
return;
}
* @param array $element The seed of the recursion
* @return void
*/
- function no_outcomes(&$element) {
+ public function no_outcomes(&$element) {
if ($element['type'] != 'category') {
return;
}
* @param array $element The seed of the recursion
* @return void
*/
- function category_grade_last(&$element) {
+ public function category_grade_last(&$element) {
if (empty($element['children'])) {
return;
}
* @param int $depth
* @return void
*/
- function fill_levels(&$levels, &$element, $depth) {
+ public function fill_levels(&$levels, &$element, $depth) {
if (!array_key_exists($depth, $levels)) {
$levels[$depth] = array();
}
/**
* Static recursive helper - makes full tree (all leafes are at the same level)
*/
- function inject_fillers(&$element, $depth) {
+ public function inject_fillers(&$element, $depth) {
$depth++;
if (empty($element['children'])) {
/**
* Static recursive helper - add colspan information into categories
*/
- function inject_colspans(&$element) {
+ public function inject_colspans(&$element) {
if (empty($element['children'])) {
return 1;
}
* @param int $eid
* @return object element
*/
- function locate_element($eid) {
+ public function locate_element($eid) {
// it is a grade - construct a new object
if (strpos($eid, 'n') === 0) {
if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
return null;
}
-
+
/**
* Returns a well-formed XML representation of the grade-tree using recursion.
* @param array $root The current element in the recursion. If null, starts at the top of the tree.
* @return string $xml
*/
- function exportToXML($root=null, $tabs="\t") {
+ public function exportToXML($root=null, $tabs="\t") {
$xml = null;
$first = false;
if (is_null($root)) {
$xml .= "<gradetree>\n";
$first = true;
}
-
+
$type = 'undefined';
if (strpos($root['object']->table, 'grade_categories') !== false) {
$type = 'category';
} elseif (strpos($root['object']->table, 'grade_outcomes') !== false) {
$type = 'outcome';
}
-
+
$xml .= "$tabs<element type=\"$type\">\n";
foreach ($root['object'] as $var => $value) {
if (!is_object($value) && !is_array($value) && !empty($value)) {
}
$xml .= "$tabs\t</children>\n";
}
-
+
$xml .= "$tabs</element>\n";
if ($first) {
$xml .= "</gradetree>";
}
-
+
return $xml;
}
-
+
/**
* Returns a JSON representation of the grade-tree using recursion.
* @param array $root The current element in the recursion. If null, starts at the top of the tree.
* @param int $switch The position (first or last) of the aggregations
* @return string $xml
*/
- function exportToJSON($root=null, $tabs="\t") {
+ public function exportToJSON($root=null, $tabs="\t") {
$json = null;
$first = false;
if (is_null($root)) {
$root = $this->top_element;
$first = true;
}
-
+
$name = '';
if (strpos($root['object']->table, 'grade_categories') !== false) {
$name = $root['object']->fullname;
if ($name == '?') {
- $name = $root['object']->get_name();
+ $name = $root['object']->get_name();
}
} elseif (strpos($root['object']->table, 'grade_items') !== false) {
$name = $root['object']->itemname;
} elseif (strpos($root['object']->table, 'grade_outcomes') !== false) {
$name = $root['object']->itemname;
}
-
+
$json .= "$tabs {\n";
$json .= "$tabs\t \"type\": \"{$root['type']}\",\n";
$json .= "$tabs\t \"name\": \"$name\",\n";
$json .= "$tabs\t \"$var\": \"$value\",\n";
}
}
-
+
$json = substr($json, 0, strrpos($json, ','));
-
+
if (!empty($root['children'])) {
$json .= ",\n$tabs\t\"children\": [\n";
foreach ($root['children'] as $sortorder => $child) {
}
$json = substr($json, 0, strrpos($json, ','));
$json .= "\n$tabs\t]\n";
- }
+ }
if ($first) {
$json .= "\n}";
} else {
$json .= "\n$tabs},\n";
}
-
+
return $json;
}
+
+ public function get_levels() {
+ return $this->levels;
+ }
+
+ public function get_items() {
+ return $this->items;
+ }
+
+ public function get_item($itemid) {
+ if (array_key_exists($itemid, $this->items)) {
+ return $this->items[$itemid];
+ } else {
+ return false;
+ }
+ }
}
?>
* @return mixed - array of grade item instances (one if $only_main_item true), false if error or not found
*/
function grade_get_grade_items_for_activity($cm, $only_main_item=false) {
- global $CFG;
+ global $CFG, $DB;
if (!isset($cm->modname)) {
- $cm = get_record_sql("SELECT cm.*, m.name, md.name as modname
- FROM {$CFG->prefix}course_modules cm,
- {$CFG->prefix}modules md,
- WHERE cm.id = {$cm->id} AND md.id = cm.module");
+ $params = array($cm->id);
+ $cm = $DB->get_record_sql("SELECT cm.*, m.name, md.name as modname
+ FROM {course_modules} cm,
+ {modules} md,
+ WHERE cm.id = ? AND md.id = cm.module", $params);
}
* @return array $cm objects
*/
function grade_get_gradable_activities($courseid, $modulename='') {
- global $CFG;
+ global $CFG, $DB;
if (empty($modulename)) {
- if (!$modules = get_records('modules', 'visible', '1')) {
+ if (!$modules = $DB->get_records('modules', array('visible' => '1'))) {
return false;
}
$result = array();
}
}
+ $params = array($courseid, $modulename, GRADE_TYPE_NONE, $modulename);
$sql = "SELECT cm.*, m.name, md.name as modname
- FROM {$CFG->prefix}grade_items gi, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules md, {$CFG->prefix}$modulename m
- WHERE gi.courseid = $courseid AND
+ FROM {grade_items} gi, {course_modules} cm, {modules} md, {$modulename} m
+ WHERE gi.courseid = ? AND
gi.itemtype = 'mod' AND
- gi.itemmodule = '$modulename' AND
+ gi.itemmodule = ? AND
gi.itemnumber = 0 AND
- gi.gradetype != ".GRADE_TYPE_NONE." AND
+ gi.gradetype != ? AND
gi.iteminstance = cm.instance AND
cm.instance = m.id AND
- md.name = '$modulename' AND
+ md.name = ? AND
md.id = cm.module";
- return get_records_sql($sql);
+ return $DB->get_records_sql($sql, $params);
}
-?>
\ No newline at end of file
+?>
///////////////////////////////////////////////////////////////////////////
-require_once '../../../config.php';
+require_once '../../../config.php';
require_once $CFG->libdir.'/gradelib.php';
require_once $CFG->dirroot.'/grade/lib.php';
// require_once $CFG->dirroot.'/grade/report/grader/ajaxlib.php';
if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) { // we must verify course id here!
print_error('invalidgradeitmeid');
}
-
+
/**
* Code copied from grade/report/grader/lib.php line 187+
*/
$gradestr->itemname = $grade_item->get_name();
$json_object->message = get_string($errorstr, 'grades', $gradestr);
$json_object->result = "error";
-
+
}
$finalvalue = $finalgrade;
$finalvalue = $feedback;
}
-
+
if (!empty($json_object->result) && $json_object->result == 'error') {
echo json_encode($json_object);
die();
} else {
$json_object->gradevalue = $finalvalue;
-
+
if ($grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE)) {
$json_object->result = 'success';
$json_object->message = false;
echo json_encode();
die();
}
-
+
// Get row data
$sql = "SELECT gg.id, gi.id AS itemid, gi.scaleid AS scale, gg.userid AS userid, finalgrade, gg.overridden AS overridden "
- . "FROM {$CFG->prefix}grade_grades gg, {$CFG->prefix}grade_items gi WHERE "
+ . "FROM {grade_grades} gg, {grade_items} gi WHERE "
. "gi.courseid = $courseid AND gg.itemid = gi.id AND gg.userid = $userid";
$records = get_records_sql($sql);
$json_object->row = $records;
} else {
$json_object = new stdClass();
$json_object->result = "error";
- $json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
+ $json_object->message = "Missing parameter to ajax UPDATE callback: \n" .
" userid: $userid,\n itemid: $itemid\n, type: $type\n, newvalue: $newvalue";
echo json_encode($json_object);
}
$toggle_type = optional_param('toggle_type', 0, PARAM_ALPHANUM);
/// basic access checks
-if (!$course = get_record('course', 'id', $courseid)) {
+if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
require_login($course);
// Initialise the grader report object
if (ajaxenabled() && $report->get_pref('enableajax')) {
require_once $CFG->dirroot.'/grade/report/grader/ajaxlib.php';
-
- require_js(array('yui_yahoo',
- 'yui_dom',
- 'yui_event',
- 'yui_json',
- 'yui_connection',
+
+ require_js(array('yui_yahoo',
+ 'yui_dom',
+ 'yui_event',
+ 'yui_json',
+ 'yui_connection',
'yui_dragdrop',
'yui_animation'));
// print submit button
if ($USER->gradeediting[$course->id] && ($report->get_pref('showquickfeedback')
- ||
+ ||
$report->get_pref('quickgrading')) && !$report->get_pref('enableajax')) {
echo '<div class="submit"><input type="submit" value="'.get_string('update').'" /></div>';
echo '</div></form>';
* The final grades.
* @var array $grades
*/
- var $grades;
+ public $grades;
/**
* Array of errors for bulk grades updating.
* @var array $gradeserror
*/
- var $gradeserror = array();
+ public $gradeserror = array();
//// SQL-RELATED
* The id of the grade_item by which this report will be sorted.
* @var int $sortitemid
*/
- var $sortitemid;
+ public $sortitemid;
/**
* Sortorder used in the SQL selections.
* @var int $sortorder
*/
- var $sortorder;
+ public $sortorder;
/**
* An SQL fragment affecting the search for users.
* @var string $userselect
*/
- var $userselect;
+ public $userselect;
+
+ /**
+ * The bound params for $userselect
+ * @var array $userselect_params
+ */
+ public $userselect_params = array();
/**
* List of collapsed categories from user preference
* @var array $collapsed
*/
- var $collapsed;
+ public $collapsed;
/**
* A count of the rows, used for css classes.
* @var int $rowcount
*/
- var $rowcount = 0;
+ public $rowcount = 0;
/**
* Capability check caching
* */
- var $canviewhidden;
+ public $canviewhidden;
/**
* Constructor. Sets local copies of user preferences and initialises grade_tree.
* @param int $page The current page being viewed (when report is paged)
* @param int $sortitemid The id of the grade_item by which to sort the table
*/
- function grade_report_grader($courseid, $gpr, $context, $page=null, $sortitemid=null) {
+ public function __construct($courseid, $gpr, $context, $page=null, $sortitemid=null) {
global $CFG;
- parent::grade_report($courseid, $gpr, $context, $page);
+ parent::__construct($courseid, $gpr, $context, $page);
$this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id));
* @param array $data form submission (with magic quotes)
* @return array empty array if success, array of warnings if something fails.
*/
- function process_data($data) {
+ public function process_data($data) {
$warnings = array();
// always initialize all arrays
* all this should be in the new table class that we might need to use
* for displaying grades.
*/
- function setup_sortitemid() {
+ private function setup_sortitemid() {
global $SESSION;
// this is the first sort, i.e. by last name
if (!isset($SESSION->gradeuserreport->sortitemid)) {
if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') {
- $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
+ $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
} else {
$this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
}
* this is check for user roles because there could be some users with grades
* but not supposed to be displayed
*/
- function load_users() {
- global $CFG;
+ public function load_users() {
+ global $CFG, $DB;
+ $params = array();
+ list($usql, $gbr_params) = $DB->get_in_or_equal(explode(',', $this->gradebookroles));
if (is_numeric($this->sortitemid)) {
+ $params = array_merge(array($this->sortitemid), $gbr_params, $this->groupwheresql_params);
$sort = "g.finalgrade $this->sortorder";
$sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
- FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
- {$CFG->prefix}user u ON (u.id = g.userid AND g.itemid = $this->sortitemid)
- LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
+ FROM {grade_grades} g RIGHT OUTER JOIN
+ {user} u ON (u.id = g.userid AND g.itemid = ?)
+ LEFT JOIN {role_assignments} ra ON u.id = ra.userid
$this->groupsql
- WHERE ra.roleid in ($this->gradebookroles)
+ WHERE ra.roleid in $usql
$this->groupwheresql
AND ra.contextid ".get_related_contexts_string($this->context)."
ORDER BY $sort";
$sort = "u.idnumber $this->sortorder"; break;
}
+ $params = array_merge($gbr_params, $this->groupwheresql_params);
$sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
- FROM {$CFG->prefix}user u
- JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
+ FROM {user} u
+ JOIN {role_assignments} ra ON u.id = ra.userid
$this->groupsql
- WHERE ra.roleid in ($this->gradebookroles)
+ WHERE ra.roleid $usql
$this->groupwheresql
AND ra.contextid ".get_related_contexts_string($this->context)."
ORDER BY $sort";
}
- $this->users = get_records_sql($sql, $this->get_pref('studentsperpage') * $this->page,
- $this->get_pref('studentsperpage'));
+ $this->users = $DB->get_records_sql($sql, $params, $this->get_pref('studentsperpage') * $this->page, $this->get_pref('studentsperpage'));
if (empty($this->users)) {
$this->userselect = '';
$this->users = array();
} else {
- $this->userselect = 'AND g.userid in ('.implode(',', array_keys($this->users)).')';
+ list($usql, $params) = $DB->get_in_or_equal(array_keys($this->users));
+ $this->userselect = "AND g.userid $usql";
+ $this->userselect_params = $params;
}
return $this->users;
* we supply the userids in this query, and get all the grades
* pulls out all the grades, this does not need to worry about paging
*/
- function load_final_grades() {
- global $CFG;
+ public function load_final_grades() {
+ global $CFG, $DB;
// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
+ $params = array_merge(array($this->courseid), $this->userselect_params);
$sql = "SELECT g.*
- FROM {$CFG->prefix}grade_items gi,
- {$CFG->prefix}grade_grades g
- WHERE g.itemid = gi.id AND gi.courseid = {$this->courseid} {$this->userselect}";
+ FROM {grade_items} gi,
+ {grade_grades} g
+ WHERE g.itemid = gi.id AND gi.courseid = ? {$this->userselect}";
$userids = array_keys($this->users);
- if ($grades = get_records_sql($sql)) {
+ if ($grades = $DB->get_records_sql($sql, $params)) {
foreach ($grades as $graderec) {
- if (in_array($graderec->userid, $userids) and array_key_exists($graderec->itemid, $this->gtree->items)) { // some items may not be present!!
+ if (in_array($graderec->userid, $userids) and array_key_exists($graderec->itemid, $this->gtree->get_items())) { // some items may not be present!!
$this->grades[$graderec->userid][$graderec->itemid] = new grade_grade($graderec, false);
- $this->grades[$graderec->userid][$graderec->itemid]->grade_item =& $this->gtree->items[$graderec->itemid]; // db caching
+ $this->grades[$graderec->userid][$graderec->itemid]->grade_item =& $this->gtree->get_item($graderec->itemid); // db caching
}
}
}
// prefil grades that do not exist yet
foreach ($userids as $userid) {
- foreach ($this->gtree->items as $itemid=>$unused) {
+ foreach ($this->gtree->get_items() as $itemid=>$unused) {
if (!isset($this->grades[$userid][$itemid])) {
$this->grades[$userid][$itemid] = new grade_grade();
$this->grades[$userid][$itemid]->itemid = $itemid;
$this->grades[$userid][$itemid]->userid = $userid;
- $this->grades[$userid][$itemid]->grade_item =& $this->gtree->items[$itemid]; // db caching
+ $this->grades[$userid][$itemid]->grade_item =& $this->gtree->get_item($itemid); // db caching
}
}
}
* Builds and returns a div with on/off toggles.
* @return string HTML code
*/
- function get_toggles_html() {
+ public function get_toggles_html() {
global $CFG, $USER;
$html = '<div id="grade-report-toggles">';
* @param bool $return Whether to return the HTML string rather than printing it
* @return void
*/
- function print_toggle($type, $return=false) {
+ public function print_toggle($type, $return=false) {
global $CFG;
$icons = array('eyecons' => 't/hide.gif',
* Builds and returns the HTML code for the headers.
* @return string $headerhtml
*/
- function get_headerhtml() {
+ public function get_headerhtml() {
global $CFG, $USER;
$strsortasc = $this->get_lang_string('sortasc', 'grades');
// Prepare Table Headers
$headerhtml = '';
- $numrows = count($this->gtree->levels);
+ $numrows = count($this->gtree->get_levels());
$columns_to_unset = array();
- foreach ($this->gtree->levels as $key=>$row) {
+ foreach ($this->gtree->get_levels() as $key=>$row) {
$columncount = 0;
if ($key == 0) {
// do not display course grade category
* Builds and return the HTML rows of the table (grades headed by student).
* @return string HTML
*/
- function get_studentshtml() {
+ public function get_studentshtml() {
global $CFG, $USER, $DB;
$studentshtml = '';
$scales_list = array();
$tabindices = array();
- foreach ($this->gtree->items as $item) {
+ foreach ($this->gtree->get_items() as $item) {
if (!empty($item->scaleid)) {
$scales_list[] = $item->scaleid;
}
if (!empty($scales_list)) {
$scales_array = $DB->get_records_list('scale', 'id', $scales_list);
}
-
+
$row_classes = array(' even ', ' odd ');
$row_classes = array(' even ', ' odd ');
$altered = array();
$unknown = array();
} else {
- $hiding_affected = grade_grade::get_hiding_affected($this->grades[$userid], $this->gtree->items);
+ $hiding_affected = grade_grade::get_hiding_affected($this->grades[$userid], $this->gtree->get_items());
$altered = $hiding_affected['altered'];
$unknown = $hiding_affected['unknown'];
unset($hiding_affected);
$user->idnumber.'</a></th>';
}
- foreach ($this->gtree->items as $itemid=>$unused) {
- $item =& $this->gtree->items[$itemid];
+ foreach ($this->gtree->get_items() as $itemid=>$unused) {
+ $item =& $this->gtree->get_item($itemid);
$grade = $this->grades[$userid][$item->id];
// Get the decimal points preference for this item
$hidden = ' hidden ';
}
- $gradepass = ' gradefail ';
+ $gradepass = ' gradefail ';
if ($grade->is_passed($item)) {
$gradepass = ' gradepass ';
} elseif (is_null($grade->is_passed($item))) {
* @param bool $grouponly Whether to return only group averages or all averages.
* @return string HTML
*/
- function get_avghtml($grouponly=false) {
- global $CFG, $USER;
+ public function get_avghtml($grouponly=false) {
+ global $CFG, $USER, $DB;
if (!$this->canviewhidden) {
// totals might be affected by hiding, if user can not see hidden grades the aggregations might be altered
$showaverages = $this->currentgroup && $this->get_pref('showgroups');
$groupsql = $this->groupsql;
$groupwheresql = $this->groupwheresql;
+ $groupwheresql_params = $this->groupwheresql_params;
$avgcssclass = 'groupavg';
} else {
$straverage = get_string('overallaverage', 'grades');
$showaverages = $this->get_pref('showaverages');
$groupsql = "";
$groupwheresql = "";
+ $groupwheresql_params = array();
}
if ($shownumberofgrades) {
$totalcount = $this->get_numusers($grouponly);
+ list($usql, $roles_params) = $DB->get_in_or_equal(explode(',', $this->gradebookroles));
+
if ($showaverages) {
+ $params = array_merge(array($this->courseid), $roles_params, $groupwheresql_params);
// find sums of all grade items in course
$SQL = "SELECT g.itemid, SUM(g.finalgrade) AS sum
- FROM {$CFG->prefix}grade_items gi
- JOIN {$CFG->prefix}grade_grades g ON g.itemid = gi.id
- JOIN {$CFG->prefix}user u ON u.id = g.userid
- JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
+ FROM {grade_items} gi
+ JOIN {grade_grades} g ON g.itemid = gi.id
+ JOIN {user} u ON u.id = g.userid
+ JOIN {role_assignments} ra ON ra.userid = u.id
$groupsql
- WHERE gi.courseid = $this->courseid
- AND ra.roleid in ($this->gradebookroles)
+ WHERE gi.courseid = ?
+ AND ra.roleid $usql
AND ra.contextid ".get_related_contexts_string($this->context)."
AND g.finalgrade IS NOT NULL
$groupwheresql
GROUP BY g.itemid";
$sum_array = array();
- if ($sums = get_records_sql($SQL)) {
+ if ($sums = $DB->get_records_sql($SQL, $params)) {
foreach ($sums as $itemid => $csum) {
$sum_array[$itemid] = $csum->sum;
}
// MDL-10875 Empty grades must be evaluated as grademin, NOT always 0
// This query returns a count of ungraded grades (NULL finalgrade OR no matching record in grade_grades table)
+ $params = array_merge(array($this->courseid), $roles_params, $groupwheresql_params);
$SQL = "SELECT gi.id, COUNT(u.id) AS count
- FROM {$CFG->prefix}grade_items gi
- CROSS JOIN {$CFG->prefix}user u
- JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
- LEFT OUTER JOIN {$CFG->prefix}grade_grades g ON (g.itemid = gi.id AND g.userid = u.id AND g.finalgrade IS NOT NULL)
+ FROM {grade_items} gi
+ CROSS JOIN {user} u
+ JOIN {role_assignments} ra ON ra.userid = u.id
+ LEFT OUTER JOIN {grade_grades} g ON (g.itemid = gi.id AND g.userid = u.id AND g.finalgrade IS NOT NULL)
$groupsql
- WHERE gi.courseid = $this->courseid
- AND ra.roleid in ($this->gradebookroles)
+ WHERE gi.courseid = ?
+ AND ra.roleid $usql
AND ra.contextid ".get_related_contexts_string($this->context)."
AND g.id IS NULL
$groupwheresql
GROUP BY gi.id";
- $ungraded_counts = get_records_sql($SQL);
+ $ungraded_counts = $DB->get_records_sql($SQL, $params);
foreach ($this->gtree->items as $itemid=>$unused) {
$item =& $this->gtree->items[$itemid];
* Builds and return the HTML row of ranges for each column (i.e. range).
* @return string HTML
*/
- function get_rangehtml() {
+ public function get_rangehtml() {
global $USER;
$showuseridnumber = $this->get_pref('showuseridnumber');
$columncount++;
}
- foreach ($this->gtree->items as $itemid=>$unused) {
- $item =& $this->gtree->items[$itemid];
+ foreach ($this->gtree->get_items() as $itemid=>$unused) {
+ $item =& $this->gtree->get_item($itemid);
// Determine which display type to use for this average
if ($USER->gradeediting[$this->courseid]) {
}
return $scalehtml;
}
-
+
/**
* Builds and return the HTML row of ranges for each column (i.e. range).
* @return string HTML
*/
- function get_iconshtml() {
+ public function get_iconshtml() {
global $USER;
$iconshtml = '';
. '<th class="header c0 range" scope="row" '.$colspan.'>'.$this->get_lang_string('controls','grades').'</th>';
$columncount = 1;
- foreach ($this->gtree->items as $itemid=>$unused) {
+ foreach ($this->gtree->get_items() as $itemid=>$unused) {
// emulate grade element
- $item =& $this->gtree->items[$itemid];
-
+ $item =& $this->gtree->get_item($itemid);
+
$eid = $this->gtree->get_item_eid($item);
$element = $this->gtree->locate_element($eid);
* @param object $object
* @return string HTML
*/
- function get_icons($element) {
+ protected function get_icons($element) {
global $CFG, $USER;
if (!$USER->gradeediting[$this->courseid]) {
* @param object $object
* @return string HTML
*/
- function get_collapsing_icon($element) {
+ protected function get_collapsing_icon($element) {
global $CFG;
$contract_expand_icon = '';
* @param string $action Which action to take (edit, delete etc...)
* @return
*/
- function process_action($target, $action) {
+ public function process_action($target, $action) {
// TODO: this code should be in some grade_tree static method
$targettype = substr($target, 0, 1);
$targetid = substr($target, 1);
* @abstract
* @package gradebook
*/
-class grade_report {
+abstract class grade_report {
/**
* The courseid.
* @var int $courseid
*/
- var $courseid;
+ public $courseid;
/**
* The course.
* @var object $course
*/
- var $course;
+ public $course;
/** Grade plugin return tracking object.
var $gpr;
* The context.
* @var int $context
*/
- var $context;
+ public $context;
/**
* The grade_tree object.
* @var object $gtree
*/
- var $gtree;
+ public $gtree;
/**
* User preferences related to this report.
* @var array $prefs
*/
- var $prefs = array();
+ public $prefs = array();
/**
* The roles for this report.
* @var string $gradebookroles
*/
- var $gradebookroles;
+ public $gradebookroles;
/**
* base url for sorting by first/last name.
* @var string $baseurl
*/
- var $baseurl;
+ public $baseurl;
/**
* base url for paging.
* @var string $pbarurl
*/
- var $pbarurl;
+ public $pbarurl;
/**
* Current page (for paging).
* @var int $page
*/
- var $page;
+ public $page;
/**
* Array of cached language strings (using get_string() all the time takes a long time!).
* @var array $lang_strings
*/
- var $lang_strings = array();
+ public $lang_strings = array();
//// GROUP VARIABLES (including SQL)
* The current group being displayed.
* @var int $currentgroup
*/
- var $currentgroup;
+ public $currentgroup;
/**
* A HTML select element used to select the current group.
* @var string $group_selector
*/
- var $group_selector;
+ public $group_selector;
/**
* An SQL fragment used to add linking information to the group tables.
* @var string $groupsql
*/
- var $groupsql;
+ protected $groupsql;
/**
* An SQL constraint to append to the queries used by this object to build the report.
* @var string $groupwheresql
*/
- var $groupwheresql;
+ protected $groupwheresql;
+
+ /**
+ * The ordered params for $groupwheresql
+ * @var array $groupwheresql_params
+ */
+ protected $groupwheresql_params = array();
/**
* @param string $context
* @param int $page The current page being viewed (when report is paged)
*/
- function grade_report($courseid, $gpr, $context, $page=null) {
+ public function __construct($courseid, $gpr, $context, $page=null) {
global $CFG, $COURSE;
if (empty($CFG->gradebookroles)) {
* @param int $objectid An optional itemid or categoryid to check for a more fine-grained preference
* @return mixed The value of the preference
*/
- function get_pref($pref, $objectid=null) {
+ public function get_pref($pref, $objectid=null) {
global $CFG;
$fullprefname = 'grade_report_' . $pref;
$shortprefname = 'grade_' . $pref;
} elseif (isset($CFG->$fullprefname)) {
$retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
} elseif (isset($CFG->$shortprefname)) {
- $retval = get_user_preferences($fullprefname, $CFG->$shortprefname);
+ $retval = get_user_preferences($fullprefname, $CFG->$shortprefname);
} else {
$retval = null;
}
* @param int $itemid An optional itemid to which the preference will be assigned
* @return bool Success or failure.
*/
- function set_pref($pref, $pref_value='default', $itemid=null) {
+ public function set_pref($pref, $pref_value='default', $itemid=null) {
$fullprefname = 'grade_report_' . $pref;
if ($pref_value == 'default') {
return unset_user_preference($fullprefname.$itemid);
* @param array $data
* @return mixed True or array of errors
*/
- function process_data($data) {
- // Implement in children classes
- }
+ abstract function process_data($data);
/**
* Processes a single action against a category, grade_item or grade.
* @param string $action Which action to take (edit, delete etc...)
* @return
*/
- function process_action($target, $action) {
- //implement if needed
- }
+ abstract function process_action($target, $action);
/**
* First checks the cached language strings, then returns match if found, or uses get_string()
* @param string $section Optional language section
* @return string
*/
- function get_lang_string($strcode, $section=null) {
+ public function get_lang_string($strcode, $section=null) {
if (empty($this->lang_strings[$strcode])) {
$this->lang_strings[$strcode] = get_string($strcode, $section);
}
* @param boolean $groups include groups limit
* @return int Count of users
*/
- function get_numusers($groups=true) {
- global $CFG;
+ public function get_numusers($groups=true) {
+ global $CFG, $DB;
$groupsql = "";
$groupwheresql = "";
+ list($usql, $params) = $DB->get_in_or_equal(explode(',', $this->gradebookroles));
+
if ($groups) {
$groupsql = $this->groupsql;
$groupwheresql = $this->groupwheresql;
+ $params = array_merge($params, $this->groupwheresql_params);
}
$countsql = "SELECT COUNT(DISTINCT u.id)
- FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
- {$CFG->prefix}user u ON u.id = g.userid
- LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
+ FROM {grade_grades} g RIGHT OUTER JOIN
+ {user} u ON u.id = g.userid
+ LEFT JOIN {role_assignments} ra ON u.id = ra.userid
$groupsql
- WHERE ra.roleid in ($this->gradebookroles)
+ WHERE ra.roleid $usql
$groupwheresql
AND ra.contextid ".get_related_contexts_string($this->context);
- return count_records_sql($countsql);
+ return $DB->count_records_sql($countsql, $params);
}
/**
* Sets up this object's group variables, mainly to restrict the selection of users to display.
*/
- function setup_groups() {
+ private function setup_groups() {
global $CFG;
/// find out current groups mode
$this->currentgroup = groups_get_course_group($this->course);
if ($this->currentgroup) {
- $this->groupsql = " LEFT JOIN {$CFG->prefix}groups_members gm ON gm.userid = u.id ";
- $this->groupwheresql = " AND gm.groupid = $this->currentgroup ";
+ $this->groupsql = " LEFT JOIN {groups_members} gm ON gm.userid = u.id ";
+ $this->groupwheresql = " AND gm.groupid = ? ";
+ $this->groupwheresql_params = array($this->currentgroup);
}
}
* @param string $sort_link
* @param string HTML
*/
- function get_sort_arrow($direction='move', $sort_link=null) {
+ protected function get_sort_arrow($direction='move', $sort_link=null) {
$matrix = array('up' => 'asc', 'down' => 'desc', 'move' => 'desc');
$strsort = $this->get_lang_string('sort' . $matrix[$direction]);
$arrow = print_arrow($direction, $strsort, true);
if (is_array($report_info[$outcomeid]['items'])) {
foreach ($report_info[$outcomeid]['items'] as $itemid => $item) {
$sql = "SELECT itemid, AVG(finalgrade) AS avg, COUNT(finalgrade) AS count
- FROM {$CFG->prefix}grade_grades
+ FROM {grade_grades}
WHERE itemid = $itemid
GROUP BY itemid";
$info = get_records_sql($sql);
} else if (!is_null($finalgrade)) {
/// find the number of users with a higher grade
$sql = "SELECT COUNT(DISTINCT(userid))
- FROM {$CFG->prefix}grade_grades
+ FROM {grade_grades}
WHERE finalgrade IS NOT NULL AND finalgrade > $finalgrade
AND itemid = {$grade_item->id}";
$rank = count_records_sql($sql) + 1;
}
$mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
- $mform->setHelpButton('report_overview_showrank', array('showrank', get_string('showrank', 'grades'), 'grade'));
+ $mform->setHelpButton('report_overview_showrank', array('showrank', get_string('showrank', 'grades'), 'grade'));
}
?>
} else {
/// find the number of users with a higher grade
$sql = "SELECT COUNT(DISTINCT(userid))
- FROM {$CFG->prefix}grade_grades
+ FROM {grade_grades}
WHERE finalgrade > {$grade_grade->finalgrade}
AND itemid = {$grade_item->id}";
$rank = count_records_sql($sql) + 1;
}
$mform->addElement('select', 'report_user_showrank', get_string('showrank', 'grades'), $options);
- $mform->setHelpButton('report_user_showrank', array('showrank', get_string('showrank', 'grades'), 'grade'));
+ $mform->setHelpButton('report_user_showrank', array('showrank', get_string('showrank', 'grades'), 'grade'));
$options = array(-1 => get_string('default', 'grades'),
0 => get_string('hide'),
}
$mform->addElement('select', 'report_user_showhiddenitems', get_string('showhiddenitems', 'grades'), $options);
- $mform->setHelpButton('report_user_showhiddenitems', array('showhiddenitems', get_string('showhiddenitems', 'grades'), 'grade'));
+ $mform->setHelpButton('report_user_showhiddenitems', array('showhiddenitems', get_string('showhiddenitems', 'grades'), 'grade'));
}
function grade_report_user_profilereport($course, $user) {
/**
* Returns SQL WHERE conditions
* @param array conditions - must not contain numeric indexes
- * @return array sql part and params
+ * @return array sql part and params
*/
public function where_clause(array $conditions=null) {
$allowed_types = $this->allowed_param_types();
<?php //$Id$
/**
- * Abstarct class for resultsets returned from database functions.
+ * Abstract class for resultsets returned from database functions.
* This is a simple Iterator with needed recorset closing support.
*
- * The differnece from old recorset is that the records are returned
+ * The difference from old recorset is that the records are returned
* as objects, not arrays. You should use "foreach ($recordset as $record) {}"
* followed by "$recordset->close()".
*
* @return void
*/
public abstract function close();
-}
\ No newline at end of file
+}
* @static final protected
* @return mixed array of object instances or false if not found
*/
- protected static function fetch_all_helper($table, $classname, $params) {
+ public static function fetch_all_helper($table, $classname, $params) {
$instance = new $classname();
$classvars = (array)$instance;
$wheresql = array();
- // remove incorrect params
- $params = array();
-
foreach ($params as $var=>$value) {
if (!in_array($var, $instance->required_fields) and !array_key_exists($var, $instance->optional_fields)) {
continue;