dirroot . '/grade/report/lib.php'); require_once($CFG->libdir.'/tablelib.php'); class grade_report_visual extends grade_report { /** * Capability to view hidden items. * TODO: Add a check for hidden items and grades. * @var bool $canviewhidden */ private $canviewhidden; /** * Grade objects of users in the course * @var array $grades */ private $grades = array(); private $visdata = array(); /** * The html of the report to output. * @var string $html */ public $html; /** * Constructor. Initialises grade_tree, sets up group, baseurl * and pbarurl. * @param int $courseid the coures id for the report * @param object $gpr grade plugin tracking object * @context string $context */ public function __construct($courseid, $gpr, $context) { global $CFG; parent::__construct($courseid, $gpr, $context, null); $this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id)); /// Set up urls $this->baseurl = 'index.php?id=' . $this->courseid; $this->pbarurl = 'index.php?id=' . $this->courseid; /// Build grade tree $this->gtree = new grade_tree($this->courseid, false); /// Set up Groups /*if ($this->get_pref('stats', 'showgroups') || is_null($this->get_pref('stats', 'showgroups'))) { if(is_callable(array($this, 'setup_groups'))) { $this->setup_groups(); } else { $this->local_setup_groups(); } }*/ } /// Added to keep grade_report happy public function process_data($data){} public function process_action($target, $action){} /** * Based on load user function from grader report. * Pulls out the userids of the users to be used in the stats. * @return array array of user ids to use in stats */ public function load_users() { global $CFG, $DB; if(isset($DB) && !is_null($DB)) { $params = array(); list($usql, $gbr_params) = $DB->get_in_or_equal(explode(',', $this->gradebookroles)); $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber, u.username FROM {user} u JOIN {role_assignments} ra ON u.id = ra.userid $this->groupsql WHERE ra.roleid $usql $this->groupwheresql AND ra.contextid ".get_related_contexts_string($this->context); $params = array_merge($gbr_params, $this->groupwheresql_params); $this->users = $DB->get_records_sql($sql, $params); } else { $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 $this->groupsql WHERE ra.roleid in ($this->gradebookroles) $this->groupwheresql AND ra.contextid ".get_related_contexts_string($this->context); $this->users = get_records_sql($sql); } if (empty($this->users)) { $this->userselect = ''; $this->users = array(); $this->userselect_params = array(); } else { if(isset($DB) && !is_null($DB)) { list($usql, $params) = $DB->get_in_or_equal(array_keys($this->users)); $this->userselect = "AND g.userid $usql"; $this->userselect_params = $params; }else{ $this->userselect = 'AND g.userid in ('.implode(',', array_keys($this->users)).')'; } } return $this->users; } /** * Harvest the grades from the data base and build the finalgrades array. * Filters out hidden, locked and null grades based on users settings. * Partly based on grader reports load_final_grades function. */ public function harvest_data() { global $CFG, $DB; $params = array(); if(isset($DB) && !is_null($DB)) { $params = array_merge(array($this->courseid), $this->userselect_params); /// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it! $sql = "SELECT g.* FROM {grade_items} gi, {grade_grades} g WHERE g.itemid = gi.id AND gi.courseid = ? {$this->userselect}"; $grades = $DB->get_records_sql($sql, $params); } else { /// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it! $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}"; $grades = get_records_sql($sql); } $userids = array_keys($this->users); if ($grades) { 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!! $this->grades[$graderec->itemid][$graderec->userid] = new grade_grade($graderec, false); $this->grades[$graderec->itemid][$graderec->userid]->grade_item =& $this->gtree->items[$graderec->itemid]; // db caching } } } /// prefil grades that do not exist yet foreach ($userids as $userid) { foreach ($this->gtree->items as $itemid=>$unused) { if (!isset($this->grades[$itemid][$userid])) { $this->grades[$itemid][$userid] = new grade_grade(); $this->grades[$itemid][$userid]->itemid = $itemid; $this->grades[$itemid][$userid]->userid = $userid; $this->grades[$itemid][$userid]->grade_item =& $this->gtree->items[$itemid]; // db caching } } } /*$this->finalgrades = array(); /// Build finalgrades array and filliter out unwanted grades. foreach ($this->gtree->items as $id=>$item) { if(($item->gradetype == GRADE_TYPE_SCALE && ($this->get_pref('stats', 'showscaleitems') || is_null($this->get_pref('stats', 'showscaleitems')))) || ($item->gradetype == GRADE_TYPE_VALUE && ($this->get_pref('stats', 'showvalueitems') || is_null($this->get_pref('stats', 'showvalueitems'))))) { $this->finalgrades[$id] = array(); $i = 0; if(isset($this->grades[$id]) && !is_null($this->grades[$id])) { foreach ($this->grades[$id] as $grade) { if( (($grade->is_hidden() && ($this->get_pref('stats', 'usehidden') || is_null($this->get_pref('stats', 'usehidden')))) || !$grade->is_hidden()) && (($grade->is_locked() && ($this->get_pref('stats', 'uselocked') || is_null($this->get_pref('stats', 'uselocked')))) || !$grade->is_locked())) { if($this->get_pref('stats', 'incompleasmin') && is_null($grade->finalgrade)) { $this->finalgrades[$id][$i] = $item->grademin; $i++; } elseif(!is_null($grade->finalgrade)) { $this->finalgrades[$id][$i] = $grade->finalgrade; $i++; } } } } } }*/ } public function report_data() { $this->visdata['test2'] = array(); $this->visdata['test2']['header'] = array(); $this->visdata['test2']['header']['student'] = 'student'; $this->visdata['test2']['header']['grade'] = 'grade'; $this->visdata['test2']['header']['item'] = 'item'; $this->visdata['test2']['header']['group'] = 'group'; foreach($this->grades as $itemkey=>$itemgrades) { foreach($itemgrades as $studentkey=>$studentdata) { if($studentdata != null && $studentdata->finalgrade != null) { foreach(groups_get_user_groups($this->courseid, $studentkey) as $grouping) { foreach($grouping as $group) { $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['student'] = $this->users[$studentkey]->firstname . ' ' . $this->users[$studentkey]->lastname; $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['grade'] = $studentdata->standardise_score($studentdata->finalgrade, $this->gtree->items[$itemkey]->grademin, $this->gtree->items[$itemkey]->grademax, 0, 100); $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['item'] = $this->gtree->items[$itemkey]->get_name(); $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['group'] = groups_get_group_name($group); } } } } } $this->visdata['test'] = array(); $this->visdata['test']['header'] = array(); $this->visdata['test']['header']['student'] = 'student'; foreach($this->gtree->items as $itemid=>$item) { $this->visdata['test']['header']['i' . $itemid] = $this->gtree->items[$itemid]->get_name(); } foreach($this->users as $studentid=>$student) { $this->visdata['test']['s' . $studentid]['student'] = $student->firstname . ' ' . $student->lastname; } foreach($this->grades as $itemkey=>$itemgrades) { foreach($itemgrades as $studentkey=>$studentdata) { $this->visdata['test']['s' . $studentkey]['i' . $itemkey] = $studentdata->finalgrade; } } } public function adapt_data($printerversion = false) { } public function get_json($return = true) { if ($return) { return json_encode($this->visdata['test']); } else { echo json_encode($this->visdata['test']); } } public function get_tab($return = true) { if ($return) { return $this->tab_encode($this->visdata['test2']); } else { echo $this->tab_encode($this->visdata['test2']); } } private function tab_encode(array $data) { $outstring = ''; foreach($data as $row) { foreach($row as $col) { $outstring .= $col . "\t"; } $outstring[strlen($outstring) - 1] = "\n"; } return $outstring; } } ?>