wwwroot.'/blocks/clickedu/'); // Carreguem des de BBDD el nom d'usuari i pwd configurats if( ES_MOODLE_20 ) { $sql = 'SELECT mbi.id as id_instance, "" as id_block FROM '.$CFG->prefix.'block_instances mbi WHERE mbi.blockname = "clickedu"'; } else { $sql = 'SELECT mb.id as id_block, mbi.id as id_instance FROM '.$CFG->prefix.'block_instance mbi INNER JOIN '.$CFG->prefix.'block mb ON (mb.id = mbi.blockid) WHERE mb.name = "clickedu"'; } $block = get_records_sql( $sql ); $block = current( $block ); $id_block = $block->id_block; $id_instance=$block->id_instance; $block = getClickeduBlock( $id_instance ); $CFG->ws_user = $block->config->username; $CFG->ws_password = $block->config->password; function objectToArray($object) { if(!is_object($object) && !is_array($object)) { return $object; } if(is_object( $object )) { $object = get_object_vars($object); } return array_map('objectToArray', $object); } include_once( "compatibilitat_moodle2.php" ); /* * NuSoap SERVER WebServices ClickEDU * * - Entrega informacio sobre els usuaris de ClickEDU en un WebServices Soap PHP V1.1 * - Sistema de autentificació HEADER soap */ $server = new soap_server; /* * Configuracions Servidor WSDL => Entrega informacio Html i XML de la estructura del WebServices * setup WSDL file, a WSDL file can contain multiple services */ $server->debug_flag = false; $server->configureWSDL('MoodleToClickedu WebService WSDL', Path_WS_Moodle); $server->wsdl->schemaTargetNamespace = Path_WS_Moodle; // ==== WSDL TYPES DECLARATION ============================================== // ---- InParams --------------------------------------------------------------- $server->wsdl->addComplexType( 'InParams', 'complexType', 'struct', 'all', '', array( 'User' => array('name' => 'User', 'type' => 'xsd:string'), 'Password' => array('name' => 'Password', 'type' => 'xsd:string'), 'IdNumber' => array('name' => 'IdNumber', 'type' => 'xsd:int') ) ); $server->wsdl->addComplexType( 'InParamsAuth', 'complexType', 'struct', 'all', '', array( 'User' => array('name' => 'User', 'type' => 'xsd:string'), 'Password' => array('name' => 'Password', 'type' => 'xsd:string') ) ); $server->wsdl->addComplexType( 'InParamsScale', 'complexType', 'struct', 'all', '', array( 'User' => array('name' => 'User', 'type' => 'xsd:string'), 'Password' => array('name' => 'Password', 'type' => 'xsd:string'), 'ScaleId' => array('name' => 'ScaleId', 'type' => 'xsd:int') ) ); // ---- InParamsId --------------------------------------------------------------- $server->wsdl->addComplexType( 'InParamsId', 'complexType', 'struct', 'all', '', array( 'User' => array('name' => 'User', 'type' => 'xsd:string'), 'Password' => array('name' => 'Password', 'type' => 'xsd:string'), 'IdNumber' => array('name' => 'IdNumber', 'type' => 'xsd:int'), 'Id' => array( 'name' => 'Id', 'type' => 'xsd:int' ) ) ); $server->wsdl->addComplexType( 'InParamsTasks', 'complexType', 'struct', 'all', '', array( 'User' => array('name' => 'User', 'type' => 'xsd:string'), 'Password' => array('name' => 'Password', 'type' => 'xsd:string'), 'IdNumber' => array('name' => 'IdNumber', 'type' => 'xsd:int'), 'Id' => array( 'name' => 'Id', 'type' => 'xsd:int' ), 'OnlyTasks' => array('name' => 'OnlyTaks', 'type' => 'xsd:boolean' ) ) ); // ---- InParamsSimple --------------------------------------------------------------- $server->wsdl->addComplexType( 'InParamsSimple', 'complexType', 'struct', 'all', '', array( 'User' => array('name' => 'User', 'type' => 'xsd:string'), 'Password' => array('name' => 'Password', 'type' => 'xsd:string'), 'UserId' => array( 'name' => 'UserId', 'type' => 'xsd:int' ) ) ); // ---- OutParamsTasks --------------------------------------------------------- $server->wsdl->addComplexType( 'OutParamsTasks', 'complexType', 'struct', 'all', '', array ( 'Code' => array ( 'name' => 'Code', 'type' => 'xsd:int' ), 'Result' => array ( 'name' => 'Result', 'type' => 'tns:TasksItems' ) ) ); // ---- OutParams --------------------------------------------------------- $server->wsdl->addComplexType( 'OutParams', 'complexType', 'struct', 'all', '', array ( 'Code' => array ( 'name' => 'Code', 'type' => 'xsd:int' ), 'Result' => array ( 'name' => 'Result', 'type' => 'tns:GradeItems' ) ) ); // ---- GradeItems[] --------------------------------------------------------- $server->wsdl->addComplexType( 'TasksItems', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array( array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:TaskItem[]') ), 'tns:TaskItem' ); // ---- TaskItem --------------------------------------------------------- $server->wsdl->addComplexType( 'TaskItem', 'complexType', 'struct', 'all', '', array( 'id' => array('name' => 'id', 'type' => 'xsd:int'), 'courseid' => array('name' => 'courseid', 'type' => 'xsd:int'), 'itemname' => array('name' => 'itemname', 'type' => 'xsd:string'), 'itemtype' => array('name' => 'itemtype', 'type' => 'xsd:string'), 'itemmodule' => array('name' => 'itemmodule', 'type' => 'xsd:string'), 'iteminstance' => array('name' => 'iteminstance', 'type' => 'xsd:int'), 'itemnumber' => array('name' => 'itemnumber', 'type' => 'xsd:int'), 'idnumber' => array('name' => 'idnumber', 'type' => 'xsd:int'), 'outcomes' => array( 'name' => 'outcomes', 'type' => 'tns:outcomesList') ) ); // ---- GradeItems[] --------------------------------------------------------- $server->wsdl->addComplexType( 'outcomesList', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array( array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'xsd:outcomeItem[]') ), 'tns:outcomeItem' ); $server->wsdl->addComplexType( 'outcomeItem', 'complexType', 'struct', 'all', '', array( 'id' => array('name' => 'id', 'type' => 'xsd:int'), 'outcomeid' => array('name' => 'outcomeid', 'type' => 'xsd:int') ) ); // ---- GradeItems[] --------------------------------------------------------- $server->wsdl->addComplexType( 'GradeItems', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array( array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:GradeItem[]') ), 'tns:GradeItem' ); // ---- GradeItem --------------------------------------------------------- $server->wsdl->addComplexType( 'GradeItem', 'complexType', 'struct', 'all', '', array( 'id' => array('name' => 'id', 'type' => 'xsd:int'), 'courseid' => array('name' => 'courseid', 'type' => 'xsd:int'), 'itemname' => array('name' => 'itemname', 'type' => 'xsd:string'), 'itemtype' => array('name' => 'itemtype', 'type' => 'xsd:string'), 'itemmodule' => array('name' => 'itemmodule', 'type' => 'xsd:string'), 'iteminstance' => array('name' => 'iteminstance', 'type' => 'xsd:int'), 'itemnumber' => array('name' => 'itemnumber', 'type' => 'xsd:int'), 'idnumber' => array('name' => 'idnumber', 'type' => 'xsd:int') ) ); // ---- OutParamsGrade --------------------------------------------------------- $server->wsdl->addComplexType( 'OutParamsGrade', 'complexType', 'struct', 'all', '', array ( 'Code' => array ( 'name' => 'Code', 'type' => 'xsd:int' ), 'Result' => array ( 'name' => 'Result', 'type' => 'tns:Grades' ) ) ); $server->wsdl->addComplexType( 'OutParamsCourse', 'complexType', 'struct', 'all', '', array ( 'Code' => array ( 'name' => 'Code', 'type' => 'xsd:int' ), 'Result' => array ( 'name' => 'Result', 'type' => 'tns:OutCourses' ) ) ); $server->wsdl->addComplexType( 'OutParamsOutcomes', 'complexType', 'struct', 'all', '', array( 'Code' => array ( 'name' => 'Code', 'type' => 'xsd:int' ), 'Result' => array ( 'name' => 'Result', 'type' => 'tns:OutOutcomes' ) ) ); $server->wsdl->addComplexType( 'OutOutcomes', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array( array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:OutOutcome[]') ), 'tns:OutOutcome' ); $server->wsdl->addComplexType( 'OutOutcome', 'complexType', 'struct', 'all', '', array( 'OutcomeName' => array( 'name' => 'OutcomeName', 'type' => 'xsd:string' ), 'ExternId' => array( 'name' => 'ExternId', 'type' => 'xsd:int' ), 'OutcomeShort' => array( 'name' => 'OutcomeShort', 'type' => 'xsd:string' ), 'OutcomeDescription' => array( 'name' => 'OutcomeDescription', 'type' => 'xsd:string' ), 'OutcomeCreacio' => array( 'name' => 'OutcomeCreacio', 'type' => 'xsd:int' ), 'OutcomeScale' => array( 'name' => 'OutcomeScale', 'type' => 'xsd:int' ) ) ); $server->wsdl->addComplexType( 'OutParamsScales', 'complexType', 'struct', 'all', '', array( 'Code' => array ( 'name' => 'Code', 'type' => 'xsd:int' ), 'Result' => array ( 'name' => 'Result', 'type' => 'tns:OutScales' ) ) ); $server->wsdl->addComplexType( 'OutScales', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array( array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:OutScale[]') ), 'tns:OutScale' ); $server->wsdl->addComplexType( 'OutScale', 'complexType', 'struct', 'all', '', array( 'ScaleItemId' => array( 'name' => 'ScaleItemId', 'type' => 'xsd:string' ), 'ScaleItemName' => array( 'name' => 'ScaleItemName', 'type' => 'xsd:string' ), 'DownBoundary' => array( 'name' => 'DownBoundary', 'type' => 'xsd:decimal' ), 'UpBoundary' => array( 'name' => 'UpBoundary', 'type' => 'xsd:decimal' ) ) ); // ----- OutCourses[] ----------------------------------------------------------- $server->wsdl->addComplexType( 'OutCourses', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array( array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:OutCourse[]') ), 'tns:OutCourse' ); // ----- OutCourse ------------------------------------------------------------- $server->wsdl->addComplexType( 'OutCourse', 'complexType', 'struct', 'all', '', array( 'CourseName' => array( 'name' => 'CourseName', 'type' => 'xsd:string' ), 'ExternId' => array( 'name' => 'ExternId', 'type' => 'xsd:int' ) ) ); // ---- GradeItems[] --------------------------------------------------------- $server->wsdl->addComplexType( 'Grades', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array( array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Grade[]') ), 'tns:Grade' ); // ---- GradeItem --------------------------------------------------------- $server->wsdl->addComplexType( 'Grade', 'complexType', 'struct', 'all', '', array( 'id' => array('name' => 'id', 'type' => 'xsd:int'), 'itemid' => array('name' => 'itemid', 'type' => 'xsd:int'), 'userid' => array('name' => 'userid', 'type' => 'xsd:int'), 'idnumber' => array('name' => 'idnumber', 'type' => 'xsd:int'), 'rawgrade' => array('name' => 'rawgrade', 'type' => 'xsd:decimal'), 'rawgrademax' => array('name' => 'rawgrademax', 'type' => 'xsd:decimal'), 'rawgrademin' => array('name' => 'rawgrademin', 'type' => 'xsd:decimal'), 'rawscaleid' => array('name' => 'rawscaleid', 'type' => 'xsd:int'), 'finalgrade' => array('name' => 'finalgrade', 'type' => 'xsd:decimal') ) ); // ==== WSDL METHODS REGISTRATION =========================================== // getUserLogin $server->register( 'getGradesItemsCourse', // method name array('Input' => 'tns:InParamsId'), // input parameters array('Output' => 'tns:OutParams'), // output parameters Path_WS_Moodle // namespace ); // getUserLogin $server->register( 'getGradeItems', // method name array('Input' => 'tns:InParams'), // input parameters array('Output' => 'tns:OutParamsGrade'), // output parameters Path_WS_Moodle // namespace ); // getUserLogin $server->register( 'getIdCourse', // method name array('Input' => 'tns:InParams'), // input parameters array('courseid' => 'xsd:int'), // output parameters Path_WS_Moodle // namespace ); if( ES_MOODLE_20 ) { // getMoodleCourses $server->register( 'getCourses', array('Input' => 'tns:InParamsSimple'), array('Courses' => 'tns:OutParamsCourse' ), Path_WS_Moodle ); $server->register( 'getOutcomes', array( 'Input' => 'tns:InParamsAuth'), array( 'Outcomes' => 'tns:OutParamsOutcomes'), Path_WS_Moodle ); $server->register( 'getScales', array( 'Input' => 'tns:InParamsScale'), array( 'Scales' => 'tns:OutParamsScales'), Path_WS_Moodle ); $server->register( 'getTasksItemsCourse', array( 'Input' => 'tns:InParamsTasks'), array( 'Output'=> 'tns:OutParamsTasks'), Path_WS_Moodle ); } // ==== PROCESS REQUEST ===================================================== $HTTP_RAW_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : ''; $server->service($HTTP_RAW_POST_DATA); exit(); function getGradesItemsCourse($data) { global $CFG; $username = utf8_encode( $data['User'] ); $password = utf8_encode( $data['Password'] ); $idnumber = $data['IdNumber']; $id = $data['Id']?$data['Id']:0; $onlyTasks= $data['OnlyTasks']; $Return->Code = -4; $Return->Result = NULL; if(!empty($username) and !empty($password) and !empty($idnumber)) { $Return->Code = -3; if($username == $CFG->ws_user and $password == $CFG->ws_password) { $Return->Code = -2; if( $id ) { $courseid = $id; } else { $sql = "SELECT id FROM mdl_course WHERE idnumber = '".$idnumber."'"; $res = get_record_sql($sql); $Return->Code = $idnumber; if( isset( $res->id ) ) $courseid = $res->id; } if($courseid) { $Return->Code = -1; if( $onlyTasks ) { $sel = ',outcomeid'; // $ob = ' ORDER BY itemtype, itemmodule, iteminstance, itemnumber'; $wh = ' AND gradetype = 1'; } $sql = "SELECT id, courseid, itemname, itemtype, itemmodule, iteminstance, itemnumber, idnumber".$sel." FROM mdl_grade_items WHERE courseid = '".$courseid."' ".$wh; $res = get_records_sql($sql); if($res !== false) { if( $onlyTasks ) { $new_res = array(); foreach( $res as $result ) { $sql2 = 'SELECT id, outcomeid FROM mdl_grade_items WHERE itemtype = "'.$result->itemtype.'" AND itemmodule="'.$result->itemmodule.'" AND iteminstance="'.$result->iteminstance.'" AND outcomeid IS NOT NULL;'; $res2 = get_records_sql( $sql2 ); $result->outcomes = $res2; $new_res[] = $result; } $Return->Code = 1; $Return->Result = $new_res; } else { $Return->Code = 1; $Return->Result = $res; } } } } } return objectToArray($Return); } function getTasksItemsCourse($data) { return getGradesItemsCourse( $data ); } function getGradeItems($data) { global $CFG; include_once($CFG->libdir.'/gradelib.php'); $username = utf8_encode( $data['User'] ); $password = utf8_encode( $data['Password'] ); $itemid = $data['IdNumber']; $Return->Code = -3; $Return->Result = NULL; if(!empty($username) and !empty($password) and !empty($itemid)) { $Return->Code = -2; if($username == $CFG->ws_user and $password == $CFG->ws_password) { $Return->Code = -1; $sql = "SELECT gg.id, gg.itemid, gg.userid, u.idnumber, gg.rawgrade, gg.rawgrademax, gg.rawgrademin, gg.rawscaleid, gg.finalgrade FROM mdl_grade_grades gg INNER JOIN mdl_user u ON (u.id = gg.userid) WHERE gg.itemid = '".$itemid."'"; $res = get_records_sql($sql); if($res !== false) { $Return->Code = 1; $Return->Result = $res; } } } return objectToArray($Return); } function getCourses( $data ) { global $CFG, $DB; require_once( $CFG->libdir."/enrollib.php" ); $return = new Object(); $username = utf8_encode( $data['User'] ); $password = utf8_encode( $data['Password'] ); $userid = $data['UserId']; $return->Code = -3; $return->Result = NULL; if(!empty($username) and !empty($password) /*and !empty($userid)*/) { $return->Code = -2; if($username == $CFG->ws_user and $password == $CFG->ws_password) { $return->Code = $userid?$userid:0; // Fem la conversió de l'identificador d'usuari de Clickedu a identificador d'usuari moodle $u = $DB->get_records( 'user', array( 'idnumber' => $userid ) ); if(!$u && $userid) $return->Code=-4; else { // ---------------------------------------------------------------------------------------- // Col·leccionem les dades en un array de courses pla, amb identificador i nom $plain_courses = array(); if($username == $CFG->ws_user and $password == $CFG->ws_password) { $return->Code = 0; if( $u ) { // Ens quedem amb el primer usuari que haguem trobat a la BBDD $user = current( $u ); // Cerca dels cursos als quals assisteix l'usuari. Hauriem de filtrar segons si és professor o no, // però de moment, no deixem feta aquesta funcionalitat $courses = enrol_get_users_courses( $user->id ); } else { // Cerca SENSE usuari. Retornem TOTS els cursos! $courses = get_courses(); } foreach( $courses as $course ) { $curs = array(); $curs['CourseName'] = utf8_decode( $course->fullname ); $curs['ExternId'] = $course->id; $plain_courses[] = $curs; } } $return->Result = $plain_courses; } } } return objectToArray( $return ); } function getOutcomes( $data ) { global $CFG, $DB; $return = new Object(); $username = utf8_encode( $data['User'] ); $password = utf8_encode( $data['Password'] ); $return->Code = -3; $return->Result = NULL; if(!empty($username) and !empty($password) /*and !empty($userid)*/) { $return->Code = -2; if($username == $CFG->ws_user and $password == $CFG->ws_password) { // Usuari vàlid $return->Code = 1; // Llegim la taula de Outcomes( Competències bàsiques ) $records = $DB->get_records( 'grade_outcomes' ); $outcomes = array(); // Iterem pels resultats i obtenim els resultats foreach( $records as $outcome ) { $new_outcome = array(); $new_outcome['OutcomeShort'] = $outcome->shortname; $new_outcome['OutcomeName'] = $outcome->fullname; $new_outcome['ExternId'] = $outcome->id; // Passem la descripció a text plà, extreient qualsevol tag d'html que pogues haver-hi guardat $new_outcome['OutcomeDescription'] = strip_tags($outcome->description); $new_outcome['OutcomeCreacio'] = $outcome->timecreated; $new_outcome['OutcomeScale'] = $outcome->scaleid; $outcomes[] = $new_outcome; } $return->Result = $outcomes; } } return objectToArray( $return ); } function getScales( $data ) { global $CFG, $DB; $return = new Object(); $username = utf8_encode( $data['User'] ); $password = utf8_encode( $data['Password'] ); $scaleId =$data['ScaleId']; $return->Code = -3; $return->Result = NULL; if(!empty($username) and !empty($password) and !empty($scaleId)) { $return->Code = -2; if($username == $CFG->ws_user and $password == $CFG->ws_password) { // Usuari vàlid $return->Code = $scaleId; // Llegim la escala amb l'identificador d'entrada $scale_record = $DB->get_records( 'scale', array( 'id'=> $scaleId ) ); $scale_record = current( $scale_record ); $items = explode(',', $scale_record->scale ); // Iterem pels resultats i obtenim els resultats $i = 1; $scales = array(); foreach( $items as $scale_name ) { $new_item = array(); $new_item['ScaleItemId'] = $scaleId.'#'.$i; $new_item['ScaleItemName'] = $scale_name; $new_item['DownBoundary'] = $i; $new_item['UpBoundary'] = ++$i; $scales[] = $new_item; } $return->Result = $scales; } } $ret = objectToArray( $return ); return $ret; } function getIdCourse($data) { global $CFG; $username = utf8_encode( $data['User'] ); $password = utf8_encode( $data['Password'] ); $idnumber = $data['IdNumber']; $courseid = -3; if(!empty($username) and !empty($password) and !empty($idnumber)) { $courseid = -2; //$courseid = $CFG->ws_user; if($username == $CFG->ws_user and $password == $CFG->ws_password) { $courseid = -1; $sql = "SELECT c.id FROM mdl_course c WHERE c.idnumber = '".$idnumber."'"; $res = get_record_sql($sql); if($res !== false and isset($res->id)) { $courseid = $res->id; } } } return $courseid; }