]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19756 Migrated time selectors from weblib to outputlib
authornicolasconnault <nicolasconnault>
Wed, 29 Jul 2009 01:08:52 +0000 (01:08 +0000)
committernicolasconnault <nicolasconnault>
Wed, 29 Jul 2009 01:08:52 +0000 (01:08 +0000)
lib/deprecatedlib.php
lib/outputlib.php
lib/weblib.php

index a0a09681fa5ddcdb6e915350ba46310bcd39372d..5ca3bbd46cb2817fc7b5b6d6a7227f2435f7f56a 100644 (file)
@@ -3196,3 +3196,106 @@ function choose_from_menu_nested($options,$name,$selected='',$nothing='choose',$
     }
 }
 
+/**
+ * Prints a help button about a scale
+ *
+ * @deprecated since Moodle 2.0
+ *
+ * @global object
+ * @param id $courseid
+ * @param object $scale
+ * @param boolean $return If set to true returns rather than echo's
+ * @return string|bool Depending on value of $return
+ */
+function print_scale_menu_helpbutton($courseid, $scale, $return=false) {
+    // debugging('print_scale_menu_helpbutton() has been deprecated. Please change your code to use $OUTPUT->help_button($scaleselectmenu).');
+    global $OUTPUT;
+
+    $helpbutton = help_button::make_scale_menu($courseid, $scale);
+
+    $output = $OUTPUT->help_button($helpbutton);
+
+    if ($return) {
+        return $output;
+    } else {
+        echo $output;
+    }
+}
+
+
+/**
+ * Prints time limit value selector
+ *
+ * @deprecated since Moodle 2.0
+ *
+ * Uses {@link choose_from_menu()} to generate HTML
+ * @see choose_from_menu()
+ *
+ * @global object
+ * @param int $timelimit default
+ * @param string $unit
+ * @param string $name
+ * @param boolean $return If set to true returns rather than echo's
+ * @return string|bool Depending on value of $return
+ */
+function print_timer_selector($timelimit = 0, $unit = '', $name = 'timelimit', $return=false) {
+    throw new coding_exception('print_timer_selector is completely deprecated. Please use $OUTPUT->select_menu($selectmenu) instead');
+}
+
+/**
+ * Prints form items with the names $hour and $minute
+ *
+ * @deprecated since Moodle 2.0
+ *
+ * @param string $hour  fieldname
+ * @param string $minute  fieldname
+ * @param int $currenttime A default timestamp in GMT
+ * @param int $step minute spacing
+ * @param boolean $return If set to true returns rather than echo's
+ * @return string|bool Depending on value of $return 
+ */
+function print_time_selector($hour, $minute, $currenttime=0, $step=5, $return=false) {
+    // debugging('print_time_selector() has been deprecated. Please change your code to use $OUTPUT->select_menu($timeselector).');
+    global $OUTPUT;    
+    $hourselector = moodle_select_menu::make_time_selector('hours', $hour, $currenttime);
+    $minuteselector = moodle_select_menu::make_time_selector('minutes', $minute, $currenttime, $step);
+    
+    $output = $OUTPUT->select_menu($hourselector) . $OUTPUT->select_menu($minuteselector);
+    
+    if ($return) {
+        return $output;
+    } else {
+        echo $output;
+    }
+}
+
+/**
+ * Prints form items with the names $day, $month and $year
+ *
+ * @deprecated since Moodle 2.0
+ *
+ * @param string $day   fieldname
+ * @param string $month  fieldname
+ * @param string $year  fieldname
+ * @param int $currenttime A default timestamp in GMT
+ * @param boolean $return If set to true returns rather than echo's
+ * @return string|bool Depending on value of $return
+ */
+function print_date_selector($day, $month, $year, $currenttime=0, $return=false) {
+
+    // debugging('print_date_selector() has been deprecated. Please change your code to use $OUTPUT->select_menu($dateselector).');
+    global $OUTPUT;    
+    
+    $dayselector = moodle_select_menu::make_time_selector('days', $day, $currenttime);
+    $monthselector = moodle_select_menu::make_time_selector('months', $month, $currenttime);
+    $yearselector = moodle_select_menu::make_time_selector('years', $year, $currenttime);
+    
+    $output = $OUTPUT->select_menu($dayselector) . $OUTPUT->select_menu($monthselector) . $OUTPUT->select_menu($yearselector);
+    
+    if ($return) {
+        return $output;
+    } else {
+        echo $output;
+    }
+}
+
index 6079ac76984e0e88d21e0a360deda4d15856126e..22387bb4984aa201ce7d6feaac50603814f6e163 100644 (file)
@@ -2439,11 +2439,11 @@ class moodle_core_renderer extends moodle_renderer_base {
             throw new coding_exception('The 3rd param to $OUTPUT->confirm must be either a URL (string/moodle_url) or a html_form object.');
         }
 
-        if (empty($continue->button->label)) {
-            $continue->button->label = get_string('yes');
+        if (empty($continue->button->text)) {
+            $continue->button->text = get_string('yes');
         }
-        if (empty($cancel->button->label)) {
-            $cancel->button->label = get_string('no');
+        if (empty($cancel->button->text)) {
+            $cancel->button->text = get_string('no');
         }
 
         $output = $this->box_start('generalbox', 'notice');
@@ -2470,7 +2470,7 @@ class moodle_core_renderer extends moodle_renderer_base {
 
         $buttonattributes = array('class' => $form->button->get_classes_string(),
                                   'type' => 'submit',
-                                  'value' => $form->button->label,
+                                  'value' => $form->button->text,
                                   'disabled' => $form->button->disabled,
                                   'id' => $form->button->id);
 
@@ -2799,7 +2799,7 @@ class moodle_core_renderer extends moodle_renderer_base {
         $html = '';
 
         if (!empty($selectmenu->label)) {
-            $html .= $this->output_tag('label', array('for' => $selectmenu->name), $selectmenu->label);
+            $html .= $this->label($selectmenu->label);
         }
 
         $html .= $this->output_start_tag('select', $attributes) . "\n";
@@ -2810,7 +2810,7 @@ class moodle_core_renderer extends moodle_renderer_base {
                 if (!is_array($values)) {
                     var_dump($values);
                 }
-                foreach ($values as $value => $label) {
+                foreach ($values as $value => $display) {
                     $attributes = array('value' => $value);
 
                     if ((string) $value == (string) $selectmenu->selectedvalue ||
@@ -2820,10 +2820,10 @@ class moodle_core_renderer extends moodle_renderer_base {
 
                     $html .= $this->output_start_tag('option', $attributes);
 
-                    if ($label === '') {
+                    if ($display === '') {
                         $html .= $value;
                     } else {
-                        $html .= $label;
+                        $html .= $display;
                     }
 
                     $html .= $this->output_end_tag('option');
@@ -2831,13 +2831,13 @@ class moodle_core_renderer extends moodle_renderer_base {
                 $html .= $this->output_end_tag('optgroup');
             }
         } else {
-            foreach ($selectmenu->options as $value => $label) {
+            foreach ($selectmenu->options as $value => $display) {
                 $attributes = array('value' => $value);
                 if ((string) $value == (string) $selectmenu->selectedvalue ||
                         (is_array($selectmenu->selectedvalue) && in_array($value, $selectmenu->selectedvalue))) {
                     $attributes['selected'] = 'selected';
                 }
-                $html .= '    ' . $this->output_tag('option', $attributes, s($label)) . "\n";
+                $html .= '    ' . $this->output_tag('option', $attributes, s($display)) . "\n";
             }
         }
         $html .= $this->output_end_tag('select') . "\n";
@@ -2845,7 +2845,16 @@ class moodle_core_renderer extends moodle_renderer_base {
         return $html;
     }
 
-    // TODO choose_from_menu_nested
+    /**
+     * Outputs a <label> element.
+     * @param html_label $label A html_label object
+     * @return HTML fragment
+     */
+    public function label($label) {
+        $label->prepare();
+        $this->prepare_event_handlers($label);
+        return $this->output_tag('label', array('for' => $label->for, 'class' => $label->get_classes_string()), $label->text);
+    }
 
     // TODO choose_from_radio
 
@@ -2948,7 +2957,7 @@ class moodle_core_renderer extends moodle_renderer_base {
         $form = new html_form();
         $form->url = $link;
         $form->values = $link->params();
-        $form->button->label = get_string('continue');
+        $form->button->text = get_string('continue');
         $form->method = 'get';
 
         return $this->output_tag('div', array('class' => 'continuebutton') , $this->button($form));
@@ -3374,7 +3383,7 @@ class moodle_html_component {
  */
 class moodle_select_menu extends moodle_html_component {
     /**
-     * @var array the choices to show in the menu. An array $value => $label.
+     * @var array the choices to show in the menu. An array $value => $display.
      */
     public $options;
     /**
@@ -3383,7 +3392,7 @@ class moodle_select_menu extends moodle_html_component {
      */
     public $name;
     /**
-     * @var string $label The label for that component
+     * @var mixed $label The label for that component. String or html_label object
      */
     public $label;
     /**
@@ -3451,6 +3460,13 @@ class moodle_select_menu extends moodle_html_component {
             $this->nothinglabel = '';
         }
 
+        if (!($this->label instanceof html_label)) {
+            $label = new html_label();
+            $label->text = $this->label;
+            $label->for = $this->name;
+            $this->label = $label;
+        }
+
         $this->add_class('select');
 
         parent::prepare();
@@ -3481,8 +3497,92 @@ class moodle_select_menu extends moodle_html_component {
     public static function make_yes_no($name, $selected) {
         return self::make(array(0 => get_string('no'), 1 => get_string('yes')), $name, $selected);
     }
+
+    /**
+     * This is a shortcut for making an hour selector menu.
+     * @param string $type The type of selector (years, months, days, hours, minutes)
+     * @param string $name fieldname
+     * @param int $currenttime A default timestamp in GMT
+     * @param int $step minute spacing
+     * @return moodle_select_menu A menu initialised with hour options.
+     */
+    public static function make_time_selector($type, $name, $currenttime=0, $step=5) {
+
+        if (!$currenttime) {
+            $currenttime = time();
+        }
+        $currentdate = usergetdate($currenttime);
+        $userdatetype = $type;
+
+        switch ($type) {
+            case 'years':
+                for ($i=1970; $i<=2020; $i++) {
+                    $timeunits[$i] = $i;
+                }
+                $userdatetype = 'year';
+                break;
+            case 'months':
+                for ($i=1; $i<=12; $i++) {
+                    $timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
+                }
+                $userdatetype = 'month';
+                break;
+            case 'days':
+                for ($i=1; $i<=31; $i++) {
+                    $timeunits[$i] = $i;
+                }
+                $userdatetype = 'mday';
+                break;
+            case 'hours':
+                for ($i=0; $i<=23; $i++) {
+                    $timeunits[$i] = sprintf("%02d",$i);
+                }
+                break;
+            case 'minutes':
+                if ($step != 1) {
+                    $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
+                }
+
+                for ($i=0; $i<=59; $i+=$step) {
+                    $timeunits[$i] = sprintf("%02d",$i);
+                }
+                break;
+            default:
+                throw new coding_exception("Time type $type is not supported by moodle_select_menu::make_time_selector().");
+        }
+
+        $timerselector = self::make($timeunits, $name, $currentdate[$userdatetype]);
+        $timerselector->label = new html_label();
+        $timerselector->label->text = get_string(substr($type, -1), 'form');
+        $timerselector->label->for = "menu$timerselector->name";
+        $timerselector->label->add_class('accesshide');
+        $timerselector->nothinglabel = '';
+
+        return $timerselector;
+    }
 }
 
+/**
+ * This class represents how a block appears on a page.
+ *
+ * @copyright 2009 Nicolas Connault
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since     Moodle 2.0
+ */
+class html_label extends moodle_html_component {
+    /**
+     * @var string $text The text to display in the label
+     */
+    public $text;
+    /**
+     * @var string $for The name of the form field this label is associated with
+     */
+    public $for;
+
+    public function prepare() {
+        parent::prepare();
+    }
+}
 
 /**
  * This class represents how a block appears on a page.
@@ -3937,6 +4037,19 @@ class help_icon extends moodle_html_component {
 
         parent::prepare();
     }
+
+    public static function make_scale_menu($courseid, $scale) {
+        $helpbutton = new help_button();
+        $strscales = get_string('scales');
+        $helpbutton->image->alt = $scale->name;
+        $helpbutton->link->url = new moodle_url('/course/scales.php', array('id' => $courseid, 'list' => true, 'scaleid' => $scale->id));
+        $popupaction = new popup_action('click', $helpbutton->url, 'ratingscale', $popupparams);
+        $popupaction->width = 500;
+        $popupaction->height = 400;
+        $helpbutton->link->add_action($popupaction);
+        $helpbutton->link->title = $scale->name;
+        return $helpbutton;
+    }
 }
 
 
@@ -3951,9 +4064,9 @@ class help_icon extends moodle_html_component {
  */
 class html_button extends moodle_html_component {
     /**
-     * @var string $label
+     * @var string $text
      */
-    public $label;
+    public $text;
 
     /**
      * @var boolean $disabled Whether or not this button is disabled
@@ -3967,8 +4080,8 @@ class html_button extends moodle_html_component {
     public function prepare() {
         $this->add_class('singlebutton');
 
-        if (empty($this->label)) {
-            throw new coding_exception('A html_button must have a label value!');
+        if (empty($this->text)) {
+            throw new coding_exception('A html_button must have a text value!');
         }
 
         if ($this->disabled) {
@@ -4291,7 +4404,7 @@ class html_form extends moodle_html_component {
         $this->button = new html_button();
         if (!isset($go)) {
             $go = get_string('go');
-            $this->button->label = $go;
+            $this->button->text = $go;
         }
     }
 
index cbfac083307431f968675f960472a08161820277..23ce3ba2ec2eb266992e4809ef1e070745f2f351 100644 (file)
@@ -3536,125 +3536,6 @@ function navmenulist($course, $sections, $modinfo, $strsection, $strjumpto, $wid
     return implode("\n", $menu);
 }
 
-/**
- * Prints form items with the names $day, $month and $year
- *
- * @param string $day   fieldname
- * @param string $month  fieldname
- * @param string $year  fieldname
- * @param int $currenttime A default timestamp in GMT
- * @param boolean $return If set to true returns rather than echo's
- * @return string|bool Depending on value of $return
- */
-function print_date_selector($day, $month, $year, $currenttime=0, $return=false) {
-
-    if (!$currenttime) {
-        $currenttime = time();
-    }
-    $currentdate = usergetdate($currenttime);
-
-    for ($i=1; $i<=31; $i++) {
-        $days[$i] = $i;
-    }
-    for ($i=1; $i<=12; $i++) {
-        $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
-    }
-    for ($i=1970; $i<=2020; $i++) {
-        $years[$i] = $i;
-    }
-
-    // Build or print result
-    $result='';
-    // Note: There should probably be a fieldset around these fields as they are
-    // clearly grouped. However this causes problems with display. See Mozilla
-    // bug 474415
-    $result.='<label class="accesshide" for="menu'.$day.'">'.get_string('day','form').'</label>';
-    $result.=choose_from_menu($days,   $day,   $currentdate['mday'], '', '', '0', true);
-    $result.='<label class="accesshide" for="menu'.$month.'">'.get_string('month','form').'</label>';
-    $result.=choose_from_menu($months, $month, $currentdate['mon'],  '', '', '0', true);
-    $result.='<label class="accesshide" for="menu'.$year.'">'.get_string('year','form').'</label>';
-    $result.=choose_from_menu($years,  $year,  $currentdate['year'], '', '', '0', true);
-
-    if ($return) {
-        return $result;
-    } else {
-        echo $result;
-    }
-}
-
-/**
- * Prints form items with the names $hour and $minute
- *
- * @param string $hour  fieldname
- * @param string $minute  fieldname
- * @param int $currenttime A default timestamp in GMT
- * @param int $step minute spacing
- * @param boolean $return If set to true returns rather than echo's
- * @return string|bool Depending on value of $return 
- */
-function print_time_selector($hour, $minute, $currenttime=0, $step=5, $return=false) {
-
-    if (!$currenttime) {
-        $currenttime = time();
-    }
-    $currentdate = usergetdate($currenttime);
-    if ($step != 1) {
-        $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
-    }
-    for ($i=0; $i<=23; $i++) {
-        $hours[$i] = sprintf("%02d",$i);
-    }
-    for ($i=0; $i<=59; $i+=$step) {
-        $minutes[$i] = sprintf("%02d",$i);
-    }
-
-    // Build or print result
-    $result='';
-    // Note: There should probably be a fieldset around these fields as they are
-    // clearly grouped. However this causes problems with display. See Mozilla
-    // bug 474415
-    $result.='<label class="accesshide" for="menu'.$hour.'">'.get_string('hour','form').'</label>';
-    $result.=choose_from_menu($hours,   $hour,   $currentdate['hours'],   '','','0',true);
-    $result.='<label class="accesshide" for="menu'.$minute.'">'.get_string('minute','form').'</label>';
-    $result.=choose_from_menu($minutes, $minute, $currentdate['minutes'], '','','0',true);
-
-    if ($return) {
-        return $result;
-    } else {
-        echo $result;
-    }
-}
-
-/**
- * Prints time limit value selector
- *
- * Uses {@link choose_from_menu()} to generate HTML
- * @see choose_from_menu()
- *
- * @global object
- * @param int $timelimit default
- * @param string $unit
- * @param string $name
- * @param boolean $return If set to true returns rather than echo's
- * @return string|bool Depending on value of $return
- */
-function print_timer_selector($timelimit = 0, $unit = '', $name = 'timelimit', $return=false) {
-
-    global $CFG;
-
-    if ($unit) {
-        $unit = ' '.$unit;
-    }
-
-    // Max timelimit is sessiontimeout - 10 minutes.
-    $maxvalue = ($CFG->sessiontimeout / 60) - 10;
-
-    for ($i=1; $i<=$maxvalue; $i++) {
-        $minutes[$i] = $i.$unit;
-    }
-    return choose_from_menu($minutes, $name, $timelimit, get_string('none'), '','','0',$return);
-}
-
 /**
  * Prints a grade menu (as part of an existing form) with help
  * Showing all possible numerical grades and scales
@@ -3701,32 +3582,6 @@ function print_grade_menu($courseid, $name, $current, $includenograde=true, $ret
     }
 }
 
-/**
- * Prints a help button about a scale
- *
- * @global object
- * @param id $courseid 
- * @param object $scale
- * @param boolean $return If set to true returns rather than echo's
- * @return string|bool Depending on value of $return
- */
-function print_scale_menu_helpbutton($courseid, $scale, $return=false) {
-
-    global $OUTPUT;
-
-    $output = '';
-    $strscales = get_string('scales');
-
-    $linkobject = '<span class="helplink"><img class="iconhelp" alt="'.$scale->name.'" src="'.$OUTPUT->old_icon_url('help') . '" /></span>';
-    $output .= link_to_popup_window ('/course/scales.php?id='. $courseid .'&amp;list=true&amp;scaleid='. $scale->id, 'ratingscale',
-                                     $linkobject, 400, 500, $scale->name, 'none', true);
-    if ($return) {
-        return $output;
-    } else {
-        echo $output;
-    }
-}
-
 /**
  * Print an error to STDOUT and exit with a non-zero code. For commandline scripts.
  * Default errorcode is 1.