MDL-20204 started refactoring of html_table + more cleanup
authorPetr Skoda <skodak@moodle.org>
Wed, 17 Feb 2010 19:33:24 +0000 (19:33 +0000)
committerPetr Skoda <skodak@moodle.org>
Wed, 17 Feb 2010 19:33:24 +0000 (19:33 +0000)
15 files changed:
blog/external_blogs.php
grade/edit/tree/lib.php
grade/report/grader/lib.php
lib/outputcomponents.php
lib/outputrenderers.php
mod/forum/renderer.php
mod/lesson/lib.php
mod/lesson/pagetypes/branchtable.php
mod/lesson/pagetypes/matching.php
mod/lesson/pagetypes/multichoice.php
mod/lesson/pagetypes/numerical.php
mod/lesson/pagetypes/shortanswer.php
mod/lesson/pagetypes/truefalse.php
mod/lesson/renderer.php
user/index.php

index 792360f..7eb685c 100644 (file)
@@ -85,7 +85,7 @@ if (!empty($blogs)) {
         $action = new confirm_action(get_string('externalblogdeleteconfirm', 'blog'));
         $deleteicon = $OUTPUT->action_icon($deletelink, new pix_icon('t/delete', get_string('deleteexternalblog', 'blog')), $action);
 
-        $table->data[] = html_table_row::make(array($blog->name, $blog->url, userdate($blog->timefetched), $validicon, $editicon . $deleteicon));
+        $table->data[] = new html_table_row(array($blog->name, $blog->url, userdate($blog->timefetched), $validicon, $editicon . $deleteicon));
     }
     echo $OUTPUT->table($table);
 }
index e408857..f02b96d 100755 (executable)
@@ -163,7 +163,7 @@ class grade_edit_tree {
             $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)));
+            return array(new html_table_row(array($cell)));
         }
 
         if ($element['type'] == 'category') {
@@ -217,7 +217,7 @@ class grade_edit_tree {
                     $cell->colspan = 12;
                     $cell->text = $OUTPUT->action_icon($aurl, $strmovehere, new pix_icon(array('class'=>'movetarget'), 'movehere'));
 
-                    $moveto = html_table_row::make(array($cell));
+                    $moveto = new html_table_row(array($cell));
                 }
 
                 $newparents = $parents;
@@ -306,7 +306,7 @@ class grade_edit_tree {
             $endcell->colspan = (19 - $level);
             $endcell->add_classes(array('colspan', $levelclass));
 
-            $returnrows[] = html_table_row::make(array($endcell));;
+            $returnrows[] = new html_table_row(array($endcell));;
 
         } else { // Dealing with a grade item
 
index a4091ab..4c041c2 100644 (file)
@@ -548,7 +548,7 @@ class grade_report_grader extends grade_report {
             $fillercell->add_classes(array('fixedcolumn', 'cell', 'topleft'));
             $fillercell->text = ' ';
             $fillercell->colspan = $colspan;
-            $row = html_table_row::make(array($fillercell));
+            $row = new html_table_row(array($fillercell));
             $rows[] = $row;
         }
 
index 4fe93b0..baeba86 100644 (file)
@@ -1082,7 +1082,7 @@ class js_writer {
 
 
 // ===============================================================================================
-// TODO: Following components will be refactored soon
+// TODO: Following HTML components still need some refactoring
 
 /**
  * Base class for classes representing HTML elements.
@@ -1120,15 +1120,6 @@ class html_component {
      */
     protected $actions = array();
 
-    /**
-     * Compoment constructor.
-     * @param array $options image attributes such as title, id, alt, style, class
-     */
-    public function __construct(array $options = null) {
-        // not implemented in this class because we want to set only public properties of this component
-        renderer_base::apply_component_options($this, $options);
-    }
-
     /**
      * Ensure some class names are an array.
      * @param mixed $classes either an array of class names or a space-separated
@@ -1186,102 +1177,14 @@ class html_component {
      * Perform any cleanup or final processing that should be done before an
      * instance of this class is output. This method is supposed to be called
      * only from renderers.
-     *
-     * @param renderer_base $output output renderer
-     * @param moodle_page $page
-     * @param string $target rendering target
      * @return void
      */
-    public function prepare(renderer_base $output, moodle_page $page, $target) {
+    public function prepare() {
         $this->classes = array_unique(self::clean_classes($this->classes));
     }
-
-    /**
-     * This checks developer do not try to assign a property directly
-     * if we have a setter for it. Otherwise, the property is set as expected.
-     * @param string $name The name of the variable to set
-     * @param mixed $value The value to assign to the variable
-     * @return void
-     */
-    public function __set($name, $value) {
-        if ($name == 'class') {
-            debugging('this way of setting css class has been deprecated. use set_classes() method instead.');
-            $this->set_classes($value);
-        } else {
-            $this->{$name} = $value;
-        }
-    }
-
-    /**
-     * Adds a JS action to this component.
-     * Note: the JS function you write must have only two arguments: (string)event and (object|array)args
-     * If you want to add an instantiated component_action (or one of its subclasses), give the object as the only parameter
-     *
-     * @param mixed  $event a DOM event (click, mouseover etc.) or a component_action object
-     * @param string $jsfunction The name of the JS function to call. required if argument 1 is a string (event)
-     * @param array  $jsfunctionargs An optional array of JS arguments to pass to the function
-     */
-    public function add_action($event, $jsfunction=null, $jsfunctionargs=array()) {
-        if (empty($this->id)) {
-            $this->generate_id();
-        }
-
-        if ($event instanceof component_action) {
-            $this->actions[] = $event;
-        } else {
-            if (empty($jsfunction)) {
-                throw new coding_exception('html_component::add_action requires a JS function argument if the first argument is a string event');
-            }
-            $this->actions[] = new component_action($event, $jsfunction, $jsfunctionargs);
-        }
-    }
-
-    /**
-     * Internal method for generating a unique ID for the purpose of event handlers.
-     */
-    protected function generate_id() {
-        $this->id = uniqid(get_class($this));
-    }
-
-    /**
-     * Returns the array of component_actions.
-     * @return array Component actions
-     */
-    public function get_actions() {
-        return $this->actions;
-    }
-
-    /**
-     * Shortcut for adding a JS confirm dialog when the component is clicked.
-     * The message must be a yes/no question.
-     * @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
-     * @param string $callback The name of a JS function whose scope will be set to the simpleDialog object and have this
-     *    function's arguments set as this.args.
-     * @return void
-     */
-    public function add_confirm_action($message, $callback=null) {
-        $this->add_action(new component_action('click', 'M.util.show_confirm_dialog', array('message' => $message, 'callback' => $callback)));
-    }
-
-    /**
-     * Returns true if this component has an action of the requested type (component_action by default).
-     * @param string $class The class of the action we are looking for
-     * @return boolean True if action is found
-     */
-    public function has_action($class='component_action') {
-        foreach ($this->actions as $action) {
-            if (get_class($action) == $class) {
-                return true;
-            }
-        }
-        return false;
-    }
 }
 
 
-/// Components representing HTML elements
-
-
 /**
  * Holds all the information required to render a <table> by
  * {@see core_renderer::table()} or by an overridden version of that
@@ -1437,7 +1340,7 @@ class html_table extends html_component {
      * @see html_component::prepare()
      * @return void
      */
-    public function prepare(renderer_base $output, moodle_page $page, $target) {
+    public function prepare() {
         if (!empty($this->align)) {
             foreach ($this->align as $key => $aa) {
                 if ($aa) {
@@ -1490,21 +1393,7 @@ class html_table extends html_component {
         } else {
             $this->rotateheaders = false; // Makes life easier later.
         }
-        parent::prepare($output, $page, $target);
-    }
-    /**
-     * @param string $name The name of the variable to set
-     * @param mixed $value The value to assign to the variable
-     * @return void
-     */
-    public function __set($name, $value) {
-        if ($name == 'rowclass') {
-            debugging('rowclass[] has been deprecated for html_table ' .
-                      'and should be replaced with rowclasses[]. please fix the code.');
-            $this->rowclasses = $value;
-        } else {
-            parent::__set($name, $value);
-        }
+        parent::prepare();
     }
 }
 
@@ -1526,27 +1415,23 @@ class html_table_row extends html_component {
      * @see lib/html_component#prepare()
      * @return void
      */
-    public function prepare(renderer_base $output, moodle_page $page, $target) {
-        parent::prepare($output, $page, $target);
+    public function prepare() {
+        parent::prepare();
     }
 
     /**
-     * Shortcut method for creating a row with an array of cells. Converts cells to html_table_cell objects.
+     * Constructor
      * @param array $cells
-     * @return html_table_row
-     */
-    public static function make($cells=array()) {
-        $row = new html_table_row();
-        foreach ($cells as $celltext) {
-            if (!($celltext instanceof html_table_cell)) {
-                $cell = new html_table_cell();
-                $cell->text = $celltext;
-                $row->cells[] = $cell;
+     */
+    public function __construct(array $cells=null) {
+        $cells = (array)$cells;
+        foreach ($cells as $cell) {
+            if ($cell instanceof html_table_cell) {
+                $this->cells[] = $cell;
             } else {
-                $row->cells[] = $celltext;
+                $this->cells[] = new html_table_cell($cell);
             }
         }
-        return $row;
     }
 }
 
@@ -1588,11 +1473,15 @@ class html_table_cell extends html_component {
      * @see lib/html_component#prepare()
      * @return void
      */
-    public function prepare(renderer_base $output, moodle_page $page, $target) {
+    public function prepare() {
         if ($this->header && empty($this->scope)) {
             $this->scope = 'col';
         }
-        parent::prepare($output, $page, $target);
+        parent::prepare();
+    }
+
+    public function __construct($text = null) {
+        $this->text = $text;
     }
 }
 
index a11432d..7d96748 100644 (file)
@@ -129,44 +129,6 @@ class renderer_base {
     public function pix_url($imagename, $component = 'moodle') {
         return $this->page->theme->pix_url($imagename, $component);
     }
-
-    /**
-     * A helper function that takes a html_component subclass as param.
-     * If that component has an id attribute and an array of valid component_action objects,
-     * it sets up the appropriate event handlers.
-     *
-     * @param html_component $component
-     * @return void;
-     */
-    protected function prepare_event_handlers(html_component $component) {
-        //TODO: to be deleted soon
-        $actions = $component->get_actions();
-        if (!empty($actions) && is_array($actions) && $actions[0] instanceof component_action) {
-            foreach ($actions as $action) {
-                if (!empty($action->jsfunction)) {
-                    $this->page->requires->event_handler("#$component->id", $action->event, $action->jsfunction, $action->jsfunctionargs);
-                }
-            }
-        }
-    }
-
-    /**
-     * Helper function for applying of html_component options
-     * @param html_component $component
-     * @param array $options
-     * @return void
-     */
-    public static function apply_component_options(html_component $component, array $options = null) {
-        //TODO: to be deleted soon
-        $options = (array)$options;
-        foreach ($options as $key => $value) {
-            if ($key === 'class' or $key === 'classes') {
-                $component->add_classes($value);
-            } else if (array_key_exists($key, $component)) {
-                $component->$key = $value;
-            }
-        }
-    }
 }
 
 
@@ -1770,8 +1732,6 @@ class core_renderer extends renderer_base {
                     $heading->header = true;
                 }
 
-                $this->prepare_event_handlers($heading);
-
                 $heading->add_classes(array('header', 'c' . $key));
                 if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
                     $heading->colspan = $table->headspan[$key];
@@ -1831,14 +1791,11 @@ class core_renderer extends 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])) {
                         $row->add_classes(array_unique(html_component::clean_classes($table->rowclasses[$key])));
@@ -1860,9 +1817,6 @@ class core_renderer extends renderer_base {
                             $cell = $mycell;
                         }
 
-                        // Prepare all events handlers for this cell
-                        $this->prepare_event_handlers($cell);
-
                         if (isset($table->colclasses[$key])) {
                             $cell->add_classes(array_unique(html_component::clean_classes($table->colclasses[$key])));
                         }
index 6f5ed2a..f09d279 100644 (file)
@@ -65,7 +65,7 @@ class mod_forum_renderer extends plugin_renderer_base {
 
         $table = new html_table();
         $table->set_classes(array('subscribertable','boxaligncenter'));
-        $table->data = array(html_table_row::make(array($existingcell, $actioncell, $potentialcell)));
+        $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
         $output .= $this->output->table($table);
 
         $output .= html_writer::end_tag('form');
index d9764d7..d957a05 100644 (file)
@@ -2596,7 +2596,7 @@ abstract class lesson_page extends lesson_base {
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("jump", "lesson")." $i<span>: ";
             $cells[] = $this->get_jump_name($answer->jumpto);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
             if ($i === 1){
                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
             }
index 3616486..c39d03e 100644 (file)
@@ -175,12 +175,12 @@ class lesson_page_type_branchtable extends lesson_page {
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("branch", "lesson")." $i<span>: ";
             $cells[] = format_text($answer->answer, FORMAT_MOODLE, $options);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("jump", "lesson")." $i<span>: ";
             $cells[] = $this->get_jump_name($answer->jumpto);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             if ($i === 1){
                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
index 71aae18..c0e2a02 100644 (file)
@@ -239,7 +239,7 @@ class lesson_page_type_matching extends lesson_page {
                         $cells[] = "<span class=\"label\">".get_string("wrongresponse", "lesson").'</span>';
                     }
                     $cells[] = format_text($answer->answer, FORMAT_MOODLE, $options);
-                    $table->data[] = html_table_row::make($cells);
+                    $table->data[] = new html_table_row($cells);
                 }
                 $n++;
                 $i--;
@@ -256,34 +256,34 @@ class lesson_page_type_matching extends lesson_page {
                     $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
                 }
                 $cells[] = format_text($answer->answer, FORMAT_MOODLE, $options);
-                $table->data[] = html_table_row::make($cells);
+                $table->data[] = new html_table_row($cells);
 
                 $cells = array();
                 $cells[] = '<span class="label">'.get_string("matchesanswer", "lesson")." $i</span>: ";
                 $cells[] = format_text($answer->response, FORMAT_MOODLE, $options);
-                $table->data[] = html_table_row::make($cells);
+                $table->data[] = new html_table_row($cells);
             }
 
             if ($i == 1) {
                 $cells = array();
                 $cells[] = '<span class="label">'.get_string("correctanswerscore", "lesson")." $i</span>: ";
                 $cells[] = $answer->score;
-                $table->data[] = html_table_row::make($cells);
+                $table->data[] = new html_table_row($cells);
 
                 $cells = array();
                 $cells[] = '<span class="label">'.get_string("correctanswerjump", "lesson")." $i</span>: ";
                 $cells[] = $this->get_jump_name($answer->jumpto);
-                $table->data[] = html_table_row::make($cells);
+                $table->data[] = new html_table_row($cells);
             } elseif ($i == 2) {
                 $cells = array();
                 $cells[] = '<span class="label">'.get_string("wronganswerscore", "lesson")." $i</span>: ";
                 $cells[] = $answer->score;
-                $table->data[] = html_table_row::make($cells);
+                $table->data[] = new html_table_row($cells);
 
                 $cells = array();
                 $cells[] = '<span class="label">'.get_string("wronganswerjump", "lesson")." $i</span>: ";
                 $cells[] = $this->get_jump_name($answer->jumpto);
-                $table->data[] = html_table_row::make($cells);
+                $table->data[] = new html_table_row($cells);
             }
 
             if ($i === 1){
index 59d86be..57839c9 100644 (file)
@@ -262,22 +262,22 @@ class lesson_page_type_multichoice extends lesson_page {
                 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
             }
             $cells[] = format_text($answer->answer, FORMAT_MOODLE, $options);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
             $cells[] = format_text($answer->response, FORMAT_MOODLE, $options);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
             $cells[] = $answer->score;
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
             $cells[] = $this->get_jump_name($answer->jumpto);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
             if ($i === 1){
                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
             }
index 43c1aee..1fd5b1d 100644 (file)
@@ -129,22 +129,22 @@ class lesson_page_type_numerical extends lesson_page {
                 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
             }
             $cells[] = format_text($answer->answer, FORMAT_MOODLE, $options);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
             $cells[] = format_text($answer->response, FORMAT_MOODLE, $options);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
             $cells[] = $answer->score;
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
             $cells[] = $this->get_jump_name($answer->jumpto);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
             if ($i === 1){
                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
             }
index 79f8a64..2fe51d7 100644 (file)
@@ -191,22 +191,22 @@ class lesson_page_type_shortanswer extends lesson_page {
                 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
             }
             $cells[] = format_text($answer->answer, FORMAT_MOODLE, $options);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
             $cells[] = format_text($answer->response, FORMAT_MOODLE, $options);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
             $cells[] = $answer->score;
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
             $cells[] = $this->get_jump_name($answer->jumpto);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
             if ($i === 1){
                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
             }
index e321093..4e27499 100644 (file)
@@ -114,22 +114,22 @@ class lesson_page_type_truefalse extends lesson_page {
                 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
             }
             $cells[] = format_text($answer->answer, FORMAT_MOODLE, $options);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
             $cells[] = format_text($answer->response, FORMAT_MOODLE, $options);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
             $cells[] = $answer->score;
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             $cells = array();
             $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
             $cells[] = $this->get_jump_name($answer->jumpto);
-            $table->data[] = html_table_row::make($cells);
+            $table->data[] = new html_table_row($cells);
 
             if ($i === 1){
                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
index 362d88b..5589df0 100644 (file)
@@ -282,13 +282,13 @@ class mod_lesson_renderer extends plugin_renderer_base {
             $cell->colspan = 2;
             $cell->style = 'text-align:center';
             $cell->text = format_text($page->contents, FORMAT_MOODLE, $options);
-            $pagetable->data[] = html_table_row::make(array($cell));
+            $pagetable->data[] = new html_table_row(array($cell));
 
             $cell = new html_table_cell();
             $cell->colspan = 2;
             $cell->style = 'text-align:center';
             $cell->text = '<strong>'.$qtypes[$page->qtype] . $page->option_description_string().'</strong>';
-            $pagetable->data[] = html_table_row::make(array($cell));
+            $pagetable->data[] = new html_table_row(array($cell));
 
             $pagetable = $page->display_answers($pagetable);
 
@@ -546,7 +546,7 @@ class mod_lesson_renderer extends plugin_renderer_base {
 
         $table = new html_table();
         $table->set_classes(array('progress_bar_table', 'center'));
-        $table->data = array(html_table_row::make($cells));
+        $table->data = array(new html_table_row($cells));
 
         return $this->output->box($this->output->table($table), 'progress_bar');
     }
index 6ad9378..5758db0 100644 (file)
                     $group->descriptionformat = FORMAT_MOODLE;
                 }
                 $contentcell->text = $OUTPUT->heading($contentheading, 3) . format_text($group->description, $group->descriptionformat);
-                $groupinfotable->data[] = html_table_row::make(array($picturecell, $contentcell));
+                $groupinfotable->data[] = new html_table_row(array($picturecell, $contentcell));
                 echo $OUTPUT->table($groupinfotable);
             }
         }