5397 Commits

Author SHA1 Message Date
martinlanghoff
c7a8ec8cf0 accesslib: has_cap_fad() respect local-context-wins permissions rule
The initial implementation of has_cap_fad() just added the permission
values regardless of the locality of the context. This patch adds
support (read: fixes bug) for the "local context wins" rule.

Additionally, it removes a related bug where we were exiting early
if we found a CAP_PROHIBIT, ignoring the $doanything flag.
2007-09-19 07:52:06 +00:00
martinlanghoff
bbfed0ec61 datalib: get_my_courses() honour $fields array
If the caller passes a fields array, then merge it as we say we will do.
2007-09-19 07:51:36 +00:00
martinlanghoff
b9e9491a89 accesslib: get_my_courses() fix when called when empty sort
get_my_courses() was failing if called with an empty sortorder on a
cached courselist. Fix.
2007-09-19 07:51:19 +00:00
martinlanghoff
015e19287b Revert "textlib: Don't require_once() class.t3lib_div.php which we never use"
Turns out class.t3lib_cs.php depends on class.t3lib_div.php. Will drop
the two commits before we go into HEAD.

This reverts commit 5768bf6fb4dfa334dc81a80d26111904c4d13abd.
2007-09-19 07:49:49 +00:00
martinlanghoff
1dcf763428 accesslib: get_user_access_bycontext() remove 2 unused var declarations (trivial) 2007-09-19 07:49:35 +00:00
martinlanghoff
e89f157b0a datalib: get_courses_wmanagers() exclude the sitecourse
Should never return the sitecourse - so we exclude it explicitly.
2007-09-19 07:49:10 +00:00
martinlanghoff
987e7eb19a accesslib: is_siteadmin() bugfixes
Fixed two horrid bugs in is_siteadmin(). Both userid and the check for
matching cpabilities lines were wrong.
2007-09-19 07:48:43 +00:00
martinlanghoff
9f560a0ccb accesslib: fix SQL CAST()s to work on MySQL and PostgreSQL
CAST() target types aren't very portable. Use DECIMAL which works
for MySQL and Pg.

DECIMAL does seem to be supported in Oracle - but the syntax seems
different. We may still need a compat function.
2007-09-19 07:48:31 +00:00
martinlanghoff
9264620341 accesslib: remove has_capability_including_child_contexts()
No code is calling it, and it never appeared in any release.
2007-09-19 07:48:19 +00:00
martinlanghoff
0df35335ba moodlelib: isediting() uses $PAGE->user_allowed_editing(), drop editcourseallowed()
With this commit, isediting() checks with the page if the user is
allowed editing. And as the last caller of editcourseallowed() is gone,
remove it.
2007-09-19 07:48:03 +00:00
martinlanghoff
2e159efb1c Revert "datalib: Introducing sql_intarray_to_in()"
We don't need sql_intarray_to_in(), use implode() instead.

This reverts commit 3ef7fc54d9df780c837c64a8ace1fc565d6d7744.
2007-09-19 07:47:26 +00:00
martinlanghoff
41709a387f accesslib: replace join() and sql_intarray_to_in() with implode()
implode() is the PHP-ish way of saying this. join() is too Perlish and
may get confused (or misgrepped) with a SQL JOIN.
2007-09-19 07:47:11 +00:00
martinlanghoff
0c13a90844 textlib: Don't require_once() class.t3lib_div.php which we never use
The largest of the 2 typo3 libs we ship in Moodle is never used. So
don't even parse it.

This takes Moodle's "default homepage" from 1.8MB to 1.6MB of PHP.
Tiny, but every bit helps...
2007-09-19 07:46:54 +00:00
martinlanghoff
2a2057736e moodlelib: moodle_request_shutdown() prints included files
If MDL_PERFINC is defined, we now print to errorlog a listing
of the files included, their size, and then a total size.

The total size isn't the most important metric, though it does give us
a good idea of how much PHP the PHP engine is parsing for us. The main
cost is still in the seeks involved.

Even when using precompilers -- our best-case scenario -- each include
or require forces at least 2 stat()s to compare timestamps in the php
file vs the precompiled file. If the working set fits in buffers we are
fine, but our 60+ stat() calls per page is quite a bit.
2007-09-19 07:46:39 +00:00
martinlanghoff
c24f4599c3 accesslib rework: Upgrade block to populate context.path and friends
Note: this upgrade changes how accesslib works.
2007-09-19 07:42:42 +00:00
martinlanghoff
394074424a accesslib: Introducing is_siteadmin() to reliably check for siteadmins
is_siteadmin checks a few key capabilities to suss out if the user is
an admin. The main virtue of the function is that it does not use
the accesslib infrastructure -- it reads directly from the DB, which
is useful for the 1.9 accesslib upgrade.
2007-09-19 07:30:09 +00:00
martinlanghoff
d4bec85829 accesslib: get_user_courses_bycap() fix to deal with empty $order
If we are passed an empty string for $order, still create valid
SQL. Some callers in 1.9 seem to not care about order, passing
an explicit ''. Shocking! ;-)
2007-09-19 07:29:43 +00:00
martinlanghoff
1e460fdfc2 accesslib: remove references to deprecated context_rel table and insert_context_rel()
These references to the deprecated functions were erroring out. Remove
them.

Note however that other role related cleanups done as part of
MDL-10679 "improvement to context_rel table and load_user_capability()"
are kept.
2007-09-19 07:29:31 +00:00
martinlanghoff
ad4c7473a9 accesslib: build_context_path() is now much much cheaper
We now populate the context.path only where it's empty,
this means that we take 0.15s instead of 0.6s. More importantly,
we avoid thrashing the DB's indexes pointlessly.

We also support Oracle and its dirty hack here.

And the function now has a $force parameter that can be used to
actually overwrite the paths/depths in case they've been corrupted.
2007-09-19 07:29:07 +00:00
martinlanghoff
44f1636ee2 datalib: get_categories() - remove PostgreSQL-ism 2007-09-19 07:28:43 +00:00
martinlanghoff
a72921ae68 accesslib: Use sql_compat() to remove unportable PostgreSQL-isms 2007-09-19 07:28:31 +00:00
martinlanghoff
2dff3a0681 accesslib: get_assignable_roles() reworked to be constant-queries
get_assignable_roles() was calling user_can_assign() (cost of 1~2 DBq)
once-per-role. Instead, we can do a single DB query that answers
all our questions in one go.

On a Moodle w 8 roles defined, saves 19 DB queries for the course page
for teachers/admins.

NOTE NOTE NOTE! With this patch we drop the insane strip/escape bit.
Only the caller knows if this is for display on html or for other uses,
so we'll be true and not mangle the data.

A review of all callers in 1.8 shows no problem - the strings were being
strip/escaped already.
2007-09-19 07:27:46 +00:00
martinlanghoff
40fb8aa69a datalib: get_categories() returns context, supports !shallow
With this patch, get_categories() now adds a nice context
sub-object to the returned object, which means callers can
save DB traffic.

It now also supports "deep" retrievals, which means we can
rewrite the course categories display pages to avoid
costly recursion.
2007-09-19 07:27:20 +00:00
martinlanghoff
c5d13b689f datalib: fix_course_sortorder() - minor optimisation
Don't update fields unnecessarily. Cuts 3 DB queries per category
on course/index page (45 in a 15 category setup).

fix_course_sortorder() should be fixed to avoid recursion.
2007-09-19 07:27:08 +00:00
martinlanghoff
70f158789e datalib: Introducing get_courses_wmanagers() for print_courses() usage
For an efficient print_courses() we need to grab in a constant number
of queries...

 - course data
 - "course manager" role assignments
 - user records for the coursemanagers' fullname()

So here we do it in 2 DB queries. The 2nd one (grabbing RAs and user
records) can be expensive if we are dealing with a large number of
courses.

Which we shouldn't - When the number of courses is large the course
listing doesn't come this way anyway...
2007-09-19 07:26:42 +00:00
martinlanghoff
3cdf0dd253 accesslib: get_parent_contexts() bugfix 2007-09-19 07:26:15 +00:00
martinlanghoff
420bfab156 accesslib: has_capability() now loads sub-course accessdata for $ACCESS
When querying capabilities of non-logged-in users, has_capability()
will now load accessdata for the subcontexts as needed.

Without this patch, below-the-course RAs and rdefs were ignored when
checking caps for a user different from $USER. I don't think it is
ever done in current moodle code, so the problem wasn't visible.

In any case - it's fixed ;-)
2007-09-19 07:26:02 +00:00
martinlanghoff
21e2dcd946 moodlelib: require_login() rework
A bit of rework around require_login()

 - Fixed a subtle bug in the check whether a user can see sitecourse
   hidden activities

 - Save 1 DBq and 2 includes per call by only calling
   get_auth_plugin() only when needed.

 - Grab the contexts we are interested in only once and keep them in
   variables.
2007-09-19 07:25:49 +00:00
martinlanghoff
3c2dbf376e accesslib: Fix dirty paths checks on PHPv4
$DIRTYPATHS and its derivatives are all arrays. Not objects.

Drop the silly -> notation.
2007-09-19 07:25:37 +00:00
martinlanghoff
41017705c3 accesslib: phpdoc update get_child_contexts() 2007-09-19 07:25:25 +00:00
martinlanghoff
ad833c4283 accesslib: rm get_role_context_capability() and update phpdoc
Remove unused function.
2007-09-19 07:25:10 +00:00
martinlanghoff
7ac2bd55eb accesslib: remove role_add_lastaccess_entries(),role_remove_lastaccess_entries()
Both of them are dead code in 18_STABLE and HEAD. And if
role_add_lastaccess_entries() is ever used on a large site it will
hammer the DB to bits.

Remove before anyone is foolish enough to try it.
2007-09-19 07:24:57 +00:00
martinlanghoff
efd6fce585 accesslib: require_capability() phpdoc notes 2007-09-19 07:24:43 +00:00
martinlanghoff
0315aeb02f accesslib: get_parent_cats() goes away too...
Not used anymore, and very inefficient.

If you need something like it, use course_category.path
just like we use context.path .
2007-09-19 07:24:26 +00:00
martinlanghoff
8612e194e8 accesslib: remove get_role_caps() merge_role_caps()
Both unused, and not part of the external API.
2007-09-19 07:24:14 +00:00
martinlanghoff
03160d776c accesslib: remove validate_context()
No longer used anywhere. Not really useful outside of accesslib, so no
point in keeping it.
2007-09-19 07:24:02 +00:00
martinlanghoff
56743fabc9 accesslib: get_context_instance() - ignore clearcache
"cleacache" is no longer needed, and nothing is calling it.

Still - handle it as a noop to avoid erroring out with custom/contrib
code.
2007-09-19 07:23:41 +00:00
martinlanghoff
564870b7b8 accesslib: get_parent_contexts() reworked to use context.path
Now that we can use $context->path trivially, and avoid touching the db
for data we already have... Just Do It!
2007-09-19 07:23:30 +00:00
martinlanghoff
5f38222405 accesslib: remove insert_context_rel(), build_context_rel() and others
The context_rel table is not used anymore. Get rid of all references
to it.

TODO: Drop the orphaned table - but we'll do that on HEAD.
2007-09-19 07:23:15 +00:00
martinlanghoff
8e9fa6b1a2 accesslib: mark_context_dirty() - fix race condition
We had a 1s race condition where a user could get their rights loaded
at the exact time an admin is changing roles/caps and see the "old"
data. Or even see a half-updated view of the access controls.

Yuck.

So we fix the race condition backdating the dirtyness. Cheap, but
effective. And then we backdate it some more to cover for minor clock
flutter on clusters (you still need ntp however!).
2007-09-19 07:23:01 +00:00
martinlanghoff
16cb1ef759 accesslib: mark_context_dirty() don't mark it there is no path 2007-09-19 07:22:49 +00:00
martinlanghoff
2ee469b3b8 datalib: add_to_log() fix a bug and better error handling
All the $db->Execute calls now do proper err handling, and send their
message to debugging().
2007-09-19 07:22:37 +00:00
martinlanghoff
d4c4ecb808 accesslib: get_user_access_sitewide() fix invalid SQL for users without RAs 2007-09-19 07:22:12 +00:00
martinlanghoff
2965f8fdb8 datalib: get_admin() - cache in a static var
get_admin is called a zillion times during cron, and is a tiny
bit of data we can safely keep around... so...

(shaves 2 queries per email we send!)
2007-09-19 07:22:01 +00:00
martinlanghoff
b00cb46b27 datalib: get_my_courses() now returns invisible courses if visible to $USER
So admins can see the appropriate user listing in the profile page.

In fact, probably _all_ the visibility checks should be done by callers.
Hmmm...
2007-09-19 07:21:35 +00:00
martinlanghoff
62a7a32d79 accesslib: has_capability() - fix warnings about FULLME constant 2007-09-19 07:21:23 +00:00
martinlanghoff
1a9b67874f accesslib: has_capability() now supports fake $USER for forum cron
has_capability() can handle the fake user that forum cron sets up
and will load the appropriate accessdata into $USER->access.

This makes forum cron work again. A test comparison between before
this patchseries yields:

With 1 forum post, sent total 24 times
 - Before 11 000 DB queries (approx)
 - After     506 DB queries

With 6 forum posts, sent a total of 452 times
 - Before 47 876 DB queries
 - After   8 256 DB queries

There is a very high variability, but we are going from 100-500
queries per sent email to 18-21 queries per sent email. The
variability probably stems from 2 of the 6 posts being in a 200-user
forum.

Still huge - by the time we are sending the email, we should know
everything we need to know about the user, the forum/thread/post and
the form. The average should be well below 1 DB query per email sent!
2007-09-19 07:20:24 +00:00
martinlanghoff
aebfa1546e weblib: print_user()/print_user_picture() optimisations
print_user_picture() was forcing an unneeded dbquery
if you need an imagealt. And who doesn't need one these days.

So - teach print_user_picture() to take either $userid
_or_ $userobj as the first parameter. If that first
parameter has the fields we need, never touch the db.

In other words, only touch the DB as a last resort.

There is a bit of ugliness in testing whether we have
the fields or not, because these fields are inconsistently
with/without NOT NULL in the DB definitions. So we cannot
use isset() because it barfs on nulls. And we cannot use empty()
because it will match both on "missing key" and ''.

And while at it, silence warnings that we are missing string
for the year(s). Also fixes a missing string bug in really boring
courses that noone's visitied in many years ;-)
2007-09-19 07:19:35 +00:00
martinlanghoff
1cf2e21b6e moodlelib: require_login() - rework course/category visibility checks
Reworked the logic of the visibility checks so that we evaluate in a
chain:

   - can view course   (visible or user can see hidden)
   - can view category (visible or user can see hidden)

Without this patch, users that could see hidden categories could not get
into courses inside of them.

While at it, fix reference to the old $USER->switchrole
2007-09-19 07:19:20 +00:00
martinlanghoff
3511647c44 datalib:course_parent_visible() rework, remove category_parent_visible()
Reworked course_parent_visible() to always return in a constant
number of db queries (2 worst case) regardless of nesting depth.

The rewritten version has a small cache, but if you are going to
walk many courses, it's still 1~2 DB queries per category seen,
so the right thing to do is to check it in the caller, as seen
in get_my_courses().
2007-09-19 07:19:05 +00:00