为Joomla创建回调脚本

时间:2015-02-16 02:19:08

标签: javascript callback joomla3.0

在线评估有“API回调网址”,当学生提交我需要捕获到joomla数据库用户帐户的测验时,它会调用joomla root中的网页/脚本/ php文件来传递信息(异步)。

我需要一个脚本来放入这个php文件来检查用户名是否不存在然后创建用户(作为注册组)并添加信息。如果存在用户名,则只需将信息添加到他的帐户(此处的密钥为用户名)。学生现在可以登录Joomla并查看测试历史记录。

这些是在线评估支持的变量:

username        :the username of the user, if available.
firstname       :the first name of the user, if available.
lastname        :the last name of the user, if available.
cf_label        :the value of this custom field (for example, cf_company if you have a custom field labelled mpany).
title           :the title of the publication.
accesscode      :the access code entered by the user, if available.
id              :the id of the response.
ispreview       :indicates if it's a preview (1) or a real response (0).
pin             :the PIN of the publication (empty if it's a preview).
score           :the score (points) got by the user.
scorepercent    :the score (percent) got by the user.
startdate       :the date/time (UTC, yyyy-mm-ddThh:mm:ss) when the response was started.
enddate         :the date/time (UTC, yyyy-mm-ddThh:mm:ss) when the response was completed.
status          :the status of the response (2=completed, 3=passed, 4=not passed, 5=to be graded).
gr_label        :the value of this custom grade (for example, gr_passed if you have a custom grade labelled passed).
maxscore        :the maximum score available for the publication.
minscore        :the minimum score available for the publication.

它们提供了两个方法(POST)和(GET)的一般示例:

<?php

    // === GET SINGLE PARAMETER (VIA POST) ===
    $accessCode = $_POST["accesscode"];
    $cfCompany = $_POST["cf_company"];

    echo "Access Code: ".$accessCode."<br />";
    echo "Company (Custom Field): ".$cfCompany."<br />";

    // === GET SINGLE PARAMETER (VIA GET) ===
    $pin = $_GET["pin"];
    $grPassed = $_GET["gr_passed"];

    echo "Pin: ".$pin."<br />";
    echo "Grade: ".$grPassed."<br />";

    // === GET ALL PARAMETERS (VIA POST) ===
    foreach ($_POST as $key => $value)
        echo htmlspecialchars($key).": ".htmlspecialchars($value)."<br>";

?>

你能帮忙创建脚本吗? 理解的,

UPDATE1 : 我从diadem.in获得了这个脚本并按照我的理解将其放在html中,但是这是正确的,如何通过帖子检查用户名,就像上面的例子一样!!

<!DOCTYPE html>
<html>
<body>

<script>
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();


//Check for request forgeries: comment this since tokens are not generated in the html 
//JRequest::checkToken() or jexit( 'Invalid Token' );

//Get required system objects
$user         = clone(JFactory::getUser());
$pathway          = & $mainframe->getPathway();
$config       = & JFactory::getConfig();
$authorize        = & JFactory::getACL();
$document       = & JFactory::getDocument();

//Initialize new usertype setting
$newUsertype = $usersConfig->get( 'new_usertype' );
if (!$newUsertype)
{
$newUsertype = 'Registered';
}

//Bind the post array to the user object
if (!$user->bind( JRequest::get('post'), 'usertype' ))
{
JError::raiseError( 500, $user->getError());
}

//Set some initial user values
$user->set('id', 0);
$user->set('usertype', '');
$user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));

$date =& JFactory::getDate();
$user->set('registerDate', $date->toMySQL());

//Save the details of the user
$user->save();


</script> 

</body>
</html>

Update2:我试过这个,但没有运气..

<?php 

function register_user ($username, $email, $firstname, $lastname, $password){ 

//This is the original code which I comment to get via POST
 //$firstname = $email; // generate $firstname
 //$lastname = ''; // generate $lastname
 //$username = $email; // username is the same as email

// === GET SINGLE PARAMETER (VIA POST) ===
    $username = $_POST["username"];
    $email = $_POST["email"];
    $firstname = $_POST["firstname"];
    $lastname = $_POST["lastname"];
    $password = $_POST["username"];

 /*
 I handle this code as if it is a snippet of a method or function!!
 First set up some variables/objects     */

 // get the ACL
 $acl =& JFactory::getACL();

 /* get the com_user params */
 jimport('joomla.application.component.helper'); // include libraries/application/component/helper.php
 $usersParams = &JComponentHelper::getParams( 'com_users' ); // load the Params

 // "generate" a new JUser Object
 $user = JFactory::getUser(0); // it's important to set the "0" otherwise your admin user information will be loaded

 $data = array(); // array for all user settings

 // get the default usertype
 $usertype = $usersParams->get( 'new_usertype' );
 if (!$usertype) {
     $usertype = 'Registered';
 }

 // set up the "main" user information
 //original logic of name creation
 //$data['name'] = $firstname.' '.$lastname; // add first- and lastname
 $data['name'] = $firstname.$lastname; // add first- and lastname

 $data['username'] = $username; // add username
 $data['email'] = $email; // add email
 $data['gid'] = $acl->get_group_id( '', $usertype, 'ARO' );  // generate the gid from the usertype

 /* no need to add the usertype, it will be generated automaticaly from the gid */

 $data['password'] = $password; // set the password
 $data['password2'] = $password; // confirm the password
 $data['sendEmail'] = 1; // should the user receive system mails?

 /* Now we can decide, if the user will need an activation */
 $useractivation = $usersParams->get( 'useractivation' ); // in this example, we load the config-setting
 if ($useractivation == 1) { // yeah we want an activation

     jimport('joomla.user.helper'); // include libraries/user/helper.php
     $data['block'] = 1; // block the User
     $data['activation'] =JUtility::getHash( JUserHelper::genRandomPassword() ); // set activation hash (don't forget to send an activation email)

 }
 else { // no we need no activation

     $data['block'] = 1; // don't block the user

 }

 if (!$user->bind($data)) { // now bind the data to the JUser Object, if it not works....

     JError::raiseWarning('', JText::_( $user->getError())); // ...raise an Warning
     return false; // if you're in a method/function return false

 }

 if (!$user->save()) { // if the user is NOT saved...

     JError::raiseWarning('', JText::_( $user->getError())); // ...raise an Warning
     return false; // if you're in a method/function return false

 }

 return $user; // else return the new JUser object

 }

 $email = JRequest::getVar('email');
 $password = JRequest::getVar('password');

 //echo 'User registration...'.'<br/>';
 register_user($email, $password);
 //echo '<br/>'.'User registration is completed'.'<br/>';
?>

0 个答案:

没有答案