$CFG->oauth_consumer,
'consumer_secret' => $CFG->oauth_consumer_secret,
'host' => 'https://'.$url,
'use_ssl' => true, // Change this on production
);
$tokens = getAccessTokens();
if( $tokens )
{
$config['token'] = $tokens['token'];
$config['secret']= $tokens['secret'];
}
$oauth = new OAuthMoodle( $config );
return $oauth;
}
function getClickeduBlock( $id )
{
try{
if( ES_MOODLE_20 )
{
global $DB;
$instance = $DB->get_record( 'block_instances', array('id'=> $id) );
}
else
{
// OJO!!! A la 1.9, la taula és block-instance i a la 2.x és amb s final! (block_instances)
$instance = get_record( 'block_instance', 'id', $id );
}
}
catch( Exception $e )
{
return NULL;
}
$block = block_instance( 'clickedu', $instance );
return $block;
}
function getAccessTokens()
{
if( ES_MOODLE_20 )
$plugin = 'core';
else
$plugin = NULL;
$access_token = get_config( $plugin, 'access_token' );
$access_secret= get_config( $plugin, 'access_secret');
$ret = NULL;
if( $access_token && $access_secret )
$ret = array( 'token' => $access_token, 'secret' => $access_secret );
return $ret;
}
/*
* @param int
*/
function getCoursesUser($userid, $rol=false) {
global $CFG;
global $DB;
if(empty($userid)) return false;
$and_rol = '';
if($rol) {
$and_rol = "AND r.shortname = 'editingteacher'";
}
$sql = "SELECT
con.instanceid,
cou.idnumber,
ras.contextid
FROM {$CFG->prefix}course cou
INNER JOIN {$CFG->prefix}context con ON (cou.id = con.instanceid)
INNER JOIN {$CFG->prefix}role_assignments ras ON (con.id = ras.contextid)
INNER JOIN {$CFG->prefix}role r ON (ras.roleid = r.id)
WHERE cou.idnumber <> ''
".$and_rol."
AND ras.userid = ".$userid;
//INNER JOIN {$CFG->prefix}role r ON (ras.roleid = r.id AND r.shortname = 'editingteacher')
if( ES_MOODLE_20 )
return $DB->get_records_sql( $sql );
else
return get_records_sql($sql);
}
function check_in_range($a, $b, $c) {
if(date("H", $a) > 12 and ($a - $b) > 0){
$a = strtotime("-1 day", $a);
}
if($a > $c){
$c = strtotime("+1 day", $c);
}
//echo '
inicio '.date("Y-m-d H:i", $a).' ahora '.date("Y-m-d H:i", $b).' fin '.date("Y-m-d H:i", $c);
//echo "
___".($b >= $a);
//echo "
___".($b <= $c);
return (($b >= $a) && ($b <= $c));
}
function load_table($table) {
global $CACHE_TABLE;
if(!is_array($CACHE_TABLE)) {
$CACHE_TABLE = array();
}
if ($table != '') {
$CACHE_TABLE[$table] = array_keys( get_object_vars( get_record_sql( 'SELECT * FROM '.$table.' LIMIT 1' ) ) );
return $CACHE_TABLE[$table];
}
}
function protected_field($v) {
if (is_string($v)) {
$v = ($v != '') ? "'".mysql_real_escape_string(mb_convert_encoding($v, "UTF-8"))."'" : "NULL";
}
else if(is_int($v)) {
$v = (int)$v;
}
else {
}
return $v;
}
function load_strings($str='') {
global $load_strings;
if(!is_array($str))
$str = array('required_idnumber', 'required_params', 'error_rol_required', 'error_wsLogin', 'wsnull_results', 'total', 'import', 'delete', 'error', 'update', 'content');
foreach($str as $key) {
if(($string = get_string($key, 'block_clickedu')) or ($string = get_string($key)))
$arrstring[$key] = $string;
}
$load_strings = $arrstring;
return $load_strings;
}
function add_to_log_click($accio, $missatge, $log_moodle=false) {
global $CFG, $USER;
$dir_log = $CFG->dirroot.'/blocks/clickedu/logs/';
if(!is_dir($dir_log)) {
mkdir($dir_log, 0777);
}
$dir_log .= 'log.log';
if($log_moodle) {
$href = 'view.php?id='.SITEID;
add_to_log(SITEID, 'course', $accio, $href, $missatge);
}
$userid = empty($USER->id) ? '0' : $USER->id;
$REMOTE_ADDR = getremoteaddr();
if (empty($REMOTE_ADDR)) {
$REMOTE_ADDR = '0.0.0.0';
}
$data = date("Y-m-d H:i:s")." - ".$REMOTE_ADDR." - ".$userid." - ".$accio." - ".$missatge;
$doc = fopen($dir_log, 'a+');
if($doc) {
fwrite($doc, $data.PHP_EOL);
fclose($doc);
return true;
}
return false;
}
function alertreturn($massage, $return=false) {
global $CFG;
$massage = ($massage=='') ? 'nostring' : $massage;
$msg = "";
if ($return)
return $msg;
echo $msg;
die;
}
function result_tostring($result=NULL, $return=NULL) {
global $load_strings;
$string = '';
if(is_array($result)) {
foreach($result as $key => $value) {
if( ($str = get_string($key, 'block_clickedu')) or ($str = get_string($key)) or ($str = $key)) {
$string .= $str." ".$value." ";
}
}
}
if($return) return $string;
alertreturn($string);
}
function pre($print) {
echo '
'.print_r($print,true).''; } function convert($size) { $unit=array('b','kb','mb','gb','tb','pb'); return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; } function memory_usage($marca) { echo '