models.py 12.9 KB
Newer Older
1 2 3 4 5 6
"""
WE'RE USING MIGRATIONS!

If you make changes to this model, be sure to create an appropriate migration
file and check it in at the same time as your model changes. To do that,

7
1. Go to the edx-platform dir
8
2. ./manage.py schemamigration courseware --auto description_of_your_change
9
3. Add the migration file created in edx-platform/lms/djangoapps/courseware/migrations/
10

11 12 13

ASSUMPTIONS: modules have unique IDs, even across different module_types

14
"""
15
import itertools
16
import logging
17

18
from django.conf import settings
19
from django.contrib.auth.models import User
20 21
from django.db import models
from django.db.models.signals import post_save
22 23
from model_utils.models import TimeStampedModel

24 25
import coursewarehistoryextended
from openedx.core.djangoapps.xmodule_django.models import BlockTypeKeyField, CourseKeyField, LocationKeyField
26

27 28
log = logging.getLogger("edx.courseware")

29

30 31 32 33 34 35 36 37 38 39 40 41 42
def chunks(items, chunk_size):
    """
    Yields the values from items in chunks of size chunk_size
    """
    items = list(items)
    return (items[i:i + chunk_size] for i in xrange(0, len(items), chunk_size))


class ChunkingManager(models.Manager):
    """
    :class:`~Manager` that adds an additional method :meth:`chunked_filter` to provide
    the ability to make select queries with specific chunk sizes.
    """
43 44 45
    class Meta(object):
        app_label = "courseware"

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    def chunked_filter(self, chunk_field, items, **kwargs):
        """
        Queries model_class with `chunk_field` set to chunks of size `chunk_size`,
        and all other parameters from `**kwargs`.

        This works around a limitation in sqlite3 on the number of parameters
        that can be put into a single query.

        Arguments:
            chunk_field (str): The name of the field to chunk the query on.
            items: The values for of chunk_field to select. This is chunked into ``chunk_size``
                chunks, and passed as the value for the ``chunk_field`` keyword argument to
                :meth:`~Manager.filter`. This implies that ``chunk_field`` should be an
                ``__in`` key.
            chunk_size (int): The size of chunks to pass. Defaults to 500.
        """
        chunk_size = kwargs.pop('chunk_size', 500)
        res = itertools.chain.from_iterable(
            self.filter(**dict([(chunk_field, chunk)] + kwargs.items()))
            for chunk in chunks(items, chunk_size)
        )
        return res


70
class StudentModule(models.Model):
71 72 73
    """
    Keeps student state for a particular module in a particular course.
    """
74
    objects = ChunkingManager()
75 76
    MODEL_TAGS = ['course_id', 'module_type']

Piotr Mitros committed
77 78
    # For a homework problem, contains a JSON
    # object consisting of state
79 80 81
    MODULE_TYPES = (('problem', 'problem'),
                    ('video', 'video'),
                    ('html', 'html'),
82 83 84
                    ('course', 'course'),
                    ('chapter', 'Section'),
                    ('sequential', 'Subsection'),
85
                    ('library_content', 'Library Content'))
86
    ## These three are the key for the object
87
    module_type = models.CharField(max_length=32, choices=MODULE_TYPES, default='problem', db_index=True)
88

89
    # Key used to share state. This is the XBlock usage_id
90
    module_state_key = LocationKeyField(max_length=255, db_index=True, db_column='module_id')
91
    student = models.ForeignKey(User, db_index=True)
92

93
    course_id = CourseKeyField(max_length=255, db_index=True)
94

95
    class Meta(object):
96
        app_label = "courseware"
97
        unique_together = (('student', 'module_state_key', 'course_id'),)
Ernie Park committed
98

utkjad committed
99
    # Internal state of the object
Piotr Mitros committed
100
    state = models.TextField(null=True, blank=True)
101

utkjad committed
102
    # Grade, and are we done?
103
    grade = models.FloatField(null=True, blank=True, db_index=True)
104
    max_grade = models.FloatField(null=True, blank=True)
105 106 107 108 109
    DONE_TYPES = (
        ('na', 'NOT_APPLICABLE'),
        ('f', 'FINISHED'),
        ('i', 'INCOMPLETE'),
    )
110 111
    done = models.CharField(max_length=8, choices=DONE_TYPES, default='na', db_index=True)

112 113
    created = models.DateTimeField(auto_now_add=True, db_index=True)
    modified = models.DateTimeField(auto_now=True, db_index=True)
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    @classmethod
    def all_submitted_problems_read_only(cls, course_id):
        """
        Return all model instances that correspond to problems that have been
        submitted for a given course. So module_type='problem' and a non-null
        grade. Use a read replica if one exists for this environment.
        """
        queryset = cls.objects.filter(
            course_id=course_id,
            module_type='problem',
            grade__isnull=False
        )
        if "read_replica" in settings.DATABASES:
            return queryset.using("read_replica")
        else:
            return queryset

132
    def __repr__(self):
133 134 135
        return 'StudentModule<%r>' % ({
            'course_id': self.course_id,
            'module_type': self.module_type,
136 137 138
            # We use the student_id instead of username to avoid a database hop.
            # This can actually matter in cases where we're logging many of
            # these (e.g. on a broken progress page).
139
            'student_id': self.student_id,
140 141 142
            'module_state_key': self.module_state_key,
            'state': str(self.state)[:20],
        },)
143

144 145
    def __unicode__(self):
        return unicode(repr(self))
146

147

148 149 150
class BaseStudentModuleHistory(models.Model):
    """Abstract class containing most fields used by any class
    storing Student Module History"""
151
    objects = ChunkingManager()
152 153
    HISTORY_SAVING_TYPES = {'problem'}

154
    class Meta(object):
155
        abstract = True
156 157 158 159 160 161 162 163 164

    version = models.CharField(max_length=255, null=True, blank=True, db_index=True)

    # This should be populated from the modified field in StudentModule
    created = models.DateTimeField(db_index=True)
    state = models.TextField(null=True, blank=True)
    grade = models.FloatField(null=True, blank=True)
    max_grade = models.FloatField(null=True, blank=True)

165 166
    @property
    def csm(self):
167
        """
168 169 170
        Finds the StudentModule object for this history record, even if our data is split
        across multiple data stores.  Django does not handle this correctly with the built-in
        student_module property.
171
        """
172
        return StudentModule.objects.get(pk=self.student_module_id)
173

174 175 176 177 178 179 180
    @staticmethod
    def get_history(student_modules):
        """
        Find history objects across multiple backend stores for a given StudentModule
        """

        history_entries = []
181

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        if settings.FEATURES.get('ENABLE_CSMH_EXTENDED'):
            history_entries += coursewarehistoryextended.models.StudentModuleHistoryExtended.objects.filter(
                # Django will sometimes try to join to courseware_studentmodule
                # so just do an in query
                student_module__in=[module.id for module in student_modules]
            ).order_by('-id')

        # If we turn off reading from multiple history tables, then we don't want to read from
        # StudentModuleHistory anymore, we believe that all history is in the Extended table.
        if settings.FEATURES.get('ENABLE_READING_FROM_MULTIPLE_HISTORY_TABLES'):
            # we want to save later SQL queries on the model which allows us to prefetch
            history_entries += StudentModuleHistory.objects.prefetch_related('student_module').filter(
                student_module__in=student_modules
            ).order_by('-id')

        return history_entries


class StudentModuleHistory(BaseStudentModuleHistory):
201 202
    """Keeps a complete history of state changes for a given XModule for a given
    Student. Right now, we restrict this to problems so that the table doesn't
203
    explode in size."""
204 205 206 207 208

    class Meta(object):
        app_label = "courseware"
        get_latest_by = "created"

209
    student_module = models.ForeignKey(StudentModule, db_index=True)
210

211 212
    def __unicode__(self):
        return unicode(repr(self))
213 214 215 216

    def save_history(sender, instance, **kwargs):  # pylint: disable=no-self-argument, unused-argument
        """
        Checks the instance's module_type, and creates & saves a
217
        StudentModuleHistoryExtended entry if the module_type is one that
218 219
        we save.
        """
220 221
        if instance.module_type in StudentModuleHistory.HISTORY_SAVING_TYPES:
            history_entry = StudentModuleHistory(student_module=instance,
222 223 224
                                                 version=None,
                                                 created=instance.modified,
                                                 state=instance.state,
225 226 227 228
                                                 grade=instance.grade,
                                                 max_grade=instance.max_grade)
            history_entry.save()

229 230 231 232 233
    # When the extended studentmodulehistory table exists, don't save
    # duplicate history into courseware_studentmodulehistory, just retain
    # data for reading.
    if not settings.FEATURES.get('ENABLE_CSMH_EXTENDED'):
        post_save.connect(save_history, sender=StudentModule)
234

235

236
class XBlockFieldBase(models.Model):
237
    """
238
    Base class for all XBlock field storage.
239
    """
240 241
    objects = ChunkingManager()

242
    class Meta(object):
243
        app_label = "courseware"
244
        abstract = True
245 246 247 248

    # The name of the field
    field_name = models.CharField(max_length=64, db_index=True)

Calen Pennington committed
249
    # The value of the field. Defaults to None dumped as json
250 251 252 253 254 255
    value = models.TextField(default='null')

    created = models.DateTimeField(auto_now_add=True, db_index=True)
    modified = models.DateTimeField(auto_now=True, db_index=True)

    def __unicode__(self):
256 257 258
        return u'{}<{!r}'.format(
            self.__class__.__name__,
            {
259
                key: getattr(self, key)
260 261 262 263 264 265 266 267 268 269
                for key in self._meta.get_all_field_names()
                if key not in ('created', 'modified')
            }
        )


class XModuleUserStateSummaryField(XBlockFieldBase):
    """
    Stores data set in the Scope.user_state_summary scope by an xmodule field
    """
270
    class Meta(object):
271
        app_label = "courseware"
272 273 274 275
        unique_together = (('usage_id', 'field_name'),)

    # The definition id for the module
    usage_id = LocationKeyField(max_length=255, db_index=True)
276 277


278
class XModuleStudentPrefsField(XBlockFieldBase):
279
    """
280
    Stores data set in the Scope.preferences scope by an xmodule field
281
    """
282
    class Meta(object):
283
        app_label = "courseware"
284 285 286
        unique_together = (('student', 'module_type', 'field_name'),)

    # The type of the module for these preferences
287
    module_type = BlockTypeKeyField(max_length=64, db_index=True)
288 289 290 291

    student = models.ForeignKey(User, db_index=True)


292
class XModuleStudentInfoField(XBlockFieldBase):
293
    """
294
    Stores data set in the Scope.preferences scope by an xmodule field
295
    """
296
    class Meta(object):
297
        app_label = "courseware"
298
        unique_together = (('student', 'field_name'),)
299

300 301
    student = models.ForeignKey(User, db_index=True)

302 303 304 305 306 307

class OfflineComputedGrade(models.Model):
    """
    Table of grades computed offline for a given user and course.
    """
    user = models.ForeignKey(User, db_index=True)
308
    course_id = CourseKeyField(max_length=255, db_index=True)
309 310 311 312 313 314

    created = models.DateTimeField(auto_now_add=True, null=True, db_index=True)
    updated = models.DateTimeField(auto_now=True, db_index=True)

    gradeset = models.TextField(null=True, blank=True)		# grades, stored as JSON

315
    class Meta(object):
316
        app_label = "courseware"
317 318 319 320 321 322 323 324 325 326 327
        unique_together = (('user', 'course_id'), )

    def __unicode__(self):
        return "[OfflineComputedGrade] %s: %s (%s) = %s" % (self.user, self.course_id, self.created, self.gradeset)


class OfflineComputedGradeLog(models.Model):
    """
    Log of when offline grades are computed.
    Use this to be able to show instructor when the last computed grades were done.
    """
328
    class Meta(object):
329
        app_label = "courseware"
330 331 332
        ordering = ["-created"]
        get_latest_by = "created"

333
    course_id = CourseKeyField(max_length=255, db_index=True)
334
    created = models.DateTimeField(auto_now_add=True, null=True, db_index=True)
Calen Pennington committed
335
    seconds = models.IntegerField(default=0)  	# seconds elapsed for computation
336 337 338
    nstudents = models.IntegerField(default=0)

    def __unicode__(self):
339
        return "[OCGLog] %s: %s" % (self.course_id.to_deprecated_string(), self.created)  # pylint: disable=no-member
340 341


342
class StudentFieldOverride(TimeStampedModel):
343 344 345 346 347 348 349 350 351
    """
    Holds the value of a specific field overriden for a student.  This is used
    by the code in the `courseware.student_field_overrides` module to provide
    overrides of xblock fields on a per user basis.
    """
    course_id = CourseKeyField(max_length=255, db_index=True)
    location = LocationKeyField(max_length=255, db_index=True)
    student = models.ForeignKey(User, db_index=True)

352
    class Meta(object):
353
        app_label = "courseware"
354
        unique_together = (('course_id', 'field', 'location', 'student'),)
355 356 357

    field = models.CharField(max_length=255)
    value = models.TextField(default='null')