I used these assets to help solve this requirement.
Moodle doc on update api
PHP strtotime documentation
redirect on cancel button using javascript
Here is my code that accomplished. I created a page called updateEnrollDate.php and stored in the root of my plugin. The index.php page is also located at the root of the plugin.
HTML
form method='post' action='updateEnrollDate.php'>
input type='text' name='newdate' value=".$_REQUEST['edate']." size='8'>
input type='hidden' name='checkit' value=".$_REQUEST['ueid'].">
input type='submit' value='Update'>
input type='button' value='Cancel' onclick='history.back()'>
/form>Checking if form has been submitted.
if(isset($_REQUEST['checkit'])){Calling function to update the DB, using moodle DB API.
//update the record
updateDB($_REQUEST['checkit'],$_REQUEST['newdate']);
//return to enroll report
header("Location: index.php");
}
function updateDB($ueid,$edate){
global $DB;
//echo "
in updateDB....";
//var_dump($ueid);
//var_dump($edate);
$dataObj = new stdClass();
$dataObj->id = $ueid;
$unixconverted = strtotime($edate);
$unixconvertedadjusted = strtotime('+1 day', $unixconverted);//why does my converted time come a day short? adding day
$dataObj->timecreated = $unixconvertedadjusted;
$table = 'user_enrolments';
$DB->update_record($table, $dataObj);
}
Things that tripped me up while working through this?
- Using the $_REQUEST variable, I tried using $_FORM initially, did not work.
- Forgot to put the word global in front of $DB. errr
- Converting the $edate variable from a string entered in from to a unixtime format.
- putting mdl_ prefix in front of the table name for my $table variable. The moodle API functions do not like that.
Note: If I had been looking in my error log or if I had been echoing php errors to the console, I would have spent less time trying to figure out the steps listed above. I get lazy and think the task is trivial, and do not thing to look in the logs. Dummy! Use the logs!
can u post the whole code or send it via email
ReplyDeletehi
ReplyDeleteI want use function getAnswerText($aid,$qid,$sid)
{
$sql = "select
a.answer
FROM mdl_question_states s, mdl_question_answers a
where a.question = s.question and a.id=$aid and a.question=$qid and s.id=$sid
";
// $ars = dbCall($sql,false,'training_moodle');
$ars = dbCall($sql,false,'moodle');
return $ars->fields['answer'];
}
external php page how can achive this using moodle 1.9 v