Skip to content

moodlehq/refactoring-interview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Moodle refactoring interview

A small widget provided to potential candidates to refactor and improve.

composer install

./vendor/bin/phpunit tests/* for tests.


Explanation of the current state

This is a legacy widget that you have inherited which caught the eye of an interested manager.

The interested manager wants it updated and ready for production asap so any changes shouldn't take more than half a day to complete.

The structure consists of the following files:

lib.php         - The functions used by the various entry points from the command line
data.json       - The provided json that the widget gets from the school as a data source
courses.php     - The entry point that allows the management of courses
users.php       - Manage and update users
school.php      - Display the entire school structure
tests/          - Directory containing the tests for the widget
    quick_test.php    - The initial developer wanted to state that their project had unit tests however, unfortunately they are not meaningful 


Main task:

Example data.json file

Users and courses are stored in a JSON file. The JSON file has the following structure:

{
  "users": [
    {"name": "Abed Nadir", "email": "[email protected]", "classes": [{"id": 1}, {"id": 2}]}
  ],
  "courses": [
    {"id": 1, "name": "Biology 101"}
  ]
}

Enrolments are currently stored in the users array item object. If you were to provide the school with the rational to normalise their data, they might be willing to update their processes.

The coursesarray currently contains each course with their associated ID and name. The school has indicated that they would like add class locations and times in the future.

Running the code

Assuming PHP code is in school.php, you could run it by this command:

> php school.php 
stdClass Object
(
    [1] => stdClass Object
        (
            [name] => Biology 101
            [students] => Array
                (
                    [0] => Abed Nadir: [email protected]
                    [1] => Lex Williams:
                )

            [teachers] => Walter White:
        )

    [2] => stdClass Object
        (
            [name] => Chemistry 101
            [students] => Array
                (
                    [0] => Abed Nadir: [email protected]
                    [1] => Jessie Pinkman:
                )

            [teachers] => Walter White:
        )

)

Assuming PHP code is in courses.php, you could run it by this command:

> php courses.php -a
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Biology 101
            [location] => Greendale Community College
        )

    [1] => Array
        (
            [id] => 2
            [name] => Chemistry 101
        )

)

Assuming PHP code is in users.php, you could run it by this command:

> php users.php -a
Array
(
    [0] => Array
        (
            [name] => Abed Nadir
            [email] => [email protected]
            [courseinfo] => Array
                (
                    [0] => Biology 101: Greendale Community College
                    [1] => Chemistry 101:
                )

        )
    [1] => Array
        (
            [name] => Walter White
            [role] => Teacher
            [courseinfo] => Array
                (
                    [0] => Biology 101: Greendale Community College
                    [1] => Chemistry 101:
                )

        )
)

Notes about this code

  1. PHP 8.2 is the target version, and you can use any features from it
  2. Run time changes do not need to be saved or written back to the data source
  3. Backwards compatibility is not an issue so functions can be removed and updated as desired

Requirements for your code

  1. Improve the code hygiene of the widget and provide your insight on any areas of concern
  2. Refactor the lib.php file to have clearer representations of the functionality. This can take the shape of implementing a design pattern
    1. Re-implement the course and user CRUD functions to be single purpose discreet functions
  3. Provide unit tests for the new functions
  4. Changes should be commited and a pull request should be made to this repository