[moodle] / contrib / plugins / grade / report / visual / lib.php Repository:

View of /contrib/plugins/grade/report/visual/lib.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (annotate)
Tue Jul 15 03:27:17 2008 WST (16 months, 1 week ago) by dservos
Branch: MAIN
Changes since 1.1: +25 -0 lines
CONTRIB-497
Cleaned up some of the code for report/visual and the flex based application for viewing the visualizations.

Improved the UI of the visualization.

Added documentation to both the php and actionscript 3 (flex) code.

Removed some unneeded files.

TODO:
*Add more visualizations.
*Add printer firendly version.
*Make flex appplications settings less hardcoded and loaded from moodle.
*Load lang files from moodle to flex for full language support.
*Add a settings page for the report.
<?php
 ///////////////////////////////////////////////////////////////////////////
//                                                                       //
// NOTICE OF COPYRIGHT                                                   //
//                                                                       //
// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
//          http://moodle.org                                            //
//                                                                       //
// Copyright (C) 1999 onwards  Martin Dougiamas  http://moodle.com       //
//                                                                       //
// This program is free software; you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by  //
// the Free Software Foundation; either version 2 of the License, or     //
// (at your option) any later version.                                   //
//                                                                       //
// This program is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
// GNU General Public License for more details:                          //
//                                                                       //
//          http://www.gnu.org/copyleft/gpl.html                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////
 
 
require_once($CFG->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++;
                                    }
                            }               
                        }
                    }
                }
        }*/
    }

    /**
    * TODO: have this make the data depending on what kind of visulsation
    * is needed rather then a singal visulasztion witch is currently hardcoded.
    */
    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;
            }
        }
    }

    /**
    *TODO: Have this call on flex.php to generate html for the report.,
    *rather then having it hardcoded as it is now.
    */
    public function adapt_data($printerversion = false) {
        
    }
    
    /**
     * Returns the data for a visulazation in json format.
     * TODO: Use moodle's json encoding functions rather then phps.
     * @param boolean $return if true return a string, other wise echo the data.
     * @return string If return is set to true, returns a string of the data in json format.
     */
    public function get_json($return = true) {
        if ($return) {
            return json_encode($this->visdata['test']);
        } else {
            echo json_encode($this->visdata['test']);
        }
    }
    
    
    /**
     * Returns the data for a visulasation in tab format.
     * @param boolean $return if true return a string, other wise echo the data.
     * @return string If return is set to true, returns a string of the data in tab format.
     */
    public function get_tab($return = true) {
        if ($return) {
            return $this->tab_encode($this->visdata['test2']);
        } else {
            echo $this->tab_encode($this->visdata['test2']);
        }
    }
    
    /**
     * Encodes an array to a string using the tab format.
     *@param array $data the array to encode.
     *@return string a string encoded in tab format based on the given array.
     */
    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;
    }
}
?>

Moodle CVS Admin
ViewVC Help
Powered by ViewVC 1.0.7