KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Teacher Schedule report #2247

Closed Ron opened this topic on on Aug 6, 2021 - 6 comments

Ron commented on Aug 6, 2021

Hi, I have a report the is showing a teacher weekly schedule. in some of the cases the teachers are not working in all days so in order to include the missing days column I use the following process which you recommended

->pipe(new \koolreport\processes\Map(array(
                "{end}" => function($count, $mapState) {
                    $emptyRows = [];
                    $days = [1, 2, 3, 4, 5, 6]; //use day name if it's your case
                    $emptyRows[] = ['day' => $key, 'hour_num' => $h_key];
                    return $emptyRows;
                }
            )))

I use Cube to make the days as columns and hour_num as row as follow:

->pipe(new Cube(array(
    'row' => 'hour_num',
    'column' => 'day',
    'max' => 'class_name'
)))

The issue starts when a teacher does not have scheduled hours in a specific day or more and the process above add it as empty rows but in the end of the report. for instance Teacher works on Monday, Tuesday, Wednesday and Friday, On Sunday and Tuesday he is not scheduled. in the report with the above code it will show

Monday | Tuesday | Wednesday | Friday | Sunday | Thursday

I need it to be shown as the correct weekdays! btw, I used Sort process after the cube process but does not solve the problem.

Sebastian Morales commented on Aug 6, 2021

I think there's a process called koolreport\processes\ColumnsSort. You could apply it after the Cube process:

    ->pipe(new Cube(...))
    ->pipe(new koolreport\processes\ColumnsSort(array(
        "{name}" => "asc", //or "desc" or function($colName1, $colName2) {...}
    )))

If your day column names are 1, 2, ... then "asc" or "desc" is enough. If they are like "Monday", "Tuesday", ... you would need to use a compare function. Rgds,

Ron commented on Aug 8, 2021

Its not working. it give the same result!

Ron commented on Aug 8, 2021

this is the current updated code

$this->src("db")
        ->query('CALL getTeacherSchedule(:year, :teacher_id)')
        ->params(array(
            ':year' => $this->params['year'],
            ':teacher_id' => $this->params['teacher_id']
        ))
        ->saveTo($node1);

        foreach ($this->teachersList as $teacher) {
            $node1->pipe( new Filter( array (
                array("teacher_id", "=", $teacher['id']),
                "or",
                array("teacher_id","findInSet",$teacher['id']),
            )))
            ->pipe(new \koolreport\processes\Map(array(
                "{value}" => function($row) {
                    $row['class_name'] =
                        //$row['class_name'].'<br>'.
                        ( $this->params['show_class'] != null ? $row['class_name'] != '' ? $row['class_name'] : lang('tts.no_class') : null ).'<br>'.
                        ( $this->params['show_profession'] != null ? $row['profession_name'] : null ).'<br>'.
                        ( $this->params['show_room'] != null ? $row['room_name'] : null );
                    if ( $this->params['teacher_id'] != 0 ) {
                        $this->teachersList[0]['teacher_name'] = $row['teacher_name'];
                    }
                    return $row;
                },
            )))
            ->pipe(new \koolreport\processes\Map(array(
                "{end}" => function($count, $mapState) {
                    $emptyRows = [];
                    //$days = [1, 2, 3, 4, 5, 6]; //use day name if it's your case
                    foreach ($_SESSION['settings']->allowed_days as $key => $day) {
                        if ($day == true) {
                            foreach ($_SESSION['settings']->allowed_hours as $h_key => $hour) {
                                if ($hour == true) {
                                    $emptyRows[] = ['day' => $key, 'hour_num' => $h_key];
                                }
                            }
                        }
                    }
                    return $emptyRows;
                }
            )))
            ->pipe(new Cube(array(
                'row' => 'hour_num',
                'column' => 'day',
                'max' => 'class_name'
            )))
            ->pipe(new koolreport\processes\ColumnsSort(array(
                "{day}" => "asc", //or "desc" or function($colName1, $colName2) {...}
            )))
            ->pipe(new \koolreport\processes\Map(array(
                "{value}" => function($row) {
                    foreach ($row as $k => $v)
                        if ($v == null) $row[$k] = "";
                    return $row;
                }
            )))
            ->pipe(new \koolreport\processes\RemoveColumn(array(
                '{{all}}','{{others}}'
            )))
            ->pipe(new \koolreport\processes\Map(array(
                "{meta}" => function($meta) {
                    $colMetas = $meta["columns"];
                    foreach ($colMetas as $colKey => $colMeta) {
                        if (is_numeric($colKey)) {
                            $colMetas[$colKey]['label'] = lang("tts.day_$colKey"); //for columns '1', '2', etc
                            $colMetas[$colKey]['cssStyle'] = "width: 120px";
                        }
                        else {
                            $colMetas[$colKey]['label'] = lang("tts.$colKey"); //for column 'hour_num'
                            $colMetas[$colKey]['cssStyle'] = "width: 80px";
                        }
                    }
                    $meta["columns"] = $colMetas;
                    return $meta;
                }
            )))
            ->pipe($this->dataStore("DS_" . $teacher['id']));
        }
Sebastian Morales commented on Aug 9, 2021

This code of yours is wrong:

            ->pipe(new koolreport\processes\ColumnsSort(array(
                "{day}" => "asc", //or "desc" or function($colName1, $colName2) {...}
            )))

It should be exactly like I posted:

    ->pipe(new koolreport\processes\ColumnsSort(array(
        "{name}" => "asc", //or "desc" or function($colName1, $colName2) {...}
    )))

It means we sort all columns by their names. "{day}" is meaningless in this case.

Ron commented on Aug 9, 2021

ok. I fixed the code and it sorted the columns. the issue is the the first column is hour_num and after that i have 6 columns 1,2,3,4,5,6 each column represent a day starts from 1=Sunday etc. now after columnssort process it fixes the days but moves the first column name hour num to the end after Friday. please see screenshot

Ron commented on Aug 9, 2021

Issue solved:

->pipe(new koolreport\processes\ColumnsSort(array(
    "{name}" => function($colName1, $colName2)
    {
        return $colName1>$colName2;
    }
)))

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None