Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/grade/report/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00023 require_once($CFG->libdir.'/gradelib.php');
00024 
00030 abstract class grade_report {
00035     public $courseid;
00036 
00041     public $course;
00042 
00046     public $gpr;
00047 
00052     public $context;
00053 
00058     public $gtree;
00059 
00064     public $prefs = array();
00065 
00070     public $gradebookroles;
00071 
00076     public $baseurl;
00077 
00082     public $pbarurl;
00083 
00088     public $page;
00089 
00094     public $lang_strings = array();
00095 
00097 
00102     public $currentgroup;
00103 
00108     var $groupmode;
00109 
00114     public $group_selector;
00115 
00120     protected $groupsql;
00121 
00126     protected $groupwheresql;
00127 
00132     protected $groupwheresql_params = array();
00133 
00134 
00142     public function __construct($courseid, $gpr, $context, $page=null) {
00143         global $CFG, $COURSE, $DB;
00144 
00145         if (empty($CFG->gradebookroles)) {
00146             print_error('norolesdefined', 'grades');
00147         }
00148 
00149 
00150         $this->courseid  = $courseid;
00151         if ($this->courseid == $COURSE->id) {
00152             $this->course = $COURSE;
00153         } else {
00154             $this->course = $DB->get_record('course', array('id' => $this->courseid));
00155         }
00156 
00157         $this->gpr       = $gpr;
00158         $this->context   = $context;
00159         $this->page      = $page;
00160 
00161         // roles to be displayed in the gradebook
00162         $this->gradebookroles = $CFG->gradebookroles;
00163 
00164         // Set up link to preferences page
00165         $this->preferences_page = $CFG->wwwroot.'/grade/report/grader/preferences.php?id='.$courseid;
00166 
00167         // init gtree in child class
00168     }
00169 
00180     public function get_pref($pref, $objectid=null) {
00181         global $CFG;
00182         $fullprefname = 'grade_report_' . $pref;
00183         $shortprefname = 'grade_' . $pref;
00184 
00185         $retval = null;
00186 
00187         if (!isset($this) OR get_class($this) != 'grade_report') {
00188             if (!empty($objectid)) {
00189                 $retval = get_user_preferences($fullprefname . $objectid, grade_report::get_pref($pref));
00190             } elseif (isset($CFG->$fullprefname)) {
00191                 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
00192             } elseif (isset($CFG->$shortprefname)) {
00193                 $retval = get_user_preferences($fullprefname, $CFG->$shortprefname);
00194             } else {
00195                 $retval = null;
00196             }
00197         } else {
00198             if (empty($this->prefs[$pref.$objectid])) {
00199 
00200                 if (!empty($objectid)) {
00201                     $retval = get_user_preferences($fullprefname . $objectid);
00202                     if (empty($retval)) {
00203                         // No item pref found, we are returning the global preference
00204                         $retval = $this->get_pref($pref);
00205                         $objectid = null;
00206                     }
00207                 } else {
00208                     $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
00209                 }
00210                 $this->prefs[$pref.$objectid] = $retval;
00211             } else {
00212                 $retval = $this->prefs[$pref.$objectid];
00213             }
00214         }
00215 
00216         return $retval;
00217     }
00218 
00228     public function set_pref($pref, $pref_value='default', $itemid=null) {
00229         $fullprefname = 'grade_report_' . $pref;
00230         if ($pref_value == 'default') {
00231             return unset_user_preference($fullprefname.$itemid);
00232         } else {
00233             return set_user_preference($fullprefname.$itemid, $pref_value);
00234         }
00235     }
00236 
00243     abstract function process_data($data);
00244 
00251     abstract function process_action($target, $action);
00252 
00260     public function get_lang_string($strcode, $section=null) {
00261         if (empty($this->lang_strings[$strcode])) {
00262             $this->lang_strings[$strcode] = get_string($strcode, $section);
00263         }
00264         return $this->lang_strings[$strcode];
00265     }
00266 
00272     public function get_numusers($groups=true) {
00273         global $CFG, $DB;
00274 
00275         $groupsql      = "";
00276         $groupwheresql = "";
00277 
00278         //limit to users with a gradeable role
00279         list($gradebookrolessql, $gradebookrolesparams) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
00280 
00281         //limit to users with an active enrollment
00282         list($enrolledsql, $enrolledparams) = get_enrolled_sql($this->context);
00283 
00284         $params = array_merge($gradebookrolesparams, $enrolledparams);
00285 
00286         if ($groups) {
00287             $groupsql      = $this->groupsql;
00288             $groupwheresql = $this->groupwheresql;
00289             $params        = array_merge($params, $this->groupwheresql_params);
00290         }
00291 
00292         $countsql = "SELECT COUNT(DISTINCT u.id)
00293                        FROM {user} u
00294                        JOIN ($enrolledsql) je
00295                             ON je.id = u.id
00296                        JOIN {role_assignments} ra
00297                             ON u.id = ra.userid
00298                        $groupsql
00299                       WHERE ra.roleid $gradebookrolessql
00300                             AND u.deleted = 0
00301                             $groupwheresql
00302                             AND ra.contextid ".get_related_contexts_string($this->context);
00303         return $DB->count_records_sql($countsql, $params);
00304     }
00305 
00309     protected function setup_groups() {
00311         if ($this->groupmode = groups_get_course_groupmode($this->course)) {
00312             $this->currentgroup = groups_get_course_group($this->course, true);
00313             $this->group_selector = groups_print_course_menu($this->course, $this->pbarurl, true);
00314 
00315             if ($this->groupmode == SEPARATEGROUPS and !$this->currentgroup and !has_capability('moodle/site:accessallgroups', $this->context)) {
00316                 $this->currentgroup = -2; // means can not access any groups at all
00317             }
00318 
00319             if ($this->currentgroup) {
00320                 $this->groupsql             = " JOIN {groups_members} gm ON gm.userid = u.id ";
00321                 $this->groupwheresql        = " AND gm.groupid = :gr_grpid ";
00322                 $this->groupwheresql_params = array('gr_grpid'=>$this->currentgroup);
00323             }
00324         }
00325     }
00326 
00333     protected function get_sort_arrow($direction='move', $sortlink=null) {
00334         global $OUTPUT;
00335         $matrix = array('up' => 'desc', 'down' => 'asc', 'move' => 'desc');
00336         $strsort = $this->get_lang_string('sort' . $matrix[$direction]);
00337 
00338         $arrow = print_arrow($direction, $strsort, true);
00339         return html_writer::link($sortlink, $arrow, array('title'=>$strsort));
00340     }
00341 
00349     protected function blank_hidden_total($courseid, $course_item, $finalgrade) {
00350         global $CFG, $DB;
00351         static $hiding_affected = null;//array of items in this course affected by hiding
00352 
00353         //if we're dealing with multiple users we need to know when we've moved on to a new user
00354         static $previous_userid = null;
00355 
00356         if( $this->showtotalsifcontainhidden==GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN ) {
00357             return $finalgrade;
00358         }
00359 
00360         //if we've moved on to another user don't return the previous user's affected grades
00361         if ($previous_userid!=$this->user->id) {
00362             $hiding_affected = null;
00363             $previous_userid = $this->user->id;
00364         }
00365 
00366         if( !$hiding_affected ) {
00367             $items = grade_item::fetch_all(array('courseid'=>$courseid));
00368             $grades = array();
00369             $sql = "SELECT g.*
00370                       FROM {grade_grades} g
00371                       JOIN {grade_items} gi ON gi.id = g.itemid
00372                      WHERE g.userid = {$this->user->id} AND gi.courseid = {$courseid}";
00373             if ($gradesrecords = $DB->get_records_sql($sql)) {
00374                 foreach ($gradesrecords as $grade) {
00375                     $grades[$grade->itemid] = new grade_grade($grade, false);
00376                 }
00377                 unset($gradesrecords);
00378             }
00379             foreach ($items as $itemid=>$unused) {
00380                 if (!isset($grades[$itemid])) {
00381                     $grade_grade = new grade_grade();
00382                     $grade_grade->userid = $this->user->id;
00383                     $grade_grade->itemid = $items[$itemid]->id;
00384                     $grades[$itemid] = $grade_grade;
00385                 }
00386                 $grades[$itemid]->grade_item =& $items[$itemid];
00387             }
00388             $hiding_affected = grade_grade::get_hiding_affected($grades, $items);
00389         }
00390 
00391         //if the item definitely depends on a hidden item
00392         if (array_key_exists($course_item->id, $hiding_affected['altered'])) {
00393             if( !$this->showtotalsifcontainhidden ) {
00394                 //hide the grade
00395                 $finalgrade = null;
00396             }
00397             else {
00398                 //use reprocessed marks that exclude hidden items
00399                 $finalgrade = $hiding_affected['altered'][$course_item->id];
00400             }
00401         } else if (!empty($hiding_affected['unknown'][$course_item->id])) {
00402             //not sure whether or not this item depends on a hidden item
00403             if( !$this->showtotalsifcontainhidden ) {
00404                 //hide the grade
00405                 $finalgrade = null;
00406             }
00407             else {
00408                 //use reprocessed marks that exclude hidden items
00409                 $finalgrade = $hiding_affected['unknown'][$course_item->id];
00410             }
00411         }
00412 
00413         return $finalgrade;
00414     }
00415 }
00416 
 All Data Structures Namespaces Files Functions Variables Enumerations