. // // // /////////////////////////////////////////////////////////////////////////// /** * Web service documentation renderer. * @package webservice * @copyright 2009 Moodle Pty Ltd (http://moodle.com) * @author Jerome Mouneyrac * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class core_webservice_renderer extends plugin_renderer_base { /** * Display Reset token confirmation box * @param object $token to reset * @return string html */ public function user_reset_token_confirmation($token) { global $OUTPUT, $CFG; $managetokenurl = $CFG->wwwroot."/user/managetoken.php?sesskey=" . sesskey(); $optionsyes = array('tokenid'=>$token->id, 'action'=>'resetwstoken', 'confirm'=>1, 'sesskey'=>sesskey()); $optionsno = array('section'=>'webservicetokens', 'sesskey'=>sesskey()); $formcontinue = new single_button(new moodle_url($managetokenurl, $optionsyes), get_string('reset')); $formcancel = new single_button(new moodle_url($managetokenurl, $optionsno), get_string('cancel'), 'get'); $html = $OUTPUT->confirm(get_string('resettokenconfirm', 'webservice', (object)array('user'=>$token->firstname." ".$token->lastname, 'service'=>$token->name)), $formcontinue, $formcancel); return $html; } /** * Display user tokens with buttons to reset them * @param object $tokens * @param int $userid * @return string html code */ public function user_webservice_tokens_box($tokens, $userid) { global $OUTPUT, $CFG; // display strings $stroperation = get_string('operation', 'webservice'); $strtoken = get_string('key', 'webservice'); $strservice = get_string('service', 'webservice'); $strcreator = get_string('tokencreator', 'webservice'); $strcontext = get_string('context', 'webservice'); $strvaliduntil = get_string('validuntil', 'webservice'); $return = $OUTPUT->heading(get_string('securitykeys', 'webservice'), 3, 'main', true); $return .= $OUTPUT->box_start('generalbox webservicestokenui'); $return .= get_string('keyshelp', 'webservice'); $table = new html_table(); $table->head = array($strtoken, $strservice, $strvaliduntil, $strcreator, $stroperation); $table->align = array('left', 'left', 'left', 'center', 'left', 'center'); $table->width = '100%'; $table->data = array(); if (!empty($tokens)) { foreach ($tokens as $token) { //TODO: retrieve context if ($token->creatorid == $userid) { $reset = "wwwroot."/user/managetoken.php?sesskey=".sesskey(). "&action=resetwstoken&tokenid=".$token->id."\">"; $reset .= get_string('reset').""; $creator = $token->firstname." ".$token->lastname; } else { //retrive administrator name require_once($CFG->dirroot.'/user/lib.php'); $creators = user_get_users_by_id(array($token->creatorid)); $admincreator = $creators[$token->creatorid]; $creator = $admincreator->firstname." ".$admincreator->lastname; $reset = ''; } $userprofilurl = new moodle_url('/user/view.php?id='.$token->creatorid); $creatoratag = html_writer::start_tag('a', array('href' => $userprofilurl)); $creatoratag .= $creator; $creatoratag .= html_writer::end_tag('a'); $validuntil = ''; if (!empty($token->validuntil)) { $validuntil = date("F j, Y"); //TODO: language support (look for moodle function) } $table->data[] = array($token->token, $token->name, $validuntil, $creatoratag, $reset); } $return .= html_writer::table($table); } else { $return .= get_string('notoken', 'webservice'); } $return .= $OUTPUT->box_end(); return $return; } /** * Return documentation for a ws description object * ws description object can be 'external_multiple_structure', 'external_single_structure' or 'external_value' * Example of documentation for moodle_group_create_groups function: list of ( object { courseid int //id of course name string //multilang compatible name, course unique description string //group description text enrolmentkey string //group enrol secret phrase } ) * @param object $params a part of parameter/return description * @return string the html to display */ public function detailed_description_html($params) { /// retrieve the description of the description object $paramdesc = ""; if (!empty($params->desc)) { $paramdesc .= html_writer::start_tag('span', array('style' => "color:#2A33A6")); if ($params->required == VALUE_REQUIRED) { $required = ''; } if ($params->required == VALUE_DEFAULT) { if ($params->default === null) { $params->default = "null"; } $required = html_writer::start_tag('b', array()).get_string('default', 'webservice', $params->default).html_writer::end_tag('b'); } if ($params->required == VALUE_OPTIONAL) { $required = html_writer::start_tag('b', array()).get_string('optional', 'webservice').html_writer::end_tag('b'); } $paramdesc .= " ".$required." "; $paramdesc .= html_writer::start_tag('i', array()); $paramdesc .= "//"; $paramdesc .= $params->desc; $paramdesc .= html_writer::end_tag('i'); $paramdesc .= html_writer::end_tag('span'); $paramdesc .= html_writer::empty_tag('br', array()); } /// description object is a list if ($params instanceof external_multiple_structure) { return $paramdesc . "list of ( " . html_writer::empty_tag('br', array()) . $this->detailed_description_html($params->content) . ")"; } else if ($params instanceof external_single_structure) { /// description object is an object $singlestructuredesc = $paramdesc."object {". html_writer::empty_tag('br', array()); foreach ($params->keys as $attributname => $attribut) { $singlestructuredesc .= html_writer::start_tag('b', array()); $singlestructuredesc .= $attributname; $singlestructuredesc .= html_writer::end_tag('b'); $singlestructuredesc .= " ".$this->detailed_description_html($params->keys[$attributname]); } $singlestructuredesc .= "} "; $singlestructuredesc .= html_writer::empty_tag('br', array()); return $singlestructuredesc; } else { /// description object is a primary type (string, integer) switch($params->type) { case PARAM_BOOL: // 0 or 1 only for now case PARAM_INT: $type = 'int'; break; case PARAM_FLOAT; $type = 'double'; break; default: $type = 'string'; } return $type." ".$paramdesc; } } /** * Return a description object in indented xml format (for REST response) * It is indented in order to be displayed into
 tag
     * @param object $returndescription
     * @param string $indentation composed by space only
     * @return string the html to diplay
     */
    public function description_in_indented_xml_format($returndescription, $indentation = "") {
        $indentation = $indentation . "    ";
        $brakeline = <<".$brakeline;
            $return .= $this->description_in_indented_xml_format($returndescription->content, $indentation);
            $return .= $indentation."".$brakeline;
            return $return;
        } else if ($returndescription instanceof external_single_structure) {
    /// description object is an object
            $singlestructuredesc = $indentation."".$brakeline;
            $keyindentation = $indentation."    ";
            foreach ($returndescription->keys as $attributname => $attribut) {
                $singlestructuredesc .= $keyindentation."".$brakeline.
                        $this->description_in_indented_xml_format($returndescription->keys[$attributname], $keyindentation).
                        $keyindentation."".$brakeline;
            }
            $singlestructuredesc .= $indentation."".$brakeline;
            return $singlestructuredesc;
        } else {
    /// description object is a primary type (string, integer)
            switch($returndescription->type) {
                case PARAM_BOOL: // 0 or 1 only for now
                case PARAM_INT:
                    $type = 'int';
                    break;
                case PARAM_FLOAT;
                    $type = 'double';
                    break;
                default:
                    $type = 'string';
            }
            return $indentation."".$type."".$brakeline;
        }
    }

     /**
     * Create indented XML-RPC  param description
     * @param object $paramdescription
     * @param string $indentation composed by space only
     * @return string the html to diplay
     */
    public function xmlrpc_param_description_html($paramdescription, $indentation = "") {
        $indentation = $indentation . "    ";
        $brakeline = <<";
            $return .= $this->xmlrpc_param_description_html($paramdescription->content, $indentation);
            $return .= $brakeline.$indentation.")";
            return $return;
        } else if ($paramdescription instanceof external_single_structure) {
    /// description object is an object
            $singlestructuredesc = $brakeline.$indentation."Array ";
            $keyindentation = $indentation."    ";
            $singlestructuredesc  .= $brakeline.$keyindentation."(";
            foreach ($paramdescription->keys as $attributname => $attribut) {
                $singlestructuredesc .= $brakeline.$keyindentation."[".$attributname."] =>".
                        $this->xmlrpc_param_description_html($paramdescription->keys[$attributname], $keyindentation).
                        $keyindentation;
            }
            $singlestructuredesc .= $brakeline.$keyindentation.")";
            return $singlestructuredesc;
        } else {
    /// description object is a primary type (string, integer)
            switch($paramdescription->type) {
                case PARAM_BOOL: // 0 or 1 only for now
                case PARAM_INT:
                    $type = 'int';
                    break;
                case PARAM_FLOAT;
                    $type = 'double';
                    break;
                default:
                    $type = 'string';
            }
            return " ".$type;
        }
    }

    /**
     * Return the html of a colored box with content
     * @param string $title - the title of the box
     * @param string $content - the content to displayed
     * @param string $rgb - the background color of the box
     * @return 
     */
    public function colored_box_with_pre_tag($title, $content, $rgb = 'FEEBE5') {
        $coloredbox = html_writer::start_tag('ins', array()); //TODO: this tag removes xhtml strict error but cause warning
        $coloredbox .= html_writer::start_tag('div', array('style' => "border:solid 1px #DEDEDE;background:#".$rgb.";color:#222222;padding:4px;"));
        $coloredbox .= html_writer::start_tag('pre', array());
        $coloredbox .= html_writer::start_tag('b', array());
        $coloredbox .= $title;
        $coloredbox .= html_writer::end_tag('b', array());
        $coloredbox .= html_writer::empty_tag('br', array());
        $coloredbox .= "\n".$content."\n";
        $coloredbox .= html_writer::end_tag('pre', array());
        $coloredbox .= html_writer::end_tag('div', array());
        $coloredbox .= html_writer::end_tag('ins', array());
        return $coloredbox;
    }


     /**
     * Return indented REST param description
     * @param object $paramdescription
     * @param string $indentation composed by space only
     * @return string the html to diplay
     */
    public function rest_param_description_html($paramdescription, $paramstring) {
        $brakeline = <<rest_param_description_html($paramdescription->content, $paramstring);
            return $return;
        } else if ($paramdescription instanceof external_single_structure) {
    /// description object is an object
            $singlestructuredesc = "";
            $initialparamstring = $paramstring;
            foreach ($paramdescription->keys as $attributname => $attribut) {
                $paramstring = $initialparamstring.'['.$attributname.']';
                $singlestructuredesc .= $this->rest_param_description_html($paramdescription->keys[$attributname], $paramstring);
            }
            return $singlestructuredesc;
        } else {
    /// description object is a primary type (string, integer)
            $paramstring = $paramstring.'=';
            switch($paramdescription->type) {
                case PARAM_BOOL: // 0 or 1 only for now
                case PARAM_INT:
                    $type = 'int';
                    break;
                case PARAM_FLOAT;
                    $type = 'double';
                    break;
                default:
                    $type = 'string';
            }
            return $paramstring." ".$type.$brakeline;
        }
    }


    /**
     * This display all the documentation
     * @param array $functions contains all decription objects
     * @param array $authparam keys are either 'username'/'password' or 'token'
     * @param boolean $printableformat true if we want to display the documentation in a printable format
     * @param array $activatedprotocol
     * @return string the html to diplay
     */
    public function documentation_html($functions, $printableformat, $activatedprotocol, $authparams) {
        global $OUTPUT, $CFG;
        $br = html_writer::empty_tag('br', array());
        $brakeline = << "margin-left:auto; margin-right:auto;"));
        $documentationhtml .= html_writer::start_tag('tr', array());
        $documentationhtml .= html_writer::start_tag('td', array());
        $documentationhtml .= get_string('wsdocumentationintro', 'webservice', $authparams['wsusername']);
        $documentationhtml .= $br.$br;
        

    /// Print button
        $authparams['print'] = true;
        //$parameters = array ('token' => $token, 'wsusername' => $username, 'wspassword' => $password, 'print' => true);
        $url = new moodle_url('/webservice/wsdoc.php', $authparams); // Required
        $documentationhtml .= $OUTPUT->single_button($url, get_string('print','webservice'));
        $documentationhtml .= $br;
        
        
    /// each functions will be displayed into a collapsible region (opened if printableformat = true)
        foreach ($functions as $functionname => $description) {

            if (empty($printableformat)) {
                $documentationhtml .= print_collapsible_region_start('',
                                                                 'aera_'.$functionname,
                                                                 html_writer::start_tag('strong', array()).$functionname.html_writer::end_tag('strong'),
                                                                 false,
                                                                 !$printableformat,
                                                                 true);
            } else {
                $documentationhtml .= html_writer::tag('strong', $functionname);
                $documentationhtml .= $br;
            }

        /// function global description
            $documentationhtml .= $br;
            $documentationhtml .= html_writer::start_tag('div', array('style' => 'border:solid 1px #DEDEDE;background:#E2E0E0;color:#222222;padding:4px;'));
            $documentationhtml .= $description->description;
            $documentationhtml .= html_writer::end_tag('div');
            $documentationhtml .= $br.$br;

        /// function arguments documentation
            $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6'));
            $documentationhtml .= get_string('arguments', 'webservice');
            $documentationhtml .= html_writer::end_tag('span');
            $documentationhtml .= $br;
            foreach ($description->parameters_desc->keys as $paramname => $paramdesc) {
            /// a argument documentation
                $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%'));
                                
                if ($paramdesc->required == VALUE_REQUIRED) {
                      $required = get_string('required', 'webservice');
                }
                if ($paramdesc->required == VALUE_DEFAULT) {
                    if ($paramdesc->default === null) {
                        $default = "null";
                    } else {
                        $default = $paramdesc->default;
                    }
                    $required = get_string('default', 'webservice', $default);
                }
                if ($paramdesc->required == VALUE_OPTIONAL) {
                      $required = get_string('optional', 'webservice');
                }
                
                $documentationhtml .= html_writer::start_tag('b', array());
                $documentationhtml .= $paramname;
                $documentationhtml .= html_writer::end_tag('b');
                $documentationhtml .= " (" .$required. ")"; // argument is required or optional ?
                $documentationhtml .= $br;
                $documentationhtml .= "        ".$paramdesc->desc; // argument description
                $documentationhtml .= $br.$br;
                ///general structure of the argument
                $documentationhtml .= $this->colored_box_with_pre_tag(get_string('generalstructure', 'webservice'), 
                                                                      $this->detailed_description_html($paramdesc),
                                                                      'FFF1BC');
                ///xml-rpc structure of the argument in PHP format
                if (!empty($activatedprotocol['xmlrpc'])) {
                    $documentationhtml .= $this->colored_box_with_pre_tag(get_string('phpparam', 'webservice'), 
                                                                          htmlentities('['.$paramname.'] =>'.$this->xmlrpc_param_description_html($paramdesc)),
                                                                          'DFEEE7');
                }
                ///POST format for the REST protocol for the argument
                if (!empty($activatedprotocol['rest'])) {
                    $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restparam', 'webservice'), 
                                                                          htmlentities($this->rest_param_description_html($paramdesc,$paramname)),
                                                                          'FEEBE5');
                }
                $documentationhtml .= html_writer::end_tag('span');
            }
            $documentationhtml .= $br.$br;


        /// function response documentation
            $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6'));
            $documentationhtml .= get_string('response', 'webservice');
            $documentationhtml .= html_writer::end_tag('span');
            $documentationhtml .= $br;
            /// function response description
            $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%'));
            if (!empty($description->returns_desc->desc)) {
                $documentationhtml .= $description->returns_desc->desc;
                $documentationhtml .= $br.$br;
            }
            if (!empty($description->returns_desc)) {
                ///general structure of the response
                $documentationhtml .= $this->colored_box_with_pre_tag(get_string('generalstructure', 'webservice'), 
                                                                      $this->detailed_description_html($description->returns_desc),
                                                                      'FFF1BC');
                ///xml-rpc structure of the response in PHP format
                if (!empty($activatedprotocol['xmlrpc'])) {
                     $documentationhtml .= $this->colored_box_with_pre_tag(get_string('phpresponse', 'webservice'),
                                                                           htmlentities($this->xmlrpc_param_description_html($description->returns_desc)),
                                                                           'DFEEE7');
                }
                ///XML response for the REST protocol
                if (!empty($activatedprotocol['rest'])) {
                    $restresponse  = "".$brakeline."".$brakeline;
                    $restresponse .= $this->description_in_indented_xml_format($description->returns_desc);
                    $restresponse .="".$brakeline;
                    $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restcode', 'webservice'), 
                                                                          htmlentities($restresponse),
                                                                          'FEEBE5');
                }
            }
            $documentationhtml .= html_writer::end_tag('span');
            $documentationhtml .= $br.$br;

       /// function errors documentation for REST protocol
            if (!empty($activatedprotocol['rest'])) {
                $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6'));
                $documentationhtml .= get_string('errorcodes', 'webservice');
                $documentationhtml .= html_writer::end_tag('span');
                $documentationhtml .= $br.$br;
                $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%'));
                $errormessage = get_string('invalidparameter', 'debug');
                $restexceptiontext =<<

    {$errormessage}
    

EOF;
                $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restexception', 'webservice'), 
                                                                      htmlentities($restexceptiontext),
                                                                      'FEEBE5');

            $documentationhtml .= html_writer::end_tag('span');
            }
            $documentationhtml .= $br.$br;
            if (empty($printableformat)) {
                $documentationhtml .= print_collapsible_region_end(true);
            }
        }

     /// close the table and return the documentation
        $documentationhtml .= html_writer::end_tag('td');
        $documentationhtml .= html_writer::end_tag('tr');
        $documentationhtml .= html_writer::end_tag('table');

        return $documentationhtml;

    }

    /**
     * Return the login page html
     * @param string $errormessage - the error message to display
     * @return string the html to diplay
     */
    public function login_page_html($errormessage) {
        global $CFG, $OUTPUT;

        $br = html_writer::empty_tag('br', array());

        $htmlloginpage = html_writer::start_tag('table', array('style' => "margin-left:auto; margin-right:auto;"));
        $htmlloginpage .= html_writer::start_tag('tr', array());
        $htmlloginpage .= html_writer::start_tag('td', array());

//        /// Display detailed error message when can't login
//        $htmlloginpage .= get_string('error','webservice',$errormessage);
//        $htmlloginpage .= html_writer::empty_tag('br', array());
//        $htmlloginpage .= html_writer::empty_tag('br', array());

        //login form - we cannot use moodle form as we don't have sessionkey
        $target = new moodle_url('/webservice/wsdoc.php', array()); // Required

        $contents = get_string('entertoken', 'webservice');
        $contents .= $br.$br;
        $contents .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>'token', 'style'=>'width: 30em;'));
        
        $contents .= $br.$br;
        $contents .= get_string('wsdocumentationlogin', 'webservice');
        $contents .= $br.$br;
        $contents .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>'wsusername', 'style'=>'width: 30em;', 'value'=>get_string('wsusername', 'webservice')));
        $contents .= $br.$br;
        $contents .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>'wspassword', 'style'=>'width: 30em;', 'value'=>get_string('wspassword', 'webservice')));
        $contents .= $br.$br;
        $contents .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('wsdocumentation', 'webservice')));
        
        $htmlloginpage .= html_writer::tag('form', "
$contents
", array('method'=>'post', 'target'=>$target)); $htmlloginpage .= html_writer::end_tag('td'); $htmlloginpage .= html_writer::end_tag('tr'); $htmlloginpage .= html_writer::end_tag('table'); return $htmlloginpage; } }