From 6c096a496dd2dbf4ccc6c0e8da6273a856ad8afb Mon Sep 17 00:00:00 2001 From: Nicolas Connault Date: Thu, 29 Oct 2009 08:14:40 +0000 Subject: [PATCH] MDL-20418 Entirely removed the duplication of code by refactoring the way the grader report is built. Uses $OUTPUT only now. --- grade/report/grader/index.php | 11 +- grade/report/grader/lib.php | 893 +++++++++++++++++----------------- 2 files changed, 435 insertions(+), 469 deletions(-) diff --git a/grade/report/grader/index.php b/grade/report/grader/index.php index 0ba3f079a1..cafdd39d92 100644 --- a/grade/report/grader/index.php +++ b/grade/report/grader/index.php @@ -170,16 +170,7 @@ if (!empty($studentsperpage)) { echo $OUTPUT->paging_bar(moodle_paging_bar::make($numusers, $report->page, $studentsperpage, $report->pbarurl)); } -$reporthtml = '
'; -$reporthtml .= $report->get_studentnameshtml(); -$reporthtml .= $report->get_headerhtml(); -$reporthtml .= $report->get_iconshtml(); -$reporthtml .= $report->get_studentshtml(); -$reporthtml .= $report->get_rangehtml(); -$reporthtml .= $report->get_avghtml(true); -$reporthtml .= $report->get_avghtml(); -$reporthtml .= $report->get_endhtml(); -$reporthtml .= '
'; +$reporthtml = $report->get_grade_table(); // print submit button if ($USER->gradeediting[$course->id] && ($report->get_pref('showquickfeedback') || $report->get_pref('quickgrading')) && !$report->get_pref('enableajax')) { diff --git a/grade/report/grader/lib.php b/grade/report/grader/lib.php index 0c3f80473c..2573b62db6 100644 --- a/grade/report/grader/lib.php +++ b/grade/report/grader/lib.php @@ -133,7 +133,7 @@ class grade_report_grader extends grade_report { $this->baseurl->params(array('perpage' => $studentsperpage, 'page' => $this->page)); } - $this->pbarurl = new moodle_url('index.php', array('id' => $this->courseid, 'perpage' => $studentsperpage)); + $this->pbarurl = new moodle_url($CFG->wwwroot.'/grade/report/grader/index.php', array('id' => $this->courseid, 'perpage' => $studentsperpage)); $this->setup_groups(); @@ -515,50 +515,148 @@ class grade_report_grader extends grade_report { } /** - * Builds and returns the HTML code for the headers. - * @return string $headerhtml + * Builds and returns the rows that will make up the left part of the grader report + * This consists of student names and icons, links to user reports and id numbers, as well + * as header cells for these columns. It also includes the fillers required for the + * categories displayed on the right side of the report. + * @return array Array of html_table_row objects */ - public function get_headerhtml() { + public function get_left_rows() { global $CFG, $USER, $OUTPUT; - $this->rowcount = 0; + $rows = array(); + + $showuserimage = $this->get_pref('showuserimage'); + $showuseridnumber = $this->get_pref('showuseridnumber'); $fixedstudents = $this->is_fixed_students(); - if (!$fixedstudents) { - $strsortasc = $this->get_lang_string('sortasc', 'grades'); - $strsortdesc = $this->get_lang_string('sortdesc', 'grades'); - $strfirstname = $this->get_lang_string('firstname'); - $strlastname = $this->get_lang_string('lastname'); - $showuseridnumber = $this->get_pref('showuseridnumber'); + $strfeedback = $this->get_lang_string("feedback"); + $strgrade = $this->get_lang_string('grade'); - if ($this->sortitemid === 'lastname') { - if ($this->sortorder == 'ASC') { - $lastarrow = print_arrow('up', $strsortasc, true); - } else { - $lastarrow = print_arrow('down', $strsortdesc, true); - } - } else { - $lastarrow = ''; + $arrows = $this->get_sort_arrows(); + + $colspan = 1; + + if (has_capability('gradereport/user:view', $this->context)) { + $colspan++; + } + + if ($showuseridnumber) { + $colspan++; + } + + $levels = count($this->gtree->levels) - 1; + + for ($i = 0; $i < $levels; $i++) { + $fillercell = new html_table_cell(); + $fillercell->add_classes(array('fixedcolumn', 'cell', 'c0', 'topleft')); + $fillercell->text = ' '; + $fillercell->colspan = $colspan; + $row = html_table_row::make(array($fillercell)); + $rows[] = $row; + } + + $headerrow = new html_table_row(); + $headerrow->add_class('heading'); + + $studentheader = new html_table_cell(); + $studentheader->add_classes(array('header', 'c0')); + $studentheader->scope = 'col'; + $studentheader->header = true; + $studentheader->id = 'studentheader'; + if (has_capability('gradereport/user:view', $this->context)) { + $studentheader->colspan = 2; + } + $studentheader->text = $arrows['studentname']; + + $headerrow->cells[] = $studentheader; + + if ($showuseridnumber) { + $sortidnumberlink = html_link::make(clone($this->baseurl), get_string('idnumber')); + $sortidnumberlink->url->param('sortitemid', 'idnumber'); + + $idnumberheader = new html_table_cell(); + $idnumberheader->add_classes(array('header', 'c0', 'useridnumber')); + $idnumberheader->scope = 'col'; + $idnumberheader->header = true; + $idnumberheader->text = $arrows['idnumber']; + + $headerrow->cells[] = $idnumberheader; + } + + $rows[] = $headerrow; + + $rows = $this->get_left_icons_row($rows, $colspan); + + $rowclasses = array('even', 'odd'); + + foreach ($this->users as $userid => $user) { + $userrow = new html_table_row(); + $userrow->add_classes(array('r'.$this->rowcount++, $rowclasses[$this->rowcount % 2])); + + $usercell = new html_table_cell(); + $usercell->add_classes(array('c0', 'user')); + $usercell->header = true; + $usercell->scope = 'row'; + $usercell->add_action('click', 'yui_set_row'); + + if ($showuserimage) { + $usercell->text = $OUTPUT->container($OUTPUT->user_picture(moodle_user_picture::make($user, $this->courseid)), 'userpic'); } - if ($this->sortitemid === 'firstname') { - if ($this->sortorder == 'ASC') { - $firstarrow = print_arrow('up', $strsortasc, true); - } else { - $firstarrow = print_arrow('down', $strsortdesc, true); - } - } else { - $firstarrow = ''; + $usercell->text .= $OUTPUT->link(html_link::make(new moodle_url($CFG->wwwroot.'/user/view.php', array('id' => $user->id, 'course' => $this->course->id)), fullname($user))); + + $userrow->cells[] = $usercell; + + if (has_capability('gradereport/user:view', $this->context)) { + $userreportcell = new html_table_cell(); + $userreportcell->add_class('userreport'); + $userreportcell->header = true; + $a->user = fullname($user); + $strgradesforuser = get_string('gradesforuser', 'grades', $a); + $userreporticon = new moodle_action_icon(); + $userreporticon->link->url = new moodle_url($CFG->wwwroot.'/grade/report/user/index.php', array('userid' => $user->id, 'id' => $this->course->id)); + $userreporticon->image->add_class('iconsmall'); + $userreporticon->image->src = $OUTPUT->old_icon_url('t/grades'); + $userreporticon->image->alt = $strgradesforuser; + $userreportcell->text = $OUTPUT->action_icon($userreporticon); + $userrow->cells[] = $userreportcell; } + if ($showuseridnumber) { + $idnumbercell = new html_table_cell(); + $idnumbercell->add_classes(array('header', 'c0', 'useridnumber')); + $idnumbercell->header = true; + $idnumbercell->scope = 'row'; + $idnumbercell->add_action('click', 'yui_set_row'); + $userrow->cells[] = $idnumbercell; + } + + $rows[] = $userrow; } - // Prepare Table Headers - $headerhtml = ''; + $rows = $this->get_left_range_row($rows, $colspan); + $rows = $this->get_left_avg_row($rows, $colspan, true); + $rows = $this->get_left_avg_row($rows, $colspan); - $numrows = count($this->gtree->get_levels()); + return $rows; + } + + /** + * Builds and returns the rows that will make up the right part of the grader report + * @return array Array of html_table_row objects + */ + public function get_right_rows() { + global $CFG, $USER, $OUTPUT; + $rows = array(); + $this->rowcount = 0; + $numrows = count($this->gtree->get_levels()); + $numusers = count($this->users); + $gradetabindex = 1; $columnstounset = array(); + $strgrade = $this->get_lang_string('grade'); + $arrows = $this->get_sort_arrows(); foreach ($this->gtree->get_levels() as $key=>$row) { $columncount = 0; @@ -567,56 +665,8 @@ class grade_report_grader extends grade_report { // continue; } - if ($fixedstudents) { - $headerhtml .= ''; - } else { - $headerhtml .= ''; - if ($key == $numrows - 1) { - $colspan = 1; - if (has_capability('gradereport/user:view', $this->context)) { - $colspan++; - } - $colspan='colspan="'.$colspan.'"'; - - $sortfirstname = html_link::make(clone($this->baseurl), $strfirstname); - $sortfirstname->url->param('sortitemid', 'firstname'); - $sortlastname = html_link::make(clone($this->baseurl), $strlastname); - $sortlastname->url->param('sortitemid', 'lastname'); - $headerhtml .= '' . $OUTPUT->link($sortfirstname) - . $firstarrow. '/ ' . $OUTPUT->link($sortlastname) . $lastarrow .''; - if ($showuseridnumber) { - if ('idnumber' == $this->sortitemid) { - if ($this->sortorder == 'ASC') { - $idnumberarrow = print_arrow('up', $strsortasc, true); - } else { - $idnumberarrow = print_arrow('down', $strsortdesc, true); - } - } else { - $idnumberarrow = ''; - } - $sortidnumber = html_link::make(clone($this->baseurl), get_string('idnumber')); - $sortidnumber->url->param('sortitemid', 'idnumber'); - $headerhtml .= '' . $OUTPUT->link($sortidnumber) . $idnumberarrow . ''; - } - } else { - $colspan = 1; - if (has_capability('gradereport/user:view', $this->context)) { - $colspan++; - } - if ($showuseridnumber) { - $colspan++; - } - - $colspan='colspan="'.$colspan.'"'; - - $headerhtml .= ' '; - - if ($showuseridnumber) { - $columncount++; - } - } - } - + $headingrow = new html_table_row(); + $headingrow->add_class('heading_name_row'); foreach ($row as $columnkey => $element) { $sortlink = clone($this->baseurl); @@ -633,34 +683,44 @@ class grade_report_grader extends grade_report { $columnclass = 'c' . $columncount++; if (!empty($element['colspan'])) { - $colspan = 'colspan="'.$element['colspan'].'"'; + $colspan = $element['colspan']; $columnclass = ''; } else { - $colspan = ''; + $colspan = 1; } if (!empty($element['depth'])) { - $catlevel = ' catlevel'.$element['depth']; + $catlevel = 'catlevel'.$element['depth']; } else { $catlevel = ''; } // Element is a filler if ($type == 'filler' or $type == 'fillerfirst' or $type == 'fillerlast') { - $headerhtml .= ' '; + $fillercell = new html_table_cell(); + $fillercell->add_classes(array($columnclass, $type, $catlevel)); + $fillercell->colspan = $colspan; + $fillercell->text = ' '; + $fillercell->header = true; + $fillercell->scope = 'col'; + $headingrow->cells[] = $fillercell; } // Element is a category else if ($type == 'category') { - $headerhtml .= '' - . shorten_text($element['object']->get_name()); - $headerhtml .= $this->get_collapsing_icon($element); + $categorycell = new html_table_cell(); + $categorycell->add_classes(array($columnclass, 'category', $catlevel)); + $categorycell->colspan = $colspan; + $categorycell->text = shorten_text($element['object']->get_name()); + $categorycell->text .= $this->get_collapsing_icon($element); + $categorycell->header = true; + $categorycell->scope = 'col'; // Print icons if ($USER->gradeediting[$this->courseid]) { - $headerhtml .= $this->get_icons($element); + $categorycell->text .= $this->get_icons($element); } - $headerhtml .= ''; + $headingrow->cells[] = $categorycell; } // Element is a grade_item else { @@ -677,39 +737,29 @@ class grade_report_grader extends grade_report { $arrow = $this->get_sort_arrow('move', $sortlink); } - $hidden = ''; + $headerlink = $this->gtree->get_element_header($element, true, $this->get_pref('showactivityicons'), false); + + $itemcell = new html_table_cell(); + $itemcell->add_classes(array($columnclass, $type, $catlevel)); + if ($element['object']->is_hidden()) { - $hidden = ' hidden '; + $itemcell->add_class('hidden'); } - $headerlink = $this->gtree->get_element_header($element, true, $this->get_pref('showactivityicons'), false); - $headerhtml .= '' - . shorten_text($headerlink) . $arrow; - $headerhtml .= ''; + $itemcell->colspan = $colspan; + $itemcell->text = shorten_text($headerlink) . $arrow; + $itemcell->header = true; + $itemcell->scope = 'col'; + $itemcell->onclick = 'set_col(this.cellIndex);'; + + $headingrow->cells[] = $itemcell; } } - - $headerhtml .= ''; + $rows[] = $headingrow; } - return $headerhtml; - } - /** - * Builds and return the HTML rows of the table (grades headed by student). - * @return string HTML - */ - public function get_studentshtml() { - global $CFG, $USER, $DB, $OUTPUT; - - $studentshtml = ''; - $strfeedback = $this->get_lang_string("feedback"); - $strgrade = $this->get_lang_string('grade'); - $gradetabindex = 1; - $numusers = count($this->users); - $showuserimage = $this->get_pref('showuserimage'); - $showuseridnumber = $this->get_pref('showuseridnumber'); - $fixedstudents = $this->is_fixed_students(); + $rows = $this->get_right_icons_row($rows); // Preload scale objects for items with a scaleid $scaleslist = array(); @@ -730,9 +780,7 @@ class grade_report_grader extends grade_report { $scalesarray = $DB->get_records_list('scale', 'id', $scaleslist); } - $rowclasses = array(' even ', ' odd '); - - $rowclasses = array(' even ', ' odd '); + $rowclasses = array('even', 'odd'); foreach ($this->users as $userid => $user) { @@ -747,44 +795,16 @@ class grade_report_grader extends grade_report { } $columncount = 0; - if ($fixedstudents) { - $studentshtml .= ''; - } else { - // Student name and link - $userpic = null; - if ($showuserimage) { - $userpic = $OUTPUT->container($OUTPUT->user_picture(moodle_user_picture::make($user, $this->courseid)), 'userpic'); - } - - $userreportcell = ''; - - if (has_capability('gradereport/user:view', $this->context)) { - $a->user = fullname($user); - $strgradesforuser = get_string('gradesforuser', 'grades', $a); - $userreporticon = new moodle_action_icon(); - $userreporticon->link->url = new moodle_url($CFG->wwwroot.'/grade/report/user/index.php', array('userid' => $user->id, 'id' => $this->course->id)); - $userreporticon->image->add_class('iconsmall'); - $userreporticon->image->src = $OUTPUT->old_icon_url('t/grades'); - $userreporticon->image->alt = $strgradesforuser; - $userreportcell = ''.$OUTPUT->action_icon($userreporticon).''; - } - $userlink = html_link::make(new moodle_url($CFG->wwwroot.'/user/view.php', array('id' => $user->id, 'course' => $this->course->id)), fullname($user)); - $studentshtml .= '' - .''.$userpic - .$OUTPUT->link($userlink).$userreportcell.''; - - if ($showuseridnumber) { - $studentshtml .= ''. - $user->idnumber.''; - } - - } + $itemrow = new html_table_row(); + $itemrow->add_class($rowclasses[$this->rowcount % 2]); foreach ($this->gtree->items as $itemid=>$unused) { $item =& $this->gtree->items[$itemid]; $grade = $this->grades[$userid][$item->id]; + $itemcell = new html_table_cell(); + // Get the decimal points preference for this item $decimalpoints = $item->get_decimals(); @@ -801,11 +821,11 @@ class grade_report_grader extends grade_report { if (!$this->canviewhidden and $grade->is_hidden()) { if (!empty($CFG->grade_hiddenasdate) and $grade->get_datesubmitted() and !$item->is_category_item() and !$item->is_course_item()) { // the problem here is that we do not have the time when grade value was modified, 'timemodified' is general modification date for grade_grades records - $studentshtml .= '' - . $OUTPUT->span(userdate($grade->get_datesubmitted(),get_string('strftimedatetimeshort')), 'datesubmitted') . ''; + $itemcell->text = $OUTPUT->span(userdate($grade->get_datesubmitted(),get_string('strftimedatetimeshort')), 'datesubmitted'); } else { - $studentshtml .= '-'; + $itemcell->text = '-'; } + $itemrow->cells[] = $itemcell; continue; } @@ -813,19 +833,19 @@ class grade_report_grader extends grade_report { $eid = $this->gtree->get_grade_eid($grade); $element = array('eid'=>$eid, 'object'=>$grade, 'type'=>'grade'); - $cellclasses = 'grade cell c'.$columncount++; + $itemcell->add_class('grade'); if ($item->is_category_item()) { - $cellclasses .= ' cat'; + $itemcell->add_class('cat'); } if ($item->is_course_item()) { - $cellclasses .= ' course'; + $itemcell->add_class('course'); } if ($grade->is_overridden()) { - $cellclasses .= ' overridden'; + $itemcell->add_class('overridden'); } if ($grade->is_excluded()) { - // $cellclasses .= ' excluded'; + // $itemcell->add_class('excluded'); } $gradetitle = $OUTPUT->container(fullname($user), 'fullname'); @@ -835,15 +855,15 @@ class grade_report_grader extends grade_report { $gradetitle .= $OUTPUT->container(wordwrap(trim(format_string($grade->feedback, $grade->feedbackformat)), 34, '
'), 'feedback'); } - $studentshtml .= ''; + $itemcell->title = s($gradetitle); if ($grade->is_excluded()) { - $studentshtml .= $OUTPUT->span(get_string('excluded', 'grades'), 'excludedfloater'); + $itemcell->text .= $OUTPUT->span(get_string('excluded', 'grades'), 'excludedfloater'); } // Do not show any icons if no grade (no record in DB to match) if (!$item->needsupdate and $USER->gradeediting[$this->courseid]) { - $studentshtml .= $this->get_icons($element); + $itemcell->text .= $this->get_icons($element); } $hidden = ''; @@ -862,7 +882,7 @@ class grade_report_grader extends grade_report { // or a drop down (for scales) // grades in item of type grade category or course are not directly editable if ($item->needsupdate) { - $studentshtml .= $OUTPUT->span(get_string('error'), "gradingerror$hidden"); + $itemcell->text .= $OUTPUT->span(get_string('error'), "gradingerror$hidden"); } else if ($USER->gradeediting[$this->courseid]) { @@ -888,21 +908,21 @@ class grade_report_grader extends grade_report { } else { $nogradestr = $this->get_lang_string('nooutcome', 'grades'); } - $studentshtml .= ''; $select = html_select::make($scaleopt, 'grade_'.$userid.'_'.$item->id,$gradeval, $nogradestr); $select->nothingvalue = '-1'; $select->tabindex = $tabindices[$item->id]['grade']; - $studentshtml .= $OUTPUT->select($select); + $itemcell->text .= $OUTPUT->select($select); } elseif(!empty($scale)) { $scales = explode(",", $scale->scale); // invalid grade if gradeval < 1 if ($gradeval < 1) { - $studentshtml .= $OUTPUT->span('-', "gradevalue$hidden$gradepass"); + $itemcell->text .= $OUTPUT->span('-', "gradevalue$hidden$gradepass"); } else { $gradeval = $grade->grade_item->bounded_grade($gradeval); //just in case somebody changes scale - $studentshtml .= $OUTPUT->span($scales[$gradeval-1], "gradevalue$hidden$gradepass"); + $itemcell->text .= $OUTPUT->span($scales[$gradeval-1], "gradevalue$hidden$gradepass"); } } else { // no such scale, throw error? @@ -911,12 +931,12 @@ class grade_report_grader extends grade_report { } else if ($item->gradetype != GRADE_TYPE_TEXT) { // Value type if ($this->get_pref('quickgrading') and $grade->is_editable()) { $value = format_float($gradeval, $decimalpoints); - $studentshtml .= ''; - $studentshtml .= ''; + $itemcell->text .= ''; } else { - $studentshtml .= $OUTPUT->span(format_float($gradeval, $decimalpoints), "gradevalue$hidden$gradepass"); + $itemcell->text .= $OUTPUT->span(format_float($gradeval, $decimalpoints), "gradevalue$hidden$gradepass"); } } @@ -924,9 +944,9 @@ class grade_report_grader extends grade_report { // If quickfeedback is on, print an input element if ($this->get_pref('showquickfeedback') and $grade->is_editable()) { - $studentshtml .= ''; - $studentshtml .= ''; } @@ -937,202 +957,140 @@ class grade_report_grader extends grade_report { // If feedback present, surround grade with feedback tooltip: Open span here if ($item->needsupdate) { - $studentshtml .= $OUTPUT->span(get_string('error'), "gradingerror$hidden$gradepass"); + $itemcell->text .= $OUTPUT->span(get_string('error'), "gradingerror$hidden$gradepass"); } else { - $studentshtml .= $OUTPUT->span(grade_format_gradevalue($gradeval, $item, true, $gradedisplaytype, null), "gradevalue$hidden$gradepass"); + $itemcell->text .= $OUTPUT->span(grade_format_gradevalue($gradeval, $item, true, $gradedisplaytype, null), "gradevalue$hidden$gradepass"); } } if (!empty($this->gradeserror[$item->id][$userid])) { - $studentshtml .= $this->gradeserror[$item->id][$userid]; + $itemcell->text .= $this->gradeserror[$item->id][$userid]; } - $studentshtml .= '' . "\n"; + $itemrow->cells[] = $itemcell; } - $studentshtml .= ''; + $rows[] = $itemrow; } - return $studentshtml; - } - function get_studentnameshtml() { - global $CFG, $USER, $OUTPUT; - $studentshtml = ''; + $rows = $this->get_right_range_row($rows); + $rows = $this->get_right_avg_row($rows, true); + $rows = $this->get_right_avg_row($rows); - $showuserimage = $this->get_pref('showuserimage'); - $showuseridnumber = $this->get_pref('showuseridnumber'); - $fixedstudents = $this->is_fixed_students(); + return $rows; + } - $strsortasc = $this->get_lang_string('sortasc', 'grades'); - $strsortdesc = $this->get_lang_string('sortdesc', 'grades'); - $strfirstname = $this->get_lang_string('firstname'); - $strlastname = $this->get_lang_string('lastname'); + /** + * Depending on the style of report (fixedstudents vs traditional one-table), + * arranges the rows of data in one or two tables, and returns the output of + * these tables in HTML + * @return string HTML + */ + public function get_grade_table() { + global $OUTPUT; + $fixedstudents = $this->is_fixed_students(); - if ($this->sortitemid === 'lastname') { - if ($this->sortorder == 'ASC') { - $lastarrow = print_arrow('up', $strsortasc, true); - } else { - $lastarrow = print_arrow('down', $strsortdesc, true); - } - } else { - $lastarrow = ''; - } + $leftrows = $this->get_left_rows(); + $rightrows = $this->get_right_rows(); - if ($this->sortitemid === 'firstname') { - if ($this->sortorder == 'ASC') { - $firstarrow = print_arrow('up', $strsortasc, true); - } else { - $firstarrow = print_arrow('down', $strsortdesc, true); - } - } else { - $firstarrow = ''; - } + $html = ''; if ($fixedstudents) { $fixedcolumntable = new html_table(); $fixedcolumntable->id = 'fixed_column'; $fixedcolumntable->add_class('fixed_grades_column'); $fixedcolumntable->bodyclasses = 'leftbody'; + $fixedcolumntable->data = $leftrows; + $html .= $OUTPUT->container($OUTPUT->table($fixedcolumntable), 'left_scroller'); - $colspan = 1; - - if (has_capability('gradereport/user:view', $this->context)) { - $colspan++; - } - - if ($showuseridnumber) { - $colspan++; - } - - $levels = count($this->gtree->levels) - 1; - - for ($i = 0; $i < $levels; $i++) { - $fillercell = new html_table_cell(); - $fillercell->add_classes(array('fixedcolumn', 'cell', 'c0', 'topleft')); - $fillercell->text = ' '; - $fillercell->colspan = $colspan; - $fixedcolumntable->data[] = html_table_row::make(array($fillercell)); - } - - $sortfirstname = html_link::make(clone($this->baseurl), $strfirstname); - $sortfirstname->url->param('sortitemid', 'firstname'); - $sortlastname = html_link::make(clone($this->baseurl), $strlastname); - $sortlastname->url->param('sortitemid', 'lastname'); - - $headerrow = new html_table_row(); - $headerrow->add_class('heading'); + $righttable = new html_table(); + $righttable->id = 'user-grades'; + $righttable->bodyclasses = array('righttest'); + $righttable->data = $rightrows; - $studentheader = new html_table_cell(); - $studentheader->add_classes(array('header', 'c0')); - $studentheader->scope = 'col'; - $studentheader->header = true; - $studentheader->id = 'studentheader'; - if (has_capability('gradereport/user:view', $this->context)) { - $studentheader->colspan = 2; - } - $studentheader->text = $OUTPUT->link($sortfirstname) . $firstarrow. '/ ' . $OUTPUT->link($sortlastname) . $lastarrow; - - $headerrow->cells[] = $studentheader; - - if ($showuseridnumber) { - $sortidnumberlink = html_link::make(clone($this->baseurl), get_string('idnumber')); - $sortidnumberlink->url->param('sortitemid', 'idnumber'); - - $idnumberheader = new html_table_cell(); - $idnumberheader->add_classes(array('header', 'c0', 'useridnumber')); - $idnumberheader->scope = 'col'; - $idnumberheader->header = true; - $idnumberheader->text = $OUTPUT->link($sortidnumberlink); - - if ('idnumber' == $this->sortitemid) { - if ($this->sortorder == 'ASC') { - $idnumberheader->text .= print_arrow('up', $strsortasc, true); - } else { - $idnumberheader->text .= print_arrow('down', $strsortdesc, true); - } - } - $headerrow->cells[] = $idnumberheader; - } - - $fixedcolumntable->data[] = $headerrow; - - if ($USER->gradeediting[$this->courseid]) { - $controlsrow = new html_table_row(); - $controlsrow->add_class('controls'); - $controlscell = new html_table_cell(); - $controlscell->add_classes(array('header', 'c0', 'controls')); - $controlscell->colspan = $colspan; - $controlscell->text = $this->get_lang_string('controls','grades'); - - $controlsrow->cells[] = $controlscell; - $fixedcolumntable->data[] = $controlsrow; + $html .= $OUTPUT->container($OUTPUT->table($righttable), 'right_scroller'); + } else { + $fulltable = new html_table(); + $fulltable->add_classes(array('gradestable', 'flexible', 'boxaligncenter', 'generaltable')); + $fulltable->id = 'user-grades'; + + // Extract rows from each side (left and right) and collate them into one row each + foreach ($leftrows as $key => $row) { + $row->cells = array_merge($row->cells, $rightrows[$key]->cells); + $fulltable->data[] = $row; } + $html .= $OUTPUT->table($fulltable); + } + return $OUTPUT->container($html, 'gradeparent'); + } - $rowclasses = array('even', 'odd'); - - foreach ($this->users as $userid => $user) { - $userrow = new html_table_row(); - $userrow->add_classes(array('r'.$this->rowcount++, $rowclasses[$this->rowcount % 2])); - - $usercell = new html_table_cell(); - $usercell->add_classes(array('c0', 'user')); - $usercell->header = true; - $usercell->scope = 'row'; - $usercell->add_action('click', 'yui_set_row'); + /** + * Builds and return the row of icons for the left side of the report. + * It only has one cell that says "Controls" + * @param array $rows The Array of rows for the left part of the report + * @param int $colspan The number of columns this cell has to span + * @return array Array of rows for the left part of the report + */ + public function get_left_icons_row($rows=array(), $colspan=1) { + global $USER; - if ($showuserimage) { - $usercell->text = $OUTPUT->container($OUTPUT->user_picture(moodle_user_picture::make($user, $this->courseid)), 'userpic'); - } + if ($USER->gradeediting[$this->courseid]) { + $controlsrow = new html_table_row(); + $controlsrow->add_class('controls'); + $controlscell = new html_table_cell(); + $controlscell->add_classes(array('header', 'c0', 'controls')); + $controlscell->colspan = $colspan; + $controlscell->text = $this->get_lang_string('controls','grades'); + + $controlsrow->cells[] = $controlscell; + $rows[] = $controlsrow; + } + return $rows; + } - $usercell->text .= $OUTPUT->link(html_link::make(new moodle_url($CFG->wwwroot.'/user/view.php', array('id' => $user->id, 'course' => $this->course->id)), fullname($user))); - - $userrow->cells[] = $usercell; - - if (has_capability('gradereport/user:view', $this->context)) { - $userreportcell = new html_table_cell(); - $userreportcell->add_class('userreport'); - $userreportcell->header = true; - $a->user = fullname($user); - $strgradesforuser = get_string('gradesforuser', 'grades', $a); - $userreporticon = new moodle_action_icon(); - $userreporticon->link->url = new moodle_url($CFG->wwwroot.'/grade/report/user/index.php', array('userid' => $user->id, 'id' => $this->course->id)); - $userreporticon->image->add_class('iconsmall'); - $userreporticon->image->src = $OUTPUT->old_icon_url('t/grades'); - $userreporticon->image->alt = $strgradesforuser; - $userreportcell->text = $OUTPUT->action_icon($userreporticon); - $userrow->cells[] = $userreportcell; - } + /** + * Builds and return the header for the row of ranges, for the left part of the grader report. + * @param array $rows The Array of rows for the left part of the report + * @param int $colspan The number of columns this cell has to span + * @return array Array of rows for the left part of the report + */ + public function get_left_range_row($rows=array(), $colspan=1) { + global $CFG, $USER; - if ($showuseridnumber) { - $idnumbercell = new html_table_cell(); - $idnumbercell->add_classes(array('header', 'c0', 'useridnumber')); - $idnumbercell->header = true; - $idnumbercell->scope = 'row'; - $idnumbercell->add_action('click', 'yui_set_row'); - $userrow->cells[] = $idnumbercell; - } - $fixedcolumntable->data[] = $userrow; - } + if ($this->get_pref('showranges')) { + $rangerow = new html_table_row(); + $rangerow->add_classes(array('range', 'r'.$this->rowcount++)); + $rangecell = new html_table_cell(); + $rangecell->add_classes(array('header', 'c0', 'range')); + $rangecell->colspan = $colspan; + $rangecell->header = true; + $rangecell->scope = 'row'; + $rangecell->text = $this->get_lang_string('range','grades'); + $rangerow->cells[] = $rangecell; + $rows[] = $rangerow; + } - if ($this->get_pref('showranges')) { - $rangerow = new html_table_row(); - $rangerow->add_classes(array('range', 'r'.$this->rowcount++)); - $rangecell = new html_table_cell(); - $rangecell->add_classes(array('header', 'c0', 'range')); - $rangecell->colspan = $colspan; - $rangecell->header = true; - $rangecell->scope = 'row'; - $rangecell->text = $this->get_lang_string('range','grades'); - $rangerow->cells[] = $rangecell; - $fixedcolumntable->data[] = $rangerow; - } + return $rows; + } - // Averages heading + /** + * Builds and return the headers for the rows of averages, for the left part of the grader report. + * @param array $rows The Array of rows for the left part of the report + * @param int $colspan The number of columns this cell has to span + * @param bool $groupavg If true, returns the row for group averages, otherwise for overall averages + * @return array Array of rows for the left part of the report + */ + public function get_left_avg_row($rows=array(), $colspan=1, $groupavg=false) { + if (!$this->canviewhidden) { + // totals might be affected by hiding, if user can not see hidden grades the aggregations might be altered + // better not show them at all if user can not see all hideen grades + return $rows; + } - $straveragegroup = get_string('groupavg', 'grades'); - $straverage = get_string('overallaverage', 'grades'); - $showaverages = $this->get_pref('showaverages'); - $showaveragesgroup = $this->currentgroup && $showaverages; + $showaverages = $this->get_pref('showaverages'); + $showaveragesgroup = $this->currentgroup && $showaverages; + $straveragegroup = get_string('groupavg', 'grades'); + if ($groupavg) { if ($showaveragesgroup) { $groupavgrow = new html_table_row(); $groupavgrow->add_classes(array('groupavg', 'r'.$this->rowcount++)); @@ -1143,8 +1101,10 @@ class grade_report_grader extends grade_report { $groupavgcell->scope = 'row'; $groupavgcell->text = $straveragegroup; $groupavgrow->cells[] = $groupavgcell; - $fixedcolumntable->data[] = $groupavgrow; + $rows[] = $groupavgrow; } + } else { + $straverage = get_string('overallaverage', 'grades'); if ($showaverages) { $avgrow = new html_table_row(); @@ -1156,52 +1116,96 @@ class grade_report_grader extends grade_report { $avgcell->scope = 'row'; $avgcell->text = $straverage; $avgrow->cells[] = $avgcell; - $fixedcolumntable->data[] = $avgrow; + $rows[] = $avgrow; } + } - $studentshtml .= $OUTPUT->container($OUTPUT->table($fixedcolumntable), 'left_scroller'); - $studentshtml .= ' -
- - '; + return $rows; + } - } else { - $studentshtml .= '
- '; - } + /** + * Builds and return the row of icons when editing is on, for the right part of the grader report. + * @param array $rows The Array of rows for the right part of the report + * @return array Array of rows for the right part of the report + */ + public function get_right_icons_row($rows=array()) { + global $USER; + if ($USER->gradeediting[$this->courseid]) { + $iconsrow = new html_table_row(); + $iconsrow->add_class('controls'); + + $showuseridnumber = $this->get_pref('showuseridnumber'); - return $studentshtml; + $columncount = 0; + foreach ($this->gtree->items as $itemid=>$unused) { + // emulate grade element + $item =& $this->gtree->get_item($itemid); + + $eid = $this->gtree->get_item_eid($item); + $element = $this->gtree->locate_element($eid); + $itemcell = new html_table_cell(); + $itemcell->add_classes(array('controls', 'icons')); + $itemcell->text = $this->get_icons($element); + $iconsrow->cells[] = $itemcell; + } + $rows[] = $iconsrow; + } + return $rows; } /** - * Closes all open elements + * Builds and return the row of ranges for the right part of the grader report. + * @param array $rows The Array of rows for the right part of the report + * @return array Array of rows for the right part of the report */ - public function get_endhtml() { - global $CFG, $USER; + public function get_right_range_row($rows=array()) { + global $OUTPUT; - $fixedstudents = $this->is_fixed_students(); + if ($this->get_pref('showranges')) { + $rangesdisplaytype = $this->get_pref('rangesdisplaytype'); + $rangesdecimalpoints = $this->get_pref('rangesdecimalpoints'); + $rangerow = new html_table_row(); + $rangerow->add_classes(array('heading', 'range')); - if ($fixedstudents) { - return "
"; - } else { - return ""; + foreach ($this->gtree->items as $itemid=>$unused) { + $item =& $this->gtree->items[$itemid]; + $itemcell = new html_table_cell(); + $itemcell->header = true; + $itemcell->add_classes(array('header', 'range')); + + $hidden = ''; + if ($item->is_hidden()) { + $hidden = ' hidden '; + } + + $formattedrange = $item->get_formatted_range($rangesdisplaytype, $rangesdecimalpoints); + + $itemcell->text = $OUTPUT->container($formattedrange, 'rangevalues'.$hidden); + $rangerow->cells[] = $itemcell; + } + $rows[] = $rangerow; } + return $rows; } /** - * Builds and return the HTML row of column totals. - * @param bool $grouponly Whether to return only group averages or all averages. - * @return string HTML + * Builds and return the row of averages for the right part of the grader report. + * @param array $rows Whether to return only group averages or all averages. + * @param bool $grouponly Whether to return only group averages or all averages. + * @return array Array of rows for the right part of the report */ - public function get_avghtml($grouponly=false) { - global $CFG, $USER, $DB; + public function get_right_avg_row($rows=array(), $grouponly=false) { + global $CFG, $USER, $DB, $OUTPUT; if (!$this->canviewhidden) { // totals might be affected by hiding, if user can not see hidden grades the aggregations might be altered // better not show them at all if user can not see all hideen grades - return; + return $rows; } + $showaverages = $this->get_pref('showaverages'); + $showaveragesgroup = $this->currentgroup && $showaverages; + $averagesdisplaytype = $this->get_pref('averagesdisplaytype'); $averagesdecimalpoints = $this->get_pref('averagesdecimalpoints'); $meanselection = $this->get_pref('meanselection'); @@ -1256,9 +1260,6 @@ class grade_report_grader extends grade_report { } } - $columncount=0; - $avghtml = ''; - // 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('courseid'=>$this->courseid), $rolesparams, $groupwheresqlparams); @@ -1277,20 +1278,16 @@ class grade_report_grader extends grade_report { $ungradedcounts = $DB->get_records_sql($SQL, $params); - $fixedstudents = $this->is_fixed_students(); - if (!$fixedstudents) { - $colspan=''; - if ($this->get_pref('showuseridnumber')) { - $colspan = 'colspan="2" '; - } - $avghtml .= ''.$straverage.''; - } + $avgrow = new html_table_row(); + $avgrow->add_class('avg'); foreach ($this->gtree->items as $itemid=>$unused) { $item =& $this->gtree->items[$itemid]; if ($item->needsupdate) { - $avghtml .= ''.get_string('error').''; + $avgcell = new html_table_cell(); + $avgcell->text = $OUTPUT->container(get_string('error'), 'gradingerror'); + $avgrow->cells[] = $avgcell; continue; } @@ -1333,7 +1330,10 @@ class grade_report_grader extends grade_report { } if (!isset($sumarray[$item->id]) || $meancount == 0) { - $avghtml .= '-'; + $avgcell = new html_table_cell(); + $avgcell->text = '-'; + $avgrow->cells[] = $avgcell; + } else { $sum = $sumarray[$item->id]; $avgradeval = $sum/$meancount; @@ -1344,94 +1344,14 @@ class grade_report_grader extends grade_report { $numberofgrades = " ($meancount)"; } - $avghtml .= ''.$gradehtml.$numberofgrades.''; - } - } - $avghtml .= ''; - } - return $avghtml; - } - - /** - * Builds and return the HTML row of ranges for each column (i.e. range). - * @return string HTML - */ - public function get_rangehtml() { - global $CFG, $USER; - - $rangehtml = ''; - if ($this->get_pref('showranges')) { - $rangesdisplaytype = $this->get_pref('rangesdisplaytype'); - $rangesdecimalpoints = $this->get_pref('rangesdecimalpoints'); - - $columncount=0; - $rangehtml = ''; - - $fixedstudents = $this->is_fixed_students(); - if (!$fixedstudents) { - $colspan=''; - if ($this->get_pref('showuseridnumber')) { - $colspan = 'colspan="2" '; - } - $rangehtml .= ''.$this->get_lang_string('range','grades').''; - } - - foreach ($this->gtree->items as $itemid=>$unused) { - $item =& $this->gtree->items[$itemid]; - - - $hidden = ''; - if ($item->is_hidden()) { - $hidden = ' hidden '; + $avgcell = new html_table_cell(); + $avgcell->text = $gradehtml.$numberofgrades; + $avgrow->cells[] = $avgcell; } - - $formattedrange = $item->get_formatted_range($rangesdisplaytype, $rangesdecimalpoints); - - $rangehtml .= ''. $formattedrange .''; - - } - $rangehtml .= ''; - } - return $rangehtml; - } - - /** - * Builds and return the HTML row of ranges for each column (i.e. range). - * @return string HTML - */ - public function get_iconshtml() { - global $USER, $CFG; - - $iconshtml = ''; - if ($USER->gradeediting[$this->courseid]) { - - $iconshtml = ''; - - $fixedstudents = $this->is_fixed_students(); - $showuseridnumber = $this->get_pref('showuseridnumber'); - - $colspan = ''; - if ($showuseridnumber) { - $colspan = 'colspan="2"'; - } - - if (!$fixedstudents) { - $iconshtml .= ''.$this->get_lang_string('controls','grades').''; - } - - $columncount = 0; - foreach ($this->gtree->items as $itemid=>$unused) { - // emulate grade element - $item =& $this->gtree->get_item($itemid); - - $eid = $this->gtree->get_item_eid($item); - $element = $this->gtree->locate_element($eid); - - $iconshtml .= '' . $this->get_icons($element) . ''; } - $iconshtml .= ''; + $rows[] = $avgrow; } - return $iconshtml; + return $rows; } /** @@ -1581,5 +1501,60 @@ class grade_report_grader extends grade_report { check_browser_version('Opera', '6.0') || check_browser_version('Safari', '2.0')); } + + /** + * Refactored function for generating HTML of sorting links with matching arrows. + * Returns an array with 'studentname' and 'idnumber' as keys, with HTML ready + * to inject into a table header cell. + * @return array An associative array of HTML sorting links+arrows + */ + public function get_sort_arrows() { + global $OUTPUT; + $arrows = array(); + + $strsortasc = $this->get_lang_string('sortasc', 'grades'); + $strsortdesc = $this->get_lang_string('sortdesc', 'grades'); + $strfirstname = $this->get_lang_string('firstname'); + $strlastname = $this->get_lang_string('lastname'); + + $firstlink = html_link::make(clone($this->baseurl), $strfirstname); + $firstlink->url->param('sortitemid', 'firstname'); + $lastlink = html_link::make(clone($this->baseurl), $strlastname); + $lastlink->url->param('sortitemid', 'lastname'); + $idnumberlink = html_link::make(clone($this->baseurl), get_string('idnumber')); + $idnumberlink->url->param('sortitemid', 'idnumber'); + + $arrows['studentname'] = $OUTPUT->link($lastlink); + + if ($this->sortitemid === 'lastname') { + if ($this->sortorder == 'ASC') { + $arrows['studentname'] .= print_arrow('up', $strsortasc, true); + } else { + $arrows['studentname'] .= print_arrow('down', $strsortdesc, true); + } + } + + $arrows['studentname'] .= ' ' . $OUTPUT->link($firstlink); + + if ($this->sortitemid === 'firstname') { + if ($this->sortorder == 'ASC') { + $arrows['studentname'] .= print_arrow('up', $strsortasc, true); + } else { + $arrows['studentname'] .= print_arrow('down', $strsortdesc, true); + } + } + + $arrows['idnumber'] = $OUTPUT->link($idnumberlink); + + if ('idnumber' == $this->sortitemid) { + if ($this->sortorder == 'ASC') { + $arrows['idnumber'] .= print_arrow('up', $strsortasc, true); + } else { + $arrows['idnumber'] .= print_arrow('down', $strsortdesc, true); + } + } + + return $arrows; + } } ?> -- 2.39.5