2023-09-08 10:12:23 +02:00
< ? php
/**
* This file is part of Zwii .
* For full copyright and license information , please see the LICENSE
* file that was distributed with this source code .
*
* @ author Rémi Jean < remi . jean @ outlook . com >
* @ copyright Copyright ( C ) 2008 - 2018 , Rémi Jean
* @ author Frédéric Tempez < frederic . tempez @ outlook . com >
* @ copyright Copyright ( C ) 2018 - 2023 , Frédéric Tempez
* @ license CC Attribution - NonCommercial - NoDerivatives 4.0 International
* @ link http :// zwiicms . fr /
*/
2023-09-08 22:09:25 +02:00
class course extends common
2023-09-08 10:12:23 +02:00
{
public static $actions = [
2023-09-12 22:16:25 +02:00
'index' => self :: GROUP_ADMIN ,
'edit' => self :: GROUP_ADMIN ,
2023-09-08 22:09:25 +02:00
'add' => self :: GROUP_ADMIN ,
2023-09-12 22:16:25 +02:00
'delete' => self :: GROUP_ADMIN ,
2023-09-21 21:19:39 +02:00
'swap' => self :: GROUP_VISITOR ,
2023-09-08 10:12:23 +02:00
];
2023-09-08 22:09:25 +02:00
public static $courseAccess = [
2023-09-20 22:02:42 +02:00
self :: COURSE_ACCESS_OPEN => 'Ouvert' ,
self :: COURSE_ACCESS_DATE => 'Période d\'ouverture' ,
self :: COURSE_ACCESS_CLOSE => 'Fermé' ,
2023-09-08 22:09:25 +02:00
];
public static $courseEnrolment = [
2023-09-20 22:02:42 +02:00
self :: COURSE_ENROLMENT_GUEST => 'Anonyme' ,
self :: COURSE_ENROLMENT_SELF => 'Auto-inscrition libre' ,
self :: COURSE_ENROLMENT_SELF_KEY => 'Auto-inscription avec clé' ,
self :: COURSE_ENROLMENT_MANUAL => 'Manuelle'
2023-09-08 22:09:25 +02:00
];
public static $courseTeachers = [];
2023-09-12 22:16:25 +02:00
public static $courses = [];
2023-09-22 19:33:03 +02:00
public static $swapMessage = '' ;
2023-09-20 22:02:42 +02:00
2023-09-17 17:12:53 +02:00
2023-09-08 10:12:23 +02:00
public function index ()
{
2023-09-12 22:16:25 +02:00
$courseIdShortTitle = helper :: arrayColumn ( $this -> getData ([ 'course' ]), 'shortTitle' );
2023-09-16 17:27:22 +02:00
ksort ( $courseIdShortTitle );
2023-09-12 22:16:25 +02:00
foreach ( $courseIdShortTitle as $courseId => $courseTitle ) {
self :: $courses [] = [
$courseTitle ,
$this -> getData ([ 'course' , $courseId , 'author' ]),
$this -> getData ([ 'course' , $courseId , 'description' ]),
template :: button ( 'courseEdit' . $courseId , [
'href' => helper :: baseUrl () . 'course/edit/' . $courseId ,
'value' => template :: ico ( 'pencil' ),
'help' => 'Éditer'
]),
template :: button ( 'courseDelete' . $courseId , [
'class' => 'courseDelete buttonRed' ,
'href' => helper :: baseUrl () . 'course/delete/' . $courseId ,
'value' => template :: ico ( 'trash' ),
'help' => 'Supprimer'
])
];
}
2023-09-16 17:27:22 +02:00
//var_dump($this->getCourseHierarchy(1, 1));
2023-09-08 22:09:25 +02:00
// Valeurs en sortie
$this -> addOutput ([
'title' => helper :: translate ( 'Cours' ),
'view' => 'index'
]);
}
2023-09-11 22:39:19 +02:00
/**
* Ajoute un nouveau cours
*/
2023-09-08 22:09:25 +02:00
public function add ()
{
// Soumission du formulaire
if (
$this -> getUser ( 'permission' , __CLASS__ , __FUNCTION__ ) === true &&
$this -> isPost ()
) {
2023-09-11 22:39:19 +02:00
$courseId = uniqid ();
2023-09-16 17:27:22 +02:00
$author = $this -> getInput ( 'courseAddAuthor' );
2023-09-08 22:09:25 +02:00
$this -> setData ([
'course' ,
2023-09-11 22:39:19 +02:00
$courseId ,
2023-09-08 22:09:25 +02:00
[
2023-09-11 22:39:19 +02:00
'title' => $this -> getInput ( 'courseAddTitle' , helper :: FILTER_STRING_SHORT , true ),
'shortTitle' => $this -> getInput ( 'courseAddShortTitle' , helper :: FILTER_STRING_SHORT , true ),
2023-09-16 17:27:22 +02:00
'author' => $author ,
2023-09-08 22:09:25 +02:00
'description' => $this -> getInput ( 'courseAddDescription' , helper :: FILTER_STRING_SHORT , true ),
2023-09-21 15:34:03 +02:00
'access' => $this -> getInput ( 'courseAddAccess' , helper :: FILTER_INT ),
2023-09-11 22:39:19 +02:00
'openingDate' => $this -> getInput ( 'courseOpeningDate' , helper :: FILTER_DATETIME ),
'closingDate' => $this -> getInput ( 'courseClosingDate' , helper :: FILTER_DATETIME ),
2023-09-21 15:34:03 +02:00
'enrolment' => $this -> getInput ( 'courseAddEnrolment' , helper :: FILTER_INT ),
2023-09-08 22:09:25 +02:00
'enrolmentKey' => $this -> getInput ( 'courseAddEnrolmentKey' ),
]
]);
2023-09-08 10:12:23 +02:00
2023-09-11 22:39:19 +02:00
// Créer la structure de données
mkdir ( self :: DATA_DIR . $courseId );
2023-09-21 11:34:42 +02:00
$this -> initDB ( 'page' , $courseId );
$this -> initDB ( 'module' , $courseId );
$this -> initData ( 'page' , $courseId );
$this -> initData ( 'module' , $courseId );
2023-09-16 17:27:22 +02:00
2023-09-11 22:39:19 +02:00
// BDD des inscrits
2023-09-16 17:27:22 +02:00
$this -> setData ([
'enrolment' ,
$courseId ,
2023-09-21 19:32:30 +02:00
[]
2023-09-16 17:27:22 +02:00
]);
2023-09-11 22:39:19 +02:00
// Valeurs en sortie
$this -> addOutput ([
'redirect' => helper :: baseUrl () . 'course' ,
'notification' => helper :: translate ( 'Cours créé' ),
'state' => true
]);
2023-09-08 22:09:25 +02:00
}
2023-09-11 22:39:19 +02:00
// Liste des enseignants pour le sélecteur d'auteurs
2023-09-08 22:09:25 +02:00
$teachers = $this -> getData ([ 'user' ]);
foreach ( $teachers as $teacherId => $teacherInfo ) {
2023-09-11 22:39:19 +02:00
if ( $teacherInfo [ " group " ] >= 2 ) {
2023-09-08 22:09:25 +02:00
self :: $courseTeachers [ $teacherId ] = $teacherInfo [ " firstname " ] . ' ' . $teacherInfo [ " lastname " ];
}
}
// Valeurs en sortie
$this -> addOutput ([
'title' => helper :: translate ( 'Ajouter un cours' ),
'view' => 'add'
]);
2023-09-08 10:12:23 +02:00
}
2023-09-08 22:09:25 +02:00
2023-09-16 17:27:22 +02:00
public function delete ()
{
2023-09-12 22:16:25 +02:00
if (
2023-09-16 17:27:22 +02:00
$this -> getUser ( 'permission' , __CLASS__ , __FUNCTION__ ) !== true ||
2023-09-12 22:16:25 +02:00
// Le cours n'existe pas
2023-09-16 17:27:22 +02:00
$this -> getData ([ 'course' , $this -> getUrl ( 2 )]) === null
// Groupe insuffisant
2023-09-22 15:19:18 +02:00
and ( $this -> getUrl ( 'group' ) < self :: GROUP_EDITOR )
2023-09-16 17:27:22 +02:00
) {
// Valeurs en sortie
$this -> addOutput ([
'access' => false
]);
// Suppression
} else {
$this -> deleteData ([ 'course' , $this -> getUrl ( 2 )]);
$this -> deleteData ([ 'enrolment' , $this -> getUrl ( 2 )]);
if ( is_dir ( self :: DATA_DIR . $this -> getUrl ( 2 ))) {
$this -> deleteDir ( self :: DATA_DIR . $this -> getUrl ( 2 ));
}
// Valeurs en sortie
$this -> addOutput ([
'redirect' => helper :: baseUrl () . 'course' ,
'notification' => helper :: translate ( 'Cours supprimé' ),
'state' => true
]);
}
2023-09-12 22:16:25 +02:00
}
/**
* Edite un cours
*/
public function edit ()
{
// Soumission du formulaire
if (
$this -> getUser ( 'permission' , __CLASS__ , __FUNCTION__ ) === true &&
$this -> isPost ()
) {
$courseId = $this -> getUrl ( 2 );
2023-09-16 17:27:22 +02:00
$author = $this -> getInput ( 'courseAddAuthor' );
2023-09-12 22:16:25 +02:00
$this -> setData ([
'course' ,
$courseId ,
[
'title' => $this -> getInput ( 'courseEditTitle' , helper :: FILTER_STRING_SHORT , true ),
'shortTitle' => $this -> getInput ( 'courseEditShortTitle' , helper :: FILTER_STRING_SHORT , true ),
2023-09-16 18:50:23 +02:00
'author' => $author ,
2023-09-12 22:16:25 +02:00
'description' => $this -> getInput ( 'courseEditDescription' , helper :: FILTER_STRING_SHORT , true ),
2023-09-16 17:27:22 +02:00
'access' => $this -> getInput ( 'courseEditAccess' , helper :: FILTER_INT ),
2023-09-12 22:16:25 +02:00
'openingDate' => $this -> getInput ( 'courseOpeningDate' , helper :: FILTER_DATETIME ),
'closingDate' => $this -> getInput ( 'courseClosingDate' , helper :: FILTER_DATETIME ),
2023-09-16 17:27:22 +02:00
'enrolment' => $this -> getInput ( 'courseEditEnrolment' , helper :: FILTER_INT ),
2023-09-12 22:16:25 +02:00
'enrolmentKey' => $this -> getInput ( 'courseEditEnrolmentKey' ),
]
]);
// Valeurs en sortie
$this -> addOutput ([
'redirect' => helper :: baseUrl () . 'course' ,
'notification' => helper :: translate ( 'Cours édité' ),
'state' => true
]);
}
// Liste des enseignants pour le sélecteur d'auteurs
$teachers = $this -> getData ([ 'user' ]);
foreach ( $teachers as $teacherId => $teacherInfo ) {
if ( $teacherInfo [ " group " ] >= 2 ) {
self :: $courseTeachers [ $teacherId ] = $teacherInfo [ " firstname " ] . ' ' . $teacherInfo [ " lastname " ];
}
}
// Valeurs en sortie
$this -> addOutput ([
'title' => helper :: translate ( 'Editer un cours' ),
'view' => 'edit'
]);
}
2023-09-21 15:34:03 +02:00
/*
* Affiche un écran de connexion à un cours
*/
public function change ()
{
// Soumission du formulaire
if (
$this -> isPost () ||
$this -> getUrl ( 2 ) === 'home'
2023-09-21 19:32:30 +02:00
2023-09-21 15:34:03 +02:00
) {
$this -> swap ();
}
2023-09-21 19:32:30 +02:00
// Bouton de connexion ou d'inscription
// C'est un prof ou un admin
2023-09-22 19:33:03 +02:00
self :: $changeMessages = $this -> getUser ( 'group' ) >= self :: GROUP_EDITOR
? 'Se connecter'
// C'est un étudiant ou un visiteur
: '' ;
2023-09-21 19:32:30 +02:00
2023-09-21 15:34:03 +02:00
// Valeurs en sortie
$this -> addOutput ([
2023-09-21 19:32:30 +02:00
'title' => sprintf ( helper :: translate ( 'Accéder au cours %s' ), $this -> getData ([ 'course' , $this -> getUrl ( 2 ), 'shortTitle' ])),
2023-09-21 15:34:03 +02:00
'view' => 'change' ,
'display' => self :: DISPLAY_LAYOUT_LIGHT ,
]);
}
2023-09-11 22:39:19 +02:00
/*
* Traitement du changement de langue
* Fonction utilisée par le noyau
*/
public function swap ()
{
// Cours sélectionnée
$courseId = $this -> getUrl ( 2 );
2023-09-17 17:12:53 +02:00
2023-09-22 19:33:03 +02:00
// Le cours est disponible ?
if ( $courseId === 'home' ) {
$_SESSION [ 'ZWII_SITE_CONTENT' ] = $courseId ;
// Valeurs en sortie
$this -> addOutput ([
'redirect' => helper :: baseUrl (),
]);
} elseif ( $this -> courseIsAvailable ( $courseId ) === false ) {
$message = 'Ce cours est fermé.' ;
if ( $this -> getData ([ 'course' , $courseId , 'access' ]) === self :: COURSE_ACCESS_DATE ) {
$from = helper :: dateUTF8 ( '%m %B %Y' , $this -> getData ([ 'course' , $courseId , 'openingDate' ])) . helper :: translate ( ' à ' ) . helper :: dateUTF8 ( '%H:%M' , $this -> getData ([ 'course' , $courseId , 'openingDate' ]));
$to = helper :: dateUTF8 ( '%m %B %Y' , $this -> getData ([ 'course' , $courseId , 'closingDate' ])) . helper :: translate ( ' à ' ) . helper :: dateUTF8 ( '%H:%M' , $this -> getData ([ 'course' , $courseId , 'closingDate' ]));
$message = sprintf ( helper :: translate ( 'Ce cours ouvre le <br>%s <br> et ferme le %s' ), $from , $to );
}
// Valeurs en sortie
$this -> addOutput ([
'redirect' => helper :: baseUrl (),
'notification' => helper :: translate ( $message ),
'state' => false ,
]);
}
// Soumission du formulaire
2023-09-11 22:39:19 +02:00
if (
2023-09-22 19:33:03 +02:00
$this -> isPost ()
2023-09-11 22:39:19 +02:00
) {
2023-09-22 19:33:03 +02:00
if (
// Contrôle la validité du cours demandé
( is_dir ( self :: DATA_DIR . $courseId ) &&
$this -> getData ([ 'course' , $courseId ]))
) {
// Stocker la sélection
$_SESSION [ 'ZWII_SITE_CONTENT' ] = $courseId ;
}
// Valeurs en sortie
$this -> addOutput ([
'redirect' => helper :: baseUrl ()
]);
} else {
// Génération du message d'inscription
$userId = $this -> getUser ( 'id' );
// L'étudiant est-il inscrit
self :: $swapMessage = 'Se connecter' ;
if ( $this -> courseUserEnrolment ( $courseId , $userId ) === false ) {
// Inscription libre
if ( $this -> getData ([ 'course' , $courseId , 'enrolment' ]) <= self :: COURSE_ENROLMENT_SELF ) {
self :: $swapMessage = helper :: translate ( 'S\'inscrire' );
}
}
// Valeurs en sortie
$this -> addOutput ([
'title' => sprintf ( helper :: translate ( 'Accéder au cours %s' ), $this -> getData ([ 'course' , $this -> getUrl ( 2 ), 'shortTitle' ])),
'view' => 'swap' ,
'display' => self :: DISPLAY_LAYOUT_LIGHT ,
]);
2023-09-11 22:39:19 +02:00
}
2023-09-22 19:33:03 +02:00
2023-09-11 22:39:19 +02:00
}
2023-09-11 21:45:31 +02:00
2023-09-21 15:34:03 +02:00
2023-09-08 10:12:23 +02:00
}