Conditional activities: New Hack: Activity Locking for 1.4.5

Conditional activities: New Hack: Activity Locking for 1.4.5

by Stuart Mayor -
Number of replies: 32
I have been developing a new hack for Moodle 1.4.5. It is inspired by Bernard Boucher's "Activity Linking", although little of his original code remains tongueout

This hack is very much in the development stage. DO NOT USE THIS ON A PRODUCTION SYSTEM. You have been warned.

Installation
  • Attached should be the file "moodle_activity_locking.zip". This contains the following files:
    • course/lib.php
    • course/lock.php
    • course/view.php
    • pix/t/lock.gif
    • pix/t/open.gif
    • pix/t/closed.gif
    • lib/locklib.php
  • Extract them to the corresponding folders within your Moodle installation.
  • The following piece of code needs to be pasted into view.php files of each module you have installed. Look in the "mod" folder of your Moodle installation, then look in each module folder in turn.
///Activity Locking ///
require_once($CFG->libdir.'/locklib.php');
if (($locks = islocked($cm, $course)) and array_search("closed", $locks) and !isteacher()) {
print_lock_notice($locks, $course);
}
/// End Activity Locking ///
  • This code needs to be placed after the "require_login" function and before any "add_to_log" function.
  • A new table needs to be added to your Moodle database as per the sql file included in the zip.
  • That's it smile
Useage
  • Log in as a user that has rights to modify a course
  • In the course view window (the one where you can move, delete etc the activities within a course) and go into Edit mode.
  • You will now see a new icon after the activity names: a padlock
  • Clicking on the padlock will take you to the activity locking screen for that activity. If any grades are selected on this screen, then that activity must be completed to that level before this activity may be accessed.
  • To unlock an activity, set all predecessors' grades to 0.
  • Locked activities will appear "dimmed" with "(locked)" after the title. A number of icons will appear showing the number of predecessors for that activity and how many are completed.
Known Issues
  • Sloppy programming blush
  • Poor code commenting (I just get carried away with coding) tongueoutblushblush
  • No language strings used. Currently all labels, notices, alerts etc are in English.
  • Need to test what happens when an activity is deleted. It should delete all locks associated with that activity, but it hasn't been tested.
  • Not sure how it behaves alongside Bernard's activity linking (or indeed any other hacks).
  • 2 or more activities can be locked to each other, meaning it is possible to create an impossible loop that a student can't break into. I'll leave it to you not to do that! Do I really need to code a safeguard against this?
  • Poor on-screen instructions.
  • Plenty more I can't think of at the moment! big grin
How You Can Help
  • Test Test Test. Install it. Play with it. Let me know how it behaves. Remember, this is for Moodle 1.4.5 ONLY.
  • How can improve how predecessors are shown on the course view? Currently, those little tick/cross icons are displayed. Is there a better way?
  • Are there any performance issues with this hack? Obviously, the more locks you have, the more database queries there will be. I'm not sure just how many more though.
  • Any other comments.
Remember
THIS IS FOR MOODLE 1.4.5 ONLY. DO NOT USE ON A PRODUCTION SYSTEM. THIS IS STILL IN THE TESTING PHASE
Average of ratings: -
In reply to Stuart Mayor

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Bernard Boucher -
Hi Stuart,
             that look very good and with it I just started preparing to be retiredwink.
I will install 1.4.5 and test it probably tomorrow.

Bye,
Bernard

In reply to Stuart Mayor

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Stuart Mayor -
BTW. I'll be updating this to run on 1.5 once it goes into full beta. smile
In reply to Stuart Mayor

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Chardelle Busch -
Picture of Core developers
I look forward to testing it on 1.5 Stuart.
In reply to Chardelle Busch

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Darren Smith -

Me too tongueout

It's not something I was that bothered about but now it's out there I can think of loads of situations where I would find it useful - especially as you appear to be able to use grades rather than just completed flags cool

In reply to Stuart Mayor

New Hack: Activity Locking for 1.5

by Stuart Mayor -

I couldn't wait for 1.5beta to arrive, so I've written the code for the current nightly build big grin

As above, all the standard warnings apply. Especially DO NOT USE THIS ON A PRODUCTION SYSTEM, but if you are daft enough to have 1.5 running in production you deserve all you get tongueout

I've made a few modifications since the last release. Namely,

  • Better commenting. Although still shoddy blush
  • Inclusion of language strings... /lang/en/lock.php

Installation is basically as above. There's an extra file for the language strings in this one. You'll also need to add the language string $string['lock'] = 'Lock'; into the moodle.php language file.

You'll also need to add the following snippet of code at the top of each view.php file for each module you use. It needs to go above any add_to_log function and after require_login function. It needs to go in instead of the code above.

///Activity Locking ///
require_once($CFG->libdir.'/locklib.php');
check_locks($cm, $course);
/// End Activity Locking ///

I've added an example view.php for the quiz module to the zip.

I look forward to your comments...

In reply to Stuart Mayor

Re: New Hack: Activity Locking for 1.5

by Bernard Boucher -
Hi Stuart,
great work! Works first time with 1.5 lastest.


I will check how to integrate regulate rights with it and maybe how to display all regulations and locks with xtree block.

Thanks,

Bernard

In reply to Bernard Boucher

Re: New Hack: Activity Locking for 1.5

by Chardelle Busch -
Picture of Core developers
Hi Stuart,

I just tried this in 1.5 and got numerous errors.  I'm going to assume I did something wrong creating the table in the database, for one thing, I'm not sure what it means: autoincrement=60.  Here's my table, if you could look at it and see if something is wrong.  Thanks.  I did get the lock to show up, though, and one comment is that you could add a mouseover for itsmile.
Attachment table.gif
In reply to Chardelle Busch

Re: New Hack: Activity Locking for 1.5

by Stuart Mayor -
Ah. Right.

Ok. Firstly, here's a pic of my table on my database. It looks very similar. The only thing I can see different is the index table.
I think the "autoincrement=60" comes from the fact that my table was up to an id of 60 when I did the export.  You can ignore the =60 bit.
What other errors did you get? Also, are you using the very latest nightly build?

There should be mouseovers for all the locks that follow the name of a module. They work on my machine with Firefox. I'll check it with IE too.

Thanks for the feedback.
Attachment tablepic.gif
In reply to Stuart Mayor

Re: New Hack: Activity Locking for 1.5

by Stuart Mayor -
I've just noticed there's a load of trailling whitespace in the locklib.php file.
Go right to the bottom of the page. On the last line, delete everything after ?>

I'll make sure this is fixed in the next version I upload. Right now I'm working on making the code more efficient.
In reply to Stuart Mayor

Re: New Hack: Activity Locking for 1.5

by Brad Zehr -

Have you stopped updating the version for 1.4.5, and are now only completing on 1.5?  I was going to download and test the files for 1.4.5 but wasn't sure if they contained the latest changes to your hack.

In reply to Brad Zehr

Re: New Hack: Activity Locking for 1.5

by Stuart Mayor -
Yes, at the moment I've only updated the ersion for 1.5. That said, most of the changes have just been cosmetic.
In reply to Stuart Mayor

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Stuart Mayor -
I wonder if I could have your opinions?
What do you think would be better...

  1. The way activity locking is now - Hacked into the standard Moodle code, giving teachers the ability to add locks to existing course formats.
  2. Adding new course formats, e.g. Weeks with locking, Topics with locking etc. This should be less intrusive to the Moodle code (I should be able to do it by just adding pages, rather than editing existing ones). There will be the need for some editing of existing files, for example each view.php of modules.
Thanks.
In reply to Stuart Mayor

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Chardelle Busch -
Picture of Core developers
Hi Stuart,
Well, something is still very wrong.  I fixed the database and upgraded to today's 1.5 but that didn't fix anything.  I get this error right off the bat:
Notice: Undefined property: lock in /home/cbusch/public_html/moodle/course/lib.php on line 1794

Notice: Undefined property: lock in /home/cbusch/public_html/moodle/course/lib.php on line 1797

To test it, I only updated the quiz.view, I didn't change any other mod views, would that  be the problem?   If I click on the quiz lock I get  these weird errors:
Notice: Undefined variable: action in /home/cbusch/public_html/moodle/course/lock.php on line 39

Notice: Undefined index: 9 in /home/cbusch/public_html/moodle/course/lock.php on line 77
Table 'cbusch_mdle2.mdl_' doesn't exist

SELECT * FROM mdl_ WHERE id = ''

I may have to start over.
 
In reply to Stuart Mayor

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Bernard Boucher -
Hi Stuart,
              I think you are on the right way to have your code Martinizedwink

For your 2 points:

1 - Using existing course formats make great sense  but will need to be tested on big sites ( I have only 120 users )  for performances issues.

2 - The way you did it make adding new courses formats less usefull.  A variable in course parameters should be enough to enable/disable the locking mecanisim by the editing teacher.

I think keeping it simple as you did will make it easier to maintain and more prone to be part of standard code after some security and compatibilty review.

Thanks,

Bernard
 
In reply to Bernard Boucher

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Chardelle Busch -
Picture of Core developers
Hi Guys,

I just re-installed 1.5 and the code and its working great (still no mouseover for the lock, though).  I have a question, Stuart, you mentioned using the certificate module, I was hoping to test this with the certificate, but realized it hasn't been re-worked for 1.5 code yet.  Have you updated the certificate by any chance?  If not, I will check into getting that done.
In reply to Chardelle Busch

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Chardelle Busch -
Picture of Core developers
I just noticed that the lock shows up for the news forum in the main menu, so maybe it would be more appropriate to do it on a per course basis (leaving out the homepage).
In reply to Chardelle Busch

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Stuart Mayor -
The idea of the code is that you can lock any activity anywhere on your Moodle site with any other activity on the same course.
I agree that most people wouldn't want to lock the forum (or indeed any other activity) but it might be that there's a small number that want to limit access to the forum until a user has, say, completed an agreement or read a terms and conditions statement.

Where abouts is it you say there is no mouseover? Is it on the little padlock icon or on the tick and crosses that show how many predecessors an activity has? What browser are you using?

I'm also thinking of adding an option that allows a teacher to choose whether students see what predecessors an activity had (I noticed that you, Chardelle, asked how to switch off the "(predecessor)" that appears after an activity on another post).
In reply to Chardelle Busch

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Stuart Mayor -
Chardelle,

This certificate works on mine...
I just hope I've got all the files that are needed in the zip smile
In reply to Stuart Mayor

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Brian b -
Thank you Stuart for this Module! I've been looking for this for a long time. I have an older version of Moodle that I can't upgrade right now. With a few tweaks from André Duarte at the post below, I was able to get it to work with my system. Thanks!

http://moodle.org/mod/forum/discuss.php?d=24877
In reply to Bernard Boucher

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Mike Shields -

Hi everybody on this thread...

Been trying to get everything to work started with the 1.5 zips on a 1.5beta site - did not work sad So now on a 1.5.2 site http://www.healthsafetyplus.com/eLearning

evertyhing uploaded ok smile

Just a few questions (forgive me if answered elsewhere)

1. although locklib is installed and I have added the code

///Activity Locking ///
require_once($CFG->libdir.'/locklib.php');
check_locks($cm, $course);
/// End Activity Locking ///

to the lesson mod - I can not get the option to hide cert until end of lesson.

1.b in addition should I want students to gain say 80% from the questions within the lesson before been awarded the cert, how can I do this??

2. Is there a centeral download place for the activity_locking_for_1.5.zip
 &  the moodle_certificate_for_1.5.zip OR are these just replaced with newer versions as and when code is altered/added to??

3. Is there a step by step readme of how to install everthing - I'm sure I've missed something - and trying to glean info from the forums get confusing as some is for 1.4 and some are directed at the above mentioned zip files

Think that's all for now...

Thanks in advance,

Mike wink

In reply to Mike Shields

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by David T. Cannon -

Hi Mike,

I can not get the option to hide cert until end of lesson

although the library needs to be included in every module to work, I just downloaded AL and found it was included in all /mod directories except hotspot(whatever that is). Be sure that this locklib.php is in the certificate view.php file to prevent users from seeing it until they have finished pre-requisites.

1.b in addition should  I want students to gain say 80%...

I am not sure about this. If you are using my certificate for 1.5, that is set up as part of activity locking, on a particular grade. I am not sure we have a method for using grade book yet.

 Is there a centeral download place

AL is in CVS under the /contrib directory. Althought it seems the download page -> Activity Modules is not being maintained to date, so you wont find it there. Also, I have been collaborating with others on a new certificate module, you can find the link for it at: http://moodle.org/mod/forum/discuss.php?d=28089

I think this will give you latest cvs version http://download.moodle.org/download.php/modules/activity_locking.zip

Is there a step by step readme of how to install everthing

Wouldn't that be nice. hehe. You should be able to unzip the packages in the moodle top directory, and all the files fit in place. Then you just go to your admin menu in moodle. This should update everything. I did notice last AL module had top level directory, which made unziping in the moodle directory not possible.
Dave.

In reply to David T. Cannon

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Chardelle Busch -
Picture of Core developers
Hi Mike,

As for instructions, this is from a post of mine from the Moodle in Business course that might help:
>>Activity locking is neither--it is code that is added to several pages in the course folder--you simply replace those files, and a new file is added to the lib folder, and some code is added to the view.php file of each of the modules--so you replace those also. It also includes a pix folder for the icons it uses. A block is added but it is not used in the course as a block, when you upload it, it creates the database tables.<<

As for the lesson, I have tested it and it works fine.  I have a lesson that can earn a total of 40 points.  I have a certificate locked based on earning a grade on the lesson of 40 points (although you could have it based on less points, e.g. 80% of 40), when a user completes the lesson with the required # of points the certificate shows up.

The only thing not working right now, is the locking completed icon (check) does not show up when a certificate is viewed--I think David is working on that.
In reply to David T. Cannon

Re: Conditional activities: New Hack: Activity Locking for 1.4.5

by Mike Shields -

I did not have this version of AL from your above link http://download.moodle.org/download.php/modules/activity_locking.zip

Tried to install it but there is an error 'Block activity_locking tables could NOT be set up successfully!'

Looking at the MySQL - It is apparent that this is for an update as it calls for altering a table. I never had this table in the first place, so I can not even update manually sad

# Table structure for table `course_module_locks`

CREATE TABLE `prefix_course_module_locks` (
 `id` int(10) unsigned NOT NULL auto_increment,
   `courseid` int(10) unsigned NOT NULL default '0',
   `moduleid` int(10) unsigned NOT NULL default '0',
   `lockid` int(10) unsigned NOT NULL default '0',
   `requirement` varchar(10) NOT NULL default '',
   PRIMARY KEY  (id),
   UNIQUE KEY id (id),
   KEY lockid (lockid),
   KEY moduleid (moduleid),
   KEY courseid (courseid)
 ) TYPE=MyISAM;
 
ALTER TABLE `prefix_course` ADD `showactivitytracking` SMALLINT( 2 ) UNSIGNED NOT NULL default '1' AFTER `showgrades`;

ALTER TABLE `prefix_course_modules` ADD `delay` VARCHAR( 10 ) NOT NULL AFTER `added`;

# --------------------------------------------------------

>> What do I need to change to the above ???

Thanks for the responses to my last posting, I figure I will get to learn more about how it works, when I finnally get it working wink

Mike.

In reply to Stuart Mayor

Re: New Hack: Activity Locking for 1.5

by Bernard Boucher -
Hi Stuart,
just one more comment.

For the quiz module the file attempt.php must also include your locking code call to enforce the locking mechanisim to those who try to acces the attempt directly with something like:

http://localhost/moodle15test/mod/quiz/attempt.php?q=1

Bye,

Bernard

In reply to Bernard Boucher

Re: New Hack: Activity Locking for 1.5

by Stuart Mayor -
Great. Thanks for noticing that one Bernard. I assume there'll be more "back doors" I've missed...
I'll include that one in the latest version.
In reply to Stuart Mayor

Re: New Hack: Activity Locking for 1.5

by Chardelle Busch -
Picture of Core developers
Thanks Stuart, I'll give this certificate a try. As for the mouseover, in Firefox, I do have a mouseover for the check and x, but not for the lock.

"how to switch off the "(predecessor)" that appears after an activity on another post"

Hmm, I don't remember that and I'm not sure why I would need to "hide" what the predecessor was, but somebody might want to--and this might be good if an activity has several predecessors--then all of the little boxes might get to be too much.
In reply to Chardelle Busch

Re: New Hack: Activity Locking for 1.5

by Chardelle Busch -
Picture of Core developers
Hi Stuart,

Got the certificate to work fine.  One thing, though, you are missing the sesskey, add: <input type="hidden" name="sesskey"    value="<?php p($form->sesskey) ?>" />

to the mod.html form.

Also, you still have some blank spaces (not lines) on line 78 in locklib.php which caused problems viewing the certificate.

As for the new lock code, I noticed you had a moodle.php lang file.  I was curious what you added there and why you added it to that file instead of the lock lang file?

Thanks so much for your work on this!
In reply to Chardelle Busch

Re: New Hack: Activity Locking for 1.5

by Stuart Mayor -
Thanks Chardelle.
The blank lines have been fixed in the latest version.
I'm not sure why I added the string to moodle.php rather than lock.php language file. I think I'll change it to how you suggest, so that installation doesn't involve overwriting an existing moodle.php.
In reply to Chardelle Busch

Re: New Hack: Activity Locking for 1.5

by H C -

Hi Chardelle:

I installed the moodle_certificate_for_1.5.zip done by Stuart. I added <input type="hidden" name=sesskey    value="<?php p($form->sesskey) ?>" /> to the mod.html but I cannot find the locklib.php file.

Also the certificate does not hide prior to taking the quiz.  I remember the previous Certificate version for Moodle 1.3 will be able to hide the icon before passing the quiz.

Is there a complete version that I can download for re-installation?  Thanks!

Helen

In reply to H C

Re: New Hack: Activity Locking for 1.5

by Chardelle Busch -
Picture of Core developers
Hi Helen,
Are you trying this in 1.5?  The locking code mentioned in this thread is really for Moodle 1.5 (and I just tried it an got errors, so it already needs some updating I think) and the locklib file is included in it.  If you are wanting to use the old activity linking code for 1.4 with the certificate, then a good place to get information on that is in the Moodle for Business course:

http://moodle.org/mod/forum/discuss.php?d=12421
In reply to Chardelle Busch

Re: New Hack: Activity Locking for 1.5

by H C -

Thank you Chardelle. This is the information I am looking for.  I have the Moodle 1.5 version installed and wish the certificate module with the locking function work for version 1.5.

Helen

In reply to Chardelle Busch

Re: New Hack: Activity Locking for 1.5

by Stuart Mayor -
Sorry Chardelle. It was Anna Ferguson in this post that asked about removing (predecessor) from a module link. blush