summaryrefslogtreecommitdiff
path: root/public/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'public/app/controllers')
-rw-r--r--public/app/controllers/Auth.php7
-rw-r--r--public/app/controllers/admin/Course_admin.php282
-rw-r--r--public/app/controllers/cms/Course.php36
3 files changed, 308 insertions, 17 deletions
diff --git a/public/app/controllers/Auth.php b/public/app/controllers/Auth.php
index d7b0c3b..b00843c 100644
--- a/public/app/controllers/Auth.php
+++ b/public/app/controllers/Auth.php
@@ -322,6 +322,13 @@ class Auth {
}
+ public static function is_admin()
+ {
+ $manager = new Classroom_manager();
+ return ($manager->isGranted([1,2,3]));
+ }
+
+
}
diff --git a/public/app/controllers/admin/Course_admin.php b/public/app/controllers/admin/Course_admin.php
index ef9ef0e..d1048b7 100644
--- a/public/app/controllers/admin/Course_admin.php
+++ b/public/app/controllers/admin/Course_admin.php
@@ -98,6 +98,167 @@ class Course_admin {
}
+ ## -------------------------------------------------------------------------
+ ##
+ ## LESSON METHODS
+ ##
+ ## -------------------------------------------------------------------------
+
+
+ /** get_lesson
+ *
+ * @param $id (int) : lesson's id
+ */
+ public static function get_lesson($id)
+ {
+ // get (first) lesson with id = $id; render as json
+ Render::json(Registry::use('database')->query(
+ "SELECT lesson.*,
+ ( SELECT
+ CONCAT('[', GROUP_CONCAT(JSON_OBJECT(
+ 'id', media.id,
+ 'label', media.label,
+ 'type', media.type,
+ 'path', media.path )),
+ ']')
+ FROM media
+ LEFT JOIN lesson_media ON lesson_media.media_id = media.id
+ WHERE lesson_media.lesson_id = :id
+ ) AS medias_json,
+ lesson_privilege.privilege_id
+ FROM lesson
+ LEFT JOIN lesson_privilege ON lesson_privilege.lesson_id = lesson.id
+ WHERE lesson.id = :id",
+ [ ':id' => $id]
+ )->getFirst());
+ }
+
+
+ /** add_lesson
+ * insert a new lesson
+ *
+ * all parametres are passed via $_POST array
+ *
+ * POST @param title
+ * POST @param course_id
+ * POST @param intro
+ * POST @param body
+ * POST @param published
+ */
+ public static function add_lesson()
+ {
+ $post = Registry::get('REQUEST')->POST;
+
+ // insert lesson content into daabase
+ $id = Registry::use('database')->query(
+ "INSERT INTO lesson (title, course_id, intro, `body`, `status`)
+ VALUES (:title, :courseid, :intro, :body, :status)",
+ [
+ ':title' => $post['title'],
+ ':courseid' => $post['course_id'],
+ ':intro' => $post['intro'],
+ ':body' => $post['body'],
+ 'status' => $post['status']
+ ]
+ )->lastInsertID();
+
+ // set lesson privileges to database
+ Registry::use('database')->runQuery(
+ "INSERT INTO lesson_privilege (lesson_id, privilege_id)
+ VALUES (:lesson_id, :privilege_id)",
+ [
+ ':lesson_id' => $id,
+ ':privilege_id' => $post['privilege_id']
+ ]
+ );
+
+ // update media's privileges
+ self::update_medias_privileges($post['media'], $post['privilege_id']);
+
+ // set lesson's media files
+ self::create_medias_for_lesson($post['media'], $id, $post['privilege_id']);
+
+ return true;
+ }
+
+
+ public static function update_lesson()
+ {
+ $post = Registry::get('REQUEST')->POST;
+
+ // print_r($post); die();
+
+ // update lesson's content
+ Registry::use('database')->query(
+ "UPDATE lesson
+ SET title = :title,
+ course_id = :courseid,
+ intro = :intro, `body` = :body,
+ `status` = :status
+ WHERE id = :id",
+ [
+ ':id' => $post['id'],
+ ':title' => $post['title'],
+ ':courseid' => $post['course_id'],
+ ':intro' => $post['intro'],
+ ':body' => $post['body'],
+ ':status' => $post['status']
+ ]
+ );
+
+ // update lesson's privileges
+ Registry::use('database')->runQuery(
+ "UPDATE lesson_privilege SET privilege_id = :privilege_id
+ WHERE lesson_id = :lesson_id",
+ [
+ ':lesson_id' => $post['id'],
+ ':privilege_id' => $post['privilege_id']
+ ]
+ );
+
+ // update media's privileges
+ self::update_medias_privileges($post['media'], $post['privilege_id']);
+
+ // remove all old lesson's links to media files
+ Registry::use('database')->runQuery(
+ "DELETE from lesson_media WHERE lesson_id = :lesson",
+ [ ':lesson' => $post['id'] ]
+ );
+
+ // update lesson's media files
+ self::create_medias_for_lesson($post['media'], $post['id'], $post['privilege_id']);
+
+ return true;
+ }
+
+ /** update medias privileges
+ *
+ * @param $medias (array) : array of media id(s)
+ * @param $privilege (int) : privilege id
+ * @return true (always)
+ */
+ private static function update_medias_privileges($medias, $privilege)
+ {
+ // create WHERE IN (LIST) holders and params for prepare statement
+ $params = [ ':privilege' => $privilege];
+ $holders = [];
+ foreach($medias as $key => $media) {
+ $params[':id'.$key] = $media;
+ $holders[] = ':id'.$key;
+ }
+ $list = '('. implode(',', $holders) .')';
+
+ // print_r(['params' => $params, 'list' => $list]); die();
+
+ Registry::use('database')->runQuery(
+ "UPDATE media SET privilege_id = :privilege
+ WHERE id IN {$list}",
+ $params
+ );
+
+ return true;
+ }
+
## -------------------------------------------------------------------------
@@ -107,18 +268,66 @@ class Course_admin {
## -------------------------------------------------------------------------
+ /** files
+ * echo all files
+ * @return (array)
+ */
+ public static function files()
+ {
+ return proxy( // cache-get files
+ [\app\models\cms\Course_model::class, 'files'],
+ [], CACHE_ROOT_TTL
+ );
+ }
+
+
/** upload_file
* upload the file to the file system
*
* the method reads the POST and FILES array
* to retrieve all needed parametres
*
- * + $_FILES[file]
- * + $_POST[folder] : lesson's ID
- * + $_POST[reference] : reference type
+ * FILES @param file
+ * POST @param folder : lesson's ID or somthing random
+ * POST @param reference : reference type
+ * POST @param
*/
public static function upload_file()
{
+ $request = Registry::get('REQUEST');
+
+ $uploaded = self::upload_to_fs(); // upload file to file-system
+
+ if ($uploaded['success']) {
+
+ $media_id = self::define_media([ // define media in database; get id
+ 'title' => $request->POST['title'],
+ 'type' => $uploaded['type'],
+ 'path' => $uploaded['path']
+ ]);
+
+ Render::json([ // render results as json
+ 'success' => true,
+ 'id' => $media_id,
+ 'title' => $request->POST['title'],
+ 'path' => $uploaded['path'],
+ 'type' => $uploaded['type']
+ ]);
+
+ } else {
+ Render::json(['success' => false ]);
+ }
+ }
+
+
+ /** upload to fs
+ * upload file to File-System
+ *
+ * POST @param folder
+ * FILES @param file
+ */
+ private static function upload_to_fs()
+ {
$post = Registry::get('REQUEST')->POST;
$files = Registry::get('REQUEST')->FILES;
@@ -181,7 +390,7 @@ class Course_admin {
if (move_uploaded_file($files["file"]["tmp_name"], $store_filename)) {
return [
'success' => true,
- 'path' => $store_filename,
+ 'path' => $relative_filename,
'type' => $mime_type
];
@@ -207,22 +416,61 @@ class Course_admin {
*
* create a record in media table
*
- * @param $data
+ * @param $data (array): [title => , path => , type => mime-type]
* @return id (int): id of created media record
*/
- public static function define_media($data)
+ private static function define_media($data)
{
- $sql = "INSERT INTO wiki_media (`title`, `type`, `path`)
- VALUES (:title, :mimetype, :filepath)";
- $stmt = $this->pdo->prepare($sql);
- $stmt->execute([
- 'title' => $data['title'],
- 'mimetype' => $data['type'],
- 'filepath' => $data['path']
- ]);
- $inserted_id = $this->pdo->lastInsertId();
-
- return $inserted_id;
+ $request = Registry::get('REQUEST');
+
+ $media_id = Registry::use('database')->query(
+ "INSERT INTO media (label, `type`, `path`)
+ VALUES (:label, :mimetype, :filepath)",
+ [
+ 'label' => $data['title'],
+ 'mimetype' => $data['type'],
+ 'filepath' => $data['path']
+ ]
+ )->lastInsertID();
+ return $media_id;
+ }
+
+
+ /** create media for post
+ *
+ * links post to each media-file of the media `id`s array
+ * @param $media (array): a list of media-file `id`s
+ * @param $post_id (int)
+ */
+ private static function create_medias_for_lesson($medias, $lesson_id)
+ {
+ foreach($medias as $key => $medi) {
+ self::link_media_to_lesson($medi, $lesson_id); // link to lesson
+ }
+ return true;
+ }
+
+
+ /** link one media-file to a specific post
+ *
+ * NOTE:
+ * the method does not check if media is linked already
+ * so be sure that the pair of (media_id,post_id) not exist
+ *
+ * @param $media_id (int)
+ * @param $lesson_id (int)
+ */
+ private static function link_media_to_lesson( $media_id, $lesson_id )
+ {
+ Registry::use('database')->runQuery(
+ "INSERT INTO lesson_media (lesson_id, media_id)
+ VALUES (:lesson, :media)",
+ [
+ 'lesson' => $lesson_id,
+ 'media' => $media_id,
+ ]
+ );
+ return true;
}
diff --git a/public/app/controllers/cms/Course.php b/public/app/controllers/cms/Course.php
new file mode 100644
index 0000000..b671a5f
--- /dev/null
+++ b/public/app/controllers/cms/Course.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace app\controllers\cms;
+
+use Registry;
+use Render;
+use app\controllers\Auth;
+use app\models\cms\Course_model;
+
+class Course {
+
+ /** serve file by file_path
+ * (request is valid only for admin users)
+ *
+ * @param $file_path (string): relative file path
+ * GET @param type (string) : media-type of file
+ */
+ public static function serve_file($file_path)
+ {
+ // check privileges
+ if (Auth::is_admin()) { // TODO: -OR- has prvivilege
+
+ $media_type = Registry::get('REQUEST')->GET['type']; // get media-type
+ $real_path = MEDIA_STORAGE_ROOT . $file_path; // construct real path
+
+ if (!file_exists($real_path)) {
+ Render::view('error/404');
+
+ } else {
+ Render::file($real_path, $media_type);
+ }
+
+ }
+ }
+
+} \ No newline at end of file