Skip to content

Commit

Permalink
fix: fix add youtube video
Browse files Browse the repository at this point in the history
  • Loading branch information
karimalik committed Aug 31, 2024
1 parent 16639d1 commit d83b6dd
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 113 deletions.
36 changes: 36 additions & 0 deletions mod/youtubevideo/db/access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* Capability definitions for the youtubevideo module.
*
* @package mod_youtubevideo
* @copyright 2024 NYX-EI {@link http://nyx-ei.tech}
* @author NYX-EI <help@nyx-ei.tech>
*/

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
)
],
];
35 changes: 21 additions & 14 deletions mod/youtubevideo/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/youtubevideo/db" VERSION="2024080800" COMMENT="YouTube Video Plugin Database Schema" >
<TABLES>
<TABLE NAME="youtubevideo" ID="id" COMMENT="YouTube Video Plugin Table">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true"/>
<FIELD NAME="video_url" TYPE="text" NOTNULL="true"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
<XMLDB PATH="mod/youtubevideo/db" VERSION="2024080800" COMMENT="YouTube Video module Database Schema">
<TABLES>
<TABLE NAME="youtubevideo" ID="id" COMMENT="Stores YouTube video information">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Course youtubevideo activity belongs to"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="name field for moodle instances"/>
<FIELD NAME="intro" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="General introduction of the youtubevideo activity"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Format of the intro field (MOODLE, HTML, MARKDOWN...)"/>
<FIELD NAME="youtubeurl" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="YouTube video URL"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="course" UNIQUE="false" FIELDS="course"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
24 changes: 0 additions & 24 deletions mod/youtubevideo/form/youtubevideo_form.php

This file was deleted.

15 changes: 8 additions & 7 deletions mod/youtubevideo/lang/en/youtubevideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
43 changes: 34 additions & 9 deletions mod/youtubevideo/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
53 changes: 53 additions & 0 deletions mod/youtubevideo/mod_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @package youtubevideo
* @copyright 2024 NYX-EI {@link https://nyx-ei.tech}
* @author NYX-EI <help@nyx-ei.tech>
*/

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;
}
}
3 changes: 3 additions & 0 deletions mod/youtubevideo/pix/monologo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions mod/youtubevideo/readme.txt
Original file line number Diff line number Diff line change
@@ -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).
6 changes: 4 additions & 2 deletions mod/youtubevideo/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
$plugin->release = '1.0';
$plugin->maturity = MATURITY_STABLE;
96 changes: 39 additions & 57 deletions mod/youtubevideo/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,54 @@
* @author NYX-EI <help@nyx-ei.tech>
*/

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();

0 comments on commit d83b6dd

Please sign in to comment.