Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/grade/report/overview/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->dirroot . '/grade/report/lib.php');
00024 require_once($CFG->libdir.'/tablelib.php');
00025 
00031 class grade_report_overview extends grade_report {
00032 
00037     public $user;
00038 
00043     public $table;
00044 
00048     public $showrank;
00049 
00053     var $showtotalsifcontainhidden;
00054 
00061     public function __construct($userid, $gpr, $context) {
00062         global $CFG, $COURSE, $DB;
00063         parent::__construct($COURSE->id, $gpr, $context);
00064 
00065         $this->showrank = grade_get_setting($this->courseid, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
00066         $this->showtotalsifcontainhidden = grade_get_setting($this->courseid, 'report_overview_showtotalsifcontainhidden', $CFG->grade_report_overview_showtotalsifcontainhidden);
00067 
00068         // get the user (for full name)
00069         $this->user = $DB->get_record('user', array('id' => $userid));
00070 
00071         // base url for sorting by first/last name
00072         $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
00073         $this->pbarurl = $this->baseurl;
00074 
00075         $this->setup_table();
00076     }
00077 
00081     public function setup_table() {
00082         /*
00083          * Table has 3 columns
00084          *| course  | final grade | rank (optional) |
00085          */
00086 
00087         // setting up table headers
00088         if ($this->showrank) {
00089             $tablecolumns = array('coursename', 'grade', 'rank');
00090             $tableheaders = array($this->get_lang_string('coursename', 'grades'),
00091                                   $this->get_lang_string('grade'),
00092                                   $this->get_lang_string('rank', 'grades'));
00093         } else {
00094             $tablecolumns = array('coursename', 'grade');
00095             $tableheaders = array($this->get_lang_string('coursename', 'grades'),
00096                                   $this->get_lang_string('grade'));
00097         }
00098         $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
00099 
00100         $this->table->define_columns($tablecolumns);
00101         $this->table->define_headers($tableheaders);
00102         $this->table->define_baseurl($this->baseurl);
00103 
00104         $this->table->set_attribute('cellspacing', '0');
00105         $this->table->set_attribute('id', 'overview-grade');
00106         $this->table->set_attribute('class', 'boxaligncenter generaltable');
00107 
00108         $this->table->setup();
00109     }
00110 
00111     public function fill_table() {
00112         global $CFG, $DB, $OUTPUT;
00113 
00114         // MDL-11679, only show user's courses instead of all courses
00115         if ($courses = enrol_get_users_courses($this->user->id, false, 'id, shortname, showgrades')) {
00116             $numusers = $this->get_numusers(false);
00117 
00118             foreach ($courses as $course) {
00119                 if (!$course->showgrades) {
00120                     continue;
00121                 }
00122 
00123                 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00124 
00125                 if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
00126                     // The course is hidden and the user isn't allowed to see it
00127                     continue;
00128                 }
00129 
00130                 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
00131                 $courselink = html_writer::link(new moodle_url('/grade/report/user/index.php', array('id' => $course->id, 'userid' => $this->user->id)), $courseshortname);
00132                 $canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext);
00133 
00134                 // Get course grade_item
00135                 $course_item = grade_item::fetch_course_item($course->id);
00136 
00137                 // Get the stored grade
00138                 $course_grade = new grade_grade(array('itemid'=>$course_item->id, 'userid'=>$this->user->id));
00139                 $course_grade->grade_item =& $course_item;
00140                 $finalgrade = $course_grade->finalgrade;
00141 
00142                 if (!$canviewhidden and !is_null($finalgrade)) {
00143                     if ($course_grade->is_hidden()) {
00144                         $finalgrade = null;
00145                     } else {
00146                         $finalgrade = $this->blank_hidden_total($course->id, $course_item, $finalgrade);
00147                     }
00148                 }
00149 
00150                 $data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true));
00151 
00152                 if (!$this->showrank) {
00153                     //nothing to do
00154 
00155                 } else if (!is_null($finalgrade)) {
00158                     $params = array($finalgrade, $course_item->id);
00159                     $sql = "SELECT COUNT(DISTINCT(userid))
00160                               FROM {grade_grades}
00161                              WHERE finalgrade IS NOT NULL AND finalgrade > ?
00162                                    AND itemid = ?";
00163                     $rank = $DB->count_records_sql($sql, $params) + 1;
00164 
00165                     $data[] = "$rank/$numusers";
00166 
00167                 } else {
00168                     // no grade, no rank
00169                     $data[] = '-';
00170                 }
00171 
00172                 $this->table->add_data($data);
00173             }
00174             return true;
00175 
00176         } else {
00177             echo $OUTPUT->notification(get_string('nocourses', 'grades'));
00178             return false;
00179         }
00180     }
00181 
00187     public function print_table($return=false) {
00188         ob_start();
00189         $this->table->print_html();
00190         $html = ob_get_clean();
00191         if ($return) {
00192             return $html;
00193         } else {
00194             echo $html;
00195         }
00196     }
00197 
00203     function process_data($data) {
00204     }
00205     function process_action($target, $action) {
00206     }
00207 }
00208 
00209 function grade_report_overview_settings_definition(&$mform) {
00210     global $CFG;
00211 
00212     //show rank
00213     $options = array(-1 => get_string('default', 'grades'),
00214                       0 => get_string('hide'),
00215                       1 => get_string('show'));
00216 
00217     if (empty($CFG->grade_overviewreport_showrank)) {
00218         $options[-1] = get_string('defaultprev', 'grades', $options[0]);
00219     } else {
00220         $options[-1] = get_string('defaultprev', 'grades', $options[1]);
00221     }
00222 
00223     $mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
00224     $mform->addHelpButton('report_overview_showrank', 'showrank', 'grades');
00225 
00226     //showtotalsifcontainhidden
00227     $options = array(-1 => get_string('default', 'grades'),
00228                       GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'),
00229                       GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'),
00230                       GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades') );
00231 
00232     if (empty($CFG->grade_report_overview_showtotalsifcontainhidden)) {
00233         $options[-1] = get_string('defaultprev', 'grades', $options[0]);
00234     } else {
00235         $options[-1] = get_string('defaultprev', 'grades', $options[1]);
00236     }
00237 
00238     $mform->addElement('select', 'report_overview_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options);
00239     $mform->addHelpButton('report_overview_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades');
00240 }
00241 
00242 
 All Data Structures Namespaces Files Functions Variables Enumerations