Пример #1
0
/**
 * Function to be run periodically according to the moodle cron
 * This function searches for things that need to be done, such 
 * as sending out mail, toggling flags etc ... 
 *
 * @uses $CFG
 * @return boolean
 * @todo Finish documenting this function
 **/
function mediaboard_cron()
{
    global $CFG, $DB;
    $fs = get_file_storage();
    if ($data = $DB->get_record_sql("SELECT * FROM {mediaboard_process} WHERE `status`='open' LIMIT 1")) {
        $CFG->mediaboard_convert = 0;
        if (in_array($data->type, json_decode(MEDIABOARD_VIDEOTYPES))) {
            $CFG->mediaboard_convert = $CFG->mediaboard_video_convert;
        } else {
            if (in_array($data->type, json_decode(MEDIABOARD_AUDIOTYPES))) {
                $CFG->mediaboard_convert = $CFG->mediaboard_audio_convert;
            }
        }
        //Check converting method local or mserver
        if ($CFG->mediaboard_convert == 1) {
            if (strstr($CFG->mediaboard_convert_url, "ffmpeg")) {
                $CFG->mediaboard_convert = 2;
            }
        }
        //local
        if ($CFG->mediaboard_convert == 1) {
            $from = mediaboard_getfileid($data->itemid);
            $add = new stdClass();
            $add->id = $data->id;
            $add->status = 'send';
            $DB->update_record("mediaboard_process", $add);
            $ch = curl_init();
            if (in_array($data->type, json_decode(MEDIABOARD_AUDIOTYPES))) {
                $datasend = array('name' => $data->name, 'mconverter_wav' => '@' . $from->fullpatch);
            }
            if (in_array($data->type, json_decode(MEDIABOARD_VIDEOTYPES))) {
                $datasend = array('name' => $data->name, 'mconverter_m4a' => '@' . $from->fullpatch);
            }
            curl_setopt($ch, CURLOPT_URL, $CFG->mediaboard_convert_url . '/send.php');
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $datasend);
            curl_exec($ch);
        } else {
            if ($CFG->mediaboard_convert == 3) {
                $from = mediaboard_getfileid($data->itemid);
                $add = new stdClass();
                $add->id = $data->id;
                $add->status = 'send';
                $DB->update_record("mediaboard_process", $add);
                if (in_array($data->type, json_decode(MEDIABOARD_VIDEOTYPES))) {
                    if ($item = $DB->get_record("mediaboard_files", array("itemoldid" => $data->itemid))) {
                        $table = 'mediaboard_files';
                    } else {
                        if ($item = $DB->get_record("mediaboard_comments", array("itemoldid" => $data->itemid))) {
                            $table = 'mediaboard_comments';
                        }
                    }
                    @set_include_path($CFG->dirroot . '/mod/mediaboard/library');
                    require_once "Zend/Gdata/ClientLogin.php";
                    require_once "Zend/Gdata/HttpClient.php";
                    require_once "Zend/Gdata/YouTube.php";
                    require_once "Zend/Gdata/App/HttpException.php";
                    require_once 'Zend/Uri/Http.php';
                    $authenticationURL = 'https://www.google.com/youtube/accounts/ClientLogin';
                    $httpClient = Zend_Gdata_ClientLogin::getHttpClient($username = $CFG->mediaboard_youtube_email, $password = $CFG->mediaboard_youtube_password, $service = 'youtube', $client = null, $source = 'mediaboard', $loginToken = null, $loginCaptcha = null, $authenticationURL);
                    $yt = new Zend_Gdata_YouTube($httpClient, 'mediaboard', NULL, $CFG->mediaboard_youtube_apikey);
                    $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
                    /// unlisted upload
                    $accessControlElement = new Zend_Gdata_App_Extension_Element('yt:accessControl', 'yt', 'http://gdata.youtube.com/schemas/2007', '');
                    $accessControlElement->extensionAttributes = array(array('namespaceUri' => '', 'name' => 'action', 'value' => 'list'), array('namespaceUri' => '', 'name' => 'permission', 'value' => 'denied'));
                    $myVideoEntry->extensionElements = array($accessControlElement);
                    $filesource = $yt->newMediaFileSource($from->fullpatch);
                    $filesource->setContentType($data->type);
                    $filesource->setSlug('slug');
                    $myVideoEntry->setMediaSource($filesource);
                    $myVideoEntry->setVideoTitle($from->author);
                    $myVideoEntry->setVideoDescription($from->author);
                    $myVideoEntry->setVideoCategory('Education');
                    $myVideoEntry->SetVideoTags('mediaboard');
                    //$myVideoEntry->setVideoDeveloperTags(array($item->id));
                    //$yt->registerPackage('Zend_Gdata_Geo');
                    //$yt->registerPackage('Zend_Gdata_Geo_Extension');
                    //$where = $yt->newGeoRssWhere();
                    //$position = $yt->newGmlPos('37.0 -122.0');
                    //$where->point = $yt->newGmlPoint($position);
                    //$myVideoEntry->setWhere($where);
                    $uploadUrl = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';
                    try {
                        $newEntry = $yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry');
                    } catch (Zend_Gdata_App_HttpException $httpException) {
                        echo $httpException->getRawResponseBody();
                        $DB->delete_records('mediaboard_process', array('id' => $data->id));
                    } catch (Zend_Gdata_App_Exception $e) {
                        echo $e->getMessage();
                        $DB->delete_records('mediaboard_process', array('id' => $data->id));
                    }
                    $itemidyoutube = $newEntry->getVideoId();
                    if (!empty($itemidyoutube)) {
                        $DB->set_field($table, "itemyoutube", $itemidyoutube, array("id" => $item->id));
                    }
                    $DB->delete_records('mediaboard_process', array('id' => $data->id));
                } else {
                    $DB->delete_records('mediaboard_process', array('id' => $data->id));
                }
            } else {
                if ($CFG->mediaboard_convert == 2) {
                    ///Old method
                    $DB->delete_records('mediaboard_process', array('id' => $data->id));
                    if (!($item = $DB->get_record("mediaboard_files", array("itemoldid" => $data->itemid)))) {
                        $item = $DB->get_record("mediaboard_comments", array("itemoldid" => $data->itemid));
                        $table = 'mediaboard_comments';
                    } else {
                        $table = 'mediaboard_files';
                    }
                    $student = $DB->get_record("user", array("id" => $item->userid));
                    $context = get_context_instance(CONTEXT_MODULE, $item->instance);
                    $file_record = new stdClass();
                    $file_record->component = 'mod_mediaboard';
                    $file_record->contextid = $context->id;
                    $file_record->userid = $item->userid;
                    $file_record->filearea = 'private';
                    $file_record->filepath = "/";
                    $file_record->itemid = $item->id;
                    $file_record->license = $CFG->sitedefaultlicense;
                    $file_record->author = fullname($student);
                    $file_record->source = '';
                    if (in_array($data->type, json_decode(MEDIABOARD_VIDEOTYPES))) {
                        $from = mediaboard_getfileid($data->itemid);
                        $to = $CFG->dataroot . "/temp/" . $item->filename . ".mp4";
                        $toimg = $CFG->dataroot . "/temp/" . $item->filename . ".jpg";
                        mediaboard_runExternal("/opt/handbrake/HandBrakeCLI -Z Universal -i {$from->fullpatch} -o {$to} -w 432 -l 320", $code);
                        mediaboard_runExternal("{$CFG->mediaboard_convert_url} -i {$to} -f image2 -s 432x320 {$toimg}", $code);
                        $file_record->filename = $item->filename . ".mp4";
                        $itemid = $fs->create_file_from_pathname($file_record, $to);
                        $file_record->filename = $item->filename . ".jpg";
                        $itemimgid = $fs->create_file_from_pathname($file_record, $toimg);
                        $DB->set_field($table, "itemid", $itemid->get_id(), array("id" => $item->id));
                        $DB->set_field($table, "itemimgid", $itemimgid->get_id(), array("id" => $item->id));
                        unlink($to);
                        unlink($toimg);
                    } else {
                        if (in_array($data->type, json_decode(MEDIABOARD_AUDIOTYPES))) {
                            $from = mediaboard_getfileid($data->itemid);
                            $to = $CFG->dataroot . "/temp/" . $item->filename . ".mp3";
                            mediaboard_runExternal("{$CFG->mediaboard_convert_url} -y -i {$from->fullpatch} -acodec libmp3lame -ab 68k -ar 44100 {$to}", $code);
                            $file_record->filename = $item->filename . ".mp3";
                            $itemid = $fs->create_file_from_pathname($file_record, $to);
                            $DB->set_field($table, "itemid", $itemid->get_id(), array("id" => $item->id));
                            unlink($to);
                        }
                    }
                }
            }
        }
    }
    ///Check convert server file ready
    if ($dataall = $DB->get_records_sql("SELECT * FROM {mediaboard_process} WHERE `status` = 'send'")) {
        foreach ($dataall as $data) {
            if (!($item = $DB->get_record("mediaboard_files", array("itemoldid" => $data->itemid)))) {
                if ($item = $DB->get_record("mediaboard_items", array("audio1" => $data->itemid))) {
                    $mark = 1;
                    $table = 'mediaboard_items';
                } else {
                    if ($item = $DB->get_record("mediaboard_items", array("audio2" => $data->itemid))) {
                        $mark = 2;
                        $table = 'mediaboard_items';
                    } else {
                        if ($item = $DB->get_record("mediaboard_items", array("audio3" => $data->itemid))) {
                            $mark = 3;
                            $table = 'mediaboard_items';
                        } else {
                            if ($item = $DB->get_record("mediaboard_items", array("audio4" => $data->itemid))) {
                                $mark = 4;
                                $table = 'mediaboard_items';
                            } else {
                                if ($item = $DB->get_record("mediaboard_items", array("audio5" => $data->itemid))) {
                                    $mark = 5;
                                    $table = 'mediaboard_items';
                                } else {
                                    if ($item = $DB->get_record("mediaboard_items", array("audio6" => $data->itemid))) {
                                        $mark = 6;
                                        $table = 'mediaboard_items';
                                    } else {
                                        if ($item = $DB->get_record("mediaboard_items", array("audio7" => $data->itemid))) {
                                            $mark = 7;
                                            $table = 'mediaboard_items';
                                        } else {
                                            if ($item = $DB->get_record("mediaboard_items", array("audio8" => $data->itemid))) {
                                                $mark = 8;
                                                $table = 'mediaboard_items';
                                            } else {
                                                if ($item = $DB->get_record("mediaboard_items", array("audio9" => $data->itemid))) {
                                                    $mark = 9;
                                                    $table = 'mediaboard_items';
                                                } else {
                                                    if ($item = $DB->get_record("mediaboard_items", array("audio10" => $data->itemid))) {
                                                        $mark = 10;
                                                        $table = 'mediaboard_items';
                                                    } else {
                                                        $DB->delete_records('mediaboard_process', array('id' => $data->id));
                                                        return true;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                $fid = $DB->get_record("mediaboard_files", array("id" => $item->fileid));
                $itemdata = new stdClass();
                $itemdata->userid = $item->userid;
                $itemdata->instance = $fid->instance;
                $itemdata->id = $item->id;
                $itemdata->filename = 'slide_audio';
            } else {
                $table = 'mediaboard_files';
                $itemdata = new stdClass();
                $itemdata->userid = $item->userid;
                $itemdata->instance = $item->instance;
                $itemdata->id = $item->id;
                $itemdata->filename = $item->filename;
            }
            $unicalid = substr(time(), 2) . rand(0, 9);
            $student = $DB->get_record("user", array("id" => $itemdata->userid));
            $context = get_context_instance(CONTEXT_MODULE, $itemdata->instance);
            $file_record = new stdClass();
            $file_record->component = 'mod_mediaboard';
            $file_record->contextid = $context->id;
            $file_record->userid = $itemdata->userid;
            $file_record->filearea = 'private';
            $file_record->filepath = "/";
            $file_record->itemid = $unicalid;
            $file_record->license = $CFG->sitedefaultlicense;
            $file_record->author = fullname($student);
            $file_record->source = '';
            if (in_array($data->type, json_decode(MEDIABOARD_VIDEOTYPES)) && $CFG->mediaboard_video_convert == 1) {
                $json = json_decode(file_get_contents($CFG->mediaboard_convert_url . "/get.php?name={$data->name}.mp4"));
                $jsonimg = json_decode(file_get_contents($CFG->mediaboard_convert_url . "/get.php?name={$data->name}.jpg"));
            } else {
                if ($CFG->mediaboard_audio_convert == 1) {
                    $json = json_decode(file_get_contents($CFG->mediaboard_convert_url . "/get.php?name={$data->name}.mp3"));
                }
            }
            if (@(!empty($json->url))) {
                $DB->delete_records('mediaboard_process', array('id' => $data->id));
                if (in_array($data->type, json_decode(MEDIABOARD_VIDEOTYPES))) {
                    $to = $CFG->dataroot . "/temp/" . $itemdata->filename . ".mp4";
                    file_put_contents($to, file_get_contents($json->url));
                    $file_record->filename = $itemdata->filename . ".mp4";
                    $itemid = $fs->create_file_from_pathname($file_record, $to);
                    $file = mediaboard_getfileid($itemid->get_id());
                    @chmod($file->fullpatch, 0755);
                    $DB->set_field($table, "itemid", $itemid->get_id(), array("id" => $itemdata->id));
                    $toimg = $CFG->dataroot . "/temp/" . $itemdata->filename . ".jpg";
                    file_put_contents($toimg, file_get_contents($jsonimg->url));
                    $file_record->filename = $itemdata->filename . ".jpg";
                    $itemid = $fs->create_file_from_pathname($file_record, $toimg);
                    $file = mediaboard_getfileid($itemid->get_id());
                    @chmod($file->fullpatch, 0755);
                    $DB->set_field($table, "itemimgid", $itemid->get_id(), array("id" => $itemdata->id));
                } else {
                    $to = $CFG->dataroot . "/temp/" . $itemdata->filename . ".mp3";
                    file_put_contents($to, file_get_contents($json->url));
                    $file_record->filename = $itemdata->filename . ".mp3";
                    $itemid = $fs->create_file_from_pathname($file_record, $to);
                    $file = mediaboard_getfileid($itemid->get_id());
                    @chmod($file->fullpatch, 0755);
                    if ($table != 'mediaboard_items') {
                        $DB->set_field($table, "itemid", $itemid->get_id(), array("id" => $itemdata->id));
                    } else {
                        $DB->set_field($table, "audio" . $mark, $itemid->get_id(), array("id" => $itemdata->id));
                    }
                }
                unlink($to);
                @unlink($toimg);
                break;
            }
        }
    }
    $item = $DB->get_record_sql("SELECT * FROM {mediaboard_items} WHERE `combinateaudio`=0 AND `type`='photo' LIMIT 1");
    if ($item && $DB->count_records("mediaboard_process", array()) == 0) {
        if ($CFG->mediaboard_audio_convert == 2) {
            $combinatefiles = "";
            for ($i = 1; $i <= 10; $i++) {
                $name = 'audio' . $i;
                if ($file = mediaboard_getfile($item->{$name})) {
                    $combinatefiles .= $file->fullpatch . "\\|";
                }
            }
            mediaboard_runExternal("{$CFG->mediaboard_convert_url} -i concat:{$combinatefiles} -acodec libmp3lame {$CFG->dataroot}/combinate.mp3", $code);
            $filedata = $DB->get_record("mediaboard_files", array("id" => $item->fileid));
            $contextmodule = get_context_instance(CONTEXT_MODULE, $filedata->instance);
            $unicalid = substr(time(), 2) . rand(0, 9);
            $file_record = new stdClass();
            $file_record->component = 'mod_mediaboard';
            $file_record->contextid = $contextmodule->id;
            $file_record->userid = $USER->id;
            $file_record->filearea = 'private';
            $file_record->filepath = "/";
            $file_record->itemid = $unicalid;
            $file_record->license = $CFG->sitedefaultlicense;
            $file_record->author = fullname($USER);
            $file_record->source = '';
            $file_record->filename = "combinate.mp3";
            $itemid = $fs->create_file_from_pathname($file_record, "{$CFG->dataroot}/combinate.mp3");
            unlink("{$CFG->dataroot}/combinate.mp3");
            $DB->set_field("mediaboard_items", "combinateaudio", $itemid->get_id(), array("id" => $item->id));
        } else {
            if ($CFG->mediaboard_audio_convert == 1) {
                $combinatefiles = "";
                for ($i = 1; $i <= 10; $i++) {
                    $name = 'audio' . $i;
                    if ($file = mediaboard_getfileid($item->{$name})) {
                        $link = new moodle_url("/pluginfile.php/{$file->contextid}/mod_mediaboard/0/{$file->id}/");
                        $combinatefiles .= $link . "\\|";
                    }
                }
                $combinatefiles = substr($combinatefiles, 0, -2);
                if (!empty($combinatefiles)) {
                    $data = array('combinate' => $combinatefiles);
                    $options = array('http' => array('method' => 'POST', 'content' => http_build_query($data)));
                    $context = stream_context_create($options);
                    @file_put_contents("{$CFG->dataroot}/combinate.mp3", file_get_contents($CFG->mediaboard_convert_url . '/combinate.php', false, $context));
                    $filedata = $DB->get_record("mediaboard_files", array("id" => $item->fileid));
                    $contextmodule = get_context_instance(CONTEXT_MODULE, $filedata->instance);
                    if (!empty($contextmodule->id) && !empty($item->userid)) {
                        $unicalid = substr(time(), 2) . rand(0, 9);
                        $student = $DB->get_record("user", array("id" => $item->userid));
                        //$fs->delete_area_files($contextmodule->id, 'mod_mediaboard', 'private', $unicalid);
                        $file_record = new stdClass();
                        $file_record->component = 'mod_mediaboard';
                        $file_record->contextid = $contextmodule->id;
                        $file_record->userid = $item->userid;
                        $file_record->filearea = 'private';
                        $file_record->filepath = "/";
                        $file_record->itemid = $unicalid;
                        $file_record->license = $CFG->sitedefaultlicense;
                        $file_record->author = fullname($student);
                        $file_record->source = '';
                        $file_record->filename = "combinate.mp3";
                        $itemid = $fs->create_file_from_pathname($file_record, "{$CFG->dataroot}/combinate.mp3");
                        unlink("{$CFG->dataroot}/combinate.mp3");
                        $DB->set_field("mediaboard_items", "combinateaudio", $itemid->get_id(), array("id" => $item->id));
                    } else {
                        $DB->set_field("mediaboard_items", "combinateaudio", 1, array("id" => $item->id));
                    }
                } else {
                    $DB->set_field("mediaboard_items", "combinateaudio", 1, array("id" => $item->id));
                }
            }
        }
    }
    return true;
}
Пример #2
0
            $name = 'image' . ($k + 1);
            $nameimg = 'img' . ($k + 1);
            if ($mediaboard->presetimages == 1) {
                $add->{$name} = $mediaboard->{$nameimg};
            } else {
                if ($file = mediaboard_getfile($v)) {
                    $add->{$name} = $file->id;
                }
            }
        }
        $getID3 = new getID3();
        $getID3->setOption(array('encoding' => 'UTF-8'));
        foreach ($unicalsmp3 as $k => $v) {
            $name = 'audio' . ($k + 1);
            $nameduration = 'duration' . ($k + 1);
            if ($file = mediaboard_getfile($v)) {
                $add->{$name} = $file->id;
                if ($usevoice[$v]) {
                    $ThisFileInfo = $getID3->analyze($file->fullpatch);
                    $add->{$nameduration} = @round((double) $ThisFileInfo['playtime_seconds'] * 1000);
                } else {
                    $add->{$nameduration} = 4000;
                }
            }
        }
    }
    $add->time = time();
    $DB->insert_record('mediaboard_items', $add);
    redirect($CFG->wwwroot . '/mod/mediaboard/view.php?id=' . $id, "Done");
}
//------------------------------//