I want to get all the HTML content of the page resources in a Moodle course through a Moodle Webservice. The documentation is missing examples, and discussion threads I have read through don't seem to address this seemingly basic action.
What is the correct webservice function to get page resource HTML content?
The mod_page_get_pages_by_courses function returns information plus HTML content of a given course (if provided) or all courses the user has access to (if courseids is left blank).
Below is a generalized call to the webservice to retrieve all page information of a specific course, assuming you have a Moodle Webservice correctly set up:
https://<your-moodle-url>/webservice/rest/server.php?wstoken=<your-token>&moodlewsrestformat=json&wsfunction=mod_page_get_pages_by_courses&courseids[0]=<your-course-id>
Here's the abbreviated info from the documentation that helped me figure it out:
mod_page_get_pages_by_courses
Returns a list of pages in a provided list of courses, if no list is provided all pages that the user can view will be returned.
Arguments
courseids (Default to "Array ( ) ") - Array of course ids
Response
Note the content entry, which is a HTML string with the page content.
object {
pages list of (
object {
id int //Module id
coursemodule int //Course module id
course int //Course id
name string //Page name
intro string //Summary
introformat int Default to "1" //intro format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN)
introfiles //Files in the introduction text
list of (
//File.
object {
filename string Optional //File name.
filepath string Optional //File path.
filesize int Optional //File size.
fileurl string Optional //Downloadable file url.
timemodified int Optional //Time modified.
mimetype string Optional //File mime type.
isexternalfile int Optional //Whether is an external file.
repositorytype string Optional //The repository type for external files.
}
)content string //Page content
contentformat int Default to "1" //content format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN)
contentfiles //Files in the content
list of (
//File.
object {
filename string Optional //File name.
filepath string Optional //File path.
filesize int Optional //File size.
fileurl string Optional //Downloadable file url.
timemodified int Optional //Time modified.
mimetype string Optional //File mime type.
isexternalfile int Optional //Whether is an external file.
repositorytype string Optional //The repository type for external files.
}
)legacyfiles int //Legacy files flag
legacyfileslast int //Legacy files last control flag
display int //How to display the page
displayoptions string //Display options (width, height)
revision int //Incremented when after each file changes, to avoid cache
timemodified int //Last time the page was modified
section int //Course section id
visible int //Module visibility
groupmode int //Group mode
groupingid int //Grouping id
}
)warnings Optional //list of warnings
list of (
//warning
object {
item string Optional //item
itemid int Optional //item id
warningcode string //the warning code can be used by the client app to implement specific behaviour
message string //untranslated english message to explain the warning
}
)}
Alternative: core_course_get_contents
Found this helpful forum post by Juan Levya that presents an alternative way of getting the page contents as downloadable HTML files. Adapted info from the original post:
The function core_course_get_contents returns:
Sections
Modules in each section (activities or resources)
If the module is a resource it returns a list of the files used by this resource in the "contents" attribute
That attribute is an array of files, there you will have a fileurl attribute that is the download URL for the file. If you are using Web Services you should append the WS token.
Example of URL returned:
https://<your-moodle-url>/webservice/pluginfile.php/29/mod_page/content/index.html?forcedownload=1
You need to modify the URL to download the file by adding your WS token as a query parameter with the key token:
https://<your-moodle-url>/webservice/pluginfile.php/29/mod_page/content/index.html?forcedownload=1&token=<your-token>
This will initialize a download of the resource file.
Get Course Contents Using Webservice is a somewhat related question, however it specifically asks about lesson resources, which have their own dedicated function.
Related
I have two structures:
type GoogleAccount struct {
Id uint64
Token string
}
It represent my custom PostgreSQL object type (i created myself):
CREATE TYPE GOOGLE_ACCOUNT AS
(
id NUMERIC,
token TEXT
);
And next structure is table in DB:
type Client struct {
IdClient uint64 `gorm:"primary_key"`
Name string
PhotoUrl string
ApprovalNumber uint16
Phone string
Password string
HoursOfNotice int8
Google GoogleAccount
}
And my custom object nested in type Client and named as google. I've tried to read data by the next way:
var users model.Client
db.First(&users)
But unfortunately I can't read field google (have a default value). I don't want to create separate table with google_account, or make this structure as separated fields in client table or packed it as json (created separate entity, because this structure used not only in this table and I'm searching new ways, that get the same result, but more gracefully). The task is not to simplify the presentation of data in the table. I need to make the correct mapping of the object from postgres to the entity.
Right now I found one solution - implement Scanner to GoogleAccount. But value in the input method is []uint8. As I can suppose, []uint8 can cast to string, and after that I can parse this string. This string (that keep in db) look like (x,x) - where x - is value. Is the right way, to parse string and set value to object? Or is way to get this result by ORM?
Is the possible way, to read this data as nested structure object?
It looks like you'll want to do two things with what you have: (1) update the model so you have the right relationship binding, and (2) use the .Preload() method if you're trying to get it to associate the data on read.
Model Changes
Gorm automatically infers relationships based on the name of the attributes in your struct and the name of the referenced struct. The problem is that Google attribute of type GoogleAccount isn't associating because gorm is looking for a type Google struct.
You're also missing a foreign key on GoogleAccount. How would the ORM know which GoogleAccount to associate with which Client? You should add a ClientId to your GoogleAccount struct definition.
Also, I would change the primary keys you're using to type uint since that's what gorm defaults to (unless you have a good reason not to use it)
If I were you, I would change my struct definitions to the following:
type Client struct {
IdClient uint `gorm:"primary_key"`
Name string
PhotoUrl string
ApprovalNumber uint16
Phone string
Password string
HoursOfNotice int8
GoogleAccount GoogleAccount // Change this to `GoogleAccount`, the same name of your struct
}
type GoogleAccount struct {
Id uint
ClientId uint // Foreign key
Token string
}
For more information on this, take a look at the associations documentation here: http://gorm.io/associations.html#has-one
Preloading associations
Now that you actually have them properly related, you can .Preload() get the nested object you want:
db.Preload("GoogleAccount").First(&user)
Using .Preload() will populate the user.GoogleAccount attribute with the correctly associated GoogleAccount based on the ClientId.
For more information on this, take a look at the preloading documentation: http://gorm.io/crud.html#preloading-eager-loading
Right now I found one solution - implement Scanner to GoogleAccount. At input of the Scan method I got []uint8, that I cast to string and parse in the end. This string (that keeping in db) look like (x,x) - where x - is value. Of course, it is not correct way to achieve my goal. But I couldn't found other solution.
I highly recommended use classical binding by relationship or simple keeping these fields in table as simplest value (not as object).
But if you would like to experiment with nested object in table, you can look at my realization, maybe it would be useful for you:
type Client struct {
// many others fields
Google GoogleAccount `json:"google"`
}
type GoogleAccount struct {
Id uint64 `json:"id"`
Token string `json:"token"`
}
func (google GoogleAccount) Value() (driver.Value, error) {
return "(" + strconv.FormatUint(google.Id, 10) + "," + google.Token + ")", nil
}
func (google *GoogleAccount) Scan(value interface{}) error {
values := utils.GetValuesFromObject(value)
google.Id, _ = strconv.ParseUint(values[0], 10, 64)
google.Token = values[1]
return nil
}
I'm trying to use the TYPO3 tags function to get strings from an xlf file. If I hard code the reference to the string I want, it returns successfully, but I need to know how to get it to return the appropriate string based on the tag content.
For example, this works:
HTML:
<var>myVar</var>
Typoscript:
lib.parseFunc_myExt{
tags {
var = TEXT
var{
value = {LLL:path/to/locallang.xlf:var.myVar}
insertData = 1
}
}
}
I need a method to take the content of the <var> tag and use it in place of myVar in the value.
I apologize for what seems to be an exceedingly simple question, but after 4 hours of searching and beating my head against the wall, I'm doubting my sanity.
I need a string format expression that trims a supplied string argument much like Substring(0,1). Although I've never seen this in code, it just seems like it should be possible.
Here's a very basic example of what I'm trying to do:
string ClassCode = "B608H2014"; // sample value from the user's class schedule
string fRoom = "{0:0,2}";
string fGrade = "{0:2,2}";
string fYear = "{0:5,4}";
string Classroom = String.Format(fRoom, ClassCode); // intended result - "B6"
string Gradelevel = String.Format(fGrade, ClassCode); // intended result - "08"
string Schoolyear = String.Format(fYear, ClassCode); // intended result - "2014"
This is a very basic example, but I'm trying to use the String.Format() so I can store the format pattern(s) in the database for each respective DTO property since the class code layouts are not consistent. I can just pass in the formats from the database along with the required properties and it extracts what I need.
Does this make sense?
I have created a multifield custom widget having two fields with names ./urlLink and ./urlText.
Now i m trying to fetch the values from widget into the component's jsp with following code
String property = properties.get("./urlLink",String[].class);
for(String value: property ) {
out.print(value);
}
out.print(property);
But i am not able to get its value instead i m getting error.
If you're getting a property and it contains a string value, you need to use the method getString() - that way when you have the property, you can set the string to the value by doing something like this:
Property property = properties.get("./urlLink",String.class);
String value = property.getString();
Just a side note, if your return is supposed to be a string array, your type that you're putting the values in should be a string array.
String[] value
Check out the documentation on day.com for Properties and getting the values inside them.
Looks like a typo: you don't prefix a property name with .\ when accessing it.
My guess is you got a NullPointerException, right? That's because there's no ./urlLink property in the value map (properties). You should check against that anyway (so that it's not thrown on a fresh page with no content).
If that doesn't help -- double check that you have the properties in the content (call your page with .xml or .infinite.json extensions, and then double check if you can read them as plain strings (you should be able to -- CRX does some magic, smart type conversions).
It's good to register custom xtype as :
// registering the custom widget with the name dualfield
CQ.Ext.reg("dualfield", CQ.Ext.form.DualField);
Then u can easily fetch the value as :
String[] data = properties.get("multi",String[].class);
Here multi is the name of widget having multifield as xtype
I'm working on a new application and i need to make a messages interface with lookup, using the key to find the value (like ConstantsWithLookup, but capable to receive parameters). I have being investigating Dictionary class functionality but it lacks message customization through parameters.
With ConstantsWithLookup i can make the following:
myConstantsWithLookupInterface.getString("key");
and get something like:
Field must be filled with numbers
But i need to do this:
myMessagesWithLookupInterface.getString("key", "param1", "param2",...);
and get something like:
Field _param1_ must be filled with numbers greater than _param2_
I have no clue how to do this.
Use GWT regular expressions:
//fields in your class
RegEx pattern1 = RegEx.compile("_param1_");
RegEx pattern2 = RegEx.compile("_param2_");
public String getString(String key, String replace1, String replace2){
// your original getString() method
String content = getString(key);
content = pattern1.replace(content,replace1);
content = pattern2.replace(content,replace2);
return content;
}
If your data contains Field _param1_ must be filled with numbers greater than _param2_ then this will replace _param1_ with content of string replace1.