It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by The Bard In Green -
Number of replies: 12

No API functions, I'm working directly with the database outside of Moodle.

EDIT: Let me clarify a little:  I've written a script in bash that recreates quiz_attempts, question_attempts and question_attempt_steps, along with accompanying question_usages in order to import quiz results from csv files into Moodle.  This is all working great (after like 150 hours of work. . . never mind).

The ONLY thing that's not working is the quiz is marked as "not completed" from a student's point of view, which is preventing the student from downloading their certificate.  I have the state set to "finished" on the quiz_attempt.  What am I missing?

Average of ratings: -
In reply to The Bard In Green

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by The Bard In Green -

Well, it turns out it's NOT mdl_course_modules_completion.  I guess that would have been too easy.  It did mark the activity as complete, but the certificate module remains hidden.  So there must be an additional issue somewhere still.

In reply to The Bard In Green

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Yes - adding / updating a record in mdl_course_modules_completion is the correct place to mark an activity as 'complete'. All you then need to do is figure out a way of clearing the user's cached copy of the completion state. Possibly logging out and logging in again would achieve this, if you don't want to go down the much simpler route of using the API (why not write a simple PHP file that calls the API for you then call it via your script?).

If you do decide to do it properly, the function you want is called 'update_state' and can be found in lib/completionlib.php (you will need to construct a 'completion_info' instance to call this function on). 

In reply to Davo Smith

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by The Bard In Green -

Thanks very much for responding.  I appreciate it.

That's not it.  The certificate remains hidden even after clearing local cache and relogging in.  Something is getting set, somewhere else.

Yes, I could go the simple route and build something that calls the API, but at this point, I'm not that interested in that.  I've put a LOT of effort into understanding the Moodle database over the past few weeks and I want to understand this too.

As a DBA I have two (to me HUGE) complaints about Moodle, the first being that the API allows me to dev without understanding the database behind my work (which scares me) and the second being that the Moodle database has been built in a way that almost requires this (Like the architects for Moodle have either never heard of KISS, or think it's just a band).

In reply to The Bard In Green

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

The Bard in Green: "... the API allows me to dev without understanding the database behind my work (which scares me)"

As an amateur programmer, I would say that "the Moodle API allows me to dev without understanding the database behind my work (which I do appreciate)".wink

Joseph

In reply to Joseph Rézeau

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by Anne Krijger -

Hi Jospeh,

As a professional developer I can tell you that is exactly why there are API's.
Wanting to influence the behaviour of an application by poking around in the database layer is a DBA-ers choice, and from a developers POV not a good one at that smile

That's why the concept of a model was conceived; so you do not have to know, nor are restricted to a specific database level implementation.

Creating a better table structure, for example, is only possible if there is an API on top of it that will not change. Otherwise all the Moodle code out there would need to be rewritten. If the API stays the same, no code needs to be rewritten when the DB layer is changed.

So we need more API and less DB hacking, not the other way around smile

Anne.

In reply to Anne Krijger

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by The Bard In Green -

So I was able to solve this.  And I now understand the Moodle database pretty well. :D

In reply to The Bard In Green

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by María José López Monzón -

The Bard In Green, i have to import quiz results with grades and feedbacks into the database but i can't write in quiz_attempts neither in other tables could you please give me some advise? Thanks in advanced!!

In reply to María José López Monzón

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by The Bard In Green -

@María  What do you mean when you say you can't write in quiz_attempts?  Are you getting an SQL error?  I can tell you what tables you need to write to and how to structure your queries, but I don't know WHY you wouldn't be able to write to those tables. 

The most likely reason is you lack permissions.

@Joseph  That which you appreciate now maybe that which frustrates and annoys you in a few years when you no longer describe yourself as "amateur."  But if you don't understand the Moodle database, don't worry.  It's very hard to understand.  But take it from me, it doesn't have to be.  IMHO, this is a function of poorly thought out implementation and feature creep.

I firmly believe most of the important functionality of Moodle could be replicated with about half the number of tables, laid out in a much more intuitive manner.  However to do so would be an enormous undertaking that would require a group of talented people to put in many many hours.

In reply to The Bard In Green

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by The Bard In Green -

By playing around with the Certificate's activity settings, I have verified that changing it's settings in "Restrict Access" does NOT unhide it.  It stubbornly continues to say "Sorry, this activity is currently hidden."  So the question I asked at the beginning was, unfortunately, the WRONG question.  Well. . . back to square one.

In reply to The Bard In Green

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Have you checked logging out and back in or logging in with a different user to check that? I seem to remember (but could be remembering incorrectly) that the conditional access settings are set when a user logs in - hmm not sure, can't find the reference to look up and check where I thought I had that from, perhaps someone with more knowledge of the conditional activities can confirm/deny??

In reply to Richard Oelmann

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by The Bard In Green -

@Richard

Yeah, I've even checked by logging in on different computers where the user was never logged in.  That's not it.

Anyone know how Moodle checks if an activity is supposed to be hidden or not?

 

EDIT: Better question might be: When and how does the certificate module call $string['activityiscurrentlyhidden'].  A question I could probably answer myself, with clever use of grep.

EDIT2: Hrm.  According to grep, nothing in mod/certificate calls this string at all.  Something else is happening here.  Something closer to the Moodle core.

In reply to The Bard In Green

Re: It turns out I have a better question: How do I arbitrarily mark an activity as completed for a user with a MySQL string.

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The data about which activities are visible / hidden is cached within Moodle (to save doing several database queries for every course view). Up to Moodle 2.5 this is stored within the mdl_course table as the 'modinfo' and 'sectioninfo' fields (off the top of my head).

With Moodle 2.6, this is (I think) going to be moved into the MUC, so it will be stored in whichever caching mechanism you have specified for that particular type of cache.

So, the upshot is, if you are going to change the visibility of an activity via a change to the database, you are also going to have to clear the course cache (before Moodle 2.6, you can hack this by clearing the above mentioned fields in mdl_course; the right way to do this is to call the rebuild_course_cache($course) function).

Average of ratings: Useful (1)