config->url.'/ws/moodle_clickedu_query.php' ); global $CFG, $USER; try { if(!$action = required_param('action', PARAM_CLEAN)) { throw new Exception(get_string('required_params', 'block_clickedu'), 3); } $isteacher = getCoursesUser($USER->id, true); if(!isadmin() && !$isteacher) { throw new Exception(get_string('error_rol_required', 'block_clickedu'), 3); } switch ($action) { case 'syncLastUsers': $date = date('d/m/Y', strtotime("-7 day")); case 'syncUsers': $usuaris = json_decode($oauth->getUsers()); foreach($usuaris as $usuari){ $usuari->name = $usuari->name; $usuari->surname = $usuari->surname; } $res = sync_users( $usuaris ); unset($ws_users); $str = result_tostring(json_decode($res), true); $result_string = get_string('users').' '.$str; break; case 'syncTeacher': if(!$isteacher) { throw new Exception(get_string('wsnull_results', 'block_clickedu'), 3); } if (empty($USER->idnumber)) { throw new Exception(get_string('str_idnumber_user', 'block_clickedu'), 3); } $imp_contents = true; $ws_teachers[] = array('userid' => $USER->idnumber); case 'syncCourses': $cgaps = json_decode($oauth->getCgaps()); $raw_cgaps = array(); foreach( $cgaps as $cgap ) { $raw_cgaps[] = (int)$cgap->id; } $alumnes_cgap = $oauth->getAlumnesCgaps( $raw_cgaps ); $result_string = sync_teachers_course( $cgaps, json_decode($alumnes_cgap) ); break; case 'syncContents': //--Cursos per SQL - Tots els que tinguin idnumber son cursos sincronizats if (!$courses = get_records_sql("SELECT idnumber FROM {$CFG->prefix}course WHERE idnumber <> ''")) { throw new Exception(get_string('wsnull_results', 'block_clickedu'), 3); } $imp_content = $imp_courses = 0; //--Ini Resources foreach($courses as $course) { ++$imp_courses; if ($ws_content = WS_getContentCourse($Licences, $course->idnumber)) { ++$imp_content; sync_contents_course($course->idnumber, $ws_content); unset($ws_content); } } $result_string = str_replace('[COURSES]',$imp_courses, str_replace('[CONTENT]',$imp_content, get_string('str_contents_course', 'block_clickedu'))); add_to_log_click("clickedu", $result_string); //--End build_context_path(); break; case 'syncMoodle': if(!check_in_range(strtotime($CFG->ini_cron), strtotime(date("H:i")), strtotime($CFG->fin_cron))) { throw new Exception(get_string('str_fora_rang', 'block_clickedu'), 3); } //--Ini Users if(!$ws_users = WS_getUsers($Licences, '')) { throw new Exception(get_string('wsnull_results', 'block_clickedu'), 3); } $rs = sync_users($ws_users); $str = result_tostring($rs, true); add_to_log_click("clickedu", "getUsers OK ".$str); $result_string = get_string('users')." ".$str."\\n"; //--End //--Ini Courses if(!$ws_teachers = WS_getUsersCourse($Licences)) { throw new Exception("WS_getUsersCourse ".get_string('wsnull_results', 'block_clickedu'), 2); } $str_result = sync_teachers_course($Licences, $ws_teachers, true); //- End $result_string .= $str_result; build_context_path(); break; } } catch(Exception $e) { add_to_log_click($action, "Line:".$e->getLine()." Message:".$e->getMessage().$e->getTraceAsString()); $result_string = $e->getMessage(); } alertreturn($result_string); exit; ?>