]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19797 Optimising output code for the Edit grades categories and items interface
authornicolasconnault <nicolasconnault>
Tue, 25 Aug 2009 07:31:13 +0000 (07:31 +0000)
committernicolasconnault <nicolasconnault>
Tue, 25 Aug 2009 07:31:13 +0000 (07:31 +0000)
grade/edit/tree/functions.js
grade/edit/tree/index.php
grade/edit/tree/lib.php
lib/outputcomponents.php
lib/outputrenderers.php

index 64f30ae66768417fe0dbcbe8f2abaf990aa74d85..92fb7945fbaf5e84bc241270184d49ceeb185726 100755 (executable)
@@ -1,14 +1,14 @@
 /**
  * Toggles the selection checkboxes of all grade items children of the given eid (a category id)
  */
-function togglecheckboxes(eid, value) {
-    var rows = YAHOO.util.Dom.getElementsByClassName(eid);
+function togglecheckboxes(event, args) {
+    var rows = YAHOO.util.Dom.getElementsByClassName(args.eid);
 
     for (var i = 0; i < rows.length; i++) {
         var element = new YAHOO.util.Element(rows[i]);
         var checkboxes = element.getElementsByClassName('itemselect');
         if (checkboxes[0]) {
-            checkboxes[0].checked=value;
+            checkboxes[0].checked=args.check;
         }
     }
 
index 2d1400af2d6b9ec42d924bf336fbb4bbec86d819..62121da7fc18d0d05f93a284d65482ccaf13f9ef 100644 (file)
@@ -133,8 +133,16 @@ $strcategoriesanditems = get_string('categoriesanditems', 'grades');
 
 $navigation = grade_build_nav(__FILE__, $strcategoriesanditems, array('courseid' => $courseid));
 $moving = false;
+$movingeid = false;
 
-$grade_edit_tree = new grade_edit_tree($gtree, $moving, $gpr);
+if ($action == 'moveselect') {
+    if ($eid and confirm_sesskey()) {
+        $movingeid = $eid;
+        $moving=true;
+    }
+}
+
+$grade_edit_tree = new grade_edit_tree($gtree, $movingeid, $gpr);
 
 switch ($action) {
     case 'delete':
@@ -193,13 +201,6 @@ switch ($action) {
         }
         break;
 
-    case 'moveselect':
-        if ($eid and confirm_sesskey()) {
-            $grade_edit_tree->moving = $eid;
-            $moving=true;
-        }
-        break;
-
     default:
         break;
 }
@@ -304,11 +305,7 @@ echo '<form id="gradetreeform" method="post" action="'.$returnurl.'">';
 echo '<div>';
 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
 
-// Build up an array of categories for move drop-down (by reference)
-$categories = array();
-$level      = 0;
-$row_count  = 0;
-echo $grade_edit_tree->build_html_tree($gtree->top_element, true, array(), $categories, $level, $row_count);
+echo $OUTPUT->table($grade_edit_tree->table);
 
 echo '<div id="gradetreesubmit">';
 if (!$moving) {
@@ -316,12 +313,12 @@ if (!$moving) {
 }
 
 // We don't print a bulk move menu if there are no other categories than course category
-if (!$moving && count($categories) > 1) {
+if (!$moving && count($grade_edit_tree->categories) > 1) {
     echo '<br /><br />';
     echo '<input type="hidden" name="bulkmove" value="0" id="bulkmoveinput" />';
     echo get_string('moveselectedto', 'grades') . ' ';
     $select = new html_select();
-    $select->options = $categories;
+    $select->options = $grade_edit_tree->categories;
     $select->name = 'moveafter';
     $select->disabled = true;
     $select->id = 'menumoveafter';
index ea4b1068cc47d043153e6fb3bd217b6bf97d32d6..4ec3a789a0b504f436c63c81a7ba1949e6db2456 100755 (executable)
@@ -38,11 +38,16 @@ class grade_edit_tree {
     public $uses_extra_credit = false;
 
     public $uses_weight = false;
-
+    
+    public $table;
+    
+    public $categories = array();
     /**
      * Constructor
      */
     public function __construct($gtree, $moving=false, $gpr) {
+        global $USER, $OUTPUT, $COURSE;
+
         $this->gtree = $gtree;
         $this->moving = $moving;
         $this->gpr = $gpr;
@@ -68,6 +73,28 @@ class grade_edit_tree {
         $this->columns[] = grade_edit_tree_column::factory('plusfactor', array('adv' => true));
         $this->columns[] = grade_edit_tree_column::factory('actions');
         $this->columns[] = grade_edit_tree_column::factory('select');
+
+        $mode = ($USER->gradeediting[$COURSE->id]) ? 'advanced' : 'simple';
+        
+        $widthstyle = '';
+        if ($mode == 'simple') {
+            $widthstyle = ' style="width:auto;" ';
+        }
+
+        $this->table = new html_table();
+        $this->table->id = "grade_edit_tree_table";
+        $this->table->cellpadding = 5;
+        $this->table->add_class('generaltable');
+        $this->table->style = $widthstyle;
+
+        foreach ($this->columns as $column) {
+            if (!($this->moving && $column->hide_when_moving) && !$column->is_hidden($mode)) {
+                $this->table->head[] = $column->get_header_cell();
+            }
+        }
+
+        $rowcount = 0;
+        $this->table->data = $this->build_html_tree($this->gtree->top_element, true, array(), 0, $rowcount);
     }
 
     /**
@@ -80,9 +107,9 @@ class grade_edit_tree {
      *
      * @return string HTML
      */
-    public function build_html_tree($element, $totals, $parents, &$categories, $level, &$row_count) {
+    public function build_html_tree($element, $totals, $parents, $level, &$row_count) {
         global $CFG, $COURSE, $USER, $OUTPUT;
-
+        
         $object = $element['object'];
         $eid    = $element['eid'];
         $object->name = $this->gtree->get_element_header($element, true, true, false);
@@ -93,9 +120,9 @@ class grade_edit_tree {
             $is_category_item = true;
         }
 
-        $rowclasses = '';
+        $rowclasses = array();
         foreach ($parents as $parent_eid) {
-            $rowclasses .= " $parent_eid ";
+            $rowclasses[] = $parent_eid;
         }
 
         $actions = '';
@@ -131,10 +158,9 @@ class grade_edit_tree {
 
         $mode = ($USER->gradeediting[$COURSE->id]) ? 'advanced' : 'simple';
 
-        $html = '';
+        $returnrows = array();
         $root = false;
 
-
         $id = required_param('id', PARAM_INT);
 
         /// prepare move target if needed
@@ -142,31 +168,33 @@ class grade_edit_tree {
 
         /// print the list items now
         if ($this->moving == $eid) {
-
             // do not diplay children
-            return '<tr><td colspan="12" class="'.$element['type'].' moving">'.$object->name.' ('.get_string('move').')</td></tr>';
-
+            $cell = new html_table_cell();
+            $cell->colspan = 12;
+            $cell->add_classes(array($element['type'], 'moving'));
+            $cell->text = $object->name.' ('.get_string('move').')';
+            return array(html_table_row::make(array($cell)));
         }
 
         if ($element['type'] == 'category') {
             $level++;
-            $categories[$object->id] = $object->stripped_name;
+            $this->categories[$object->id] = $object->stripped_name;
             $category = grade_category::fetch(array('id' => $object->id));
             $item = $category->get_grade_item();
 
             // Add aggregation coef input if not a course item and if parent category has correct aggregation type
-            $dimmed = ($item->is_hidden()) ? " dimmed " : "";
+            $dimmed = ($item->is_hidden()) ? 'dimmed' : '';
 
             // Before we print the category's row, we must find out how many rows will appear below it (for the filler cell's rowspan)
             $aggregation_position = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
             $category_total_data = null; // Used if aggregationposition is set to "last", so we can print it last
 
-            $html_children = '';
+            $html_children = array();
 
             $row_count = 0;
 
             foreach($element['children'] as $child_el) {
-                $moveto = '';
+                $moveto = null;
 
                 if (empty($child_el['object']->itemtype)) {
                     $child_el['object']->itemtype = false;
@@ -176,7 +204,7 @@ class grade_edit_tree {
                     continue;
                 }
 
-                $child_eid    = $child_el['eid'];
+                $child_eid = $child_el['eid'];
                 $first = '';
 
                 if ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
@@ -199,8 +227,12 @@ class grade_edit_tree {
                     $moveicon->image->src = $OUTPUT->old_icon_url('movehere');
                     $moveicon->image->alt = $strmovehere;
                     $moveicon->image->title = $strmovehere;
+                    
+                    $cell = new html_table_cell();
+                    $cell->colspan = 12;
+                    $cell->text = $OUTPUT->action_icon($moveicon);
 
-                    $moveto = '<tr><td colspan="12">'.$OUTPUT->action_icon($moveicon) .'</td></tr>';
+                    $moveto = html_table_row::make(array($cell));
                 }
 
                 $newparents = $parents;
@@ -211,15 +243,18 @@ class grade_edit_tree {
 
                 // If moving, do not print course and category totals, but still print the moveto target box
                 if ($this->moving && ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category')) {
-                    $html_children .= $moveto;
+                    $html_children[] = $moveto;
                 } elseif ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
                     // We don't build the item yet because we first need to know the deepest level of categories (for category/name colspans)
-                    $category_total_item = $this->build_html_tree($child_el, $totals, $newparents, $categories, $level, $child_row_count);
+                    $category_total_item = $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count);
                     if (!$aggregation_position) {
-                        $html_children .= $category_total_item;
+                        $html_children = array_merge($html_children, $category_total_item);
                     }
                 } else {
-                    $html_children .= $this->build_html_tree($child_el, $totals, $newparents, $categories, $level, $child_row_count) . $moveto;
+                    $html_children = array_merge($html_children, $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count));
+                    if (!empty($moveto)) {
+                        $html_children[] = $moveto;
+                    }
 
                     if ($this->moving) {
                         $row_count++;
@@ -236,28 +271,11 @@ class grade_edit_tree {
 
             // Print category total at the end if aggregation position is "last" (1)
             if (!empty($category_total_item) && $aggregation_position) {
-                $html_children .= $category_total_item;
+                $html_children = array_merge($html_children, $category_total_item);
             }
 
-            // now build the header
+            // Determine if we are at the root
             if (isset($element['object']->grade_item) && $element['object']->grade_item->is_course_item()) {
-                // Reduce width if advanced elements are not shown
-                $width_style = '';
-
-                if ($mode == 'simple') {
-                    $width_style = ' style="width:auto;" ';
-                }
-
-                $html .= '<table id="grade_edit_tree_table" cellpadding="5" class="generaltable" '.$width_style.'>
-                            <tr>';
-
-                foreach ($this->columns as $column) {
-                    if (!($this->moving && $column->hide_when_moving) && !$column->is_hidden($mode)) {
-                        $html .= $column->get_header_cell();
-                    }
-                }
-
-                $html .= '</tr>';
                 $root = true;
             }
 
@@ -267,29 +285,43 @@ class grade_edit_tree {
                 $row_count_offset = -1;
             }
 
-            $levelclass = " level$level ";
+            $levelclass = "level$level";
 
             $courseclass = '';
             if ($level == 1) {
                 $courseclass = 'coursecategory';
             }
+            
+            $row = new html_table_row();
+            $row->add_classes(array($courseclass, 'category', $dimmed));
+            foreach ($rowclasses as $class) {
+                $row->add_class($class);
+            }
 
-            $html .= '
-                    <tr class="'.$courseclass.' category '.$dimmed.$rowclasses.'">
-                      <th scope="row" title="'.s($object->stripped_name).'" class="cell rowspan '.$levelclass.'" rowspan="'.($row_count+1+$row_count_offset).'"></th>';
+            $headercell = new html_table_cell();
+            $headercell->header = true;
+            $headercell->scope = 'row';
+            $headercell->title = $object->stripped_name;
+            $headercell->add_classes(array('cell', 'rowspan', $levelclass));
+            $headercell->rowspan = $row_count+1+$row_count_offset;
+            $row->cells[] = $headercell;
 
             foreach ($this->columns as $column) {
                 if (!($this->moving && $column->hide_when_moving) && !$column->is_hidden($mode)) {
-                    $html .= $column->get_category_cell($category, $levelclass, array('id' => $id, 'name' => $object->name, 'level' => $level, 'actions' => $actions, 'eid' => $eid));
+                    $row->cells[] = $column->get_category_cell($category, $levelclass, array('id' => $id, 'name' => $object->name, 'level' => $level, 'actions' => $actions, 'eid' => $eid));
                 }
             }
+            
+            $returnrows[] = $row;
 
-            $html .= '</tr>';
-
-            $html .= $html_children;
+            $returnrows = array_merge($returnrows, $html_children);
 
             // Print a coloured row to show the end of the category accross the table
-            $html .= '<tr><td colspan="'.(19 - $level).'" class="colspan '.$levelclass.'"></td></tr>';
+            $endcell = new html_table_cell();
+            $endcell->colspan = (19 - $level);
+            $endcell->add_classes(array('colspan', $levelclass));
+
+            $returnrows[] = html_table_row::make(array($endcell));;
 
         } else { // Dealing with a grade item
 
@@ -302,25 +334,24 @@ class grade_edit_tree {
                 $categoryitemclass = 'categoryitem';
             }
 
-            $dimmed = ($item->is_hidden()) ? " dimmed_text " : "";
-            $html .= '<tr class="'.$categoryitemclass.' item'.$dimmed.$rowclasses.'">';
+            $dimmed = ($item->is_hidden()) ? "dimmed_text" : "";
+            $gradeitemrow = new html_table_row();
+            $gradeitemrow->add_classes(array($categoryitemclass, 'item', $dimmed));
+            foreach ($rowclasses as $class) {
+                $gradeitemrow->add_class($class);
+            }
 
             foreach ($this->columns as $column) {
                 if (!($this->moving && $column->hide_when_moving) && !$column->is_hidden($mode)) {
-                    $html .= $column->get_item_cell($item, array('id' => $id, 'name' => $object->name, 'level' => $level, 'actions' => $actions,
+                    $gradeitemrow->cells[] = $column->get_item_cell($item, array('id' => $id, 'name' => $object->name, 'level' => $level, 'actions' => $actions,
                                                                  'element' => $element, 'eid' => $eid, 'itemtype' => $object->itemtype));
                 }
             }
 
-            $html .= '</tr>';
+            $returnrows[] = $gradeitemrow;
         }
 
-
-        if ($root) {
-            $html .= "</table>\n";
-        }
-
-        return $html;
+        return $returnrows;
 
     }
 
@@ -361,7 +392,7 @@ class grade_edit_tree {
             $weightfield->type = 'hidden';
             $weightfield->name = "extracredit_$item->id";
             $weightfield->value = 0;
-            
+
             $extracredit = html_select_option::make_checkbox(1, ($item->aggregationcoef > 0), '&nbsp;');
             $extracredit->id = "extracredit_$item->id";
             $extracredit->name = "extracredit_$item->id";
@@ -476,6 +507,24 @@ abstract class grade_edit_tree_column {
     public $forced_hidden;
     public $advanced_hidden;
     public $hide_when_moving = true;
+    /**
+     * html_table_cell object used as a template for header cells in all categories.
+     * It must be cloned before being used.
+     * @var html_table_cell $headercell
+     */
+    public $headercell;
+    /**
+     * html_table_cell object used as a template for category cells in all categories.
+     * It must be cloned before being used.
+     * @var html_table_cell $categorycell
+     */
+    public $categorycell;
+    /**
+     * html_table_cell object used as a template for item cells in all categories.
+     * It must be cloned before being used.
+     * @var html_table_cell $itemcell
+     */
+    public $itemcell;
 
     public static function factory($name, $params=array()) {
         $class_name = "grade_edit_tree_column_$name";
@@ -491,6 +540,19 @@ abstract class grade_edit_tree_column {
     public abstract function get_item_cell($item, $params);
 
     public abstract function is_hidden($mode='simple');
+
+    public function __construct() {
+        $this->headercell = new html_table_cell();
+        $this->headercell->header = true;
+        $this->headercell->style = 'whitespace: normal;';
+        $this->headercell->add_class('header');
+
+        $this->categorycell = new html_table_cell();
+        $this->categorycell->add_class('cell');
+
+        $this->itemcell = new html_table_cell();
+        $this->itemcell->add_class('cell');
+    }
 }
 
 abstract class grade_edit_tree_column_category extends grade_edit_tree_column {
@@ -502,6 +564,7 @@ abstract class grade_edit_tree_column_category extends grade_edit_tree_column {
         global $CFG;
         $this->forced = (int)$CFG->{"grade_$name"."_flag"} & 1;
         $this->advanced = (int)$CFG->{"grade_$name"."_flag"} & 2;
+        parent::__construct();
     }
 
     public function is_hidden($mode='simple') {
@@ -532,18 +595,27 @@ class grade_edit_tree_column_name extends grade_edit_tree_column {
         }
 
         $this->deepest_level = $params['deepest_level'];
+        parent::__construct();
     }
 
     public function get_header_cell() {
-        return '<th class="header name" colspan="'.($this->deepest_level + 1).'" scope="col">'.get_string('name').'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->add_class('name');
+        $headercell->colspan = $this->deepest_level + 1;
+        $headercell->text = get_string('name');
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
+        global $OUTPUT;
         if (empty($params['name']) || empty($params['level'])) {
             throw new Exception('Array key (name or level) missing from 3rd param of grade_edit_tree_column_name::get_category_cell($category, $levelclass, $params)');
         }
-
-        return '<td class="cell name '.$levelclass.'" colspan="'.(($this->deepest_level +1) - $params['level']).'"><h4>' . $params['name'] . "</h4></td>\n";
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_classes(array('name', $levelclass));
+        $categorycell->colspan = ($this->deepest_level +1) - $params['level'];
+        $categorycell->text = $OUTPUT->heading($params['name'], 4);
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
@@ -555,7 +627,11 @@ class grade_edit_tree_column_name extends grade_edit_tree_column {
 
         $name = $params['name'];
 
-        return '<td class="cell name" colspan="'.(($this->deepest_level + 1) - $params['level']).'">' . $name . '</td>';
+        $itemcell = clone($this->itemcell);
+        $itemcell->add_class('name');
+        $itemcell->colspan = ($this->deepest_level + 1) - $params['level'];
+        $itemcell->text = $name;
+        return $itemcell;
     }
 
     public function is_hidden($mode='simple') {
@@ -571,7 +647,9 @@ class grade_edit_tree_column_aggregation extends grade_edit_tree_column_category
 
     public function get_header_cell() {
         global $OUTPUT;
-        return '<th class="header" scope="col">'.get_string('aggregation', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('aggregation', 'aggregation', 'grade')).'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->text = get_string('aggregation', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('aggregation', 'aggregation', 'grade'));
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
@@ -608,12 +686,17 @@ class grade_edit_tree_column_aggregation extends grade_edit_tree_column_category
             $aggregation = $options[$category->aggregation];
         }
 
-        return '<td class="cell '.$levelclass.'">' . $aggregation . '</td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = $aggregation;
+        return $categorycell;
 
     }
 
     public function get_item_cell($item, $params) {
-          return '<td class="cell"> - </td>';
+        $itemcell = clone($this->itemcell);
+        $itemcell->text = ' - ';
+        return $itemcell;
     }
 }
 
@@ -621,14 +704,17 @@ class grade_edit_tree_column_extracredit extends grade_edit_tree_column {
 
     public function get_header_cell() {
         global $OUTPUT;
-        return '<th class="header" scope="col">'.get_string('extracredit', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('aggregationcoefcombo', 'aggregationcoefcombo', 'grade')).'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->text = get_string('extracredit', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('aggregationcoefcombo', 'aggregationcoefcombo', 'grade'));
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
-
         $item = $category->get_grade_item();
-        $aggcoef_input = grade_edit_tree::get_weight_input($item, 'extra');
-        return '<td class="cell '.$levelclass.'">' . $aggcoef_input . '</td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = grade_edit_tree::get_weight_input($item, 'extra');
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
@@ -636,13 +722,14 @@ class grade_edit_tree_column_extracredit extends grade_edit_tree_column {
             throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
         }
 
-        $html = '<td class="cell">';
+        $itemcell = clone($this->itemcell);
+        $itemcell->text = '&nbsp;';
 
         if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))) {
-            $html .= grade_edit_tree::get_weight_input($item, 'extra');
+            $itemcell->text = grade_edit_tree::get_weight_input($item, 'extra');
         }
 
-        return $html.'</td>';
+        return $itemcell;
     }
 
     public function is_hidden($mode='simple') {
@@ -659,28 +746,32 @@ class grade_edit_tree_column_weight extends grade_edit_tree_column {
 
     public function get_header_cell() {
         global $OUTPUT;
-        return '<th class="header" scope="col">'.get_string('weightuc', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('aggregationcoefweight', 'aggregationcoefweight', 'grade')).'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->text = get_string('weightuc', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('aggregationcoefweight', 'aggregationcoefweight', 'grade'));
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
 
         $item = $category->get_grade_item();
-        $aggcoef_input = grade_edit_tree::get_weight_input($item, 'weight');
-        return '<td class="cell '.$levelclass.'">' . $aggcoef_input . '</td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = grade_edit_tree::get_weight_input($item, 'weight');
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
         if (empty($params['element'])) {
             throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
         }
-
-        $html = '<td class="cell">';
+        $itemcell = clone($this->itemcell);
+        $itemcell->text = '&nbsp;';
 
         if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))) {
-            $html .= grade_edit_tree::get_weight_input($item, 'weight');
+            $itemcell->text = grade_edit_tree::get_weight_input($item, 'weight');
         }
 
-        return $html.'</td>';
+        return $itemcell;
     }
 
     public function is_hidden($mode='simple') {
@@ -696,11 +787,16 @@ class grade_edit_tree_column_weight extends grade_edit_tree_column {
 class grade_edit_tree_column_range extends grade_edit_tree_column {
 
     public function get_header_cell() {
-        return '<th class="header" scope="col">'.get_string('maxgrade', 'grades').'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->text = get_string('maxgrade', 'grades');
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
-        return '<td class="cell range '.$levelclass.'"> - </td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_classes(array('range', $levelclass));
+        $categorycell->text = ' - ';
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
@@ -723,7 +819,9 @@ class grade_edit_tree_column_range extends grade_edit_tree_column {
             $grademax = $OUTPUT->field($grademaxinput);
         }
 
-        return '<td class="cell">'.$grademax.'</td>';
+        $itemcell = clone($this->itemcell);
+        $itemcell->text = $grademax;
+        return $itemcell;
     }
 
     public function is_hidden($mode='simple') {
@@ -744,31 +842,40 @@ class grade_edit_tree_column_aggregateonlygraded extends grade_edit_tree_column_
 
     public function get_header_cell() {
         global $OUTPUT;
-        return '<th class="header" style="width: 40px" scope="col">'.get_string('aggregateonlygraded', 'grades')
-              .$OUTPUT->help_icon(moodle_help_icon::make('aggregateonlygraded', 'aggregateonlygraded', 'grade')).'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->style .= 'width: 40px;';
+        $headercell->text = get_string('aggregateonlygraded', 'grades') 
+                . $OUTPUT->help_icon(moodle_help_icon::make('aggregateonlygraded', 'aggregateonlygraded', 'grade'));
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
         global $OUTPUT;
-        
+
         $hidden = new html_field();
         $hidden->type = 'hidden';
         $hidden->name = "aggregateonlygraded_$category->id";
         $hidden->value = 0;
-        
+
         $aggregateonlygraded = html_select_option::make_checkbox(1, ($category->aggregateonlygraded == 1), '&nbsp;');
         $aggregateonlygraded->id = "aggregateonlygraded_$category->id";
         $aggregateonlygraded->name = "aggregateonlygraded_$category->id";
+        $aggregateonlygraded = $OUTPUT->checkbox($aggregateonlygraded);
 
         if ($this->forced) {
             $aggregateonlygraded = ($category->aggregateonlygraded) ? get_string('yes') : get_string('no');
         }
 
-        return '<td class="cell '.$levelclass.'">'.$OUTPUT->field($hidden).$OUTPUT->checkbox($aggregateonlygraded).'</td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = $OUTPUT->field($hidden).$aggregateonlygraded;
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
-        return '<td class="cell"> - </td>';
+        $itemcell = clone($this->itemcell);
+        $itemcell->text = ' - ';
+        return $itemcell;
     }
 }
 
@@ -780,25 +887,40 @@ class grade_edit_tree_column_aggregatesubcats extends grade_edit_tree_column_cat
 
     public function get_header_cell() {
         global $OUTPUT;
-        return '<th class="header" style="width: 40px" scope="col">'.get_string('aggregatesubcats', 'grades')
-              .$OUTPUT->help_icon(moodle_help_icon::make('aggregatesubcats', 'aggregatesubcats', 'grade')).'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->style .= 'width: 40px;';
+        $headercell->text = get_string('aggregatesubcats', 'grades')
+              .$OUTPUT->help_icon(moodle_help_icon::make('aggregatesubcats', 'aggregatesubcats', 'grade'));
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
-        $subcatscheck = ($category->aggregatesubcats == 1) ? 'checked="checked"' : '';
-        $hidden = '<input type="hidden" name="aggregatesubcats_'.$category->id.'" value="0" />';
-        $aggregatesubcats = '<input type="checkbox" id="aggregatesubcats_'.$category->id.'" name="aggregatesubcats_'.$category->id.'" value="1" ' . $subcatscheck.' />';
+        global $OUTPUT;
+        $hidden = new html_field();
+        $hidden->type = 'hidden';
+        $hidden->name = "aggregatesubcats_$category->id";
+        $hidden->value = 0;
+
+        $aggregatesubcats = html_select_option::make_checkbox(1, ($category->aggregatesubcats == 1), '&nbsp;');
+        $aggregatesubcats->id = "aggregatesubcats_$category->id";
+        $aggregatesubcats->name = "aggregatesubcats_$category->id";
+        $aggregatesubcats = $OUTPUT->checkbox($aggregatesubcats);
 
         if ($this->forced) {
             $aggregatesubcats = ($category->aggregatesubcats) ? get_string('yes') : get_string('no');
         }
 
-        return '<td class="cell '.$levelclass.'">'.$hidden.$aggregatesubcats.'</td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = $OUTPUT->field($hidden).$aggregatesubcats;
+        return $categorycell;
 
     }
 
     public function get_item_cell($item, $params) {
-        return '<td class="cell"> - </td>';
+        $itemcell = clone($this->itemcell);
+        $itemcell->text = ' - ';
+        return $itemcell;
     }
 }
 
@@ -810,25 +932,39 @@ class grade_edit_tree_column_aggregateoutcomes extends grade_edit_tree_column_ca
 
     public function get_header_cell() {
         global $OUTPUT;
-        return '<th class="header" style="width: 40px" scope="col">'.get_string('aggregateoutcomes', 'grades')
-              .$OUTPUT->help_icon(moodle_help_icon::make('aggregateoutcomes', 'aggregateoutcomes', 'grade')).'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->style .= 'width: 40px;';
+        $headercell->text = get_string('aggregateoutcomes', 'grades')
+              .$OUTPUT->help_icon(moodle_help_icon::make('aggregateoutcomes', 'aggregateoutcomes', 'grade'));
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
+        global $OUTPUT;
+        $hidden = new html_field();
+        $hidden->type = 'hidden';
+        $hidden->name = "aggregateoutcomes_$category->id";
+        $hidden->value = 0;
 
-        $outcomescheck = ($category->aggregateoutcomes == 1) ? 'checked="checked"' : '';
-        $hidden = '<input type="hidden" name="aggregateoutcomes_'.$category->id.'" value="0" />';
-        $aggregateoutcomes = '<input type="checkbox" id="aggregateoutcomes_'.$category->id.'" name="aggregateoutcomes_'.$category->id.'" value="1" ' . $outcomescheck.' />';
+        $aggregateoutcomes = html_select_option::make_checkbox(1, ($category->aggregateoutcomes == 1), '&nbsp;');
+        $aggregateoutcomes->id = "aggregateoutcomes_$category->id";
+        $aggregateoutcomes->name = "aggregateoutcomes_$category->id";
+        $aggregateoutcomes = $OUTPUT->checkbox($aggregateoutcomes);
 
         if ($this->forced) {
             $aggregateoutcomes = ($category->aggregateoutcomes) ? get_string('yes') : get_string('no');
         }
 
-        return '<td class="cell '.$levelclass.'">'.$hidden.$aggregateoutcomes.'</td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = $OUTPUT->field($hidden).$aggregateoutcomes;
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
-        return '<td class="cell"> - </td>';
+        $itemcell = clone($this->itemcell);
+        $itemcell->text = ' - ';
+        return $itemcell;
     }
 
     public function is_hidden($mode='simple') {
@@ -849,21 +985,31 @@ class grade_edit_tree_column_droplow extends grade_edit_tree_column_category {
 
     public function get_header_cell() {
         global $OUTPUT;
-        return '<th class="header" scope="col">'.get_string('droplow', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('droplow', 'droplow', 'grade')).'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->text = get_string('droplow', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('droplow', 'droplow', 'grade'));
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
-        $droplow = '<input type="text" size="3" id="droplow_'.$category->id.'" name="droplow_'.$category->id.'" value="'.$category->droplow.'" />';
+        global $OUTPUT;
+        $droplowinput = html_field::make_text("droplow_$category->id", $category->droplow, get_string('droplow', 'grades'));
+        $droplowinput->id = $droplowinput->name;
+        $droplow = $OUTPUT->field($droplowinput);
 
         if ($this->forced) {
             $droplow = $category->droplow;
         }
 
-        return '<td class="cell '.$levelclass.'">' . $droplow . '</td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = $droplow;
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
-        return '<td class="cell"> - </td>';
+        $itemcell = clone($this->itemcell);
+        $itemcell->text = ' - ';
+        return $itemcell;
     }
 }
 
@@ -875,46 +1021,68 @@ class grade_edit_tree_column_keephigh extends grade_edit_tree_column_category {
 
     public function get_header_cell() {
         global $OUTPUT;
-        return '<th class="header" scope="col">'.get_string('keephigh', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('keephigh', 'keephigh', 'grade')).'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->text = get_string('keephigh', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('keephigh', 'keephigh', 'grade'));
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
-        $keephigh = '<input type="text" size="3" id="keephigh_'.$category->id.'" name="keephigh_'.$category->id.'" value="'.$category->keephigh.'" />';
+        global $OUTPUT;
+        $keephighinput = html_field::make_text("keephigh_$category->id", $category->keephigh, get_string('keephigh', 'grades'));
+        $keephighinput->id = $keephighinput->name;
+        $keephigh = $OUTPUT->field($keephighinput);
 
         if ($this->forced) {
             $keephigh = $category->keephigh;
         }
 
-        return '<td class="cell '.$levelclass.'">' . $keephigh . '</td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = $keephigh;
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
-        return '<td class="cell"> - </td>';
+        $itemcell = clone($this->itemcell);
+        $itemcell->text = ' - ';
+        return $itemcell;
     }
 }
 
 class grade_edit_tree_column_multfactor extends grade_edit_tree_column {
 
     public function __construct($params) {
-
+        parent::__construct();
     }
 
     public function get_header_cell() {
         global $OUTPUT;
-        return '<th class="header" scope="col">'.get_string('multfactor', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('multfactor', 'multfactor', 'grade')).'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->text = get_string('multfactor', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('multfactor', 'multfactor', 'grade'));
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
-
-        return '<td class="cell '.$levelclass.'"> - </td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = ' - ';
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
+        global $OUTPUT;
+
+        $itemcell = clone($this->itemcell);
         if (!$item->is_raw_used()) {
-            return '<td class="cell">&nbsp;</td>';
+            $itemcell->text = '&nbsp;';
+            return $itemcell;
         }
-        $multfactor = '<input type="text" size="4" id="multfactor'.$item->id.'" name="multfactor_'.$item->id.'" value="'.format_float($item->multfactor, 4).'" />';
-        return '<td class="cell">'.$multfactor.'</td>';
+
+        $multfactorinput = html_field::make_text("multfactor_$item->id", format_float($item->multfactor, 4), get_string('multfactor', 'grades'));
+        $multfactorinput->id = $multfactorinput->name;
+
+        $itemcell->text = $OUTPUT->field($multfactorinput);
+        return $itemcell;
     }
 
     public function is_hidden($mode='simple') {
@@ -931,20 +1099,32 @@ class grade_edit_tree_column_plusfactor extends grade_edit_tree_column {
 
     public function get_header_cell() {
         global $OUTPUT;
-        return '<th class="header" scope="col">'.get_string('plusfactor', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('plusfactor', 'plusfactor', 'grade')).'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->text = get_string('plusfactor', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('plusfactor', 'plusfactor', 'grade'));
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
-        return '<td class="cell '.$levelclass.'"> - </td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = ' - ';
+        return $categorycell;
 
     }
 
     public function get_item_cell($item, $params) {
+        global $OUTPUT;
+
+        $itemcell = clone($this->itemcell);
         if (!$item->is_raw_used()) {
-            return '<td class="cell">&nbsp;</td>';
+            $itemcell->text = '&nbsp;';
+            return $itemcell;
         }
-        $plusfactor = '<input type="text" size="4" id="plusfactor_'.$item->id.'" name="plusfactor_'.$item->id.'" value="'.format_float($item->plusfactor, 4).'" />';
-        return '<td class="cell">'.$plusfactor.'</td>';
+
+        $plusfactorinput = html_field::make_text("plusfactor_$item->id", format_float($item->plusfactor, 4), get_string('plusfactor', 'grades'));
+        $plusfactorinput->id = $plusfactorinput->name;
+        $itemcell->text = $OUTPUT->field($plusfactorinput);
+        return $itemcell;
 
     }
 
@@ -961,11 +1141,14 @@ class grade_edit_tree_column_plusfactor extends grade_edit_tree_column {
 class grade_edit_tree_column_actions extends grade_edit_tree_column {
 
     public function __construct($params) {
-
+        parent::__construct(); 
     }
 
     public function get_header_cell() {
-        return '<th class="header actions" scope="col">'.get_string('actions').'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->add_class('actions');
+        $headercell->text = get_string('actions');
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
@@ -974,14 +1157,20 @@ class grade_edit_tree_column_actions extends grade_edit_tree_column {
             throw new Exception('Array key (actions) missing from 3rd param of grade_edit_tree_column_actions::get_category_actions($category, $levelclass, $params)');
         }
 
-        return '<td class="cell actions '.$levelclass.'">' . $params['actions'] . '</td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_class($levelclass);
+        $categorycell->text = $params['actions'];
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
         if (empty($params['actions'])) {
             throw new Exception('Array key (actions) missing from 2nd param of grade_edit_tree_column_actions::get_item_cell($item, $params)');
         }
-        return '<td class="cell actions">' . $params['actions'] . '</td>';
+        $itemcell = clone($this->itemcell);
+        $itemcell->add_class('actions');
+        $itemcell->text = $params['actions'];
+        return $itemcell;
     }
 
     public function is_hidden($mode='simple') {
@@ -992,22 +1181,46 @@ class grade_edit_tree_column_actions extends grade_edit_tree_column {
 class grade_edit_tree_column_select extends grade_edit_tree_column {
 
     public function get_header_cell() {
-        return '<th class="header selection" scope="col">'.get_string('select').'</th>';
+        $headercell = clone($this->headercell);
+        $headercell->add_class('selection');
+        $headercell->text = get_string('select');
+        return $headercell;
     }
 
     public function get_category_cell($category, $levelclass, $params) {
-
+        global $OUTPUT;
         if (empty($params['eid'])) {
             throw new Exception('Array key (eid) missing from 3rd param of grade_edit_tree_column_select::get_category_cell($category, $levelclass, $params)');
         }
+        $selectall = new html_span();
+        $selectall->add_class('actionlink');
+        $selectnone = clone($selectall);
+        $selectall->add_action('click', 'togglecheckboxes', array('eid' => $params['eid'], 'check' => true));
+        $selectall->contents = get_string('all');
+        $selectnone->add_action('click', 'togglecheckboxes', array('eid' => $params['eid'], 'check' => false));
+        $selectnone->contents = get_string('none');
 
-        return '<td class="cell last  '.$levelclass.'" style="text-align: center">
-                    <span class="actionlink" onclick="togglecheckboxes(\''.$params['eid'].'\', true);">'.get_string('all').'</span><br />
-                    <span class="actionlink" onclick="togglecheckboxes(\''.$params['eid'].'\', false);">'.get_string('none').'</span>
-                </td>';
+        $categorycell = clone($this->categorycell);
+        $categorycell->add_classes(array('last', $levelclass));
+        $categorycell->style .= 'text-align: center;';
+        $categorycell->text = $OUTPUT->span($selectall) . '<br />' . $OUTPUT->span($selectnone);
+        return $categorycell;
     }
 
     public function get_item_cell($item, $params) {
+        global $OUTPUT;
+        if (empty($params['itemtype']) || empty($params['eid'])) {
+            throw new Exception('Array key (itemtype or eid) missing from 2nd param of grade_edit_tree_column_select::get_item_cell($item, $params)');
+        }
+        $itemselect = '';
+
+        if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
+            $itemselect = html_select_option::make_checkbox('0', false, '&nbsp;');
+            $itemselect->add_action('change', 'toggleCategorySelector');
+            $itemselect->add_class('itemselect');
+            $itemselect = $OUTPUT->checkbox($itemselect, 'select_'.$params['eid']);
+        }
+        /*
         if (empty($params['itemtype']) || empty($params['eid'])) {
             throw new Exception('Array key (itemtype or eid) missing from 2nd param of grade_edit_tree_column_select::get_item_cell($item, $params)');
         }
@@ -1016,7 +1229,11 @@ class grade_edit_tree_column_select extends grade_edit_tree_column {
         if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
             $itemselect = '<input class="itemselect" type="checkbox" name="select_'.$params['eid'].'" onchange="toggleCategorySelector();"/>';
         }
-        return '<td class="cell last selection">' . $itemselect . '</td>';
+        */
+        $itemcell = clone($this->itemcell);
+        $itemcell->add_classes(array('last', 'selection'));
+        $itemcell->text = $itemselect;
+        return $itemcell;
     }
 
     public function is_hidden($mode='simple') {
index 79627eecc10af61ce0dea9642a563b5dba52cb72..d5f6f42f12856464f29116abf298d42bffd84672 100644 (file)
@@ -929,6 +929,8 @@ class html_field extends labelled_html_component {
  */
 class html_table extends labelled_html_component {
     /**
+     * For more control over the rendering of the headers, an array of html_table_cell objects 
+     * can be passed instead of an array of strings.
      * @var array of headings. The n-th array item is used as a heading of the n-th column.
      *
      * Example of usage:
@@ -1144,6 +1146,17 @@ class html_table_row extends moodle_html_component {
     public function prepare() {
         parent::prepare();
     }
+    
+    /**
+     * Shortcut method for creating a row with an array of cells.
+     * @param array $cells
+     * @return html_table_row
+     */
+    public function make($cells=array()) {
+        $row = new html_table_row();
+        $row->cells = $cells;
+        return $row;
+    }
 }
 
 /**
@@ -1184,6 +1197,9 @@ class html_table_cell extends moodle_html_component {
      * @return void
      */
     public function prepare() {
+        if ($this->header && empty($this->scope)) {
+            $this->scope = 'col';
+        }
         parent::prepare();
     }
 }
@@ -1540,6 +1556,28 @@ class html_list_item extends moodle_html_component {
     }
 }
 
+/**
+ * Component representing a span element. It has no special attributes, so 
+ * it is very low-level and can be used for styling and JS actions.
+ *
+ * @copyright 2009 Nicolas Connault
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since     Moodle 2.0
+ */
+class html_span extends moodle_html_component {
+    /**
+     * @var string $text The contents of the span
+     */
+    public $contents;
+    /**
+     * @see lib/moodle_html_component#prepare()
+     * @return void
+     */
+    public function prepare() {
+        parent::prepare();
+    }
+}
+
 /// Complex components aggregating simpler components
 
 /**
index 3a75051ba463fe2a84368176e263be9bc766eb8d..f906ee3703f52d9c8701c898216bd5e9c54a05f7 100644 (file)
@@ -1552,6 +1552,24 @@ class moodle_core_renderer extends moodle_renderer_base {
 
         return $output . $this->output_end_tag($tag);
     }
+    
+    /**
+     * Prints an inline span element with optional text contents.
+     *
+     * @param html_span $span A html_span object
+     * @return string A HTML fragment
+     */
+    public function span($span) {
+        $span = clone($span);
+        $span->prepare();
+        $this->prepare_event_handlers($span);
+        $attributes = array('class' => $span->get_classes_string(),
+                            'alt' => $span->alt,
+                            'style' => $span->style,
+                            'title' => $span->title,
+                            'id' => $span->id);
+        return $this->output_tag('span', $attributes, $span->contents);
+    }
 
     /**
      * Prints a simple button to close a window
@@ -2016,30 +2034,42 @@ class moodle_core_renderer extends moodle_renderer_base {
             $output .= $this->output_start_tag('tr', array()) . "\n";
             $keys = array_keys($table->head);
             $lastkey = end($keys);
+
             foreach ($table->head as $key => $heading) {
-                $classes = array('header', 'c' . $key);
+                // Convert plain string headings into html_table_cell objects
+                if (!($heading instanceof html_table_cell)) {
+                    $headingtext = $heading;
+                    $heading = new html_table_cell();
+                    $heading->text = $headingtext;
+                    $heading->header = true;
+                }
+
+                $this->prepare_event_handlers($heading);
+
+                $heading->add_classes(array('header', 'c' . $key));
                 if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
-                    $colspan = $table->headspan[$key];
+                    $heading->colspan = $table->headspan[$key];
                     $countcols += $table->headspan[$key] - 1;
-                } else {
-                    $colspan = '';
-                }
+                } 
+
                 if ($key == $lastkey) {
-                    $classes[] = 'lastcol';
+                    $heading->add_class('lastcol');
                 }
                 if (isset($table->colclasses[$key])) {
-                    $classes[] = $table->colclasses[$key];
+                    $heading->add_class($table->colclasses[$key]);
                 }
                 if ($table->rotateheaders) {
                     // we need to wrap the heading content
-                    $heading = $this->output_tag('span', '', $heading);
+                    $heading->text = $this->output_tag('span', '', $heading->text);
                 }
+
                 $attributes = array(
-                        'style'     => $table->align[$key] . $table->size[$key] . 'white-space:nowrap;',
-                        'class'     => moodle_renderer_base::prepare_classes($classes),
-                        'scope'     => 'col',
-                        'colspan'   => $colspan);
-                $output .= $this->output_tag('th', $attributes, $heading) . "\n";
+                        'style'     => $table->align[$key] . $table->size[$key] . $heading->style,
+                        'class'     => $heading->get_classes_string(),
+                        'scope'     => $heading->scope,
+                        'colspan'   => $heading->colspan);
+                
+                $output .= $this->output_tag('th', $attributes, $heading->text) . "\n";
             }
             $output .= $this->output_end_tag('tr') . "\n";
             $output .= $this->output_end_tag('thead') . "\n";
@@ -2063,10 +2093,13 @@ class moodle_core_renderer extends moodle_renderer_base {
                         foreach ($row as $unused => $item) {
                             $cell = new html_table_cell();
                             $cell->text = $item;
+                            $this->prepare_event_handlers($cell);
                             $newrow->cells[] = $cell;
                         }
                         $row = $newrow;
                     }
+                    
+                    $this->prepare_event_handlers($row);
 
                     $oddeven = $oddeven ? 0 : 1;
                     if (isset($table->rowclasses[$key])) {
@@ -2083,6 +2116,13 @@ class moodle_core_renderer extends moodle_renderer_base {
                     $lastkey = end($keys2);
 
                     foreach ($row->cells as $key => $cell) {
+                        if (!($cell instanceof html_table_cell)) {
+                            $mycell = new html_table_cell();
+                            $mycell->text = $cell;
+                            $this->prepare_event_handlers($mycell);
+                            $cell = $mycell;
+                        }
+
                         if (isset($table->colclasses[$key])) {
                             $cell->add_classes(array_unique(moodle_html_component::clean_classes($table->colclasses[$key])));
                         }