0
0

【ChatGPT】CakePHP で e ラーニングシステムを bake するコマンドを聞いてみた

Last updated at Posted at 2024-05-30

CakePHP には
データベースの内容から
プログラムを生成できる
bake という機能があります。

今回は ChatGPT で
e ラーニングシステムを
bake するコマンドを聞いてみました。

ChatGPT と bake を組み合わせれば
e ラーニングシステムが簡単に作れる事を
かなり期待しています。

対戦よろしくお願いします。


ChatGPT に聞いた質問文

個人的に思っていることですが
ChatGPT は英語で聞いた方が
良い回答を得やすいと思っています。

恐らく日本語で聞いた場合は
内部で英語に変換しているのではないか
と勝手に思っているため
初めから Google 翻訳でしっくりくる英語を
入力した方が良い回答を
得やすいのではないかという勝手なイメージです。

そこで ChatGPT に以下の質問をしてみました。

質問文
I have a request.
Could you please tell me the command to bake an e-learning system with CakePHP?
I can't imagine the contents of the bake command at all, so I would appreciate it if you could suggest a bake command that can implement all the functions you think are necessary.
I would appreciate it if you could also suggest the SQL that outputs the table.
I would be happy if you could let me know.

上司が有料版を持っていますが
今回は無料版で聞いています。

基本的に ChatGPT で質問する際は
下手に出た方が良いと勝手に思っていますので
少しだけ丁寧な文章を意識しています。

上司は有料版を持っていますが
両者攻撃的な会話になっているイメージなので
良い回答を貰えていない印象があるためです。

上記の文章の日本語訳は以下の通りです。

質問文を Google 翻訳で
お願いがあります。
CakePHPでeラーニングシステムをbakeするコマンドを教えていただけませんか?
bakeコマンドの内容が全く想像できないので、必要と思われる機能をすべて実装できるbakeコマンドを提案していただけるとありがたいです。
テーブルを出力するSQLも提案していただけるとありがたいです。
教えていただけると嬉しいです。

お待ちかねの ChatGPT の回答は
以下の通りです。

ChatGPT の回答
Creating an e-learning system with CakePHP involves multiple steps, including designing your database schema, setting up your models, controllers, and views. The bake command in CakePHP can help streamline this process by generating the necessary files based on your database schema.

Here's a general outline to get you started:

1. Database Schema
Let's assume we need the following tables for an e-learning system:

users: Stores user information (students, teachers, admin).
courses: Contains course information.
lessons: Stores lesson details for each course.
enrollments: Tracks which users are enrolled in which courses.
assignments: Contains assignment details.
submissions: Stores users' assignment submissions.
2. SQL to Create Tables
Here's an example SQL schema for these tables:

sql
コードをコピーする
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    role ENUM('student', 'teacher', 'admin') NOT NULL,
    created DATETIME,
    modified DATETIME
);

CREATE TABLE courses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    teacher_id INT,
    created DATETIME,
    modified DATETIME,
    FOREIGN KEY (teacher_id) REFERENCES users(id)
);

CREATE TABLE lessons (
    id INT AUTO_INCREMENT PRIMARY KEY,
    course_id INT,
    title VARCHAR(255) NOT NULL,
    content TEXT,
    created DATETIME,
    modified DATETIME,
    FOREIGN KEY (course_id) REFERENCES courses(id)
);

CREATE TABLE enrollments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    course_id INT,
    created DATETIME,
    modified DATETIME,
    FOREIGN KEY (user_id) REFERENCES users(id),
    FOREIGN KEY (course_id) REFERENCES courses(id)
);

CREATE TABLE assignments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    lesson_id INT,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    due_date DATETIME,
    created DATETIME,
    modified DATETIME,
    FOREIGN KEY (lesson_id) REFERENCES lessons(id)
);

CREATE TABLE submissions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    assignment_id INT,
    user_id INT,
    content TEXT,
    created DATETIME,
    modified DATETIME,
    FOREIGN KEY (assignment_id) REFERENCES assignments(id),
    FOREIGN KEY (user_id) REFERENCES users(id)
);
3. Bake Command
Once you have your database schema, you can use the bake command to generate models, controllers, and views for each table. Here are the commands for each table:

bash
コードをコピーする
# Baking Users
bin/cake bake all users

# Baking Courses
bin/cake bake all courses

# Baking Lessons
bin/cake bake all lessons

# Baking Enrollments
bin/cake bake all enrollments

# Baking Assignments
bin/cake bake all assignments

# Baking Submissions
bin/cake bake all submissions
4. Configuring Relationships
After baking the initial files, you will need to configure the relationships between the models. For example:

User model (src/Model/Table/UsersTable.php):
php
コードをコピーする
public function initialize(array $config): void
{
    parent::initialize($config);
    $this->hasMany('Courses', [
        'foreignKey' => 'teacher_id'
    ]);
    $this->hasMany('Enrollments');
    $this->hasMany('Submissions');
}
Course model (src/Model/Table/CoursesTable.php):
php
コードをコピーする
public function initialize(array $config): void
{
    parent::initialize($config);
    $this->belongsTo('Users', [
        'foreignKey' => 'teacher_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('Lessons');
    $this->hasMany('Enrollments');
}
Lesson model (src/Model/Table/LessonsTable.php):
php
コードをコピーする
public function initialize(array $config): void
{
    parent::initialize($config);
    $this->belongsTo('Courses', [
        'foreignKey' => 'course_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('Assignments');
}
Enrollment model (src/Model/Table/EnrollmentsTable.php):
php
コードをコピーする
public function initialize(array $config): void
{
    parent::initialize($config);
    $this->belongsTo('Users');
    $this->belongsTo('Courses');
}
Assignment model (src/Model/Table/AssignmentsTable.php):
php
コードをコピーする
public function initialize(array $config): void
{
    parent::initialize($config);
    $this->belongsTo('Lessons', [
        'foreignKey' => 'lesson_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('Submissions');
}
Submission model (src/Model/Table/SubmissionsTable.php):
php
コードをコピーする
public function initialize(array $config): void
{
    parent::initialize($config);
    $this->belongsTo('Assignments');
    $this->belongsTo('Users');
}
Final Steps
After setting up the relationships, you may need to adjust the generated views and controllers to better fit your needs. For instance, you might want to add additional logic to the controllers for handling specific functionalities, or modify the views to improve the user interface.

This setup provides a solid foundation for an e-learning system using CakePHP. You can further expand and customize it based on your specific requirements.

仕方ない事ですが
bake した後に調整作業は
当然必要なようです。

しかしながら
step2 step3 を実行した所
確かに e ラーニングシステムの
土台が出来ていました。
image1.png

この土台に手を加えるだけで
User 認証も簡単に実装できました。

コースを選択して、そのコースに属する
lesson 一覧を表示する等の修正も
簡単にできそうです。

修正作業はかなりの時間が必要そうですが
一から e ラーニングシステムを作るより
絶対に作業工程が少なくなりそうな出来でした。

ただし lesson にファイルを保存するなど
データベースのカラムの追加は
いずれ必要になりそうです。


まとめ

以上 ChatGPT と bake を
組み合わせてみました。

e ラーニングシステムが
それだけで出来るほど甘くありませんでしたが
土台は確かに完成しており
修正していく事で e ラーニングシステムが
一から作るより早く出来そうなコードが
出力されました。

修正が必要な点は
bake に過度の期待をしすぎたかもしれません。
ChatGPT は bake を最大限活用できる
回答をして頂けたと思います。

皆さんも bake で
土台を作りたい時は
ChatGPT を活用してみてはいかがでしょうか?

閲覧ありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0