How do we get the context of the current page opened in moodle
i.e., the context could be either system/course/coursecat etc.,
I would appreciate your help
Really simple :)
$context = $PAGE->context;
And to check the type of context, use the context constants
switch ($context->contextlevel) {
case CONTEXT_SYSTEM:
break;
case CONTEXT_USER:
break;
case CONTEXT_COURSECAT:
break;
case CONTEXT_COURSE:
break;
case CONTEXT_MODULE:
break;
case CONTEXT_BLOCK:
break;
}
Make sure you have the following set before you call $PAGE:
GLOBAL = $PAGE;
Related
I have a simple code for preserving and later using register address:
PWMChannel::PWMChannel(TIM_HandleTypeDef *timer, int channel)
{
switch(channel)
{
case 1: ccr = &(timer->Instance->CCR1); break;
case 2: ccr = &(timer->Instance->CCR2); break;
case 3: ccr = &(timer->Instance->CCR3); break;
case 4: ccr = &(timer->Instance->CCR4); break;
case 5: ccr = &(timer->Instance->CCR5); break;
case 6: ccr = &(timer->Instance->CCR6); break;
}
}
Where ccr is a private class member: uint32_t *ccr = nullptr;
It is used to change duty cycle like this: *ccr = duty;
The code above worked just fine some time ago when I was using System Workbench. Recently I switched to CubeIDE. The only issue with porting project to new toolchain was with this member definition - it now required "volatile", so I changed it to: volatile uint32_t *ccr = nullptr;
However the code stopped working. The debugging shows that with channel parameter = 4 the ccr value becomes 0x40. Now, 0x40 is an offset of CCR4 within TIM_TypeDef structure (referenced by Instance), not an actual address of CCR4. If this is how it supposed to be then why it worked before and how do I change the code to make it work again?
OK, I think I figured out what happened.
The object in question is statically initialized. Somehow this initialization happened after MX_TIM1_Init() call (in main.c) when I was building under System Workbench and before that call when I was building under CubeIDE. The "Instance" field is not yet initialized by the time constructor called, so it got wrong register address.
This is not the first time order of initialization bites me... :(
I already have most of the global objects either dynamically allocated or hidden behind functions in singleton pattern. Did not want to do it for this tiny helper object.
If somebody thinks this question is useless, tell me and I'll delete it.
I'm having a problem calling images that are sitting inside the res folder, and I assume I'm doing something wrong.
I've read several posts in SOF stating that all i need to do is to add all the images in a flat way to the res folder:
But I can't reach them in the code below (the code in under MapsActivity)
private BitmapDescriptor
getMarkerIcon(Job.jobTypes jobType){
switch ((jobType)) {
case LIFESTYLE:
return BitmapDescriptorFactory.fromResource(android.R.drawable.lifestyle); <-- states it cannot find the resource
case DELIVERY:
break;
case PRIVATELESSONS:
break;
case HANDYMAN:
break;
}
}
What I'm trying to achieve is a way of returning to another function the current image for the map marker based on the enum type.
Resource files cannot have space in their name. Remove spaces from the file names.
Load image from your resource:
return BitmapDescriptorFactory.fromResource(R.drawable.lifestyle);
My site has doAppInitAfterRestart set to true and is working, but I'd like to know when the application has completed it's initialization.
Is there a way through powershell and WMI to know when the site's application initialization is completed?
Try below mentioned VB Script code. I have scraped this code from one of my working script , if you see any error, shoot a comment.
iis_con= "localhost"
set WMI_IIS = GetObject( "IIS://" & iis_con & "/W3SVC" )
if WMI_IIS.count = 0 then
' do nothing
else
for each objitem in WMI_IIS
if objitem.class = "IIsWebServer" then
Select Case objItem.serverstate
Case 1: wscript.echo("Starting")
Case 2: wscript.echo("Running")
Case 3: wscript.echo("Stopping")
Case 4: wscript.echo("Stopped")
Case 5: wscript.echo("Pausing")
Case 6: wscript.echo("Paused")
Case 7: wscript.echo("Continuing")
Case Default: wscript.echo("Unknown")
End Select
Next
end if
I don't know why the class does not be udpated in following script using the (case):
if (favorite !== null) {
switch (favorite) {
case 'cat':
document.getElementById("one").className = "favBlue";
//document.getElementById('one').className ='favRed';
//document.createAttribute('class','favRed')
break;
case 'dog':
document.getElementsByName('dog').className = 'favBlue';
break;
case 'gerbil':
document.getElementsByName('gerbil').className = 'favYellow';
break;
case 'gopher':
document.getElementsByName('gopher').className = 'favWhite';
break;
}
}
Please click on this link in order to see the complete script http://jsfiddle.net/gu8u6eoc/6/
Your case statement is working. But I think you should use document.getElementById(id) instead of document.getElementsByName(name) to change the class values. Also, you are applying the classnames to the checkboxes which won't change their colors. You should apply the classnames to the texts instead.
According to MDN, getElementsByName have different behavior per browser (e.g. sometimes it will work on elements with a similar id attribute to name). Also, getElementsByName() returns a NodeList instead of an Element object.
BTW, here is a simplified working JSFiddle. This contains both the usage of getElementById and getElementsByName.
In my code I need to be able to jump (goto) a different case within the same switch statement. Is there a way to do this?
My code is something like this: (There is a lot of code I just left it all out)
switch (viewNumber) {
case 500:
// [...]
break;
case 501:
// [...]
break;
.
.
.
.
.
case 510:
// [...]
break;
default:
break;
}
Thank you for your time!
-Jeff
It's generally very bad practice to unconditionally jump like you're asking.
I think a more readable/maintainable solution would be to place the shared code in a method and have multiple cases call the method.
If you really want to, you can use goto to do something like:
switch(viewNumber) {
case 500:
// [...]
goto jumpLabel;
case 501:
// [...]
break;
case 502:
// [...]
jumpLabel:
// Code that 500 also will execute
break;
default:break;
}
Note: I only provided the code example above to answer your question. I now feel so dirty I might have to buy some Bad Code Offsets.
Instead of using goto, refactor your code so that the two (or more) cases that use common code instead call it in a common method.
Something like:
switch (value) {
case (firstValue):
// ...
break;
case (secondValue):
[self doSharedCodeForSecondAndThirdValues];
break;
case (thirdValue):
[self doSharedCodeForSecondAndThirdValues];
break;
default:
break;
}
// ...
- (void) doSharedCodeForSecondAndThirdValues {
// do stuff here that is common to second and third value cases
}
It wouldn't be the end of the world to use goto, though it is bad practice.
The practical reason for avoiding use of goto is that you have to search through your swtich-case tree to find that goto label.
If your switch logic changes, you'll have a messy situation on your hands.
If you pull out common code to its own method, the code is easier to read, debug and extend.
You should probably try rewrite your code, like a recursive call or just factor out common stuff and call a separate function. But as a fix and quick answer to your question you could put a label before your switch and goto it, like so
switchLabel:
switch(viewNumber) {
case 500: {
viewNumber = 501;
goto switchLabel;
}
}
Not sure of the Objective-C syntax here, but you could also try a variation thereof
int lastView = 0;
while (lastView != viewNumber)
switch(lastView = viewNumber) {
case 500: {
viewNumber = 501;
break;
}
}
which will keep on looping until the viewNumber doesn't change any more. This is still pretty much just a pretty-looking goto though.
And since we're doing gotos you could just goto into another case, as pointed out already. You could also do fancy stuff similar to Duff's device, by putting cases inside of other blocks. But that's just mad.. :)
[I'm making this answer community wiki because this doesn't actually answer the question per se]
As others have said, this is very bad style, and makes for unreadable code...
Alternatives:
Factor the common code into a separate function, and call that in 2 places.
Use fallthroughs, leave off the the break on a case and it falls through to the next one (remember, cases don't have to be in numerical order!)
if you only want part of a case to be done in the other case, protect it with an if:
as in
case 500:
.
.
.
case 501:
if(viewNumber == 501) {
.
.
.
}
.
.
.
break;