From 61349c25a0106745fde3a1737956a7ba72658e8c Mon Sep 17 00:00:00 2001 From: Artem Andreev Date: Sat, 30 Apr 2011 17:11:41 +0400 Subject: [PATCH] Implemented button date picker through YUI calendar + some refactoring of renderer.php and locallib.php --- calendar.js | 12 +- lang/en/attforblock.php | 4 +- lang/ru/attforblock.php | 4 +- locallib.php | 254 +++++++++++++++++++++++++--------------- manage.php | 14 ++- renderer.php | 102 ++++++++++++---- styles.css | 4 + 7 files changed, 262 insertions(+), 132 deletions(-) diff --git a/calendar.js b/calendar.js index eb32974..a150f48 100644 --- a/calendar.js +++ b/calendar.js @@ -34,7 +34,7 @@ YAHOO.util.Event.onDOMReady(function(){ dialog = new YAHOO.widget.Dialog("container", { visible:false, context:["show", "tl", "bl"], - buttons:[{text: cal_today, handler: resetHandler, isDefault:true}, {text: cal_close, handler: closeHandler}], + buttons:[{text: M.str.attforblock.caltoday, handler: resetHandler, isDefault:true}, {text: M.str.attforblock.calclose, handler: closeHandler}], draggable:false, close:false }); @@ -60,16 +60,16 @@ YAHOO.util.Event.onDOMReady(function(){ hide_blank_weeks:true // Enable, to demonstrate how we handle changing height, using changeContent }); - calendar.cfg.setProperty("start_weekday", cal_start_weekday); - calendar.cfg.setProperty("MONTHS_LONG", cal_months); - calendar.cfg.setProperty("WEEKDAYS_SHORT", cal_week_days); - calendar.select(new Date(cal_cur_date*1000)); + calendar.cfg.setProperty("start_weekday", M.attforblock.cal_start_weekday); + calendar.cfg.setProperty("MONTHS_LONG", M.attforblock.cal_months); + calendar.cfg.setProperty("WEEKDAYS_SHORT", M.attforblock.cal_week_days); + calendar.select(new Date(M.attforblock.cal_cur_date*1000)); calendar.render(); calendar.selectEvent.subscribe(function() { if (calendar.getSelectedDates().length > 0) { - Dom.get("current").value = calendar.getSelectedDates()[0] / 1000; + Dom.get("curdate").value = calendar.getSelectedDates()[0] / 1000; Dom.get("currentdate").submit(); } diff --git a/lang/en/attforblock.php b/lang/en/attforblock.php index 21f7d03..1abcfaf 100644 --- a/lang/en/attforblock.php +++ b/lang/en/attforblock.php @@ -34,10 +34,10 @@ $string['attforblock:view'] = 'Viewing Attendances'; $string['attforblock:viewreports'] = 'Viewing Reports'; $string['attrecords'] = 'Attendances records'; $string['calclose'] = 'Close'; -$string['calmonths'] = '\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"'; +$string['calmonths'] = 'January,February,March,April,May,June,July,August,September,October,November,December'; $string['calshow'] = 'Choose date'; $string['caltoday'] = 'Today'; -$string['calweekdays'] = ' \"Su\", \"Mo\", \"Tu\", \"We\", \"Th\", \"Fr\", \"Sa\"'; +$string['calweekdays'] = 'Su,Mo,Tu,We,Th,Fr,Sa'; $string['changeattendance'] = 'Change attendance'; $string['changeduration'] = 'Change duration'; $string['changesession'] = 'Change session'; diff --git a/lang/ru/attforblock.php b/lang/ru/attforblock.php index 407c29e..25c48bd 100644 --- a/lang/ru/attforblock.php +++ b/lang/ru/attforblock.php @@ -32,10 +32,10 @@ $string['attforblock:view'] = 'Просмотр посещаемости'; $string['attforblock:viewreports'] = 'Просмотр отчетов'; $string['attrecords'] = 'Отметок о посещаемости'; $string['calclose'] = 'Закрыть'; -$string['calmonths'] = '\"Январь\", \"Февраль\", \"Март\", \"Апрель\", \"Май\", \"Июнь\", \"Июль\", \"Август\", \"Сентябрь\", \"Октябрь\", \"Ноябрь\", \"Декабрь\"'; +$string['calmonths'] = 'Январь,Февраль,Март,Апрель,Май,Июнь,Июль,Август,Сентябрь,Октябрь,Ноябрь,Декабрь'; $string['calshow'] = 'Выбрать дату'; $string['caltoday'] = 'Сегодня'; -$string['calweekdays'] = ' \"Вс\", \"Пн\", \"Вт\", \"Ср\", \"Чт\", \"Пт\", \"Сб\"'; +$string['calweekdays'] = 'Вс,Пн,Вт,Ср,Чт,Пт,Сб'; $string['changeattendance'] = 'Изменить посещаемость'; $string['changeduration'] = 'Изменить продолжительность'; $string['changesession'] = 'Изменить занятие'; diff --git a/locallib.php b/locallib.php index 867e04c..fbc6847 100644 --- a/locallib.php +++ b/locallib.php @@ -76,10 +76,7 @@ class attforblock_permissions { } } -class attforblock { - const SESSION_COMMON = 1; - const SESSION_GROUP = 2; - +class attforblock_view_params { const VIEW_DAYS = 1; const VIEW_WEEKS = 2; const VIEW_MONTHS = 3; @@ -93,34 +90,15 @@ class attforblock { const SORTED_LIST = 1; const SORTED_GRID = 2; - const DEFAULT_VIEW = attforblock::VIEW_WEEKS; - const DEFAULT_CURDATE = 0; - const DEFAULT_VIEW_TAKE = attforblock::SORTED_LIST; + const DEFAULT_VIEW = self::VIEW_WEEKS; + const DEFAULT_VIEW_TAKE = self::SORTED_LIST; const DEFAULT_SHOWENDTIME = 0; - /** @var stdclass course module record */ - public $cm; - - /** @var stdclass course record */ - public $course; - - /** @var stdclass context object */ - public $context; - - /** @var int attendance instance identifier */ - public $id; - - /** @var string attendance activity name */ - public $name; - - /** @var float number (10, 5) unsigned, the maximum grade for attendance */ - public $grade; - /** @var int current view mode */ - public $view = self::DEFAULT_VIEW; + public $view; /** @var int $view and $curdate specify displaed date range */ - public $curdate = self::DEFAULT_CURDATE; + public $curdate; /** @var int start date of displayed date range */ public $startdate; @@ -129,65 +107,68 @@ class attforblock { public $enddate; /** @var int view mode of taking attendance page*/ - public $view_take = self::DEFAULT_VIEW_TAKE; + public $view_take; /** @var int whether sessions end time will be displayed on manage.php */ - public $showendtime = self::DEFAULT_SHOWENDTIME; + public $show_endtime; - /** @var attforblock_permissions permission of current user for attendance instance*/ - public $perm; - /** - * Initializes the attendance API instance using the data from DB - * - * Makes deep copy of all passed records properties. Replaces integer $course attribute - * with a full database record (course should not be stored in instances table anyway). - * - * @param stdClass $dbrecord Attandance instance data from {attforblock} table - * @param stdClass $cm Course module record as returned by {@link get_coursemodule_from_id()} - * @param stdClass $course Course record from {course} table - * @param stdClass $context The context of the workshop instance - */ - public function __construct(stdclass $dbrecord, stdclass $cm, stdclass $course, stdclass $context=null) { - foreach ($dbrecord as $field => $value) { - if (property_exists('attforblock', $field)) { - $this->{$field} = $value; - } - else { - throw new coding_exception('The attendance table has field for which there is no property in the attforblock class'); - } - } - $this->cm = $cm; - $this->course = $course; - if (is_null($context)) { - $this->context = get_context_instance(CONTEXT_MODULE, $this->cm->id); - } else { - $this->context = $context; - } + public $students_sort; - $this->perm = new attforblock_permissions($this->context); + public $student_id; + + private $courseid; + + public function init_defaults($courseid) { + $this->view = self::DEFAULT_VIEW; + $this->curdate = time(); + $this->view_take = self::DEFAULT_VIEW_TAKE; + $this->show_endtime = self::DEFAULT_SHOWENDTIME; + + $this->courseid = $courseid; } - public function init_view_params($view=NULL, $curdate=NULL, $view_take=NULL, $showendtime=NULL) { + public function init(attforblock_view_params $view_params) { + $this->init_view($view_params->view); + + $this->init_curdate($view_params->curdate); + + $this->init_view_take($view_params->view_take); + + $this->init_show_endtime($view_params->show_endtime); + + $this->students_sort = $view_params->students_sort; + + $this->student_id = $view_params->student_id; + + $this->init_start_end_date(); + } + + private function init_view($view) { global $SESSION; if (isset($view)) { - $SESSION->attcurrentattview[$this->course->id] = $view; + $SESSION->attcurrentattview[$this->courseid] = $view; $this->view = $view; } - elseif (isset($SESSION->attcurrentattview[$this->course->id])) { - $this->view = $SESSION->attcurrentattview[$this->course->id]; + elseif (isset($SESSION->attcurrentattview[$this->courseid])) { + $this->view = $SESSION->attcurrentattview[$this->courseid]; } + } + + private function init_curdate($curdate) { + global $SESSION; if ($curdate) { - $SESSION->attcurrentattdate[$this->course->id] = $curdate; + $SESSION->attcurrentattdate[$this->courseid] = $curdate; $this->curdate = $curdate; } - elseif (isset($SESSION->attcurrentattdate[$this->course->id])) { - $this->curdate = $SESSION->attcurrentattdate[$this->course->id]; - } - else { - $this->curdate = time(); + elseif (isset($SESSION->attcurrentattdate[$this->courseid])) { + $this->curdate = $SESSION->attcurrentattdate[$this->courseid]; } + } + + private function init_view_take($view_take) { + global $SESSION; if (isset($view_take)) { set_user_preference("attforblock_view_take", $view_take); @@ -196,16 +177,18 @@ class attforblock { else { $this->view_take = get_user_preferences("attforblock_view_take", $this->view_take); } + } + + private function init_show_endtime($show_endtime) { + global $SESSION; - if (isset($showendtime)) { - set_user_preference("attforblock_showendtime", $showendtime); - $this->showendtime = $showendtime; + if (isset($show_endtime)) { + set_user_preference("attforblock_showendtime", $show_endtime); + $this->show_endtime = $show_endtime; } else { - $this->showendtime = get_user_preferences("attforblock_showendtime", $this->showendtime); + $this->show_endtime = get_user_preferences("attforblock_showendtime", $this->show_endtime); } - - $this->init_start_end_date(); } private function init_start_end_date() { @@ -238,6 +221,68 @@ class attforblock { break; } } +} + +class attforblock { + const SESSION_COMMON = 1; + const SESSION_GROUP = 2; + + /** @var stdclass course module record */ + public $cm; + + /** @var stdclass course record */ + public $course; + + /** @var stdclass context object */ + public $context; + + /** @var int attendance instance identifier */ + public $id; + + /** @var string attendance activity name */ + public $name; + + /** @var float number (10, 5) unsigned, the maximum grade for attendance */ + public $grade; + + /** @var attforblock_view_params view parameters current attendance instance*/ + public $view_params; + + /** @var attforblock_permissions permission of current user for attendance instance*/ + public $perm; + /** + * Initializes the attendance API instance using the data from DB + * + * Makes deep copy of all passed records properties. Replaces integer $course attribute + * with a full database record (course should not be stored in instances table anyway). + * + * @param stdClass $dbrecord Attandance instance data from {attforblock} table + * @param stdClass $cm Course module record as returned by {@link get_coursemodule_from_id()} + * @param stdClass $course Course record from {course} table + * @param stdClass $context The context of the workshop instance + */ + public function __construct(stdclass $dbrecord, stdclass $cm, stdclass $course, stdclass $context=null) { + foreach ($dbrecord as $field => $value) { + if (property_exists('attforblock', $field)) { + $this->{$field} = $value; + } + else { + throw new coding_exception('The attendance table has field for which there is no property in the attforblock class'); + } + } + $this->cm = $cm; + $this->course = $course; + if (is_null($context)) { + $this->context = get_context_instance(CONTEXT_MODULE, $this->cm->id); + } else { + $this->context = $context; + } + + $this->view_params = new attforblock_view_params(); + $this->view_params->init_defaults($this->course->id); + + $this->perm = new attforblock_permissions($this->context); + } /** * Returns today sessions for this attendance @@ -403,49 +448,74 @@ class attforblock_filter_controls implements renderable { /** @var int current view mode */ public $view; + public $curdate; + public $prevcur; public $nextcur; public $curdatetxt; + private $url_path; + private $url_params; + private $att; public function __construct(attforblock $att) { - $this->view = $att->view; + global $PAGE; + + $this->view = $att->view_params->view; + + $this->curdate = $att->view_params->curdate; - $date = usergetdate($att->curdate); + $date = usergetdate($att->view_params->curdate); $mday = $date['mday']; $wday = $date['wday']; $mon = $date['mon']; $year = $date['year']; switch ($this->view) { - case attforblock::VIEW_DAYS: + case attforblock_view_params::VIEW_DAYS: $format = get_string('strftimedm', 'attforblock'); $this->prevcur = make_timestamp($year, $mon, $mday - 1); $this->nextcur = make_timestamp($year, $mon, $mday + 1); - $this->curdatetxt = userdate($att->startdate, $format); + $this->curdatetxt = userdate($att->view_params->startdate, $format); break; - case attforblock::VIEW_WEEKS: + case attforblock_view_params::VIEW_WEEKS: $format = get_string('strftimedm', 'attforblock'); - $this->prevcur = $att->startdate - WEEKSECS; - $this->nextcur = $att->startdate + WEEKSECS; - $this->curdatetxt = userdate($att->startdate, $format)." - ".userdate($att->enddate, $format); + $this->prevcur = $att->view_params->startdate - WEEKSECS; + $this->nextcur = $att->view_params->startdate + WEEKSECS; + $this->curdatetxt = userdate($att->view_params->startdate, $format)." - ".userdate($att->view_params->enddate, $format); break; - case attforblock::VIEW_MONTHS: + case attforblock_view_params::VIEW_MONTHS: $format = '%B'; $this->prevcur = make_timestamp($year, $mon - 1); $this->nextcur = make_timestamp($year, $mon + 1); - $this->curdatetxt = userdate($att->startdate, $format); + $this->curdatetxt = userdate($att->view_params->startdate, $format); break; } + $this->url_path = $PAGE->url->out_omit_querystring(); + $params = array('id' => $att->cm->id); + if (isset($att->view_params->students_sort)) $params['sort'] = $att->view_params->students_sort; + if (isset($att->view_params->student_id)) $params['studentid'] = $att->view_params->student_id; + $this->url_params = $params; + $this->att = $att; } - public function url($params=NULL) { - global $PAGE; - - return new moodle_url($PAGE->url, $params); + public function url($params=array()) { + $params = array_merge($this->url_params, $params); + + return new moodle_url($this->url_path, $params); + } + + public function url_path() { + return $this->url_path; + } + + public function url_params($params=array()) { + $params = array_merge($this->url_params, $params); + + return $params; } } @@ -488,10 +558,10 @@ class attforblock_sessions_manage_data implements renderable { $this->perm = $att->perm; - $this->showendtime = $att->showendtime; + $this->showendtime = $att->view_params->show_endtime; - $this->startdate = $att->startdate; - $this->enddate = $att->enddate; + $this->startdate = $att->view_params->startdate; + $this->enddate = $att->view_params->enddate; if ($this->startdate && $this->enddate) { $where = "courseid=:cid AND attendanceid = :aid AND sessdate >= :csdate AND sessdate >= :sdate AND sessdate < :edate"; diff --git a/manage.php b/manage.php index 88a7284..c68b70f 100644 --- a/manage.php +++ b/manage.php @@ -12,11 +12,13 @@ require_once(dirname(__FILE__).'/../../config.php'); require_once(dirname(__FILE__).'/locallib.php'); -$id = optional_param('id', 0, PARAM_INT); // Course Module ID, or -$from = optional_param('from', NULL, PARAM_ACTION); -$view = optional_param('view', NULL, PARAM_INT); // which page to show -$curdate = optional_param('curdate', 0, PARAM_INT); -$showendtime = optional_param('showendtime', NULL, PARAM_INT); +$view_params = new attforblock_view_params(); + +$id = optional_param('id', 0, PARAM_INT); // Course Module ID, or +$from = optional_param('from', NULL, PARAM_ACTION); +$view_params ->view = optional_param('view', NULL, PARAM_INT); // which page to show +$view_params ->curdate = optional_param('curdate', NULL, PARAM_INT); +$view_params ->showendtime = optional_param('showendtime', NULL, PARAM_INT); $cm = get_coursemodule_from_id('attforblock', $id, 0, false, MUST_EXIST); $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); @@ -31,7 +33,7 @@ if ($canmanage && $cantake && $canchange) redirect("view.php?id=$cm->id"); $att = new attforblock($att, $cm, $course, $PAGE->context); -$att->init_view_params($view, $curdate, NULL, $showendtime); +$att->view_params->init($view_params); // if teacher is coming from block, then check for a session exists for today if ($from === 'block') { diff --git a/renderer.php b/renderer.php index 9a8794f..6061a5f 100644 --- a/renderer.php +++ b/renderer.php @@ -47,20 +47,68 @@ class mod_attforblock_renderer extends plugin_renderer_base { $filtertable->data[0][] = ''; - $curdatecontrols = ''; + $filtertable->data[0][] = $this->render_curdate_controls($fcontrols); + + $filtertable->data[0][] = $this->render_view_controls($fcontrols); + + $o = html_writer::table($filtertable); + $o = $this->output->container($o, 'attfiltercontrols attwidth'); + + return $o; + } + + private function render_curdate_controls(attforblock_filter_controls $fcontrols) { + global $CFG; + + $curdate_controls = ''; if ($fcontrols->curdatetxt) { - $curdatecontrols = html_writer::link($fcontrols->url(array('curdate' => $fcontrols->prevcur)), $this->output->larrow()); - $curdatecontrols .= $fcontrols->curdatetxt; - $curdatecontrols .= html_writer::link($fcontrols->url(array('curdate' => $fcontrols->nextcur)), $this->output->rarrow()); - //plug_yui_calendar($current); + $this->page->requires->strings_for_js(array('calclose', 'caltoday'), 'attforblock'); + $jsvals = array( + 'cal_months' => explode(',', get_string('calmonths','attforblock')), + 'cal_week_days' => explode(',', get_string('calweekdays','attforblock')), + 'cal_start_weekday' => $CFG->calendar_startwday, + 'cal_cur_date' => $fcontrols->curdate); + $curdate_controls = html_writer::script(js_writer::set_variable('M.attforblock', $jsvals)); + + $this->page->requires->yui2_lib('container'); + $this->page->requires->yui2_lib('calendar'); + $this->page->requires->js('/mod/attforblock/calendar.js'); + + $curdate_controls .= html_writer::link($fcontrols->url(array('curdate' => $fcontrols->prevcur)), $this->output->larrow()); + $params = array( + 'title' => get_string('calshow', 'attforblock'), + 'id' => 'show', + 'type' => 'button'); + $button_form = html_writer::tag('button', $fcontrols->curdatetxt, $params); + foreach ($fcontrols->url_params(array('curdate' => '')) as $name => $value) { + $params = array( + 'type' => 'hidden', + 'id' => $name, + 'name' => $name, + 'value' => $value); + $button_form .= html_writer::empty_tag('input', $params); + } + $params = array( + 'id' => 'currentdate', + 'action' => $fcontrols->url_path(), + 'method' => 'post' + ); + + $button_form = html_writer::tag('form', $button_form, $params); + $curdate_controls .= $button_form; + + $curdate_controls .= html_writer::link($fcontrols->url(array('curdate' => $fcontrols->nextcur)), $this->output->rarrow()); } - $filtertable->data[0][] = $curdatecontrols; - $views[attforblock::VIEW_ALL] = get_string('all', 'attforblock'); - $views[attforblock::VIEW_ALLTAKEN] = get_string('alltaken', 'attforblock'); - $views[attforblock::VIEW_MONTHS] = get_string('months', 'attforblock'); - $views[attforblock::VIEW_WEEKS] = get_string('weeks', 'attforblock'); - $views[attforblock::VIEW_DAYS] = get_string('days', 'attforblock'); + return $curdate_controls; + } + + private function render_view_controls(attforblock_filter_controls $fcontrols) { + $views[attforblock_view_params::VIEW_ALL] = get_string('all', 'attforblock'); + $views[attforblock_view_params::VIEW_ALLTAKEN] = get_string('alltaken', 'attforblock'); + $views[attforblock_view_params::VIEW_MONTHS] = get_string('months', 'attforblock'); + $views[attforblock_view_params::VIEW_WEEKS] = get_string('weeks', 'attforblock'); + $views[attforblock_view_params::VIEW_DAYS] = get_string('days', 'attforblock'); $viewcontrols = ''; foreach ($views as $key => $sview) { if ($key != $fcontrols->view) { @@ -70,14 +118,10 @@ class mod_attforblock_renderer extends plugin_renderer_base { else $viewcontrols .= html_writer::tag('span', $sview, array('class' => 'attcurbtn')); } - $filtertable->data[0][] = html_writer::tag('nobr', $viewcontrols); - $o = html_writer::table($filtertable); - $o = $this->output->container($o, 'attfiltercontrols attwidth'); - - return $o; + return html_writer::tag('nobr', $viewcontrols); } - + /** * Renders attendance sessions managing table * @@ -85,6 +129,16 @@ class mod_attforblock_renderer extends plugin_renderer_base { * @return string html code */ protected function render_attforblock_sessions_manage_data(attforblock_sessions_manage_data $sessdata) { + + $o = $this->render_sess_manage_table($sessdata) . $this->render_sess_control_table($sessdata); + $o = html_writer::tag('form', $o, array('method' => 'post', 'action' => $sessdata->url_sessions()->out())); + $o = $this->output->container($o, 'generalbox attwidth'); + $o = $this->output->container($o, 'attsessions_manage_table'); + + return $o; + } + + private function render_sess_manage_table(attforblock_sessions_manage_data $sessdata) { $sesstable = new html_table(); $sesstable->width = '100%'; $sesstable->head = array( @@ -149,6 +203,10 @@ class mod_attforblock_renderer extends plugin_renderer_base { $sesstable->data[$sess->id][] = html_writer::checkbox('sessid', $sess->id, false); } + return html_writer::table($sesstable); + } + + private function render_sess_control_table(attforblock_sessions_manage_data $sessdata) { $controltable = new html_table(); $controltable->attributes['class'] = ' '; $controltable->width = '100%'; @@ -156,7 +214,7 @@ class mod_attforblock_renderer extends plugin_renderer_base { $controltable->data[0][] = $this->output->help_icon('hiddensessions', 'attforblock', get_string('hiddensessions', 'attforblock').': '.$sessdata->hiddensessionscount); - + $controls = html_writer::link('javascript:checkall();', get_string('selectall')).' / '. html_writer::link('javascript:checknone();', get_string('deselectall')). html_writer::empty_tag('br'); @@ -174,12 +232,8 @@ class mod_attforblock_renderer extends plugin_renderer_base { } $controltable->data[0][] = $controls; - $o = html_writer::table($sesstable) . html_writer::table($controltable); - $o = html_writer::tag('form', $o, array('method' => 'post', 'action' => $sessdata->url_sessions()->out())); - $o = $this->output->container($o, 'generalbox attwidth'); - $o = $this->output->container($o, 'attsessions_manage_table'); - - return $o; + return html_writer::table($controltable); } + } ?> diff --git a/styles.css b/styles.css index bd21cd9..1064dea 100644 --- a/styles.css +++ b/styles.css @@ -22,6 +22,10 @@ margin-left:auto; } +.path-mod-attforblock .attfiltercontrols #currentdate{ + display: inline; +} + .path-mod-attforblock .attwidth { width:90%; margin: auto;