From d83b6dd2761e4b6923ffc418f5961eb0e755be70 Mon Sep 17 00:00:00 2001 From: Karim Kompissi Date: Sat, 31 Aug 2024 14:19:54 +0100 Subject: [PATCH] fix: fix add youtube video --- mod/youtubevideo/db/access.php | 36 ++++++++ mod/youtubevideo/db/install.xml | 35 +++++--- mod/youtubevideo/form/youtubevideo_form.php | 24 ------ mod/youtubevideo/lang/en/youtubevideo.php | 15 ++-- mod/youtubevideo/lib.php | 43 +++++++-- mod/youtubevideo/mod_form.php | 53 ++++++++++++ mod/youtubevideo/pix/monologo.svg | 3 + mod/youtubevideo/readme.txt | 22 +++++ mod/youtubevideo/version.php | 6 +- mod/youtubevideo/view.php | 96 +++++++++------------ 10 files changed, 220 insertions(+), 113 deletions(-) create mode 100644 mod/youtubevideo/db/access.php delete mode 100755 mod/youtubevideo/form/youtubevideo_form.php create mode 100644 mod/youtubevideo/mod_form.php create mode 100644 mod/youtubevideo/pix/monologo.svg create mode 100644 mod/youtubevideo/readme.txt diff --git a/mod/youtubevideo/db/access.php b/mod/youtubevideo/db/access.php new file mode 100644 index 0000000000000..9862b85f8a615 --- /dev/null +++ b/mod/youtubevideo/db/access.php @@ -0,0 +1,36 @@ + + */ + +defined('MOODLE_INTERNAL') || die(); + +$capabilities = [ + 'mod/youtubevideo:addinstance' => [ + 'riskbitmask' => RISK_XSS, + 'captype' => 'write', + 'contextlevel' => CONTEXT_COURSE, + 'archetypes' => array( + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ), + 'clonepermissionsfrom' => 'moodle/course:manageactivities' + ], + + 'mod/youtubevideo:view' => [ + 'captype' => 'read', + 'contextlevel' => CONTEXT_MODULE, + 'archetypes' => array( + 'guest' => CAP_ALLOW, + 'student' => CAP_ALLOW, + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ) + ], +]; diff --git a/mod/youtubevideo/db/install.xml b/mod/youtubevideo/db/install.xml index 008174163ca72..32f6127e114cc 100755 --- a/mod/youtubevideo/db/install.xml +++ b/mod/youtubevideo/db/install.xml @@ -1,16 +1,23 @@ - - - - - - - - - - - - -
-
+ + + + + + + + + + + + + + + + + + + +
+
diff --git a/mod/youtubevideo/form/youtubevideo_form.php b/mod/youtubevideo/form/youtubevideo_form.php deleted file mode 100755 index e37b10ef5e717..0000000000000 --- a/mod/youtubevideo/form/youtubevideo_form.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ - -defined('MOODLE_INTERNAL') || die(); - -require_once('../config.php'); -require_once($CFG->libdir . '/formslib.php'); - -class youtubevideo_form extends moodleform { - public function definition() - { - $mform = $this->_form; - - $mform->addElement('text', 'video_url', get_string('video_url', 'youtubevideo')); - $mform->setType('video_url', PARAM_URL); - $mform->addRule('video_url', null, 'required', null, 'client'); - - $mform->addElement('submit', 'submitbutton', get_string('submit', 'youtubevideo')); - } -} \ No newline at end of file diff --git a/mod/youtubevideo/lang/en/youtubevideo.php b/mod/youtubevideo/lang/en/youtubevideo.php index e28116845de76..548c9a6bd9954 100755 --- a/mod/youtubevideo/lang/en/youtubevideo.php +++ b/mod/youtubevideo/lang/en/youtubevideo.php @@ -7,11 +7,12 @@ * @copyright 2024 Nyx-EI {@link https://nyx-ei.tech} */ +$string['modulename'] = 'YouTube Video'; +$string['modulenameplural'] = 'YouTube Videos'; +$string['modulename_help'] = 'The YouTube video module allows a teacher to embed a YouTube video as a course resource.'; +$string['youtubevideo:addinstance'] = 'Add a new YouTube video'; +$string['youtubevideo:view'] = 'View YouTube video'; $string['pluginname'] = 'YouTube Video'; -$string['video_url'] = 'YouTube Video URL'; -$string['submit'] = 'Submit'; -$string['add_youtube_video'] = 'Add YouTube Video'; -$string['form_cancelled'] = 'The form has been cancelled. You are being redirected.'; -$string['invalid_url'] = 'Invalid Url.'; -$string['insert_error'] = 'An error occurred during data insertion.'; - +$string['youtubeurl'] = 'YouTube URL'; +$string['youtubeurl_help'] = 'Enter the URL of the YouTube video you want to embed.'; +$string['invalidyoutubeurl'] = 'The entered YouTube URL is not valid.'; diff --git a/mod/youtubevideo/lib.php b/mod/youtubevideo/lib.php index aa4d484af32d8..963c42d45d49d 100755 --- a/mod/youtubevideo/lib.php +++ b/mod/youtubevideo/lib.php @@ -8,21 +8,22 @@ defined('MOODLE_INTERNAL') || die(); -function youtubevideo_add_instance($data, $mform) +function youtubevideo_add_instance($youtubevideo) { global $DB; - $data->timemodified = time(); - return $DB->insert_record('youtubevideo', $data); + $youtubevideo->timecreated = time(); + $youtubevideo->timemodified = time(); + return $DB->insert_record('youtubevideo', $youtubevideo); } -function youtubevideo_update_instance($data, $mform) +function youtubevideo_update_instance($youtubevideo) { global $DB; - $data->timemodified = time(); - $data->id = $data->instance; - return $DB->update_record('youtubevideo', $data); + $youtubevideo->timemodified = time(); + $youtubevideo->id = $youtubevideo->instance; + return $DB->update_record('youtubevideo', $youtubevideo); } function youtubevideo_delete_instance($id) @@ -32,8 +33,32 @@ function youtubevideo_delete_instance($id) if (!$youtubevideo = $DB->get_record('youtubevideo', array('id' => $id))) { return false; } + $DB->delete_records('youtubevideo', array('id' => $youtubevideo->id)); + return true; +} - $DB->delete_records('youtubevideo', array('id' => $id)); +function youtubevideo_supports($feature) +{ + switch($feature) { + case FEATURE_MOD_INTRO: return true; + case FEATURE_SHOW_DESCRIPTION: return true; + case FEATURE_GRADE_HAS_GRADE: return false; + case FEATURE_BACKUP_MOODLE2: return true; + default: return null; + } +} - return true; +function get_youtube_id($url) +{ + $parts = parse_url($url); + if(isset($parts['query'])){ + parse_str($parts['query'], $qs); + if(isset($qs['v'])) return $qs['v']; + else if(isset($qs['vi'])) return $qs['vi']; + } + if(isset($parts['path'])){ + $path = explode('/', trim($parts['path'], '/')); + return $path[count($path)-1]; + } + return false; } \ No newline at end of file diff --git a/mod/youtubevideo/mod_form.php b/mod/youtubevideo/mod_form.php new file mode 100644 index 0000000000000..e4b83e0bd77ac --- /dev/null +++ b/mod/youtubevideo/mod_form.php @@ -0,0 +1,53 @@ + + */ + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/course/moodleform_mod.php'); + +class mod_youtubevideo_mod_form extends moodleform_mod +{ + + function definition() + { + global $CFG; + + $mform = $this->_form; + + $mform->addElement('header', 'general', get_string('general', 'form')); + $mform->addElement('text', 'name', get_string('name'), array('size' => '64')); + + if (!empty($CFG->formatstringstriptags)) { + $mform->setType('name', PARAM_TEXT); + } else { + $mform->setType('name', PARAM_CLEANHTML); + } + + $mform->addRule('name', null, 'required', null, 'client'); + $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); + + $this->standard_intro_elements(); + + $mform->addElement('text', 'youtubeurl', get_string('youtubeurl', 'youtubevideo'), array('size' => '64')); + $mform->setType('youtubeurl', PARAM_URL); + $mform->addRule('youtubeurl', null, 'required', null, 'client'); + $mform->addHelpButton('youtubeurl', 'youtubeurl', 'youtubevideo'); + + $this->standard_coursemodule_elements(); + $this->add_action_buttons(); + } + + function validated_url($data, $files) + { + $errors = parent::validation($data, $files); + if (!preg_match('/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/', $data['youtubeurl'])) { + $errors['youtubeurl'] = get_string('invalidyoutubeurl', 'youtubevideo'); + } + return $errors; + } +} diff --git a/mod/youtubevideo/pix/monologo.svg b/mod/youtubevideo/pix/monologo.svg new file mode 100644 index 0000000000000..883643bf8bfa4 --- /dev/null +++ b/mod/youtubevideo/pix/monologo.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/mod/youtubevideo/readme.txt b/mod/youtubevideo/readme.txt new file mode 100644 index 0000000000000..120d73dad6a45 --- /dev/null +++ b/mod/youtubevideo/readme.txt @@ -0,0 +1,22 @@ +# Module Vidéo YouTube pour Moodle + +Ce module permet aux enseignants d'intégrer facilement des vidéos YouTube dans leurs cours Moodle. + +## Fonctionnalités + +- Intégration directe des vidéos YouTube dans les cours Moodle +- Interface simple pour les enseignants pour ajouter des vidéos +- Lecture des vidéos dans l'environnement Moodle + +## Utilisation + +1. Activez le mode édition dans votre cours. +2. Cliquez sur "Ajouter une activité ou une ressource" et sélectionnez "Vidéo YouTube" dans la liste. +3. Donnez un nom à votre vidéo et collez l'URL YouTube dans le champ "URL YouTube". +4. Enregistrez et retournez au cours. +5. La vidéo sera maintenant intégrée dans votre cours. + + +## Support + +Si vous rencontrez des problèmes ou avez des questions, veuillez ouvrir un ticket (issue) sur le dépôt [GitHub](https://github.com/nyx-ei/Mood-NYX/issues). diff --git a/mod/youtubevideo/version.php b/mod/youtubevideo/version.php index 33f65cc8860c4..d4bfd3a832a17 100755 --- a/mod/youtubevideo/version.php +++ b/mod/youtubevideo/version.php @@ -9,6 +9,8 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024080800; +$plugin->component = 'mod_youtubevideo'; +$plugin->version = 2024083100; $plugin->requires = 2024062700; -$plugin->component = 'mod_youtubevideo'; \ No newline at end of file +$plugin->release = '1.0'; +$plugin->maturity = MATURITY_STABLE; \ No newline at end of file diff --git a/mod/youtubevideo/view.php b/mod/youtubevideo/view.php index 19a1b6836765d..b392cad3f7c8f 100755 --- a/mod/youtubevideo/view.php +++ b/mod/youtubevideo/view.php @@ -6,72 +6,54 @@ * @author NYX-EI */ -require('../../config.php'); +require_once(__DIR__ . '/../../config.php'); require_once($CFG->dirroot.'/mod/youtubevideo/lib.php'); -require_once($CFG->dirroot.'/mod/youtubevideo/form/youtubevideo_form.php'); -//id of course -$id = required_param('id', PARAM_INT); +$id = optional_param('id', 0, PARAM_INT); +$y = optional_param('y', 0, PARAM_INT); //youtube instance ID + +if ($id) { + $cm = get_coursemodule_from_id('youtubevideo', $id, 0, false, MUST_EXIST); + $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); + $youtubevideo = $DB->get_record('youtubevideo', array('id' => $cm->instance), '*', MUST_EXIST); +} elseif ($y) { + $youtubevideo = $DB->get_record('youtubevideo', array('id' => $y), '*', MUST_EXIST); + $course = $DB->get_record('course', array('id' => $youtubevideo->course), '*', MUST_EXIST); + $cm = get_coursemodule_from_instance('youtubevideo', $youtubevideo->id, $course->id, false, MUST_EXIST); +} else { + throw new moodle_exception('missingidandcmid', 'youtubevideo'); +} -$cm = get_coursemodule_from_id('youtubevideo', $id, 0, false, MUST_EXIST); -$course = get_course($cm->course); -$context = context_module::instance($cm->id); require_login($course, true, $cm); +$context = context_module::instance($cm->id); -$video_url = optional_param('video_url', '', PARAM_URL); +$PAGE->set_url('/mod/youtubevideo/view.php', array('id' => $cm->id)); +$PAGE->set_title(format_string($youtubevideo->name)); +$PAGE->set_heading(format_string($course->fullname)); +$PAGE->set_context($context); -if ($video_url) -{ +$output = $PAGE->get_renderer('mod_youtubevideo'); - if (filter_var($video_url, FILTER_VALIDATE_URL) && strpos($video_url, 'youtube.com') !== false) - { - $video_id = parse_youtube_url($video_url); - echo html_writer::start_tag( - 'iframe', - array( - 'width' => '560', - 'height' => '315', - 'src' => "https://www.youtube.com/embed/$video_id", - 'frameborder' => '0', - 'allow' => 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture', - 'allowfullscreen' => 'true' - ), - ); - echo html_writer::end_tag('iframe'); - } else { - echo $OUTPUT->notification(get_string('invalid_url', 'youtubevideo'), 'notiferror'); - } +echo $output->header(); +echo $output->heading(format_string($youtubevideo->name)); -} else { - $mform = new youtubevideo_form(); +if (!empty($youtubevideo->intro)) { + echo $output->box(format_module_intro('youtubevideo', $youtubevideo, $cm->id), 'generalbox mod_introbox', 'youtubevideorintro'); +} - if ($mform->is_cancelled()) - { - echo $OUTPUT->notification(get_string('form_cancelled', 'youtubevideo'), 'notifinfo'); - redirect(new moodle_url('/course/view.php', array('id' => $course->id)), '', 3); - exit(); - } elseif ($data = $mform->get_data()) { - global $DB; +$youtube_id = get_youtube_id($youtubevideo->youtubeurl); - $record = new stdClass(); - $record->course = $course->id; - $record->video_url = $data->video_url; - $record->timemodified = time(); +$video_container = html_writer::start_div('youtubevideo-container'); +$video_container .= html_writer::empty_tag('iframe', array( + 'width' => '560', + 'height' => '315', + 'src' => "https://www.youtube.com/embed/$youtube_id", + 'frameborder' => '0', + 'allow' => 'autoplay; encrypted-media', + 'allowfullscreen' => 'allowfullscreen' +)); +$video_container .= html_writer::end_div(); - try { - $DB->insert_record('youtubevideo', $record); - redirect(new moodle_url('/course/view.php', array('id' => $course->id))); - } catch (Exception $e) { - echo $OUTPUT->notification(get_string('insert_error', 'youtubevideo'), 'notiferror'); - } - } else { - echo $OUTPUT->heading(get_string('add_youtube_video', 'youtubevideo')); - $mform->display(); - } +echo $video_container; -} -function parse_youtube_url($url) -{ - parse_str(parse_url($url, PHP_URL_QUERY), $params); - return $params['v']; -} +echo $output->footer();